Skip to content

Commit 91ee5a2

Browse files
authored
Merge pull request #548 from junyuanz1/fix/token-endpoint-single-auth-method
fix(client/oauth): omit body client_id when using HTTP Basic
2 parents c6a5512 + 4192ea5 commit 91ee5a2

2 files changed

Lines changed: 74 additions & 24 deletions

File tree

‎lib/mcp/client/oauth/flow.rb‎

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -997,9 +997,9 @@ def exchange_refresh_token(as_metadata:, client_info:, refresh_token:, resource:
997997
post_to_token_endpoint(as_metadata: as_metadata, client_info: client_info, form: form)
998998
end
999999

1000-
# Submits a form-encoded request to the token endpoint, applying
1001-
# the client authentication method advertised in `client_information` and
1002-
# adding `client_id` (and `client_secret` when not using HTTP Basic).
1000+
# Submits a form-encoded token request using the authentication method
1001+
# stored in `client_information`. The method determines whether client
1002+
# credentials belong in the form body, a Basic header, or a JWT assertion.
10031003
def post_to_token_endpoint(as_metadata:, client_info:, form:)
10041004
client_id = client_info_required_value(client_info, "client_id")
10051005
unless client_id
@@ -1010,12 +1010,13 @@ def post_to_token_endpoint(as_metadata:, client_info:, form:)
10101010
client_secret = client_info_required_value(client_info, "client_secret")
10111011
token_endpoint_auth_method = client_info_value(client_info, "token_endpoint_auth_method")
10121012

1013-
form = if token_endpoint_auth_method == "private_key_jwt"
1014-
# RFC 7523 Section 2.2 JWT client assertion for the `private_key_jwt` method of
1015-
# the `io.modelcontextprotocol/oauth-client-credentials` extension (SEP-1046).
1016-
# The client identity travels in the assertion's `iss`/`sub` claims, so `client_id` is
1017-
# omitted from the body per RFC 7521 Section 4.2 (the `client_assertion` conveys the client identity).
1018-
# The audience is the issuer identifier that `ensure_issuer_matches!` already byte-validated.
1013+
# Apply one client authentication method per request (RFC 6749 Section 2.3).
1014+
headers = {}
1015+
form = case token_endpoint_auth_method
1016+
when "private_key_jwt"
1017+
# The assertion identifies the client through its `iss` and `sub`
1018+
# claims, so the body needs no separate `client_id` (RFC 7521 Section 4.2).
1019+
# Use the issuer already checked by `ensure_issuer_matches!` as the audience.
10191020
unless @provider.respond_to?(:client_assertion)
10201021
raise AuthorizationError,
10211022
"token_endpoint_auth_method is private_key_jwt but the provider does not " \
@@ -1026,22 +1027,24 @@ def post_to_token_endpoint(as_metadata:, client_info:, form:)
10261027
"client_assertion_type" => JWTClientAssertion::ASSERTION_TYPE,
10271028
"client_assertion" => @provider.client_assertion(audience: as_metadata["issuer"]),
10281029
)
1030+
when "client_secret_post"
1031+
# Send the client ID and available secret in the form body.
1032+
body = form.merge("client_id" => client_id)
1033+
body["client_secret"] = client_secret if client_secret
1034+
body
10291035
else
1030-
form.merge("client_id" => client_id)
1031-
end
1032-
1033-
headers = {}
1034-
if client_secret
1035-
case token_endpoint_auth_method
1036-
when "client_secret_post"
1037-
form["client_secret"] = client_secret
1038-
when "none"
1039-
# Public client; no credential.
1040-
else
1041-
# RFC 6749 §2.3.1 recommends Basic for confidential clients and
1042-
# both Python and TypeScript SDKs default here when
1043-
# the authentication method is not explicitly stored.
1036+
if client_secret && token_endpoint_auth_method != "none"
1037+
# Basic is also the fallback when a secret is present but no method
1038+
# is stored. A body `client_id` is optional (RFC 6749 Section 3.2.1);
1039+
# omit it because some servers treat it alongside Basic as a second
1040+
# authentication method and reject the request with `invalid_request`.
10441041
headers["Authorization"] = "Basic " + basic_auth_credentials(client_id, client_secret)
1042+
form
1043+
else
1044+
# With `none` or no secret, identify the client using `client_id`.
1045+
# This is required for unauthenticated authorization-code exchanges
1046+
# (RFC 6749 Section 3.2.1).
1047+
form.merge("client_id" => client_id)
10451048
end
10461049
end
10471050

‎test/mcp/client/oauth/flow_test.rb‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,21 @@ def test_run_client_credentials_with_client_secret_post_sends_credentials_in_bod
158158
end
159159
end
160160

161+
def test_run_client_credentials_with_client_secret_basic_omits_client_id_from_body
162+
# Explicit Basic authentication must keep both credentials in the header
163+
# to avoid servers treating body credentials as a second auth method.
164+
provider = client_credentials_provider(token_endpoint_auth_method: "client_secret_basic")
165+
166+
Flow.new(provider: provider).run!(server_url: @server_url, resource_metadata_url: @prm_url)
167+
168+
assert_requested(:post, "#{@auth_base}/token") do |req|
169+
form = URI.decode_www_form(req.body).to_h
170+
req.headers["Authorization"] == "Basic " + Base64.strict_encode64("cc-client:cc-secret") &&
171+
!form.key?("client_id") &&
172+
!form.key?("client_secret")
173+
end
174+
end
175+
161176
def test_run_client_credentials_raises_clean_error_when_client_information_missing
162177
# The constructor always stores credentials, but if a custom storage loses them
163178
# the grant must fail with a domain error rather than a `NoMethodError` from
@@ -1860,8 +1875,10 @@ def test_run_uses_basic_auth_when_token_endpoint_auth_method_is_client_secret_ba
18601875

18611876
expected = "Basic " + Base64.strict_encode64("pre-registered-client:pre-registered-secret")
18621877
assert_requested(:post, "#{@auth_base}/token") do |req|
1878+
form = URI.decode_www_form(req.body).to_h
18631879
req.headers["Authorization"] == expected &&
1864-
!URI.decode_www_form(req.body).to_h.key?("client_secret")
1880+
!form.key?("client_id") &&
1881+
!form.key?("client_secret")
18651882
end
18661883
end
18671884

@@ -2365,6 +2382,36 @@ def test_refresh_swaps_refresh_token_for_new_access_token
23652382
assert_equal("saved-rt", provider.tokens["refresh_token"])
23662383
end
23672384

2385+
def test_refresh_with_client_secret_basic_omits_client_id_from_body
2386+
# Stored credentials without an auth method default to Basic. Refresh
2387+
# must also omit the body `client_id` when using that fallback.
2388+
stub_request(:post, "#{@auth_base}/token")
2389+
.with(body: hash_including("grant_type" => "refresh_token"))
2390+
.to_return(
2391+
status: 200,
2392+
headers: { "Content-Type" => "application/json" },
2393+
body: JSON.generate(access_token: "fresh-at", token_type: "Bearer", expires_in: 3600),
2394+
)
2395+
2396+
provider = Provider.new(
2397+
client_metadata: { redirect_uris: ["http://localhost:0/callback"] },
2398+
redirect_uri: "http://localhost:0/callback",
2399+
redirect_handler: ->(_url) {},
2400+
callback_handler: -> { [nil, nil] },
2401+
)
2402+
provider.save_client_information("client_id" => "conf-client", "client_secret" => "conf-secret")
2403+
provider.save_tokens("access_token" => "stale-at", "refresh_token" => "saved-rt")
2404+
2405+
Flow.new(provider: provider).refresh!(server_url: @server_url, resource_metadata_url: @prm_url)
2406+
2407+
assert_requested(:post, "#{@auth_base}/token") do |req|
2408+
form = URI.decode_www_form(req.body).to_h
2409+
req.headers["Authorization"] == "Basic " + Base64.strict_encode64("conf-client:conf-secret") &&
2410+
!form.key?("client_id") &&
2411+
!form.key?("client_secret")
2412+
end
2413+
end
2414+
23682415
def test_run_records_the_issuer_that_minted_the_tokens
23692416
state_holder = {}
23702417
provider = Provider.new(

0 commit comments

Comments
 (0)