File tree Expand file tree Collapse file tree 3 files changed +2
-21
lines changed
ModelContextProtocol.AspNetCore/Authentication
ModelContextProtocol/Authentication Expand file tree Collapse file tree 3 files changed +2
-21
lines changed Original file line number Diff line number Diff line change @@ -25,10 +25,8 @@ public static AuthorizationOptions AddMcpPolicy(
2525 . RequireAuthenticatedUser ( )
2626 . AddAuthenticationSchemes ( McpAuthenticationDefaults . AuthenticationScheme ) ;
2727
28- // Allow additional configuration if provided
2928 configurePolicy ? . Invoke ( policyBuilder ) ;
3029
31- // Add the configured policy
3230 options . AddPolicy ( policyName , policyBuilder . Build ( ) ) ;
3331
3432 return options ;
@@ -53,16 +51,13 @@ public static AuthorizationOptions AddMcpPolicy(
5351 return AddMcpPolicy ( options , policyName , configurePolicy ) ;
5452 }
5553
56- // Create a policy builder with MCP and additional authentication schemes
5754 var allSchemes = new [ ] { McpAuthenticationDefaults . AuthenticationScheme } . Concat ( additionalSchemes ) . ToArray ( ) ;
5855
5956 var policyBuilder = new AuthorizationPolicyBuilder ( allSchemes )
6057 . RequireAuthenticatedUser ( ) ;
6158
62- // Allow additional configuration if provided
6359 configurePolicy ? . Invoke ( policyBuilder ) ;
6460
65- // Add the configured policy
6661 options . AddPolicy ( policyName , policyBuilder . Build ( ) ) ;
6762
6863 return options ;
Original file line number Diff line number Diff line change @@ -23,32 +23,25 @@ public AuthorizationDelegatingHandler(IMcpAuthorizationProvider authorizationPro
2323 /// </summary>
2424 protected override async Task < HttpResponseMessage > SendAsync ( HttpRequestMessage request , CancellationToken cancellationToken )
2525 {
26- // Add the authentication token to the request if not already present
2726 if ( request . Headers . Authorization == null )
2827 {
2928 await AddAuthorizationHeaderAsync ( request , cancellationToken ) ;
3029 }
3130
32- // Send the request through the inner handler
3331 var response = await base . SendAsync ( request , cancellationToken ) ;
3432
35- // Handle unauthorized responses
3633 if ( response . StatusCode == System . Net . HttpStatusCode . Unauthorized )
3734 {
38- // Try to handle the unauthorized response
3935 var handled = await _authorizationProvider . HandleUnauthorizedResponseAsync (
4036 response ,
4137 cancellationToken ) ;
4238
4339 if ( handled )
4440 {
45- // If the unauthorized response was handled, retry the request
4641 var retryRequest = await CloneHttpRequestMessageAsync ( request ) ;
4742
48- // Get a new token
4943 await AddAuthorizationHeaderAsync ( retryRequest , cancellationToken ) ;
5044
51- // Send the retry request
5245 return await base . SendAsync ( retryRequest , cancellationToken ) ;
5346 }
5447 }
Original file line number Diff line number Diff line change @@ -110,16 +110,9 @@ public static async Task<ProtectedResourceMetadata> ExtractProtectedResourceMeta
110110 throw new InvalidOperationException ( "The WWW-Authenticate header does not contain a resource_metadata parameter" ) ;
111111 }
112112
113- Uri metadataUri = new Uri ( resourceMetadataUrl ) ;
113+ Uri metadataUri = new ( resourceMetadataUrl ) ;
114114
115- // Fetch the resource metadata
116- var metadata = await FetchProtectedResourceMetadataAsync ( metadataUri , cancellationToken ) ;
117- if ( metadata == null )
118- {
119- throw new InvalidOperationException ( $ "Failed to fetch resource metadata from { resourceMetadataUrl } ") ;
120- }
121-
122- // Verify the resource matches the server
115+ var metadata = await FetchProtectedResourceMetadataAsync ( metadataUri , cancellationToken ) ?? throw new InvalidOperationException ( $ "Failed to fetch resource metadata from { resourceMetadataUrl } ") ;
123116 if ( ! VerifyResourceMatch ( metadata , serverUrl ) )
124117 {
125118 throw new InvalidOperationException (
You can’t perform that action at this time.
0 commit comments