Set AuthStyle to InParams for public PKCE OAuth clients#4150
Open
gkatz2 wants to merge 2 commits intostacklok:mainfrom
Open
Set AuthStyle to InParams for public PKCE OAuth clients#4150gkatz2 wants to merge 2 commits intostacklok:mainfrom
gkatz2 wants to merge 2 commits intostacklok:mainfrom
Conversation
When oauth2.Endpoint.AuthStyle is unset (zero value), Go's oauth2 library uses AuthStyleAutoDetect, which tries HTTP Basic Auth first. For public PKCE clients (token_endpoint_auth_method=none), this sends an Authorization header with an empty password. Spec-compliant servers reject this and consume the single-use authorization code, causing the retry with client_id in POST body to fail with invalid_grant. Set AuthStyleInParams explicitly in all three locations where oauth2.Endpoint is constructed without AuthStyle: - pkg/auth/oauth/flow.go (authorization code exchange) - pkg/auth/remote/handler.go (token refresh from cached tokens) - pkg/registry/auth/oauth_token_source.go (registry auth) Add regression test with a strict mock server that rejects Basic Auth for public clients. Without the fix: 2 requests (auto-detect probing). With the fix: exactly 1 request. Fixes stacklok#4149 Signed-off-by: Greg Katz <gkatz@indeed.com>
Signed-off-by: Greg Katz <gkatz@indeed.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4150 +/- ##
==========================================
- Coverage 68.88% 68.79% -0.09%
==========================================
Files 461 464 +3
Lines 46562 46732 +170
==========================================
+ Hits 32075 32151 +76
- Misses 11987 12019 +32
- Partials 2500 2562 +62 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ToolHive's OAuth PKCE token exchange fails against strict OAuth 2.1 servers (e.g., Datadog's
mcp.datadoghq.com) becauseoauth2.Endpointstructs default toAuthStyleAutoDetect, which probes with HTTP Basic Auth first and burns the single-use authorization code. See the linked issue for the full mechanism.AuthStyle: oauth2.AuthStyleInParamsin the three locations whereoauth2.Endpointis constructed without itFixes #4149
Type of change
Test plan
task test)task lint-fix)mcp.datadoghq.com)Changes
pkg/auth/oauth/flow.goAuthStyleInParamson endpoint inNewFlowpkg/auth/remote/handler.goAuthStyleInParamson endpoint intryRestoreFromCachedTokenspkg/registry/auth/oauth_token_source.goAuthStyleInParamson endpoint inbuildOAuth2Configpkg/auth/oauth/flow_test.goTestAuthStyleInParams_StrictPublicClientServerregression testDoes this introduce a user-facing change?
Yes. Remote MCP servers with strict OAuth 2.1 implementations that previously failed with
invalid_grantduring PKCE token exchange will now connect successfully. No configuration changes needed.Special notes for reviewers
Why
AuthStyleInParamsis safe:AuthStyleAutoDetecttries HTTP Basic Auth first. For public PKCE clients (the common case — ToolHive's DCR registers withtoken_endpoint_auth_method=none), this sends anAuthorization: Basic base64(client_id:)header with an empty password. Spec-compliant servers reject this and consume the single-use authorization code, making the retry fail withinvalid_grant.AuthStyleInParamssendsclient_idin the POST body instead, avoiding the problem entirely.For the less common case where users provide their own client credentials via
--remote-auth-client-id/--remote-auth-client-secret,AuthStyleInParamssends the secret in the POST body (client_secret_poststyle). This is accepted by the vast majority of OAuth servers. The only servers that would break are those that exclusively requireclient_secret_basicand reject POST body credentials — an uncommon configuration, and a much smaller risk than the auth-code-burning bug this fixes.Generated with Claude Code