@@ -28,10 +28,9 @@ public McpEndpointAuthorizationFilter(ILogger logger, IServerAuthorizationProvid
2828
2929 // Check if the Authorization header is present
3030 if ( ! httpContext . Request . Headers . TryGetValue ( "Authorization" , out var authHeader ) || string . IsNullOrEmpty ( authHeader ) )
31- {
32- // No Authorization header present, return 401 Unauthorized
31+ { // No Authorization header present, return 401 Unauthorized
3332 var prm = _authProvider . GetProtectedResourceMetadata ( ) ;
34- var prmUrl = GetPrmUrl ( httpContext , prm . Resource ) ;
33+ var prmUrl = ProtectedResourceMetadataHandler . GetProtectedResourceMetadataUrl ( prm . Resource ) ;
3534
3635 _logger . LogDebug ( "Authorization required, returning 401 Unauthorized with WWW-Authenticate header" ) ;
3736 httpContext . Response . StatusCode = StatusCodes . Status401Unauthorized ;
@@ -43,28 +42,15 @@ public McpEndpointAuthorizationFilter(ILogger logger, IServerAuthorizationProvid
4342 string authHeaderValue = authHeader . ToString ( ) ;
4443 bool isValid = await _authProvider . ValidateTokenAsync ( authHeaderValue ) ;
4544 if ( ! isValid )
46- {
47- // Invalid token, return 401 Unauthorized
45+ { // Invalid token, return 401 Unauthorized
4846 var prm = _authProvider . GetProtectedResourceMetadata ( ) ;
49- var prmUrl = GetPrmUrl ( httpContext , prm . Resource ) ;
47+ var prmUrl = ProtectedResourceMetadataHandler . GetProtectedResourceMetadataUrl ( prm . Resource ) ;
5048
5149 _logger . LogDebug ( "Invalid authorization token, returning 401 Unauthorized" ) ;
5250 httpContext . Response . StatusCode = StatusCodes . Status401Unauthorized ;
5351 httpContext . Response . Headers . Append ( "WWW-Authenticate" , $ "Bearer resource_metadata=\" { prmUrl } \" ") ;
5452 return Results . Empty ;
55- }
56-
57- // Token is valid, proceed to the next filter
53+ } // Token is valid, proceed to the next filter
5854 return await next ( context ) ;
59- } /// <summary>
60- /// Builds the URL for the protected resource metadata endpoint.
61- /// </summary>
62- /// <param name="context">The HTTP context.</param>
63- /// <param name="resourceUri">The resource URI from the protected resource metadata.</param>
64- /// <returns>The full URL to the protected resource metadata endpoint.</returns>
65- private static string GetPrmUrl ( HttpContext context , Uri resourceUri )
66- {
67- // Create a new URI with the well-known path appended
68- return new Uri ( resourceUri , ".well-known/oauth-protected-resource" ) . ToString ( ) ;
6955 }
7056}
0 commit comments