authorizer/server/token/jwt.go

108 lines
3.6 KiB
Go
Raw Permalink Normal View History

2022-02-12 10:24:23 +00:00
package token
import (
"errors"
"github.com/authorizerdev/authorizer/server/constants"
2022-02-28 02:25:01 +00:00
"github.com/authorizerdev/authorizer/server/crypto"
2022-02-12 10:24:23 +00:00
"github.com/authorizerdev/authorizer/server/envstore"
"github.com/golang-jwt/jwt"
)
// SignJWTToken common util to sing jwt token
func SignJWTToken(claims jwt.MapClaims) (string, error) {
2022-02-28 02:25:01 +00:00
jwtType := envstore.EnvStoreObj.GetStringStoreEnvVariable(constants.EnvKeyJwtType)
2022-02-12 10:24:23 +00:00
signingMethod := jwt.GetSigningMethod(jwtType)
2022-02-12 13:56:37 +00:00
if signingMethod == nil {
return "", errors.New("unsupported signing method")
}
2022-02-12 10:24:23 +00:00
t := jwt.New(signingMethod)
2022-02-12 13:56:37 +00:00
if t == nil {
return "", errors.New("unsupported signing method")
}
2022-02-12 10:24:23 +00:00
t.Claims = claims
switch signingMethod {
case jwt.SigningMethodHS256, jwt.SigningMethodHS384, jwt.SigningMethodHS512:
2022-02-28 02:25:01 +00:00
return t.SignedString([]byte(envstore.EnvStoreObj.GetStringStoreEnvVariable(constants.EnvKeyJwtSecret)))
2022-02-12 10:24:23 +00:00
case jwt.SigningMethodRS256, jwt.SigningMethodRS384, jwt.SigningMethodRS512:
2022-02-28 02:25:01 +00:00
key, err := crypto.ParseRsaPrivateKeyFromPemStr(envstore.EnvStoreObj.GetStringStoreEnvVariable(constants.EnvKeyJwtPrivateKey))
2022-02-12 10:24:23 +00:00
if err != nil {
return "", err
}
return t.SignedString(key)
case jwt.SigningMethodES256, jwt.SigningMethodES384, jwt.SigningMethodES512:
2022-02-28 02:25:01 +00:00
key, err := crypto.ParseEcdsaPrivateKeyFromPemStr(envstore.EnvStoreObj.GetStringStoreEnvVariable(constants.EnvKeyJwtPrivateKey))
2022-02-12 10:24:23 +00:00
if err != nil {
return "", err
}
2022-02-28 02:25:01 +00:00
2022-02-12 10:24:23 +00:00
return t.SignedString(key)
default:
return "", errors.New("unsupported signing method")
}
}
// ParseJWTToken common util to parse jwt token
2022-03-02 12:12:31 +00:00
func ParseJWTToken(token, hostname, nonce, subject string) (jwt.MapClaims, error) {
2022-02-28 02:25:01 +00:00
jwtType := envstore.EnvStoreObj.GetStringStoreEnvVariable(constants.EnvKeyJwtType)
2022-02-12 10:24:23 +00:00
signingMethod := jwt.GetSigningMethod(jwtType)
var err error
var claims jwt.MapClaims
switch signingMethod {
case jwt.SigningMethodHS256, jwt.SigningMethodHS384, jwt.SigningMethodHS512:
2022-02-12 13:56:37 +00:00
_, err = jwt.ParseWithClaims(token, &claims, func(token *jwt.Token) (interface{}, error) {
2022-02-28 02:25:01 +00:00
return []byte(envstore.EnvStoreObj.GetStringStoreEnvVariable(constants.EnvKeyJwtSecret)), nil
2022-02-12 10:24:23 +00:00
})
case jwt.SigningMethodRS256, jwt.SigningMethodRS384, jwt.SigningMethodRS512:
_, err = jwt.ParseWithClaims(token, &claims, func(token *jwt.Token) (interface{}, error) {
2022-02-28 02:25:01 +00:00
key, err := crypto.ParseRsaPublicKeyFromPemStr(envstore.EnvStoreObj.GetStringStoreEnvVariable(constants.EnvKeyJwtPublicKey))
2022-02-12 10:24:23 +00:00
if err != nil {
return nil, err
}
return key, nil
})
case jwt.SigningMethodES256, jwt.SigningMethodES384, jwt.SigningMethodES512:
_, err = jwt.ParseWithClaims(token, &claims, func(token *jwt.Token) (interface{}, error) {
2022-02-28 02:25:01 +00:00
key, err := crypto.ParseEcdsaPublicKeyFromPemStr(envstore.EnvStoreObj.GetStringStoreEnvVariable(constants.EnvKeyJwtPublicKey))
2022-02-12 10:24:23 +00:00
if err != nil {
return nil, err
}
return key, nil
})
default:
err = errors.New("unsupported signing method")
}
if err != nil {
return claims, err
}
// 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))
claims["exp"] = intExp
claims["iat"] = intIat
2022-03-02 12:12:31 +00:00
if claims["aud"] != envstore.EnvStoreObj.GetStringStoreEnvVariable(constants.EnvKeyClientID) {
return claims, errors.New("invalid audience")
}
if claims["nonce"] != nonce {
return claims, errors.New("invalid nonce")
}
if claims["iss"] != hostname {
return claims, errors.New("invalid issuer")
}
if claims["sub"] != subject {
return claims, errors.New("invalid subject")
}
2022-02-12 10:24:23 +00:00
return claims, nil
}