mirror of
https://github.com/grafana/grafana.git
synced 2024-12-02 13:39:19 -06:00
382b24742a
* FeatureToggle: Add toggle to use a new way of rotating tokens * API: Add endpoints to perform token rotation, one endpoint for api request and one endpoint for redirectsd * Auth: Aling not authorized handling between auth middleware and access control middleware * API: add utility function to get redirect for login * API: Handle token rotation redirect for login page * Frontend: Add job scheduling for token rotation and make call to token rotation as fallback in retry request * ContextHandler: Prevent in-request rotation if feature flag is enabled and check if token needs to be rotated * AuthN: Prevent in-request rotation if feature flag is enabled and check if token needs to be rotated * Cookies: Add option NotHttpOnly * AuthToken: Add helper function to get next rotation time and another function to check if token need to be rotated * AuthN: Add function to delete session cookie and set expiry cookie Co-authored-by: Ieva <ieva.vasiljeva@grafana.com>
55 lines
1.2 KiB
Go
55 lines
1.2 KiB
Go
package cookies
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
)
|
|
|
|
type CookieOptions struct {
|
|
NotHttpOnly bool
|
|
Path string
|
|
Secure bool
|
|
SameSiteDisabled bool
|
|
SameSiteMode http.SameSite
|
|
}
|
|
|
|
func NewCookieOptions() CookieOptions {
|
|
path := "/"
|
|
if len(setting.AppSubUrl) > 0 {
|
|
path = setting.AppSubUrl
|
|
}
|
|
return CookieOptions{
|
|
Path: path,
|
|
Secure: setting.CookieSecure,
|
|
SameSiteDisabled: setting.CookieSameSiteDisabled,
|
|
SameSiteMode: setting.CookieSameSiteMode,
|
|
}
|
|
}
|
|
|
|
type getCookieOptionsFunc func() CookieOptions
|
|
|
|
func DeleteCookie(w http.ResponseWriter, name string, getCookieOptions getCookieOptionsFunc) {
|
|
WriteCookie(w, name, "", -1, getCookieOptions)
|
|
}
|
|
|
|
func WriteCookie(w http.ResponseWriter, name string, value string, maxAge int, getCookieOptions getCookieOptionsFunc) {
|
|
if getCookieOptions == nil {
|
|
getCookieOptions = NewCookieOptions
|
|
}
|
|
|
|
options := getCookieOptions()
|
|
cookie := http.Cookie{
|
|
Name: name,
|
|
MaxAge: maxAge,
|
|
Value: value,
|
|
HttpOnly: !options.NotHttpOnly,
|
|
Path: options.Path,
|
|
Secure: options.Secure,
|
|
}
|
|
if !options.SameSiteDisabled {
|
|
cookie.SameSite = options.SameSiteMode
|
|
}
|
|
http.SetCookie(w, &cookie)
|
|
}
|