Repository navigation
auth: add ErrForbidden for valid tokens the resource must refuse - #1544
Conversation
A TokenVerifier can only signal an invalid token (401), an OAuth protocol error (400), or some other error that the middleware answers with 500. A valid token that the resource server must refuse, for example one issued to a client the server does not serve, has no way to reach the 403 response RFC 6750 §3.1 and the MCP authorization spec call for. Add ErrForbidden: when a verifier error unwraps to it, RequireBearerToken answers 403 with a WWW-Authenticate challenge carrying error="insufficient_scope", plus resource_metadata and scope as before. Unknown verifier errors still answer 500, so a broken verifier stays distinguishable from a refused token.
| if errors.Is(err, ErrForbidden) { | ||
| return nil, err.Error(), http.StatusForbidden, "insufficient_scope" | ||
| } |
There was a problem hiding this comment.
emitting "insufficient_scope" will trigger a new authentication flow in authentication_code.go. In this case we should not emit any challengeErr
There was a problem hiding this comment.
you're right, this would trigger step-up authorization: Authorize in authorization_code.go only skips the flow for a 403 whose challenge lacks error="insufficient_scope". A token that is valid but refused for a non-scope reason would be refused again after that flow, so ErrForbidden now answers 403 with no OAuth error code in the challenge (commit ae9831f). The scope check still emits insufficient_scope, so step-up works as before for missing scopes. I updated the doc and the table tests, including the middleware case where resource_metadata/scope are still emitted but without error.
A 403 whose WWW-Authenticate challenge carries error="insufficient_scope" makes the authorization-code client start step-up authorization (authorization_code.go Authorize). A token that is valid but refused for a non-scope reason would be refused again after that flow, so ErrForbidden now answers 403 with a challenge that has no OAuth error code. The scope check keeps error="insufficient_scope".
|
@xianshujun thank you for the contribution! |
Fixes #1543
TokenVerifiercan currently signal an invalid token (401), an OAuth protocol error (400), or a verifier failure (500). A valid token that the resource server must refuse — for example one issued to a client the server does not serve — has no way to reach the 403 that the MCP authorization spec asks for.This adds
auth.ErrForbidden: when a verifier error unwraps to it,RequireBearerTokenanswers 403 Forbidden. The challenge deliberately carries no OAuth error code, so the authorization-code client does not start step-up authorization for a token that would be refused the same way;resource_metadataandscopefrom the options are still emitted. Unknown verifier errors still return 500, so a broken verifier stays distinguishable from a refused token.Tests: table cases for a bare and a wrapped
ErrForbiddenthroughverifyand through the middleware (with and without options), plus a plain error that still yields 500. The scope check keepserror="insufficient_scope", so step-up still applies to missing scopes.go test ./auth/... ./mcp/...,go build ./...,gofmt -l auth/andgo vet ./auth/...pass.Naming (
ErrForbiddenvsErrInsufficientScope) is easy to change if you prefer.AI-assisted: implementation, tests and this description were drafted with an agent under my direction; I reproduced the issue and reviewed and tested the change.