Skip to content

Commit ecc40ab

Browse files
committed
Tinkering with test logic
1 parent 3e9462c commit ecc40ab

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

samples/AuthorizationExample/Program.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class Program
1212
public static async Task Main(string[] args)
1313
{
1414
// Define the MCP server endpoint that requires OAuth authentication
15-
var serverEndpoint = new Uri("https://example.com/mcp");
15+
var serverEndpoint = new Uri("http://localhost:7071/sse");
1616

1717
// Set up the SSE transport with authorization support
1818
var transportOptions = new SseClientTransportOptions
@@ -21,7 +21,6 @@ public static async Task Main(string[] args)
2121
AuthorizeCallback = SseClientTransport.CreateLocalServerAuthorizeCallback(
2222
openBrowser: async (url) =>
2323
{
24-
// Open the URL in the user's default browser
2524
Process.Start(new ProcessStartInfo(url) { UseShellExecute = true });
2625
}
2726
)

samples/AuthorizationServerExample/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static async Task Main(string[] args)
2424
var prm = new ProtectedResourceMetadata
2525
{
2626
Resource = "http://localhost:7071", // Changed from HTTPS to HTTP for local development
27-
AuthorizationServers = ["https://auth.example.com"], // Auth servers that can issue tokens for this resource
27+
AuthorizationServers = ["https://login.microsoftonline.com/a2213e1c-e51e-4304-9a0d-effe57f31655/v2.0"], // Let's use a dummy Entra ID tenant here
2828
BearerMethodsSupported = ["header"], // We support the Authorization header
2929
ScopesSupported = ["mcp.tools", "mcp.prompts", "mcp.resources"], // Scopes supported by this resource
3030
ResourceDocumentation = "https://example.com/docs/mcp-server-auth" // Optional documentation URL

src/ModelContextProtocol/Protocol/Auth/AuthorizationContext.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,15 @@ public bool ValidateResourceUrl(string resourceUrl)
8585
return false;
8686
}
8787

88-
// Resource URL must match exactly
88+
// Compare the host part (FQDN) rather than the full URL
89+
if (Uri.TryCreate(resourceUrl, UriKind.Absolute, out Uri? resourceUri) &&
90+
Uri.TryCreate(ResourceMetadata.Resource, UriKind.Absolute, out Uri? metadataUri))
91+
{
92+
// Compare only the host (domain name)
93+
return string.Equals(resourceUri.Host, metadataUri.Host, StringComparison.OrdinalIgnoreCase);
94+
}
95+
96+
// If we can't parse both URLs, fall back to exact string comparison
8997
return string.Equals(resourceUrl, ResourceMetadata.Resource, StringComparison.OrdinalIgnoreCase);
9098
}
9199
}

src/ModelContextProtocol/Protocol/Auth/DefaultAuthorizationHandler.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,11 @@ public async Task<bool> HandleUnauthorizedResponseAsync(HttpResponseMessage resp
7575
throw exception;
7676
}
7777

78+
// Store the resource metadata in the context before validating the resource URL
79+
authContext.Value.ResourceMetadata = resourceMetadata;
80+
7881
// Validate that the resource matches the server FQDN
79-
if (!authContext.Value.ValidateResourceUrl(serverUri.ToString()) &&
80-
!string.Equals(resourceMetadata.Resource, serverUri.ToString(), StringComparison.OrdinalIgnoreCase))
82+
if (!authContext.Value.ValidateResourceUrl(serverUri.ToString()))
8183
{
8284
_logger.LogWarning("Resource URL mismatch: expected {Expected}, got {Actual}",
8385
serverUri, resourceMetadata.Resource);
@@ -87,8 +89,6 @@ public async Task<bool> HandleUnauthorizedResponseAsync(HttpResponseMessage resp
8789
throw exception;
8890
}
8991

90-
authContext.Value.ResourceMetadata = resourceMetadata;
91-
9292
// Get the first authorization server from the metadata
9393
if (resourceMetadata.AuthorizationServers == null || resourceMetadata.AuthorizationServers.Length == 0)
9494
{

0 commit comments

Comments
 (0)