@@ -131,6 +131,85 @@ public async Task CanAuthenticate_WithClientMetadataDocument()
131131 transport , loggerFactory : LoggerFactory , cancellationToken : TestContext . Current . CancellationToken ) ;
132132 }
133133
134+ [ Fact ]
135+ public async Task CannotAuthenticate_WhenMetadataOmitsPkceMethods ( )
136+ {
137+ TestOAuthServer . CodeChallengeMethodsSupported = null ;
138+ await using var app = await StartMcpServerAsync ( ) ;
139+
140+ await using var transport = new HttpClientTransport ( new ( )
141+ {
142+ Endpoint = new ( McpServerUrl ) ,
143+ OAuth = new ( )
144+ {
145+ ClientId = "demo-client" ,
146+ ClientSecret = "demo-secret" ,
147+ RedirectUri = new Uri ( "http://localhost:1179/callback" ) ,
148+ AuthorizationRedirectDelegate = HandleAuthorizationUrlAsync ,
149+ } ,
150+ } , HttpClient , LoggerFactory ) ;
151+
152+ await Assert . ThrowsAsync < McpException > ( ( ) => McpClient . CreateAsync (
153+ transport , loggerFactory : LoggerFactory , cancellationToken : TestContext . Current . CancellationToken ) ) ;
154+
155+ // No discovery endpoint advertises PKCE, so metadata discovery is exhausted. The precise PKCE reason
156+ // is logged as each endpoint is skipped.
157+ Assert . Contains (
158+ MockLoggerProvider . LogMessages ,
159+ m => m . Exception ? . Message . Contains ( "code_challenge_methods_supported" ) == true ) ;
160+ }
161+
162+ [ Fact ]
163+ public async Task CannotAuthenticate_WhenMetadataLacksS256PkceMethod ( )
164+ {
165+ TestOAuthServer . CodeChallengeMethodsSupported = [ "plain" ] ;
166+ await using var app = await StartMcpServerAsync ( ) ;
167+
168+ await using var transport = new HttpClientTransport ( new ( )
169+ {
170+ Endpoint = new ( McpServerUrl ) ,
171+ OAuth = new ( )
172+ {
173+ ClientId = "demo-client" ,
174+ ClientSecret = "demo-secret" ,
175+ RedirectUri = new Uri ( "http://localhost:1179/callback" ) ,
176+ AuthorizationRedirectDelegate = HandleAuthorizationUrlAsync ,
177+ } ,
178+ } , HttpClient , LoggerFactory ) ;
179+
180+ await Assert . ThrowsAsync < McpException > ( ( ) => McpClient . CreateAsync (
181+ transport , loggerFactory : LoggerFactory , cancellationToken : TestContext . Current . CancellationToken ) ) ;
182+
183+ Assert . Contains (
184+ MockLoggerProvider . LogMessages ,
185+ m => m . Exception ? . Message . Contains ( "required PKCE method 'S256'" ) == true ) ;
186+ }
187+
188+ [ Fact ]
189+ public async Task CanAuthenticate_WhenFirstMetadataEndpointOmitsPkce_ButAnotherAdvertisesIt ( )
190+ {
191+ // The OAuth 2.0 authorization server metadata endpoint is tried before the OpenID Connect one.
192+ // Simulate a server where only the OpenID Connect document advertises PKCE support, and verify the
193+ // client falls through to it rather than failing on the first PKCE-less document.
194+ TestOAuthServer . MetadataPathsWithoutPkceSupport . Add ( "/.well-known/oauth-authorization-server" ) ;
195+ await using var app = await StartMcpServerAsync ( ) ;
196+
197+ await using var transport = new HttpClientTransport ( new ( )
198+ {
199+ Endpoint = new ( McpServerUrl ) ,
200+ OAuth = new ( )
201+ {
202+ ClientId = "demo-client" ,
203+ ClientSecret = "demo-secret" ,
204+ RedirectUri = new Uri ( "http://localhost:1179/callback" ) ,
205+ AuthorizationRedirectDelegate = HandleAuthorizationUrlAsync ,
206+ } ,
207+ } , HttpClient , LoggerFactory ) ;
208+
209+ await using var client = await McpClient . CreateAsync (
210+ transport , loggerFactory : LoggerFactory , cancellationToken : TestContext . Current . CancellationToken ) ;
211+ }
212+
134213 [ Fact ]
135214 public async Task UsesDynamicClientRegistration_WhenCimdNotSupported ( )
136215 {
@@ -1671,6 +1750,81 @@ public async Task CanAuthenticate_WithLegacyServerUsingDefaultEndpointFallback()
16711750 transport , loggerFactory : LoggerFactory , cancellationToken : TestContext . Current . CancellationToken ) ;
16721751 }
16731752
1753+ [ Fact ]
1754+ public async Task CannotAuthenticate_WithLegacyServerWhoseMetadataOmitsPkceMethods ( )
1755+ {
1756+ // 2025-03-26 backcompat regression guard: PRM is unavailable (resourceUri is null), but the server
1757+ // DOES serve an auth server metadata document that omits 'code_challenge_methods_supported'.
1758+ // The client must refuse to proceed rather than falling back to synthesized S256 defaults, since a
1759+ // discovered metadata document that fails PKCE validation disqualifies the legacy default fallback.
1760+ TestOAuthServer . ExpectResource = false ;
1761+
1762+ // Use JwtBearer as the challenge scheme so the 401 response does NOT include resource_metadata.
1763+ Builder . Services . Configure < AuthenticationOptions > ( options => options . DefaultChallengeScheme = JwtBearerDefaults . AuthenticationScheme ) ;
1764+
1765+ Builder . Services . Configure < JwtBearerOptions > ( JwtBearerDefaults . AuthenticationScheme , options =>
1766+ {
1767+ options . TokenValidationParameters . ValidateAudience = false ;
1768+ } ) ;
1769+
1770+ await using var app = Builder . Build ( ) ;
1771+
1772+ app . Use ( async ( context , next ) =>
1773+ {
1774+ // Return 404 for PRM to simulate a legacy server that doesn't support RFC 9728.
1775+ if ( context . Request . Path . StartsWithSegments ( "/.well-known/oauth-protected-resource" ) )
1776+ {
1777+ context . Response . StatusCode = StatusCodes . Status404NotFound ;
1778+ return ;
1779+ }
1780+
1781+ // Serve auth server metadata that omits 'code_challenge_methods_supported' entirely.
1782+ if ( context . Request . Path . StartsWithSegments ( "/.well-known/oauth-authorization-server" ) ||
1783+ context . Request . Path . StartsWithSegments ( "/.well-known/openid-configuration" ) )
1784+ {
1785+ context . Response . ContentType = "application/json" ;
1786+ await context . Response . WriteAsync ( $$ """
1787+ {
1788+ "issuer": "{{ OAuthServerUrl }} ",
1789+ "authorization_endpoint": "{{ McpServerUrl }} /authorize",
1790+ "token_endpoint": "{{ McpServerUrl }} /token",
1791+ "registration_endpoint": "{{ McpServerUrl }} /register",
1792+ "response_types_supported": ["code"],
1793+ "grant_types_supported": ["authorization_code", "refresh_token"],
1794+ "token_endpoint_auth_methods_supported": ["client_secret_post"]
1795+ }
1796+ """ ) ;
1797+ return ;
1798+ }
1799+
1800+ await next ( ) ;
1801+ } ) ;
1802+
1803+ app . UseAuthentication ( ) ;
1804+ app . UseAuthorization ( ) ;
1805+ app . MapMcp ( ) . RequireAuthorization ( ) ;
1806+ await app . StartAsync ( TestContext . Current . CancellationToken ) ;
1807+
1808+ await using var transport = new HttpClientTransport ( new ( )
1809+ {
1810+ Endpoint = new ( McpServerUrl ) ,
1811+ OAuth = new ( )
1812+ {
1813+ ClientId = "demo-client" ,
1814+ ClientSecret = "demo-secret" ,
1815+ RedirectUri = new Uri ( "http://localhost:1179/callback" ) ,
1816+ AuthorizationRedirectDelegate = HandleAuthorizationUrlAsync ,
1817+ } ,
1818+ } , HttpClient , LoggerFactory ) ;
1819+
1820+ var ex = await Assert . ThrowsAsync < McpException > ( ( ) => McpClient . CreateAsync (
1821+ transport , loggerFactory : LoggerFactory , cancellationToken : TestContext . Current . CancellationToken ) ) ;
1822+
1823+ // The specific PKCE failure reason is surfaced rather than a generic discovery failure or a
1824+ // silently-synthesized S256 fallback.
1825+ Assert . Contains ( "code_challenge_methods_supported" , ex . Message ) ;
1826+ }
1827+
16741828 [ Fact ]
16751829 public async Task AuthorizationFlow_AppendsOfflineAccess_WhenServerAdvertisesIt ( )
16761830 {
0 commit comments