2021-07-14 18:43:19 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2021-09-20 05:06:26 +00:00
|
|
|
"encoding/json"
|
2021-07-18 03:55:20 +00:00
|
|
|
"fmt"
|
2021-07-15 09:43:00 +00:00
|
|
|
"log"
|
|
|
|
"strings"
|
2021-07-14 18:43:19 +00:00
|
|
|
"time"
|
|
|
|
|
2021-07-23 16:27:44 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
2021-09-20 05:06:26 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/db"
|
2021-07-23 16:27:44 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/enum"
|
2021-07-15 09:43:00 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2021-07-14 18:43:19 +00:00
|
|
|
"github.com/golang-jwt/jwt"
|
|
|
|
)
|
|
|
|
|
2021-09-20 05:06:26 +00:00
|
|
|
// type UserAuthInfo struct {
|
|
|
|
// Email string `json:"email"`
|
|
|
|
// ID string `json:"id"`
|
|
|
|
// }
|
|
|
|
|
|
|
|
type JWTCustomClaim map[string]interface{}
|
2021-07-14 18:43:19 +00:00
|
|
|
|
|
|
|
type UserAuthClaim struct {
|
|
|
|
*jwt.StandardClaims
|
2021-09-20 05:06:26 +00:00
|
|
|
*JWTCustomClaim `json:"authorizer"`
|
2021-07-14 18:43:19 +00:00
|
|
|
}
|
|
|
|
|
2021-10-13 16:41:41 +00:00
|
|
|
func CreateAuthToken(user db.User, tokenType enum.TokenType, roles []string) (string, int64, error) {
|
2021-07-14 18:43:19 +00:00
|
|
|
t := jwt.New(jwt.GetSigningMethod(constants.JWT_TYPE))
|
|
|
|
expiryBound := time.Hour
|
|
|
|
if tokenType == enum.RefreshToken {
|
2021-07-15 12:02:55 +00:00
|
|
|
// expires in 1 year
|
|
|
|
expiryBound = time.Hour * 8760
|
2021-07-14 18:43:19 +00:00
|
|
|
}
|
|
|
|
|
2021-07-15 12:02:55 +00:00
|
|
|
expiresAt := time.Now().Add(expiryBound).Unix()
|
|
|
|
|
2021-09-20 05:06:26 +00:00
|
|
|
customClaims := JWTCustomClaim{
|
|
|
|
"token_type": tokenType.String(),
|
|
|
|
"email": user.Email,
|
|
|
|
"id": user.ID,
|
|
|
|
"allowed_roles": strings.Split(user.Roles, ","),
|
2021-10-13 16:41:41 +00:00
|
|
|
constants.JWT_ROLE_CLAIM: roles,
|
2021-09-20 05:06:26 +00:00
|
|
|
}
|
|
|
|
|
2021-07-14 18:43:19 +00:00
|
|
|
t.Claims = &UserAuthClaim{
|
|
|
|
&jwt.StandardClaims{
|
2021-07-15 12:02:55 +00:00
|
|
|
ExpiresAt: expiresAt,
|
2021-07-14 18:43:19 +00:00
|
|
|
},
|
2021-09-20 05:06:26 +00:00
|
|
|
&customClaims,
|
2021-07-14 18:43:19 +00:00
|
|
|
}
|
|
|
|
|
2021-07-15 12:02:55 +00:00
|
|
|
token, err := t.SignedString([]byte(constants.JWT_SECRET))
|
|
|
|
if err != nil {
|
|
|
|
return "", 0, err
|
|
|
|
}
|
|
|
|
return token, expiresAt, nil
|
2021-07-14 18:43:19 +00:00
|
|
|
}
|
2021-07-15 09:43:00 +00:00
|
|
|
|
|
|
|
func GetAuthToken(gc *gin.Context) (string, error) {
|
2021-07-27 12:16:02 +00:00
|
|
|
token, err := GetCookie(gc)
|
|
|
|
if err != nil || token == "" {
|
2021-07-15 09:43:00 +00:00
|
|
|
// try to check in auth header for cookie
|
|
|
|
log.Println("cookie not found checking headers")
|
|
|
|
auth := gc.Request.Header.Get("Authorization")
|
|
|
|
if auth == "" {
|
2021-07-28 07:55:52 +00:00
|
|
|
return "", fmt.Errorf(`unauthorized`)
|
2021-07-15 09:43:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
token = strings.TrimPrefix(auth, "Bearer ")
|
|
|
|
}
|
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
|
2021-09-20 05:06:26 +00:00
|
|
|
func VerifyAuthToken(token string) (map[string]interface{}, error) {
|
|
|
|
var res map[string]interface{}
|
2021-07-15 09:43:00 +00:00
|
|
|
claims := &UserAuthClaim{}
|
2021-09-20 05:06:26 +00:00
|
|
|
|
2021-07-15 09:43:00 +00:00
|
|
|
_, err := jwt.ParseWithClaims(token, claims, func(token *jwt.Token) (interface{}, error) {
|
|
|
|
return []byte(constants.JWT_SECRET), nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-09-20 05:06:26 +00:00
|
|
|
return res, err
|
2021-07-15 09:43:00 +00:00
|
|
|
}
|
|
|
|
|
2021-09-20 05:06:26 +00:00
|
|
|
data, _ := json.Marshal(claims.JWTCustomClaim)
|
|
|
|
json.Unmarshal(data, &res)
|
|
|
|
res["exp"] = claims.ExpiresAt
|
|
|
|
|
|
|
|
return res, nil
|
2021-07-15 09:43:00 +00:00
|
|
|
}
|