From 9bb4f9a378f906fbff2d83e1ae23a28b0be0a782 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Chalet?= Date: Sat, 1 Oct 2022 16:36:03 +0200 Subject: [PATCH] Update the provider builders to reject null or empty strings --- ...OpenIddictClientWebIntegrationGenerator.cs | 72 ++++++++++++++----- 1 file changed, 56 insertions(+), 16 deletions(-) diff --git a/gen/OpenIddict.Client.WebIntegration.Generators/OpenIddictClientWebIntegrationGenerator.cs b/gen/OpenIddict.Client.WebIntegration.Generators/OpenIddictClientWebIntegrationGenerator.cs index 83e4ac5b..480a2d51 100644 --- a/gen/OpenIddict.Client.WebIntegration.Generators/OpenIddictClientWebIntegrationGenerator.cs +++ b/gen/OpenIddict.Client.WebIntegration.Generators/OpenIddictClientWebIntegrationGenerator.cs @@ -61,7 +61,7 @@ public partial class OpenIddictClientWebIntegrationBuilder { {{~ for provider in providers ~}} /// - /// Enables {{ provider.name }} integration using the specified options. + /// Enables the {{ provider.name }} integration and registers the associated services in the DI container. {{~ if provider.documentation ~}} /// For more information, visit the official website. /// @@ -83,7 +83,7 @@ public partial class OpenIddictClientWebIntegrationBuilder } /// - /// Enables {{ provider.name }} integration using the specified options. + /// Enables the {{ provider.name }} integration and registers the associated services in the DI container. {{~ if provider.documentation ~}} /// For more information, visit the official website. /// @@ -147,7 +147,14 @@ public partial class OpenIddictClientWebIntegrationBuilder /// The client identifier. /// The . public {{ provider.name }} SetClientId(string identifier) - => Configure(options => options.ClientId = identifier); + { + if (string.IsNullOrEmpty(identifier)) + { + throw new ArgumentException(SR.GetResourceString(SR.ID0124), nameof(identifier)); + } + + return Configure(options => options.ClientId = identifier); + } /// /// Sets the client secret, if applicable. @@ -155,23 +162,49 @@ public partial class OpenIddictClientWebIntegrationBuilder /// The client secret. /// The . public {{ provider.name }} SetClientSecret(string secret) - => Configure(options => options.ClientSecret = secret); + { + if (string.IsNullOrEmpty(secret)) + { + throw new ArgumentException(SR.GetResourceString(SR.ID0125), nameof(secret)); + } + + return Configure(options => options.ClientSecret = secret); + } /// /// Sets the redirection URI, if applicable. /// /// The redirection URI. /// The . - public {{ provider.name }} SetRedirectUri(Uri? address) - => Configure(options => options.RedirectUri = address); + public {{ provider.name }} SetRedirectUri(Uri address) + { + if (address is null) + { + throw new ArgumentNullException(nameof(address)); + } + + return Configure(options => options.RedirectUri = address); + } /// /// Sets the redirection URI, if applicable. /// /// The redirection URI. /// The . - public {{ provider.name }} SetRedirectUri(string? address) - => SetRedirectUri(!string.IsNullOrEmpty(address) ? new Uri(address, UriKind.RelativeOrAbsolute) : null); + public {{ provider.name }} SetRedirectUri(string address) + { + if (string.IsNullOrEmpty(address)) + { + throw new ArgumentException(SR.GetResourceString(SR.ID0143), nameof(address)); + } + + if (!Uri.TryCreate(address, UriKind.Absolute, out Uri? uri) || !uri.IsWellFormedOriginalString()) + { + throw new ArgumentException(SR.GetResourceString(SR.ID0144), nameof(address)); + } + + return SetRedirectUri(uri); + } /// /// Adds one or more scopes to the list of requested scopes, if applicable. @@ -179,7 +212,14 @@ public partial class OpenIddictClientWebIntegrationBuilder /// The scopes. /// The . public {{ provider.name }} AddScopes(params string[] scopes) - => Configure(options => options.Scopes.UnionWith(scopes)); + { + if (scopes is null) + { + throw new ArgumentNullException(nameof(scopes)); + } + + return Configure(options => options.Scopes.UnionWith(scopes)); + } {{~ for environment in provider.environments ~}} /// @@ -429,21 +469,21 @@ public partial class OpenIddictClientWebIntegrationConfiguration /// The options instance to initialize. public void Configure(OpenIddictClientOptions options) { + // Resolve the provider options from the service provider and create a registration based on the specified settings. + var settings = _provider.GetRequiredService>().CurrentValue; + var formatter = Smart.CreateDefaultSmartFormat(new SmartSettings { CaseSensitivity = CaseSensitivityType.CaseInsensitive }); - // Resolve the provider options from the service provider and create a registration based on the specified settings. - var settings = _provider.GetRequiredService>().CurrentValue; - var registration = new OpenIddictClientRegistration { Issuer = settings.Environment switch { {{~ for environment in provider.environments ~}} OpenIddictClientWebIntegrationConstants.{{ provider.name }}.Environments.{{ environment.name }} - => new Uri(formatter.Format(""{{ environment.issuer }}"", options), UriKind.Absolute), + => new Uri(formatter.Format(""{{ environment.issuer }}"", settings), UriKind.Absolute), {{~ end ~}} _ => throw new InvalidOperationException(SR.FormatID0194(nameof(settings.Environment))) @@ -460,15 +500,15 @@ public partial class OpenIddictClientWebIntegrationConfiguration OpenIddictClientWebIntegrationConstants.{{ provider.name }}.Environments.{{ environment.name }} => new OpenIddictConfiguration { {{~ if environment.configuration.authorization_endpoint ~}} - AuthorizationEndpoint = new Uri(formatter.Format(""{{ environment.configuration.authorization_endpoint }}"", options), UriKind.Absolute), + AuthorizationEndpoint = new Uri(formatter.Format(""{{ environment.configuration.authorization_endpoint }}"", settings), UriKind.Absolute), {{~ end ~}} {{~ if environment.configuration.token_endpoint ~}} - TokenEndpoint = new Uri(formatter.Format(""{{ environment.configuration.token_endpoint }}"", options), UriKind.Absolute), + TokenEndpoint = new Uri(formatter.Format(""{{ environment.configuration.token_endpoint }}"", settings), UriKind.Absolute), {{~ end ~}} {{~ if environment.configuration.userinfo_endpoint ~}} - UserinfoEndpoint = new Uri(formatter.Format(""{{ environment.configuration.userinfo_endpoint }}"", options), UriKind.Absolute), + UserinfoEndpoint = new Uri(formatter.Format(""{{ environment.configuration.userinfo_endpoint }}"", settings), UriKind.Absolute), {{~ end ~}} CodeChallengeMethodsSupported =