From 01c0d10d3452ee613994fce03aa5c5ce38fcb52a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Chalet?= Date: Tue, 22 Apr 2025 11:27:12 +0200 Subject: [PATCH] Reduce the number of OpenIddictParameter.GetUnnamedParameters() calls --- .../OpenIddictClientHandlers.Discovery.cs | 120 +++++------------- .../OpenIddictValidationHandlers.Discovery.cs | 12 +- 2 files changed, 33 insertions(+), 99 deletions(-) diff --git a/src/OpenIddict.Client/OpenIddictClientHandlers.Discovery.cs b/src/OpenIddict.Client/OpenIddictClientHandlers.Discovery.cs index d6c24636..8e5f021a 100644 --- a/src/OpenIddict.Client/OpenIddictClientHandlers.Discovery.cs +++ b/src/OpenIddict.Client/OpenIddictClientHandlers.Discovery.cs @@ -903,17 +903,11 @@ public static partial class OpenIddictClientHandlers } // Resolve the grant types supported by the authorization endpoint, if available. - var types = context.Response[Metadata.GrantTypesSupported]?.GetUnnamedParameters(); - if (types is { Count: > 0 }) + foreach (var type in (ImmutableArray?) context.Response[Metadata.GrantTypesSupported] ?? []) { - for (var index = 0; index < types.Count; index++) + if (!string.IsNullOrEmpty(type)) { - // Note: custom values are allowed in this case. - var type = (string?) types[index]; - if (!string.IsNullOrEmpty(type)) - { - context.Configuration.GrantTypesSupported.Add(type); - } + context.Configuration.GrantTypesSupported.Add(type); } } @@ -945,17 +939,11 @@ public static partial class OpenIddictClientHandlers } // Resolve the response modes supported by the authorization endpoint, if available. - var modes = context.Response[Metadata.ResponseModesSupported]?.GetUnnamedParameters(); - if (modes is { Count: > 0 }) + foreach (var mode in (ImmutableArray?) context.Response[Metadata.ResponseModesSupported] ?? []) { - for (var index = 0; index < modes.Count; index++) + if (!string.IsNullOrEmpty(mode)) { - // Note: custom values are allowed in this case. - var mode = (string?) modes[index]; - if (!string.IsNullOrEmpty(mode)) - { - context.Configuration.ResponseModesSupported.Add(mode); - } + context.Configuration.ResponseModesSupported.Add(mode); } } @@ -987,17 +975,11 @@ public static partial class OpenIddictClientHandlers } // Resolve the response types supported by the authorization endpoint, if available. - var types = context.Response[Metadata.ResponseTypesSupported]?.GetUnnamedParameters(); - if (types is { Count: > 0 }) + foreach (var type in (ImmutableArray?) context.Response[Metadata.ResponseTypesSupported] ?? []) { - for (var index = 0; index < types.Count; index++) + if (!string.IsNullOrEmpty(type)) { - // Note: custom values are allowed in this case. - var type = (string?) types[index]; - if (!string.IsNullOrEmpty(type)) - { - context.Configuration.ResponseTypesSupported.Add(type); - } + context.Configuration.ResponseTypesSupported.Add(type); } } @@ -1029,17 +1011,11 @@ public static partial class OpenIddictClientHandlers } // Resolve the code challenge methods supported by the authorization endpoint, if available. - var methods = context.Response[Metadata.CodeChallengeMethodsSupported]?.GetUnnamedParameters(); - if (methods is { Count: > 0 }) + foreach (var method in (ImmutableArray?) context.Response[Metadata.CodeChallengeMethodsSupported] ?? []) { - for (var index = 0; index < methods.Count; index++) + if (!string.IsNullOrEmpty(method)) { - // Note: custom values are allowed in this case. - var method = (string?) methods[index]; - if (!string.IsNullOrEmpty(method)) - { - context.Configuration.CodeChallengeMethodsSupported.Add(method); - } + context.Configuration.CodeChallengeMethodsSupported.Add(method); } } @@ -1071,17 +1047,11 @@ public static partial class OpenIddictClientHandlers } // Resolve the scopes supported by the remote server, if available. - var scopes = context.Response[Metadata.ScopesSupported]?.GetUnnamedParameters(); - if (scopes is { Count: > 0 }) + foreach (var scope in (ImmutableArray?) context.Response[Metadata.ScopesSupported] ?? []) { - for (var index = 0; index < scopes.Count; index++) + if (!string.IsNullOrEmpty(scope)) { - // Note: custom values are allowed in this case. - var scope = (string?) scopes[index]; - if (!string.IsNullOrEmpty(scope)) - { - context.Configuration.ScopesSupported.Add(scope); - } + context.Configuration.ScopesSupported.Add(scope); } } @@ -1210,17 +1180,11 @@ public static partial class OpenIddictClientHandlers // // Note: "device_authorization_endpoint_auth_methods_supported" is not a standard parameter // but is supported by OpenIddict 4.3.0 and higher for consistency with the other endpoints. - var methods = context.Response[Metadata.DeviceAuthorizationEndpointAuthMethodsSupported]?.GetUnnamedParameters(); - if (methods is { Count: > 0 }) + foreach (var method in (ImmutableArray?) context.Response[Metadata.DeviceAuthorizationEndpointAuthMethodsSupported] ?? []) { - for (var index = 0; index < methods.Count; index++) + if (!string.IsNullOrEmpty(method)) { - // Note: custom values are allowed in this case. - var method = (string?) methods[index]; - if (!string.IsNullOrEmpty(method)) - { - context.Configuration.DeviceAuthorizationEndpointAuthMethodsSupported.Add(method); - } + context.Configuration.DeviceAuthorizationEndpointAuthMethodsSupported.Add(method); } } @@ -1253,17 +1217,11 @@ public static partial class OpenIddictClientHandlers } // Resolve the client authentication methods supported by the introspection endpoint, if available. - var methods = context.Response[Metadata.IntrospectionEndpointAuthMethodsSupported]?.GetUnnamedParameters(); - if (methods is { Count: > 0 }) + foreach (var method in (ImmutableArray?) context.Response[Metadata.IntrospectionEndpointAuthMethodsSupported] ?? []) { - for (var index = 0; index < methods.Count; index++) + if (!string.IsNullOrEmpty(method)) { - // Note: custom values are allowed in this case. - var method = (string?) methods[index]; - if (!string.IsNullOrEmpty(method)) - { - context.Configuration.IntrospectionEndpointAuthMethodsSupported.Add(method); - } + context.Configuration.IntrospectionEndpointAuthMethodsSupported.Add(method); } } @@ -1299,17 +1257,11 @@ public static partial class OpenIddictClientHandlers // // Note: "pushed_authorization_request_endpoint_auth_methods_supported" is not a standard parameter // but is supported by OpenIddict 6.1.0 and higher for consistency with the other endpoints. - var methods = context.Response[Metadata.PushedAuthorizationRequestEndpointAuthMethodsSupported]?.GetUnnamedParameters(); - if (methods is { Count: > 0 }) + foreach (var method in (ImmutableArray?) context.Response[Metadata.PushedAuthorizationRequestEndpointAuthMethodsSupported] ?? []) { - for (var index = 0; index < methods.Count; index++) + if (!string.IsNullOrEmpty(method)) { - // Note: custom values are allowed in this case. - var method = (string?) methods[index]; - if (!string.IsNullOrEmpty(method)) - { - context.Configuration.PushedAuthorizationEndpointAuthMethodsSupported.Add(method); - } + context.Configuration.PushedAuthorizationEndpointAuthMethodsSupported.Add(method); } } @@ -1342,17 +1294,11 @@ public static partial class OpenIddictClientHandlers } // Resolve the client authentication methods supported by the revocation endpoint, if available. - var methods = context.Response[Metadata.RevocationEndpointAuthMethodsSupported]?.GetUnnamedParameters(); - if (methods is { Count: > 0 }) + foreach (var method in (ImmutableArray?) context.Response[Metadata.RevocationEndpointAuthMethodsSupported] ?? []) { - for (var index = 0; index < methods.Count; index++) + if (!string.IsNullOrEmpty(method)) { - // Note: custom values are allowed in this case. - var method = (string?) methods[index]; - if (!string.IsNullOrEmpty(method)) - { - context.Configuration.RevocationEndpointAuthMethodsSupported.Add(method); - } + context.Configuration.RevocationEndpointAuthMethodsSupported.Add(method); } } @@ -1385,17 +1331,11 @@ public static partial class OpenIddictClientHandlers } // Resolve the client authentication methods supported by the token endpoint, if available. - var methods = context.Response[Metadata.TokenEndpointAuthMethodsSupported]?.GetUnnamedParameters(); - if (methods is { Count: > 0 }) + foreach (var method in (ImmutableArray?) context.Response[Metadata.TokenEndpointAuthMethodsSupported] ?? []) { - for (var index = 0; index < methods.Count; index++) + if (!string.IsNullOrEmpty(method)) { - // Note: custom values are allowed in this case. - var method = (string?) methods[index]; - if (!string.IsNullOrEmpty(method)) - { - context.Configuration.TokenEndpointAuthMethodsSupported.Add(method); - } + context.Configuration.TokenEndpointAuthMethodsSupported.Add(method); } } diff --git a/src/OpenIddict.Validation/OpenIddictValidationHandlers.Discovery.cs b/src/OpenIddict.Validation/OpenIddictValidationHandlers.Discovery.cs index 19684dab..c79e2fd9 100644 --- a/src/OpenIddict.Validation/OpenIddictValidationHandlers.Discovery.cs +++ b/src/OpenIddict.Validation/OpenIddictValidationHandlers.Discovery.cs @@ -374,17 +374,11 @@ public static partial class OpenIddictValidationHandlers } // Resolve the client authentication methods supported by the introspection endpoint, if available. - var methods = context.Response[Metadata.IntrospectionEndpointAuthMethodsSupported]?.GetUnnamedParameters(); - if (methods is { Count: > 0 }) + foreach (var method in (ImmutableArray?) context.Response[Metadata.IntrospectionEndpointAuthMethodsSupported] ?? []) { - for (var index = 0; index < methods.Count; index++) + if (!string.IsNullOrEmpty(method)) { - // Note: custom values are allowed in this case. - var method = (string?) methods[index]; - if (!string.IsNullOrEmpty(method)) - { - context.Configuration.IntrospectionEndpointAuthMethodsSupported.Add(method); - } + context.Configuration.IntrospectionEndpointAuthMethodsSupported.Add(method); } }