You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
auth: RequireBearerToken omits error="invalid_token" when a presented token is rejected #1497
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:
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
Wrap a handler with RequireBearerToken(verifier, &RequireBearerTokenOptions{ResourceMetadataURL: "https://example.com/.well-known/oauth-protected-resource", Scopes: []string{"read"}}), where verifier returns ErrInvalidToken.
Send a request with Authorization: Bearer some-token.
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.
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.
Activity
xianshujun commentedon Nov 5, 2026
I reproduced this on
mainatf91620fwith a verifier that returnsauth.ErrInvalidTokenfor a presented token, and separately with an expired token. Both get a 401 whoseWWW-Authenticatechallenge is identical to the no-credentials case (or absent entirely whenoptsis nil). A 403 from a missing scope does includeerror="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:ErrInvalidToken,A request with no bearer token still omits
error, per RFC 6750 §3.1, and the 403insufficient_scopepath is unchanged. I extended the existing table tests for all these cases (includingopts == niland theresource_metadata/scopecombinations);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.
guglielmo-san commentedon Nov 5, 2026
@xianshujun I assigned the issue to you, you can send a PR
auth: include error="invalid_token" in 401 challenges (modelcontextpr…