fix: accept any Authentik provider issuer in JWT validation
- isValidIssuer() accepts portfolio-agent, memory-agent, api-gw, etc. - All Authentik providers use same signing key (JWKS valid) - CheckPermissions now checks both 'permissions' (users) and 'roles' (service accounts) - Fixes JWT issuer mismatch for portfolio-agent, memory-agent tokens
This commit is contained in:
+35
-10
@@ -3,6 +3,7 @@ package auth
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -10,6 +11,13 @@ import (
|
|||||||
"github.com/golang-jwt/jwt/v5"
|
"github.com/golang-jwt/jwt/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// isValidIssuer checks if issuer is from Authentik (any provider/app).
|
||||||
|
// Accepts: https://authentik.riotpiao.com/application/o/{provider}/
|
||||||
|
func isValidIssuer(iss string) bool {
|
||||||
|
return strings.Contains(iss, "authentik.riotpiao.com/application/o/") &&
|
||||||
|
strings.HasSuffix(iss, "/")
|
||||||
|
}
|
||||||
|
|
||||||
// Validator validates JWTs against Authentik JWKS.
|
// Validator validates JWTs against Authentik JWKS.
|
||||||
type Validator struct {
|
type Validator struct {
|
||||||
issuer string
|
issuer string
|
||||||
@@ -107,9 +115,13 @@ func (v *Validator) ValidateBearerToken(authHeader string) (jwt.MapClaims, error
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check iss (issuer)
|
// Check iss (issuer) - accept any Authentik provider issuer
|
||||||
if iss, ok := claims["iss"].(string); !ok || iss != v.issuer {
|
// (portfolio-agent, memory-agent, api-gw, etc.)
|
||||||
return nil, fmt.Errorf("invalid issuer: expected %s, got %s", v.issuer, iss)
|
// All use same signing key so JWKS validation is sufficient
|
||||||
|
if iss, ok := claims["iss"].(string); !ok {
|
||||||
|
return nil, fmt.Errorf("missing issuer claim")
|
||||||
|
} else if !isValidIssuer(iss) {
|
||||||
|
return nil, fmt.Errorf("invalid issuer: %s", iss)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check aud (audience)
|
// Check aud (audience)
|
||||||
@@ -121,18 +133,32 @@ func (v *Validator) ValidateBearerToken(authHeader string) (jwt.MapClaims, error
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CheckPermissions checks if claims contain required permission(s).
|
// CheckPermissions checks if claims contain required permission(s).
|
||||||
|
// Checks both "permissions" claim (for users) and "roles" claim (for service accounts).
|
||||||
// Returns true if any required permission is found or wildcard "*" exists.
|
// Returns true if any required permission is found or wildcard "*" exists.
|
||||||
func (v *Validator) CheckPermissions(claims jwt.MapClaims, required ...string) bool {
|
func (v *Validator) CheckPermissions(claims jwt.MapClaims, required ...string) bool {
|
||||||
permsIface, ok := claims["permissions"]
|
// Try permissions claim first (for user tokens)
|
||||||
if !ok {
|
if permsIface, ok := claims["permissions"]; ok {
|
||||||
return false
|
if perms, ok := permsIface.([]interface{}); ok {
|
||||||
|
if v.checkPermList(perms, required...) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
perms, ok := permsIface.([]interface{})
|
// Fall back to roles claim (for service account tokens)
|
||||||
if !ok {
|
if rolesIface, ok := claims["roles"]; ok {
|
||||||
return false
|
if roles, ok := rolesIface.([]interface{}); ok {
|
||||||
|
if v.checkPermList(roles, required...) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// checkPermList is a helper that checks a permission/role list.
|
||||||
|
func (v *Validator) checkPermList(perms []interface{}, required ...string) bool {
|
||||||
for _, perm := range perms {
|
for _, perm := range perms {
|
||||||
permStr, ok := perm.(string)
|
permStr, ok := perm.(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -147,7 +173,6 @@ func (v *Validator) CheckPermissions(claims jwt.MapClaims, required ...string) b
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user