Repository navigation
Description
Describe the bug
While reading through RequireBearerToken I noticed that verify checks RequireBearerTokenOptions.Scopes before expiry. So a token that's expired and also missing a required scope gets 403 insufficient scope instead of 401 token expired. Same for a token with no expiration when AllowMissingExpiration is false.
Per RFC 6750 §3.1 an expired token is invalid_token (401). It doesn't grant anything, so "insufficient scope" doesn't really apply, and the two codes ask the client for different things: 401 means get a new token, 403 means ask for more scopes. The SDK's own client doesn't re-authorize on a 403 without error="insufficient_scope" (see AuthorizationCodeHandler.Authorize), so the call just fails.
To Reproduce
- Run this against
main(6737e73):
package main
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"time"
"github.com/modelcontextprotocol/go-sdk/auth"
)
func main() {
// Token passes verification but expired an hour ago and has no scopes.
verifier := func(context.Context, string, *http.Request) (*auth.TokenInfo, error) {
return &auth.TokenInfo{Expiration: time.Now().Add(-time.Hour)}, nil
}
h := auth.RequireBearerToken(verifier, &auth.RequireBearerTokenOptions{
ResourceMetadataURL: "https://example.com/.well-known/oauth-protected-resource",
Scopes: []string{"read"},
})(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}))
req := httptest.NewRequest("GET", "/mcp", nil)
req.Header.Set("Authorization", "Bearer expired-token")
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
fmt.Println(rec.Code, rec.Body.String())
fmt.Println("WWW-Authenticate:", rec.Header().Get("WWW-Authenticate"))
}- It prints:
403 insufficient scope
WWW-Authenticate: Bearer resource_metadata="https://example.com/.well-known/oauth-protected-resource", scope="read"
Expected behavior
401 token expired. The scope check should only run on a token that's otherwise valid.
Logs
N/A, the repro output above is all there is.
Additional context
- go1.27.1 darwin/arm64, go-sdk
mainat 6737e73. v1.8.0 checks in the same order. - The Python SDK returns 401 for an expired token before it looks at scopes.
- auth: advertise RFC 6750 error code in WWW-Authenticate challenge #1135 adds
error="insufficient_scope"to the 403 but keeps this order. Once it lands, an expired token would be labeledinsufficient_scopeand the SDK client would start step-up authorization for a token that just needs a refresh. - The fix is to move the expiry block in
verifyabove the scope loop, no API change. I have a patch with two newTestVerifycases and will open a PR. It touches the same lines as auth: advertise RFC 6750 error code in WWW-Authenticate challenge #1135, so I'll rebase on it if that lands first.
AI assistance: I used Claude Code to help dig into this and write the repro and this report.
Activity
auth: check token expiry before required scopes (modelcontextprotocol…