|
| 1 | +package cookie |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/url" |
| 6 | + |
| 7 | + log "github.com/sirupsen/logrus" |
| 8 | + |
| 9 | + "github.com/authorizerdev/authorizer/server/constants" |
| 10 | + "github.com/authorizerdev/authorizer/server/memorystore" |
| 11 | + "github.com/authorizerdev/authorizer/server/parsers" |
| 12 | + "github.com/gin-gonic/gin" |
| 13 | +) |
| 14 | + |
| 15 | +// SetMfaSession sets the mfa session cookie in the response |
| 16 | +func SetMfaSession(gc *gin.Context, sessionID string) { |
| 17 | + appCookieSecure, err := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyAppCookieSecure) |
| 18 | + if err != nil { |
| 19 | + log.Debug("Error while getting app cookie secure from env variable: %v", err) |
| 20 | + appCookieSecure = true |
| 21 | + } |
| 22 | + |
| 23 | + secure := appCookieSecure |
| 24 | + httpOnly := appCookieSecure |
| 25 | + hostname := parsers.GetHost(gc) |
| 26 | + host, _ := parsers.GetHostParts(hostname) |
| 27 | + domain := parsers.GetDomainName(hostname) |
| 28 | + if domain != "localhost" { |
| 29 | + domain = "." + domain |
| 30 | + } |
| 31 | + |
| 32 | + // Since app cookie can come from cross site it becomes important to set this in lax mode when insecure. |
| 33 | + // Example person using custom UI on their app domain and making request to authorizer domain. |
| 34 | + // For more information check: |
| 35 | + // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite |
| 36 | + // https://github.com/gin-gonic/gin/blob/master/context.go#L86 |
| 37 | + // TODO add ability to sameSite = none / strict from dashboard |
| 38 | + if !appCookieSecure { |
| 39 | + gc.SetSameSite(http.SameSiteLaxMode) |
| 40 | + } else { |
| 41 | + gc.SetSameSite(http.SameSiteNoneMode) |
| 42 | + } |
| 43 | + // TODO allow configuring from dashboard |
| 44 | + age := 60 |
| 45 | + |
| 46 | + gc.SetCookie(constants.MfaCookieName+"_session", sessionID, age, "/", host, secure, httpOnly) |
| 47 | + gc.SetCookie(constants.MfaCookieName+"_session_domain", sessionID, age, "/", domain, secure, httpOnly) |
| 48 | +} |
| 49 | + |
| 50 | +// DeleteMfaSession deletes the mfa session cookies to expire |
| 51 | +func DeleteMfaSession(gc *gin.Context) { |
| 52 | + appCookieSecure, err := memorystore.Provider.GetBoolStoreEnvVariable(constants.EnvKeyAppCookieSecure) |
| 53 | + if err != nil { |
| 54 | + log.Debug("Error while getting app cookie secure from env variable: %v", err) |
| 55 | + appCookieSecure = true |
| 56 | + } |
| 57 | + |
| 58 | + secure := appCookieSecure |
| 59 | + httpOnly := appCookieSecure |
| 60 | + hostname := parsers.GetHost(gc) |
| 61 | + host, _ := parsers.GetHostParts(hostname) |
| 62 | + domain := parsers.GetDomainName(hostname) |
| 63 | + if domain != "localhost" { |
| 64 | + domain = "." + domain |
| 65 | + } |
| 66 | + |
| 67 | + gc.SetSameSite(http.SameSiteNoneMode) |
| 68 | + gc.SetCookie(constants.MfaCookieName+"_session", "", -1, "/", host, secure, httpOnly) |
| 69 | + gc.SetCookie(constants.MfaCookieName+"_session_domain", "", -1, "/", domain, secure, httpOnly) |
| 70 | +} |
| 71 | + |
| 72 | +// GetMfaSession gets the mfa session cookie from context |
| 73 | +func GetMfaSession(gc *gin.Context) (string, error) { |
| 74 | + var cookie *http.Cookie |
| 75 | + var err error |
| 76 | + cookie, err = gc.Request.Cookie(constants.MfaCookieName + "_session") |
| 77 | + if err != nil { |
| 78 | + cookie, err = gc.Request.Cookie(constants.MfaCookieName + "_session_domain") |
| 79 | + if err != nil { |
| 80 | + return "", err |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + decodedValue, err := url.PathUnescape(cookie.Value) |
| 85 | + if err != nil { |
| 86 | + return "", err |
| 87 | + } |
| 88 | + return decodedValue, nil |
| 89 | +} |
0 commit comments