2022-01-22 19:54:41 +00:00
|
|
|
package cookie
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
|
|
|
"github.com/authorizerdev/authorizer/server/envstore"
|
|
|
|
"github.com/authorizerdev/authorizer/server/utils"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SetAdminCookie sets the admin cookie in the response
|
|
|
|
func SetAdminCookie(gc *gin.Context, token string) {
|
|
|
|
secure := true
|
|
|
|
httpOnly := true
|
2022-01-31 06:05:24 +00:00
|
|
|
hostname := utils.GetHost(gc)
|
|
|
|
host, _ := utils.GetHostParts(hostname)
|
2022-01-22 19:54:41 +00:00
|
|
|
|
2022-02-28 02:25:01 +00:00
|
|
|
gc.SetCookie(envstore.EnvStoreObj.GetStringStoreEnvVariable(constants.EnvKeyAdminCookieName), token, 3600, "/", host, secure, httpOnly)
|
2022-01-22 19:54:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetAdminCookie gets the admin cookie from the request
|
|
|
|
func GetAdminCookie(gc *gin.Context) (string, error) {
|
2022-02-28 02:25:01 +00:00
|
|
|
cookie, err := gc.Request.Cookie(envstore.EnvStoreObj.GetStringStoreEnvVariable(constants.EnvKeyAdminCookieName))
|
2022-01-22 19:54:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// cookie escapes special characters like $
|
|
|
|
// hence we need to unescape before comparing
|
|
|
|
decodedValue, err := url.QueryUnescape(cookie.Value)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return decodedValue, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteAdminCookie sets the response cookie to empty
|
|
|
|
func DeleteAdminCookie(gc *gin.Context) {
|
|
|
|
secure := true
|
|
|
|
httpOnly := true
|
2022-01-31 06:05:24 +00:00
|
|
|
hostname := utils.GetHost(gc)
|
|
|
|
host, _ := utils.GetHostParts(hostname)
|
2022-01-22 19:54:41 +00:00
|
|
|
|
2022-02-28 02:25:01 +00:00
|
|
|
gc.SetCookie(envstore.EnvStoreObj.GetStringStoreEnvVariable(constants.EnvKeyAdminCookieName), "", -1, "/", host, secure, httpOnly)
|
2022-01-22 19:54:41 +00:00
|
|
|
}
|