go-sdk icon indicating copy to clipboard operation
go-sdk copied to clipboard

auth: allow advertising different scopes in `WWW-Authenticate` than the ones `RequireBearerToken` enforces

Open pmokeev opened this issue 2 months ago • 2 comments

Problem

RequireBearerTokenOptions.Scopes currently serves two purposes at once:

  1. Enforcement: every scope in the list must be present in TokenInfo.Scopes, or the middleware returns 403 insufficient scope.
  2. Challenge: the same list is sent to the client as scope="..." in the WWW-Authenticate header of 401/403 responses.

Per the MCP authorization spec (SEP-835), clients request exactly the scopes from the WWW-Authenticate challenge when one is present, and only fall back to scopes_supported from the protected resource metadata otherwise.

This makes a common setup hard to express: the server requires only its own resource scope, but wants the client to request extra scopes as well (for example openid email).

Example

auth.RequireBearerToken(verify, &auth.RequireBearerTokenOptions{
    ResourceMetadataURL: metadataURL,
    Scopes:              []string{"read", "openid", "email"},
})

The client requests all three scopes, but some authorization servers issue an access token that carries only the resource scope, for example when a resource indicator is used. The middleware then rejects every request with 403.

Changing it to Scopes: []string{"read"} fixes enforcement, but the challenge becomes scope="read", so the client stops requesting openid email altogether.

Current workaround

Leave Scopes empty so that no scope parameter is sent and the client falls back to scopes_supported, then enforce the required scope manually inside the TokenVerifier. This works, but:

  • scope enforcement moves out of the middleware into user code;
  • a missing scope is reported as 401 invalid_token instead of 403 insufficient_scope (see also #1134).

pmokeev avatar Oct 02 '26 08:10 pmokeev

I think separating the two lists for the example given would weaken the protocol boundary rather than fix it.

The current 2026-07-28 authorization text says the challenge scope is authoritative for the current operation and that those scopes are required to satisfy the request: https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization#scope-selection-strategy. The runtime guidance is even narrower: the scope attribute describes the scopes necessary to access the requested resource, and a 403 should identify the minimum scopes needed for that operation: https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization#runtime-insufficient-scope-errors.

On that reading, RequireBearerTokenOptions.Scopes doing both enforcement and challenge emission is a useful invariant. Advertising openid email while accepting an access token that lacks them tells the client they are resource requirements when they are not. It also conflicts with the least-privilege reason the challenge takes precedence over scopes_supported.

There is already a client-side policy seam for the actual need. AuthorizationCodeHandlerConfig.ScopeFilter explicitly permits extending the discovered set before authorization, so a client that needs OIDC scopes can append openid/email while the MCP resource challenges only read. The separate enterprise OIDC flow likewise has OIDCLoginConfig.Scopes. I would preserve Scopes as “required, enforced, and challenged,” document that invariant, and steer identity/client-policy scopes to those client-side options.

If a server-side split is still wanted for a different case, I think the extra field should remain “scopes required for this operation” and must be enforced before a successful response; an “advertise but do not enforce” field would be easy to misuse.

This is a source/specification review; I did not exercise a live authorization server.

AI assistance disclosure: I used ChatGPT to inspect the current Go SDK source and cross-check the cited specification language; I reviewed and edited this comment.

jarrettdustinqq avatar Oct 02 '26 12:10 jarrettdustinqq

Agree with the comment @jarrettdustinqq, will close the issue

guglielmo-san avatar Oct 27 '26 13:10 guglielmo-san