Repository navigation
auth: advertise RFC 6750 error code in WWW-Authenticate challenge - #1135
PratikDhanave wants to merge 2 commits into
Conversation
RequireBearerToken never added an error= auth-param to the WWW-Authenticate header, so on insufficient scope it returned 403 with resource_metadata/scope but no error="insufficient_scope". The SDK's own client gates step-up re-authorization on exactly that value (AuthorizationCodeHandler.Authorize), so errorFromChallenges returned "" and step-up never fired — a client hitting an SDK server that needs more scopes just kept failing with 403 instead of upgrading scopes. Thread the RFC 6750 error code out of verify and add the error= param: insufficient_scope on the 403 scope check, invalid_token on the token-invalid 401 paths, invalid_request on the 400. A missing token (no credentials presented) and 5xx failures advertise no code. The change is additive to the header, non-breaking. Fixes modelcontextprotocol#1134
| } | ||
| if errors.Is(err, ErrOAuth) { | ||
| return nil, err.Error(), http.StatusBadRequest | ||
| return nil, err.Error(), http.StatusBadRequest, "invalid_request" |
There was a problem hiding this comment.
this will not trigger the authorization flow as the code in @authorization_code is just checking for http.StatuForbidden case.
I am not sure tho if we should extend the case for the flow re-trigger
There was a problem hiding this comment.
Thanks @guglielmo-san — good catch to check, but the step-up path does fire with this change. AuthorizationCodeHandler.Authorize gates re-auth on two conditions together:
if resp.StatusCode == http.StatusForbidden && errorFromChallenges(wwwChallenges) != "insufficient_scope" {
return nil // skip step-up
}It skips step-up only when the status is 403 and the challenge's error param is not insufficient_scope. Before this PR the server never set error= at all, so errorFromChallenges returned "", the condition was true, and step-up was skipped — that's the bug. By advertising error="insufficient_scope" on the 403 challenge (RFC 6750 §3.1), the condition becomes false and re-authorization proceeds.
TestRequireBearerTokenAdvertisesInsufficientScope drives this end-to-end and parses the emitted header with the same oauthex.ParseWWWAuthenticate the client uses, asserting the code is visible. So the 403-checking logic doesn't need to change — the fix is exactly to populate the error param it already reads.
While reading `RequireBearerToken` I noticed that `verify` checks required scopes before expiry. A token that's expired and also missing a scope gets `403 insufficient scope` instead of `401 token expired`, and a token with no expiration gets the same 403 when `AllowMissingExpiration` is false. Repro is in #1349. An expired token is `invalid_token` (RFC 6750 §3.1), so the client should be told to get a new token, not to ask for more scopes. I moved the expiry check, including the missing-expiration case, above the scope loop. No API change. This touches the same lines in `verify` and `TestVerify` as #1135. If that one lands first I'll rebase on it. Tested with go1.27.1 on darwin/arm64: - The two new `TestVerify` cases ("expired and missing scope", "no expiration and missing scope") fail on main with `got ("insufficient scope", 403)` and pass with the fix. - `gofmt -l auth` and `go vet ./...` are clean, and `go test -count=1 ./...` passes. - The repro from the issue now prints `401 token expired`. Fixes #1349 _AI assistance: I used Claude Code to help write this fix and the tests._ 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <[email protected]>
|
Closing as duplicate of #1134 |
Summary
RequireBearerTokennever added anerror=auth-param to theWWW-Authenticateheader it emits. On insufficient scope it returned403withresource_metadata/scopebut noerror="insufficient_scope".The SDK's own client gates step-up re-authorization on exactly that value (
AuthorizationCodeHandler.Authorize):errorFromChallengesreadsParams["error"]; since the server never set it, it returned"", the condition was true, and step-up never fired. A client hitting an SDK-based server that needs additional scopes just kept failing with403instead of upgrading scopes. It's also an RFC 6750 §3.1 deviation.Fixes #1134
Change
Thread the RFC 6750 error code out of
verifyand add theerror=auth-param to the challenge:error=insufficient_scopeinvalid_tokenErrOAuthinvalid_requestThe change only adds a param to the header — it is non-breaking.
Testing
TestVerifyto assert the error code returned for each failure path.TestRequireBearerTokenAdvertisesInsufficientScope, which drives the middleware end-to-end and parses the emitted header with the sameoauthex.ParseWWWAuthenticatethe client uses, assertingerror="insufficient_scope"is visible.TestRequireBearerToken's expectedWWW-Authenticateheaders to include the new code.error param = "", want "insufficient_scope") and passes with it.go vet ./auth/and the fullgo test ./auth/suite pass.