Skip to content

Commit c406230

Browse files
committed
Changes based on feedback
1 parent 7b6c9fd commit c406230

2 files changed

Lines changed: 15 additions & 24 deletions

File tree

samples/ProtectedMCPClient/BasicOAuthAuthorizationProvider.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ public class BasicOAuthAuthorizationProvider : ITokenProvider
2727
// Lazy-initialized shared HttpClient for when no client is provided
2828
private static readonly Lazy<HttpClient> _defaultHttpClient = new(() => new HttpClient());
2929

30+
// Cached JsonSerializerOptions to avoid recreating it for each deserialization
31+
private static readonly JsonSerializerOptions _jsonOptions = new() { PropertyNameCaseInsensitive = true };
32+
3033
private TokenContainer? _token;
3134
private AuthorizationServerMetadata? _authServerMetadata;
3235

@@ -159,9 +162,17 @@ public BasicOAuthAuthorizationProvider(
159162
{
160163
var json = await response.Content.ReadAsStringAsync(cancellationToken);
161164
var metadata = JsonSerializer.Deserialize<AuthorizationServerMetadata>(
162-
json, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
165+
json, _jsonOptions);
163166

164-
if (metadata != null) return metadata;
167+
if (metadata != null)
168+
{
169+
metadata.ResponseTypesSupported ??= ["code"];
170+
metadata.GrantTypesSupported ??= ["authorization_code", "refresh_token"];
171+
metadata.TokenEndpointAuthMethodsSupported ??= ["client_secret_basic"];
172+
metadata.CodeChallengeMethodsSupported ??= ["S256"];
173+
174+
return metadata;
175+
}
165176
}
166177
}
167178
catch (Exception ex)
@@ -200,7 +211,7 @@ public BasicOAuthAuthorizationProvider(
200211
{
201212
var json = await response.Content.ReadAsStringAsync(cancellationToken);
202213
var tokenResponse = JsonSerializer.Deserialize<TokenContainer>(
203-
json, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
214+
json, _jsonOptions);
204215

205216
if (tokenResponse != null)
206217
{
@@ -341,7 +352,7 @@ private Uri BuildAuthorizationUrl(AuthorizationServerMetadata authServerMetadata
341352
{
342353
var json = await response.Content.ReadAsStringAsync(cancellationToken);
343354
var tokenResponse = JsonSerializer.Deserialize<TokenContainer>(
344-
json, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
355+
json, _jsonOptions);
345356

346357
if (tokenResponse != null)
347358
{

samples/ProtectedMCPClient/Types/AuthorizationServerMetadata.cs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,4 @@ public class AuthorizationServerMetadata
6666
/// </summary>
6767
[JsonPropertyName("scopes_supported")]
6868
public List<string>? ScopesSupported { get; set; }
69-
70-
/// <summary>
71-
/// Gets the response types supported by the authorization server or returns the default.
72-
/// </summary>
73-
public IReadOnlyList<string> GetResponseTypesSupported() => ResponseTypesSupported ?? new List<string> { "code" };
74-
75-
/// <summary>
76-
/// Gets the grant types supported by the authorization server or returns the default.
77-
/// </summary>
78-
public IReadOnlyList<string> GetGrantTypesSupported() => GrantTypesSupported ?? new List<string> { "authorization_code", "refresh_token" };
79-
80-
/// <summary>
81-
/// Gets the token endpoint authentication methods supported by the authorization server or returns the default.
82-
/// </summary>
83-
public IReadOnlyList<string> GetTokenEndpointAuthMethodsSupported() => TokenEndpointAuthMethodsSupported ?? new List<string> { "client_secret_basic" };
84-
85-
/// <summary>
86-
/// Gets the code challenge methods supported by the authorization server or returns the default.
87-
/// </summary>
88-
public IReadOnlyList<string> GetCodeChallengeMethodsSupported() => CodeChallengeMethodsSupported ?? new List<string> { "S256" };
8969
}

0 commit comments

Comments
 (0)