refactor: allow purpose to be none

This commit is contained in:
Derrick Hammer 2024-03-13 18:44:09 -04:00
parent 749a932663
commit d5118beb58
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 2 additions and 4 deletions

View File

@ -26,6 +26,7 @@ var (
const ( const (
JWTPurposeLogin JWTPurpose = "login" JWTPurposeLogin JWTPurpose = "login"
JWTPurpose2FA JWTPurpose = "2fa" JWTPurpose2FA JWTPurpose = "2fa"
JWTPurposeNone JWTPurpose = ""
) )
func JWTGenerateToken(domain string, privateKey ed25519.PrivateKey, userID uint, purpose JWTPurpose) (string, error) { func JWTGenerateToken(domain string, privateKey ed25519.PrivateKey, userID uint, purpose JWTPurpose) (string, error) {

View File

@ -104,9 +104,6 @@ func AuthMiddleware(options AuthMiddlewareOptions) func(http.Handler) http.Handl
if options.AuthContextKey == "" { if options.AuthContextKey == "" {
options.AuthContextKey = DEFAULT_AUTH_CONTEXT_KEY options.AuthContextKey = DEFAULT_AUTH_CONTEXT_KEY
} }
if options.Purpose == "" {
panic("purpose is missing")
}
domain := options.Config.Config().Core.Domain domain := options.Config.Config().Core.Domain
@ -126,7 +123,7 @@ func AuthMiddleware(options AuthMiddlewareOptions) func(http.Handler) http.Handl
claim, err := account.JWTVerifyToken(authToken, domain, options.Identity, func(claim *jwt.RegisteredClaims) error { claim, err := account.JWTVerifyToken(authToken, domain, options.Identity, func(claim *jwt.RegisteredClaims) error {
aud, _ := claim.GetAudience() aud, _ := claim.GetAudience()
if slices.Contains[jwt.ClaimStrings, string](aud, string(options.Purpose)) == false { if options.Purpose != account.JWTPurposeNone && slices.Contains[jwt.ClaimStrings, string](aud, string(options.Purpose)) == false {
return account.ErrJWTInvalid return account.ErrJWTInvalid
} }