authorizer/server/cookie/cookie.go

89 lines
2.8 KiB
Go
Raw Normal View History

package cookie
import (
"net/http"
2022-03-04 07:26:11 +00:00
"net/url"
log "github.com/sirupsen/logrus"
"github.com/authorizerdev/authorizer/server/constants"
"github.com/authorizerdev/authorizer/server/memorystore"
2022-05-30 06:24:16 +00:00
"github.com/authorizerdev/authorizer/server/parsers"
"github.com/gin-gonic/gin"
)
2022-03-02 12:12:31 +00:00
// SetSession sets the session cookie in the response
func SetSession(gc *gin.Context, sessionID string) {
appCookieSecure, err := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyAppCookieSecure)
if err != nil {
log.Debug("Error while getting app cookie secure from env variable: %v", err)
appCookieSecure = true
}
secure := appCookieSecure
httpOnly := appCookieSecure
2022-05-30 06:24:16 +00:00
hostname := parsers.GetHost(gc)
host, _ := parsers.GetHostParts(hostname)
domain := parsers.GetDomainName(hostname)
2022-02-28 15:56:49 +00:00
if domain != "localhost" {
domain = "." + domain
}
// Use sameSite = lax by default
// Since app cookie can come from cross site it becomes important to set this in lax mode.
// Example person using custom UI on their app domain and making request to authorizer domain.
// For more information check:
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite
// https://github.com/gin-gonic/gin/blob/master/context.go#L86
// TODO add ability to sameSite = none / strict from dashboard
2022-09-28 13:00:30 +00:00
if !appCookieSecure {
gc.SetSameSite(http.SameSiteLaxMode)
}
2022-02-28 15:56:49 +00:00
// TODO allow configuring from dashboard
year := 60 * 60 * 24 * 365
2022-05-29 11:52:46 +00:00
gc.SetCookie(constants.AppCookieName+"_session", sessionID, year, "/", host, secure, httpOnly)
gc.SetCookie(constants.AppCookieName+"_session_domain", sessionID, year, "/", domain, secure, httpOnly)
2022-02-28 15:56:49 +00:00
}
2022-03-02 12:12:31 +00:00
// DeleteSession sets session cookies to expire
func DeleteSession(gc *gin.Context) {
appCookieSecure, err := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyAppCookieSecure)
if err != nil {
log.Debug("Error while getting app cookie secure from env variable: %v", err)
appCookieSecure = true
}
secure := appCookieSecure
httpOnly := appCookieSecure
2022-05-30 06:24:16 +00:00
hostname := parsers.GetHost(gc)
host, _ := parsers.GetHostParts(hostname)
domain := parsers.GetDomainName(hostname)
if domain != "localhost" {
domain = "." + domain
}
gc.SetSameSite(http.SameSiteNoneMode)
2022-05-29 11:52:46 +00:00
gc.SetCookie(constants.AppCookieName+"_session", "", -1, "/", host, secure, httpOnly)
gc.SetCookie(constants.AppCookieName+"_session_domain", "", -1, "/", domain, secure, httpOnly)
}
2022-03-02 12:12:31 +00:00
// GetSession gets the session cookie from context
func GetSession(gc *gin.Context) (string, error) {
var cookie *http.Cookie
var err error
2022-05-29 11:52:46 +00:00
cookie, err = gc.Request.Cookie(constants.AppCookieName + "_session")
if err != nil {
2022-05-29 11:52:46 +00:00
cookie, err = gc.Request.Cookie(constants.AppCookieName + "_session_domain")
if err != nil {
2022-03-03 03:51:48 +00:00
return "", err
}
}
2022-03-04 07:26:11 +00:00
decodedValue, err := url.PathUnescape(cookie.Value)
if err != nil {
return "", err
}
return decodedValue, nil
}