| | | 1 | | using Microsoft.Extensions.Options; |
| | | 2 | | using ProjectTemplate.Web.Authentication.Providers.OpenIdConnect; |
| | | 3 | | using ProjectTemplate.Web.Authentication.Providers.Saml2; |
| | | 4 | | |
| | | 5 | | namespace ProjectTemplate.Web.Authentication.Options; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Validates application authentication configuration during application startup. |
| | | 9 | | /// </summary> |
| | 194 | 10 | | public sealed class ApplicationAuthenticationOptionsValidator(IHostEnvironment? environment) |
| | | 11 | | : IValidateOptions<ApplicationAuthenticationOptions> |
| | | 12 | | { |
| | 194 | 13 | | private readonly IHostEnvironment? _environment = environment; |
| | | 14 | | |
| | | 15 | | /// <inheritdoc /> |
| | | 16 | | public ValidateOptionsResult Validate(string? name, ApplicationAuthenticationOptions options) |
| | | 17 | | { |
| | 324 | 18 | | ArgumentNullException.ThrowIfNull(options); |
| | | 19 | | |
| | 324 | 20 | | List<string> failures = []; |
| | | 21 | | |
| | 324 | 22 | | ValidateApplicationAuthentication(options, failures); |
| | 324 | 23 | | ValidateOpenIdConnectProvider(options.Providers.OpenIdConnect, failures); |
| | 324 | 24 | | ValidateSaml2Provider(options.Providers.Saml2, failures); |
| | 324 | 25 | | ValidateExternalProvider("Microsoft", options.Providers.Microsoft, failures); |
| | 324 | 26 | | ValidateExternalProvider("Google", options.Providers.Google, failures); |
| | 324 | 27 | | ValidateExternalProvider("GitHub", options.Providers.GitHub, failures); |
| | | 28 | | |
| | 324 | 29 | | return failures.Count == 0 |
| | 324 | 30 | | ? ValidateOptionsResult.Success |
| | 324 | 31 | | : ValidateOptionsResult.Fail(failures); |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | private void ValidateApplicationAuthentication( |
| | | 35 | | ApplicationAuthenticationOptions options, |
| | | 36 | | ICollection<string> failures) |
| | | 37 | | { |
| | 324 | 38 | | Require( |
| | 324 | 39 | | !string.IsNullOrWhiteSpace(options.DefaultScheme), |
| | 324 | 40 | | "ProjectTemplate:Authentication:DefaultScheme is required.", |
| | 324 | 41 | | failures); |
| | | 42 | | |
| | 324 | 43 | | Require( |
| | 324 | 44 | | !string.IsNullOrWhiteSpace(options.DefaultChallengeScheme), |
| | 324 | 45 | | "ProjectTemplate:Authentication:DefaultChallengeScheme is required.", |
| | 324 | 46 | | failures); |
| | | 47 | | |
| | 324 | 48 | | Require( |
| | 324 | 49 | | !string.IsNullOrWhiteSpace(options.DefaultSignInScheme), |
| | 324 | 50 | | "ProjectTemplate:Authentication:DefaultSignInScheme is required.", |
| | 324 | 51 | | failures); |
| | | 52 | | |
| | 324 | 53 | | Require( |
| | 324 | 54 | | !options.Cookie.AllowInsecureHttp || _environment?.IsDevelopment() == true, |
| | 324 | 55 | | "ProjectTemplate:Authentication:Cookie:AllowInsecureHttp may only be enabled in the Development environment. |
| | 324 | 56 | | failures); |
| | | 57 | | |
| | 324 | 58 | | if (!options.Enabled) |
| | | 59 | | { |
| | 14 | 60 | | return; |
| | | 61 | | } |
| | | 62 | | |
| | 310 | 63 | | Require( |
| | 310 | 64 | | options.Cookie.Enabled, |
| | 310 | 65 | | "ProjectTemplate:Authentication:Cookie:Enabled must be true when application authentication is enabled.", |
| | 310 | 66 | | failures); |
| | | 67 | | |
| | 310 | 68 | | Require( |
| | 310 | 69 | | !string.IsNullOrWhiteSpace(options.Cookie.Scheme), |
| | 310 | 70 | | "ProjectTemplate:Authentication:Cookie:Scheme is required when application authentication is enabled.", |
| | 310 | 71 | | failures); |
| | | 72 | | |
| | 310 | 73 | | Require( |
| | 310 | 74 | | options.Cookie.ExpireMinutes > 0, |
| | 310 | 75 | | "ProjectTemplate:Authentication:Cookie:ExpireMinutes must be greater than zero when application authenticati |
| | 310 | 76 | | failures); |
| | 310 | 77 | | } |
| | | 78 | | |
| | | 79 | | private static void ValidateOpenIdConnectProvider( |
| | | 80 | | OpenIdConnectAuthenticationOptions options, |
| | | 81 | | ICollection<string> failures) |
| | | 82 | | { |
| | 324 | 83 | | if (!options.Enabled) |
| | | 84 | | { |
| | 314 | 85 | | return; |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | const string prefix = "ProjectTemplate:Authentication:Providers:OpenIdConnect"; |
| | | 89 | | |
| | 10 | 90 | | RequireProviderValue(prefix, nameof(options.Scheme), options.Scheme, failures); |
| | 10 | 91 | | RequireProviderValue(prefix, nameof(options.DisplayName), options.DisplayName, failures); |
| | 10 | 92 | | RequireProviderValue(prefix, nameof(options.Authority), options.Authority, failures); |
| | 10 | 93 | | RequireProviderValue(prefix, nameof(options.ClientId), options.ClientId, failures); |
| | 10 | 94 | | RequireProviderValue(prefix, nameof(options.CallbackPath), options.CallbackPath, failures); |
| | 10 | 95 | | RequireProviderValue(prefix, nameof(options.ResponseType), options.ResponseType, failures); |
| | | 96 | | |
| | 10 | 97 | | Require( |
| | 10 | 98 | | options.Scopes.Any(scope => !string.IsNullOrWhiteSpace(scope)), |
| | 10 | 99 | | $"{prefix}:Scopes must contain at least one non-empty value when the OpenID Connect provider is enabled.", |
| | 10 | 100 | | failures); |
| | 10 | 101 | | } |
| | | 102 | | |
| | | 103 | | private static void ValidateSaml2Provider( |
| | | 104 | | Saml2AuthenticationOptions options, |
| | | 105 | | ICollection<string> failures) |
| | | 106 | | { |
| | 324 | 107 | | if (!options.Enabled) |
| | | 108 | | { |
| | 314 | 109 | | return; |
| | | 110 | | } |
| | | 111 | | |
| | | 112 | | const string prefix = "ProjectTemplate:Authentication:Providers:Saml2"; |
| | | 113 | | |
| | 10 | 114 | | RequireProviderValue(prefix, nameof(options.Scheme), options.Scheme, failures); |
| | 10 | 115 | | RequireProviderValue(prefix, nameof(options.DisplayName), options.DisplayName, failures); |
| | 10 | 116 | | RequireProviderValue(prefix, nameof(options.EntityId), options.EntityId, failures); |
| | 10 | 117 | | RequireProviderValue(prefix, nameof(options.MetadataUrl), options.MetadataUrl, failures); |
| | 10 | 118 | | RequireProviderValue(prefix, nameof(options.ModulePath), options.ModulePath, failures); |
| | 10 | 119 | | } |
| | | 120 | | |
| | | 121 | | private static void ValidateExternalProvider( |
| | | 122 | | string providerName, |
| | | 123 | | ApplicationExternalAuthenticationProviderOptions options, |
| | | 124 | | ICollection<string> failures) |
| | | 125 | | { |
| | 972 | 126 | | if (!options.Enabled) |
| | | 127 | | { |
| | 938 | 128 | | return; |
| | | 129 | | } |
| | | 130 | | |
| | 34 | 131 | | string prefix = $"ProjectTemplate:Authentication:Providers:{providerName}"; |
| | | 132 | | |
| | 34 | 133 | | RequireProviderValue(prefix, nameof(options.Scheme), options.Scheme, failures); |
| | 34 | 134 | | RequireProviderValue(prefix, nameof(options.DisplayName), options.DisplayName, failures); |
| | 34 | 135 | | RequireProviderValue(prefix, nameof(options.ClientId), options.ClientId, failures); |
| | 34 | 136 | | RequireProviderValue(prefix, nameof(options.ClientSecret), options.ClientSecret, failures); |
| | 34 | 137 | | RequireProviderValue(prefix, nameof(options.CallbackPath), options.CallbackPath, failures); |
| | 34 | 138 | | } |
| | | 139 | | |
| | | 140 | | private static void RequireProviderValue( |
| | | 141 | | string prefix, |
| | | 142 | | string key, |
| | | 143 | | string? value, |
| | | 144 | | ICollection<string> failures) |
| | | 145 | | { |
| | 280 | 146 | | Require( |
| | 280 | 147 | | !string.IsNullOrWhiteSpace(value), |
| | 280 | 148 | | $"{prefix}:{key} is required when the provider is enabled.", |
| | 280 | 149 | | failures); |
| | 280 | 150 | | } |
| | | 151 | | |
| | | 152 | | private static void Require( |
| | | 153 | | bool condition, |
| | | 154 | | string failureMessage, |
| | | 155 | | ICollection<string> failures) |
| | | 156 | | { |
| | 2516 | 157 | | if (!condition) |
| | | 158 | | { |
| | 16 | 159 | | failures.Add(failureMessage); |
| | | 160 | | } |
| | 2516 | 161 | | } |
| | | 162 | | } |