Skip to content

Commit 4d37991

Browse files
committed
Functional cleanup
1 parent c88e473 commit 4d37991

File tree

3 files changed

+22
-39
lines changed

3 files changed

+22
-39
lines changed

src/ModelContextProtocol.AspNetCore/AuthorizationMiddleware.cs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,25 +58,8 @@ await JsonSerializer.SerializeAsync(
5858
authProvider.GetProtectedResourceMetadata(),
5959
McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(ProtectedResourceMetadata)));
6060
return;
61-
}
62-
63-
// Proceed to the next middleware - authorization for SSE and message endpoints
61+
} // Proceed to the next middleware - authorization for SSE and message endpoints
6462
// is now handled by endpoint filters
6563
await _next(context);
6664
}
67-
68-
private static string GetPrmUrl(HttpContext context, string resourceUri)
69-
{
70-
// Use the actual resource URI from PRM if it's an absolute URL, otherwise build the URL
71-
if (Uri.TryCreate(resourceUri, UriKind.Absolute, out _))
72-
{
73-
return $"{resourceUri.TrimEnd('/')}/.well-known/oauth-protected-resource";
74-
}
75-
76-
// Build the URL from the current request
77-
var request = context.Request;
78-
var scheme = request.Scheme;
79-
var host = request.Host.Value;
80-
return $"{scheme}://{host}/.well-known/oauth-protected-resource";
81-
}
8265
}

src/ModelContextProtocol.AspNetCore/McpEndpointAuthorizationFilter.cs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/ModelContextProtocol.AspNetCore/ProtectedResourceMetadataHandler.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,26 @@ public ProtectedResourceMetadataHandler(
3333
/// <param name="context">The HTTP context.</param>
3434
/// <returns>A task that represents the asynchronous operation.</returns>
3535
public async Task HandleAsync(HttpContext context)
36-
{
37-
_logger.LogDebug("Serving Protected Resource Metadata document");
36+
{ _logger.LogDebug("Serving Protected Resource Metadata document");
3837
context.Response.ContentType = "application/json";
3938
await JsonSerializer.SerializeAsync(
4039
context.Response.Body,
4140
_authProvider.GetProtectedResourceMetadata(),
4241
McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(ProtectedResourceMetadata)));
42+
} /// <summary>
43+
/// Builds the URL for the protected resource metadata endpoint.
44+
/// </summary>
45+
/// <param name="resourceUri">The resource URI from the protected resource metadata.</param>
46+
/// <returns>The full URL to the protected resource metadata endpoint.</returns>
47+
/// <exception cref="ArgumentNullException">Thrown when resourceUri is null.</exception>
48+
public static string GetProtectedResourceMetadataUrl(Uri resourceUri)
49+
{
50+
if (resourceUri == null)
51+
{
52+
throw new ArgumentNullException(nameof(resourceUri), "Resource URI must be provided to build the protected resource metadata URL.");
53+
}
54+
55+
// Create a new URI with the well-known path appended
56+
return new Uri(resourceUri, ".well-known/oauth-protected-resource").ToString();
4357
}
4458
}

0 commit comments

Comments
 (0)