Skip to content

Commit 22962cf

Browse files
committed
Minor optimizations to the delegating handler
1 parent 4fbf2e9 commit 22962cf

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/ModelContextProtocol/Authentication/AuthorizationDelegatingHandler.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,20 @@ private async Task<HttpResponseMessage> HandleUnauthorizedResponseAsync(
5959
var serverSchemes = ExtractServerSupportedSchemes(response);
6060

6161
// Find the intersection between what the server supports and what our provider supports
62-
var supportedSchemes = _authorizationProvider.SupportedSchemes.ToList();
6362
string? bestSchemeMatch = null;
6463

6564
// First try to find a direct match with the current scheme if it's still valid
6665
string schemeUsed = originalRequest.Headers.Authorization?.Scheme ?? _currentScheme ?? string.Empty;
67-
if (serverSchemes.Contains(schemeUsed) && supportedSchemes.Contains(schemeUsed))
66+
if (!string.IsNullOrEmpty(schemeUsed) &&
67+
serverSchemes.Contains(schemeUsed) &&
68+
_authorizationProvider.SupportedSchemes.Contains(schemeUsed))
6869
{
6970
bestSchemeMatch = schemeUsed;
7071
}
7172
else
7273
{
7374
// Find the first server scheme that's in our supported set
74-
bestSchemeMatch = serverSchemes.Intersect(supportedSchemes, StringComparer.OrdinalIgnoreCase).FirstOrDefault();
75+
bestSchemeMatch = serverSchemes.Intersect(_authorizationProvider.SupportedSchemes, StringComparer.OrdinalIgnoreCase).FirstOrDefault();
7576

7677
// If no match was found, either throw an exception or use default
7778
if (bestSchemeMatch is null)
@@ -80,11 +81,11 @@ private async Task<HttpResponseMessage> HandleUnauthorizedResponseAsync(
8081
{
8182
throw new InvalidOperationException(
8283
$"No matching authentication scheme found. Server supports: [{string.Join(", ", serverSchemes)}], " +
83-
$"Provider supports: [{string.Join(", ", supportedSchemes)}].");
84+
$"Provider supports: [{string.Join(", ", _authorizationProvider.SupportedSchemes)}].");
8485
}
8586

8687
// If the server didn't specify any schemes, use the provider's default
87-
bestSchemeMatch = supportedSchemes.FirstOrDefault();
88+
bestSchemeMatch = _authorizationProvider.SupportedSchemes.FirstOrDefault();
8889
}
8990
}
9091

@@ -148,9 +149,9 @@ private async Task<HttpResponseMessage> HandleUnauthorizedResponseAsync(
148149
/// <summary>
149150
/// Extracts the authentication schemes that the server supports from the WWW-Authenticate headers.
150151
/// </summary>
151-
private static List<string> ExtractServerSupportedSchemes(HttpResponseMessage response)
152+
private static HashSet<string> ExtractServerSupportedSchemes(HttpResponseMessage response)
152153
{
153-
var serverSchemes = new List<string>();
154+
var serverSchemes = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
154155

155156
if (response.Headers.Contains("WWW-Authenticate"))
156157
{

0 commit comments

Comments
 (0)