authorizer/server/utils/cookie.go

50 lines
1.2 KiB
Go
Raw Normal View History

package utils
import (
2021-07-22 00:54:15 +00:00
"net/http"
2021-07-23 16:27:44 +00:00
"github.com/authorizerdev/authorizer/server/constants"
"github.com/gin-gonic/gin"
)
func SetCookie(gc *gin.Context, token string) {
secure := true
httpOnly := true
host, _ := GetHostParts(constants.AUTHORIZER_URL)
domain := GetDomainName(constants.AUTHORIZER_URL)
if domain != "localhost" {
domain = "." + domain
}
2021-07-22 00:54:15 +00:00
gc.SetSameSite(http.SameSiteNoneMode)
2021-07-28 18:23:54 +00:00
gc.SetCookie(constants.COOKIE_NAME, token, 3600, "/", host, secure, httpOnly)
gc.SetCookie(constants.COOKIE_NAME+"-client", token, 3600, "/", domain, secure, httpOnly)
}
2021-07-15 09:43:00 +00:00
2021-07-27 12:16:02 +00:00
func GetCookie(gc *gin.Context) (string, error) {
cookie, err := gc.Request.Cookie(constants.COOKIE_NAME)
if err != nil {
cookie, err = gc.Request.Cookie(constants.COOKIE_NAME + "-client")
if err != nil {
return "", err
}
2021-07-27 12:16:02 +00:00
}
return cookie.Value, nil
}
2021-07-15 09:43:00 +00:00
func DeleteCookie(gc *gin.Context) {
secure := true
httpOnly := true
host, _ := GetHostParts(constants.AUTHORIZER_URL)
domain := GetDomainName(constants.AUTHORIZER_URL)
if domain != "localhost" {
domain = "." + domain
}
2021-07-22 00:54:15 +00:00
gc.SetSameSite(http.SameSiteNoneMode)
2021-07-28 18:23:54 +00:00
gc.SetCookie(constants.COOKIE_NAME, "", -1, "/", host, secure, httpOnly)
gc.SetCookie(constants.COOKIE_NAME+"-client", "", -1, "/", domain, secure, httpOnly)
2021-07-15 09:43:00 +00:00
}