feat: add api for admin login
This commit is contained in:
@@ -124,3 +124,31 @@ func VerifyAuthToken(token string) (map[string]interface{}, error) {
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func CreateAdminAuthToken(tokenType enum.TokenType, c *gin.Context) (string, int64, error) {
|
||||
t := jwt.New(jwt.GetSigningMethod(constants.JWT_TYPE))
|
||||
expiryBound := time.Hour
|
||||
if tokenType == enum.RefreshToken {
|
||||
// expires in 1 year
|
||||
expiryBound = time.Hour * 8760
|
||||
}
|
||||
|
||||
expiresAt := time.Now().Add(expiryBound).Unix()
|
||||
|
||||
customClaims := jwt.MapClaims{
|
||||
"exp": expiresAt,
|
||||
"iat": time.Now().Unix(),
|
||||
"user_agent": GetUserAgent(c.Request),
|
||||
"ip": GetIP(c.Request),
|
||||
"role": "authorizer_admin",
|
||||
"created_at": time.Now().Unix(),
|
||||
}
|
||||
|
||||
t.Claims = customClaims
|
||||
|
||||
token, err := t.SignedString([]byte(constants.JWT_SECRET))
|
||||
if err != nil {
|
||||
return "", 0, err
|
||||
}
|
||||
return token, expiresAt, nil
|
||||
}
|
||||
|
@@ -47,3 +47,19 @@ func DeleteCookie(gc *gin.Context) {
|
||||
gc.SetCookie(constants.COOKIE_NAME, "", -1, "/", host, secure, httpOnly)
|
||||
gc.SetCookie(constants.COOKIE_NAME+"-client", "", -1, "/", domain, secure, httpOnly)
|
||||
}
|
||||
|
||||
func SetAdminCookie(gc *gin.Context, token string) {
|
||||
secure := true
|
||||
httpOnly := true
|
||||
host, _ := GetHostParts(constants.AUTHORIZER_URL)
|
||||
|
||||
gc.SetCookie("authorizer-admin", token, 3600, "/", host, secure, httpOnly)
|
||||
}
|
||||
|
||||
func DeleteAdminCookie(gc *gin.Context, token string) {
|
||||
secure := true
|
||||
httpOnly := true
|
||||
host, _ := GetHostParts(constants.AUTHORIZER_URL)
|
||||
|
||||
gc.SetCookie("authorizer-admin", "", -1, "/", host, secure, httpOnly)
|
||||
}
|
||||
|
Reference in New Issue
Block a user