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"
|
2021-10-24 00:54:12 +00:00
|
|
|
"os"
|
2021-07-15 09:43:00 +00:00
|
|
|
"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-10-29 16:11:47 +00:00
|
|
|
"github.com/robertkrimen/otto"
|
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-12-24 01:05:02 +00:00
|
|
|
resUser := GetResponseUserData(user)
|
2021-12-22 05:21:12 +00:00
|
|
|
userBytes, _ := json.Marshal(&resUser)
|
|
|
|
var userMap map[string]interface{}
|
|
|
|
json.Unmarshal(userBytes, &userMap)
|
|
|
|
|
2021-10-24 00:54:12 +00:00
|
|
|
customClaims := jwt.MapClaims{
|
|
|
|
"exp": expiresAt,
|
|
|
|
"iat": time.Now().Unix(),
|
2021-09-20 05:06:26 +00:00
|
|
|
"token_type": tokenType.String(),
|
|
|
|
"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-12-22 05:21:12 +00:00
|
|
|
for k, v := range userMap {
|
|
|
|
if k != "roles" {
|
|
|
|
customClaims[k] = v
|
|
|
|
}
|
|
|
|
}
|
2021-10-24 00:54:12 +00:00
|
|
|
|
2021-12-22 05:21:12 +00:00
|
|
|
// check for the extra access token script
|
2021-10-24 00:54:12 +00:00
|
|
|
accessTokenScript := os.Getenv("CUSTOM_ACCESS_TOKEN_SCRIPT")
|
|
|
|
if accessTokenScript != "" {
|
2021-10-29 16:11:47 +00:00
|
|
|
vm := otto.New()
|
2021-10-24 00:54:12 +00:00
|
|
|
|
2021-12-22 05:21:12 +00:00
|
|
|
claimBytes, _ := json.Marshal(customClaims)
|
2021-10-29 16:11:47 +00:00
|
|
|
vm.Run(fmt.Sprintf(`
|
|
|
|
var user = %s;
|
|
|
|
var tokenPayload = %s;
|
|
|
|
var customFunction = %s;
|
|
|
|
var functionRes = JSON.stringify(customFunction(user, tokenPayload));
|
|
|
|
`, string(userBytes), string(claimBytes), accessTokenScript))
|
2021-10-24 00:54:12 +00:00
|
|
|
|
2021-10-29 16:11:47 +00:00
|
|
|
val, err := vm.Get("functionRes")
|
2021-10-24 00:54:12 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2021-12-17 15:55:07 +00:00
|
|
|
log.Println("error getting custom access token script:", err)
|
2021-10-24 00:54:12 +00:00
|
|
|
} else {
|
|
|
|
extraPayload := make(map[string]interface{})
|
|
|
|
err = json.Unmarshal([]byte(fmt.Sprintf("%s", val)), &extraPayload)
|
|
|
|
if err != nil {
|
2021-12-17 15:55:07 +00:00
|
|
|
log.Println("error converting accessTokenScript response to map:", err)
|
2021-10-24 00:54:12 +00:00
|
|
|
} else {
|
|
|
|
for k, v := range extraPayload {
|
|
|
|
customClaims[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-14 18:43:19 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 00:54:12 +00:00
|
|
|
t.Claims = customClaims
|
|
|
|
|
2021-07-15 12:02:55 +00:00
|
|
|
token, err := t.SignedString([]byte(constants.JWT_SECRET))
|
|
|
|
if err != nil {
|
|
|
|
return "", 0, err
|
|
|
|
}
|
2021-10-24 00:54:12 +00:00
|
|
|
|
2021-07-15 12:02:55 +00:00
|
|
|
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-10-24 00:54:12 +00:00
|
|
|
claims := jwt.MapClaims{}
|
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-10-24 00:54:12 +00:00
|
|
|
// claim parses exp & iat into float 64 with e^10,
|
|
|
|
// but we expect it to be int64
|
|
|
|
// hence we need to assert interface and convert to int64
|
|
|
|
intExp := int64(claims["exp"].(float64))
|
|
|
|
intIat := int64(claims["iat"].(float64))
|
|
|
|
|
|
|
|
data, _ := json.Marshal(claims)
|
2021-09-20 05:06:26 +00:00
|
|
|
json.Unmarshal(data, &res)
|
2021-10-24 00:54:12 +00:00
|
|
|
res["exp"] = intExp
|
|
|
|
res["iat"] = intIat
|
2021-09-20 05:06:26 +00:00
|
|
|
|
|
|
|
return res, nil
|
2021-07-15 09:43:00 +00:00
|
|
|
}
|