2020-01-10 07:55:30 -06:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CookieOptions struct {
|
2020-01-14 10:41:54 -06:00
|
|
|
Path string
|
|
|
|
Secure bool
|
|
|
|
SameSiteDisabled bool
|
|
|
|
SameSiteMode http.SameSite
|
2020-01-10 07:55:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func newCookieOptions() CookieOptions {
|
2020-04-06 09:56:19 -05:00
|
|
|
path := "/"
|
|
|
|
if len(setting.AppSubUrl) > 0 {
|
|
|
|
path = setting.AppSubUrl
|
|
|
|
}
|
2020-01-10 07:55:30 -06:00
|
|
|
return CookieOptions{
|
2020-04-06 09:56:19 -05:00
|
|
|
Path: path,
|
2020-01-14 10:41:54 -06:00
|
|
|
Secure: setting.CookieSecure,
|
|
|
|
SameSiteDisabled: setting.CookieSameSiteDisabled,
|
|
|
|
SameSiteMode: setting.CookieSameSiteMode,
|
2020-01-10 07:55:30 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type GetCookieOptionsFunc func() CookieOptions
|
|
|
|
|
|
|
|
func DeleteCookie(w http.ResponseWriter, name string, getCookieOptionsFunc GetCookieOptionsFunc) {
|
|
|
|
WriteCookie(w, name, "", -1, getCookieOptionsFunc)
|
|
|
|
}
|
|
|
|
|
|
|
|
func WriteCookie(w http.ResponseWriter, name string, value string, maxAge int, getCookieOptionsFunc GetCookieOptionsFunc) {
|
|
|
|
options := getCookieOptionsFunc()
|
|
|
|
cookie := http.Cookie{
|
|
|
|
Name: name,
|
|
|
|
MaxAge: maxAge,
|
|
|
|
Value: value,
|
|
|
|
HttpOnly: true,
|
|
|
|
Path: options.Path,
|
|
|
|
Secure: options.Secure,
|
|
|
|
}
|
2020-01-14 10:41:54 -06:00
|
|
|
if !options.SameSiteDisabled {
|
|
|
|
cookie.SameSite = options.SameSiteMode
|
2020-01-10 07:55:30 -06:00
|
|
|
}
|
|
|
|
http.SetCookie(w, &cookie)
|
|
|
|
}
|