From 99e4ca1404b9ac36a14769c90032b2979afa76c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Chalet?= Date: Sun, 11 May 2025 16:56:53 +0200 Subject: [PATCH] Update the provider generator to support generating string constants --- ...OpenIddictClientWebIntegrationGenerator.cs | 69 ++++++++----- .../OpenIddictClientWebIntegrationHandlers.cs | 4 +- ...penIddictClientWebIntegrationProviders.xml | 99 +++++++++++++++++-- ...penIddictClientWebIntegrationProviders.xsd | 42 ++++++++ 4 files changed, 178 insertions(+), 36 deletions(-) diff --git a/gen/OpenIddict.Client.WebIntegration.Generators/OpenIddictClientWebIntegrationGenerator.cs b/gen/OpenIddict.Client.WebIntegration.Generators/OpenIddictClientWebIntegrationGenerator.cs index d410ea4e..d7eadd5f 100644 --- a/gen/OpenIddict.Client.WebIntegration.Generators/OpenIddictClientWebIntegrationGenerator.cs +++ b/gen/OpenIddict.Client.WebIntegration.Generators/OpenIddictClientWebIntegrationGenerator.cs @@ -805,7 +805,7 @@ public sealed partial class OpenIddictClientWebIntegrationBuilder return template.Render(new { Providers = document.Root.Elements("Provider") - .Select(provider => new + .Select(static provider => new { Name = (string) provider.Attribute("Name"), DisplayName = (string?) provider.Attribute("DisplayName") ?? (string) provider.Attribute("Name"), @@ -813,13 +813,13 @@ public sealed partial class OpenIddictClientWebIntegrationBuilder Obsolete = (bool?) provider.Attribute("Obsolete") ?? false, - Environments = provider.Elements("Environment").Select(environment => new + Environments = provider.Elements("Environment").Select(static environment => new { Name = (string?) environment.Attribute("Name") ?? "Production" }) .ToList(), - Settings = provider.Elements("Setting").Select(setting => new + Settings = provider.Elements("Setting").Select(static setting => new { PropertyName = (string) setting.Attribute("PropertyName"), ParameterName = (string) setting.Attribute("ParameterName"), @@ -886,6 +886,15 @@ public static partial class OpenIddictClientWebIntegrationConstants public const string {{ property.name }} = ""{{ property.dictionary_key }}""; {{~ end ~}} } + + {{~ for group in provider.constants ~}} + public static class {{ group.key }} + { + {{~ for constant in group ~}} + public const string {{ constant.name }} = ""{{ constant.value }}""; + {{~ end ~}} + } + {{~ end ~}} } {{~ end ~}} @@ -907,23 +916,33 @@ public static partial class OpenIddictClientWebIntegrationConstants return template.Render(new { Providers = document.Root.Elements("Provider") - .Select(provider => new + .Select(static provider => new { Name = (string) provider.Attribute("Name"), Id = (string) provider.Attribute("Id"), - Environments = provider.Elements("Environment").Select(environment => new + Environments = provider.Elements("Environment").Select(static environment => new { Name = (string?) environment.Attribute("Name") ?? "Production" }) .ToList(), - Properties = provider.Elements("Property").Select(property => new + Properties = provider.Elements("Property").Select(static property => new { Name = (string) property.Attribute("Name"), DictionaryKey = (string) property.Attribute("DictionaryKey") }) .ToList(), + + Constants = provider.Elements("Constant") + .Select(static constant => new + { + Class = (string) constant.Attribute("Class"), + Name = (string) constant.Attribute("Name"), + Value = (string) constant.Attribute("Value") + }) + .GroupBy(static constant => constant.Class) + .ToList(), }) .ToList() }); @@ -1289,12 +1308,12 @@ public sealed partial class OpenIddictClientWebIntegrationConfiguration return template.Render(new { Providers = document.Root.Elements("Provider") - .Select(provider => new + .Select(static provider => new { Name = (string) provider.Attribute("Name"), DisplayName = (string?) provider.Attribute("DisplayName") ?? (string) provider.Attribute("Name"), - Environments = provider.Elements("Environment").Select(environment => new + Environments = provider.Elements("Environment").Select(static environment => new { Name = (string?) environment.Attribute("Name") ?? "Production", @@ -1314,14 +1333,14 @@ public sealed partial class OpenIddictClientWebIntegrationConfiguration CodeChallengeMethodsSupported = configuration.Elements("CodeChallengeMethod").ToList() switch { - { Count: > 0 } methods => methods.Select(type => (string?) type.Attribute("Value")).ToList(), + { Count: > 0 } methods => methods.Select(static type => (string?) type.Attribute("Value")).ToList(), _ => [] }, GrantTypesSupported = configuration.Elements("GrantType").ToList() switch { - { Count: > 0 } types => types.Select(type => (string?) type.Attribute("Value")).ToList(), + { Count: > 0 } types => types.Select(static type => (string?) type.Attribute("Value")).ToList(), // If no explicit grant type was set, assume the provider only supports the code flow. _ => [GrantTypes.AuthorizationCode] @@ -1329,7 +1348,7 @@ public sealed partial class OpenIddictClientWebIntegrationConfiguration ResponseModesSupported = configuration.Elements("ResponseMode").ToList() switch { - { Count: > 0 } modes => modes.Select(type => (string?) type.Attribute("Value")).ToList(), + { Count: > 0 } modes => modes.Select(static type => (string?) type.Attribute("Value")).ToList(), // If no explicit response mode was set, assume the provider only supports the query response mode. _ => [ResponseModes.Query] @@ -1337,7 +1356,7 @@ public sealed partial class OpenIddictClientWebIntegrationConfiguration ResponseTypesSupported = configuration.Elements("ResponseType").ToList() switch { - { Count: > 0 } types => types.Select(type => (string?) type.Attribute("Value")).ToList(), + { Count: > 0 } types => types.Select(static type => (string?) type.Attribute("Value")).ToList(), // If no explicit response type was set, assume the provider only supports the code flow. _ => [ResponseTypes.Code] @@ -1345,14 +1364,14 @@ public sealed partial class OpenIddictClientWebIntegrationConfiguration ScopesSupported = configuration.Elements("Scope").ToList() switch { - { Count: > 0 } types => types.Select(type => (string?) type.Attribute("Value")).ToList(), + { Count: > 0 } types => types.Select(static type => (string?) type.Attribute("Value")).ToList(), _ => [] }, DeviceAuthorizationEndpointAuthMethodsSupported = configuration.Elements("DeviceAuthorizationEndpointAuthMethod").ToList() switch { - { Count: > 0 } methods => methods.Select(type => (string?) type.Attribute("Value")).ToList(), + { Count: > 0 } methods => methods.Select(static type => (string?) type.Attribute("Value")).ToList(), // If no explicit client authentication method was set, assume the provider only supports // flowing the client credentials as part of the device authorization request payload. @@ -1361,7 +1380,7 @@ public sealed partial class OpenIddictClientWebIntegrationConfiguration IntrospectionEndpointAuthMethodsSupported = configuration.Elements("IntrospectionEndpointAuthMethod").ToList() switch { - { Count: > 0 } methods => methods.Select(type => (string?) type.Attribute("Value")).ToList(), + { Count: > 0 } methods => methods.Select(static type => (string?) type.Attribute("Value")).ToList(), // If no explicit client authentication method was set, assume the provider only // supports flowing the client credentials as part of the introspection request payload. @@ -1370,7 +1389,7 @@ public sealed partial class OpenIddictClientWebIntegrationConfiguration RevocationEndpointAuthMethodsSupported = configuration.Elements("RevocationEndpointAuthMethod").ToList() switch { - { Count: > 0 } methods => methods.Select(type => (string?) type.Attribute("Value")).ToList(), + { Count: > 0 } methods => methods.Select(static type => (string?) type.Attribute("Value")).ToList(), // If no explicit client authentication method was set, assume the provider only // supports flowing the client credentials as part of the revocation request payload. @@ -1379,7 +1398,7 @@ public sealed partial class OpenIddictClientWebIntegrationConfiguration TokenEndpointAuthMethodsSupported = configuration.Elements("TokenEndpointAuthMethod").ToList() switch { - { Count: > 0 } methods => methods.Select(type => (string?) type.Attribute("Value")).ToList(), + { Count: > 0 } methods => methods.Select(static type => (string?) type.Attribute("Value")).ToList(), // If no explicit client authentication method was set, assume the provider only // supports flowing the client credentials as part of the token request payload. @@ -1390,7 +1409,7 @@ public sealed partial class OpenIddictClientWebIntegrationConfiguration _ => null }, - Scopes = environment.Elements("Scope").Select(setting => new + Scopes = environment.Elements("Scope").Select(static setting => new { Name = (string) setting.Attribute("Name"), Default = (bool?) setting.Attribute("Default") ?? false, @@ -1399,7 +1418,7 @@ public sealed partial class OpenIddictClientWebIntegrationConfiguration }) .ToList(), - Settings = provider.Elements("Setting").Select(setting => new + Settings = provider.Elements("Setting").Select(static setting => new { PropertyName = (string) setting.Attribute("PropertyName"), @@ -1412,7 +1431,7 @@ public sealed partial class OpenIddictClientWebIntegrationConfiguration DefaultValue = (string?) setting.Attribute("DefaultValue"), - Items = setting.Elements("Item").Select(item => new + Items = setting.Elements("Item").Select(static item => new { Value = (string) item.Attribute("Value"), Default = (bool?) item.Attribute("Default") ?? false, @@ -1456,7 +1475,7 @@ public static partial class OpenIddictClientWebIntegrationHelpers return template.Render(new { Providers = document.Root.Elements("Provider") - .Select(provider => new + .Select(static provider => new { Name = (string) provider.Attribute("Name"), DisplayName = (string?) provider.Attribute("DisplayName") ?? (string) provider.Attribute("Name") @@ -1508,12 +1527,12 @@ public sealed partial class OpenIddictClientWebIntegrationSettings return template.Render(new { Providers = document.Root.Elements("Provider") - .Select(provider => new + .Select(static provider => new { Name = (string) provider.Attribute("Name"), DisplayName = (string?) provider.Attribute("DisplayName") ?? (string) provider.Attribute("Name"), - Settings = provider.Elements("Setting").Select(setting => new + Settings = provider.Elements("Setting").Select(static setting => new { PropertyName = (string) setting.Attribute("PropertyName"), @@ -1555,8 +1574,4 @@ public sealed partial class OpenIddictClientWebIntegrationSettings }); } } - - public void Initialize(GeneratorInitializationContext context) - { - } } diff --git a/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.cs b/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.cs index a7b27b04..df1b6d17 100644 --- a/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.cs +++ b/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.cs @@ -1693,8 +1693,8 @@ public static partial class OpenIddictClientWebIntegrationHandlers ProviderTypes.StripeConnect when context.Properties.TryGetValue( StripeConnect.Properties.AccountType, out string? type) => type switch { - "Express" => new Uri("https://connect.stripe.com/express/oauth/authorize", UriKind.Absolute), - "Standard" => new Uri("https://connect.stripe.com/oauth/authorize", UriKind.Absolute), + "express" => new Uri("https://connect.stripe.com/express/oauth/authorize", UriKind.Absolute), + "standard" => new Uri("https://connect.stripe.com/oauth/authorize", UriKind.Absolute), _ => throw new InvalidOperationException(SR.GetResourceString(SR.ID2190)) }, diff --git a/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationProviders.xml b/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationProviders.xml index 601cdb36..738946e9 100644 --- a/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationProviders.xml +++ b/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationProviders.xml @@ -99,6 +99,9 @@ _ => throw new InvalidOperationException(SR.GetResourceString(SR.ID2184)) }}" /> + + + @@ -312,6 +315,11 @@ _ => throw new InvalidOperationException(SR.GetResourceString(SR.ID2185)) }}" /> + + + + + @@ -474,6 +482,44 @@ _ => throw new InvalidOperationException(SR.GetResourceString(SR.ID2186)) }}" /> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -837,7 +883,7 @@ 'Middle East (UAE)' or 'me-central-1' => 'https://login.mec1.pure.cloud/', 'South America (São Paulo)' or 'sa-east-1' => 'https://login.sae1.pure.cloud/', 'US East (Virginia)' or 'us-east-1' => 'https://login.mypurecloud.com/', - 'US East 2 (Ohio)' or 'us-east-2' => 'https://login.use2.us-gov-pure.cloud/', + 'US East (Ohio)' or 'us-east-2' => 'https://login.use2.us-gov-pure.cloud/', 'US West (Oregon)' or 'us-west-2' => 'https://login.usw2.pure.cloud/', _ => throw new InvalidOperationException(SR.GetResourceString(SR.ID2189)) }}"> @@ -855,7 +901,7 @@ 'Middle East (UAE)' or 'me-central-1' => 'https://login.mec1.pure.cloud/oauth/authorize', 'South America (São Paulo)' or 'sa-east-1' => 'https://login.sae1.pure.cloud/oauth/authorize', 'US East (Virginia)' or 'us-east-1' => 'https://login.mypurecloud.com/oauth/authorize', - 'US East 2 (Ohio)' or 'us-east-2' => 'https://login.use2.us-gov-pure.cloud/oauth/authorize', + 'US East (Ohio)' or 'us-east-2' => 'https://login.use2.us-gov-pure.cloud/oauth/authorize', 'US West (Oregon)' or 'us-west-2' => 'https://login.usw2.pure.cloud/oauth/authorize', _ => throw new InvalidOperationException(SR.GetResourceString(SR.ID2189)) }}" @@ -873,7 +919,7 @@ 'Middle East (UAE)' or 'me-central-1' => 'https://login.mec1.pure.cloud/oauth/token', 'South America (São Paulo)' or 'sa-east-1' => 'https://login.sae1.pure.cloud/oauth/token', 'US East (Virginia)' or 'us-east-1' => 'https://login.mypurecloud.com/oauth/token', - 'US East 2 (Ohio)' or 'us-east-2' => 'https://login.use2.us-gov-pure.cloud/oauth/token', + 'US East (Ohio)' or 'us-east-2' => 'https://login.use2.us-gov-pure.cloud/oauth/token', 'US West (Oregon)' or 'us-west-2' => 'https://login.usw2.pure.cloud/oauth/token', _ => throw new InvalidOperationException(SR.GetResourceString(SR.ID2189)) }}" @@ -891,7 +937,7 @@ 'Middle East (UAE)' or 'me-central-1' => 'https://api.mec1.pure.cloud/api/v2/users/me', 'South America (São Paulo)' or 'sa-east-1' => 'https://api.sae1.pure.cloud/api/v2/users/me', 'US East (Virginia)' or 'us-east-1' => 'https://api.mypurecloud.com/api/v2/users/me', - 'US East 2 (Ohio)' or 'us-east-2' => 'https://api.use2.us-gov-pure.cloud/api/v2/users/me', + 'US East (Ohio)' or 'us-east-2' => 'https://api.use2.us-gov-pure.cloud/api/v2/users/me', 'US West (Oregon)' or 'us-west-2' => 'https://api.usw2.pure.cloud/api/v2/users/me', _ => throw new InvalidOperationException(SR.GetResourceString(SR.ID2189)) }}"> @@ -902,6 +948,27 @@ + + + + + + + + + + + + + + + + + + @@ -1229,6 +1296,9 @@ + + + @@ -1835,6 +1905,9 @@ + + + @@ -1983,10 +2056,13 @@ + + + + Description="The type of the Stripe account (by default, 'standard', but can also be set to 'express'). Note: the account type can also be set dynamically via the authentication properties" />