go-sdk/auth
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Thomas Bechtold 0c004ee48a
auth: add ScopeFilter hook to AuthorizationCodeHandlerConfig (#1044)
## Summary

The authorization-code handler requests every scope advertised in the
protected resource's metadata `scopes_supported` (or named in the
`WWW-Authenticate` challenge), and offers the caller no way to adjust
that set. Some servers advertise a scope a client should *not* request:

- Google's Gmail MCP server advertises `gmail.metadata` in
`scopes_supported`.
- The Gmail API refuses the search `q` parameter on any token that
carries `gmail.metadata` — **even when `gmail.readonly` is also
granted** (a long-standing documented Gmail restriction, see
googleapis/google-api-python-client#582).

Because the handler requests the full advertised set, `gmail.metadata`
ends up in the grant and breaks search, with no client-side remedy.

## Change

Add an optional `ScopeFilter func(discovered []string) []string`
callback to `AuthorizationCodeHandlerConfig`. When non-nil, it receives
the discovered scopes and returns the set to request, giving the client
full control to narrow, extend, or reorder them. It runs **before** the
`offline_access` (SEP-2207) and step-up union (SEP-2350) logic, so
refresh-token support and scope accumulation are unaffected.

(Started as a static `Scopes []string` allowlist; switched to a callback
per review feedback for more flexibility.)

## Test

`TestAuthorize_ScopeFilter` drives a full authorize flow and asserts the
requested `scope` parameter for a nil filter (unchanged), a filter that
drops a scope, and one that replaces the set entirely. `go test
./auth/...` (incl. `-race`), `go vet`, and `gofmt` are clean.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-authored-by: Guglielmo Colombo <guglielmoc@google.com>
2026-07-31 10:35:49 +02:00
..
extauth mcp: add optional issuer validator for pre-registered client validation (SEP-2352) (#946) 2026-05-29 12:46:34 +00:00
auth.go auth: add ClockSkew option to RequireBearerTokenOptions (#969) 2026-06-05 12:16:43 +02:00
auth_example_test.go auth: support OAuth2 session persistence (#1058) 2026-07-13 15:10:05 +02:00
auth_test.go auth: add ClockSkew option to RequireBearerTokenOptions (#969) 2026-06-05 12:16:43 +02:00
authorization_code.go auth: add ScopeFilter hook to AuthorizationCodeHandlerConfig (#1044) 2026-07-31 10:35:49 +02:00
authorization_code_test.go auth: add ScopeFilter hook to AuthorizationCodeHandlerConfig (#1044) 2026-07-31 10:35:49 +02:00
client.go Enterprise managed authorization (#770) 2026-03-30 16:29:30 +02:00
shared.go all: stabilize client OAuth support (#861) 2026-03-31 12:36:31 +02:00
shared_test.go all: stabilize client OAuth support (#861) 2026-03-31 12:36:31 +02:00