2021-07-14 18:43:19 +00:00
|
|
|
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"
|
2021-07-14 18:43:19 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
func SetCookie(gc *gin.Context, token string) {
|
|
|
|
secure := true
|
|
|
|
httpOnly := true
|
2021-12-21 13:16:54 +00:00
|
|
|
host, _ := GetHostParts(constants.AUTHORIZER_URL)
|
2021-12-11 01:11:35 +00:00
|
|
|
domain := GetDomainName(constants.AUTHORIZER_URL)
|
|
|
|
if domain != "localhost" {
|
|
|
|
domain = "." + domain
|
|
|
|
}
|
2021-12-07 12:20:50 +00:00
|
|
|
|
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)
|
2021-12-11 01:11:35 +00:00
|
|
|
gc.SetCookie(constants.COOKIE_NAME+"-client", token, 3600, "/", domain, secure, httpOnly)
|
2021-07-14 18:43:19 +00:00
|
|
|
}
|
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 {
|
2021-12-11 01:15:15 +00:00
|
|
|
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
|
|
|
|
|
2021-12-21 13:16:54 +00:00
|
|
|
host, _ := GetHostParts(constants.AUTHORIZER_URL)
|
2021-12-11 01:11:35 +00:00
|
|
|
domain := GetDomainName(constants.AUTHORIZER_URL)
|
|
|
|
if domain != "localhost" {
|
|
|
|
domain = "." + domain
|
|
|
|
}
|
2021-12-07 12:20:50 +00:00
|
|
|
|
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)
|
2021-12-11 01:11:35 +00:00
|
|
|
gc.SetCookie(constants.COOKIE_NAME+"-client", "", -1, "/", domain, secure, httpOnly)
|
2021-07-15 09:43:00 +00:00
|
|
|
}
|