-
Notifications
You must be signed in to change notification settings - Fork 14
feat(verifier): protect dynamic client registration with static/JWT auth #282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
leifj
wants to merge
2
commits into
SUNET:main
Choose a base branch
from
sirosfoundation:feature/verifier-dcr-auth-modes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,212 @@ | ||
| package middleware | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/subtle" | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "time" | ||
| "vc/pkg/logger" | ||
| "vc/pkg/model" | ||
|
|
||
| "github.com/coreos/go-oidc/v3/oidc" | ||
| "github.com/gin-gonic/gin" | ||
| ) | ||
|
|
||
| const ( | ||
| errCodeInvalidToken = "invalid_token" | ||
| errDescInvalidRegistrationAuthorizationToken = "invalid registration authorization token" | ||
| errDescMissingOrInvalidBearerToken = "missing or invalid bearer token" | ||
| ) | ||
|
|
||
| // RegistrationAuthValidator validates initial access tokens for dynamic client registration. | ||
| type RegistrationAuthValidator interface { | ||
| Validate(ctx context.Context, token string) error | ||
| } | ||
|
|
||
| type registrationAuthError struct { | ||
| status int | ||
| errorCode string | ||
| description string | ||
| } | ||
|
|
||
| func (e *registrationAuthError) Error() string { | ||
| return e.errorCode + ": " + e.description | ||
| } | ||
|
|
||
| func unauthorizedRegistrationError(description string) *registrationAuthError { | ||
| return ®istrationAuthError{ | ||
| status: http.StatusUnauthorized, | ||
| errorCode: errCodeInvalidToken, | ||
| description: description, | ||
| } | ||
| } | ||
|
|
||
| // NewRegistrationAuthMiddleware creates middleware for protecting POST /register. | ||
| // | ||
| // Modes: | ||
| // - open: no auth required | ||
| // - static: expects a fixed bearer token loaded from file | ||
| // - jwt: expects a signed JWT validated against configured issuer/audience/JWKS | ||
| // | ||
| // Future option (not implemented): external introspection. | ||
| func NewRegistrationAuthMiddleware(cfg *model.Cfg, log *logger.Log) (gin.HandlerFunc, error) { | ||
| passThrough := func(c *gin.Context) { c.Next() } | ||
| authCfg := getDynamicRegistrationAuthConfig(cfg) | ||
| if authCfg == nil { | ||
| return passThrough, nil | ||
| } | ||
|
|
||
| mode := strings.ToLower(strings.TrimSpace(authCfg.Mode)) | ||
| if mode == "" || mode == "open" { | ||
| return passThrough, nil | ||
| } | ||
|
|
||
| validator, err := buildRegistrationAuthValidator(mode, authCfg) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if log != nil { | ||
| log.Info("Dynamic registration authorization enabled", "mode", mode) | ||
| } | ||
|
|
||
| return func(c *gin.Context) { | ||
| token, err := extractBearerToken(c.GetHeader("Authorization")) | ||
| if err != nil { | ||
| writeRegistrationAuthError(c, unauthorizedRegistrationError(errDescMissingOrInvalidBearerToken)) | ||
| return | ||
| } | ||
|
|
||
| if err := validator.Validate(c.Request.Context(), token); err != nil { | ||
| var authErr *registrationAuthError | ||
| if errors.As(err, &authErr) { | ||
| writeRegistrationAuthError(c, authErr) | ||
| return | ||
| } | ||
|
|
||
| writeRegistrationAuthError(c, unauthorizedRegistrationError(errDescInvalidRegistrationAuthorizationToken)) | ||
| return | ||
| } | ||
|
|
||
| c.Next() | ||
| }, nil | ||
| } | ||
|
|
||
| func getDynamicRegistrationAuthConfig(cfg *model.Cfg) *model.DynamicRegistrationAuthConfig { | ||
| if cfg == nil || cfg.Verifier == nil || cfg.Verifier.OIDC == nil { | ||
| return nil | ||
| } | ||
|
|
||
| return cfg.Verifier.OIDC.DynamicRegistrationAuth | ||
| } | ||
|
|
||
| func buildRegistrationAuthValidator(mode string, authCfg *model.DynamicRegistrationAuthConfig) (RegistrationAuthValidator, error) { | ||
| switch mode { | ||
| case "static": | ||
| return newStaticBearerValidator(authCfg.StaticBearerTokenFile) | ||
| case "jwt": | ||
| return newJWTBearerValidator(authCfg.JWT) | ||
| case "introspection": | ||
| return nil, fmt.Errorf("verifier OIDC dynamic registration auth mode 'introspection' is not implemented yet") | ||
| default: | ||
| return nil, fmt.Errorf("unsupported verifier OIDC dynamic registration auth mode: %s", mode) | ||
| } | ||
| } | ||
|
|
||
| func writeRegistrationAuthError(c *gin.Context, authErr *registrationAuthError) { | ||
| c.Header("WWW-Authenticate", fmt.Sprintf("Bearer error=\"%s\"", authErr.errorCode)) | ||
| c.JSON(authErr.status, gin.H{ | ||
| "error": authErr.errorCode, | ||
| "error_description": authErr.description, | ||
| }) | ||
| c.Abort() | ||
| } | ||
|
|
||
| func extractBearerToken(authHeader string) (string, error) { | ||
| parts := strings.SplitN(strings.TrimSpace(authHeader), " ", 2) | ||
| if len(parts) != 2 || !strings.EqualFold(parts[0], "bearer") { | ||
| return "", fmt.Errorf("invalid authorization header") | ||
| } | ||
|
|
||
| token := strings.TrimSpace(parts[1]) | ||
| if token == "" { | ||
| return "", fmt.Errorf("empty bearer token") | ||
| } | ||
|
|
||
| return token, nil | ||
| } | ||
|
|
||
| type staticBearerValidator struct { | ||
| token string | ||
| } | ||
|
|
||
| func newStaticBearerValidator(tokenFilePath string) (*staticBearerValidator, error) { | ||
| if strings.TrimSpace(tokenFilePath) == "" { | ||
| return nil, fmt.Errorf("static mode requires dynamic_registration_auth.static_bearer_token_file") | ||
| } | ||
|
|
||
| content, err := os.ReadFile(filepath.Clean(tokenFilePath)) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to read static bearer token file: %w", err) | ||
| } | ||
|
|
||
| token := strings.TrimSpace(string(content)) | ||
| if token == "" { | ||
| return nil, fmt.Errorf("static bearer token file is empty") | ||
| } | ||
|
|
||
| return &staticBearerValidator{token: token}, nil | ||
| } | ||
|
|
||
| func (v *staticBearerValidator) Validate(_ context.Context, token string) error { | ||
| if subtle.ConstantTimeCompare([]byte(token), []byte(v.token)) != 1 { | ||
| return unauthorizedRegistrationError(errDescInvalidRegistrationAuthorizationToken) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| type jwtBearerValidator struct { | ||
| verifier *oidc.IDTokenVerifier | ||
| } | ||
|
|
||
| func newJWTBearerValidator(cfg *model.DynamicRegistrationJWTAuthConfig) (*jwtBearerValidator, error) { | ||
| if cfg == nil { | ||
| return nil, fmt.Errorf("jwt mode requires dynamic_registration_auth.jwt configuration") | ||
| } | ||
|
|
||
| if strings.TrimSpace(cfg.JWKSURI) == "" || strings.TrimSpace(cfg.Issuer) == "" || strings.TrimSpace(cfg.Audience) == "" { | ||
| return nil, fmt.Errorf("jwt mode requires jwks_uri, issuer, and audience") | ||
| } | ||
|
|
||
| algs := cfg.AllowedSigningAlgs | ||
| if len(algs) == 0 { | ||
| algs = []string{"RS256", "ES256"} | ||
| } | ||
|
|
||
| // NOTE: Introspection mode is intentionally deferred. | ||
| // JWT mode validates token signature and claims locally against JWKS. | ||
| keySet := oidc.NewRemoteKeySet(context.Background(), cfg.JWKSURI) | ||
| verifier := oidc.NewVerifier(cfg.Issuer, keySet, &oidc.Config{ | ||
| ClientID: cfg.Audience, | ||
| SupportedSigningAlgs: algs, | ||
| }) | ||
|
|
||
| return &jwtBearerValidator{verifier: verifier}, nil | ||
| } | ||
|
|
||
| func (v *jwtBearerValidator) Validate(ctx context.Context, token string) error { | ||
| verifyCtx, cancel := context.WithTimeout(ctx, 10*time.Second) | ||
| defer cancel() | ||
|
|
||
| if _, err := v.verifier.Verify(verifyCtx, token); err != nil { | ||
| return unauthorizedRegistrationError(errDescInvalidRegistrationAuthorizationToken) | ||
| } | ||
|
|
||
| return nil | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
registerAuthfield is initialized with a pass-through handler here at line 64, but is unconditionally overwritten at line 90 by the result ofNewRegistrationAuthMiddleware. This default is dead code. If the intent is a safety net in caseNewRegistrationAuthMiddlewareisn't called (defensive programming), that path currently can't happen since the overwrite always executes beforeregisterAuthis used. Consider removing this default initialization to avoid confusion, or adding a comment explaining the defensive intent.