Skip to content

auth: RequireBearerToken omits error="invalid_token" when a presented token is rejected #1497

Closed

Description

@lashinijay

Checklist

  • I searched the existing issues, and this has not been reported before.
  • This is a real problem that I ran into myself, using the latest release of the SDK.
  • I ran into it in a production system, not in a toy example, a benchmark, or an automated or AI-driven scan of the code.
  • I (a human) examined the relevant SDK code, and wrote or reviewed every line of this report. It is not unreviewed AI output.
  • This report is brief and concise, and sticks to the facts needed to understand the problem.

What happened?

#1134 asked for both RFC 6750 §3.1 error codes in the WWW-Authenticate challenge: insufficient_scope on the 403, and invalid_token on the 401 for a rejected token. #1421 added only insufficient_scope, and #1135, which added both, was closed as a duplicate of #1134 (comment), so the invalid_token half was dropped.

On main, a presented token that the verifier rejects with ErrInvalidToken, or that has expired, still gets a 401 whose challenge is the same as for a request with no credentials:

WWW-Authenticate: Bearer resource_metadata="...", scope="..."

RFC 6750 §3.1 says this challenge should carry error="invalid_token", while §3 says a request with no credentials should carry no error code. With one header for both, a client cannot tell a rejected token from missing credentials.

How to reproduce

  1. Wrap a handler with RequireBearerToken(verifier, &RequireBearerTokenOptions{ResourceMetadataURL: "https://example.com/.well-known/oauth-protected-resource", Scopes: []string{"read"}}), where verifier returns ErrInvalidToken.
  2. Send a request with Authorization: Bearer some-token.
  3. The response is 401 with WWW-Authenticate: Bearer resource_metadata="...", scope="read", the same header a request without an Authorization header gets.

Expected: the challenge for the rejected token also carries error="invalid_token". The challenge for the request without credentials stays as it is.

Where did you hit this?

ThunderID's MCP server (https://github.com/thunder-id/thunderid) protects its /mcp endpoint with RequireBearerToken. We track the endpoint's RFC 6750 conformance in thunder-id/thunderid#4509.

Go MCP SDK version

v1.8.0. Also checked main at f91620f.

Go version

1.27.2

Logs

N/A

Activity

  1. xianshujun commented on Nov 5, 2026

    @xianshujun
    Contributor

    I reproduced this on main at f91620f with a verifier that returns auth.ErrInvalidToken for a presented token, and separately with an expired token. Both get a 401 whose WWW-Authenticate challenge is identical to the no-credentials case (or absent entirely when opts is nil). A 403 from a missing scope does include error="insufficient_scope", so the two paths are inconsistent.

    I have a branch that passes the rejection reason through to the middleware and emits error="invalid_token" for:

    • a token rejected with ErrInvalidToken,
    • an expired token,
    • a token missing the required expiration.

    A request with no bearer token still omits error, per RFC 6750 §3.1, and the 403 insufficient_scope path is unchanged. I extended the existing table tests for all these cases (including opts == nil and the resource_metadata / scope combinations); go test ./... passes on the branch.

    Branch: https://github.com/xianshujun/go-sdk/tree/fix-1497-invalid-token-challenge (commit 60f9b35)

    Would a PR be welcome? Happy to adjust the approach or split it if you'd prefer. If so, could a maintainer assign the issue so the PR check passes?

    AI-assisted: implementation and tests drafted with ZCode under my direction.

  2. guglielmo-san commented on Nov 5, 2026

    @guglielmo-san
    Contributor

    @xianshujun I assigned the issue to you, you can send a PR

  3. added a commit that references this issue on Nov 6, 2026
    e66275d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions