From c47d15407401c6860c8fcb1cf9210fc54885d422 Mon Sep 17 00:00:00 2001 From: yuwk <1729065730@qq.com> Date: Fri, 28 Aug 2026 23:11:21 +0800 Subject: [PATCH] Use none as the token-endpoint auth method for CIMD public clients A client identified by a Client ID Metadata Document is a public client: its identifier is a URL and it has no client secret, so it must authenticate at the token endpoint with 'none' and prove possession via PKCE. Previously ApplyClientIdMetadataDocument left the auth method unset, so GetAccessTokenAsync fell through to the first method the authorization server advertised. Servers like Auth0 advertise client_secret_basic first, which made the token exchange send a Basic header built from the CIMD URL and an empty secret and omit client_id from the body, so the exchange failed with 401 access_denied. ApplyClientIdMetadataDocument now sets the auth method to 'none'. The test OAuth server can advertise an arbitrary ordered auth-method list, and a new regression test proves a CIMD client authenticates when the server advertises client_secret_basic before none. Fixes modelcontextprotocol/csharp-sdk#1612 --- .../Authentication/ClientOAuthProvider.cs | 7 +++++ .../OAuth/AuthTests.cs | 28 +++++++++++++++++++ .../Program.cs | 13 ++++++++- 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/ModelContextProtocol.Core/Authentication/ClientOAuthProvider.cs b/src/ModelContextProtocol.Core/Authentication/ClientOAuthProvider.cs index 785e3cc2e..774fe4365 100644 --- a/src/ModelContextProtocol.Core/Authentication/ClientOAuthProvider.cs +++ b/src/ModelContextProtocol.Core/Authentication/ClientOAuthProvider.cs @@ -494,6 +494,13 @@ private void ApplyClientIdMetadataDocument(Uri metadataUri) _clientId = metadataUri.AbsoluteUri; + // A CIMD client is a public client (it has no client secret, and its identifier is a URL), + // so it authenticates at the token endpoint with "none" and proves possession via PKCE. + // Without this, GetAccessTokenAsync falls through to the first method the authorization + // server advertises (e.g. client_secret_basic on Auth0), which fails with 401 access_denied. + // See https://github.com/modelcontextprotocol/csharp-sdk/issues/1612. + _tokenEndpointAuthMethod = "none"; + // See: https://datatracker.ietf.org/doc/html/draft-ietf-oauth-client-id-metadata-document-00#section-3 static bool IsValidClientMetadataDocumentUri(Uri uri) => uri.IsAbsoluteUri diff --git a/tests/ModelContextProtocol.AspNetCore.Tests/OAuth/AuthTests.cs b/tests/ModelContextProtocol.AspNetCore.Tests/OAuth/AuthTests.cs index 693c77943..3b770fc6c 100644 --- a/tests/ModelContextProtocol.AspNetCore.Tests/OAuth/AuthTests.cs +++ b/tests/ModelContextProtocol.AspNetCore.Tests/OAuth/AuthTests.cs @@ -360,6 +360,34 @@ public async Task CanAuthenticate_WithClientMetadataDocument() transport, loggerFactory: LoggerFactory, cancellationToken: TestContext.Current.CancellationToken); } + [Fact] + public async Task CanAuthenticate_WithClientMetadataDocument_WhenServerAdvertisesClientSecretBasicFirst() + { + // Auth0 advertises client_secret_basic ahead of none. A CIMD client is a public client and + // must use "none" (PKCE) regardless of the advertised order, otherwise the token exchange + // sends an empty-secret Basic header and is rejected with 401 access_denied (#1612). + TestOAuthServer.TokenEndpointAuthMethodsSupported = ["client_secret_basic", "client_secret_post", "private_key_jwt", "none"]; + await using var app = await StartMcpServerAsync(); + + await using var transport = new HttpClientTransport(new() + { + Endpoint = new(McpServerUrl), + OAuth = new ClientOAuthOptions() + { + RedirectUri = new Uri("http://localhost:1179/callback"), + AuthorizationCallbackHandler = HandleAuthorizationUrlAsync, + ClientMetadataDocumentUri = new Uri(ClientMetadataDocumentUrl), + DynamicClientRegistration = new() + { + ApplicationType = "web", + }, + }, + }, HttpClient, LoggerFactory); + + await using var client = await McpClient.CreateAsync( + transport, loggerFactory: LoggerFactory, cancellationToken: TestContext.Current.CancellationToken); + } + [Fact] public async Task CannotAuthenticate_WhenMetadataOmitsPkceMethods() { diff --git a/tests/ModelContextProtocol.TestOAuthServer/Program.cs b/tests/ModelContextProtocol.TestOAuthServer/Program.cs index 73663dc94..68239186b 100644 --- a/tests/ModelContextProtocol.TestOAuthServer/Program.cs +++ b/tests/ModelContextProtocol.TestOAuthServer/Program.cs @@ -80,6 +80,17 @@ public Program(ILoggerProvider? loggerProvider = null, IConnectionListenerFactor /// public bool ClientIdMetadataDocumentSupported { get; set; } = true; + /// + /// Gets or sets the token_endpoint_auth_methods_supported values the authorization server + /// advertises in its discovery document. Tests set this to a list that does not lead with + /// none (e.g. ["client_secret_basic", "none"], mirroring Auth0) to verify that a + /// CIMD public client still authenticates with none rather than the first advertised method. + /// + /// + /// The default value is ["client_secret_post"]. + /// + public List TokenEndpointAuthMethodsSupported { get; set; } = ["client_secret_post"]; + /// /// Gets or sets a value indicating whether the authorization server expects a resource parameter. /// When true, the resource parameter must be present and match a valid resource. @@ -276,7 +287,7 @@ IResult HandleMetadataRequest(HttpContext context, string? issuerPath = null) ScopesSupported = IncludeOfflineAccessInMetadata ? ["openid", "profile", "email", "mcp:tools", "offline_access"] : ["openid", "profile", "email", "mcp:tools"], - TokenEndpointAuthMethodsSupported = ["client_secret_post"], + TokenEndpointAuthMethodsSupported = TokenEndpointAuthMethodsSupported, ClaimsSupported = ["sub", "iss", "name", "email", "aud"], CodeChallengeMethodsSupported = MetadataPathsWithoutPkceSupport.Contains(context.Request.Path) ? null