feat: add api for admin login

This commit is contained in:
Lakhan Samani
2021-12-30 10:01:51 +05:30
parent 86bcb8ca87
commit d9c40057e6
16 changed files with 517 additions and 75 deletions

View File

@@ -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
}

View File

@@ -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)
}