diff --git a/.editorconfig b/.editorconfig index c487b3c4..1b59796a 100644 --- a/.editorconfig +++ b/.editorconfig @@ -95,7 +95,7 @@ csharp_using_directive_placement = outside_namespace [*.{cs,vb}] dotnet_code_quality_unused_parameters = all -dotnet_diagnostic.CA1510.severity = none +dotnet_diagnostic.CA1510.severity = suggestion dotnet_diagnostic.CA2254.severity = none dotnet_diagnostic.IDE0002.severity = none dotnet_diagnostic.IDE0305.severity = none diff --git a/Directory.Build.props b/Directory.Build.props index f727d266..25aef65d 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -16,12 +16,15 @@ portable snupkg true + true false true true false false true + true + true @@ -207,10 +210,6 @@ true - - true - - diff --git a/Directory.Build.targets b/Directory.Build.targets index b543c1bc..dc320ece 100644 --- a/Directory.Build.targets +++ b/Directory.Build.targets @@ -55,30 +55,24 @@ $(DefineConstants);SUPPORTS_APPLICATION_CONFIGURATION_INITIALIZATION $(DefineConstants);SUPPORTS_AUTHENTICATION_HANDLER_SELECTION_FALLBACK + $(DefineConstants);SUPPORTS_AUTHENTICATION_HANDLER_TIME_PROVIDER_ARGUMENT $(DefineConstants);SUPPORTS_AUTHORIZATION_MIDDLEWARE $(DefineConstants);SUPPORTS_BCL_ASYNC_ENUMERABLE $(DefineConstants);SUPPORTS_BULK_DBSET_OPERATIONS - $(DefineConstants);SUPPORTS_CHUNK_LINQ_EXTENSION $(DefineConstants);SUPPORTS_DBSET_VALUETASK_FINDASYNC $(DefineConstants);SUPPORTS_ENDPOINT_ROUTING $(DefineConstants);SUPPORTS_ENVIRONMENT_PROCESS_PATH - $(DefineConstants);SUPPORTS_HEXADECIMAL_STRING_CONVERSION $(DefineConstants);SUPPORTS_HTTP_CLIENT_DEFAULT_REQUEST_VERSION $(DefineConstants);SUPPORTS_HTTP_CLIENT_DEFAULT_REQUEST_VERSION_POLICY $(DefineConstants);SUPPORTS_HTTP_CLIENT_RESILIENCE - $(DefineConstants);SUPPORTS_INT32_RANDOM_NUMBER_GENERATOR_METHODS $(DefineConstants);SUPPORTS_MULTIPLE_VALUES_IN_QUERYHELPERS $(DefineConstants);SUPPORTS_NAMED_PIPE_STATIC_FACTORY_WITH_ACL $(DefineConstants);SUPPORTS_ONE_SHOT_HASHING_METHODS $(DefineConstants);SUPPORTS_ONE_SHOT_KEY_DERIVATION_METHODS - $(DefineConstants);SUPPORTS_ONE_SHOT_RANDOM_NUMBER_GENERATOR_METHODS $(DefineConstants);SUPPORTS_OPERATING_SYSTEM_VERSIONS_COMPARISON $(DefineConstants);SUPPORTS_PEM_ENCODED_KEY_IMPORT $(DefineConstants);SUPPORTS_REDIRECTION_ON_SIGN_IN - $(DefineConstants);SUPPORTS_TASK_WAIT_ASYNC $(DefineConstants);SUPPORTS_TEXT_ELEMENT_ENUMERATOR - $(DefineConstants);SUPPORTS_TIME_PROVIDER - $(DefineConstants);SUPPORTS_VALUETASK_COMPLETED_TASK $(DefineConstants);SUPPORTS_WINFORMS_TASK_DIALOG $(DefineConstants);SUPPORTS_ZLIB_COMPRESSION @@ -98,7 +92,6 @@ $(DefineConstants);SUPPORTS_EPHEMERAL_KEY_SETS $(DefineConstants);SUPPORTS_KEY_DERIVATION_WITH_SPECIFIED_HASH_ALGORITHM $(DefineConstants);SUPPORTS_RSA_KEY_CREATION_WITH_SPECIFIED_SIZE - $(DefineConstants);SUPPORTS_TOHASHSET_LINQ_EXTENSION $(DefineConstants);SUPPORTS_BROTLI_COMPRESSION $(DefineConstants);SUPPORTS_CURRENT_USER_ONLY_PIPE_OPTION $(DefineConstants);SUPPORTS_SERVICE_PROVIDER_IN_HTTP_MESSAGE_HANDLER_BUILDER - $(DefineConstants);SUPPORTS_STATIC_RANDOM_NUMBER_GENERATOR_METHODS - $(DefineConstants);SUPPORTS_STREAM_MEMORY_METHODS $(DefineConstants);SUPPORTS_TIME_CONSTANT_COMPARISONS $(DefineConstants);SUPPORTS_WEB_INTEGRATION_IN_GENERIC_HOST @@ -164,6 +155,19 @@ $(DefineConstants);SUPPORTS_WINDOWS_RUNTIME + + + + $(DefineConstants);FeatureRuntimeInformation + + + + + + + + + diff --git a/Directory.Packages.props b/Directory.Packages.props index c3147cfe..e0efa239 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -11,15 +11,117 @@ --> - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -96,10 +198,10 @@ - + - + - + - + - + - + - + diff --git a/gen/OpenIddict.Client.WebIntegration.Generators/OpenIddictClientWebIntegrationGenerator.cs b/gen/OpenIddict.Client.WebIntegration.Generators/OpenIddictClientWebIntegrationGenerator.cs index 469d3703..b3ad01df 100644 --- a/gen/OpenIddict.Client.WebIntegration.Generators/OpenIddictClientWebIntegrationGenerator.cs +++ b/gen/OpenIddict.Client.WebIntegration.Generators/OpenIddictClientWebIntegrationGenerator.cs @@ -241,10 +241,7 @@ public sealed partial class OpenIddictClientWebIntegrationBuilder /// The instance. public {{ provider.name }} SetClaimsIssuer(string issuer) { - if (string.IsNullOrEmpty(issuer)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0124), nameof(issuer)); - } + ArgumentException.ThrowIfNullOrEmpty(issuer); return Set(registration => registration.ClaimsIssuer = issuer); } @@ -256,10 +253,7 @@ public sealed partial class OpenIddictClientWebIntegrationBuilder /// The instance. public {{ provider.name }} SetProviderName(string name) { - if (string.IsNullOrEmpty(name)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0124), nameof(name)); - } + ArgumentException.ThrowIfNullOrEmpty(name); return Set(registration => registration.ProviderName = name); } @@ -271,10 +265,7 @@ public sealed partial class OpenIddictClientWebIntegrationBuilder /// The instance. public {{ provider.name }} SetProviderDisplayName(string name) { - if (string.IsNullOrEmpty(name)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0124), nameof(name)); - } + ArgumentException.ThrowIfNullOrEmpty(name); return Set(registration => registration.ProviderDisplayName = name); } @@ -286,10 +277,7 @@ public sealed partial class OpenIddictClientWebIntegrationBuilder /// The instance. public {{ provider.name }} SetRegistrationId(string identifier) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0124), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); return Set(registration => registration.RegistrationId = identifier); } @@ -301,10 +289,7 @@ public sealed partial class OpenIddictClientWebIntegrationBuilder /// The instance. public {{ provider.name }} SetClientId(string identifier) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0124), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); return Set(registration => registration.ClientId = identifier); } @@ -316,10 +301,7 @@ public sealed partial class OpenIddictClientWebIntegrationBuilder /// The instance. public {{ provider.name }} SetClientSecret(string secret) { - if (string.IsNullOrEmpty(secret)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0125), nameof(secret)); - } + ArgumentException.ThrowIfNullOrEmpty(secret); return Set(registration => registration.ClientSecret = secret); } @@ -335,10 +317,7 @@ public sealed partial class OpenIddictClientWebIntegrationBuilder /// The instance. public {{ provider.name }} SetPostLogoutRedirectUri(Uri uri) { - if (uri is null) - { - throw new ArgumentNullException(nameof(uri)); - } + ArgumentNullException.ThrowIfNull(uri); return Set(registration => registration.PostLogoutRedirectUri = uri); } @@ -354,10 +333,7 @@ public sealed partial class OpenIddictClientWebIntegrationBuilder /// The instance. public {{ provider.name }} SetPostLogoutRedirectUri([StringSyntax(StringSyntaxAttribute.Uri)] string uri) { - if (string.IsNullOrEmpty(uri)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0143), nameof(uri)); - } + ArgumentException.ThrowIfNullOrEmpty(uri); return SetPostLogoutRedirectUri(new Uri(uri, UriKind.RelativeOrAbsolute)); } @@ -373,10 +349,7 @@ public sealed partial class OpenIddictClientWebIntegrationBuilder /// The instance. public {{ provider.name }} SetRedirectUri(Uri uri) { - if (uri is null) - { - throw new ArgumentNullException(nameof(uri)); - } + ArgumentNullException.ThrowIfNull(uri); return Set(registration => registration.RedirectUri = uri); } @@ -392,10 +365,7 @@ public sealed partial class OpenIddictClientWebIntegrationBuilder /// The instance. public {{ provider.name }} SetRedirectUri([StringSyntax(StringSyntaxAttribute.Uri)] string uri) { - if (string.IsNullOrEmpty(uri)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0143), nameof(uri)); - } + ArgumentException.ThrowIfNullOrEmpty(uri); return SetRedirectUri(new Uri(uri, UriKind.RelativeOrAbsolute)); } @@ -421,10 +391,7 @@ public sealed partial class OpenIddictClientWebIntegrationBuilder {{~ end ~}} public {{ provider.name }} Add{{ setting.property_name }}(params {{ setting.clr_type }}[] {{ setting.parameter_name }}) { - if ({{ setting.parameter_name }} is null) - { - throw new ArgumentNullException(nameof({{ setting.parameter_name }})); - } + ArgumentNullException.ThrowIfNull({{ setting.parameter_name }}); return Set(registration => registration.Get{{ provider.name }}Settings().{{ setting.property_name }}.UnionWith({{ setting.parameter_name }})); } @@ -439,10 +406,7 @@ public sealed partial class OpenIddictClientWebIntegrationBuilder {{~ end ~}} public {{ provider.name }} Set{{ setting.property_name }}(ECDsaSecurityKey {{ setting.parameter_name }}) { - if ({{ setting.parameter_name }} is null) - { - throw new ArgumentNullException(nameof({{ setting.parameter_name }})); - } + ArgumentNullException.ThrowIfNull({{ setting.parameter_name }}); if ({{ setting.parameter_name }}.PrivateKeyStatus is PrivateKeyStatus.DoesNotExist) { @@ -524,10 +488,7 @@ public sealed partial class OpenIddictClientWebIntegrationBuilder {{~ end ~}} public {{ provider.name }} Set{{ setting.property_name }}(Uri {{ setting.parameter_name }}) { - if ({{ setting.parameter_name }} is null) - { - throw new ArgumentNullException(nameof({{ setting.parameter_name }})); - } + ArgumentNullException.ThrowIfNull({{ setting.parameter_name }}); if (!{{ setting.parameter_name }}.IsAbsoluteUri || OpenIddictHelpers.IsImplicitFileUri({{ setting.parameter_name }})) { @@ -547,10 +508,7 @@ public sealed partial class OpenIddictClientWebIntegrationBuilder {{~ end ~}} public {{ provider.name }} Set{{ setting.property_name }}(string {{ setting.parameter_name }}) { - if (string.IsNullOrEmpty({{ setting.parameter_name }})) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0143), nameof({{ setting.parameter_name }})); - } + ArgumentException.ThrowIfNullOrEmpty({{ setting.parameter_name }}); return Set{{ setting.property_name }}(new Uri({{ setting.parameter_name }}, UriKind.RelativeOrAbsolute)); } @@ -565,10 +523,7 @@ public sealed partial class OpenIddictClientWebIntegrationBuilder {{~ end ~}} public {{ provider.name }} Set{{ setting.property_name }}(X509Certificate2 {{ setting.parameter_name }}) { - if ({{ setting.parameter_name }} is null) - { - throw new ArgumentNullException(nameof({{ setting.parameter_name }})); - } + ArgumentNullException.ThrowIfNull({{ setting.parameter_name }}); if (!{{ setting.parameter_name }}.HasPrivateKey) { @@ -591,7 +546,7 @@ public sealed partial class OpenIddictClientWebIntegrationBuilder public {{ provider.name }} Set{{ setting.property_name }}(Assembly assembly, string resource, string? password) #if SUPPORTS_EPHEMERAL_KEY_SETS // Note: ephemeral key sets are currently not supported on macOS. - => Set{{ setting.property_name }}(assembly, resource, password, RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? + => Set{{ setting.property_name }}(assembly, resource, password, OperatingSystem.IsMacOS() ? X509KeyStorageFlags.MachineKeySet : X509KeyStorageFlags.EphemeralKeySet); #else @@ -613,15 +568,8 @@ public sealed partial class OpenIddictClientWebIntegrationBuilder Assembly assembly, string resource, string? password, X509KeyStorageFlags flags) { - if (assembly is null) - { - throw new ArgumentNullException(nameof(assembly)); - } - - if (string.IsNullOrEmpty(resource)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0062), nameof(resource)); - } + ArgumentNullException.ThrowIfNull(assembly); + ArgumentException.ThrowIfNullOrEmpty(resource); using var stream = assembly.GetManifestResourceStream(resource) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0064)); @@ -641,7 +589,7 @@ public sealed partial class OpenIddictClientWebIntegrationBuilder public {{ provider.name }} Set{{ setting.property_name }}(Stream stream, string? password) #if SUPPORTS_EPHEMERAL_KEY_SETS // Note: ephemeral key sets are currently not supported on macOS. - => Set{{ setting.property_name }}(stream, password, RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? + => Set{{ setting.property_name }}(stream, password, OperatingSystem.IsMacOS() ? X509KeyStorageFlags.MachineKeySet : X509KeyStorageFlags.EphemeralKeySet); #else @@ -660,10 +608,7 @@ public sealed partial class OpenIddictClientWebIntegrationBuilder {{~ end ~}} public {{ provider.name }} Set{{ setting.property_name }}(Stream stream, string? password, X509KeyStorageFlags flags) { - if (stream is null) - { - throw new ArgumentNullException(nameof(stream)); - } + ArgumentNullException.ThrowIfNull(stream); using var buffer = new MemoryStream(); stream.CopyTo(buffer); @@ -691,10 +636,7 @@ public sealed partial class OpenIddictClientWebIntegrationBuilder {{~ end ~}} public {{ provider.name }} Set{{ setting.property_name }}(string thumbprint) { - if (string.IsNullOrEmpty(thumbprint)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0065), nameof(thumbprint)); - } + ArgumentException.ThrowIfNullOrEmpty(thumbprint); return Set{{ setting.property_name }}( GetCertificate(StoreLocation.CurrentUser, thumbprint) ?? @@ -724,10 +666,7 @@ public sealed partial class OpenIddictClientWebIntegrationBuilder {{~ end ~}} public {{ provider.name }} Set{{ setting.property_name }}(string thumbprint, StoreName name, StoreLocation location) { - if (string.IsNullOrEmpty(thumbprint)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0065), nameof(thumbprint)); - } + ArgumentException.ThrowIfNullOrEmpty(thumbprint); using var store = new X509Store(name, location); store.Open(OpenFlags.ReadOnly); @@ -759,10 +698,7 @@ public sealed partial class OpenIddictClientWebIntegrationBuilder {{~ end ~}} public {{ provider.name }} Set{{ setting.property_name }}({{ setting.clr_type }} {{ setting.parameter_name }}) { - if ({{ setting.parameter_name }} is null) - { - throw new ArgumentNullException(nameof({{ setting.parameter_name }})); - } + ArgumentNullException.ThrowIfNull({{ setting.parameter_name }}); return Set(registration => registration.Get{{ provider.name }}Settings().{{ setting.property_name }} = {{ setting.parameter_name }}); } @@ -790,10 +726,7 @@ public sealed partial class OpenIddictClientWebIntegrationBuilder /// The instance. private {{ provider.name }} Set(Action configuration) { - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(configuration); configuration(Registration); @@ -973,6 +906,8 @@ public sealed partial class OpenIddictClientWebIntegrationConfiguration { public static partial void ConfigureProvider(OpenIddictClientRegistration registration) { + ArgumentNullException.ThrowIfNull(registration); + {{~ for provider in providers ~}} {{~ if for.index == 0 ~}} if (registration.ProviderType is ProviderTypes.{{ provider.name }}) diff --git a/sandbox/OpenIddict.Sandbox.AspNet.Client/OpenIddict.Sandbox.AspNet.Client.csproj b/sandbox/OpenIddict.Sandbox.AspNet.Client/OpenIddict.Sandbox.AspNet.Client.csproj index ff9daf7d..c8b52ada 100644 --- a/sandbox/OpenIddict.Sandbox.AspNet.Client/OpenIddict.Sandbox.AspNet.Client.csproj +++ b/sandbox/OpenIddict.Sandbox.AspNet.Client/OpenIddict.Sandbox.AspNet.Client.csproj @@ -7,7 +7,7 @@ false disable CA3147 - true + false diff --git a/sandbox/OpenIddict.Sandbox.AspNet.Server/OpenIddict.Sandbox.AspNet.Server.csproj b/sandbox/OpenIddict.Sandbox.AspNet.Server/OpenIddict.Sandbox.AspNet.Server.csproj index 28ae28f1..e0d2cc92 100644 --- a/sandbox/OpenIddict.Sandbox.AspNet.Server/OpenIddict.Sandbox.AspNet.Server.csproj +++ b/sandbox/OpenIddict.Sandbox.AspNet.Server/OpenIddict.Sandbox.AspNet.Server.csproj @@ -7,7 +7,7 @@ false disable CA3147 - true + false diff --git a/sandbox/OpenIddict.Sandbox.Console.Client/InteractiveService.cs b/sandbox/OpenIddict.Sandbox.Console.Client/InteractiveService.cs index 9fb74c50..856be8ab 100644 --- a/sandbox/OpenIddict.Sandbox.Console.Client/InteractiveService.cs +++ b/sandbox/OpenIddict.Sandbox.Console.Client/InteractiveService.cs @@ -436,7 +436,7 @@ public class InteractiveService : BackgroundService select registration) .UseConverter(registration => registration.ProviderDisplayName!)).ProviderName!; - return WaitAsync(Task.Run(PromptAsync, cancellationToken), cancellationToken); + return Task.Run(PromptAsync, cancellationToken).WaitAsync(cancellationToken); } Task<(string? GrantType, string? ResponseType)> GetSelectedFlowAsync( @@ -557,7 +557,7 @@ public class InteractiveService : BackgroundService .UseConverter(choice => choice.DisplayName)).Item1; } - return WaitAsync(Task.Run(() => Prompt(registration, configuration), cancellationToken), cancellationToken); + return Task.Run(() => Prompt(registration, configuration), cancellationToken).WaitAsync(cancellationToken); } Task GetSelectedGrantTypeAsync( @@ -608,7 +608,7 @@ public class InteractiveService : BackgroundService .UseConverter(choice => choice.DisplayName)).GrantType; } - return WaitAsync(Task.Run(() => Prompt(registration, configuration), cancellationToken), cancellationToken); + return Task.Run(() => Prompt(registration, configuration), cancellationToken).WaitAsync(cancellationToken); } Task AuthenticateUserInteractivelyAsync( @@ -632,7 +632,7 @@ public class InteractiveService : BackgroundService registration.GrantTypes.Any(static type => type is not ( GrantTypes.AuthorizationCode or GrantTypes.Implicit or GrantTypes.RefreshToken)))) { - return WaitAsync(Task.Run(Prompt, cancellationToken), cancellationToken); + return Task.Run(Prompt, cancellationToken).WaitAsync(cancellationToken); } return Task.FromResult(true); @@ -647,7 +647,7 @@ public class InteractiveService : BackgroundService registration.GrantTypes.Any(static type => type is not ( GrantTypes.AuthorizationCode or GrantTypes.Implicit or GrantTypes.RefreshToken)))) { - return WaitAsync(Task.Run(Prompt, cancellationToken), cancellationToken); + return Task.Run(Prompt, cancellationToken).WaitAsync(cancellationToken); } return Task.FromResult(true); @@ -664,7 +664,7 @@ public class InteractiveService : BackgroundService IsSecret = false }); - return WaitAsync(Task.Run(Prompt, cancellationToken), cancellationToken); + return Task.Run(Prompt, cancellationToken).WaitAsync(cancellationToken); } Task GetPasswordAsync(CancellationToken cancellationToken) @@ -675,7 +675,7 @@ public class InteractiveService : BackgroundService IsSecret = true }); - return WaitAsync(Task.Run(Prompt, cancellationToken), cancellationToken); + return Task.Run(Prompt, cancellationToken).WaitAsync(cancellationToken); } static Task RefreshTokenAsync(CancellationToken cancellationToken) @@ -688,7 +688,7 @@ public class InteractiveService : BackgroundService ShowDefaultValue = true }); - return WaitAsync(Task.Run(Prompt, cancellationToken), cancellationToken); + return Task.Run(Prompt, cancellationToken).WaitAsync(cancellationToken); } Task GetRequestedTokenTypeAsync(CancellationToken cancellationToken) @@ -704,7 +704,7 @@ public class InteractiveService : BackgroundService ]) .UseConverter(choice => choice.DisplayName)).TokenType; - return WaitAsync(Task.Run(Prompt, cancellationToken), cancellationToken); + return Task.Run(Prompt, cancellationToken).WaitAsync(cancellationToken); } Task<(string TokenType, string Token)> GetSubjectTokenAsync(CancellationToken cancellationToken) @@ -730,7 +730,7 @@ public class InteractiveService : BackgroundService return (type, token); } - return WaitAsync(Task.Run(Prompt, cancellationToken), cancellationToken); + return Task.Run(Prompt, cancellationToken).WaitAsync(cancellationToken); } Task<(string? TokenType, string? Token)> GetActorTokenAsync(CancellationToken cancellationToken) @@ -760,7 +760,7 @@ public class InteractiveService : BackgroundService })); } - return WaitAsync(Task.Run(Prompt, cancellationToken), cancellationToken); + return Task.Run(Prompt, cancellationToken).WaitAsync(cancellationToken); } static Task IntrospectAccessTokenAsync(CancellationToken cancellationToken) @@ -773,7 +773,7 @@ public class InteractiveService : BackgroundService ShowDefaultValue = true }); - return WaitAsync(Task.Run(Prompt, cancellationToken), cancellationToken); + return Task.Run(Prompt, cancellationToken).WaitAsync(cancellationToken); } static Task LogOutAsync(CancellationToken cancellationToken) @@ -785,7 +785,7 @@ public class InteractiveService : BackgroundService ShowDefaultValue = true }); - return WaitAsync(Task.Run(Prompt, cancellationToken), cancellationToken); + return Task.Run(Prompt, cancellationToken).WaitAsync(cancellationToken); } static Task RevokeAccessTokenAsync(CancellationToken cancellationToken) @@ -798,26 +798,7 @@ public class InteractiveService : BackgroundService ShowDefaultValue = true }); - return WaitAsync(Task.Run(Prompt, cancellationToken), cancellationToken); - } - - static async Task WaitAsync(Task task, CancellationToken cancellationToken) - { -#if SUPPORTS_TASK_WAIT_ASYNC - return await task.WaitAsync(cancellationToken); -#else - var source = new TaskCompletionSource(TaskCreationOptions.None); - - using (cancellationToken.Register(static state => ((TaskCompletionSource) state!).SetResult(true), source)) - { - if (await Task.WhenAny(task, source.Task) == source.Task) - { - throw new OperationCanceledException(cancellationToken); - } - - return await task; - } -#endif + return Task.Run(Prompt, cancellationToken).WaitAsync(cancellationToken); } } } diff --git a/shared/OpenIddict.Extensions/OpenIddict.Extensions.csproj b/shared/OpenIddict.Extensions/OpenIddict.Extensions.csproj index 72076a39..d1f609b1 100644 --- a/shared/OpenIddict.Extensions/OpenIddict.Extensions.csproj +++ b/shared/OpenIddict.Extensions/OpenIddict.Extensions.csproj @@ -3,6 +3,7 @@ netstandard2.0 false + $(DefineConstants);FeatureValueTuple diff --git a/shared/OpenIddict.Extensions/OpenIddictHelpers.cs b/shared/OpenIddict.Extensions/OpenIddictHelpers.cs index 0ebe8b84..42bf09c7 100644 --- a/shared/OpenIddict.Extensions/OpenIddictHelpers.cs +++ b/shared/OpenIddict.Extensions/OpenIddictHelpers.cs @@ -35,10 +35,7 @@ internal static class OpenIddictHelpers /// public static bool IncludesAnyFromSet(IReadOnlyList array, ISet set) { - if (set is null) - { - throw new ArgumentNullException(nameof(set)); - } + ArgumentNullException.ThrowIfNull(set); for (var index = 0; index < array.Count; index++) { @@ -150,15 +147,8 @@ internal static class OpenIddictHelpers /// is not an absolute URI. public static bool IsBaseOf(Uri left, Uri right) { - if (left is null) - { - throw new ArgumentNullException(nameof(left)); - } - - if (right is null) - { - throw new ArgumentNullException(nameof(right)); - } + ArgumentNullException.ThrowIfNull(left); + ArgumentNullException.ThrowIfNull(right); if (left is not { IsAbsoluteUri: true }) { @@ -188,10 +178,7 @@ internal static class OpenIddictHelpers /// is . public static bool IsImplicitFileUri(Uri uri) { - if (uri is null) - { - throw new ArgumentNullException(nameof(uri)); - } + ArgumentNullException.ThrowIfNull(uri); return uri.IsAbsoluteUri && uri.IsFile && !uri.OriginalString.StartsWith(uri.Scheme, StringComparison.OrdinalIgnoreCase); @@ -206,10 +193,7 @@ internal static class OpenIddictHelpers /// The final instance, with the specified parameter appended. public static Uri AddQueryStringParameter(Uri uri, string name, string? value) { - if (uri is null) - { - throw new ArgumentNullException(nameof(uri)); - } + ArgumentNullException.ThrowIfNull(uri); var builder = new StringBuilder(uri.Query); if (builder.Length > 0) @@ -238,15 +222,8 @@ internal static class OpenIddictHelpers /// is . public static Uri AddQueryStringParameters(Uri uri, IReadOnlyDictionary parameters) { - if (uri is null) - { - throw new ArgumentNullException(nameof(uri)); - } - - if (parameters is null) - { - throw new ArgumentNullException(nameof(parameters)); - } + ArgumentNullException.ThrowIfNull(uri); + ArgumentNullException.ThrowIfNull(parameters); if (parameters.Count is 0) { @@ -302,10 +279,7 @@ internal static class OpenIddictHelpers /// is . public static IReadOnlyDictionary ParseQuery(string query) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); return query.TrimStart(Separators.QuestionMark[0]) .Split([Separators.Ampersand[0], Separators.Semicolon[0]], StringSplitOptions.RemoveEmptyEntries) @@ -326,10 +300,7 @@ internal static class OpenIddictHelpers /// is . public static IReadOnlyDictionary ParseFragment(string fragment) { - if (fragment is null) - { - throw new ArgumentNullException(nameof(fragment)); - } + ArgumentNullException.ThrowIfNull(fragment); return fragment.TrimStart(Separators.Hash[0]) .Split([Separators.Ampersand[0], Separators.Semicolon[0]], StringSplitOptions.RemoveEmptyEntries) @@ -353,15 +324,8 @@ internal static class OpenIddictHelpers public static async ValueTask> ParseFormAsync( Stream stream, Encoding encoding, CancellationToken cancellationToken) { - if (stream is null) - { - throw new ArgumentNullException(nameof(stream)); - } - - if (encoding is null) - { - throw new ArgumentNullException(nameof(encoding)); - } + ArgumentNullException.ThrowIfNull(stream); + ArgumentNullException.ThrowIfNull(encoding); var reader = new FormReader(stream, encoding); return await reader.ReadFormAsync(cancellationToken); @@ -692,7 +656,7 @@ internal static class OpenIddictHelpers /// public static byte[] CreateRandomArray(int size) { - var algorithm = GetAlgorithmFromConfig() switch + using var algorithm = GetAlgorithmFromConfig() switch { RandomNumberGenerator result => result, null => null, @@ -701,32 +665,13 @@ internal static class OpenIddictHelpers // If no custom random number generator was registered, use either the static GetBytes() or // Fill() APIs on platforms that support them or create a default instance provided by the BCL. -#if SUPPORTS_ONE_SHOT_RANDOM_NUMBER_GENERATOR_METHODS if (algorithm is null) { return RandomNumberGenerator.GetBytes(size / 8); } -#endif - var array = new byte[size / 8]; - -#if SUPPORTS_STATIC_RANDOM_NUMBER_GENERATOR_METHODS - if (algorithm is null) - { - RandomNumberGenerator.Fill(array); - return array; - } -#else - algorithm ??= RandomNumberGenerator.Create(); -#endif - try - { - algorithm.GetBytes(array); - } - finally - { - algorithm.Dispose(); - } + var array = new byte[size / 8]; + algorithm.GetBytes(array); return array; @@ -747,40 +692,30 @@ internal static class OpenIddictHelpers /// public static string CreateRandomString(ReadOnlySpan charset, int count) { - var algorithm = GetAlgorithmFromConfig() switch + using var algorithm = GetAlgorithmFromConfig() switch { RandomNumberGenerator result => result, null => null, var result => throw new CryptographicException(SR.FormatID0351(result.GetType().FullName)) }; - try - { - var builder = new StringBuilder(); + var builder = new StringBuilder(); - for (var index = 0; index < count; index++) + for (var index = 0; index < count; index++) + { + // Pick a character in the specified charset by generating a random index. + builder.Append(charset[index: algorithm switch { - // Pick a character in the specified charset by generating a random index. - builder.Append(charset[index: algorithm switch - { -#if SUPPORTS_INT32_RANDOM_NUMBER_GENERATOR_METHODS - // If no custom random number generator was registered, use - // the static GetInt32() API on platforms that support it. - null => RandomNumberGenerator.GetInt32(0, charset.Length), -#endif - // Otherwise, create a default implementation if necessary - // and use the local function that achieves the same result. - _ => GetInt32(algorithm ??= RandomNumberGenerator.Create(), 0..charset.Length) - }]); - } + // If no custom random number generator was registered, use the static GetInt32() API. + null => RandomNumberGenerator.GetInt32(0, charset.Length), - return builder.ToString(); + // Otherwise, create a default implementation if necessary + // and use the local function that achieves the same result. + _ => GetInt32(algorithm, 0..charset.Length) + }]); } - finally - { - algorithm?.Dispose(); - } + return builder.ToString(); static int GetInt32(RandomNumberGenerator algorithm, Range range) { @@ -867,32 +802,6 @@ internal static class OpenIddictHelpers #endif } - /// - /// Converts the specified hex-encoded to a byte array. - /// - /// The hexadecimal string. - /// The byte array. - public static byte[] ConvertFromHexadecimalString(string value) - { -#if SUPPORTS_HEXADECIMAL_STRING_CONVERSION - return Convert.FromHexString(value); -#else - if ((uint) value.Length % 2 is not 0) - { - throw new FormatException(SR.GetResourceString(SR.ID0413)); - } - - var array = new byte[value.Length / 2]; - - for (var index = 0; index < value.Length; index += 2) - { - array[index / 2] = Convert.ToByte(value.Substring(index, 2), 16); - } - - return array; -#endif - } - /// /// Removes the characters that are not part of /// from the specified string. @@ -906,10 +815,7 @@ internal static class OpenIddictHelpers /// is . public static string? RemoveDisallowedCharacters(string? value, IReadOnlyCollection charset) { - if (charset is null) - { - throw new ArgumentNullException(nameof(charset)); - } + ArgumentNullException.ThrowIfNull(charset); if (charset.Count is 0 || string.IsNullOrEmpty(value)) { diff --git a/shared/OpenIddict.Extensions/OpenIddictPolyfills.cs b/shared/OpenIddict.Extensions/OpenIddictPolyfills.cs deleted file mode 100644 index 6a5f50fd..00000000 --- a/shared/OpenIddict.Extensions/OpenIddictPolyfills.cs +++ /dev/null @@ -1,170 +0,0 @@ -/* - * Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) - * See https://github.com/openiddict/openiddict-core for more information concerning - * the license and the contributors participating to this project. - */ - -using System.Diagnostics; - -namespace OpenIddict.Extensions; - -/// -/// Exposes common polyfills used by the OpenIddict assemblies. -/// -internal static class OpenIddictPolyfills -{ - extension(IEnumerable source) - { -#if !SUPPORTS_CHUNK_LINQ_EXTENSION - /// - /// Split the elements of a sequence into chunks of size at most . - /// - /// - /// Every chunk except the last will be of size . - /// The last chunk will contain the remaining elements and may be of a smaller size. - /// - /// Maximum size of each chunk. - /// - /// An that contains the elements of the input - /// sequence split into chunks of size . - /// - public IEnumerable Chunk(int size) - { - // Note: this polyfill was directly copied from .NET's source code: - // https://github.com/dotnet/runtime/blob/main/src/libraries/System.Linq/src/System/Linq/Chunk.cs. - - using IEnumerator enumerator = source.GetEnumerator(); - - if (enumerator.MoveNext()) - { - var count = Math.Min(size, 4); - int index; - - do - { - var array = new TSource[count]; - - array[0] = enumerator.Current; - index = 1; - - if (size != array.Length) - { - for (; index < size && enumerator.MoveNext(); index++) - { - if (index >= array.Length) - { - count = (int) Math.Min((uint) size, 2 * (uint) array.Length); - Array.Resize(ref array, count); - } - - array[index] = enumerator.Current; - } - } - - else - { - var local = array; - Debug.Assert(local.Length == size); - for (; (uint) index < (uint) local.Length && enumerator.MoveNext(); index++) - { - local[index] = enumerator.Current; - } - } - - if (index != array.Length) - { - Array.Resize(ref array, index); - } - - yield return array; - } - - while (index >= size && enumerator.MoveNext()); - } - } -#endif - -#if !SUPPORTS_TOHASHSET_LINQ_EXTENSION - /// - /// Creates a new instance and imports the elements present in the specified source. - /// - /// The comparer to use. - /// A new instance and imports the elements present in the specified source. - public HashSet ToHashSet(IEqualityComparer? comparer) => new(source, comparer); -#endif - } - - extension(Task task) - { -#if !SUPPORTS_TASK_WAIT_ASYNC - /// - /// Waits until the specified task returns a result or the cancellation token is signaled. - /// - /// The that can be used to abort the operation. - /// - /// A that can be used to monitor the asynchronous operation. - /// The specified is signaled. - public async Task WaitAsync(CancellationToken cancellationToken) - { - var source = new TaskCompletionSource(TaskCreationOptions.None); - - using (cancellationToken.Register(static state => ((TaskCompletionSource) state!).SetResult(true), source)) - { - if (await Task.WhenAny(task, source.Task) == source.Task) - { - throw new OperationCanceledException(cancellationToken); - } - - await task; - } - } -#endif - } - - extension(Task task) - { -#if !SUPPORTS_TASK_WAIT_ASYNC - /// - /// Waits until the specified task returns a result or the cancellation token is signaled. - /// - /// The that can be used to abort the operation. - /// - /// A that can be used to monitor the asynchronous operation. - /// The specified is signaled. - public async Task WaitAsync(CancellationToken cancellationToken) - { - var source = new TaskCompletionSource(TaskCreationOptions.None); - - using (cancellationToken.Register(static state => ((TaskCompletionSource) state!).SetResult(true), source)) - { - if (await Task.WhenAny(task, source.Task) == source.Task) - { - throw new OperationCanceledException(cancellationToken); - } - - return await task; - } - } -#endif - } - - extension(ValueTask) - { -#if !SUPPORTS_VALUETASK_COMPLETED_TASK - /// - /// Gets a task that has already completed successfully. - /// - public static ValueTask CompletedTask => default; -#endif - } - - extension(ValueTask) - { -#if !SUPPORTS_VALUETASK_COMPLETED_TASK - /// - /// Gets a task that has already completed successfully. - /// - public static ValueTask CompletedTask => default; -#endif - } -} diff --git a/src/OpenIddict.Abstractions/Descriptors/OpenIddictApplicationDescriptor.cs b/src/OpenIddict.Abstractions/Descriptors/OpenIddictApplicationDescriptor.cs index 114b0878..3bf0c71f 100644 --- a/src/OpenIddict.Abstractions/Descriptors/OpenIddictApplicationDescriptor.cs +++ b/src/OpenIddict.Abstractions/Descriptors/OpenIddictApplicationDescriptor.cs @@ -89,10 +89,7 @@ public class OpenIddictApplicationDescriptor /// is . public OpenIddictApplicationDescriptor AddAudiencePermissions(params string[] audiences) { - if (audiences is null) - { - throw new ArgumentNullException(nameof(audiences)); - } + ArgumentNullException.ThrowIfNull(audiences); foreach (var audience in audiences) { @@ -110,10 +107,7 @@ public class OpenIddictApplicationDescriptor /// is . public OpenIddictApplicationDescriptor AddGrantTypePermissions(params string[] types) { - if (types is null) - { - throw new ArgumentNullException(nameof(types)); - } + ArgumentNullException.ThrowIfNull(types); foreach (var type in types) { @@ -131,10 +125,7 @@ public class OpenIddictApplicationDescriptor /// is . public OpenIddictApplicationDescriptor AddResourcePermissions(params string[] resources) { - if (resources is null) - { - throw new ArgumentNullException(nameof(resources)); - } + ArgumentNullException.ThrowIfNull(resources); foreach (var resource in resources) { @@ -152,10 +143,7 @@ public class OpenIddictApplicationDescriptor /// is . public OpenIddictApplicationDescriptor AddScopePermissions(params string[] scopes) { - if (scopes is null) - { - throw new ArgumentNullException(nameof(scopes)); - } + ArgumentNullException.ThrowIfNull(scopes); foreach (var scope in scopes) { @@ -341,10 +329,7 @@ public class OpenIddictApplicationDescriptor /// is . public OpenIddictApplicationDescriptor RemoveAudiencePermissions(params string[] audiences) { - if (audiences is null) - { - throw new ArgumentNullException(nameof(audiences)); - } + ArgumentNullException.ThrowIfNull(audiences); foreach (var audience in audiences) { @@ -362,10 +347,7 @@ public class OpenIddictApplicationDescriptor /// is . public OpenIddictApplicationDescriptor RemoveGrantTypePermissions(params string[] types) { - if (types is null) - { - throw new ArgumentNullException(nameof(types)); - } + ArgumentNullException.ThrowIfNull(types); foreach (var type in types) { @@ -383,10 +365,7 @@ public class OpenIddictApplicationDescriptor /// is . public OpenIddictApplicationDescriptor RemoveResourcePermissions(params string[] resources) { - if (resources is null) - { - throw new ArgumentNullException(nameof(resources)); - } + ArgumentNullException.ThrowIfNull(resources); foreach (var resource in resources) { @@ -404,10 +383,7 @@ public class OpenIddictApplicationDescriptor /// is . public OpenIddictApplicationDescriptor RemoveScopePermissions(params string[] scopes) { - if (scopes is null) - { - throw new ArgumentNullException(nameof(scopes)); - } + ArgumentNullException.ThrowIfNull(scopes); foreach (var scope in scopes) { diff --git a/src/OpenIddict.Abstractions/OpenIddictExtensions.cs b/src/OpenIddict.Abstractions/OpenIddictExtensions.cs index d7731ae6..e93bfb73 100644 --- a/src/OpenIddict.Abstractions/OpenIddictExtensions.cs +++ b/src/OpenIddict.Abstractions/OpenIddictExtensions.cs @@ -19,10 +19,7 @@ public static class OpenIddictExtensions /// The instance. public static OpenIddictBuilder AddOpenIddict(this IServiceCollection services) { - if (services is null) - { - throw new ArgumentNullException(nameof(services)); - } + ArgumentNullException.ThrowIfNull(services); return new OpenIddictBuilder(services); } @@ -36,15 +33,8 @@ public static class OpenIddictExtensions /// The . public static IServiceCollection AddOpenIddict(this IServiceCollection services, Action configuration) { - if (services is null) - { - throw new ArgumentNullException(nameof(services)); - } - - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(services); + ArgumentNullException.ThrowIfNull(configuration); configuration(services.AddOpenIddict()); diff --git a/src/OpenIddict.Abstractions/Primitives/OpenIddictConverter.cs b/src/OpenIddict.Abstractions/Primitives/OpenIddictConverter.cs index 10326d2b..8a8f1bd1 100644 --- a/src/OpenIddict.Abstractions/Primitives/OpenIddictConverter.cs +++ b/src/OpenIddict.Abstractions/Primitives/OpenIddictConverter.cs @@ -21,10 +21,7 @@ public sealed class OpenIddictConverter : JsonConverter /// if the type is supported, otherwise. public override bool CanConvert(Type typeToConvert) { - if (typeToConvert is null) - { - throw new ArgumentNullException(nameof(typeToConvert)); - } + ArgumentNullException.ThrowIfNull(typeToConvert); return typeToConvert == typeof(OpenIddictMessage) || typeToConvert == typeof(OpenIddictRequest) || @@ -40,10 +37,7 @@ public sealed class OpenIddictConverter : JsonConverter /// The deserialized instance. public override OpenIddictMessage Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { - if (typeToConvert is null) - { - throw new ArgumentNullException(nameof(typeToConvert)); - } + ArgumentNullException.ThrowIfNull(typeToConvert); using var document = JsonDocument.ParseValue(ref reader); @@ -61,15 +55,8 @@ public sealed class OpenIddictConverter : JsonConverter /// The JSON serializer options. public override void Write(Utf8JsonWriter writer, OpenIddictMessage value, JsonSerializerOptions options) { - if (writer is null) - { - throw new ArgumentNullException(nameof(writer)); - } - - if (value is null) - { - throw new ArgumentNullException(nameof(value)); - } + ArgumentNullException.ThrowIfNull(writer); + ArgumentNullException.ThrowIfNull(value); value.WriteTo(writer); } diff --git a/src/OpenIddict.Abstractions/Primitives/OpenIddictExtensions.cs b/src/OpenIddict.Abstractions/Primitives/OpenIddictExtensions.cs index 4391d4ff..42e3643e 100644 --- a/src/OpenIddict.Abstractions/Primitives/OpenIddictExtensions.cs +++ b/src/OpenIddict.Abstractions/Primitives/OpenIddictExtensions.cs @@ -29,10 +29,7 @@ public static class OpenIddictExtensions /// The instance. public static ImmutableArray GetAcrValues(this OpenIddictRequest request) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } + ArgumentNullException.ThrowIfNull(request); return GetValues(request.AcrValues, Separators.Space); } @@ -43,10 +40,7 @@ public static class OpenIddictExtensions /// The instance. public static ImmutableArray GetAudiences(this OpenIddictRequest request) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } + ArgumentNullException.ThrowIfNull(request); if (request.Audiences is not { IsDefaultOrEmpty: false } audiences) { @@ -72,10 +66,7 @@ public static class OpenIddictExtensions /// The instance. public static ImmutableArray GetPromptValues(this OpenIddictRequest request) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } + ArgumentNullException.ThrowIfNull(request); return GetValues(request.Prompt, Separators.Space); } @@ -86,10 +77,7 @@ public static class OpenIddictExtensions /// The instance. public static ImmutableArray GetResources(this OpenIddictRequest request) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } + ArgumentNullException.ThrowIfNull(request); if (request.Resources is not { IsDefaultOrEmpty: false } resources) { @@ -115,10 +103,7 @@ public static class OpenIddictExtensions /// The instance. public static ImmutableArray GetResponseTypes(this OpenIddictRequest request) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } + ArgumentNullException.ThrowIfNull(request); return GetValues(request.ResponseType, Separators.Space); } @@ -129,10 +114,7 @@ public static class OpenIddictExtensions /// The instance. public static ImmutableArray GetScopes(this OpenIddictRequest request) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } + ArgumentNullException.ThrowIfNull(request); return GetValues(request.Scope, Separators.Space); } @@ -144,15 +126,8 @@ public static class OpenIddictExtensions /// The component to look for in the parameter. public static bool HasAcrValue(this OpenIddictRequest request, string value) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } - - if (string.IsNullOrEmpty(value)) - { - throw new ArgumentException(SR.FormatID0366(nameof(value)), nameof(value)); - } + ArgumentNullException.ThrowIfNull(request); + ArgumentException.ThrowIfNullOrEmpty(value); return HasValue(request.AcrValues, value, Separators.Space); } @@ -164,15 +139,8 @@ public static class OpenIddictExtensions /// The value to look for in the parameters. public static bool HasAudience(this OpenIddictRequest request, string audience) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } - - if (string.IsNullOrEmpty(audience)) - { - throw new ArgumentException(SR.FormatID0366(nameof(audience)), nameof(audience)); - } + ArgumentNullException.ThrowIfNull(request); + ArgumentException.ThrowIfNullOrEmpty(audience); var audiences = request.Audiences; if (audiences is null or []) @@ -199,15 +167,8 @@ public static class OpenIddictExtensions /// The component to look for in the parameter. public static bool HasPromptValue(this OpenIddictRequest request, string value) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } - - if (string.IsNullOrEmpty(value)) - { - throw new ArgumentException(SR.FormatID0366(nameof(value)), nameof(value)); - } + ArgumentNullException.ThrowIfNull(request); + ArgumentException.ThrowIfNullOrEmpty(value); return HasValue(request.Prompt, value, Separators.Space); } @@ -219,15 +180,8 @@ public static class OpenIddictExtensions /// The value to look for in the parameters. public static bool HasResource(this OpenIddictRequest request, string resource) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } - - if (string.IsNullOrEmpty(resource)) - { - throw new ArgumentException(SR.FormatID0366(nameof(resource)), nameof(resource)); - } + ArgumentNullException.ThrowIfNull(request); + ArgumentException.ThrowIfNullOrEmpty(resource); var resources = request.Resources; if (resources is null or []) @@ -254,15 +208,8 @@ public static class OpenIddictExtensions /// The component to look for in the parameter. public static bool HasResponseType(this OpenIddictRequest request, string type) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.FormatID0366(nameof(type)), nameof(type)); - } + ArgumentNullException.ThrowIfNull(request); + ArgumentException.ThrowIfNullOrEmpty(type); return HasValue(request.ResponseType, type, Separators.Space); } @@ -274,15 +221,8 @@ public static class OpenIddictExtensions /// The component to look for in the parameter. public static bool HasScope(this OpenIddictRequest request, string scope) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } - - if (string.IsNullOrEmpty(scope)) - { - throw new ArgumentException(SR.FormatID0366(nameof(scope)), nameof(scope)); - } + ArgumentNullException.ThrowIfNull(request); + ArgumentException.ThrowIfNullOrEmpty(scope); return HasValue(request.Scope, scope, Separators.Space); } @@ -295,10 +235,7 @@ public static class OpenIddictExtensions /// if the request is a response_type=none request, otherwise. public static bool IsNoneFlow(this OpenIddictRequest request) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } + ArgumentNullException.ThrowIfNull(request); if (string.IsNullOrEmpty(request.ResponseType)) { @@ -322,10 +259,7 @@ public static class OpenIddictExtensions /// if the request is a code flow request, otherwise. public static bool IsAuthorizationCodeFlow(this OpenIddictRequest request) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } + ArgumentNullException.ThrowIfNull(request); if (string.IsNullOrEmpty(request.ResponseType)) { @@ -350,10 +284,7 @@ public static class OpenIddictExtensions /// if the request is an implicit flow request, otherwise. public static bool IsImplicitFlow(this OpenIddictRequest request) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } + ArgumentNullException.ThrowIfNull(request); if (string.IsNullOrEmpty(request.ResponseType)) { @@ -404,10 +335,7 @@ public static class OpenIddictExtensions /// if the request is an hybrid flow request, otherwise. public static bool IsHybridFlow(this OpenIddictRequest request) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } + ArgumentNullException.ThrowIfNull(request); if (string.IsNullOrEmpty(request.ResponseType)) { @@ -471,10 +399,7 @@ public static class OpenIddictExtensions /// public static bool IsFragmentResponseMode(this OpenIddictRequest request) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } + ArgumentNullException.ThrowIfNull(request); if (string.Equals(request.ResponseMode, ResponseModes.Fragment, StringComparison.Ordinal)) { @@ -504,10 +429,7 @@ public static class OpenIddictExtensions /// public static bool IsQueryResponseMode(this OpenIddictRequest request) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } + ArgumentNullException.ThrowIfNull(request); if (string.Equals(request.ResponseMode, ResponseModes.Query, StringComparison.Ordinal)) { @@ -536,10 +458,7 @@ public static class OpenIddictExtensions /// public static bool IsFormPostResponseMode(this OpenIddictRequest request) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } + ArgumentNullException.ThrowIfNull(request); return string.Equals(request.ResponseMode, ResponseModes.FormPost, StringComparison.Ordinal); } @@ -552,10 +471,7 @@ public static class OpenIddictExtensions /// if the request is a code grant request, otherwise. public static bool IsAuthorizationCodeGrantType(this OpenIddictRequest request) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } + ArgumentNullException.ThrowIfNull(request); return string.Equals(request.GrantType, GrantTypes.AuthorizationCode, StringComparison.Ordinal); } @@ -568,10 +484,7 @@ public static class OpenIddictExtensions /// if the request is a client credentials grant request, otherwise. public static bool IsClientCredentialsGrantType(this OpenIddictRequest request) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } + ArgumentNullException.ThrowIfNull(request); return string.Equals(request.GrantType, GrantTypes.ClientCredentials, StringComparison.Ordinal); } @@ -584,10 +497,7 @@ public static class OpenIddictExtensions /// if the request is a device code grant request, otherwise. public static bool IsDeviceCodeGrantType(this OpenIddictRequest request) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } + ArgumentNullException.ThrowIfNull(request); return string.Equals(request.GrantType, GrantTypes.DeviceCode, StringComparison.Ordinal); } @@ -600,10 +510,7 @@ public static class OpenIddictExtensions /// if the request is a password grant request, otherwise. public static bool IsPasswordGrantType(this OpenIddictRequest request) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } + ArgumentNullException.ThrowIfNull(request); return string.Equals(request.GrantType, GrantTypes.Password, StringComparison.Ordinal); } @@ -616,10 +523,7 @@ public static class OpenIddictExtensions /// if the request is a refresh token grant request, otherwise. public static bool IsRefreshTokenGrantType(this OpenIddictRequest request) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } + ArgumentNullException.ThrowIfNull(request); return string.Equals(request.GrantType, GrantTypes.RefreshToken, StringComparison.Ordinal); } @@ -632,10 +536,7 @@ public static class OpenIddictExtensions /// if the request is a token exchange grant request, otherwise. public static bool IsTokenExchangeGrantType(this OpenIddictRequest request) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } + ArgumentNullException.ThrowIfNull(request); return string.Equals(request.GrantType, GrantTypes.TokenExchange, StringComparison.Ordinal); } @@ -647,10 +548,7 @@ public static class OpenIddictExtensions /// The destinations associated with the claim. public static ImmutableArray GetDestinations(this Claim claim) { - if (claim is null) - { - throw new ArgumentNullException(nameof(claim)); - } + ArgumentNullException.ThrowIfNull(claim); claim.Properties.TryGetValue(Properties.Destinations, out string? destinations); @@ -683,15 +581,8 @@ public static class OpenIddictExtensions /// The required destination. public static bool HasDestination(this Claim claim, string destination) { - if (claim is null) - { - throw new ArgumentNullException(nameof(claim)); - } - - if (string.IsNullOrEmpty(destination)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0181), nameof(destination)); - } + ArgumentNullException.ThrowIfNull(claim); + ArgumentException.ThrowIfNullOrEmpty(destination); claim.Properties.TryGetValue(Properties.Destinations, out string? destinations); @@ -722,10 +613,7 @@ public static class OpenIddictExtensions [OverloadResolutionPriority(0)] public static Claim SetDestinations(this Claim claim, ImmutableArray destinations) { - if (claim is null) - { - throw new ArgumentNullException(nameof(claim)); - } + ArgumentNullException.ThrowIfNull(claim); if (destinations.IsDefaultOrEmpty) { @@ -786,10 +674,7 @@ public static class OpenIddictExtensions /// The destinations, returned as a flattened dictionary. public static ImmutableDictionary> GetDestinations(this ClaimsIdentity identity) { - if (identity is null) - { - throw new ArgumentNullException(nameof(identity)); - } + ArgumentNullException.ThrowIfNull(identity); var builder = ImmutableDictionary.CreateBuilder>(StringComparer.Ordinal); @@ -823,10 +708,7 @@ public static class OpenIddictExtensions /// The destinations, returned as a flattened dictionary. public static ImmutableDictionary> GetDestinations(this ClaimsPrincipal principal) { - if (principal is null) - { - throw new ArgumentNullException(nameof(principal)); - } + ArgumentNullException.ThrowIfNull(principal); var builder = ImmutableDictionary.CreateBuilder>(StringComparer.Ordinal); @@ -862,15 +744,8 @@ public static class OpenIddictExtensions public static ClaimsIdentity SetDestinations(this ClaimsIdentity identity, ImmutableDictionary> destinations) { - if (identity is null) - { - throw new ArgumentNullException(nameof(identity)); - } - - if (destinations is null) - { - throw new ArgumentNullException(nameof(destinations)); - } + ArgumentNullException.ThrowIfNull(identity); + ArgumentNullException.ThrowIfNull(destinations); foreach (var destination in destinations) { @@ -892,15 +767,8 @@ public static class OpenIddictExtensions public static ClaimsPrincipal SetDestinations(this ClaimsPrincipal principal, ImmutableDictionary> destinations) { - if (principal is null) - { - throw new ArgumentNullException(nameof(principal)); - } - - if (destinations is null) - { - throw new ArgumentNullException(nameof(destinations)); - } + ArgumentNullException.ThrowIfNull(principal); + ArgumentNullException.ThrowIfNull(destinations); foreach (var destination in destinations) { @@ -921,15 +789,8 @@ public static class OpenIddictExtensions /// The identity. public static ClaimsIdentity SetDestinations(this ClaimsIdentity identity, Func> selector) { - if (identity is null) - { - throw new ArgumentNullException(nameof(identity)); - } - - if (selector is null) - { - throw new ArgumentNullException(nameof(selector)); - } + ArgumentNullException.ThrowIfNull(identity); + ArgumentNullException.ThrowIfNull(selector); foreach (var claim in identity.Claims) { @@ -947,15 +808,8 @@ public static class OpenIddictExtensions /// The principal. public static ClaimsPrincipal SetDestinations(this ClaimsPrincipal principal, Func> selector) { - if (principal is null) - { - throw new ArgumentNullException(nameof(principal)); - } - - if (selector is null) - { - throw new ArgumentNullException(nameof(selector)); - } + ArgumentNullException.ThrowIfNull(principal); + ArgumentNullException.ThrowIfNull(selector); foreach (var claim in principal.Claims) { @@ -975,15 +829,8 @@ public static class OpenIddictExtensions /// public static ClaimsIdentity Clone(this ClaimsIdentity identity, Func filter) { - if (identity is null) - { - throw new ArgumentNullException(nameof(identity)); - } - - if (filter is null) - { - throw new ArgumentNullException(nameof(filter)); - } + ArgumentNullException.ThrowIfNull(identity); + ArgumentNullException.ThrowIfNull(filter); var clone = identity.Clone(); @@ -1015,15 +862,8 @@ public static class OpenIddictExtensions /// public static ClaimsPrincipal Clone(this ClaimsPrincipal principal, Func filter) { - if (principal is null) - { - throw new ArgumentNullException(nameof(principal)); - } - - if (filter is null) - { - throw new ArgumentNullException(nameof(filter)); - } + ArgumentNullException.ThrowIfNull(principal); + ArgumentNullException.ThrowIfNull(filter); var clone = new ClaimsPrincipal(); @@ -1062,20 +902,9 @@ public static class OpenIddictExtensions /// The issuer associated with the claim. public static ClaimsIdentity AddClaim(this ClaimsIdentity identity, string type, string value, string issuer) { - if (identity is null) - { - throw new ArgumentNullException(nameof(identity)); - } - - if (value is null) - { - throw new ArgumentNullException(nameof(value)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } + ArgumentNullException.ThrowIfNull(identity); + ArgumentNullException.ThrowIfNull(value); + ArgumentException.ThrowIfNullOrEmpty(type); identity.AddClaim(new Claim(type, value, ClaimValueTypes.String, issuer, issuer, identity)); return identity; @@ -1090,21 +919,14 @@ public static class OpenIddictExtensions /// The issuer associated with the claim. public static ClaimsPrincipal AddClaim(this ClaimsPrincipal principal, string type, string value, string issuer) { - if (principal is null) - { - throw new ArgumentNullException(nameof(principal)); - } + ArgumentNullException.ThrowIfNull(principal); + ArgumentException.ThrowIfNullOrEmpty(type); if (principal.Identity is not ClaimsIdentity identity) { throw new ArgumentException(SR.GetResourceString(SR.ID0286), nameof(principal)); } - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } - identity.AddClaim(type, value, issuer); return principal; } @@ -1136,15 +958,8 @@ public static class OpenIddictExtensions /// The issuer associated with the claim. public static ClaimsIdentity AddClaim(this ClaimsIdentity identity, string type, bool value, string issuer) { - if (identity is null) - { - throw new ArgumentNullException(nameof(identity)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } + ArgumentNullException.ThrowIfNull(identity); + ArgumentException.ThrowIfNullOrEmpty(type); identity.AddClaim(new Claim(type, value.ToString(), ClaimValueTypes.Boolean, issuer, issuer, identity)); return identity; @@ -1159,10 +974,7 @@ public static class OpenIddictExtensions /// The issuer associated with the claim. public static ClaimsPrincipal AddClaim(this ClaimsPrincipal principal, string type, bool value, string issuer) { - if (principal is null) - { - throw new ArgumentNullException(nameof(principal)); - } + ArgumentNullException.ThrowIfNull(principal); if (principal.Identity is not ClaimsIdentity identity) { @@ -1200,15 +1012,8 @@ public static class OpenIddictExtensions /// The issuer associated with the claim. public static ClaimsIdentity AddClaim(this ClaimsIdentity identity, string type, long value, string issuer) { - if (identity is null) - { - throw new ArgumentNullException(nameof(identity)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } + ArgumentNullException.ThrowIfNull(identity); + ArgumentException.ThrowIfNullOrEmpty(type); identity.AddClaim(new Claim(type, value.ToString(CultureInfo.InvariantCulture), ClaimValueTypes.Integer64, issuer, issuer, identity)); @@ -1224,21 +1029,14 @@ public static class OpenIddictExtensions /// The issuer associated with the claim. public static ClaimsPrincipal AddClaim(this ClaimsPrincipal principal, string type, long value, string issuer) { - if (principal is null) - { - throw new ArgumentNullException(nameof(principal)); - } + ArgumentNullException.ThrowIfNull(principal); + ArgumentException.ThrowIfNullOrEmpty(type); if (principal.Identity is not ClaimsIdentity identity) { throw new ArgumentException(SR.GetResourceString(SR.ID0286), nameof(principal)); } - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } - identity.AddClaim(type, value, issuer); return principal; } @@ -1270,15 +1068,8 @@ public static class OpenIddictExtensions /// The issuer associated with the claim. public static ClaimsIdentity AddClaim(this ClaimsIdentity identity, string type, JsonElement value, string issuer) { - if (identity is null) - { - throw new ArgumentNullException(nameof(identity)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } + ArgumentNullException.ThrowIfNull(identity); + ArgumentException.ThrowIfNullOrEmpty(type); if (value.ValueKind is JsonValueKind.Array) { @@ -1305,21 +1096,14 @@ public static class OpenIddictExtensions /// The issuer associated with the claim. public static ClaimsPrincipal AddClaim(this ClaimsPrincipal principal, string type, JsonElement value, string issuer) { - if (principal is null) - { - throw new ArgumentNullException(nameof(principal)); - } + ArgumentNullException.ThrowIfNull(principal); + ArgumentException.ThrowIfNullOrEmpty(type); if (principal.Identity is not ClaimsIdentity identity) { throw new ArgumentException(SR.GetResourceString(SR.ID0286), nameof(principal)); } - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } - identity.AddClaim(type, value, issuer); return principal; @@ -1352,20 +1136,10 @@ public static class OpenIddictExtensions /// The issuer associated with the claim. public static ClaimsIdentity AddClaim(this ClaimsIdentity identity, string type, JsonNode value, string issuer) { - if (identity is null) - { - throw new ArgumentNullException(nameof(identity)); - } + ArgumentNullException.ThrowIfNull(identity); + ArgumentException.ThrowIfNullOrEmpty(type); - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } - - if (value is null) - { - throw new ArgumentNullException(nameof(value)); - } + ArgumentNullException.ThrowIfNull(value); if (value is JsonArray) { @@ -1399,25 +1173,15 @@ public static class OpenIddictExtensions /// The issuer associated with the claim. public static ClaimsPrincipal AddClaim(this ClaimsPrincipal principal, string type, JsonNode value, string issuer) { - if (principal is null) - { - throw new ArgumentNullException(nameof(principal)); - } + ArgumentNullException.ThrowIfNull(principal); + ArgumentException.ThrowIfNullOrEmpty(type); if (principal.Identity is not ClaimsIdentity identity) { throw new ArgumentException(SR.GetResourceString(SR.ID0286), nameof(principal)); } - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } - - if (value is null) - { - throw new ArgumentNullException(nameof(value)); - } + ArgumentNullException.ThrowIfNull(value); identity.AddClaim(type, value, issuer); @@ -1454,20 +1218,9 @@ public static class OpenIddictExtensions public static ClaimsIdentity AddClaim(this ClaimsIdentity identity, string type, IDictionary value, string issuer) { - if (identity is null) - { - throw new ArgumentNullException(nameof(identity)); - } - - if (value is null) - { - throw new ArgumentNullException(nameof(value)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } + ArgumentNullException.ThrowIfNull(identity); + ArgumentNullException.ThrowIfNull(value); + ArgumentException.ThrowIfNullOrEmpty(type); using var stream = new MemoryStream(); using var writer = new Utf8JsonWriter(stream, new JsonWriterOptions @@ -1508,21 +1261,14 @@ public static class OpenIddictExtensions public static ClaimsPrincipal AddClaim(this ClaimsPrincipal principal, string type, IDictionary value, string issuer) { - if (principal is null) - { - throw new ArgumentNullException(nameof(principal)); - } + ArgumentNullException.ThrowIfNull(principal); + ArgumentException.ThrowIfNullOrEmpty(type); if (principal.Identity is not ClaimsIdentity identity) { throw new ArgumentException(SR.GetResourceString(SR.ID0286), nameof(principal)); } - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } - identity.AddClaim(type, value, issuer); return principal; @@ -1558,15 +1304,8 @@ public static class OpenIddictExtensions [OverloadResolutionPriority(0)] public static ClaimsIdentity AddClaims(this ClaimsIdentity identity, string type, ImmutableArray values, string issuer) { - if (identity is null) - { - throw new ArgumentNullException(nameof(identity)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } + ArgumentNullException.ThrowIfNull(identity); + ArgumentException.ThrowIfNullOrEmpty(type); var set = new HashSet(StringComparer.Ordinal); @@ -1599,21 +1338,14 @@ public static class OpenIddictExtensions public static ClaimsPrincipal AddClaims(this ClaimsPrincipal principal, string type, ImmutableArray values, string issuer) { - if (principal is null) - { - throw new ArgumentNullException(nameof(principal)); - } + ArgumentNullException.ThrowIfNull(principal); + ArgumentException.ThrowIfNullOrEmpty(type); if (principal.Identity is not ClaimsIdentity identity) { throw new ArgumentException(SR.GetResourceString(SR.ID0286), nameof(principal)); } - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } - identity.AddClaims(type, values, issuer); return principal; @@ -1646,15 +1378,8 @@ public static class OpenIddictExtensions /// The issuer associated with the claims. public static ClaimsIdentity AddClaims(this ClaimsIdentity identity, string type, JsonElement value, string issuer) { - if (identity is null) - { - throw new ArgumentNullException(nameof(identity)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } + ArgumentNullException.ThrowIfNull(identity); + ArgumentException.ThrowIfNullOrEmpty(type); if (value.ValueKind is not JsonValueKind.Array) { @@ -1690,21 +1415,14 @@ public static class OpenIddictExtensions /// The issuer associated with the claims. public static ClaimsPrincipal AddClaims(this ClaimsPrincipal principal, string type, JsonElement value, string issuer) { - if (principal is null) - { - throw new ArgumentNullException(nameof(principal)); - } + ArgumentNullException.ThrowIfNull(principal); + ArgumentException.ThrowIfNullOrEmpty(type); if (principal.Identity is not ClaimsIdentity identity) { throw new ArgumentException(SR.GetResourceString(SR.ID0286), nameof(principal)); } - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } - if (value.ValueKind is not JsonValueKind.Array) { throw new ArgumentException(SR.GetResourceString(SR.ID0185), nameof(value)); @@ -1745,20 +1463,9 @@ public static class OpenIddictExtensions [OverloadResolutionPriority(-1)] public static ClaimsIdentity AddClaims(this ClaimsIdentity identity, string type, JsonArray value, string issuer) { - if (identity is null) - { - throw new ArgumentNullException(nameof(identity)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } - - if (value is null) - { - throw new ArgumentNullException(nameof(value)); - } + ArgumentNullException.ThrowIfNull(identity); + ArgumentNullException.ThrowIfNull(value); + ArgumentException.ThrowIfNullOrEmpty(type); var set = new HashSet(StringComparer.Ordinal); @@ -1801,21 +1508,14 @@ public static class OpenIddictExtensions [OverloadResolutionPriority(-1)] public static ClaimsPrincipal AddClaims(this ClaimsPrincipal principal, string type, JsonArray value, string issuer) { - if (principal is null) - { - throw new ArgumentNullException(nameof(principal)); - } + ArgumentNullException.ThrowIfNull(principal); + ArgumentException.ThrowIfNullOrEmpty(type); if (principal.Identity is not ClaimsIdentity identity) { throw new ArgumentException(SR.GetResourceString(SR.ID0286), nameof(principal)); } - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } - identity.AddClaims(type, value, issuer); return principal; @@ -1832,15 +1532,8 @@ public static class OpenIddictExtensions /// The claim value. public static string? GetClaim(this ClaimsIdentity identity, string type) { - if (identity is null) - { - throw new ArgumentNullException(nameof(identity)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } + ArgumentNullException.ThrowIfNull(identity); + ArgumentException.ThrowIfNullOrEmpty(type); var claims = identity.FindAll(type); if (claims is IList list) @@ -1882,15 +1575,8 @@ public static class OpenIddictExtensions /// The claim value. public static string? GetClaim(this ClaimsPrincipal principal, string type) { - if (principal is null) - { - throw new ArgumentNullException(nameof(principal)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } + ArgumentNullException.ThrowIfNull(principal); + ArgumentException.ThrowIfNullOrEmpty(type); var claims = principal.FindAll(type); if (claims is IList list) @@ -1929,15 +1615,8 @@ public static class OpenIddictExtensions /// The claim values. public static ImmutableArray GetClaims(this ClaimsIdentity identity, string type) { - if (identity is null) - { - throw new ArgumentNullException(nameof(identity)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } + ArgumentNullException.ThrowIfNull(identity); + ArgumentException.ThrowIfNullOrEmpty(type); var builder = ImmutableArray.CreateBuilder(); @@ -1981,15 +1660,8 @@ public static class OpenIddictExtensions /// The claim values. public static ImmutableArray GetClaims(this ClaimsPrincipal principal, string type) { - if (principal is null) - { - throw new ArgumentNullException(nameof(principal)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } + ArgumentNullException.ThrowIfNull(principal); + ArgumentException.ThrowIfNullOrEmpty(type); var builder = ImmutableArray.CreateBuilder(); @@ -2033,15 +1705,8 @@ public static class OpenIddictExtensions /// if the identity contains at least one claim of the specified type. public static bool HasClaim(this ClaimsIdentity identity, string type) { - if (identity is null) - { - throw new ArgumentNullException(nameof(identity)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } + ArgumentNullException.ThrowIfNull(identity); + ArgumentException.ThrowIfNullOrEmpty(type); return identity.FindAll(type).Any(); } @@ -2054,15 +1719,8 @@ public static class OpenIddictExtensions /// if the principal contains at least one claim of the specified type. public static bool HasClaim(this ClaimsPrincipal principal, string type) { - if (principal is null) - { - throw new ArgumentNullException(nameof(principal)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } + ArgumentNullException.ThrowIfNull(principal); + ArgumentException.ThrowIfNullOrEmpty(type); return principal.FindAll(type).Any(); } @@ -2075,15 +1733,8 @@ public static class OpenIddictExtensions /// The claims identity. public static ClaimsIdentity RemoveClaims(this ClaimsIdentity identity, string type) { - if (identity is null) - { - throw new ArgumentNullException(nameof(identity)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } + ArgumentNullException.ThrowIfNull(identity); + ArgumentException.ThrowIfNullOrEmpty(type); foreach (var claim in identity.FindAll(type).ToList()) { @@ -2101,15 +1752,8 @@ public static class OpenIddictExtensions /// The claims identity. public static ClaimsPrincipal RemoveClaims(this ClaimsPrincipal principal, string type) { - if (principal is null) - { - throw new ArgumentNullException(nameof(principal)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } + ArgumentNullException.ThrowIfNull(principal); + ArgumentException.ThrowIfNullOrEmpty(type); foreach (var identity in principal.Identities) { @@ -2152,15 +1796,8 @@ public static class OpenIddictExtensions /// The claims identity. public static ClaimsIdentity SetClaim(this ClaimsIdentity identity, string type, string? value, string issuer) { - if (identity is null) - { - throw new ArgumentNullException(nameof(identity)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } + ArgumentNullException.ThrowIfNull(identity); + ArgumentException.ThrowIfNullOrEmpty(type); identity.RemoveClaims(type); @@ -2182,15 +1819,8 @@ public static class OpenIddictExtensions /// The claims identity. public static ClaimsPrincipal SetClaim(this ClaimsPrincipal principal, string type, string? value, string issuer) { - if (principal is null) - { - throw new ArgumentNullException(nameof(principal)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } + ArgumentNullException.ThrowIfNull(principal); + ArgumentException.ThrowIfNullOrEmpty(type); principal.RemoveClaims(type); @@ -2232,15 +1862,8 @@ public static class OpenIddictExtensions /// The claims identity. public static ClaimsIdentity SetClaim(this ClaimsIdentity identity, string type, bool? value, string issuer) { - if (identity is null) - { - throw new ArgumentNullException(nameof(identity)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } + ArgumentNullException.ThrowIfNull(identity); + ArgumentException.ThrowIfNullOrEmpty(type); identity.RemoveClaims(type); @@ -2262,15 +1885,8 @@ public static class OpenIddictExtensions /// The claims identity. public static ClaimsPrincipal SetClaim(this ClaimsPrincipal principal, string type, bool? value, string issuer) { - if (principal is null) - { - throw new ArgumentNullException(nameof(principal)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } + ArgumentNullException.ThrowIfNull(principal); + ArgumentException.ThrowIfNullOrEmpty(type); principal.RemoveClaims(type); @@ -2312,15 +1928,8 @@ public static class OpenIddictExtensions /// The claims identity. public static ClaimsIdentity SetClaim(this ClaimsIdentity identity, string type, long? value, string issuer) { - if (identity is null) - { - throw new ArgumentNullException(nameof(identity)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } + ArgumentNullException.ThrowIfNull(identity); + ArgumentException.ThrowIfNullOrEmpty(type); identity.RemoveClaims(type); @@ -2342,15 +1951,8 @@ public static class OpenIddictExtensions /// The claims identity. public static ClaimsPrincipal SetClaim(this ClaimsPrincipal principal, string type, long? value, string issuer) { - if (principal is null) - { - throw new ArgumentNullException(nameof(principal)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } + ArgumentNullException.ThrowIfNull(principal); + ArgumentException.ThrowIfNullOrEmpty(type); principal.RemoveClaims(type); @@ -2392,15 +1994,8 @@ public static class OpenIddictExtensions /// The claims identity. public static ClaimsIdentity SetClaim(this ClaimsIdentity identity, string type, JsonElement value, string issuer) { - if (identity is null) - { - throw new ArgumentNullException(nameof(identity)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } + ArgumentNullException.ThrowIfNull(identity); + ArgumentException.ThrowIfNullOrEmpty(type); identity.RemoveClaims(type); @@ -2422,15 +2017,8 @@ public static class OpenIddictExtensions /// The claims identity. public static ClaimsPrincipal SetClaim(this ClaimsPrincipal principal, string type, JsonElement value, string issuer) { - if (principal is null) - { - throw new ArgumentNullException(nameof(principal)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } + ArgumentNullException.ThrowIfNull(principal); + ArgumentException.ThrowIfNullOrEmpty(type); principal.RemoveClaims(type); @@ -2472,15 +2060,8 @@ public static class OpenIddictExtensions /// The claims identity. public static ClaimsIdentity SetClaim(this ClaimsIdentity identity, string type, JsonNode? value, string issuer) { - if (identity is null) - { - throw new ArgumentNullException(nameof(identity)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } + ArgumentNullException.ThrowIfNull(identity); + ArgumentException.ThrowIfNullOrEmpty(type); identity.RemoveClaims(type); @@ -2502,15 +2083,8 @@ public static class OpenIddictExtensions /// The claims identity. public static ClaimsPrincipal SetClaim(this ClaimsPrincipal principal, string type, JsonNode? value, string issuer) { - if (principal is null) - { - throw new ArgumentNullException(nameof(principal)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } + ArgumentNullException.ThrowIfNull(principal); + ArgumentException.ThrowIfNullOrEmpty(type); principal.RemoveClaims(type); @@ -2555,15 +2129,8 @@ public static class OpenIddictExtensions public static ClaimsIdentity SetClaim(this ClaimsIdentity identity, string type, IDictionary? value, string issuer) { - if (identity is null) - { - throw new ArgumentNullException(nameof(identity)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } + ArgumentNullException.ThrowIfNull(identity); + ArgumentException.ThrowIfNullOrEmpty(type); identity.RemoveClaims(type); @@ -2586,15 +2153,8 @@ public static class OpenIddictExtensions public static ClaimsPrincipal SetClaim(this ClaimsPrincipal principal, string type, IDictionary? value, string issuer) { - if (principal is null) - { - throw new ArgumentNullException(nameof(principal)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } + ArgumentNullException.ThrowIfNull(principal); + ArgumentException.ThrowIfNullOrEmpty(type); principal.RemoveClaims(type); @@ -2640,15 +2200,8 @@ public static class OpenIddictExtensions public static ClaimsIdentity SetClaims(this ClaimsIdentity identity, string type, ImmutableArray values, string issuer) { - if (identity is null) - { - throw new ArgumentNullException(nameof(identity)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } + ArgumentNullException.ThrowIfNull(identity); + ArgumentException.ThrowIfNullOrEmpty(type); identity.RemoveClaims(type); @@ -2672,15 +2225,8 @@ public static class OpenIddictExtensions public static ClaimsPrincipal SetClaims(this ClaimsPrincipal principal, string type, ImmutableArray values, string issuer) { - if (principal is null) - { - throw new ArgumentNullException(nameof(principal)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } + ArgumentNullException.ThrowIfNull(principal); + ArgumentException.ThrowIfNullOrEmpty(type); principal.RemoveClaims(type); @@ -2722,15 +2268,8 @@ public static class OpenIddictExtensions /// The claims identity. public static ClaimsIdentity SetClaims(this ClaimsIdentity identity, string type, JsonElement value, string issuer) { - if (identity is null) - { - throw new ArgumentNullException(nameof(identity)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } + ArgumentNullException.ThrowIfNull(identity); + ArgumentException.ThrowIfNullOrEmpty(type); identity.RemoveClaims(type); @@ -2752,15 +2291,8 @@ public static class OpenIddictExtensions /// The claims identity. public static ClaimsPrincipal SetClaims(this ClaimsPrincipal principal, string type, JsonElement value, string issuer) { - if (principal is null) - { - throw new ArgumentNullException(nameof(principal)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } + ArgumentNullException.ThrowIfNull(principal); + ArgumentException.ThrowIfNullOrEmpty(type); principal.RemoveClaims(type); @@ -2805,20 +2337,9 @@ public static class OpenIddictExtensions [OverloadResolutionPriority(-1)] public static ClaimsIdentity SetClaims(this ClaimsIdentity identity, string type, JsonArray value, string issuer) { - if (identity is null) - { - throw new ArgumentNullException(nameof(identity)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } - - if (value is null) - { - throw new ArgumentNullException(nameof(value)); - } + ArgumentNullException.ThrowIfNull(identity); + ArgumentNullException.ThrowIfNull(value); + ArgumentException.ThrowIfNullOrEmpty(type); identity.RemoveClaims(type); @@ -2841,20 +2362,9 @@ public static class OpenIddictExtensions [OverloadResolutionPriority(-1)] public static ClaimsPrincipal SetClaims(this ClaimsPrincipal principal, string type, JsonArray value, string issuer) { - if (principal is null) - { - throw new ArgumentNullException(nameof(principal)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0184), nameof(type)); - } - - if (value is null) - { - throw new ArgumentNullException(nameof(value)); - } + ArgumentNullException.ThrowIfNull(principal); + ArgumentNullException.ThrowIfNull(value); + ArgumentException.ThrowIfNullOrEmpty(type); principal.RemoveClaims(type); @@ -2873,10 +2383,7 @@ public static class OpenIddictExtensions /// The creation date or if the claim cannot be found. public static DateTimeOffset? GetCreationDate(this ClaimsIdentity identity) { - if (identity is null) - { - throw new ArgumentNullException(nameof(identity)); - } + ArgumentNullException.ThrowIfNull(identity); var claim = identity.FindFirst(Claims.Private.CreationDate); if (claim is null) @@ -2899,10 +2406,7 @@ public static class OpenIddictExtensions /// The creation date or if the claim cannot be found. public static DateTimeOffset? GetCreationDate(this ClaimsPrincipal principal) { - if (principal is null) - { - throw new ArgumentNullException(nameof(principal)); - } + ArgumentNullException.ThrowIfNull(principal); var claim = principal.FindFirst(Claims.Private.CreationDate); if (claim is null) @@ -2925,10 +2429,7 @@ public static class OpenIddictExtensions /// The expiration date or if the claim cannot be found. public static DateTimeOffset? GetExpirationDate(this ClaimsIdentity identity) { - if (identity is null) - { - throw new ArgumentNullException(nameof(identity)); - } + ArgumentNullException.ThrowIfNull(identity); var claim = identity.FindFirst(Claims.Private.ExpirationDate); if (claim is null) @@ -2951,10 +2452,7 @@ public static class OpenIddictExtensions /// The expiration date or if the claim cannot be found. public static DateTimeOffset? GetExpirationDate(this ClaimsPrincipal principal) { - if (principal is null) - { - throw new ArgumentNullException(nameof(principal)); - } + ArgumentNullException.ThrowIfNull(principal); var claim = principal.FindFirst(Claims.Private.ExpirationDate); if (claim is null) @@ -3234,15 +2732,8 @@ public static class OpenIddictExtensions /// if the identity contains the given audience. public static bool HasAudience(this ClaimsIdentity identity, string audience) { - if (identity is null) - { - throw new ArgumentNullException(nameof(identity)); - } - - if (string.IsNullOrEmpty(audience)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0186), nameof(audience)); - } + ArgumentNullException.ThrowIfNull(identity); + ArgumentException.ThrowIfNullOrEmpty(audience); return identity.HasClaim(Claims.Private.Audience, audience); } @@ -3255,15 +2746,8 @@ public static class OpenIddictExtensions /// if the principal contains the given audience. public static bool HasAudience(this ClaimsPrincipal principal, string audience) { - if (principal is null) - { - throw new ArgumentNullException(nameof(principal)); - } - - if (string.IsNullOrEmpty(audience)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0186), nameof(audience)); - } + ArgumentNullException.ThrowIfNull(principal); + ArgumentException.ThrowIfNullOrEmpty(audience); return principal.HasClaim(Claims.Private.Audience, audience); } @@ -3276,15 +2760,8 @@ public static class OpenIddictExtensions /// if the identity contains the given presenter. public static bool HasPresenter(this ClaimsIdentity identity, string presenter) { - if (identity is null) - { - throw new ArgumentNullException(nameof(identity)); - } - - if (string.IsNullOrEmpty(presenter)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0187), nameof(presenter)); - } + ArgumentNullException.ThrowIfNull(identity); + ArgumentException.ThrowIfNullOrEmpty(presenter); return identity.HasClaim(Claims.Private.Presenter, presenter); } @@ -3297,15 +2774,8 @@ public static class OpenIddictExtensions /// if the principal contains the given presenter. public static bool HasPresenter(this ClaimsPrincipal principal, string presenter) { - if (principal is null) - { - throw new ArgumentNullException(nameof(principal)); - } - - if (string.IsNullOrEmpty(presenter)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0187), nameof(presenter)); - } + ArgumentNullException.ThrowIfNull(principal); + ArgumentException.ThrowIfNullOrEmpty(presenter); return principal.HasClaim(Claims.Private.Presenter, presenter); } @@ -3318,15 +2788,8 @@ public static class OpenIddictExtensions /// if the identity contains the given resource. public static bool HasResource(this ClaimsIdentity identity, string resource) { - if (identity is null) - { - throw new ArgumentNullException(nameof(identity)); - } - - if (string.IsNullOrEmpty(resource)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0062), nameof(resource)); - } + ArgumentNullException.ThrowIfNull(identity); + ArgumentException.ThrowIfNullOrEmpty(resource); return identity.HasClaim(Claims.Private.Resource, resource); } @@ -3339,15 +2802,8 @@ public static class OpenIddictExtensions /// if the principal contains the given resource. public static bool HasResource(this ClaimsPrincipal principal, string resource) { - if (principal is null) - { - throw new ArgumentNullException(nameof(principal)); - } - - if (string.IsNullOrEmpty(resource)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0062), nameof(resource)); - } + ArgumentNullException.ThrowIfNull(principal); + ArgumentException.ThrowIfNullOrEmpty(resource); return principal.HasClaim(Claims.Private.Resource, resource); } @@ -3360,15 +2816,8 @@ public static class OpenIddictExtensions /// if the identity contains the given scope. public static bool HasScope(this ClaimsIdentity identity, string scope) { - if (identity is null) - { - throw new ArgumentNullException(nameof(identity)); - } - - if (string.IsNullOrEmpty(scope)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0180), nameof(scope)); - } + ArgumentNullException.ThrowIfNull(identity); + ArgumentException.ThrowIfNullOrEmpty(scope); return identity.HasClaim(Claims.Private.Scope, scope); } @@ -3381,15 +2830,8 @@ public static class OpenIddictExtensions /// if the principal contains the given scope. public static bool HasScope(this ClaimsPrincipal principal, string scope) { - if (principal is null) - { - throw new ArgumentNullException(nameof(principal)); - } - - if (string.IsNullOrEmpty(scope)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0180), nameof(scope)); - } + ArgumentNullException.ThrowIfNull(principal); + ArgumentException.ThrowIfNullOrEmpty(scope); return principal.HasClaim(Claims.Private.Scope, scope); } @@ -3402,15 +2844,8 @@ public static class OpenIddictExtensions /// if the token type matches the specified type. public static bool HasTokenType(this ClaimsIdentity identity, string type) { - if (identity is null) - { - throw new ArgumentNullException(nameof(identity)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0188), nameof(type)); - } + ArgumentNullException.ThrowIfNull(identity); + ArgumentException.ThrowIfNullOrEmpty(type); return string.Equals(identity.GetTokenType(), type, StringComparison.OrdinalIgnoreCase); } @@ -3423,15 +2858,8 @@ public static class OpenIddictExtensions /// if the token type matches the specified type. public static bool HasTokenType(this ClaimsPrincipal principal, string type) { - if (principal is null) - { - throw new ArgumentNullException(nameof(principal)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0188), nameof(type)); - } + ArgumentNullException.ThrowIfNull(principal); + ArgumentException.ThrowIfNullOrEmpty(type); return string.Equals(principal.GetTokenType(), type, StringComparison.OrdinalIgnoreCase); } @@ -4118,10 +3546,7 @@ public static class OpenIddictExtensions private static TimeSpan? GetLifetime(ClaimsIdentity identity, string type) { - if (identity is null) - { - throw new ArgumentNullException(nameof(identity)); - } + ArgumentNullException.ThrowIfNull(identity); var value = identity.GetClaim(type); if (string.IsNullOrEmpty(value)) @@ -4139,10 +3564,7 @@ public static class OpenIddictExtensions private static TimeSpan? GetLifetime(ClaimsPrincipal principal, string type) { - if (principal is null) - { - throw new ArgumentNullException(nameof(principal)); - } + ArgumentNullException.ThrowIfNull(principal); var value = principal.GetClaim(type); if (string.IsNullOrEmpty(value)) diff --git a/src/OpenIddict.Abstractions/Primitives/OpenIddictMessage.cs b/src/OpenIddict.Abstractions/Primitives/OpenIddictMessage.cs index aa3cf153..ae9fed9f 100644 --- a/src/OpenIddict.Abstractions/Primitives/OpenIddictMessage.cs +++ b/src/OpenIddict.Abstractions/Primitives/OpenIddictMessage.cs @@ -75,10 +75,7 @@ public class OpenIddictMessage /// Parameters with a null or empty key are always ignored. public OpenIddictMessage(JsonObject parameters) { - if (parameters is null) - { - throw new ArgumentNullException(nameof(parameters)); - } + ArgumentNullException.ThrowIfNull(parameters); foreach (var parameter in parameters) { @@ -106,10 +103,7 @@ public class OpenIddictMessage /// Parameters with a null or empty key are always ignored. public OpenIddictMessage(IEnumerable> parameters) { - if (parameters is null) - { - throw new ArgumentNullException(nameof(parameters)); - } + ArgumentNullException.ThrowIfNull(parameters); foreach (var parameter in parameters) { @@ -130,10 +124,7 @@ public class OpenIddictMessage /// Parameters with a null or empty key are always ignored. public OpenIddictMessage(IEnumerable> parameters) { - if (parameters is null) - { - throw new ArgumentNullException(nameof(parameters)); - } + ArgumentNullException.ThrowIfNull(parameters); foreach (var parameter in parameters.GroupBy(parameter => parameter.Key)) { @@ -163,10 +154,7 @@ public class OpenIddictMessage /// Parameters with a null or empty key are always ignored. public OpenIddictMessage(IEnumerable?>> parameters) { - if (parameters is null) - { - throw new ArgumentNullException(nameof(parameters)); - } + ArgumentNullException.ThrowIfNull(parameters); foreach (var parameter in parameters) { @@ -196,10 +184,7 @@ public class OpenIddictMessage /// Parameters with a null or empty key are always ignored. public OpenIddictMessage(IEnumerable> parameters) { - if (parameters is null) - { - throw new ArgumentNullException(nameof(parameters)); - } + ArgumentNullException.ThrowIfNull(parameters); foreach (var parameter in parameters) { @@ -229,10 +214,7 @@ public class OpenIddictMessage /// Parameters with a null or empty key are always ignored. public OpenIddictMessage(NameValueCollection parameters) { - if (parameters is null) - { - throw new ArgumentNullException(nameof(parameters)); - } + ArgumentNullException.ThrowIfNull(parameters); for (var index = 0; index < parameters.AllKeys.Length; index++) { @@ -286,10 +268,7 @@ public class OpenIddictMessage /// The current instance, which allows chaining calls. public OpenIddictMessage AddParameter(string name, OpenIddictParameter value) { - if (string.IsNullOrEmpty(name)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0190), nameof(name)); - } + ArgumentException.ThrowIfNullOrEmpty(name); if (Parameters.ContainsKey(name)) { @@ -323,10 +302,7 @@ public class OpenIddictMessage /// if the parameter is present, otherwise. public bool HasParameter(string name) { - if (string.IsNullOrEmpty(name)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0190), nameof(name)); - } + ArgumentException.ThrowIfNullOrEmpty(name); return Parameters.ContainsKey(name); } @@ -338,10 +314,7 @@ public class OpenIddictMessage /// The current instance, which allows chaining calls. public OpenIddictMessage RemoveParameter(string name) { - if (string.IsNullOrEmpty(name)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0190), nameof(name)); - } + ArgumentException.ThrowIfNullOrEmpty(name); Parameters.Remove(name); @@ -357,10 +330,7 @@ public class OpenIddictMessage /// The current instance, which allows chaining calls. public OpenIddictMessage SetParameter(string name, OpenIddictParameter? value) { - if (string.IsNullOrEmpty(name)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0190), nameof(name)); - } + ArgumentException.ThrowIfNullOrEmpty(name); // If the parameter value is null or empty, remove the corresponding entry from the collection. if (value is null || OpenIddictParameter.IsNullOrEmpty(value.GetValueOrDefault())) @@ -384,10 +354,7 @@ public class OpenIddictMessage /// if the parameter could be found, otherwise. public bool TryGetParameter(string name, out OpenIddictParameter value) { - if (string.IsNullOrEmpty(name)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0190), nameof(name)); - } + ArgumentException.ThrowIfNullOrEmpty(name); return Parameters.TryGetValue(name, out value); } @@ -445,10 +412,7 @@ public class OpenIddictMessage /// The UTF-8 JSON writer. public void WriteTo(Utf8JsonWriter writer) { - if (writer is null) - { - throw new ArgumentNullException(nameof(writer)); - } + ArgumentNullException.ThrowIfNull(writer); writer.WriteStartObject(); diff --git a/src/OpenIddict.Abstractions/Primitives/OpenIddictParameter.cs b/src/OpenIddict.Abstractions/Primitives/OpenIddictParameter.cs index 35823237..c3a810a8 100644 --- a/src/OpenIddict.Abstractions/Primitives/OpenIddictParameter.cs +++ b/src/OpenIddict.Abstractions/Primitives/OpenIddictParameter.cs @@ -673,10 +673,7 @@ public readonly struct OpenIddictParameter : IEquatable /// if the parameter could be found, otherwise. public bool TryGetNamedParameter(string name, out OpenIddictParameter value) { - if (string.IsNullOrEmpty(name)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0192), nameof(name)); - } + ArgumentException.ThrowIfNullOrEmpty(name); var result = _value switch { @@ -709,10 +706,7 @@ public readonly struct OpenIddictParameter : IEquatable /// if the parameter could be found, otherwise. public bool TryGetUnnamedParameter(int index, out OpenIddictParameter value) { - if (index < 0) - { - throw new ArgumentOutOfRangeException(nameof(index), SR.GetResourceString(SR.ID0193)); - } + ArgumentOutOfRangeException.ThrowIfNegative(index); var result = _value switch { @@ -746,10 +740,7 @@ public readonly struct OpenIddictParameter : IEquatable /// The UTF-8 JSON writer. public void WriteTo(Utf8JsonWriter writer) { - if (writer is null) - { - throw new ArgumentNullException(nameof(writer)); - } + ArgumentNullException.ThrowIfNull(writer); switch (_value) { diff --git a/src/OpenIddict.AspNetCore/OpenIddict.AspNetCore.csproj b/src/OpenIddict.AspNetCore/OpenIddict.AspNetCore.csproj index 8cc45bfb..3e39660c 100644 --- a/src/OpenIddict.AspNetCore/OpenIddict.AspNetCore.csproj +++ b/src/OpenIddict.AspNetCore/OpenIddict.AspNetCore.csproj @@ -4,6 +4,7 @@ $(NetFrameworkTargetFrameworks);$(NetCoreTargetFrameworks) false false + false false diff --git a/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreBuilder.cs b/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreBuilder.cs index e0fe1034..1aa96570 100644 --- a/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreBuilder.cs +++ b/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreBuilder.cs @@ -38,10 +38,7 @@ public sealed class OpenIddictClientAspNetCoreBuilder /// The instance. public OpenIddictClientAspNetCoreBuilder Configure(Action configuration) { - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(configuration); Services.Configure(configuration); @@ -74,10 +71,7 @@ public sealed class OpenIddictClientAspNetCoreBuilder [EditorBrowsable(EditorBrowsableState.Advanced)] public OpenIddictClientAspNetCoreBuilder AddForwardedAuthenticationScheme(string provider, string? caption) { - if (string.IsNullOrEmpty(provider)) - { - throw new ArgumentException(SR.FormatID0366(nameof(provider)), nameof(provider)); - } + ArgumentException.ThrowIfNullOrEmpty(provider); return Configure(options => options.ForwardedAuthenticationSchemes.Add( new AuthenticationScheme(provider, caption, typeof(OpenIddictClientAspNetCoreForwarder)))); diff --git a/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreConfiguration.cs b/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreConfiguration.cs index 5ea75824..cf699322 100644 --- a/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreConfiguration.cs +++ b/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreConfiguration.cs @@ -31,10 +31,7 @@ public sealed class OpenIddictClientAspNetCoreConfiguration : IConfigureOptions< /// public void Configure(AuthenticationOptions options) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); // If a handler was already registered and the type doesn't correspond to the OpenIddict handler, throw an exception. if (options.SchemeMap.TryGetValue(OpenIddictClientAspNetCoreDefaults.AuthenticationScheme, out var builder) && @@ -64,10 +61,7 @@ public sealed class OpenIddictClientAspNetCoreConfiguration : IConfigureOptions< /// public void PostConfigure(string? name, OpenIddictClientAspNetCoreOptions options) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); if (!options.DisableAutomaticAuthenticationSchemeForwarding) { @@ -102,10 +96,7 @@ public sealed class OpenIddictClientAspNetCoreConfiguration : IConfigureOptions< /// public void Configure(OpenIddictClientOptions options) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); // Register the built-in event handlers used by the OpenIddict ASP.NET Core client components. options.Handlers.AddRange(OpenIddictClientAspNetCoreHandlers.DefaultHandlers); @@ -114,10 +105,7 @@ public sealed class OpenIddictClientAspNetCoreConfiguration : IConfigureOptions< /// public void PostConfigure(string? name, AuthenticationOptions options) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); if (!TryValidate(options.SchemeMap, options.DefaultAuthenticateScheme) || !TryValidate(options.SchemeMap, options.DefaultScheme) || diff --git a/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreExtensions.cs b/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreExtensions.cs index 83b3fa35..cac9e143 100644 --- a/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreExtensions.cs +++ b/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreExtensions.cs @@ -24,10 +24,7 @@ public static class OpenIddictClientAspNetCoreExtensions /// The instance. public static OpenIddictClientAspNetCoreBuilder UseAspNetCore(this OpenIddictClientBuilder builder) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } + ArgumentNullException.ThrowIfNull(builder); builder.Services.AddAuthentication(); @@ -73,15 +70,9 @@ public static class OpenIddictClientAspNetCoreExtensions public static OpenIddictClientBuilder UseAspNetCore( this OpenIddictClientBuilder builder, Action configuration) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } - - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(builder); + + ArgumentNullException.ThrowIfNull(configuration); configuration(builder.UseAspNetCore()); diff --git a/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreHandler.cs b/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreHandler.cs index 0f50f2d0..e433b4d4 100644 --- a/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreHandler.cs +++ b/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreHandler.cs @@ -29,7 +29,7 @@ public sealed class OpenIddictClientAspNetCoreHandler : AuthenticationHandler /// Creates a new instance of the class. /// -#if SUPPORTS_TIME_PROVIDER +#if SUPPORTS_AUTHENTICATION_HANDLER_TIME_PROVIDER_ARGUMENT public OpenIddictClientAspNetCoreHandler( IOpenIddictClientDispatcher dispatcher, IOpenIddictClientFactory factory, diff --git a/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreHandlerFilters.cs b/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreHandlerFilters.cs index 4b7da143..57aad07f 100644 --- a/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreHandlerFilters.cs +++ b/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreHandlerFilters.cs @@ -29,10 +29,7 @@ public static class OpenIddictClientAspNetCoreHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(_options.CurrentValue.EnableErrorPassthrough); } @@ -46,10 +43,7 @@ public static class OpenIddictClientAspNetCoreHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.Transaction.GetHttpRequest() is not null); } @@ -69,10 +63,7 @@ public static class OpenIddictClientAspNetCoreHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(_options.CurrentValue.EnablePostLogoutRedirectionEndpointPassthrough); } @@ -92,10 +83,7 @@ public static class OpenIddictClientAspNetCoreHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(_options.CurrentValue.EnableRedirectionEndpointPassthrough); } @@ -114,10 +102,7 @@ public static class OpenIddictClientAspNetCoreHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(_options.CurrentValue.EnableStatusCodePagesIntegration); } @@ -136,10 +121,7 @@ public static class OpenIddictClientAspNetCoreHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!_options.CurrentValue.DisableTransportSecurityRequirement); } diff --git a/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreHandlers.Authentication.cs b/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreHandlers.Authentication.cs index afd8be11..a071ebf7 100644 --- a/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreHandlers.Authentication.cs +++ b/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreHandlers.Authentication.cs @@ -61,10 +61,7 @@ public static partial class OpenIddictClientAspNetCoreHandlers /// public ValueTask HandleAsync(ApplyAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to ASP.NET Core requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. diff --git a/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreHandlers.Session.cs b/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreHandlers.Session.cs index 673a205f..855f7e4b 100644 --- a/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreHandlers.Session.cs +++ b/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreHandlers.Session.cs @@ -61,10 +61,7 @@ public static partial class OpenIddictClientAspNetCoreHandlers /// public ValueTask HandleAsync(ApplyEndSessionRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to ASP.NET Core requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. diff --git a/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreHandlers.cs b/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreHandlers.cs index 0c99cef6..82f213d3 100644 --- a/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreHandlers.cs +++ b/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreHandlers.cs @@ -95,10 +95,7 @@ public static partial class OpenIddictClientAspNetCoreHandlers /// public ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to ASP.NET Core requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. @@ -149,10 +146,7 @@ public static partial class OpenIddictClientAspNetCoreHandlers /// public ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to ASP.NET Core requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. @@ -194,10 +188,7 @@ public static partial class OpenIddictClientAspNetCoreHandlers /// public ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to ASP.NET Core requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. @@ -239,10 +230,7 @@ public static partial class OpenIddictClientAspNetCoreHandlers /// public async ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to ASP.NET Core requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. @@ -319,10 +307,7 @@ public static partial class OpenIddictClientAspNetCoreHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.GrantType is GrantTypes.DeviceCode) { @@ -353,10 +338,7 @@ public static partial class OpenIddictClientAspNetCoreHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (!string.IsNullOrEmpty(context.Nonce)) { @@ -394,10 +376,7 @@ public static partial class OpenIddictClientAspNetCoreHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.StateTokenPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -511,10 +490,7 @@ public static partial class OpenIddictClientAspNetCoreHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.GrantType is GrantTypes.DeviceCode) { @@ -546,10 +522,7 @@ public static partial class OpenIddictClientAspNetCoreHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to ASP.NET Core requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. @@ -656,10 +629,7 @@ public static partial class OpenIddictClientAspNetCoreHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to ASP.NET Core requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. @@ -695,10 +665,7 @@ public static partial class OpenIddictClientAspNetCoreHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If an explicit response type was specified, don't overwrite it. if (!string.IsNullOrEmpty(context.ResponseMode)) @@ -795,10 +762,7 @@ public static partial class OpenIddictClientAspNetCoreHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: using a correlation cookie serves as an injection/antiforgery protection as the request // will always be rejected if a cookie corresponding to the request forgery protection claim @@ -881,10 +845,7 @@ public static partial class OpenIddictClientAspNetCoreHandlers /// public ValueTask HandleAsync(ProcessSignOutContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to ASP.NET Core requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. @@ -981,10 +942,7 @@ public static partial class OpenIddictClientAspNetCoreHandlers /// public ValueTask HandleAsync(ProcessSignOutContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to ASP.NET Core requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. @@ -1027,10 +985,7 @@ public static partial class OpenIddictClientAspNetCoreHandlers /// public ValueTask HandleAsync(ProcessSignOutContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: using a correlation cookie serves as an injection/antiforgery protection as the request // will always be rejected if a cookie corresponding to the request forgery protection claim @@ -1115,10 +1070,7 @@ public static partial class OpenIddictClientAspNetCoreHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.SkipRequest(); @@ -1146,10 +1098,7 @@ public static partial class OpenIddictClientAspNetCoreHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to ASP.NET Core requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. @@ -1189,10 +1138,7 @@ public static partial class OpenIddictClientAspNetCoreHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to ASP.NET Core requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. @@ -1233,10 +1179,7 @@ public static partial class OpenIddictClientAspNetCoreHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Transaction.Response is not null, SR.GetResourceString(SR.ID4007)); @@ -1273,10 +1216,7 @@ public static partial class OpenIddictClientAspNetCoreHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to ASP.NET Core requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. @@ -1329,10 +1269,7 @@ public static partial class OpenIddictClientAspNetCoreHandlers /// public async ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to ASP.NET Core requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. @@ -1403,10 +1340,7 @@ public static partial class OpenIddictClientAspNetCoreHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.Logger.LogInformation(6145, SR.GetResourceString(SR.ID6145)); context.HandleRequest(); diff --git a/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreHelpers.cs b/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreHelpers.cs index ee856cf0..1806322d 100644 --- a/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreHelpers.cs +++ b/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreHelpers.cs @@ -21,10 +21,7 @@ public static class OpenIddictClientAspNetCoreHelpers /// The instance or if it couldn't be found. public static HttpRequest? GetHttpRequest(this OpenIddictClientTransaction transaction) { - if (transaction is null) - { - throw new ArgumentNullException(nameof(transaction)); - } + ArgumentNullException.ThrowIfNull(transaction); if (!transaction.Properties.TryGetValue(typeof(HttpRequest).FullName!, out object? property)) { @@ -46,10 +43,7 @@ public static class OpenIddictClientAspNetCoreHelpers /// The . public static OpenIddictClientEndpointType GetOpenIddictClientEndpointType(this HttpContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return context.Features.Get()?.Transaction?.EndpointType ?? default; } @@ -61,10 +55,7 @@ public static class OpenIddictClientAspNetCoreHelpers /// The instance or if it couldn't be found. public static OpenIddictRequest? GetOpenIddictClientRequest(this HttpContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return context.Features.Get()?.Transaction?.Request; } @@ -76,10 +67,7 @@ public static class OpenIddictClientAspNetCoreHelpers /// The instance or if it couldn't be found. public static OpenIddictResponse? GetOpenIddictClientResponse(this HttpContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return context.Features.Get()?.Transaction?.Response; } diff --git a/src/OpenIddict.Client.DataProtection/OpenIddictClientDataProtectionBuilder.cs b/src/OpenIddict.Client.DataProtection/OpenIddictClientDataProtectionBuilder.cs index f4002367..aaef1ed1 100644 --- a/src/OpenIddict.Client.DataProtection/OpenIddictClientDataProtectionBuilder.cs +++ b/src/OpenIddict.Client.DataProtection/OpenIddictClientDataProtectionBuilder.cs @@ -37,10 +37,7 @@ public sealed class OpenIddictClientDataProtectionBuilder /// The instance. public OpenIddictClientDataProtectionBuilder Configure(Action configuration) { - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(configuration); Services.Configure(configuration); @@ -55,10 +52,7 @@ public sealed class OpenIddictClientDataProtectionBuilder /// The instance. public OpenIddictClientDataProtectionBuilder UseDataProtectionProvider(IDataProtectionProvider provider) { - if (provider is null) - { - throw new ArgumentNullException(nameof(provider)); - } + ArgumentNullException.ThrowIfNull(provider); return Configure(options => options.DataProtectionProvider = provider); } @@ -70,10 +64,7 @@ public sealed class OpenIddictClientDataProtectionBuilder /// The instance. public OpenIddictClientDataProtectionBuilder UseFormatter(IOpenIddictClientDataProtectionFormatter formatter) { - if (formatter is null) - { - throw new ArgumentNullException(nameof(formatter)); - } + ArgumentNullException.ThrowIfNull(formatter); return Configure(options => options.Formatter = formatter); } diff --git a/src/OpenIddict.Client.DataProtection/OpenIddictClientDataProtectionConfiguration.cs b/src/OpenIddict.Client.DataProtection/OpenIddictClientDataProtectionConfiguration.cs index 2da06dd5..14b6dd7b 100644 --- a/src/OpenIddict.Client.DataProtection/OpenIddictClientDataProtectionConfiguration.cs +++ b/src/OpenIddict.Client.DataProtection/OpenIddictClientDataProtectionConfiguration.cs @@ -29,10 +29,7 @@ public sealed class OpenIddictClientDataProtectionConfiguration : IConfigureOpti /// public void Configure(OpenIddictClientOptions options) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); // Register the built-in event handlers used by the OpenIddict Data Protection server components. options.Handlers.AddRange(OpenIddictClientDataProtectionHandlers.DefaultHandlers); @@ -41,10 +38,7 @@ public sealed class OpenIddictClientDataProtectionConfiguration : IConfigureOpti /// public void PostConfigure(string? name, OpenIddictClientDataProtectionOptions options) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); options.DataProtectionProvider ??= _dataProtectionProvider; } diff --git a/src/OpenIddict.Client.DataProtection/OpenIddictClientDataProtectionExtensions.cs b/src/OpenIddict.Client.DataProtection/OpenIddictClientDataProtectionExtensions.cs index 2caac624..9703b04d 100644 --- a/src/OpenIddict.Client.DataProtection/OpenIddictClientDataProtectionExtensions.cs +++ b/src/OpenIddict.Client.DataProtection/OpenIddictClientDataProtectionExtensions.cs @@ -25,10 +25,7 @@ public static class OpenIddictClientDataProtectionExtensions /// The instance. public static OpenIddictClientDataProtectionBuilder UseDataProtection(this OpenIddictClientBuilder builder) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } + ArgumentNullException.ThrowIfNull(builder); builder.Services.AddDataProtection(); @@ -60,15 +57,8 @@ public static class OpenIddictClientDataProtectionExtensions public static OpenIddictClientBuilder UseDataProtection( this OpenIddictClientBuilder builder, Action configuration) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } - - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(builder); + ArgumentNullException.ThrowIfNull(configuration); configuration(builder.UseDataProtection()); diff --git a/src/OpenIddict.Client.DataProtection/OpenIddictClientDataProtectionFormatter.cs b/src/OpenIddict.Client.DataProtection/OpenIddictClientDataProtectionFormatter.cs index 8b593bcb..b5f72af2 100644 --- a/src/OpenIddict.Client.DataProtection/OpenIddictClientDataProtectionFormatter.cs +++ b/src/OpenIddict.Client.DataProtection/OpenIddictClientDataProtectionFormatter.cs @@ -20,10 +20,7 @@ public sealed class OpenIddictClientDataProtectionFormatter : IOpenIddictClientD { public ClaimsPrincipal ReadToken(BinaryReader reader) { - if (reader is null) - { - throw new ArgumentNullException(nameof(reader)); - } + ArgumentNullException.ThrowIfNull(reader); var (principal, properties) = Read(reader); @@ -182,15 +179,8 @@ public sealed class OpenIddictClientDataProtectionFormatter : IOpenIddictClientD public void WriteToken(BinaryWriter writer, ClaimsPrincipal principal) { - if (writer is null) - { - throw new ArgumentNullException(nameof(writer)); - } - - if (principal is null) - { - throw new ArgumentNullException(nameof(principal)); - } + ArgumentNullException.ThrowIfNull(writer); + ArgumentNullException.ThrowIfNull(principal); var properties = new Dictionary(); @@ -293,15 +283,8 @@ public sealed class OpenIddictClientDataProtectionFormatter : IOpenIddictClientD static void WriteClaim(BinaryWriter writer, Claim claim) { - if (writer is null) - { - throw new ArgumentNullException(nameof(writer)); - } - - if (claim is null) - { - throw new ArgumentNullException(nameof(claim)); - } + ArgumentNullException.ThrowIfNull(writer); + ArgumentNullException.ThrowIfNull(claim); WriteWithDefault(writer, claim.Type, claim.Subject?.NameClaimType ?? ClaimsIdentity.DefaultNameClaimType); writer.Write(claim.Value); diff --git a/src/OpenIddict.Client.DataProtection/OpenIddictClientDataProtectionHandlerFilters.cs b/src/OpenIddict.Client.DataProtection/OpenIddictClientDataProtectionHandlerFilters.cs index eb965363..ba04e380 100644 --- a/src/OpenIddict.Client.DataProtection/OpenIddictClientDataProtectionHandlerFilters.cs +++ b/src/OpenIddict.Client.DataProtection/OpenIddictClientDataProtectionHandlerFilters.cs @@ -20,10 +20,7 @@ public static class OpenIddictClientDataProtectionHandlerFilters /// public ValueTask IsActiveAsync(GenerateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.TokenFormat is TokenFormats.Private.DataProtection); } diff --git a/src/OpenIddict.Client.DataProtection/OpenIddictClientDataProtectionHandlers.Protection.cs b/src/OpenIddict.Client.DataProtection/OpenIddictClientDataProtectionHandlers.Protection.cs index 5d36c92c..4b8975ec 100644 --- a/src/OpenIddict.Client.DataProtection/OpenIddictClientDataProtectionHandlers.Protection.cs +++ b/src/OpenIddict.Client.DataProtection/OpenIddictClientDataProtectionHandlers.Protection.cs @@ -176,10 +176,7 @@ public static partial class OpenIddictClientDataProtectionHandlers /// public ValueTask HandleAsync(GenerateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // ASP.NET Core Data Protection can be used to format certain types of tokens in lieu // of the default token format (typically, JSON Web Token). By default, Data Protection @@ -223,10 +220,7 @@ public static partial class OpenIddictClientDataProtectionHandlers /// public ValueTask HandleAsync(GenerateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If an access token was already attached by another handler, don't overwrite it. if (!string.IsNullOrEmpty(context.Token)) diff --git a/src/OpenIddict.Client.Owin/OpenIddictClientOwinBuilder.cs b/src/OpenIddict.Client.Owin/OpenIddictClientOwinBuilder.cs index f75d35bb..cfedce9c 100644 --- a/src/OpenIddict.Client.Owin/OpenIddictClientOwinBuilder.cs +++ b/src/OpenIddict.Client.Owin/OpenIddictClientOwinBuilder.cs @@ -74,10 +74,7 @@ public sealed class OpenIddictClientOwinBuilder [EditorBrowsable(EditorBrowsableState.Advanced)] public OpenIddictClientOwinBuilder AddForwardedAuthenticationType(string provider, string? caption) { - if (string.IsNullOrEmpty(provider)) - { - throw new ArgumentException(SR.FormatID0366(nameof(provider)), nameof(provider)); - } + ArgumentException.ThrowIfNullOrEmpty(provider); return Configure(options => options.ForwardedAuthenticationTypes.Add(new AuthenticationDescription { diff --git a/src/OpenIddict.Client.SystemIntegration/OpenIddict.Client.SystemIntegration.csproj b/src/OpenIddict.Client.SystemIntegration/OpenIddict.Client.SystemIntegration.csproj index 14f25394..7c8cc5ca 100644 --- a/src/OpenIddict.Client.SystemIntegration/OpenIddict.Client.SystemIntegration.csproj +++ b/src/OpenIddict.Client.SystemIntegration/OpenIddict.Client.SystemIntegration.csproj @@ -30,6 +30,7 @@ allow loading dependencies that are not strong-named), the warning can be safely disabled. --> $(NoWarn);CS8002 + $(DefineConstants);FeatureRuntimeInformation;FeatureValueTuple diff --git a/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationActivation.cs b/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationActivation.cs index 9d2de71e..d20484f8 100644 --- a/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationActivation.cs +++ b/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationActivation.cs @@ -21,10 +21,7 @@ public sealed class OpenIddictClientSystemIntegrationActivation /// is . public OpenIddictClientSystemIntegrationActivation(Uri uri) { - if (uri is null) - { - throw new ArgumentNullException(nameof(uri)); - } + ArgumentNullException.ThrowIfNull(uri); if (!uri.IsAbsoluteUri || OpenIddictHelpers.IsImplicitFileUri(uri)) { diff --git a/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationBuilder.cs b/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationBuilder.cs index 866b0742..948c59b4 100644 --- a/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationBuilder.cs +++ b/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationBuilder.cs @@ -40,10 +40,7 @@ public sealed class OpenIddictClientSystemIntegrationBuilder /// The . public OpenIddictClientSystemIntegrationBuilder Configure(Action configuration) { - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(configuration); Services.Configure(configuration); @@ -209,10 +206,7 @@ public sealed class OpenIddictClientSystemIntegrationBuilder [EditorBrowsable(EditorBrowsableState.Advanced)] public OpenIddictClientSystemIntegrationBuilder SetApplicationDiscriminator(string discriminator) { - if (string.IsNullOrEmpty(discriminator)) - { - throw new ArgumentException(SR.FormatID0366(nameof(discriminator)), nameof(discriminator)); - } + ArgumentException.ThrowIfNullOrEmpty(discriminator); return Configure(options => options.ApplicationDiscriminator = discriminator); } @@ -226,10 +220,7 @@ public sealed class OpenIddictClientSystemIntegrationBuilder [EditorBrowsable(EditorBrowsableState.Advanced)] public OpenIddictClientSystemIntegrationBuilder SetInstanceIdentifier(string identifier) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.FormatID0366(nameof(identifier)), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); return Configure(options => options.InstanceIdentifier = identifier); } @@ -243,10 +234,7 @@ public sealed class OpenIddictClientSystemIntegrationBuilder [EditorBrowsable(EditorBrowsableState.Advanced)] public OpenIddictClientSystemIntegrationBuilder SetPipeName(string name) { - if (string.IsNullOrEmpty(name)) - { - throw new ArgumentException(SR.FormatID0366(nameof(name)), nameof(name)); - } + ArgumentException.ThrowIfNullOrEmpty(name); return Configure(options => options.PipeName = name); } @@ -270,10 +258,7 @@ public sealed class OpenIddictClientSystemIntegrationBuilder [EditorBrowsable(EditorBrowsableState.Advanced), SupportedOSPlatform("windows")] public OpenIddictClientSystemIntegrationBuilder SetPipeSecurity(PipeSecurity security) { - if (security is null) - { - throw new ArgumentNullException(nameof(security)); - } + ArgumentNullException.ThrowIfNull(security); return Configure(options => options.PipeSecurity = security); } diff --git a/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationConfiguration.cs b/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationConfiguration.cs index 336f7393..ed274d2d 100644 --- a/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationConfiguration.cs +++ b/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationConfiguration.cs @@ -7,7 +7,6 @@ using System.ComponentModel; using System.IO.Pipes; using System.Net; -using System.Runtime.InteropServices; using System.Security.AccessControl; using System.Security.Principal; using System.Text; @@ -38,10 +37,7 @@ public sealed class OpenIddictClientSystemIntegrationConfiguration : IConfigureO /// public void Configure(OpenIddictClientOptions options) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); // Register the built-in event handlers used by the OpenIddict client system integration components. options.Handlers.AddRange(OpenIddictClientSystemIntegrationHandlers.DefaultHandlers); @@ -53,10 +49,7 @@ public sealed class OpenIddictClientSystemIntegrationConfiguration : IConfigureO /// public void PostConfigure(string? name, OpenIddictClientOptions options) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); // If no explicit client URI was set, default to the static "http://localhost/" address, which is // adequate for a native/mobile client and points to the embedded web server when it is enabled. @@ -66,18 +59,15 @@ public sealed class OpenIddictClientSystemIntegrationConfiguration : IConfigureO /// public void PostConfigure(string? name, OpenIddictClientSystemIntegrationOptions options) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); - // Ensure the operating system is supported. - if (!RuntimeInformation.IsOSPlatform(OSPlatform.Create("android")) && - !RuntimeInformation.IsOSPlatform(OSPlatform.Create("ios")) && - !RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && - !RuntimeInformation.IsOSPlatform(OSPlatform.Create("maccatalyst")) && - !RuntimeInformation.IsOSPlatform(OSPlatform.OSX) && - !RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + // Ensure the operating system version is supported. + if ((OperatingSystem.IsAndroid() && !OperatingSystem.IsAndroidVersionAtLeast(21)) || + (OperatingSystem.IsIOS() && !OperatingSystem.IsIOSVersionAtLeast(12)) || + OperatingSystem.IsLinux() || + (OperatingSystem.IsMacCatalyst() && !OperatingSystem.IsMacCatalystVersionAtLeast(13, 1)) || + (OperatingSystem.IsMacOS() && !OperatingSystem.IsMacOSVersionAtLeast(10, 15)) || + (OperatingSystem.IsWindows() && !OperatingSystem.IsWindowsVersionAtLeast(7))) { throw new PlatformNotSupportedException(SR.GetResourceString(SR.ID0389)); } @@ -87,37 +77,19 @@ public sealed class OpenIddictClientSystemIntegrationConfiguration : IConfigureO // is used to prevent the generic/non-OS specific TFM from being used as launching the system // browser cannot be done using Process.Start() and requires using OS-specific APIs that are // not available on the portable version of the OpenIddict.Client.SystemIntegration package. - if (RuntimeInformation.IsOSPlatform(OSPlatform.Create("android"))) + if (OperatingSystem.IsAndroid()) { throw new PlatformNotSupportedException(SR.GetResourceString(SR.ID0449)); } #endif #if !SUPPORTS_UIKIT - if (RuntimeInformation.IsOSPlatform(OSPlatform.Create("ios")) || - RuntimeInformation.IsOSPlatform(OSPlatform.Create("maccatalyst"))) + if (OperatingSystem.IsIOS() || OperatingSystem.IsMacCatalyst()) { throw new PlatformNotSupportedException(SR.GetResourceString(SR.ID0449)); } #endif -#if SUPPORTS_OPERATING_SYSTEM_VERSIONS_COMPARISON - // Ensure the operating system version is supported. - if ((OperatingSystem.IsAndroid() && !OperatingSystem.IsAndroidVersionAtLeast(21)) || - (OperatingSystem.IsIOS() && !OperatingSystem.IsIOSVersionAtLeast(12)) || - (OperatingSystem.IsMacCatalyst() && !OperatingSystem.IsMacCatalystVersionAtLeast(13, 1)) || - (OperatingSystem.IsMacOS() && !OperatingSystem.IsMacOSVersionAtLeast(10, 15)) || - (OperatingSystem.IsWindows() && !OperatingSystem.IsWindowsVersionAtLeast(7))) - { - throw new PlatformNotSupportedException(SR.GetResourceString(SR.ID0389)); - } -#else - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !IsWindowsVersionAtLeast(7)) - { - throw new PlatformNotSupportedException(SR.GetResourceString(SR.ID0389)); - } -#endif - #pragma warning disable CA1416 // If explicitly set, ensure the specified authentication mode is supported. if (options.AuthenticationMode is ASWebAuthenticationSession && !IsASWebAuthenticationSessionSupported()) @@ -141,10 +113,8 @@ public sealed class OpenIddictClientSystemIntegrationConfiguration : IConfigureO IsASWebAuthenticationSessionSupported() ? ASWebAuthenticationSession : IsCustomTabsIntentSupported() ? CustomTabsIntent : SystemBrowser; - if (!RuntimeInformation.IsOSPlatform(OSPlatform.Create("android")) && - !RuntimeInformation.IsOSPlatform(OSPlatform.Create("ios")) && - !RuntimeInformation.IsOSPlatform(OSPlatform.Create("maccatalyst")) && - !RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + if (!OperatingSystem.IsAndroid() && !OperatingSystem.IsIOS() && + !OperatingSystem.IsMacCatalyst() && !OperatingSystem.IsMacOS()) { options.EnableActivationHandling ??= true; options.EnableActivationRedirection ??= true; @@ -189,13 +159,13 @@ public sealed class OpenIddictClientSystemIntegrationConfiguration : IConfigureO // Note: on Windows, the name is deliberately prefixed with "LOCAL\" to support // partial trust/sandboxed applications that are executed in an AppContainer // and cannot communicate with applications outside the sandbox container. - options.PipeName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? + options.PipeName = OperatingSystem.IsWindows() ? @$"LOCAL\{options.ApplicationDiscriminator}" : options.ApplicationDiscriminator; } #if SUPPORTS_CURRENT_USER_ONLY_PIPE_OPTION - if (options.PipeOptions is null && !RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + if (options.PipeOptions is null && !OperatingSystem.IsWindows()) { // Note: the CurrentUserOnly option is also supported on Windows, but is less // flexible than using a PipeSecurity object (e.g cross-process communication @@ -214,11 +184,11 @@ public sealed class OpenIddictClientSystemIntegrationConfiguration : IConfigureO // between elevated and non-elevated processes. Note: if the process executes // inside an AppContainer, don't override the default OS pipe security policy // to allow all applications with the same identity to access the named pipe. - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && options.PipeSecurity is null) + if (OperatingSystem.IsWindows() && options.PipeSecurity is null) { using var identity = WindowsIdentity.GetCurrent(TokenAccessLevels.Query); - if (!IsWindowsVersionAtLeast(10, 0, 10240) || !HasAppContainerToken(identity)) + if (!OperatingSystem.IsWindowsVersionAtLeast(10, 0, 10240) || !HasAppContainerToken(identity)) { options.PipeSecurity = new PipeSecurity(); options.PipeSecurity.SetOwner(identity.User!); diff --git a/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationExtensions.cs b/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationExtensions.cs index bb83a7fe..31f6d5c1 100644 --- a/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationExtensions.cs +++ b/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationExtensions.cs @@ -4,7 +4,6 @@ * the license and the contributors participating to this project. */ -using System.Runtime.InteropServices; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Options; @@ -26,18 +25,15 @@ public static class OpenIddictClientSystemIntegrationExtensions /// The . public static OpenIddictClientSystemIntegrationBuilder UseSystemIntegration(this OpenIddictClientBuilder builder) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } + ArgumentNullException.ThrowIfNull(builder); - // Ensure the operating system is supported. - if (!RuntimeInformation.IsOSPlatform(OSPlatform.Create("android")) && - !RuntimeInformation.IsOSPlatform(OSPlatform.Create("ios")) && - !RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && - !RuntimeInformation.IsOSPlatform(OSPlatform.Create("maccatalyst")) && - !RuntimeInformation.IsOSPlatform(OSPlatform.OSX) && - !RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + // Ensure the operating system version is supported. + if ((OperatingSystem.IsAndroid() && !OperatingSystem.IsAndroidVersionAtLeast(21)) || + (OperatingSystem.IsIOS() && !OperatingSystem.IsIOSVersionAtLeast(12)) || + OperatingSystem.IsLinux() || + (OperatingSystem.IsMacCatalyst() && !OperatingSystem.IsMacCatalystVersionAtLeast(13, 1)) || + (OperatingSystem.IsMacOS() && !OperatingSystem.IsMacOSVersionAtLeast(10, 15)) || + (OperatingSystem.IsWindows() && !OperatingSystem.IsWindowsVersionAtLeast(7))) { throw new PlatformNotSupportedException(SR.GetResourceString(SR.ID0389)); } @@ -47,37 +43,19 @@ public static class OpenIddictClientSystemIntegrationExtensions // is used to prevent the generic/non-OS specific TFM from being used as launching the system // browser cannot be done using Process.Start() and requires using OS-specific APIs that are // not available on the portable version of the OpenIddict.Client.SystemIntegration package. - if (RuntimeInformation.IsOSPlatform(OSPlatform.Create("android"))) + if (OperatingSystem.IsAndroid()) { throw new PlatformNotSupportedException(SR.GetResourceString(SR.ID0449)); } #endif #if !SUPPORTS_UIKIT - if (RuntimeInformation.IsOSPlatform(OSPlatform.Create("ios")) || - RuntimeInformation.IsOSPlatform(OSPlatform.Create("maccatalyst"))) + if (OperatingSystem.IsIOS() || OperatingSystem.IsMacCatalyst()) { throw new PlatformNotSupportedException(SR.GetResourceString(SR.ID0449)); } #endif -#if SUPPORTS_OPERATING_SYSTEM_VERSIONS_COMPARISON - // Ensure the operating system version is supported. - if ((OperatingSystem.IsAndroid() && !OperatingSystem.IsAndroidVersionAtLeast(21)) || - (OperatingSystem.IsIOS() && !OperatingSystem.IsIOSVersionAtLeast(12)) || - (OperatingSystem.IsMacCatalyst() && !OperatingSystem.IsMacCatalystVersionAtLeast(13, 1)) || - (OperatingSystem.IsMacOS() && !OperatingSystem.IsMacOSVersionAtLeast(10, 15)) || - (OperatingSystem.IsWindows() && !OperatingSystem.IsWindowsVersionAtLeast(7))) - { - throw new PlatformNotSupportedException(SR.GetResourceString(SR.ID0389)); - } -#else - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !IsWindowsVersionAtLeast(7)) - { - throw new PlatformNotSupportedException(SR.GetResourceString(SR.ID0389)); - } -#endif - // Note: the OpenIddict activation handler service is deliberately registered as early as possible to // ensure protocol activations can be handled before another service can stop the initialization of the // application (e.g Dapplo.Microsoft.Extensions.Hosting.AppServices relies on an IHostedService to implement @@ -142,15 +120,8 @@ public static class OpenIddictClientSystemIntegrationExtensions public static OpenIddictClientBuilder UseSystemIntegration( this OpenIddictClientBuilder builder, Action configuration) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } - - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(builder); + ArgumentNullException.ThrowIfNull(configuration); configuration(builder.UseSystemIntegration()); diff --git a/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHandlerFilters.cs b/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHandlerFilters.cs index 8af45ee5..f7475691 100644 --- a/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHandlerFilters.cs +++ b/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHandlerFilters.cs @@ -30,10 +30,7 @@ public static class OpenIddictClientSystemIntegrationHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); #if SUPPORTS_AUTHENTICATION_SERVICES if (IsASWebAuthenticationSessionSupported()) @@ -61,10 +58,7 @@ public static class OpenIddictClientSystemIntegrationHandlerFilters /// public ValueTask IsActiveAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!string.IsNullOrEmpty(context.Nonce)); } @@ -83,10 +77,7 @@ public static class OpenIddictClientSystemIntegrationHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); #if SUPPORTS_ANDROID && SUPPORTS_ANDROIDX_BROWSER if (IsCustomTabsIntentSupported()) @@ -118,10 +109,7 @@ public static class OpenIddictClientSystemIntegrationHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(_options.CurrentValue.EnableEmbeddedWebServer is true); } @@ -135,10 +123,7 @@ public static class OpenIddictClientSystemIntegrationHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.Transaction.GetHttpListenerContext() is not null); } @@ -152,10 +137,7 @@ public static class OpenIddictClientSystemIntegrationHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(Environment.UserInteractive); } @@ -170,10 +152,7 @@ public static class OpenIddictClientSystemIntegrationHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.Transaction.GetPlatformCallback() is not null); } @@ -187,10 +166,7 @@ public static class OpenIddictClientSystemIntegrationHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.Transaction.GetProtocolActivation() is not null); } @@ -210,10 +186,7 @@ public static class OpenIddictClientSystemIntegrationHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (!context.Transaction.Properties.TryGetValue( typeof(OpenIddictClientSystemIntegrationAuthenticationMode).FullName!, out var result) || @@ -240,10 +213,7 @@ public static class OpenIddictClientSystemIntegrationHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); #if SUPPORTS_WINDOWS_RUNTIME if (IsWebAuthenticationBrokerSupported()) diff --git a/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHandlers.Authentication.cs b/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHandlers.Authentication.cs index 2fda605a..7f322196 100644 --- a/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHandlers.Authentication.cs +++ b/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHandlers.Authentication.cs @@ -5,7 +5,6 @@ */ using System.Collections.Immutable; -using System.Runtime.InteropServices; using System.Runtime.Versioning; using System.Text; using Microsoft.Extensions.Primitives; @@ -99,10 +98,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers public async ValueTask HandleAsync(ApplyAuthorizationRequestContext context) #pragma warning restore CS1998 { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); #if SUPPORTS_AUTHENTICATION_SERVICES && SUPPORTS_FOUNDATION if (string.IsNullOrEmpty(context.RedirectUri) || @@ -322,10 +318,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers public async ValueTask HandleAsync(ApplyAuthorizationRequestContext context) #pragma warning restore CS1998 { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); #if SUPPORTS_ANDROID && SUPPORTS_ANDROIDX_BROWSER if (string.IsNullOrEmpty(context.RedirectUri)) @@ -398,10 +391,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers public async ValueTask HandleAsync(ApplyAuthorizationRequestContext context) #pragma warning restore CS1998 { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); #if SUPPORTS_WINDOWS_RUNTIME if (string.IsNullOrEmpty(context.RedirectUri)) @@ -543,10 +533,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public async ValueTask HandleAsync(ApplyAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var uri = OpenIddictHelpers.AddQueryStringParameters( uri: new Uri(context.AuthorizationEndpoint, UriKind.Absolute), @@ -554,7 +541,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers static parameter => parameter.Key, static parameter => (StringValues) parameter.Value)); - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + if (OperatingSystem.IsWindows()) { // Note: on Windows, multiple application models exist and must be supported to cover most scenarios: // @@ -612,13 +599,13 @@ public static partial class OpenIddictClientSystemIntegrationHandlers return; } #endif - if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) && await TryLaunchBrowserWithOpenAsync(uri)) + if (OperatingSystem.IsMacOS() && await TryLaunchBrowserWithOpenAsync(uri)) { context.HandleRequest(); return; } - if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && await TryLaunchBrowserWithXdgOpenAsync(uri)) + if (OperatingSystem.IsLinux() && await TryLaunchBrowserWithXdgOpenAsync(uri)) { context.HandleRequest(); return; @@ -648,10 +635,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public async ValueTask HandleAsync(ApplyRedirectionResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to HTTP listener requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. @@ -671,11 +655,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers _ => "Authentication failed. Please return to the application." }); -#if SUPPORTS_STREAM_MEMORY_METHODS await response.OutputStream.WriteAsync(buffer); -#else - await response.OutputStream.WriteAsync(buffer, 0, buffer.Length); -#endif await response.OutputStream.FlushAsync(); context.HandleRequest(); diff --git a/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHandlers.Session.cs b/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHandlers.Session.cs index 1a00d5ac..85ca7b75 100644 --- a/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHandlers.Session.cs +++ b/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHandlers.Session.cs @@ -5,7 +5,6 @@ */ using System.Collections.Immutable; -using System.Runtime.InteropServices; using System.Runtime.Versioning; using System.Text; using Microsoft.Extensions.Primitives; @@ -99,10 +98,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers public async ValueTask HandleAsync(ApplyEndSessionRequestContext context) #pragma warning restore CS1998 { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); #if SUPPORTS_AUTHENTICATION_SERVICES && SUPPORTS_FOUNDATION if (string.IsNullOrEmpty(context.PostLogoutRedirectUri) || @@ -322,10 +318,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers public async ValueTask HandleAsync(ApplyEndSessionRequestContext context) #pragma warning restore CS1998 { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); #if SUPPORTS_ANDROID && SUPPORTS_ANDROIDX_BROWSER if (string.IsNullOrEmpty(context.PostLogoutRedirectUri)) @@ -398,10 +391,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers public async ValueTask HandleAsync(ApplyEndSessionRequestContext context) #pragma warning restore CS1998 { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); #if SUPPORTS_WINDOWS_RUNTIME if (string.IsNullOrEmpty(context.PostLogoutRedirectUri)) @@ -543,10 +533,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public async ValueTask HandleAsync(ApplyEndSessionRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var uri = OpenIddictHelpers.AddQueryStringParameters( uri: new Uri(context.EndSessionEndpoint, UriKind.Absolute), @@ -554,7 +541,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers static parameter => parameter.Key, static parameter => (StringValues) parameter.Value)); - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + if (OperatingSystem.IsWindows()) { // Note: on Windows, multiple application models exist and must be supported to cover most scenarios: // @@ -612,13 +599,13 @@ public static partial class OpenIddictClientSystemIntegrationHandlers return; } #endif - if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) && await TryLaunchBrowserWithOpenAsync(uri)) + if (OperatingSystem.IsMacOS() && await TryLaunchBrowserWithOpenAsync(uri)) { context.HandleRequest(); return; } - if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && await TryLaunchBrowserWithXdgOpenAsync(uri)) + if (OperatingSystem.IsLinux() && await TryLaunchBrowserWithXdgOpenAsync(uri)) { context.HandleRequest(); return; @@ -648,10 +635,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public async ValueTask HandleAsync(ApplyPostLogoutRedirectionResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to HTTP listener requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. @@ -671,11 +655,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers _ => "Logout failed. Please return to the application." }); -#if SUPPORTS_STREAM_MEMORY_METHODS await response.OutputStream.WriteAsync(buffer); -#else - await response.OutputStream.WriteAsync(buffer, 0, buffer.Length); -#endif await response.OutputStream.FlushAsync(); context.HandleRequest(); diff --git a/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHandlers.cs b/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHandlers.cs index 12a6f172..16b5df0d 100644 --- a/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHandlers.cs +++ b/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHandlers.cs @@ -108,10 +108,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // When using the OpenIddict client system integration, requests can originate from multiple sources: // @@ -177,10 +174,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); (context.BaseUri, context.RequestUri) = context.Transaction.GetProtocolActivation() switch { @@ -215,10 +209,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); (context.BaseUri, context.RequestUri) = context.Transaction.GetPlatformCallback() switch { @@ -254,10 +245,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If the base or request URIs couldn't be resolved, don't try to infer the endpoint type. if (context is not { BaseUri.IsAbsoluteUri: true, RequestUri.IsAbsoluteUri: true }) @@ -332,10 +320,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to HTTP listener requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. @@ -377,10 +362,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to HTTP listener requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. @@ -430,10 +412,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public async ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to HTTP listener requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. @@ -520,10 +499,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.Transaction.GetProtocolActivation() is not { ActivationUri: Uri uri }) { @@ -578,10 +554,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.Transaction.GetPlatformCallback() is not OpenIddictClientSystemIntegrationPlatformCallback callback) { @@ -624,10 +597,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public async ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.Nonce), SR.GetResourceString(SR.ID4019)); @@ -721,10 +691,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.Nonce), SR.GetResourceString(SR.ID4019)); @@ -767,10 +734,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.Nonce), SR.GetResourceString(SR.ID4019)); @@ -813,10 +777,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.Nonce), SR.GetResourceString(SR.ID4019)); @@ -860,10 +821,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.Nonce), SR.GetResourceString(SR.ID4019)); @@ -918,10 +876,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public async ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.StateTokenPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -1002,10 +957,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.Nonce), SR.GetResourceString(SR.ID4019)); @@ -1053,10 +1005,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.Nonce), SR.GetResourceString(SR.ID4019)); @@ -1101,10 +1050,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.Nonce), SR.GetResourceString(SR.ID4019)); @@ -1148,10 +1094,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.Nonce), SR.GetResourceString(SR.ID4019)); @@ -1195,10 +1138,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.Nonce), SR.GetResourceString(SR.ID4019)); @@ -1242,10 +1182,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.Nonce), SR.GetResourceString(SR.ID4019)); @@ -1288,10 +1225,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.Nonce), SR.GetResourceString(SR.ID4019)); @@ -1336,10 +1270,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.Nonce), SR.GetResourceString(SR.ID4019)); @@ -1383,10 +1314,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.Nonce), SR.GetResourceString(SR.ID4019)); @@ -1430,10 +1358,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.Nonce), SR.GetResourceString(SR.ID4019)); @@ -1477,10 +1402,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.Nonce), SR.GetResourceString(SR.ID4019)); @@ -1522,10 +1444,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.Nonce), SR.GetResourceString(SR.ID4019)); @@ -1569,10 +1488,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.Nonce), SR.GetResourceString(SR.ID4019)); @@ -1611,10 +1527,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.Nonce), SR.GetResourceString(SR.ID4019)); @@ -1649,10 +1562,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.BaseUri ??= context.Options.ClientUri; @@ -1692,10 +1602,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public async ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If the redirect_uri uses a loopback host/IP as the authority and doesn't include a non-default port, // determine whether the embedded web server is running: if so, override the port in the redirect_uri @@ -1747,10 +1654,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public async ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If an explicit response type was specified, don't overwrite it. if (!string.IsNullOrEmpty(context.ResponseMode)) @@ -1931,10 +1835,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.StateTokenPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -1976,10 +1877,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (string.IsNullOrEmpty(context.Nonce)) { @@ -2020,10 +1918,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public ValueTask HandleAsync(ProcessSignOutContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.BaseUri ??= context.Options.ClientUri; @@ -2062,10 +1957,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public async ValueTask HandleAsync(ProcessSignOutContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If the post_logout_redirect_uri uses a loopback host/IP as the authority and doesn't include a non-default port, // determine whether the embedded web server is running: if so, override the port in the post_logout_redirect_uri @@ -2113,10 +2005,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public ValueTask HandleAsync(ProcessSignOutContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.StateTokenPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -2158,10 +2047,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public ValueTask HandleAsync(ProcessSignOutContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (string.IsNullOrEmpty(context.Nonce)) { @@ -2211,10 +2097,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public ValueTask HandleAsync(ProcessErrorContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Try to resolve the authentication context from the transaction, if available. var notification = context.Transaction.GetProperty( @@ -2262,10 +2145,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to HTTP listener requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. @@ -2305,10 +2185,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to HTTP listener requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. @@ -2345,10 +2222,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // For both protocol activations (initial or redirected) and web-view-like results, // no proper response can be generated and eventually displayed to the user. In this @@ -2381,10 +2255,7 @@ public static partial class OpenIddictClientSystemIntegrationHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // For both protocol activations (initial or redirected) and web-view-like results, // no proper response can be generated and eventually displayed to the user. In this diff --git a/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHelpers.cs b/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHelpers.cs index d0ee6c13..9ff291fe 100644 --- a/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHelpers.cs +++ b/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHelpers.cs @@ -68,38 +68,6 @@ public static class OpenIddictClientSystemIntegrationHelpers public static HttpListenerContext? GetHttpListenerContext(this OpenIddictClientTransaction transaction) => transaction.GetProperty(typeof(HttpListenerContext).FullName!); - /// - /// Determines whether the current Windows version - /// is greater than or equals to the specified version. - /// - /// - /// if the current Windows version is greater than - /// or equals to the specified version, otherwise. - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - [SupportedOSPlatformGuard("windows")] - internal static bool IsWindowsVersionAtLeast(int major, int minor = 0, int build = 0, int revision = 0) - { -#if SUPPORTS_OPERATING_SYSTEM_VERSIONS_COMPARISON - return OperatingSystem.IsWindowsVersionAtLeast(major, minor, build, revision); -#else - if (Environment.OSVersion.Platform is PlatformID.Win32NT && - Environment.OSVersion.Version >= new Version(major, minor, build, revision)) - { - return true; - } - - // Note: on older versions of .NET, Environment.OSVersion.Version is known to be affected by - // the compatibility shims used by Windows 10+ when the application doesn't have a manifest - // that explicitly indicates it's compatible with Windows 10 and higher. To avoid that, a - // second pass using RuntimeInformation.OSDescription (that calls NtDll.RtlGetVersion() under - // the hood) is made. Note: no version is returned on UWP due to the missing Win32 API. - return RuntimeInformation.OSDescription.StartsWith("Microsoft Windows ", StringComparison.OrdinalIgnoreCase) && - RuntimeInformation.OSDescription["Microsoft Windows ".Length..] is string value && - Version.TryParse(value, out Version? version) && version >= new Version(major, minor, build, revision); -#endif - } - /// /// Determines whether the ASWebAuthenticationSession API is supported on this platform. /// @@ -144,7 +112,7 @@ public static class OpenIddictClientSystemIntegrationHelpers // oldest supported version in the package, it is also used for the runtime check. internal static bool IsWindowsRuntimeSupported() #if SUPPORTS_WINDOWS_RUNTIME - => IsWindowsVersionAtLeast(10, 0, 17763); + => OperatingSystem.IsWindowsVersionAtLeast(10, 0, 17763); #else => false; #endif @@ -227,10 +195,7 @@ public static class OpenIddictClientSystemIntegrationHelpers [SupportedOSPlatform("windows10.0.10240")] internal static unsafe bool HasAppContainerToken(WindowsIdentity identity) { - if (identity is null) - { - throw new ArgumentNullException(nameof(identity)); - } + ArgumentNullException.ThrowIfNull(identity); int* buffer = stackalloc int[1]; diff --git a/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHttpListener.cs b/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHttpListener.cs index 2a1b40d9..0bde64a6 100644 --- a/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHttpListener.cs +++ b/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHttpListener.cs @@ -8,7 +8,6 @@ using System.ComponentModel; using System.Globalization; using System.Net; using System.Net.Sockets; -using System.Runtime.InteropServices; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; @@ -146,7 +145,7 @@ public sealed class OpenIddictClientSystemIntegrationHttpListener : BackgroundSe // non-Windows operating systems) and doesn't require running the application // as an administrator or adding a namespace reservation/ACL rule on Windows. - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + if (OperatingSystem.IsWindows()) { // On Windows 10 1511 and higher, listening on 127.0.0.1 and ::1 is preferred // to localhost as it allows ignoring requests that are sent by other machines @@ -154,7 +153,7 @@ public sealed class OpenIddictClientSystemIntegrationHttpListener : BackgroundSe // configured to reject such requests) without requiring administrator rights. // // See https://www.rfc-editor.org/rfc/rfc8252#section-8.3 for more information. - if (IsWindowsVersionAtLeast(10, 0, 10586)) + if (OperatingSystem.IsWindowsVersionAtLeast(10, 0, 10586)) { if (Socket.OSSupportsIPv4) { diff --git a/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationPipeListener.cs b/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationPipeListener.cs index 6cc07603..3f4c23b5 100644 --- a/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationPipeListener.cs +++ b/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationPipeListener.cs @@ -6,7 +6,6 @@ using System.ComponentModel; using System.IO.Pipes; -using System.Runtime.InteropServices; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; @@ -131,7 +130,7 @@ public sealed class OpenIddictClientSystemIntegrationPipeListener : BackgroundSe static NamedPipeServerStream CreatePipeServerStream(OpenIddictClientSystemIntegrationOptions options) // Note: the ACL-based PipeSecurity class is only supported on Windows. On other operating systems, // PipeOptions.CurrentUserOnly can be used as an alternative, but only for TFMs that implement it. - => RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? + => OperatingSystem.IsWindows() ? #if SUPPORTS_NAMED_PIPE_CONSTRUCTOR_WITH_ACL new NamedPipeServerStream( #elif SUPPORTS_NAMED_PIPE_STATIC_FACTORY_WITH_ACL diff --git a/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationPlatformCallback.cs b/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationPlatformCallback.cs index 215ea1e0..ed35a309 100644 --- a/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationPlatformCallback.cs +++ b/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationPlatformCallback.cs @@ -25,20 +25,14 @@ public sealed class OpenIddictClientSystemIntegrationPlatformCallback public OpenIddictClientSystemIntegrationPlatformCallback( Uri uri, IReadOnlyDictionary parameters) { - if (uri is null) - { - throw new ArgumentNullException(nameof(uri)); - } + ArgumentNullException.ThrowIfNull(uri); if (!uri.IsAbsoluteUri || OpenIddictHelpers.IsImplicitFileUri(uri)) { throw new ArgumentException(SR.GetResourceString(SR.ID0144), nameof(uri)); } - if (parameters is null) - { - throw new ArgumentNullException(nameof(parameters)); - } + ArgumentNullException.ThrowIfNull(parameters); CallbackUri = uri; Parameters = parameters.ToImmutableDictionary(); diff --git a/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationService.cs b/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationService.cs index 84a8d027..288f6cdd 100644 --- a/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationService.cs +++ b/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationService.cs @@ -53,10 +53,7 @@ public sealed class OpenIddictClientSystemIntegrationService [EditorBrowsable(EditorBrowsableState.Advanced)] public Task HandleCustomTabsIntentAsync(Intent intent, CancellationToken cancellationToken = default) { - if (intent is null) - { - throw new ArgumentNullException(nameof(intent)); - } + ArgumentNullException.ThrowIfNull(intent); if (intent.Data is null) { @@ -139,10 +136,7 @@ public sealed class OpenIddictClientSystemIntegrationService /// is . private async Task HandleRequestAsync(TProperty property, CancellationToken cancellationToken) where TProperty : class { - if (property is null) - { - throw new ArgumentNullException(nameof(property)); - } + ArgumentNullException.ThrowIfNull(property); cancellationToken.ThrowIfCancellationRequested(); @@ -188,15 +182,8 @@ public sealed class OpenIddictClientSystemIntegrationService OpenIddictClientSystemIntegrationActivation activation, string identifier, CancellationToken cancellationToken = default) { - if (activation is null) - { - throw new ArgumentNullException(nameof(activation)); - } - - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.FormatID0366(nameof(identifier)), nameof(identifier)); - } + ArgumentNullException.ThrowIfNull(activation); + ArgumentException.ThrowIfNullOrEmpty(identifier); using var buffer = new MemoryStream(); using var writer = new BinaryWriter(buffer); diff --git a/src/OpenIddict.Client.SystemNetHttp/OpenIddict.Client.SystemNetHttp.csproj b/src/OpenIddict.Client.SystemNetHttp/OpenIddict.Client.SystemNetHttp.csproj index 968b12e7..ea267002 100644 --- a/src/OpenIddict.Client.SystemNetHttp/OpenIddict.Client.SystemNetHttp.csproj +++ b/src/OpenIddict.Client.SystemNetHttp/OpenIddict.Client.SystemNetHttp.csproj @@ -6,6 +6,7 @@ $(NetCoreTargetFrameworks); $(NetStandardTargetFrameworks) + $(DefineConstants);FeatureRuntimeInformation diff --git a/src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpBuilder.cs b/src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpBuilder.cs index 8aa39b44..0def36f1 100644 --- a/src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpBuilder.cs +++ b/src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpBuilder.cs @@ -42,10 +42,7 @@ public sealed class OpenIddictClientSystemNetHttpBuilder /// The instance. public OpenIddictClientSystemNetHttpBuilder Configure(Action configuration) { - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(configuration); Services.Configure(configuration); @@ -63,10 +60,7 @@ public sealed class OpenIddictClientSystemNetHttpBuilder [EditorBrowsable(EditorBrowsableState.Advanced)] public OpenIddictClientSystemNetHttpBuilder ConfigureHttpClient(Action configuration) { - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(configuration); return ConfigureHttpClient((registration, client) => configuration(client)); } @@ -84,15 +78,8 @@ public sealed class OpenIddictClientSystemNetHttpBuilder [EditorBrowsable(EditorBrowsableState.Advanced)] public OpenIddictClientSystemNetHttpBuilder ConfigureHttpClient(string provider, Action configuration) { - if (string.IsNullOrEmpty(provider)) - { - throw new ArgumentException(SR.FormatID0366(nameof(provider)), nameof(provider)); - } - - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(configuration); + ArgumentException.ThrowIfNullOrEmpty(provider); return ConfigureHttpClient((registration, client) => { @@ -115,10 +102,7 @@ public sealed class OpenIddictClientSystemNetHttpBuilder [EditorBrowsable(EditorBrowsableState.Advanced)] public OpenIddictClientSystemNetHttpBuilder ConfigureHttpClient(Action configuration) { - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(configuration); return Configure(options => options.HttpClientActions.Add(configuration)); } @@ -134,10 +118,7 @@ public sealed class OpenIddictClientSystemNetHttpBuilder [EditorBrowsable(EditorBrowsableState.Advanced)] public OpenIddictClientSystemNetHttpBuilder ConfigureHttpClientHandler(Action configuration) { - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(configuration); return ConfigureHttpClientHandler((registration, handler) => configuration(handler)); } @@ -155,15 +136,8 @@ public sealed class OpenIddictClientSystemNetHttpBuilder [EditorBrowsable(EditorBrowsableState.Advanced)] public OpenIddictClientSystemNetHttpBuilder ConfigureHttpClientHandler(string provider, Action configuration) { - if (string.IsNullOrEmpty(provider)) - { - throw new ArgumentException(SR.FormatID0366(nameof(provider)), nameof(provider)); - } - - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(configuration); + ArgumentException.ThrowIfNullOrEmpty(provider); return ConfigureHttpClientHandler((registration, handler) => { @@ -187,10 +161,7 @@ public sealed class OpenIddictClientSystemNetHttpBuilder public OpenIddictClientSystemNetHttpBuilder ConfigureHttpClientHandler( Action configuration) { - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(configuration); return Configure(options => options.HttpClientHandlerActions.Add(configuration)); } @@ -203,10 +174,7 @@ public sealed class OpenIddictClientSystemNetHttpBuilder /// The instance. public OpenIddictClientSystemNetHttpBuilder SetContactAddress(MailAddress address) { - if (address is null) - { - throw new ArgumentNullException(nameof(address)); - } + ArgumentNullException.ThrowIfNull(address); return Configure(options => options.ContactAddress = address); } @@ -219,10 +187,7 @@ public sealed class OpenIddictClientSystemNetHttpBuilder /// The instance. public OpenIddictClientSystemNetHttpBuilder SetContactAddress(string address) { - if (string.IsNullOrEmpty(address)) - { - throw new ArgumentException(SR.FormatID0366(nameof(address)), nameof(address)); - } + ArgumentException.ThrowIfNullOrEmpty(address); return SetContactAddress(new MailAddress(address)); } @@ -234,10 +199,7 @@ public sealed class OpenIddictClientSystemNetHttpBuilder /// The instance. public OpenIddictClientSystemNetHttpBuilder SetHttpErrorPolicy(IAsyncPolicy policy) { - if (policy is null) - { - throw new ArgumentNullException(nameof(policy)); - } + ArgumentNullException.ThrowIfNull(policy); return Configure(options => options.HttpErrorPolicy = policy); } @@ -257,10 +219,7 @@ public sealed class OpenIddictClientSystemNetHttpBuilder public OpenIddictClientSystemNetHttpBuilder SetHttpResiliencePipeline( Action> configuration) { - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(configuration); var builder = new ResiliencePipelineBuilder(); configuration(builder); @@ -279,10 +238,7 @@ public sealed class OpenIddictClientSystemNetHttpBuilder /// The instance. public OpenIddictClientSystemNetHttpBuilder SetHttpResiliencePipeline(ResiliencePipeline pipeline) { - if (pipeline is null) - { - throw new ArgumentNullException(nameof(pipeline)); - } + ArgumentNullException.ThrowIfNull(pipeline); return Configure(options => options.HttpResiliencePipeline = pipeline); } @@ -296,10 +252,7 @@ public sealed class OpenIddictClientSystemNetHttpBuilder /// The instance. public OpenIddictClientSystemNetHttpBuilder SetProductInformation(ProductInfoHeaderValue information) { - if (information is null) - { - throw new ArgumentNullException(nameof(information)); - } + ArgumentNullException.ThrowIfNull(information); return Configure(options => options.ProductInformation = information); } @@ -313,10 +266,7 @@ public sealed class OpenIddictClientSystemNetHttpBuilder /// The instance. public OpenIddictClientSystemNetHttpBuilder SetProductInformation(string name, string? version) { - if (string.IsNullOrEmpty(name)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0345), nameof(name)); - } + ArgumentException.ThrowIfNullOrEmpty(name); return SetProductInformation(new ProductInfoHeaderValue(name, version)); } @@ -330,10 +280,7 @@ public sealed class OpenIddictClientSystemNetHttpBuilder /// The instance. public OpenIddictClientSystemNetHttpBuilder SetProductInformation(Assembly assembly) { - if (assembly is null) - { - throw new ArgumentNullException(nameof(assembly)); - } + ArgumentNullException.ThrowIfNull(assembly); return SetProductInformation(new ProductInfoHeaderValue( productName: assembly.GetName().Name!, @@ -356,10 +303,7 @@ public sealed class OpenIddictClientSystemNetHttpBuilder public OpenIddictClientSystemNetHttpBuilder SetSelfSignedTlsClientAuthenticationCertificateSelector( Func selector) { - if (selector is null) - { - throw new ArgumentNullException(nameof(selector)); - } + ArgumentNullException.ThrowIfNull(selector); return Configure(options => options.SelfSignedTlsClientAuthenticationCertificateSelector = selector); } @@ -380,10 +324,7 @@ public sealed class OpenIddictClientSystemNetHttpBuilder public OpenIddictClientSystemNetHttpBuilder SetTlsClientAuthenticationCertificateSelector( Func selector) { - if (selector is null) - { - throw new ArgumentNullException(nameof(selector)); - } + ArgumentNullException.ThrowIfNull(selector); return Configure(options => options.TlsClientAuthenticationCertificateSelector = selector); } diff --git a/src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpConfiguration.cs b/src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpConfiguration.cs index be289c8b..fb84d7da 100644 --- a/src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpConfiguration.cs +++ b/src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpConfiguration.cs @@ -42,10 +42,7 @@ public sealed class OpenIddictClientSystemNetHttpConfiguration : IConfigureOptio /// public void Configure(OpenIddictClientOptions options) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); // Register the built-in event handlers used by the OpenIddict System.Net.Http client components. options.Handlers.AddRange(OpenIddictClientSystemNetHttpHandlers.DefaultHandlers); @@ -62,10 +59,7 @@ public sealed class OpenIddictClientSystemNetHttpConfiguration : IConfigureOptio /// public void Configure(string? name, HttpClientFactoryOptions options) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); var assembly = typeof(OpenIddictClientSystemNetHttpOptions).Assembly.GetName(); @@ -179,10 +173,7 @@ public sealed class OpenIddictClientSystemNetHttpConfiguration : IConfigureOptio /// public void PostConfigure(string? name, HttpClientFactoryOptions options) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); var assembly = typeof(OpenIddictClientSystemNetHttpOptions).Assembly.GetName(); @@ -270,10 +261,7 @@ public sealed class OpenIddictClientSystemNetHttpConfiguration : IConfigureOptio /// public void PostConfigure(string? name, OpenIddictClientSystemNetHttpOptions options) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); // If no client authentication certificate selector was provided, use fallback delegates that // automatically use the first X.509 signing certificate attached to the client registration diff --git a/src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpExtensions.cs b/src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpExtensions.cs index 2273ba1a..9cbf374e 100644 --- a/src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpExtensions.cs +++ b/src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpExtensions.cs @@ -25,10 +25,7 @@ public static class OpenIddictClientSystemNetHttpExtensions /// The instance. public static OpenIddictClientSystemNetHttpBuilder UseSystemNetHttp(this OpenIddictClientBuilder builder) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } + ArgumentNullException.ThrowIfNull(builder); builder.Services.AddHttpClient(); @@ -65,15 +62,8 @@ public static class OpenIddictClientSystemNetHttpExtensions public static OpenIddictClientBuilder UseSystemNetHttp( this OpenIddictClientBuilder builder, Action configuration) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } - - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(builder); + ArgumentNullException.ThrowIfNull(configuration); configuration(builder.UseSystemNetHttp()); diff --git a/src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpHandlerFilters.cs b/src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpHandlerFilters.cs index 5b610d8b..bd74edec 100644 --- a/src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpHandlerFilters.cs +++ b/src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpHandlerFilters.cs @@ -19,10 +19,7 @@ public static class OpenIddictClientSystemNetHttpHandlerFilters /// public ValueTask IsActiveAsync(BaseExternalContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new( string.Equals(context.RemoteUri?.Scheme, Uri.UriSchemeHttp, StringComparison.OrdinalIgnoreCase) || diff --git a/src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpHandlers.Userinfo.cs b/src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpHandlers.Userinfo.cs index c94a3502..79a210b7 100644 --- a/src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpHandlers.Userinfo.cs +++ b/src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpHandlers.Userinfo.cs @@ -62,10 +62,7 @@ public static partial class OpenIddictClientSystemNetHttpHandlers /// public ValueTask HandleAsync(PrepareUserInfoRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Request is not null, SR.GetResourceString(SR.ID4008)); @@ -103,10 +100,7 @@ public static partial class OpenIddictClientSystemNetHttpHandlers /// public async ValueTask HandleAsync(ExtractUserInfoResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Don't overwrite the response if one was already provided. if (context.Response is not null || !string.IsNullOrEmpty(context.UserInfoToken)) diff --git a/src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpHandlers.cs b/src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpHandlers.cs index b15f3aae..058cb87d 100644 --- a/src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpHandlers.cs +++ b/src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpHandlers.cs @@ -10,7 +10,6 @@ using System.Diagnostics; using System.IO.Compression; using System.Net.Http; using System.Net.Http.Headers; -using System.Runtime.InteropServices; using System.Text; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; @@ -82,10 +81,7 @@ public static partial class OpenIddictClientSystemNetHttpHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If an explicit client authentication method was attached, don't overwrite it. if (!string.IsNullOrEmpty(context.TokenEndpointClientAuthenticationMethod)) @@ -204,10 +200,7 @@ public static partial class OpenIddictClientSystemNetHttpHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Unlike DPoP, the mTLS specification doesn't use a specific token type to represent // certificate-bound tokens. As such, most implementations (e.g Keycloak) simply return @@ -269,10 +262,7 @@ public static partial class OpenIddictClientSystemNetHttpHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If an explicit client authentication method was attached, don't overwrite it. if (!string.IsNullOrEmpty(context.DeviceAuthorizationEndpointClientAuthenticationMethod)) @@ -399,10 +389,7 @@ public static partial class OpenIddictClientSystemNetHttpHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If an explicit client authentication method was attached, don't overwrite it. if (!string.IsNullOrEmpty(context.PushedAuthorizationEndpointClientAuthenticationMethod)) @@ -530,10 +517,7 @@ public static partial class OpenIddictClientSystemNetHttpHandlers /// public ValueTask HandleAsync(ProcessIntrospectionContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If an explicit client authentication method was attached, don't overwrite it. if (!string.IsNullOrEmpty(context.IntrospectionEndpointClientAuthenticationMethod)) @@ -652,10 +636,7 @@ public static partial class OpenIddictClientSystemNetHttpHandlers /// public ValueTask HandleAsync(ProcessRevocationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If an explicit client authentication method was attached, don't overwrite it. if (!string.IsNullOrEmpty(context.RevocationEndpointClientAuthenticationMethod)) @@ -772,10 +753,7 @@ public static partial class OpenIddictClientSystemNetHttpHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: HttpClientFactory doesn't support flowing a list of properties that can be // accessed from the HttpClientAction or HttpMessageHandlerBuilderAction delegates @@ -856,10 +834,7 @@ public static partial class OpenIddictClientSystemNetHttpHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Store the HttpRequestMessage in the transaction properties. context.Transaction.SetProperty(typeof(HttpRequestMessage).FullName!, @@ -888,10 +863,7 @@ public static partial class OpenIddictClientSystemNetHttpHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Store the HttpRequestMessage in the transaction properties. context.Transaction.SetProperty(typeof(HttpRequestMessage).FullName!, @@ -920,10 +892,7 @@ public static partial class OpenIddictClientSystemNetHttpHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); #if SUPPORTS_HTTP_CLIENT_DEFAULT_REQUEST_VERSION || SUPPORTS_HTTP_CLIENT_DEFAULT_REQUEST_VERSION_POLICY // This handler only applies to System.Net.Http requests. If the HTTP request cannot be resolved, @@ -968,10 +937,7 @@ public static partial class OpenIddictClientSystemNetHttpHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to System.Net.Http requests. If the HTTP request cannot be resolved, // this may indicate that the request was incorrectly processed by another client stack. @@ -1013,10 +979,7 @@ public static partial class OpenIddictClientSystemNetHttpHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to System.Net.Http requests. If the HTTP request cannot be resolved, // this may indicate that the request was incorrectly processed by another client stack. @@ -1067,10 +1030,7 @@ public static partial class OpenIddictClientSystemNetHttpHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to System.Net.Http requests. If the HTTP request cannot be resolved, // this may indicate that the request was incorrectly processed by another client stack. @@ -1103,10 +1063,7 @@ public static partial class OpenIddictClientSystemNetHttpHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Transaction.Request is not null, SR.GetResourceString(SR.ID4008)); @@ -1160,10 +1117,7 @@ public static partial class OpenIddictClientSystemNetHttpHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Transaction.Request is not null, SR.GetResourceString(SR.ID4008)); @@ -1220,10 +1174,7 @@ public static partial class OpenIddictClientSystemNetHttpHandlers /// public async ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to System.Net.Http requests. If the HTTP request cannot be resolved, // this may indicate that the request was incorrectly processed by another client stack. @@ -1284,10 +1235,7 @@ public static partial class OpenIddictClientSystemNetHttpHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to System.Net.Http requests. If the HTTP request cannot be resolved, // this may indicate that the request was incorrectly processed by another client stack. @@ -1322,10 +1270,7 @@ public static partial class OpenIddictClientSystemNetHttpHandlers /// public async ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: automatic content decompression can be enabled by constructing an HttpClient wrapping // a generic HttpClientHandler, a SocketsHttpHandler or a WinHttpHandler instance with the @@ -1367,7 +1312,7 @@ public static partial class OpenIddictClientSystemNetHttpHandlers // Content-Encoding header from the response, which leads to incorrect results when trying // to decompress the content a second time. To avoid that, the entire logic used in this // handler is ignored on iOS if the native HTTP handler (NSUrlSessionHandler) is used. - if (RuntimeInformation.IsOSPlatform(OSPlatform.Create("ios")) && + if (OperatingSystem.IsIOS() && AppContext.TryGetSwitch("System.Net.Http.UseNativeHttpHandler", out bool value) && value) { return; @@ -1468,10 +1413,7 @@ public static partial class OpenIddictClientSystemNetHttpHandlers /// public async ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Don't overwrite the response if one was already provided. if (context.Transaction.Response is not null) @@ -1547,10 +1489,7 @@ public static partial class OpenIddictClientSystemNetHttpHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Don't overwrite the response if one was already provided. if (context.Transaction.Response is not null) @@ -1676,10 +1615,7 @@ public static partial class OpenIddictClientSystemNetHttpHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Don't overwrite the response if one was already provided. if (context.Transaction.Response is not null) @@ -1722,10 +1658,7 @@ public static partial class OpenIddictClientSystemNetHttpHandlers /// public async ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to System.Net.Http requests. If the HTTP response cannot be resolved, // this may indicate that the request was incorrectly processed by another client stack. @@ -1792,10 +1725,7 @@ public static partial class OpenIddictClientSystemNetHttpHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to System.Net.Http requests. If the HTTP response cannot be resolved, // this may indicate that the request was incorrectly processed by another client stack. diff --git a/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationBuilder.cs b/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationBuilder.cs index cea98c36..406fd217 100644 --- a/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationBuilder.cs +++ b/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationBuilder.cs @@ -35,10 +35,7 @@ public sealed partial class OpenIddictClientWebIntegrationBuilder /// The instance. public OpenIddictClientWebIntegrationBuilder Configure(Action configuration) { - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(configuration); Services.Configure(configuration); diff --git a/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationConfiguration.cs b/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationConfiguration.cs index b1bc3e0c..3e0e462b 100644 --- a/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationConfiguration.cs +++ b/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationConfiguration.cs @@ -23,10 +23,7 @@ public sealed partial class OpenIddictClientWebIntegrationConfiguration : IConfi /// public void Configure(OpenIddictClientOptions options) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); // Register the built-in event handlers used by the OpenIddict client Web components. options.Handlers.AddRange(OpenIddictClientWebIntegrationHandlers.DefaultHandlers); @@ -35,10 +32,7 @@ public sealed partial class OpenIddictClientWebIntegrationConfiguration : IConfi /// public void PostConfigure(string? name, OpenIddictClientOptions options) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); options.Registrations.ForEach(static registration => { @@ -54,10 +48,7 @@ public sealed partial class OpenIddictClientWebIntegrationConfiguration : IConfi /// public void PostConfigure(string? name, OpenIddictClientSystemNetHttpOptions options) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); // Override the default/user-defined selectors to support attaching TLS client // certificates that don't meet the requirements enforced by default by OpenIddict. diff --git a/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationExtensions.cs b/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationExtensions.cs index 1ad21802..a79f6f2e 100644 --- a/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationExtensions.cs +++ b/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationExtensions.cs @@ -25,10 +25,7 @@ public static partial class OpenIddictClientWebIntegrationExtensions /// The instance. public static OpenIddictClientWebIntegrationBuilder UseWebProviders(this OpenIddictClientBuilder builder) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } + ArgumentNullException.ThrowIfNull(builder); // Register the System.Net.Http integration. builder.UseSystemNetHttp(); @@ -68,15 +65,8 @@ public static partial class OpenIddictClientWebIntegrationExtensions public static OpenIddictClientBuilder UseWebProviders( this OpenIddictClientBuilder builder, Action configuration) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } - - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(builder); + ArgumentNullException.ThrowIfNull(configuration); configuration(builder.UseWebProviders()); diff --git a/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.Authentication.cs b/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.Authentication.cs index f86eba65..8d5c9a57 100644 --- a/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.Authentication.cs +++ b/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.Authentication.cs @@ -40,10 +40,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(PrepareAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Some providers implement old drafts of the OAuth 2.0 specification that didn't support // the "response_type" parameter but relied on a "type" parameter to determine the type diff --git a/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.Device.cs b/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.Device.cs index 2433c1f3..742c2d5c 100644 --- a/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.Device.cs +++ b/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.Device.cs @@ -41,10 +41,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(ExtractDeviceAuthorizationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.Response is null) { diff --git a/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.Discovery.cs b/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.Discovery.cs index 3c6c05c4..d85628e1 100644 --- a/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.Discovery.cs +++ b/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.Discovery.cs @@ -45,10 +45,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.Response[Metadata.Issuer] = context.Registration.ProviderType switch { @@ -97,10 +94,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: some providers don't list the grant types they support, which prevents the OpenIddict // client from using them (unless they are assumed to be enabled by default, like the @@ -182,10 +176,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Some providers support Proof Key for Code Exchange but don't list any supported code // challenge method in the server configuration metadata. To ensure the OpenIddict client @@ -227,10 +218,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Atlassian includes the "openid" scope in its server configuration but doesn't currently allow // requesting it. To prevent an error from being returned, OpenID Connect support is disabled. @@ -280,10 +268,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Alibaba Cloud doesn't document whether sending client credentials using basic authentication // is supported and doesn't return a "token_endpoint_auth_methods_supported" nor a @@ -394,10 +379,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // While Atlassian implements an OpenID Connect userinfo endpoint, using it requires // requesting the "openid" scope, which isn't allowed yet. To work around this diff --git a/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.Exchange.cs b/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.Exchange.cs index af53b926..0abf1b5e 100644 --- a/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.Exchange.cs +++ b/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.Exchange.cs @@ -60,10 +60,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(PrepareTokenRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Amazon doesn't support the standard "urn:ietf:params:oauth:grant-type:device_code" // grant type and requires using the non-standard "device_code" grant type instead. @@ -138,10 +135,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(PrepareTokenRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Some providers are known to incorrectly implement basic authentication support, either because // an incorrect encoding scheme is used (e.g the credentials are not formURL-encoded as required @@ -199,10 +193,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(PrepareTokenRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to System.Net.Http requests. If the HTTP request cannot be resolved, // this may indicate that the request was incorrectly processed by another client stack. @@ -251,10 +242,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(PrepareTokenRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to System.Net.Http requests. If the HTTP request cannot be resolved, // this may indicate that the request was incorrectly processed by another client stack. @@ -312,10 +300,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(PrepareTokenRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Transaction.Request is not null, SR.GetResourceString(SR.ID4008)); @@ -362,10 +347,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(ExtractTokenResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.Response is null) { diff --git a/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.Introspection.cs b/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.Introspection.cs index 7ab01f8c..f15f8c66 100644 --- a/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.Introspection.cs +++ b/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.Introspection.cs @@ -41,10 +41,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(ExtractIntrospectionResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.Response is null) { diff --git a/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.Protection.cs b/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.Protection.cs index 64957ba1..2f9bac60 100644 --- a/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.Protection.cs +++ b/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.Protection.cs @@ -45,10 +45,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: the client registration may be null (e.g when validating a state token). // In this case, don't amend the default token validation parameters. @@ -103,10 +100,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(GenerateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: the client registration may be null (e.g when generating a state token). // In this case, don't amend the default security token descriptor. diff --git a/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.Revocation.cs b/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.Revocation.cs index da40d51c..c56cea79 100644 --- a/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.Revocation.cs +++ b/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.Revocation.cs @@ -55,10 +55,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(PrepareRevocationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Transaction.Request is not null, SR.GetResourceString(SR.ID4008)); @@ -106,10 +103,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(PrepareRevocationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // These providers don't support the standard "token" parameter and // require using the non-standard "access_token" parameter instead. @@ -167,10 +161,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(PrepareRevocationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to System.Net.Http requests. If the HTTP request cannot be resolved, // this may indicate that the request was incorrectly processed by another client stack. @@ -208,10 +199,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(PrepareRevocationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to System.Net.Http requests. If the HTTP request cannot be resolved, // this may indicate that the request was incorrectly processed by another client stack. @@ -260,10 +248,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(ExtractRevocationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to System.Net.Http requests. If the HTTP response cannot be resolved, // this may indicate that the request was incorrectly processed by another client stack. diff --git a/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.Userinfo.cs b/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.Userinfo.cs index 6ab14581..7d3e3f63 100644 --- a/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.Userinfo.cs +++ b/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.Userinfo.cs @@ -59,10 +59,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(PrepareUserInfoRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to System.Net.Http requests. If the HTTP request cannot be resolved, // this may indicate that the request was incorrectly processed by another client stack. @@ -106,10 +103,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(PrepareUserInfoRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to System.Net.Http requests. If the HTTP request cannot be resolved, // this may indicate that the request was incorrectly processed by another client stack. @@ -169,10 +163,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(PrepareUserInfoRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to System.Net.Http requests. If the HTTP request cannot be resolved, // this may indicate that the request was incorrectly processed by another client stack. @@ -233,10 +224,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(PrepareUserInfoRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // ArcGIS Online doesn't support header-based content negotiation and requires using // the non-standard "f" parameter to get back JSON responses instead of HTML pages. @@ -274,10 +262,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(PrepareUserInfoRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Transaction.Request is not null, SR.GetResourceString(SR.ID4008)); @@ -325,10 +310,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(ExtractUserInfoResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to System.Net.Http requests. If the HTTP response cannot be resolved, // this may indicate that the request was incorrectly processed by another client stack. @@ -390,10 +372,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(ExtractUserInfoResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Response is not null, SR.GetResourceString(SR.ID4007)); @@ -511,10 +490,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(ExtractUserInfoResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Response is not null, SR.GetResourceString(SR.ID4007)); diff --git a/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.cs b/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.cs index 1a0db913..3e83a74e 100644 --- a/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.cs +++ b/src/OpenIddict.Client.WebIntegration/OpenIddictClientWebIntegrationHandlers.cs @@ -87,10 +87,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Shopify returns custom/non-standard parameters like the name of the shop for which the // installation request was initiated. To prevent these parameters from being tampered with, @@ -151,7 +148,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers try { if (!OpenIddictHelpers.FixedTimeEquals( - left : OpenIddictHelpers.ConvertFromHexadecimalString(signature), + left : Convert.FromHexString(signature), right: OpenIddictHelpers.ComputeSha256MessageAuthenticationCode( key : Encoding.UTF8.GetBytes(context.Registration.ClientSecret), data: Encoding.UTF8.GetBytes(builder.ToString())))) @@ -200,10 +197,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: some providers are known to return non-standard errors. // To normalize the set of errors handled by the OpenIddict client, @@ -349,10 +343,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.StateTokenPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -466,10 +457,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.TokenEndpointClientAuthenticationMethod = context.Registration.ProviderType switch { @@ -509,10 +497,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.TokenEndpoint = context.Registration.ProviderType switch { @@ -596,10 +581,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.ClientAssertionPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -641,10 +623,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.TokenRequest is not null, SR.GetResourceString(SR.ID4008)); @@ -707,10 +686,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.TokenRequest is not null, SR.GetResourceString(SR.ID4008)); @@ -752,10 +728,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.TokenRequest is not null, SR.GetResourceString(SR.ID4008)); @@ -810,10 +783,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); (context.ExtractBackchannelIdentityToken, context.RequireBackchannelIdentityToken, @@ -860,10 +830,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: despite implementing OpenID Connect, some providers are known to implement the // specification incorrectly and either don't support the "nonce" authorization request @@ -905,10 +872,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.SendUserInfoRequest = context.Registration.ProviderType switch { @@ -996,10 +960,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: despite implementing OpenID Connect, some providers are known to implement completely custom // userinfo endpoints or semi-standard endpoints that don't fully conform to the core specification. @@ -1038,10 +999,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.UserInfoEndpoint = context.Registration.ProviderType switch { @@ -1131,10 +1089,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.UserInfoRequest is not null, SR.GetResourceString(SR.ID4008)); @@ -1267,10 +1222,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Registration.Issuer is { IsAbsoluteUri: true }, SR.GetResourceString(SR.ID4013)); Debug.Assert(context.TokenResponse is not null, SR.GetResourceString(SR.ID4007)); @@ -1372,10 +1324,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Registration.Issuer is { IsAbsoluteUri: true }, SR.GetResourceString(SR.ID4013)); @@ -1636,10 +1585,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If no explicit shop name was attached to the challenge properties, use the // shop name set in the provider settings, if set. Otherwise, throw an exception. @@ -1679,10 +1625,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.AuthorizationEndpoint = context.Registration.ProviderType switch { @@ -1755,10 +1698,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.ResponseMode = context.Registration.ProviderType switch { @@ -1793,10 +1733,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.Request.Scope = context.Registration.ProviderType switch { @@ -1837,10 +1774,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.RedirectUri is null) { @@ -1891,10 +1825,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Active Directory Federation Services allows sending an optional custom "resource" // parameter to define what API resources the access token will give access to. @@ -2084,10 +2015,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(ProcessRevocationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.RevocationEndpointClientAuthenticationMethod = context.Registration.ProviderType switch { @@ -2122,10 +2050,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(ProcessRevocationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.ClientAssertionPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -2167,10 +2092,7 @@ public static partial class OpenIddictClientWebIntegrationHandlers /// public ValueTask HandleAsync(ProcessRevocationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.RevocationRequest is not null, SR.GetResourceString(SR.ID4008)); diff --git a/src/OpenIddict.Client/OpenIddictClientBuilder.cs b/src/OpenIddict.Client/OpenIddictClientBuilder.cs index b513b44a..8f27ebbc 100644 --- a/src/OpenIddict.Client/OpenIddictClientBuilder.cs +++ b/src/OpenIddict.Client/OpenIddictClientBuilder.cs @@ -7,7 +7,6 @@ using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using System.Reflection; -using System.Runtime.InteropServices; using System.Security.Claims; using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; @@ -46,10 +45,7 @@ public sealed class OpenIddictClientBuilder Action> configuration) where TContext : BaseContext { - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(configuration); // Note: handlers registered using this API are assumed to be custom handlers by default. var builder = OpenIddictClientHandlerDescriptor.CreateBuilder() @@ -68,10 +64,7 @@ public sealed class OpenIddictClientBuilder [EditorBrowsable(EditorBrowsableState.Advanced)] public OpenIddictClientBuilder AddEventHandler(OpenIddictClientHandlerDescriptor descriptor) { - if (descriptor is null) - { - throw new ArgumentNullException(nameof(descriptor)); - } + ArgumentNullException.ThrowIfNull(descriptor); // Register the handler in the services collection. Services.Add(descriptor.ServiceDescriptor); @@ -87,10 +80,7 @@ public sealed class OpenIddictClientBuilder [EditorBrowsable(EditorBrowsableState.Advanced)] public OpenIddictClientBuilder RemoveEventHandler(OpenIddictClientHandlerDescriptor descriptor) { - if (descriptor is null) - { - throw new ArgumentNullException(nameof(descriptor)); - } + ArgumentNullException.ThrowIfNull(descriptor); Services.RemoveAll(descriptor.ServiceDescriptor.ServiceType); @@ -116,10 +106,7 @@ public sealed class OpenIddictClientBuilder /// The instance. public OpenIddictClientBuilder Configure(Action configuration) { - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(configuration); Services.Configure(configuration); @@ -133,10 +120,7 @@ public sealed class OpenIddictClientBuilder /// The instance. public OpenIddictClientBuilder AddEncryptionCredentials(EncryptingCredentials credentials) { - if (credentials is null) - { - throw new ArgumentNullException(nameof(credentials)); - } + ArgumentNullException.ThrowIfNull(credentials); return Configure(options => options.EncryptionCredentials.Add(credentials)); } @@ -148,10 +132,7 @@ public sealed class OpenIddictClientBuilder /// The instance. public OpenIddictClientBuilder AddEncryptionKey(SecurityKey key) { - if (key is null) - { - throw new ArgumentNullException(nameof(key)); - } + ArgumentNullException.ThrowIfNull(key); // If the encryption key is an asymmetric security key, ensure it has a private key. if (key is AsymmetricSecurityKey asymmetricSecurityKey && @@ -187,10 +168,7 @@ public sealed class OpenIddictClientBuilder /// The instance. public OpenIddictClientBuilder AddEncryptionKeys(IEnumerable keys) { - if (keys is null) - { - throw new ArgumentNullException(nameof(keys)); - } + ArgumentNullException.ThrowIfNull(keys); return keys.Aggregate(this, static (builder, key) => builder.AddEncryptionKey(key)); } @@ -209,10 +187,7 @@ public sealed class OpenIddictClientBuilder /// The instance. public OpenIddictClientBuilder AddDevelopmentEncryptionCertificate(X500DistinguishedName subject) { - if (subject is null) - { - throw new ArgumentNullException(nameof(subject)); - } + ArgumentNullException.ThrowIfNull(subject); Services.AddOptions().Configure((options, provider) => { @@ -242,7 +217,7 @@ public sealed class OpenIddictClientBuilder // Note: setting the friendly name is not supported on Unix machines (including Linux and macOS). // To ensure an exception is not thrown by the property setter, an OS runtime check is used here. - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + if (OperatingSystem.IsWindows()) { certificate.FriendlyName = "OpenIddict Client Development Encryption Certificate"; } @@ -259,7 +234,7 @@ public sealed class OpenIddictClientBuilder // Note: macOS requires marking the certificate private key as exportable. // If this flag is not set, a CryptographicException is thrown at runtime. - if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + if (OperatingSystem.IsMacOS()) { flags |= X509KeyStorageFlags.Exportable; } @@ -313,10 +288,7 @@ public sealed class OpenIddictClientBuilder /// The instance. public OpenIddictClientBuilder AddEphemeralEncryptionKey(string algorithm) { - if (string.IsNullOrEmpty(algorithm)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0057), nameof(algorithm)); - } + ArgumentException.ThrowIfNullOrEmpty(algorithm); return algorithm switch { @@ -342,10 +314,7 @@ public sealed class OpenIddictClientBuilder /// The instance. public OpenIddictClientBuilder AddEncryptionCertificate(X509Certificate2 certificate) { - if (certificate is null) - { - throw new ArgumentNullException(nameof(certificate)); - } + ArgumentNullException.ThrowIfNull(certificate); // If the certificate is a X.509v3 certificate that specifies at least one // key usage, ensure that the certificate key can be used for key encryption. @@ -377,7 +346,7 @@ public sealed class OpenIddictClientBuilder public OpenIddictClientBuilder AddEncryptionCertificate(Assembly assembly, string resource, string? password) #if SUPPORTS_EPHEMERAL_KEY_SETS // Note: ephemeral key sets are currently not supported on macOS. - => AddEncryptionCertificate(assembly, resource, password, RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? + => AddEncryptionCertificate(assembly, resource, password, OperatingSystem.IsMacOS() ? X509KeyStorageFlags.MachineKeySet : X509KeyStorageFlags.EphemeralKeySet); #else @@ -396,15 +365,8 @@ public sealed class OpenIddictClientBuilder Assembly assembly, string resource, string? password, X509KeyStorageFlags flags) { - if (assembly is null) - { - throw new ArgumentNullException(nameof(assembly)); - } - - if (string.IsNullOrEmpty(resource)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0062), nameof(resource)); - } + ArgumentNullException.ThrowIfNull(assembly); + ArgumentException.ThrowIfNullOrEmpty(resource); using var stream = assembly.GetManifestResourceStream(resource) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0064)); @@ -421,7 +383,7 @@ public sealed class OpenIddictClientBuilder public OpenIddictClientBuilder AddEncryptionCertificate(Stream stream, string? password) #if SUPPORTS_EPHEMERAL_KEY_SETS // Note: ephemeral key sets are currently not supported on macOS. - => AddEncryptionCertificate(stream, password, RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? + => AddEncryptionCertificate(stream, password, OperatingSystem.IsMacOS() ? X509KeyStorageFlags.MachineKeySet : X509KeyStorageFlags.EphemeralKeySet); #else @@ -437,10 +399,7 @@ public sealed class OpenIddictClientBuilder /// The instance. public OpenIddictClientBuilder AddEncryptionCertificate(Stream stream, string? password, X509KeyStorageFlags flags) { - if (stream is null) - { - throw new ArgumentNullException(nameof(stream)); - } + ArgumentNullException.ThrowIfNull(stream); using var buffer = new MemoryStream(); stream.CopyTo(buffer); @@ -465,10 +424,7 @@ public sealed class OpenIddictClientBuilder /// The instance. public OpenIddictClientBuilder AddEncryptionCertificate(string thumbprint) { - if (string.IsNullOrEmpty(thumbprint)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0065), nameof(thumbprint)); - } + ArgumentException.ThrowIfNullOrEmpty(thumbprint); return AddEncryptionCertificate( GetCertificate(StoreLocation.CurrentUser, thumbprint) ?? @@ -495,10 +451,7 @@ public sealed class OpenIddictClientBuilder /// The instance. public OpenIddictClientBuilder AddEncryptionCertificate(string thumbprint, StoreName name, StoreLocation location) { - if (string.IsNullOrEmpty(thumbprint)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0065), nameof(thumbprint)); - } + ArgumentException.ThrowIfNullOrEmpty(thumbprint); using var store = new X509Store(name, location); store.Open(OpenFlags.ReadOnly); @@ -516,11 +469,8 @@ public sealed class OpenIddictClientBuilder /// The instance. public OpenIddictClientBuilder AddEncryptionCertificates(IEnumerable certificates) { - if (certificates is null) - { - throw new ArgumentNullException(nameof(certificates)); - } - + ArgumentNullException.ThrowIfNull(certificates); + return certificates.Aggregate(this, static (builder, certificate) => builder.AddEncryptionCertificate(certificate)); } @@ -531,10 +481,7 @@ public sealed class OpenIddictClientBuilder /// The instance. public OpenIddictClientBuilder AddSigningCredentials(SigningCredentials credentials) { - if (credentials is null) - { - throw new ArgumentNullException(nameof(credentials)); - } + ArgumentNullException.ThrowIfNull(credentials); return Configure(options => options.SigningCredentials.Add(credentials)); } @@ -546,10 +493,7 @@ public sealed class OpenIddictClientBuilder /// The instance. public OpenIddictClientBuilder AddSigningKey(SecurityKey key) { - if (key is null) - { - throw new ArgumentNullException(nameof(key)); - } + ArgumentNullException.ThrowIfNull(key); // If the signing key is an asymmetric security key, ensure it has a private key. if (key is AsymmetricSecurityKey asymmetricSecurityKey && @@ -603,10 +547,7 @@ public sealed class OpenIddictClientBuilder /// The instance. public OpenIddictClientBuilder AddSigningKeys(IEnumerable keys) { - if (keys is null) - { - throw new ArgumentNullException(nameof(keys)); - } + ArgumentNullException.ThrowIfNull(keys); return keys.Aggregate(this, static (builder, key) => builder.AddSigningKey(key)); } @@ -625,10 +566,7 @@ public sealed class OpenIddictClientBuilder /// The instance. public OpenIddictClientBuilder AddDevelopmentSigningCertificate(X500DistinguishedName subject) { - if (subject is null) - { - throw new ArgumentNullException(nameof(subject)); - } + ArgumentNullException.ThrowIfNull(subject); Services.AddOptions().Configure((options, provider) => { @@ -658,7 +596,7 @@ public sealed class OpenIddictClientBuilder // Note: setting the friendly name is not supported on Unix machines (including Linux and macOS). // To ensure an exception is not thrown by the property setter, an OS runtime check is used here. - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + if (OperatingSystem.IsWindows()) { certificate.FriendlyName = "OpenIddict Client Development Signing Certificate"; } @@ -675,7 +613,7 @@ public sealed class OpenIddictClientBuilder // Note: macOS requires marking the certificate private key as exportable. // If this flag is not set, a CryptographicException is thrown at runtime. - if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + if (OperatingSystem.IsMacOS()) { flags |= X509KeyStorageFlags.Exportable; } @@ -728,10 +666,7 @@ public sealed class OpenIddictClientBuilder /// The instance. public OpenIddictClientBuilder AddEphemeralSigningKey(string algorithm) { - if (string.IsNullOrEmpty(algorithm)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0057), nameof(algorithm)); - } + ArgumentException.ThrowIfNullOrEmpty(algorithm); return algorithm switch { @@ -786,10 +721,7 @@ public sealed class OpenIddictClientBuilder /// The instance. public OpenIddictClientBuilder AddSigningCertificate(X509Certificate2 certificate) { - if (certificate is null) - { - throw new ArgumentNullException(nameof(certificate)); - } + ArgumentNullException.ThrowIfNull(certificate); // If the certificate is a X.509v3 certificate that specifies at least // one key usage, ensure that the certificate key can be used for signing. @@ -821,7 +753,7 @@ public sealed class OpenIddictClientBuilder public OpenIddictClientBuilder AddSigningCertificate(Assembly assembly, string resource, string? password) #if SUPPORTS_EPHEMERAL_KEY_SETS // Note: ephemeral key sets are currently not supported on macOS. - => AddSigningCertificate(assembly, resource, password, RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? + => AddSigningCertificate(assembly, resource, password, OperatingSystem.IsMacOS() ? X509KeyStorageFlags.MachineKeySet : X509KeyStorageFlags.EphemeralKeySet); #else @@ -840,15 +772,8 @@ public sealed class OpenIddictClientBuilder Assembly assembly, string resource, string? password, X509KeyStorageFlags flags) { - if (assembly is null) - { - throw new ArgumentNullException(nameof(assembly)); - } - - if (string.IsNullOrEmpty(resource)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0062), nameof(resource)); - } + ArgumentNullException.ThrowIfNull(assembly); + ArgumentException.ThrowIfNullOrEmpty(resource); using var stream = assembly.GetManifestResourceStream(resource) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0064)); @@ -865,7 +790,7 @@ public sealed class OpenIddictClientBuilder public OpenIddictClientBuilder AddSigningCertificate(Stream stream, string? password) #if SUPPORTS_EPHEMERAL_KEY_SETS // Note: ephemeral key sets are currently not supported on macOS. - => AddSigningCertificate(stream, password, RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? + => AddSigningCertificate(stream, password, OperatingSystem.IsMacOS() ? X509KeyStorageFlags.MachineKeySet : X509KeyStorageFlags.EphemeralKeySet); #else @@ -881,10 +806,7 @@ public sealed class OpenIddictClientBuilder /// The instance. public OpenIddictClientBuilder AddSigningCertificate(Stream stream, string? password, X509KeyStorageFlags flags) { - if (stream is null) - { - throw new ArgumentNullException(nameof(stream)); - } + ArgumentNullException.ThrowIfNull(stream); using var buffer = new MemoryStream(); stream.CopyTo(buffer); @@ -909,10 +831,7 @@ public sealed class OpenIddictClientBuilder /// The instance. public OpenIddictClientBuilder AddSigningCertificate(string thumbprint) { - if (string.IsNullOrEmpty(thumbprint)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0065), nameof(thumbprint)); - } + ArgumentException.ThrowIfNullOrEmpty(thumbprint); return AddSigningCertificate( GetCertificate(StoreLocation.CurrentUser, thumbprint) ?? @@ -939,10 +858,7 @@ public sealed class OpenIddictClientBuilder /// The instance. public OpenIddictClientBuilder AddSigningCertificate(string thumbprint, StoreName name, StoreLocation location) { - if (string.IsNullOrEmpty(thumbprint)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0065), nameof(thumbprint)); - } + ArgumentException.ThrowIfNullOrEmpty(thumbprint); using var store = new X509Store(name, location); store.Open(OpenFlags.ReadOnly); @@ -960,11 +876,8 @@ public sealed class OpenIddictClientBuilder /// The instance. public OpenIddictClientBuilder AddSigningCertificates(IEnumerable certificates) { - if (certificates is null) - { - throw new ArgumentNullException(nameof(certificates)); - } - + ArgumentNullException.ThrowIfNull(certificates); + return certificates.Aggregate(this, static (builder, certificate) => builder.AddSigningCertificate(certificate)); } @@ -975,10 +888,7 @@ public sealed class OpenIddictClientBuilder /// The instance. public OpenIddictClientBuilder AddRegistration(OpenIddictClientRegistration registration) { - if (registration is null) - { - throw new ArgumentNullException(nameof(registration)); - } + ArgumentNullException.ThrowIfNull(registration); return Configure(options => options.Registrations.Add(registration)); } @@ -1038,10 +948,7 @@ public sealed class OpenIddictClientBuilder [EditorBrowsable(EditorBrowsableState.Advanced)] public OpenIddictClientBuilder AllowCustomFlow(string type) { - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0071), nameof(type)); - } + ArgumentException.ThrowIfNullOrEmpty(type); return Configure(options => options.GrantTypes.Add(type)); } @@ -1138,10 +1045,7 @@ public sealed class OpenIddictClientBuilder public OpenIddictClientBuilder SetPostLogoutRedirectionEndpointUris( [StringSyntax(StringSyntaxAttribute.Uri)] params string[] uris) { - if (uris is null) - { - throw new ArgumentNullException(nameof(uris)); - } + ArgumentNullException.ThrowIfNull(uris); return SetPostLogoutRedirectionEndpointUris([.. uris.Select(uri => new Uri(uri, UriKind.RelativeOrAbsolute))]); } @@ -1154,10 +1058,7 @@ public sealed class OpenIddictClientBuilder /// The instance. public OpenIddictClientBuilder SetPostLogoutRedirectionEndpointUris(params Uri[] uris) { - if (uris is null) - { - throw new ArgumentNullException(nameof(uris)); - } + ArgumentNullException.ThrowIfNull(uris); if (Array.Exists(uris, OpenIddictHelpers.IsImplicitFileUri)) { @@ -1191,10 +1092,7 @@ public sealed class OpenIddictClientBuilder public OpenIddictClientBuilder SetRedirectionEndpointUris( [StringSyntax(StringSyntaxAttribute.Uri)] params string[] uris) { - if (uris is null) - { - throw new ArgumentNullException(nameof(uris)); - } + ArgumentNullException.ThrowIfNull(uris); return SetRedirectionEndpointUris([.. uris.Select(uri => new Uri(uri, UriKind.RelativeOrAbsolute))]); } @@ -1213,10 +1111,7 @@ public sealed class OpenIddictClientBuilder /// The instance. public OpenIddictClientBuilder SetRedirectionEndpointUris(params Uri[] uris) { - if (uris is null) - { - throw new ArgumentNullException(nameof(uris)); - } + ArgumentNullException.ThrowIfNull(uris); if (Array.Exists(uris, OpenIddictHelpers.IsImplicitFileUri)) { @@ -1265,10 +1160,7 @@ public sealed class OpenIddictClientBuilder [EditorBrowsable(EditorBrowsableState.Advanced)] public OpenIddictClientBuilder SetClientUri(Uri uri) { - if (uri is null) - { - throw new ArgumentNullException(nameof(uri)); - } + ArgumentNullException.ThrowIfNull(uri); return Configure(options => options.ClientUri = uri); } @@ -1282,10 +1174,7 @@ public sealed class OpenIddictClientBuilder public OpenIddictClientBuilder SetClientUri( [StringSyntax(StringSyntaxAttribute.Uri, UriKind.Absolute)] string uri) { - if (string.IsNullOrEmpty(uri)) - { - throw new ArgumentException(SR.FormatID0366(nameof(uri)), nameof(uri)); - } + ArgumentException.ThrowIfNullOrEmpty(uri); if (!Uri.TryCreate(uri, UriKind.Absolute, out Uri? value) || OpenIddictHelpers.IsImplicitFileUri(value)) { diff --git a/src/OpenIddict.Client/OpenIddictClientConfiguration.cs b/src/OpenIddict.Client/OpenIddictClientConfiguration.cs index df0d4abe..cb615005 100644 --- a/src/OpenIddict.Client/OpenIddictClientConfiguration.cs +++ b/src/OpenIddict.Client/OpenIddictClientConfiguration.cs @@ -40,10 +40,7 @@ public sealed class OpenIddictClientConfiguration : IPostConfigureOptions public void PostConfigure(string? name, OpenIddictClientOptions options) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); if (options.JsonWebTokenHandler is null) { diff --git a/src/OpenIddict.Client/OpenIddictClientDispatcher.cs b/src/OpenIddict.Client/OpenIddictClientDispatcher.cs index d793a23e..e0373ba6 100644 --- a/src/OpenIddict.Client/OpenIddictClientDispatcher.cs +++ b/src/OpenIddict.Client/OpenIddictClientDispatcher.cs @@ -36,10 +36,7 @@ public sealed class OpenIddictClientDispatcher : IOpenIddictClientDispatcher /// public async ValueTask DispatchAsync(TContext context) where TContext : BaseContext { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); await foreach (var handler in GetHandlersAsync()) { diff --git a/src/OpenIddict.Client/OpenIddictClientExtensions.cs b/src/OpenIddict.Client/OpenIddictClientExtensions.cs index 2be9937c..0465e0c9 100644 --- a/src/OpenIddict.Client/OpenIddictClientExtensions.cs +++ b/src/OpenIddict.Client/OpenIddictClientExtensions.cs @@ -23,10 +23,7 @@ public static class OpenIddictClientExtensions /// The instance. public static OpenIddictClientBuilder AddClient(this OpenIddictBuilder builder) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } + ArgumentNullException.ThrowIfNull(builder); builder.Services.AddLogging(); builder.Services.AddOptions(); @@ -98,15 +95,8 @@ public static class OpenIddictClientExtensions /// The instance. public static OpenIddictBuilder AddClient(this OpenIddictBuilder builder, Action configuration) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } - - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(builder); + ArgumentNullException.ThrowIfNull(configuration); configuration(builder.AddClient()); diff --git a/src/OpenIddict.Client/OpenIddictClientHandlerDescriptor.cs b/src/OpenIddict.Client/OpenIddictClientHandlerDescriptor.cs index 77c046c5..7b380cc9 100644 --- a/src/OpenIddict.Client/OpenIddictClientHandlerDescriptor.cs +++ b/src/OpenIddict.Client/OpenIddictClientHandlerDescriptor.cs @@ -88,10 +88,7 @@ public sealed class OpenIddictClientHandlerDescriptor /// The builder instance, so that calls can be easily chained. public Builder Import(OpenIddictClientHandlerDescriptor descriptor) { - if (descriptor is null) - { - throw new ArgumentNullException(nameof(descriptor)); - } + ArgumentNullException.ThrowIfNull(descriptor); if (descriptor.ContextType != typeof(TContext)) { @@ -114,10 +111,7 @@ public sealed class OpenIddictClientHandlerDescriptor /// The builder instance, so that calls can be easily chained. public Builder SetServiceDescriptor(ServiceDescriptor descriptor) { - if (descriptor is null) - { - throw new ArgumentNullException(nameof(descriptor)); - } + ArgumentNullException.ThrowIfNull(descriptor); if (!typeof(IOpenIddictClientHandler).IsAssignableFrom(descriptor.ServiceType)) { diff --git a/src/OpenIddict.Client/OpenIddictClientHandlerFilters.cs b/src/OpenIddict.Client/OpenIddictClientHandlerFilters.cs index ce80ee5c..d3991c91 100644 --- a/src/OpenIddict.Client/OpenIddictClientHandlerFilters.cs +++ b/src/OpenIddict.Client/OpenIddictClientHandlerFilters.cs @@ -19,10 +19,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.ValidateAuthorizationCode); } @@ -36,10 +33,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.ValidateBackchannelAccessToken); } @@ -53,10 +47,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!context.DisableBackchannelIdentityTokenNonceValidation); } @@ -70,10 +61,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.BackchannelIdentityTokenPrincipal is not null); } @@ -87,10 +75,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.ValidateBackchannelIdentityToken); } @@ -104,10 +89,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.GenerateClientAssertion); } @@ -123,10 +105,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.GenerateClientAssertion); } @@ -134,10 +113,7 @@ public static class OpenIddictClientHandlerFilters /// ValueTask IOpenIddictClientHandlerFilter.IsActiveAsync(ProcessRevocationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.GenerateClientAssertion); } @@ -152,10 +128,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.GrantType is GrantTypes.DeviceCode); } @@ -169,10 +142,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.SendDeviceAuthorizationRequest); } @@ -186,10 +156,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.ValidateFrontchannelAccessToken); } @@ -203,10 +170,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!context.DisableFrontchannelIdentityTokenNonceValidation); } @@ -220,10 +184,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.FrontchannelIdentityTokenPrincipal is not null); } @@ -237,10 +198,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.ValidateFrontchannelIdentityToken); } @@ -255,10 +213,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.GrantType switch { @@ -277,10 +232,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(ProcessIntrospectionContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.GenerateClientAssertion); } @@ -294,10 +246,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(ProcessIntrospectionContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.SendIntrospectionRequest); } @@ -311,10 +260,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.ValidateIssuedToken); } @@ -328,10 +274,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(GenerateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.TokenFormat is TokenFormats.Private.JsonWebToken); } @@ -345,10 +288,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.GenerateStateToken); } @@ -362,10 +302,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(ProcessSignOutContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.GenerateStateToken); } @@ -379,10 +316,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.EndpointType is OpenIddictClientEndpointType.PostLogoutRedirection); } @@ -396,10 +330,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.SendPushedAuthorizationRequest); } @@ -413,10 +344,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.EndpointType is OpenIddictClientEndpointType.Redirection); } @@ -430,10 +358,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.ValidateRefreshToken); } @@ -447,10 +372,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(ProcessRevocationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.GenerateClientAssertion); } @@ -464,10 +386,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(ProcessRevocationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.SendRevocationRequest); } @@ -481,10 +400,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.StateTokenPrincipal is not null); } @@ -498,10 +414,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.ValidateStateToken); } @@ -515,10 +428,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!context.DisableAudienceValidation); } @@ -532,10 +442,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(GenerateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.CreateTokenEntry); } @@ -549,10 +456,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!string.IsNullOrEmpty(context.TokenId)); } @@ -566,10 +470,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!context.DisableLifetimeValidation); } @@ -583,10 +484,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(GenerateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.PersistTokenPayload); } @@ -600,10 +498,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!context.DisablePresenterValidation); } @@ -617,10 +512,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.SendTokenRequest); } @@ -634,10 +526,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!context.Options.DisableTokenStorage); } @@ -651,10 +540,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.SendUserInfoRequest); } @@ -668,10 +554,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.ExtractUserInfoToken); } @@ -685,10 +568,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.UserInfoTokenPrincipal is not null); } @@ -702,10 +582,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!context.DisableUserInfoValidation); } @@ -719,10 +596,7 @@ public static class OpenIddictClientHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!context.Options.DisableWebServicesFederationClaimMapping); } diff --git a/src/OpenIddict.Client/OpenIddictClientHandlers.Authentication.cs b/src/OpenIddict.Client/OpenIddictClientHandlers.Authentication.cs index ca259ad8..9173eb8b 100644 --- a/src/OpenIddict.Client/OpenIddictClientHandlers.Authentication.cs +++ b/src/OpenIddict.Client/OpenIddictClientHandlers.Authentication.cs @@ -78,10 +78,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new PrepareAuthorizationRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -123,10 +120,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ApplyAuthorizationRequestContext(context.Transaction) { @@ -178,10 +172,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ApplyAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Don't overwrite the endpoint URI if it was already set. if (!string.IsNullOrEmpty(context.AuthorizationEndpoint)) @@ -220,10 +211,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandlePushedAuthorizationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); foreach (var parameter in context.Response.GetParameters()) { @@ -284,10 +272,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandlePushedAuthorizationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // For more information, see https://www.rfc-editor.org/rfc/rfc8628#section-3.2. if (!string.IsNullOrEmpty(context.Response.Error)) @@ -331,10 +316,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandlePushedAuthorizationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Return an error if the mandatory "request_uri" parameter is missing. // @@ -384,10 +366,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandlePushedAuthorizationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Return an error if the mandatory "expires_in" parameter is missing. // @@ -429,10 +408,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ExtractRedirectionRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -491,10 +467,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ValidateRedirectionRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -547,10 +520,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new HandleRedirectionRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -604,10 +574,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ApplyRedirectionResponseContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -646,10 +613,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(PrepareAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // When the response mode corresponds to the default mode assigned to the selected // response type, the specification explicitly recommends omitting the response mode. @@ -697,10 +661,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ValidateRedirectionRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ProcessAuthenticationContext(context.Transaction); await _dispatcher.DispatchAsync(notification); diff --git a/src/OpenIddict.Client/OpenIddictClientHandlers.Device.cs b/src/OpenIddict.Client/OpenIddictClientHandlers.Device.cs index 47fa72c1..ba05df2b 100644 --- a/src/OpenIddict.Client/OpenIddictClientHandlers.Device.cs +++ b/src/OpenIddict.Client/OpenIddictClientHandlers.Device.cs @@ -43,10 +43,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleDeviceAuthorizationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); foreach (var parameter in context.Response.GetParameters()) { @@ -113,10 +110,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleDeviceAuthorizationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // For more information, see https://www.rfc-editor.org/rfc/rfc8628#section-3.2. if (!string.IsNullOrEmpty(context.Response.Error)) @@ -161,10 +155,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleDeviceAuthorizationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Return an error if the mandatory "verification_uri" parameter is missing. // For more information, see https://www.rfc-editor.org/rfc/rfc8628#section-3.2. @@ -227,10 +218,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleDeviceAuthorizationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Return an error if the mandatory "expires_in" parameter is missing. // For more information, see https://www.rfc-editor.org/rfc/rfc8628#section-3.2. diff --git a/src/OpenIddict.Client/OpenIddictClientHandlers.Discovery.cs b/src/OpenIddict.Client/OpenIddictClientHandlers.Discovery.cs index 13235fa0..c9687c7d 100644 --- a/src/OpenIddict.Client/OpenIddictClientHandlers.Discovery.cs +++ b/src/OpenIddict.Client/OpenIddictClientHandlers.Discovery.cs @@ -78,10 +78,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); foreach (var parameter in context.Response.GetParameters()) { @@ -171,10 +168,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: the specification doesn't define a standard way to return an error other than // returning a 4xx status code. That said, some implementations are known to return @@ -214,10 +208,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: the issuer returned in the discovery document must exactly match the URI used to access it. // See https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfigurationValidation. @@ -278,10 +269,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: the authorization_endpoint node is required by the OpenID Connect discovery specification // but is optional in the OAuth 2.0 authorization server metadata specification. To make OpenIddict @@ -330,10 +318,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: the jwks_uri node is required by the OpenID Connect discovery specification. // See https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfigurationClient. @@ -382,10 +367,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var endpoint = (string?) context.Response[Metadata.DeviceAuthorizationEndpoint]; if (!string.IsNullOrEmpty(endpoint)) @@ -425,10 +407,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var endpoint = (string?) context.Response[Metadata.IntrospectionEndpoint]; if (!string.IsNullOrEmpty(endpoint)) @@ -468,10 +447,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var endpoint = (string?) context.Response[Metadata.EndSessionEndpoint]; if (!string.IsNullOrEmpty(endpoint)) @@ -512,10 +488,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: as recommended by the specification, values present in the "mtls_endpoint_aliases" node // that can't be recognized as OAuth 2.0 endpoints or are not valid URIs are simply ignored. @@ -548,10 +521,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: as recommended by the specification, values present in the "mtls_endpoint_aliases" node // that can't be recognized as OAuth 2.0 endpoints or are not valid URIs are simply ignored. @@ -583,10 +553,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: as recommended by the specification, values present in the "mtls_endpoint_aliases" node // that can't be recognized as OAuth 2.0 endpoints or are not valid URIs are simply ignored. @@ -618,10 +585,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: as recommended by the specification, values present in the "mtls_endpoint_aliases" node // that can't be recognized as OAuth 2.0 endpoints or are not valid URIs are simply ignored. @@ -653,10 +617,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: as recommended by the specification, values present in the "mtls_endpoint_aliases" node // that can't be recognized as OAuth 2.0 endpoints or are not valid URIs are simply ignored. @@ -688,10 +649,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: as recommended by the specification, values present in the "mtls_endpoint_aliases" node // that can't be recognized as OAuth 2.0 endpoints or are not valid URIs are simply ignored. @@ -723,10 +681,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var endpoint = (string?) context.Response[Metadata.PushedAuthorizationRequestEndpoint]; if (!string.IsNullOrEmpty(endpoint)) @@ -766,10 +721,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var endpoint = (string?) context.Response[Metadata.RevocationEndpoint]; if (!string.IsNullOrEmpty(endpoint)) @@ -809,10 +761,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var endpoint = (string?) context.Response[Metadata.TokenEndpoint]; if (!string.IsNullOrEmpty(endpoint)) @@ -852,10 +801,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var endpoint = (string?) context.Response[Metadata.UserInfoEndpoint]; if (!string.IsNullOrEmpty(endpoint)) @@ -895,10 +841,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Resolve the grant types supported by the authorization endpoint, if available. foreach (var type in (ImmutableArray?) context.Response[Metadata.GrantTypesSupported] ?? []) @@ -931,10 +874,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Resolve the response modes supported by the authorization endpoint, if available. foreach (var mode in (ImmutableArray?) context.Response[Metadata.ResponseModesSupported] ?? []) @@ -967,10 +907,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Resolve the response types supported by the authorization endpoint, if available. foreach (var type in (ImmutableArray?) context.Response[Metadata.ResponseTypesSupported] ?? []) @@ -1003,10 +940,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Resolve the code challenge methods supported by the authorization endpoint, if available. foreach (var method in (ImmutableArray?) context.Response[Metadata.CodeChallengeMethodsSupported] ?? []) @@ -1039,10 +973,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Resolve the scopes supported by the remote server, if available. foreach (var scope in (ImmutableArray?) context.Response[Metadata.ScopesSupported] ?? []) @@ -1076,10 +1007,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.Configuration.AuthorizationResponseIssParameterSupported = (bool?) context.Response[Metadata.AuthorizationResponseIssParameterSupported]; @@ -1107,10 +1035,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.Configuration.TlsClientCertificateBoundAccessTokens = (bool?) context.Response[Metadata.TlsClientCertificateBoundAccessTokens]; @@ -1138,10 +1063,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.Configuration.RequirePushedAuthorizationRequests = (bool?) context.Response[Metadata.RequirePushedAuthorizationRequests]; @@ -1169,10 +1091,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Resolve the client authentication methods supported by the device authorization endpoint, if available. // @@ -1209,10 +1128,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Resolve the client authentication methods supported by the introspection endpoint, if available. foreach (var method in (ImmutableArray?) context.Response[Metadata.IntrospectionEndpointAuthMethodsSupported] ?? []) @@ -1246,10 +1162,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Resolve the client authentication methods supported by the pushed authorization endpoint, if available. // @@ -1286,10 +1199,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Resolve the client authentication methods supported by the revocation endpoint, if available. foreach (var method in (ImmutableArray?) context.Response[Metadata.RevocationEndpointAuthMethodsSupported] ?? []) @@ -1323,10 +1233,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Resolve the client authentication methods supported by the token endpoint, if available. foreach (var method in (ImmutableArray?) context.Response[Metadata.TokenEndpointAuthMethodsSupported] ?? []) @@ -1359,10 +1266,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleJsonWebKeySetResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); foreach (var parameter in context.Response.GetParameters()) { @@ -1421,10 +1325,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleJsonWebKeySetResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: the specification doesn't define a standard way to return an error other than // returning a 4xx status code. That said, some implementations are known to return @@ -1464,10 +1365,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleJsonWebKeySetResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var keys = context.Response[JsonWebKeySetParameterNames.Keys]?.GetUnnamedParameters(); if (keys is not { Count: > 0 }) diff --git a/src/OpenIddict.Client/OpenIddictClientHandlers.Exchange.cs b/src/OpenIddict.Client/OpenIddictClientHandlers.Exchange.cs index e4eb8f2b..98abf700 100644 --- a/src/OpenIddict.Client/OpenIddictClientHandlers.Exchange.cs +++ b/src/OpenIddict.Client/OpenIddictClientHandlers.Exchange.cs @@ -41,10 +41,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleTokenResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); foreach (var parameter in context.Response.GetParameters()) { @@ -107,10 +104,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleTokenResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // For more information, see https://datatracker.ietf.org/doc/html/rfc6749#section-5.2. if (!string.IsNullOrEmpty(context.Response.Error)) diff --git a/src/OpenIddict.Client/OpenIddictClientHandlers.Introspection.cs b/src/OpenIddict.Client/OpenIddictClientHandlers.Introspection.cs index 27097be0..69e3e4f2 100644 --- a/src/OpenIddict.Client/OpenIddictClientHandlers.Introspection.cs +++ b/src/OpenIddict.Client/OpenIddictClientHandlers.Introspection.cs @@ -50,10 +50,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleIntrospectionResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); foreach (var parameter in context.Response.GetParameters()) { @@ -127,10 +124,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleIntrospectionResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: the specification requires returning most errors (e.g invalid token errors) // as "active: false" responses instead of as proper OAuth 2.0 error responses. @@ -173,10 +167,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleIntrospectionResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: the introspection specification requires that server return "active: false" instead of a proper // OAuth 2.0 error when the token is invalid, expired, revoked or invalid for any other reason. @@ -226,10 +217,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleIntrospectionResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // The issuer claim is optional. If it's not null or empty, validate it to // ensure it matches the issuer registered in the server configuration. @@ -280,10 +268,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleIntrospectionResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: in most cases, an expired token should lead to an errored or "active=false" response // being returned by the authorization server. Unfortunately, some implementations are known not @@ -327,10 +312,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleIntrospectionResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // OpenIddict-based authorization servers always return the actual token type using // the special "token_usage" claim, that helps resource servers determine whether the @@ -383,10 +365,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleIntrospectionResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Registration.Issuer is { IsAbsoluteUri: true }, SR.GetResourceString(SR.ID4013)); @@ -476,10 +455,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleIntrospectionResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Principal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); diff --git a/src/OpenIddict.Client/OpenIddictClientHandlers.Protection.cs b/src/OpenIddict.Client/OpenIddictClientHandlers.Protection.cs index d43f7224..d2239576 100644 --- a/src/OpenIddict.Client/OpenIddictClientHandlers.Protection.cs +++ b/src/OpenIddict.Client/OpenIddictClientHandlers.Protection.cs @@ -64,10 +64,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // The OpenIddict client is expected to validate tokens it creates (e.g state tokens) and // tokens that are created by one or multiple authorization servers (e.g identity tokens). @@ -206,10 +203,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If no character was explicitly added, all characters are considered valid. if (context.AllowedCharset.Count is 0) @@ -262,10 +256,7 @@ public static partial class OpenIddictClientHandlers public async ValueTask HandleAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: reference tokens are only used for state tokens. if (context.ValidTokenTypes.Count is not 1 || @@ -336,10 +327,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If a principal was already attached, don't overwrite it. if (context.Principal is not null) @@ -494,10 +482,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: only map the private claims from fully trusted tokens. if (context.Principal is null || !context.Principal.HasTokenType(TokenTypeIdentifiers.Private.StateToken)) @@ -559,10 +544,7 @@ public static partial class OpenIddictClientHandlers public async ValueTask HandleAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.Principal is null) { @@ -625,10 +607,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.Principal is null) { @@ -678,10 +657,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Principal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -719,10 +695,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Principal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -782,10 +755,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Principal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -855,10 +825,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Principal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); Debug.Assert(!string.IsNullOrEmpty(context.TokenId), SR.GetResourceString(SR.ID4017)); @@ -920,10 +887,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(GenerateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.SecurityTokenHandler = context.Options.JsonWebTokenHandler; @@ -980,10 +944,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(GenerateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var descriptor = new OpenIddictTokenDescriptor { @@ -1028,10 +989,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(GenerateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.Principal is not { Identity: ClaimsIdentity } principal) { @@ -1077,10 +1035,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(GenerateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var claims = context.SecurityTokenDescriptor.Claims is not null ? new Dictionary(context.SecurityTokenDescriptor.Claims, StringComparer.Ordinal) : @@ -1143,10 +1098,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(GenerateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If a token was already attached by another handler, don't overwrite it. if (!string.IsNullOrEmpty(context.Token)) @@ -1191,10 +1143,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(GenerateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var identifier = context.Principal.GetTokenId(); if (string.IsNullOrEmpty(identifier)) diff --git a/src/OpenIddict.Client/OpenIddictClientHandlers.Revocation.cs b/src/OpenIddict.Client/OpenIddictClientHandlers.Revocation.cs index 2f89cf1d..1668b86d 100644 --- a/src/OpenIddict.Client/OpenIddictClientHandlers.Revocation.cs +++ b/src/OpenIddict.Client/OpenIddictClientHandlers.Revocation.cs @@ -41,10 +41,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleRevocationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); foreach (var parameter in context.Response.GetParameters()) { @@ -98,10 +95,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleRevocationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (!string.IsNullOrEmpty(context.Response.Error)) { diff --git a/src/OpenIddict.Client/OpenIddictClientHandlers.Session.cs b/src/OpenIddict.Client/OpenIddictClientHandlers.Session.cs index 0886ba53..84518dae 100644 --- a/src/OpenIddict.Client/OpenIddictClientHandlers.Session.cs +++ b/src/OpenIddict.Client/OpenIddictClientHandlers.Session.cs @@ -63,10 +63,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessSignOutContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new PrepareEndSessionRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -107,10 +104,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessSignOutContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ApplyEndSessionRequestContext(context.Transaction) { @@ -161,10 +155,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ApplyEndSessionRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Don't overwrite the endpoint URI if it was already set. if (!string.IsNullOrEmpty(context.EndSessionEndpoint)) @@ -208,10 +199,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ExtractPostLogoutRedirectionRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -270,10 +258,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ValidatePostLogoutRedirectionRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -326,10 +311,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new HandlePostLogoutRedirectionRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -383,10 +365,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ApplyPostLogoutRedirectionResponseContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -431,10 +410,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ValidatePostLogoutRedirectionRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ProcessAuthenticationContext(context.Transaction); await _dispatcher.DispatchAsync(notification); diff --git a/src/OpenIddict.Client/OpenIddictClientHandlers.Userinfo.cs b/src/OpenIddict.Client/OpenIddictClientHandlers.Userinfo.cs index 3fca98b4..f3bcbf2e 100644 --- a/src/OpenIddict.Client/OpenIddictClientHandlers.Userinfo.cs +++ b/src/OpenIddict.Client/OpenIddictClientHandlers.Userinfo.cs @@ -44,10 +44,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleUserInfoResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Ignore the response instance if a userinfo token was extracted. if (!string.IsNullOrEmpty(context.UserInfoToken)) @@ -110,10 +107,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleUserInfoResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // For more information, see https://openid.net/specs/openid-connect-core-1_0.html#UserInfoError. if (!string.IsNullOrEmpty(context.Response.Error)) @@ -156,10 +150,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(HandleUserInfoResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Registration.Issuer is { IsAbsoluteUri: true }, SR.GetResourceString(SR.ID4013)); diff --git a/src/OpenIddict.Client/OpenIddictClientHandlers.cs b/src/OpenIddict.Client/OpenIddictClientHandlers.cs index 5be35496..7919982b 100644 --- a/src/OpenIddict.Client/OpenIddictClientHandlers.cs +++ b/src/OpenIddict.Client/OpenIddictClientHandlers.cs @@ -238,10 +238,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If the base or request URIs couldn't be resolved, don't try to infer the endpoint type. if (context is not { BaseUri.IsAbsoluteUri: true, RequestUri.IsAbsoluteUri: true }) @@ -319,10 +316,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Authentication demands can be triggered from the redirection endpoints // to handle authorization/logout callbacks but also from unknown endpoints @@ -445,10 +439,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: this handler only applies to authentication demands triggered from unknown endpoints. // @@ -564,10 +555,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); (context.ExtractStateToken, context.RequireStateToken, @@ -613,10 +601,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.StateToken = context.EndpointType switch { @@ -650,10 +635,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.RequireStateToken && string.IsNullOrEmpty(context.StateToken)) { @@ -693,10 +675,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (string.IsNullOrEmpty(context.StateToken)) { @@ -762,10 +741,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.StateTokenPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -804,10 +780,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.StateTokenPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -855,10 +828,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.StateTokenPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -904,10 +874,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.StateTokenPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -956,10 +923,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.StateTokenPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -1019,10 +983,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.StateTokenPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -1130,10 +1091,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.StateTokenPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -1206,10 +1164,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // To help mitigate mix-up attacks, the identity of the issuer can be returned by // authorization servers that support it as a part of the "iss" parameter, which @@ -1287,10 +1242,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: unlike the redirection endpoint, the post-logout redirection endpoint is not expected // to be called with an error attached (in this case, the error is typically displayed directly @@ -1389,10 +1341,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.StateTokenPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -1441,10 +1390,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); (context.ExtractAuthorizationCode, context.RequireAuthorizationCode, @@ -1530,10 +1476,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.AuthorizationCode = context.EndpointType switch { @@ -1592,10 +1535,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if ((context.RequireAuthorizationCode && string.IsNullOrEmpty(context.AuthorizationCode)) || (context.RequireFrontchannelAccessToken && string.IsNullOrEmpty(context.FrontchannelAccessToken)) || @@ -1637,10 +1577,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (string.IsNullOrEmpty(context.FrontchannelIdentityToken)) { @@ -1707,10 +1644,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.FrontchannelIdentityTokenPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -1836,10 +1770,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.FrontchannelIdentityTokenPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -1881,10 +1812,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.FrontchannelIdentityTokenPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -1927,10 +1855,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: the OpenID Connect specification relies on nonces as a way to detect and // prevent replay attacks by binding the returned identity token(s) to a specific @@ -2028,10 +1953,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.FrontchannelIdentityTokenPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -2160,10 +2082,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (string.IsNullOrEmpty(context.FrontchannelAccessToken)) { @@ -2234,10 +2153,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (string.IsNullOrEmpty(context.AuthorizationCode)) { @@ -2300,10 +2216,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.SendTokenRequest = context.GrantType switch { @@ -2355,10 +2268,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If an explicit client authentication method was attached, don't overwrite it. if (!string.IsNullOrEmpty(context.TokenEndpointClientAuthenticationMethod)) @@ -2418,10 +2328,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If the URI of the token endpoint wasn't explicitly set at // this stage, try to extract it from the server configuration. @@ -2462,10 +2369,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Attach a new request instance if necessary. context.TokenRequest ??= new OpenIddictRequest(); @@ -2585,10 +2489,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); (context.GenerateClientAssertion, context.IncludeClientAssertion) = context.TokenEndpointClientAuthenticationMethod switch @@ -2624,10 +2525,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Registration.Issuer is { IsAbsoluteUri: true }, SR.GetResourceString(SR.ID4013)); @@ -2701,10 +2599,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new GenerateTokenContext(context.Transaction) { @@ -2768,10 +2663,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.TokenRequest is not null, SR.GetResourceString(SR.ID4008)); @@ -2821,10 +2713,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.TokenRequest is not null, SR.GetResourceString(SR.ID4008)); @@ -2873,10 +2762,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); (context.ExtractBackchannelAccessToken, context.RequireBackchannelAccessToken, @@ -3034,10 +2920,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.TokenResponse is not null, SR.GetResourceString(SR.ID4007)); @@ -3097,10 +2980,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if ((context.RequireBackchannelAccessToken && string.IsNullOrEmpty(context.BackchannelAccessToken)) || (context.RequireBackchannelIdentityToken && string.IsNullOrEmpty(context.BackchannelIdentityToken)) || @@ -3143,10 +3023,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (string.IsNullOrEmpty(context.BackchannelIdentityToken)) { @@ -3213,10 +3090,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.BackchannelIdentityTokenPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -3342,10 +3216,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.BackchannelIdentityTokenPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -3387,10 +3258,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.BackchannelIdentityTokenPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -3433,10 +3301,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: the OpenID Connect specification relies on nonces as a way to detect and // prevent replay attacks by binding the returned identity token(s) to a specific @@ -3534,10 +3399,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.BackchannelIdentityTokenPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); Debug.Assert(!string.IsNullOrEmpty(context.BackchannelAccessToken), SR.GetResourceString(SR.ID4010)); @@ -3630,10 +3492,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (string.IsNullOrEmpty(context.BackchannelAccessToken)) { @@ -3702,10 +3561,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (string.IsNullOrEmpty(context.IssuedToken)) { @@ -3776,10 +3632,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (string.IsNullOrEmpty(context.RefreshToken)) { @@ -3842,10 +3695,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.SendUserInfoRequest = context.GrantType switch { @@ -3942,10 +3792,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // By default, assume that sending the access token is enough and doesn't require using // an additional token binding method. To support advanced scenarios like mTLS-protected @@ -3975,10 +3822,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If the URI of the userinfo endpoint wasn't explicitly set at // this stage, try to extract it from the server configuration. @@ -4021,10 +3865,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Attach a new request instance if necessary. context.UserInfoRequest ??= new OpenIddictRequest(); @@ -4074,10 +3915,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.UserInfoRequest is not null, SR.GetResourceString(SR.ID4008)); @@ -4131,10 +3969,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); (context.ExtractUserInfoToken, context.RequireUserInfoToken, @@ -4188,10 +4023,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.RequireUserInfoToken && string.IsNullOrEmpty(context.UserInfoToken)) { @@ -4231,10 +4063,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (string.IsNullOrEmpty(context.UserInfoToken)) { @@ -4299,10 +4128,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.UserInfoTokenPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -4352,10 +4178,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.UserInfoTokenPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -4423,10 +4246,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Registration.Issuer is { IsAbsoluteUri: true }, SR.GetResourceString(SR.ID4013)); @@ -4521,10 +4341,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Registration.Issuer is { IsAbsoluteUri: true }, SR.GetResourceString(SR.ID4013)); @@ -4582,10 +4399,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.EndpointType is not OpenIddictClientEndpointType.Unknown) { @@ -4717,10 +4531,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.Registration ??= context switch { @@ -4811,10 +4622,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If an explicit grant or response type was specified, don't overwrite it. if (!string.IsNullOrEmpty(context.GrantType) || !string.IsNullOrEmpty(context.ResponseType)) @@ -5038,10 +4846,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // In OpenIddict, per-authorization demand values are stored in an encrypted and signed token // called "state token", that allows flowing per-authorization demand data like the issuer @@ -5083,10 +4888,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Principal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -5113,10 +4915,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.ClientId ??= context.Registration.ClientId switch { @@ -5156,10 +4955,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Don't overwrite the redirect_uri if one was already explicitly attached. if (context.RedirectUri is not null) @@ -5209,10 +5005,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If an explicit set of scopes was specified, don't overwrite it. if (context.Scopes.Count is > 0) @@ -5256,10 +5049,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Generate a new crypto-secure random identifier that will // be used as the non-guessable part of the state token. @@ -5288,10 +5078,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Generate a new crypto-secure random identifier that will be used as the nonce. // @@ -5328,10 +5115,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Don't attach a code challenge method if no authorization code is requested as some implementations // (like OpenIddict server) are known to eagerly block authorization requests that specify an invalid @@ -5425,10 +5209,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If an explicit response type was specified, don't overwrite it. if (!string.IsNullOrEmpty(context.ResponseMode)) @@ -5493,10 +5274,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Registration.Issuer is { IsAbsoluteUri: true }, SR.GetResourceString(SR.ID4013)); Debug.Assert(context.Principal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -5618,10 +5396,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new GenerateTokenContext(context.Transaction) { @@ -5679,10 +5454,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: while the exact order of the parameters has typically no effect on how requests // are handled by an authorization server, client_id and redirect_uri are deliberately @@ -5759,10 +5531,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.Parameters.Count is > 0) { @@ -5794,10 +5563,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.SendDeviceAuthorizationRequest = context.GrantType is GrantTypes.DeviceCode; @@ -5825,10 +5591,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If an explicit client authentication method was attached, don't overwrite it. if (!string.IsNullOrEmpty(context.DeviceAuthorizationEndpointClientAuthenticationMethod)) @@ -5896,10 +5659,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If the URI of the device authorization endpoint endpoint wasn't explicitly // set at this stage, try to extract it from the server configuration. @@ -5940,10 +5700,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Attach a new request instance if necessary. context.DeviceAuthorizationRequest ??= new OpenIddictRequest(); @@ -5978,10 +5735,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.SendPushedAuthorizationRequest = context.GrantType switch { @@ -6025,10 +5779,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If an explicit client authentication method was attached, don't overwrite it. if (!string.IsNullOrEmpty(context.PushedAuthorizationEndpointClientAuthenticationMethod)) @@ -6097,10 +5848,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If the URI of the pushed authorization endpoint endpoint wasn't // explicitly set at this stage, try to extract it from the server configuration. @@ -6141,10 +5889,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Attach a new request instance if necessary. context.PushedAuthorizationRequest ??= new OpenIddictRequest(); @@ -6178,10 +5923,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); (context.GenerateClientAssertion, context.IncludeClientAssertion) = context switch { @@ -6217,10 +5959,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Registration.Issuer is { IsAbsoluteUri: true }, SR.GetResourceString(SR.ID4013)); @@ -6280,10 +6019,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new GenerateTokenContext(context.Transaction) { @@ -6347,10 +6083,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.DeviceAuthorizationRequest is not null, SR.GetResourceString(SR.ID4008)); @@ -6400,10 +6133,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.DeviceAuthorizationRequest is not null, SR.GetResourceString(SR.ID4008)); @@ -6452,10 +6182,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); (context.ExtractDeviceCode, context.RequireDeviceCode, @@ -6510,10 +6237,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.DeviceAuthorizationResponse is not null, SR.GetResourceString(SR.ID4007)); @@ -6545,10 +6269,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if ((context.RequireDeviceCode && string.IsNullOrEmpty(context.DeviceCode)) || (context.RequireUserCode && string.IsNullOrEmpty(context.UserCode))) @@ -6583,10 +6304,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.PushedAuthorizationRequest is not null, SR.GetResourceString(SR.ID4008)); @@ -6631,10 +6349,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (!context.SendPushedAuthorizationRequest && context.Configuration.RequirePushedAuthorizationRequests is true) { @@ -6668,10 +6383,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.PushedAuthorizationRequest is not null, SR.GetResourceString(SR.ID4008)); @@ -6720,10 +6432,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); (context.ExtractRequestToken, context.RequireRequestToken, @@ -6765,10 +6474,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.PushedAuthorizationResponse is not null, SR.GetResourceString(SR.ID4007)); @@ -6799,10 +6505,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.RequireRequestToken && string.IsNullOrEmpty(context.RequestToken)) { @@ -6837,10 +6540,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.Request.RequestUri = context.RequestToken; @@ -6869,10 +6569,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.PushedAuthorizationRequest is not null, SR.GetResourceString(SR.ID4008)); @@ -6906,10 +6603,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessIntrospectionContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (string.IsNullOrEmpty(context.Token)) { @@ -6952,10 +6646,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessIntrospectionContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.Registration ??= context switch { @@ -7044,10 +6735,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessIntrospectionContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.ClientId ??= context.Registration.ClientId; @@ -7073,10 +6761,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessIntrospectionContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.SendIntrospectionRequest = true; @@ -7104,10 +6789,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessIntrospectionContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If an explicit client authentication method was attached, don't overwrite it. if (!string.IsNullOrEmpty(context.IntrospectionEndpointClientAuthenticationMethod)) @@ -7167,10 +6849,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessIntrospectionContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If the URI of the introspection endpoint endpoint wasn't explicitly // set at this stage, try to extract it from the server configuration. @@ -7211,10 +6890,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessIntrospectionContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Attach a new request instance if necessary. context.IntrospectionRequest ??= new OpenIddictRequest(); @@ -7245,10 +6921,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessIntrospectionContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); (context.GenerateClientAssertion, context.IncludeClientAssertion) = context.IntrospectionEndpointClientAuthenticationMethod switch @@ -7284,10 +6957,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessIntrospectionContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Registration.Issuer is { IsAbsoluteUri: true }, SR.GetResourceString(SR.ID4013)); @@ -7347,10 +7017,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessIntrospectionContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new GenerateTokenContext(context.Transaction) { @@ -7414,10 +7081,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessIntrospectionContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.IntrospectionRequest is not null, SR.GetResourceString(SR.ID4008)); @@ -7467,10 +7131,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessIntrospectionContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Registration.Issuer is { IsAbsoluteUri: true }, SR.GetResourceString(SR.ID4013)); Debug.Assert(context.IntrospectionRequest is not null, SR.GetResourceString(SR.ID4008)); @@ -7529,10 +7190,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessIntrospectionContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Registration.Issuer is { IsAbsoluteUri: true }, SR.GetResourceString(SR.ID4013)); Debug.Assert(context.Principal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -7577,10 +7235,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessRevocationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (string.IsNullOrEmpty(context.Token)) { @@ -7623,10 +7278,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessRevocationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.Registration ??= context switch { @@ -7715,10 +7367,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessRevocationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.ClientId ??= context.Registration.ClientId; @@ -7744,10 +7393,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessRevocationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.SendRevocationRequest = true; @@ -7775,10 +7421,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessRevocationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If an explicit client authentication method was attached, don't overwrite it. if (!string.IsNullOrEmpty(context.RevocationEndpointClientAuthenticationMethod)) @@ -7838,10 +7481,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessRevocationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If the URI of the revocation endpoint endpoint wasn't explicitly // set at this stage, try to extract it from the server configuration. @@ -7882,10 +7522,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessRevocationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Attach a new request instance if necessary. context.RevocationRequest ??= new OpenIddictRequest(); @@ -7916,10 +7553,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessRevocationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); (context.GenerateClientAssertion, context.IncludeClientAssertion) = context.RevocationEndpointClientAuthenticationMethod switch @@ -7955,10 +7589,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessRevocationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Registration.Issuer is { IsAbsoluteUri: true }, SR.GetResourceString(SR.ID4013)); @@ -8018,10 +7649,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessRevocationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new GenerateTokenContext(context.Transaction) { @@ -8085,10 +7713,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessRevocationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.RevocationRequest is not null, SR.GetResourceString(SR.ID4008)); @@ -8138,10 +7763,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessRevocationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.RevocationRequest is not null, SR.GetResourceString(SR.ID4008)); @@ -8191,10 +7813,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessSignOutContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.EndpointType is not OpenIddictClientEndpointType.Unknown) { @@ -8293,10 +7912,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessSignOutContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.Registration ??= context switch { @@ -8385,10 +8001,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessSignOutContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: the client_id parameter is optional. context.ClientId ??= context.Registration.ClientId; @@ -8414,10 +8027,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessSignOutContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Don't overwrite the post_logout_redirect_uri if one was already explicitly attached. if (context.PostLogoutRedirectUri is not null) @@ -8465,10 +8075,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessSignOutContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); (context.GenerateStateToken, context.IncludeStateToken) = (true, true); @@ -8494,10 +8101,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessSignOutContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Principal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -8524,10 +8128,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessSignOutContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Generate a new crypto-secure random identifier that will // be used as the non-guessable part of the state token. @@ -8555,10 +8156,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessSignOutContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Generate a new crypto-secure random identifier that will be used as the nonce. context.Nonce = Base64UrlEncoder.Encode(OpenIddictHelpers.CreateRandomArray(size: 256)); @@ -8587,10 +8185,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessSignOutContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Registration.Issuer is { IsAbsoluteUri: true }, SR.GetResourceString(SR.ID4013)); Debug.Assert(context.Principal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -8691,10 +8286,7 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessSignOutContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new GenerateTokenContext(context.Transaction) { @@ -8751,10 +8343,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessSignOutContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: while the exact order of the parameters has typically no effect on how requests // are handled by an authorization server, client_id and post_logout_redirect_uri are @@ -8794,10 +8383,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessSignOutContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.Parameters.Count is > 0) { @@ -8829,10 +8415,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessErrorContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.Response.Error = context.Error; context.Response.ErrorDescription = context.ErrorDescription; @@ -8861,10 +8444,7 @@ public static partial class OpenIddictClientHandlers /// public ValueTask HandleAsync(ProcessErrorContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.Parameters.Count is > 0) { diff --git a/src/OpenIddict.Client/OpenIddictClientHelpers.cs b/src/OpenIddict.Client/OpenIddictClientHelpers.cs index 3bbce2e9..cf2948b6 100644 --- a/src/OpenIddict.Client/OpenIddictClientHelpers.cs +++ b/src/OpenIddict.Client/OpenIddictClientHelpers.cs @@ -21,15 +21,8 @@ public static class OpenIddictClientHelpers public static TProperty? GetProperty( this OpenIddictClientTransaction transaction, string name) where TProperty : class { - if (transaction is null) - { - throw new ArgumentNullException(nameof(transaction)); - } - - if (string.IsNullOrEmpty(name)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0106), nameof(name)); - } + ArgumentNullException.ThrowIfNull(transaction); + ArgumentException.ThrowIfNullOrEmpty(name); if (transaction.Properties.TryGetValue(name, out var property) && property is TProperty result) { @@ -51,15 +44,8 @@ public static class OpenIddictClientHelpers this OpenIddictClientTransaction transaction, string name, TProperty? value) where TProperty : class { - if (transaction is null) - { - throw new ArgumentNullException(nameof(transaction)); - } - - if (string.IsNullOrEmpty(name)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0106), nameof(name)); - } + ArgumentNullException.ThrowIfNull(transaction); + ArgumentException.ThrowIfNullOrEmpty(name); if (value is null) { diff --git a/src/OpenIddict.Client/OpenIddictClientRetriever.cs b/src/OpenIddict.Client/OpenIddictClientRetriever.cs index ecf3a97c..7b777fe3 100644 --- a/src/OpenIddict.Client/OpenIddictClientRetriever.cs +++ b/src/OpenIddict.Client/OpenIddictClientRetriever.cs @@ -38,10 +38,7 @@ public sealed class OpenIddictClientRetriever : IConfigurationRetriever IConfigurationRetriever.GetConfigurationAsync( [StringSyntax(StringSyntaxAttribute.Uri)] string address, IDocumentRetriever retriever, CancellationToken cancel) { - if (string.IsNullOrEmpty(address)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0143), nameof(address)); - } + ArgumentException.ThrowIfNullOrEmpty(address); if (!Uri.TryCreate(address, UriKind.Absolute, out Uri? uri) || OpenIddictHelpers.IsImplicitFileUri(uri)) { diff --git a/src/OpenIddict.Client/OpenIddictClientService.cs b/src/OpenIddict.Client/OpenIddictClientService.cs index c5bfe3ab..25a4e864 100644 --- a/src/OpenIddict.Client/OpenIddictClientService.cs +++ b/src/OpenIddict.Client/OpenIddictClientService.cs @@ -68,10 +68,7 @@ public class OpenIddictClientService public virtual ValueTask GetClientRegistrationByIssuerAsync( Uri uri, CancellationToken cancellationToken = default) { - if (uri is null) - { - throw new ArgumentNullException(nameof(uri)); - } + ArgumentNullException.ThrowIfNull(uri); if (cancellationToken.IsCancellationRequested) { @@ -108,10 +105,7 @@ public class OpenIddictClientService public virtual ValueTask GetClientRegistrationByProviderNameAsync( string name, CancellationToken cancellationToken = default) { - if (string.IsNullOrEmpty(name)) - { - throw new ArgumentException(SR.FormatID0366(nameof(name)), nameof(name)); - } + ArgumentException.ThrowIfNullOrEmpty(name); if (cancellationToken.IsCancellationRequested) { @@ -146,10 +140,7 @@ public class OpenIddictClientService public virtual ValueTask GetClientRegistrationByIdAsync( string identifier, CancellationToken cancellationToken = default) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.FormatID0366(nameof(identifier)), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); if (cancellationToken.IsCancellationRequested) { @@ -179,10 +170,7 @@ public class OpenIddictClientService public virtual async ValueTask GetServerConfigurationByIssuerAsync( Uri uri, CancellationToken cancellationToken = default) { - if (uri is null) - { - throw new ArgumentNullException(nameof(uri)); - } + ArgumentNullException.ThrowIfNull(uri); var registration = await GetClientRegistrationByIssuerAsync(uri, cancellationToken); if (registration.ConfigurationManager is null) @@ -212,10 +200,7 @@ public class OpenIddictClientService public virtual async ValueTask GetServerConfigurationByProviderNameAsync( string name, CancellationToken cancellationToken = default) { - if (string.IsNullOrEmpty(name)) - { - throw new ArgumentException(SR.FormatID0366(nameof(name)), nameof(name)); - } + ArgumentException.ThrowIfNullOrEmpty(name); var registration = await GetClientRegistrationByProviderNameAsync(name, cancellationToken); if (registration.ConfigurationManager is null) @@ -242,10 +227,7 @@ public class OpenIddictClientService public virtual async ValueTask GetServerConfigurationByRegistrationIdAsync( string identifier, CancellationToken cancellationToken = default) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.FormatID0366(nameof(identifier)), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var registration = await GetClientRegistrationByIdAsync(identifier, cancellationToken); if (registration.ConfigurationManager is null) @@ -271,10 +253,7 @@ public class OpenIddictClientService /// The interactive authentication result. public async ValueTask AuthenticateInteractivelyAsync(InteractiveAuthenticationRequest request) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } + ArgumentNullException.ThrowIfNull(request); request.CancellationToken.ThrowIfCancellationRequested(); @@ -344,10 +323,7 @@ public class OpenIddictClientService /// The interactive challenge result. public async ValueTask ChallengeInteractivelyAsync(InteractiveChallengeRequest request) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } + ArgumentNullException.ThrowIfNull(request); request.CancellationToken.ThrowIfCancellationRequested(); @@ -430,10 +406,7 @@ public class OpenIddictClientService public async ValueTask AuthenticateWithClientCredentialsAsync( ClientCredentialsAuthenticationRequest request) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } + ArgumentNullException.ThrowIfNull(request); request.CancellationToken.ThrowIfCancellationRequested(); @@ -515,10 +488,7 @@ public class OpenIddictClientService [EditorBrowsable(EditorBrowsableState.Advanced)] public async ValueTask AuthenticateWithCustomGrantAsync(CustomGrantAuthenticationRequest request) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } + ArgumentNullException.ThrowIfNull(request); // Prevent well-known/non-custom grant types from being used with this API. if (request.GrantType is GrantTypes.AuthorizationCode or GrantTypes.ClientCredentials or @@ -608,10 +578,7 @@ public class OpenIddictClientService /// The device authentication result. public async ValueTask AuthenticateWithDeviceAsync(DeviceAuthenticationRequest request) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } + ArgumentNullException.ThrowIfNull(request); using var source = CancellationTokenSource.CreateLinkedTokenSource(request.CancellationToken); source.CancelAfter(request.Timeout); @@ -725,10 +692,7 @@ public class OpenIddictClientService /// The device challenge result. public async ValueTask ChallengeUsingDeviceAsync(DeviceChallengeRequest request) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } + ArgumentNullException.ThrowIfNull(request); request.CancellationToken.ThrowIfCancellationRequested(); @@ -808,10 +772,7 @@ public class OpenIddictClientService /// The resource owner password credentials authentication result. public async ValueTask AuthenticateWithPasswordAsync(PasswordAuthenticationRequest request) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } + ArgumentNullException.ThrowIfNull(request); request.CancellationToken.ThrowIfCancellationRequested(); @@ -897,10 +858,7 @@ public class OpenIddictClientService public async ValueTask AuthenticateWithTokenExchangeAsync( TokenExchangeAuthenticationRequest request) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } + ArgumentNullException.ThrowIfNull(request); request.CancellationToken.ThrowIfCancellationRequested(); @@ -986,10 +944,7 @@ public class OpenIddictClientService public async ValueTask AuthenticateWithRefreshTokenAsync( RefreshTokenAuthenticationRequest request) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } + ArgumentNullException.ThrowIfNull(request); request.CancellationToken.ThrowIfCancellationRequested(); @@ -1073,10 +1028,7 @@ public class OpenIddictClientService /// The introspection result. public async ValueTask IntrospectTokenAsync(IntrospectionRequest request) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } + ArgumentNullException.ThrowIfNull(request); request.CancellationToken.ThrowIfCancellationRequested(); @@ -1136,10 +1088,7 @@ public class OpenIddictClientService /// The revocation result. public async ValueTask RevokeTokenAsync(RevocationRequest request) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } + ArgumentNullException.ThrowIfNull(request); request.CancellationToken.ThrowIfCancellationRequested(); @@ -1201,15 +1150,8 @@ public class OpenIddictClientService internal async ValueTask GetConfigurationAsync( OpenIddictClientRegistration registration, Uri uri, CancellationToken cancellationToken = default) { - if (registration is null) - { - throw new ArgumentNullException(nameof(uri)); - } - - if (uri is null) - { - throw new ArgumentNullException(nameof(uri)); - } + ArgumentNullException.ThrowIfNull(registration); + ArgumentNullException.ThrowIfNull(uri); if (!uri.IsAbsoluteUri || OpenIddictHelpers.IsImplicitFileUri(uri)) { @@ -1338,10 +1280,7 @@ public class OpenIddictClientService /// The interactive sign-out result. public async ValueTask SignOutInteractivelyAsync(InteractiveSignOutRequest request) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } + ArgumentNullException.ThrowIfNull(request); request.CancellationToken.ThrowIfCancellationRequested(); @@ -1407,15 +1346,8 @@ public class OpenIddictClientService internal async ValueTask GetSecurityKeysAsync( OpenIddictClientRegistration registration, Uri uri, CancellationToken cancellationToken = default) { - if (registration is null) - { - throw new ArgumentNullException(nameof(registration)); - } - - if (uri is null) - { - throw new ArgumentNullException(nameof(uri)); - } + ArgumentNullException.ThrowIfNull(registration); + ArgumentNullException.ThrowIfNull(uri); if (!uri.IsAbsoluteUri || OpenIddictHelpers.IsImplicitFileUri(uri)) { @@ -1552,25 +1484,10 @@ public class OpenIddictClientService OpenIddictClientRegistration registration, OpenIddictConfiguration configuration, OpenIddictRequest request, Uri uri, string? method, CancellationToken cancellationToken = default) { - if (registration is null) - { - throw new ArgumentNullException(nameof(registration)); - } - - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } - - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } - - if (uri is null) - { - throw new ArgumentNullException(nameof(uri)); - } + ArgumentNullException.ThrowIfNull(registration); + ArgumentNullException.ThrowIfNull(configuration); + ArgumentNullException.ThrowIfNull(request); + ArgumentNullException.ThrowIfNull(uri); if (!uri.IsAbsoluteUri || OpenIddictHelpers.IsImplicitFileUri(uri)) { @@ -1710,20 +1627,9 @@ public class OpenIddictClientService OpenIddictClientRegistration registration, OpenIddictConfiguration configuration, OpenIddictRequest request, Uri uri, string? method, CancellationToken cancellationToken = default) { - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } - - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } - - if (uri is null) - { - throw new ArgumentNullException(nameof(uri)); - } + ArgumentNullException.ThrowIfNull(configuration); + ArgumentNullException.ThrowIfNull(request); + ArgumentNullException.ThrowIfNull(uri); if (!uri.IsAbsoluteUri || OpenIddictHelpers.IsImplicitFileUri(uri)) { @@ -1865,25 +1771,10 @@ public class OpenIddictClientService OpenIddictClientRegistration registration, OpenIddictConfiguration configuration, OpenIddictRequest request, Uri uri, string? method, CancellationToken cancellationToken = default) { - if (registration is null) - { - throw new ArgumentNullException(nameof(registration)); - } - - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } - - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } - - if (uri is null) - { - throw new ArgumentNullException(nameof(uri)); - } + ArgumentNullException.ThrowIfNull(registration); + ArgumentNullException.ThrowIfNull(configuration); + ArgumentNullException.ThrowIfNull(request); + ArgumentNullException.ThrowIfNull(uri); if (!uri.IsAbsoluteUri || OpenIddictHelpers.IsImplicitFileUri(uri)) { @@ -2023,20 +1914,9 @@ public class OpenIddictClientService OpenIddictClientRegistration registration, OpenIddictConfiguration configuration, OpenIddictRequest request, Uri uri, string? method, CancellationToken cancellationToken = default) { - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } - - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } - - if (uri is null) - { - throw new ArgumentNullException(nameof(uri)); - } + ArgumentNullException.ThrowIfNull(configuration); + ArgumentNullException.ThrowIfNull(request); + ArgumentNullException.ThrowIfNull(uri); if (!uri.IsAbsoluteUri || OpenIddictHelpers.IsImplicitFileUri(uri)) { @@ -2176,25 +2056,10 @@ public class OpenIddictClientService OpenIddictClientRegistration registration, OpenIddictConfiguration configuration, OpenIddictRequest request, Uri uri, string? method, CancellationToken cancellationToken = default) { - if (registration is null) - { - throw new ArgumentNullException(nameof(registration)); - } - - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } - - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } - - if (uri is null) - { - throw new ArgumentNullException(nameof(uri)); - } + ArgumentNullException.ThrowIfNull(registration); + ArgumentNullException.ThrowIfNull(configuration); + ArgumentNullException.ThrowIfNull(request); + ArgumentNullException.ThrowIfNull(uri); if (!uri.IsAbsoluteUri || OpenIddictHelpers.IsImplicitFileUri(uri)) { @@ -2334,20 +2199,9 @@ public class OpenIddictClientService OpenIddictClientRegistration registration, OpenIddictConfiguration configuration, OpenIddictRequest request, Uri uri, HashSet methods, CancellationToken cancellationToken = default) { - if (registration is null) - { - throw new ArgumentNullException(nameof(registration)); - } - - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } - - if (uri is null) - { - throw new ArgumentNullException(nameof(uri)); - } + ArgumentNullException.ThrowIfNull(registration); + ArgumentNullException.ThrowIfNull(configuration); + ArgumentNullException.ThrowIfNull(uri); if (!uri.IsAbsoluteUri || OpenIddictHelpers.IsImplicitFileUri(uri)) { diff --git a/src/OpenIddict.Core/Caches/OpenIddictApplicationCache.cs b/src/OpenIddict.Core/Caches/OpenIddictApplicationCache.cs index 1620026b..58cedd0b 100644 --- a/src/OpenIddict.Core/Caches/OpenIddictApplicationCache.cs +++ b/src/OpenIddict.Core/Caches/OpenIddictApplicationCache.cs @@ -45,10 +45,7 @@ public sealed class OpenIddictApplicationCache : IOpenIddictApplic /// public async ValueTask AddAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); _cache.Remove(new { @@ -107,10 +104,7 @@ public sealed class OpenIddictApplicationCache : IOpenIddictApplic /// public ValueTask FindByClientIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var parameters = new { @@ -141,10 +135,7 @@ public sealed class OpenIddictApplicationCache : IOpenIddictApplic /// public ValueTask FindByIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var parameters = new { @@ -176,10 +167,7 @@ public sealed class OpenIddictApplicationCache : IOpenIddictApplic public IAsyncEnumerable FindByPostLogoutRedirectUriAsync( [StringSyntax(StringSyntaxAttribute.Uri)] string uri, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(uri)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0143), nameof(uri)); - } + ArgumentException.ThrowIfNullOrEmpty(uri); return ExecuteAsync(cancellationToken); @@ -218,10 +206,7 @@ public sealed class OpenIddictApplicationCache : IOpenIddictApplic public IAsyncEnumerable FindByRedirectUriAsync( [StringSyntax(StringSyntaxAttribute.Uri)] string uri, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(uri)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0143), nameof(uri)); - } + ArgumentException.ThrowIfNullOrEmpty(uri); return ExecuteAsync(cancellationToken); @@ -259,10 +244,7 @@ public sealed class OpenIddictApplicationCache : IOpenIddictApplic /// public async ValueTask RemoveAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); var identifier = await _store.GetIdAsync(application, cancellationToken); if (string.IsNullOrEmpty(identifier)) @@ -286,10 +268,7 @@ public sealed class OpenIddictApplicationCache : IOpenIddictApplic /// A that can be used to monitor the asynchronous operation. private async ValueTask CreateEntryAsync(object key, TApplication? application, CancellationToken cancellationToken) { - if (key is null) - { - throw new ArgumentNullException(nameof(key)); - } + ArgumentNullException.ThrowIfNull(key); using var entry = _cache.CreateEntry(key); @@ -312,10 +291,7 @@ public sealed class OpenIddictApplicationCache : IOpenIddictApplic /// A that can be used to monitor the asynchronous operation. private async ValueTask CreateEntryAsync(object key, ImmutableArray applications, CancellationToken cancellationToken) { - if (key is null) - { - throw new ArgumentNullException(nameof(key)); - } + ArgumentNullException.ThrowIfNull(key); using var entry = _cache.CreateEntry(key); @@ -341,10 +317,7 @@ public sealed class OpenIddictApplicationCache : IOpenIddictApplic /// private async ValueTask CreateExpirationSignalAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); var identifier = await _store.GetIdAsync(application, cancellationToken); if (string.IsNullOrEmpty(identifier)) diff --git a/src/OpenIddict.Core/Caches/OpenIddictAuthorizationCache.cs b/src/OpenIddict.Core/Caches/OpenIddictAuthorizationCache.cs index 81ac2b79..be80393a 100644 --- a/src/OpenIddict.Core/Caches/OpenIddictAuthorizationCache.cs +++ b/src/OpenIddict.Core/Caches/OpenIddictAuthorizationCache.cs @@ -44,10 +44,7 @@ public sealed class OpenIddictAuthorizationCache : IOpenIddictAu /// public async ValueTask AddAsync(TAuthorization authorization, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); _cache.Remove(new { @@ -104,10 +101,7 @@ public sealed class OpenIddictAuthorizationCache : IOpenIddictAu /// public IAsyncEnumerable FindByApplicationIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); return ExecuteAsync(cancellationToken); @@ -145,10 +139,7 @@ public sealed class OpenIddictAuthorizationCache : IOpenIddictAu /// public ValueTask FindByIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var parameters = new { @@ -179,10 +170,7 @@ public sealed class OpenIddictAuthorizationCache : IOpenIddictAu /// public IAsyncEnumerable FindBySubjectAsync(string subject, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(subject)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0198), nameof(subject)); - } + ArgumentException.ThrowIfNullOrEmpty(subject); return ExecuteAsync(cancellationToken); @@ -220,10 +208,7 @@ public sealed class OpenIddictAuthorizationCache : IOpenIddictAu /// public async ValueTask RemoveAsync(TAuthorization authorization, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); var identifier = await _store.GetIdAsync(authorization, cancellationToken); if (string.IsNullOrEmpty(identifier)) @@ -247,10 +232,7 @@ public sealed class OpenIddictAuthorizationCache : IOpenIddictAu /// A that can be used to monitor the asynchronous operation. private async ValueTask CreateEntryAsync(object key, TAuthorization? authorization, CancellationToken cancellationToken) { - if (key is null) - { - throw new ArgumentNullException(nameof(key)); - } + ArgumentNullException.ThrowIfNull(key); using var entry = _cache.CreateEntry(key); @@ -273,10 +255,7 @@ public sealed class OpenIddictAuthorizationCache : IOpenIddictAu /// A that can be used to monitor the asynchronous operation. private async ValueTask CreateEntryAsync(object key, ImmutableArray authorizations, CancellationToken cancellationToken) { - if (key is null) - { - throw new ArgumentNullException(nameof(key)); - } + ArgumentNullException.ThrowIfNull(key); using var entry = _cache.CreateEntry(key); @@ -302,10 +281,7 @@ public sealed class OpenIddictAuthorizationCache : IOpenIddictAu /// private async ValueTask CreateExpirationSignalAsync(TAuthorization authorization, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); var identifier = await _store.GetIdAsync(authorization, cancellationToken); if (string.IsNullOrEmpty(identifier)) diff --git a/src/OpenIddict.Core/Caches/OpenIddictScopeCache.cs b/src/OpenIddict.Core/Caches/OpenIddictScopeCache.cs index c973dfbc..1bf96a42 100644 --- a/src/OpenIddict.Core/Caches/OpenIddictScopeCache.cs +++ b/src/OpenIddict.Core/Caches/OpenIddictScopeCache.cs @@ -44,10 +44,7 @@ public sealed class OpenIddictScopeCache : IOpenIddictScopeCache /// public async ValueTask AddAsync(TScope scope, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); _cache.Remove(new { @@ -97,10 +94,7 @@ public sealed class OpenIddictScopeCache : IOpenIddictScopeCache /// public ValueTask FindByIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var parameters = new { @@ -131,10 +125,7 @@ public sealed class OpenIddictScopeCache : IOpenIddictScopeCache /// public ValueTask FindByNameAsync(string name, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(name)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0202), nameof(name)); - } + ArgumentException.ThrowIfNullOrEmpty(name); var parameters = new { @@ -188,10 +179,7 @@ public sealed class OpenIddictScopeCache : IOpenIddictScopeCache /// public IAsyncEnumerable FindByResourceAsync(string resource, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(resource)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0062), nameof(resource)); - } + ArgumentException.ThrowIfNullOrEmpty(resource); return ExecuteAsync(cancellationToken); @@ -229,10 +217,7 @@ public sealed class OpenIddictScopeCache : IOpenIddictScopeCache /// public async ValueTask RemoveAsync(TScope scope, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); var identifier = await _store.GetIdAsync(scope, cancellationToken); if (string.IsNullOrEmpty(identifier)) @@ -256,10 +241,7 @@ public sealed class OpenIddictScopeCache : IOpenIddictScopeCache /// A that can be used to monitor the asynchronous operation. private async ValueTask CreateEntryAsync(object key, TScope? scope, CancellationToken cancellationToken) { - if (key is null) - { - throw new ArgumentNullException(nameof(key)); - } + ArgumentNullException.ThrowIfNull(key); using var entry = _cache.CreateEntry(key); @@ -282,10 +264,7 @@ public sealed class OpenIddictScopeCache : IOpenIddictScopeCache /// A that can be used to monitor the asynchronous operation. private async ValueTask CreateEntryAsync(object key, ImmutableArray scopes, CancellationToken cancellationToken) { - if (key is null) - { - throw new ArgumentNullException(nameof(key)); - } + ArgumentNullException.ThrowIfNull(key); using var entry = _cache.CreateEntry(key); @@ -311,10 +290,7 @@ public sealed class OpenIddictScopeCache : IOpenIddictScopeCache /// private async ValueTask CreateExpirationSignalAsync(TScope scope, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); var identifier = await _store.GetIdAsync(scope, cancellationToken); if (string.IsNullOrEmpty(identifier)) diff --git a/src/OpenIddict.Core/Caches/OpenIddictTokenCache.cs b/src/OpenIddict.Core/Caches/OpenIddictTokenCache.cs index 91321968..71edcf8f 100644 --- a/src/OpenIddict.Core/Caches/OpenIddictTokenCache.cs +++ b/src/OpenIddict.Core/Caches/OpenIddictTokenCache.cs @@ -44,10 +44,7 @@ public sealed class OpenIddictTokenCache : IOpenIddictTokenCache /// public async ValueTask AddAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); _cache.Remove(new { @@ -121,10 +118,7 @@ public sealed class OpenIddictTokenCache : IOpenIddictTokenCache /// public IAsyncEnumerable FindByApplicationIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); return ExecuteAsync(cancellationToken); @@ -162,10 +156,7 @@ public sealed class OpenIddictTokenCache : IOpenIddictTokenCache /// public IAsyncEnumerable FindByAuthorizationIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); return ExecuteAsync(cancellationToken); @@ -203,10 +194,7 @@ public sealed class OpenIddictTokenCache : IOpenIddictTokenCache /// public ValueTask FindByIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var parameters = new { @@ -237,10 +225,7 @@ public sealed class OpenIddictTokenCache : IOpenIddictTokenCache /// public ValueTask FindByReferenceIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var parameters = new { @@ -271,10 +256,7 @@ public sealed class OpenIddictTokenCache : IOpenIddictTokenCache /// public IAsyncEnumerable FindBySubjectAsync(string subject, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(subject)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0198), nameof(subject)); - } + ArgumentException.ThrowIfNullOrEmpty(subject); return ExecuteAsync(cancellationToken); @@ -312,10 +294,7 @@ public sealed class OpenIddictTokenCache : IOpenIddictTokenCache /// public async ValueTask RemoveAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); var identifier = await _store.GetIdAsync(token, cancellationToken); if (string.IsNullOrEmpty(identifier)) @@ -339,10 +318,7 @@ public sealed class OpenIddictTokenCache : IOpenIddictTokenCache /// A that can be used to monitor the asynchronous operation. private async ValueTask CreateEntryAsync(object key, TToken? token, CancellationToken cancellationToken) { - if (key is null) - { - throw new ArgumentNullException(nameof(key)); - } + ArgumentNullException.ThrowIfNull(key); using var entry = _cache.CreateEntry(key); @@ -365,10 +341,7 @@ public sealed class OpenIddictTokenCache : IOpenIddictTokenCache /// A that can be used to monitor the asynchronous operation. private async ValueTask CreateEntryAsync(object key, ImmutableArray tokens, CancellationToken cancellationToken) { - if (key is null) - { - throw new ArgumentNullException(nameof(key)); - } + ArgumentNullException.ThrowIfNull(key); using var entry = _cache.CreateEntry(key); @@ -394,10 +367,7 @@ public sealed class OpenIddictTokenCache : IOpenIddictTokenCache /// private async ValueTask CreateExpirationSignalAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); var identifier = await _store.GetIdAsync(token, cancellationToken); if (string.IsNullOrEmpty(identifier)) diff --git a/src/OpenIddict.Core/Managers/OpenIddictApplicationManager.cs b/src/OpenIddict.Core/Managers/OpenIddictApplicationManager.cs index abfcbf1b..bb529182 100644 --- a/src/OpenIddict.Core/Managers/OpenIddictApplicationManager.cs +++ b/src/OpenIddict.Core/Managers/OpenIddictApplicationManager.cs @@ -18,6 +18,7 @@ using Microsoft.Extensions.Options; using Microsoft.IdentityModel.Tokens; using ValidationException = OpenIddict.Abstractions.OpenIddictExceptions.ValidationException; + #if !SUPPORTS_KEY_DERIVATION_WITH_SPECIFIED_HASH_ALGORITHM using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Digests; @@ -100,10 +101,7 @@ public class OpenIddictApplicationManager : IOpenIddictApplication public virtual ValueTask CountAsync( Func, IQueryable> query, CancellationToken cancellationToken = default) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); return Store.CountAsync(query, cancellationToken); } @@ -132,10 +130,7 @@ public class OpenIddictApplicationManager : IOpenIddictApplication /// public virtual async ValueTask CreateAsync(TApplication application, string? secret, CancellationToken cancellationToken = default) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (!string.IsNullOrEmpty(await Store.GetClientSecretAsync(application, cancellationToken))) { @@ -226,10 +221,7 @@ public class OpenIddictApplicationManager : IOpenIddictApplication public virtual async ValueTask CreateAsync( OpenIddictApplicationDescriptor descriptor, CancellationToken cancellationToken = default) { - if (descriptor is null) - { - throw new ArgumentNullException(nameof(descriptor)); - } + ArgumentNullException.ThrowIfNull(descriptor); var application = await Store.InstantiateAsync(cancellationToken) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0208)); @@ -260,10 +252,7 @@ public class OpenIddictApplicationManager : IOpenIddictApplication /// public virtual async ValueTask DeleteAsync(TApplication application, CancellationToken cancellationToken = default) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (!Options.CurrentValue.DisableEntityCaching) { @@ -285,10 +274,7 @@ public class OpenIddictApplicationManager : IOpenIddictApplication public virtual async ValueTask FindByClientIdAsync( string identifier, CancellationToken cancellationToken = default) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var application = Options.CurrentValue.DisableEntityCaching ? await Store.FindByClientIdAsync(identifier, cancellationToken) : @@ -322,10 +308,7 @@ public class OpenIddictApplicationManager : IOpenIddictApplication /// public virtual async ValueTask FindByIdAsync(string identifier, CancellationToken cancellationToken = default) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var application = Options.CurrentValue.DisableEntityCaching ? await Store.FindByIdAsync(identifier, cancellationToken) : @@ -357,10 +340,7 @@ public class OpenIddictApplicationManager : IOpenIddictApplication public virtual IAsyncEnumerable FindByPostLogoutRedirectUriAsync( [StringSyntax(StringSyntaxAttribute.Uri)] string uri, CancellationToken cancellationToken = default) { - if (string.IsNullOrEmpty(uri)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0143), nameof(uri)); - } + ArgumentException.ThrowIfNullOrEmpty(uri); var applications = Options.CurrentValue.DisableEntityCaching ? Store.FindByPostLogoutRedirectUriAsync(uri, cancellationToken) : @@ -399,10 +379,7 @@ public class OpenIddictApplicationManager : IOpenIddictApplication public virtual IAsyncEnumerable FindByRedirectUriAsync( [StringSyntax(StringSyntaxAttribute.Uri)] string uri, CancellationToken cancellationToken = default) { - if (string.IsNullOrEmpty(uri)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0143), nameof(uri)); - } + ArgumentException.ThrowIfNullOrEmpty(uri); var applications = Options.CurrentValue.DisableEntityCaching ? Store.FindByRedirectUriAsync(uri, cancellationToken) : @@ -444,10 +421,7 @@ public class OpenIddictApplicationManager : IOpenIddictApplication public virtual async ValueTask GetApplicationTypeAsync( TApplication application, CancellationToken cancellationToken = default) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); var type = await Store.GetApplicationTypeAsync(application, cancellationToken); if (string.IsNullOrEmpty(type)) @@ -471,10 +445,7 @@ public class OpenIddictApplicationManager : IOpenIddictApplication public virtual ValueTask GetAsync( Func, IQueryable> query, CancellationToken cancellationToken = default) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); return GetAsync(static (applications, query) => query(applications), query, cancellationToken); } @@ -495,10 +466,7 @@ public class OpenIddictApplicationManager : IOpenIddictApplication Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken = default) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); return Store.GetAsync(query, state, cancellationToken); } @@ -515,10 +483,7 @@ public class OpenIddictApplicationManager : IOpenIddictApplication public virtual ValueTask GetClientIdAsync( TApplication application, CancellationToken cancellationToken = default) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); return Store.GetClientIdAsync(application, cancellationToken); } @@ -535,10 +500,7 @@ public class OpenIddictApplicationManager : IOpenIddictApplication public virtual ValueTask GetClientTypeAsync( TApplication application, CancellationToken cancellationToken = default) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); return Store.GetClientTypeAsync(application, cancellationToken); } @@ -555,10 +517,7 @@ public class OpenIddictApplicationManager : IOpenIddictApplication public virtual async ValueTask GetConsentTypeAsync( TApplication application, CancellationToken cancellationToken = default) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); var type = await Store.GetConsentTypeAsync(application, cancellationToken); if (string.IsNullOrEmpty(type)) @@ -581,10 +540,7 @@ public class OpenIddictApplicationManager : IOpenIddictApplication public virtual ValueTask GetDisplayNameAsync( TApplication application, CancellationToken cancellationToken = default) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); return Store.GetDisplayNameAsync(application, cancellationToken); } @@ -601,10 +557,7 @@ public class OpenIddictApplicationManager : IOpenIddictApplication public virtual async ValueTask> GetDisplayNamesAsync( TApplication application, CancellationToken cancellationToken = default) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); var names = await Store.GetDisplayNamesAsync(application, cancellationToken); if (names is not { Count: > 0 }) @@ -626,10 +579,7 @@ public class OpenIddictApplicationManager : IOpenIddictApplication /// public virtual ValueTask GetIdAsync(TApplication application, CancellationToken cancellationToken = default) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); return Store.GetIdAsync(application, cancellationToken); } @@ -645,10 +595,7 @@ public class OpenIddictApplicationManager : IOpenIddictApplication /// public virtual ValueTask GetJsonWebKeySetAsync(TApplication application, CancellationToken cancellationToken = default) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); return Store.GetJsonWebKeySetAsync(application, cancellationToken); } @@ -683,15 +630,8 @@ public class OpenIddictApplicationManager : IOpenIddictApplication public virtual async ValueTask GetLocalizedDisplayNameAsync( TApplication application, CultureInfo culture, CancellationToken cancellationToken = default) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } - - if (culture is null) - { - throw new ArgumentNullException(nameof(culture)); - } + ArgumentNullException.ThrowIfNull(application); + ArgumentNullException.ThrowIfNull(culture); var names = await Store.GetDisplayNamesAsync(application, cancellationToken); if (names is not { Count: > 0 }) @@ -726,10 +666,7 @@ public class OpenIddictApplicationManager : IOpenIddictApplication public virtual ValueTask> GetPermissionsAsync( TApplication application, CancellationToken cancellationToken = default) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); return Store.GetPermissionsAsync(application, cancellationToken); } @@ -746,10 +683,7 @@ public class OpenIddictApplicationManager : IOpenIddictApplication public virtual ValueTask> GetPostLogoutRedirectUrisAsync( TApplication application, CancellationToken cancellationToken = default) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); return Store.GetPostLogoutRedirectUrisAsync(application, cancellationToken); } @@ -766,10 +700,7 @@ public class OpenIddictApplicationManager : IOpenIddictApplication public virtual ValueTask> GetPropertiesAsync( TApplication application, CancellationToken cancellationToken = default) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); return Store.GetPropertiesAsync(application, cancellationToken); } @@ -786,10 +717,7 @@ public class OpenIddictApplicationManager : IOpenIddictApplication public virtual ValueTask> GetRedirectUrisAsync( TApplication application, CancellationToken cancellationToken = default) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); return Store.GetRedirectUrisAsync(application, cancellationToken); } @@ -806,10 +734,7 @@ public class OpenIddictApplicationManager : IOpenIddictApplication public virtual ValueTask> GetRequirementsAsync( TApplication application, CancellationToken cancellationToken = default) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); return Store.GetRequirementsAsync(application, cancellationToken); } @@ -826,10 +751,7 @@ public class OpenIddictApplicationManager : IOpenIddictApplication public virtual ValueTask> GetSettingsAsync( TApplication application, CancellationToken cancellationToken = default) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); return Store.GetSettingsAsync(application, cancellationToken); } @@ -844,15 +766,8 @@ public class OpenIddictApplicationManager : IOpenIddictApplication public virtual async ValueTask HasApplicationTypeAsync( TApplication application, string type, CancellationToken cancellationToken = default) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0209), nameof(type)); - } + ArgumentNullException.ThrowIfNull(application); + ArgumentException.ThrowIfNullOrEmpty(type); return string.Equals(await GetApplicationTypeAsync(application, cancellationToken), type, StringComparison.OrdinalIgnoreCase); } @@ -867,15 +782,8 @@ public class OpenIddictApplicationManager : IOpenIddictApplication public virtual async ValueTask HasClientTypeAsync( TApplication application, string type, CancellationToken cancellationToken = default) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0209), nameof(type)); - } + ArgumentNullException.ThrowIfNull(application); + ArgumentException.ThrowIfNullOrEmpty(type); return string.Equals(await GetClientTypeAsync(application, cancellationToken), type, StringComparison.OrdinalIgnoreCase); } @@ -890,15 +798,8 @@ public class OpenIddictApplicationManager : IOpenIddictApplication public virtual async ValueTask HasConsentTypeAsync( TApplication application, string type, CancellationToken cancellationToken = default) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0210), nameof(type)); - } + ArgumentNullException.ThrowIfNull(application); + ArgumentException.ThrowIfNullOrEmpty(type); return string.Equals(await GetConsentTypeAsync(application, cancellationToken), type, StringComparison.OrdinalIgnoreCase); } @@ -913,15 +814,8 @@ public class OpenIddictApplicationManager : IOpenIddictApplication public virtual async ValueTask HasPermissionAsync( TApplication application, string permission, CancellationToken cancellationToken = default) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } - - if (string.IsNullOrEmpty(permission)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0211), nameof(permission)); - } + ArgumentNullException.ThrowIfNull(application); + ArgumentException.ThrowIfNullOrEmpty(permission); return (await GetPermissionsAsync(application, cancellationToken)).Contains(permission, StringComparer.Ordinal); } @@ -936,15 +830,8 @@ public class OpenIddictApplicationManager : IOpenIddictApplication public virtual async ValueTask HasRequirementAsync( TApplication application, string requirement, CancellationToken cancellationToken = default) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } - - if (string.IsNullOrEmpty(requirement)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0212), nameof(requirement)); - } + ArgumentNullException.ThrowIfNull(application); + ArgumentException.ThrowIfNullOrEmpty(requirement); return (await GetRequirementsAsync(application, cancellationToken)).Contains(requirement, StringComparer.Ordinal); } @@ -970,10 +857,7 @@ public class OpenIddictApplicationManager : IOpenIddictApplication public virtual IAsyncEnumerable ListAsync( Func, IQueryable> query, CancellationToken cancellationToken = default) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); return ListAsync(static (applications, query) => query(applications), query, cancellationToken); } @@ -991,10 +875,7 @@ public class OpenIddictApplicationManager : IOpenIddictApplication Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken = default) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); return Store.ListAsync(query, state, cancellationToken); } @@ -1011,15 +892,8 @@ public class OpenIddictApplicationManager : IOpenIddictApplication public virtual async ValueTask PopulateAsync(TApplication application, OpenIddictApplicationDescriptor descriptor, CancellationToken cancellationToken = default) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } - - if (descriptor is null) - { - throw new ArgumentNullException(nameof(descriptor)); - } + ArgumentNullException.ThrowIfNull(application); + ArgumentNullException.ThrowIfNull(descriptor); await Store.SetApplicationTypeAsync(application, descriptor.ApplicationType, cancellationToken); await Store.SetClientIdAsync(application, descriptor.ClientId, cancellationToken); @@ -1050,15 +924,8 @@ public class OpenIddictApplicationManager : IOpenIddictApplication OpenIddictApplicationDescriptor descriptor, TApplication application, CancellationToken cancellationToken = default) { - if (descriptor is null) - { - throw new ArgumentNullException(nameof(descriptor)); - } - - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(descriptor); + ArgumentNullException.ThrowIfNull(application); descriptor.ApplicationType = await Store.GetApplicationTypeAsync(application, cancellationToken); descriptor.ClientId = await Store.GetClientIdAsync(application, cancellationToken); @@ -1084,13 +951,13 @@ public class OpenIddictApplicationManager : IOpenIddictApplication // Ensure the URI is not null or empty. if (string.IsNullOrEmpty(uri)) { - throw new ArgumentException(SR.GetResourceString(SR.ID0213)); + throw new InvalidOperationException(SR.GetResourceString(SR.ID0213)); } // Ensure the URI is a valid absolute URI. if (!Uri.TryCreate(uri, UriKind.Absolute, out Uri? value) || OpenIddictHelpers.IsImplicitFileUri(value)) { - throw new ArgumentException(SR.GetResourceString(SR.ID0214)); + throw new InvalidOperationException(SR.GetResourceString(SR.ID0214)); } descriptor.PostLogoutRedirectUris.Add(value); @@ -1108,13 +975,13 @@ public class OpenIddictApplicationManager : IOpenIddictApplication // Ensure the URI is not null or empty. if (string.IsNullOrEmpty(uri)) { - throw new ArgumentException(SR.GetResourceString(SR.ID0213)); + throw new InvalidOperationException(SR.GetResourceString(SR.ID0213)); } // Ensure the URI is a valid absolute URI. if (!Uri.TryCreate(uri, UriKind.Absolute, out Uri? value) || OpenIddictHelpers.IsImplicitFileUri(value)) { - throw new ArgumentException(SR.GetResourceString(SR.ID0214)); + throw new InvalidOperationException(SR.GetResourceString(SR.ID0214)); } descriptor.RedirectUris.Add(value); @@ -1137,10 +1004,7 @@ public class OpenIddictApplicationManager : IOpenIddictApplication /// public virtual async ValueTask UpdateAsync(TApplication application, CancellationToken cancellationToken = default) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); var results = await GetValidationResultsAsync(application, cancellationToken); if (results.Any(result => result != ValidationResult.Success)) @@ -1196,10 +1060,7 @@ public class OpenIddictApplicationManager : IOpenIddictApplication /// public virtual async ValueTask UpdateAsync(TApplication application, string? secret, CancellationToken cancellationToken = default) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (string.IsNullOrEmpty(secret)) { @@ -1227,15 +1088,8 @@ public class OpenIddictApplicationManager : IOpenIddictApplication public virtual async ValueTask UpdateAsync(TApplication application, OpenIddictApplicationDescriptor descriptor, CancellationToken cancellationToken = default) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } - - if (descriptor is null) - { - throw new ArgumentNullException(nameof(descriptor)); - } + ArgumentNullException.ThrowIfNull(application); + ArgumentNullException.ThrowIfNull(descriptor); // Store the original client secret for later comparison. var comparand = await Store.GetClientSecretAsync(application, cancellationToken); @@ -1262,10 +1116,7 @@ public class OpenIddictApplicationManager : IOpenIddictApplication public virtual IAsyncEnumerable ValidateAsync( TApplication application, CancellationToken cancellationToken = default) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); return ExecuteAsync(cancellationToken); @@ -1389,14 +1240,8 @@ public class OpenIddictApplicationManager : IOpenIddictApplication public virtual async ValueTask ValidateClientSecretAsync( TApplication application, string secret, CancellationToken cancellationToken = default) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } - if (string.IsNullOrEmpty(secret)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0216), nameof(secret)); - } + ArgumentNullException.ThrowIfNull(application); + ArgumentException.ThrowIfNullOrEmpty(secret); if (await HasClientTypeAsync(application, ClientTypes.Public, cancellationToken)) { @@ -1437,15 +1282,8 @@ public class OpenIddictApplicationManager : IOpenIddictApplication public virtual async ValueTask ValidatePostLogoutRedirectUriAsync(TApplication application, [StringSyntax(StringSyntaxAttribute.Uri)] string uri, CancellationToken cancellationToken = default) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } - - if (string.IsNullOrEmpty(uri)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0143), nameof(uri)); - } + ArgumentNullException.ThrowIfNull(application); + ArgumentException.ThrowIfNullOrEmpty(uri); foreach (var candidate in await Store.GetPostLogoutRedirectUrisAsync(application, cancellationToken)) { @@ -1508,15 +1346,8 @@ public class OpenIddictApplicationManager : IOpenIddictApplication public virtual async ValueTask ValidateRedirectUriAsync(TApplication application, [StringSyntax(StringSyntaxAttribute.Uri)] string uri, CancellationToken cancellationToken = default) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } - - if (string.IsNullOrEmpty(uri)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0143), nameof(uri)); - } + ArgumentNullException.ThrowIfNull(application); + ArgumentException.ThrowIfNullOrEmpty(uri); foreach (var candidate in await Store.GetRedirectUrisAsync(application, cancellationToken)) { @@ -1579,10 +1410,7 @@ public class OpenIddictApplicationManager : IOpenIddictApplication /// protected virtual ValueTask ObfuscateClientSecretAsync(string secret, CancellationToken cancellationToken = default) { - if (string.IsNullOrEmpty(secret)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0216), nameof(secret)); - } + ArgumentException.ThrowIfNullOrEmpty(secret); // Note: the PRF, iteration count, salt length and key length currently all match the default values // used by CryptoHelper and ASP.NET Core Identity but this may change in the future, if necessary. @@ -1644,15 +1472,8 @@ public class OpenIddictApplicationManager : IOpenIddictApplication protected virtual ValueTask ValidateClientSecretAsync( string secret, string comparand, CancellationToken cancellationToken = default) { - if (string.IsNullOrEmpty(secret)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0216), nameof(secret)); - } - - if (string.IsNullOrEmpty(comparand)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0218), nameof(comparand)); - } + ArgumentException.ThrowIfNullOrEmpty(secret); + ArgumentException.ThrowIfNullOrEmpty(comparand); try { diff --git a/src/OpenIddict.Core/Managers/OpenIddictAuthorizationManager.cs b/src/OpenIddict.Core/Managers/OpenIddictAuthorizationManager.cs index 02a5b472..3aaa3063 100644 --- a/src/OpenIddict.Core/Managers/OpenIddictAuthorizationManager.cs +++ b/src/OpenIddict.Core/Managers/OpenIddictAuthorizationManager.cs @@ -90,10 +90,7 @@ public class OpenIddictAuthorizationManager : IOpenIddictAuthori public virtual ValueTask CountAsync( Func, IQueryable> query, CancellationToken cancellationToken = default) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); return Store.CountAsync(query, cancellationToken); } @@ -108,10 +105,7 @@ public class OpenIddictAuthorizationManager : IOpenIddictAuthori /// public virtual async ValueTask CreateAsync(TAuthorization authorization, CancellationToken cancellationToken = default) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); // If no status was explicitly specified, assume that the authorization is valid. if (string.IsNullOrEmpty(await Store.GetStatusAsync(authorization, cancellationToken))) @@ -166,10 +160,7 @@ public class OpenIddictAuthorizationManager : IOpenIddictAuthori public virtual async ValueTask CreateAsync( OpenIddictAuthorizationDescriptor descriptor, CancellationToken cancellationToken = default) { - if (descriptor is null) - { - throw new ArgumentNullException(nameof(descriptor)); - } + ArgumentNullException.ThrowIfNull(descriptor); var authorization = await Store.InstantiateAsync(cancellationToken) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0220)); @@ -214,25 +205,10 @@ public class OpenIddictAuthorizationManager : IOpenIddictAuthori ClaimsPrincipal principal, string subject, string client, string type, ImmutableArray scopes, CancellationToken cancellationToken = default) { - if (principal is null) - { - throw new ArgumentNullException(nameof(principal)); - } - - if (string.IsNullOrEmpty(subject)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0198), nameof(subject)); - } - - if (string.IsNullOrEmpty(client)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0124), nameof(client)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0200), nameof(type)); - } + ArgumentNullException.ThrowIfNull(principal); + ArgumentException.ThrowIfNullOrEmpty(subject); + ArgumentException.ThrowIfNullOrEmpty(client); + ArgumentException.ThrowIfNullOrEmpty(type); var descriptor = new OpenIddictAuthorizationDescriptor { @@ -259,10 +235,7 @@ public class OpenIddictAuthorizationManager : IOpenIddictAuthori /// public virtual async ValueTask DeleteAsync(TAuthorization authorization, CancellationToken cancellationToken = default) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); if (!Options.CurrentValue.DisableEntityCaching) { @@ -331,10 +304,7 @@ public class OpenIddictAuthorizationManager : IOpenIddictAuthori public virtual IAsyncEnumerable FindByApplicationIdAsync( string identifier, CancellationToken cancellationToken = default) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var authorizations = Options.CurrentValue.DisableEntityCaching ? Store.FindByApplicationIdAsync(identifier, cancellationToken) : @@ -374,10 +344,7 @@ public class OpenIddictAuthorizationManager : IOpenIddictAuthori /// public virtual async ValueTask FindByIdAsync(string identifier, CancellationToken cancellationToken = default) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var authorization = Options.CurrentValue.DisableEntityCaching ? await Store.FindByIdAsync(identifier, cancellationToken) : @@ -409,10 +376,7 @@ public class OpenIddictAuthorizationManager : IOpenIddictAuthori public virtual IAsyncEnumerable FindBySubjectAsync( string subject, CancellationToken cancellationToken = default) { - if (string.IsNullOrEmpty(subject)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0198), nameof(subject)); - } + ArgumentException.ThrowIfNullOrEmpty(subject); var authorizations = Options.CurrentValue.DisableEntityCaching ? Store.FindBySubjectAsync(subject, cancellationToken) : @@ -453,10 +417,7 @@ public class OpenIddictAuthorizationManager : IOpenIddictAuthori public virtual ValueTask GetApplicationIdAsync( TAuthorization authorization, CancellationToken cancellationToken = default) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); return Store.GetApplicationIdAsync(authorization, cancellationToken); } @@ -474,10 +435,7 @@ public class OpenIddictAuthorizationManager : IOpenIddictAuthori public virtual ValueTask GetAsync( Func, IQueryable> query, CancellationToken cancellationToken = default) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); return GetAsync(static (authorizations, query) => query(authorizations), query, cancellationToken); } @@ -498,10 +456,7 @@ public class OpenIddictAuthorizationManager : IOpenIddictAuthori Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken = default) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); return Store.GetAsync(query, state, cancellationToken); } @@ -518,10 +473,7 @@ public class OpenIddictAuthorizationManager : IOpenIddictAuthori public virtual ValueTask GetCreationDateAsync( TAuthorization authorization, CancellationToken cancellationToken = default) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); return Store.GetCreationDateAsync(authorization, cancellationToken); } @@ -537,10 +489,7 @@ public class OpenIddictAuthorizationManager : IOpenIddictAuthori /// public virtual ValueTask GetIdAsync(TAuthorization authorization, CancellationToken cancellationToken = default) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); return Store.GetIdAsync(authorization, cancellationToken); } @@ -557,10 +506,7 @@ public class OpenIddictAuthorizationManager : IOpenIddictAuthori public virtual ValueTask> GetPropertiesAsync( TAuthorization authorization, CancellationToken cancellationToken = default) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); return Store.GetPropertiesAsync(authorization, cancellationToken); } @@ -577,10 +523,7 @@ public class OpenIddictAuthorizationManager : IOpenIddictAuthori public virtual ValueTask> GetScopesAsync( TAuthorization authorization, CancellationToken cancellationToken = default) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); return Store.GetScopesAsync(authorization, cancellationToken); } @@ -597,10 +540,7 @@ public class OpenIddictAuthorizationManager : IOpenIddictAuthori public virtual ValueTask GetStatusAsync( TAuthorization authorization, CancellationToken cancellationToken = default) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); return Store.GetStatusAsync(authorization, cancellationToken); } @@ -617,10 +557,7 @@ public class OpenIddictAuthorizationManager : IOpenIddictAuthori public virtual ValueTask GetSubjectAsync( TAuthorization authorization, CancellationToken cancellationToken = default) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); return Store.GetSubjectAsync(authorization, cancellationToken); } @@ -637,10 +574,7 @@ public class OpenIddictAuthorizationManager : IOpenIddictAuthori public virtual ValueTask GetTypeAsync( TAuthorization authorization, CancellationToken cancellationToken = default) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); return Store.GetTypeAsync(authorization, cancellationToken); } @@ -655,10 +589,7 @@ public class OpenIddictAuthorizationManager : IOpenIddictAuthori public virtual async ValueTask HasScopesAsync(TAuthorization authorization, ImmutableArray scopes, CancellationToken cancellationToken = default) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); return (await GetScopesAsync(authorization, cancellationToken)) .ToHashSet(StringComparer.Ordinal) @@ -675,15 +606,8 @@ public class OpenIddictAuthorizationManager : IOpenIddictAuthori public virtual async ValueTask HasStatusAsync(TAuthorization authorization, string status, CancellationToken cancellationToken = default) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } - - if (string.IsNullOrEmpty(status)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0199), nameof(status)); - } + ArgumentNullException.ThrowIfNull(authorization); + ArgumentException.ThrowIfNullOrEmpty(status); return string.Equals(await GetStatusAsync(authorization, cancellationToken), status, StringComparison.OrdinalIgnoreCase); } @@ -698,15 +622,8 @@ public class OpenIddictAuthorizationManager : IOpenIddictAuthori public virtual async ValueTask HasTypeAsync( TAuthorization authorization, string type, CancellationToken cancellationToken = default) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0200), nameof(type)); - } + ArgumentNullException.ThrowIfNull(authorization); + ArgumentException.ThrowIfNullOrEmpty(type); return string.Equals(await GetTypeAsync(authorization, cancellationToken), type, StringComparison.OrdinalIgnoreCase); } @@ -732,10 +649,7 @@ public class OpenIddictAuthorizationManager : IOpenIddictAuthori public virtual IAsyncEnumerable ListAsync( Func, IQueryable> query, CancellationToken cancellationToken = default) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); return ListAsync(static (authorizations, query) => query(authorizations), query, cancellationToken); } @@ -753,10 +667,7 @@ public class OpenIddictAuthorizationManager : IOpenIddictAuthori Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken = default) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); return Store.ListAsync(query, state, cancellationToken); } @@ -773,15 +684,8 @@ public class OpenIddictAuthorizationManager : IOpenIddictAuthori public virtual async ValueTask PopulateAsync(TAuthorization authorization, OpenIddictAuthorizationDescriptor descriptor, CancellationToken cancellationToken = default) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } - - if (descriptor is null) - { - throw new ArgumentNullException(nameof(descriptor)); - } + ArgumentNullException.ThrowIfNull(authorization); + ArgumentNullException.ThrowIfNull(descriptor); await Store.SetApplicationIdAsync(authorization, descriptor.ApplicationId, cancellationToken); await Store.SetCreationDateAsync(authorization, descriptor.CreationDate, cancellationToken); @@ -805,15 +709,8 @@ public class OpenIddictAuthorizationManager : IOpenIddictAuthori OpenIddictAuthorizationDescriptor descriptor, TAuthorization authorization, CancellationToken cancellationToken = default) { - if (descriptor is null) - { - throw new ArgumentNullException(nameof(descriptor)); - } - - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(descriptor); + ArgumentNullException.ThrowIfNull(authorization); descriptor.ApplicationId = await Store.GetApplicationIdAsync(authorization, cancellationToken); descriptor.CreationDate = await Store.GetCreationDateAsync(authorization, cancellationToken); @@ -864,10 +761,7 @@ public class OpenIddictAuthorizationManager : IOpenIddictAuthori /// The number of authorizations associated with the specified application that were marked as revoked. public virtual ValueTask RevokeByApplicationIdAsync(string identifier, CancellationToken cancellationToken = default) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); return Store.RevokeByApplicationIdAsync(identifier, cancellationToken); } @@ -880,10 +774,7 @@ public class OpenIddictAuthorizationManager : IOpenIddictAuthori /// The number of authorizations associated with the specified subject that were marked as revoked. public virtual ValueTask RevokeBySubjectAsync(string subject, CancellationToken cancellationToken = default) { - if (string.IsNullOrEmpty(subject)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(subject)); - } + ArgumentException.ThrowIfNullOrEmpty(subject); return Store.RevokeBySubjectAsync(subject, cancellationToken); } @@ -896,10 +787,7 @@ public class OpenIddictAuthorizationManager : IOpenIddictAuthori /// if the authorization was successfully revoked, otherwise. public virtual async ValueTask TryRevokeAsync(TAuthorization authorization, CancellationToken cancellationToken = default) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); var status = await Store.GetStatusAsync(authorization, cancellationToken); if (string.Equals(status, Statuses.Revoked, StringComparison.OrdinalIgnoreCase)) @@ -943,10 +831,7 @@ public class OpenIddictAuthorizationManager : IOpenIddictAuthori /// public virtual async ValueTask UpdateAsync(TAuthorization authorization, CancellationToken cancellationToken = default) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); var results = await GetValidationResultsAsync(authorization, cancellationToken); if (results.Any(result => result != ValidationResult.Success)) @@ -1001,15 +886,8 @@ public class OpenIddictAuthorizationManager : IOpenIddictAuthori public virtual async ValueTask UpdateAsync(TAuthorization authorization, OpenIddictAuthorizationDescriptor descriptor, CancellationToken cancellationToken = default) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } - - if (descriptor is null) - { - throw new ArgumentNullException(nameof(descriptor)); - } + ArgumentNullException.ThrowIfNull(authorization); + ArgumentNullException.ThrowIfNull(descriptor); await PopulateAsync(authorization, descriptor, cancellationToken); await UpdateAsync(authorization, cancellationToken); @@ -1024,10 +902,7 @@ public class OpenIddictAuthorizationManager : IOpenIddictAuthori public virtual IAsyncEnumerable ValidateAsync( TAuthorization authorization, CancellationToken cancellationToken = default) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); return ExecuteAsync(cancellationToken); diff --git a/src/OpenIddict.Core/Managers/OpenIddictScopeManager.cs b/src/OpenIddict.Core/Managers/OpenIddictScopeManager.cs index aceae557..7c5d6bfa 100644 --- a/src/OpenIddict.Core/Managers/OpenIddictScopeManager.cs +++ b/src/OpenIddict.Core/Managers/OpenIddictScopeManager.cs @@ -89,10 +89,7 @@ public class OpenIddictScopeManager : IOpenIddictScopeManager where TSco public virtual ValueTask CountAsync( Func, IQueryable> query, CancellationToken cancellationToken = default) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); return Store.CountAsync(query, cancellationToken); } @@ -107,10 +104,7 @@ public class OpenIddictScopeManager : IOpenIddictScopeManager where TSco /// public virtual async ValueTask CreateAsync(TScope scope, CancellationToken cancellationToken = default) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); var results = await GetValidationResultsAsync(scope, cancellationToken); if (results.Any(result => result != ValidationResult.Success)) @@ -159,10 +153,7 @@ public class OpenIddictScopeManager : IOpenIddictScopeManager where TSco public virtual async ValueTask CreateAsync( OpenIddictScopeDescriptor descriptor, CancellationToken cancellationToken = default) { - if (descriptor is null) - { - throw new ArgumentNullException(nameof(descriptor)); - } + ArgumentNullException.ThrowIfNull(descriptor); var scope = await Store.InstantiateAsync(cancellationToken) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0223)); @@ -183,10 +174,7 @@ public class OpenIddictScopeManager : IOpenIddictScopeManager where TSco /// public virtual async ValueTask DeleteAsync(TScope scope, CancellationToken cancellationToken = default) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); if (!Options.CurrentValue.DisableEntityCaching) { @@ -207,10 +195,7 @@ public class OpenIddictScopeManager : IOpenIddictScopeManager where TSco /// public virtual async ValueTask FindByIdAsync(string identifier, CancellationToken cancellationToken = default) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var scope = Options.CurrentValue.DisableEntityCaching ? await Store.FindByIdAsync(identifier, cancellationToken) : @@ -244,10 +229,7 @@ public class OpenIddictScopeManager : IOpenIddictScopeManager where TSco /// public virtual async ValueTask FindByNameAsync(string name, CancellationToken cancellationToken = default) { - if (string.IsNullOrEmpty(name)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0202), nameof(name)); - } + ArgumentException.ThrowIfNullOrEmpty(name); var scope = Options.CurrentValue.DisableEntityCaching ? await Store.FindByNameAsync(name, cancellationToken) : @@ -322,10 +304,7 @@ public class OpenIddictScopeManager : IOpenIddictScopeManager where TSco public virtual IAsyncEnumerable FindByResourceAsync( string resource, CancellationToken cancellationToken = default) { - if (string.IsNullOrEmpty(resource)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0062), nameof(resource)); - } + ArgumentException.ThrowIfNullOrEmpty(resource); var scopes = Options.CurrentValue.DisableEntityCaching ? Store.FindByResourceAsync(resource, cancellationToken) : @@ -368,10 +347,7 @@ public class OpenIddictScopeManager : IOpenIddictScopeManager where TSco public virtual ValueTask GetAsync( Func, IQueryable> query, CancellationToken cancellationToken = default) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); return GetAsync(static (scopes, query) => query(scopes), query, cancellationToken); } @@ -392,10 +368,7 @@ public class OpenIddictScopeManager : IOpenIddictScopeManager where TSco Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken = default) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); return Store.GetAsync(query, state, cancellationToken); } @@ -411,10 +384,7 @@ public class OpenIddictScopeManager : IOpenIddictScopeManager where TSco /// public virtual ValueTask GetDescriptionAsync(TScope scope, CancellationToken cancellationToken = default) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); return Store.GetDescriptionAsync(scope, cancellationToken); } @@ -431,10 +401,7 @@ public class OpenIddictScopeManager : IOpenIddictScopeManager where TSco public virtual async ValueTask> GetDescriptionsAsync( TScope scope, CancellationToken cancellationToken = default) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); var descriptions = await Store.GetDescriptionsAsync(scope, cancellationToken); if (descriptions is not { Count: > 0 }) @@ -456,10 +423,7 @@ public class OpenIddictScopeManager : IOpenIddictScopeManager where TSco /// public virtual ValueTask GetDisplayNameAsync(TScope scope, CancellationToken cancellationToken = default) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); return Store.GetDisplayNameAsync(scope, cancellationToken); } @@ -476,10 +440,7 @@ public class OpenIddictScopeManager : IOpenIddictScopeManager where TSco public virtual async ValueTask> GetDisplayNamesAsync( TScope scope, CancellationToken cancellationToken = default) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); var names = await Store.GetDisplayNamesAsync(scope, cancellationToken); if (names is not { Count: > 0 }) @@ -501,10 +462,7 @@ public class OpenIddictScopeManager : IOpenIddictScopeManager where TSco /// public virtual ValueTask GetIdAsync(TScope scope, CancellationToken cancellationToken = default) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); return Store.GetIdAsync(scope, cancellationToken); } @@ -538,15 +496,8 @@ public class OpenIddictScopeManager : IOpenIddictScopeManager where TSco public virtual async ValueTask GetLocalizedDisplayNameAsync( TScope scope, CultureInfo culture, CancellationToken cancellationToken = default) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } - - if (culture is null) - { - throw new ArgumentNullException(nameof(culture)); - } + ArgumentNullException.ThrowIfNull(scope); + ArgumentNullException.ThrowIfNull(culture); var names = await Store.GetDisplayNamesAsync(scope, cancellationToken); if (names is not { Count: > 0 }) @@ -598,15 +549,8 @@ public class OpenIddictScopeManager : IOpenIddictScopeManager where TSco public virtual async ValueTask GetLocalizedDescriptionAsync( TScope scope, CultureInfo culture, CancellationToken cancellationToken = default) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } - - if (culture is null) - { - throw new ArgumentNullException(nameof(culture)); - } + ArgumentNullException.ThrowIfNull(scope); + ArgumentNullException.ThrowIfNull(culture); var descriptions = await Store.GetDescriptionsAsync(scope, cancellationToken); if (descriptions is not { Count: > 0 }) @@ -640,10 +584,7 @@ public class OpenIddictScopeManager : IOpenIddictScopeManager where TSco /// public virtual ValueTask GetNameAsync(TScope scope, CancellationToken cancellationToken = default) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); return Store.GetNameAsync(scope, cancellationToken); } @@ -660,10 +601,7 @@ public class OpenIddictScopeManager : IOpenIddictScopeManager where TSco public virtual ValueTask> GetPropertiesAsync( TScope scope, CancellationToken cancellationToken = default) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); return Store.GetPropertiesAsync(scope, cancellationToken); } @@ -680,10 +618,7 @@ public class OpenIddictScopeManager : IOpenIddictScopeManager where TSco public virtual ValueTask> GetResourcesAsync( TScope scope, CancellationToken cancellationToken = default) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); return Store.GetResourcesAsync(scope, cancellationToken); } @@ -709,10 +644,7 @@ public class OpenIddictScopeManager : IOpenIddictScopeManager where TSco public virtual IAsyncEnumerable ListAsync( Func, IQueryable> query, CancellationToken cancellationToken = default) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); return ListAsync(static (scopes, query) => query(scopes), query, cancellationToken); } @@ -730,10 +662,7 @@ public class OpenIddictScopeManager : IOpenIddictScopeManager where TSco Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken = default) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); return Store.ListAsync(query, state, cancellationToken); } @@ -773,15 +702,8 @@ public class OpenIddictScopeManager : IOpenIddictScopeManager where TSco public virtual async ValueTask PopulateAsync(TScope scope, OpenIddictScopeDescriptor descriptor, CancellationToken cancellationToken = default) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } - - if (descriptor is null) - { - throw new ArgumentNullException(nameof(descriptor)); - } + ArgumentNullException.ThrowIfNull(scope); + ArgumentNullException.ThrowIfNull(descriptor); await Store.SetDescriptionAsync(scope, descriptor.Description, cancellationToken); await Store.SetDescriptionsAsync(scope, descriptor.Descriptions.ToImmutableDictionary(), cancellationToken); @@ -805,15 +727,8 @@ public class OpenIddictScopeManager : IOpenIddictScopeManager where TSco OpenIddictScopeDescriptor descriptor, TScope scope, CancellationToken cancellationToken = default) { - if (descriptor is null) - { - throw new ArgumentNullException(nameof(descriptor)); - } - - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(descriptor); + ArgumentNullException.ThrowIfNull(scope); descriptor.Description = await Store.GetDescriptionAsync(scope, cancellationToken); descriptor.DisplayName = await Store.GetDisplayNameAsync(scope, cancellationToken); @@ -850,10 +765,7 @@ public class OpenIddictScopeManager : IOpenIddictScopeManager where TSco /// public virtual async ValueTask UpdateAsync(TScope scope, CancellationToken cancellationToken = default) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); var results = await GetValidationResultsAsync(scope, cancellationToken); if (results.Any(result => result != ValidationResult.Success)) @@ -908,15 +820,8 @@ public class OpenIddictScopeManager : IOpenIddictScopeManager where TSco public virtual async ValueTask UpdateAsync(TScope scope, OpenIddictScopeDescriptor descriptor, CancellationToken cancellationToken = default) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } - - if (descriptor is null) - { - throw new ArgumentNullException(nameof(descriptor)); - } + ArgumentNullException.ThrowIfNull(scope); + ArgumentNullException.ThrowIfNull(descriptor); await PopulateAsync(scope, descriptor, cancellationToken); await UpdateAsync(scope, cancellationToken); @@ -930,10 +835,7 @@ public class OpenIddictScopeManager : IOpenIddictScopeManager where TSco /// The validation error encountered when validating the scope. public virtual IAsyncEnumerable ValidateAsync(TScope scope, CancellationToken cancellationToken = default) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); return ExecuteAsync(cancellationToken); diff --git a/src/OpenIddict.Core/Managers/OpenIddictTokenManager.cs b/src/OpenIddict.Core/Managers/OpenIddictTokenManager.cs index fcc697d3..ab0d3ba2 100644 --- a/src/OpenIddict.Core/Managers/OpenIddictTokenManager.cs +++ b/src/OpenIddict.Core/Managers/OpenIddictTokenManager.cs @@ -89,10 +89,7 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok public virtual ValueTask CountAsync( Func, IQueryable> query, CancellationToken cancellationToken = default) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); return Store.CountAsync(query, cancellationToken); } @@ -107,10 +104,7 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok /// public virtual async ValueTask CreateAsync(TToken token, CancellationToken cancellationToken = default) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); // If no status was explicitly specified, assume that the token is valid. if (string.IsNullOrEmpty(await Store.GetStatusAsync(token, cancellationToken))) @@ -173,10 +167,7 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok public virtual async ValueTask CreateAsync( OpenIddictTokenDescriptor descriptor, CancellationToken cancellationToken = default) { - if (descriptor is null) - { - throw new ArgumentNullException(nameof(descriptor)); - } + ArgumentNullException.ThrowIfNull(descriptor); var token = await Store.InstantiateAsync(cancellationToken) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0226)); @@ -197,10 +188,7 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok /// public virtual async ValueTask DeleteAsync(TToken token, CancellationToken cancellationToken = default) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); if (!Options.CurrentValue.DisableEntityCaching) { @@ -260,10 +248,7 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok public virtual IAsyncEnumerable FindByApplicationIdAsync( string identifier, CancellationToken cancellationToken = default) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var tokens = Options.CurrentValue.DisableEntityCaching ? Store.FindByApplicationIdAsync(identifier, cancellationToken) : @@ -301,10 +286,7 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok public virtual IAsyncEnumerable FindByAuthorizationIdAsync( string identifier, CancellationToken cancellationToken = default) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var tokens = Options.CurrentValue.DisableEntityCaching ? Store.FindByAuthorizationIdAsync(identifier, cancellationToken) : @@ -344,10 +326,7 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok /// public virtual async ValueTask FindByIdAsync(string identifier, CancellationToken cancellationToken = default) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var token = Options.CurrentValue.DisableEntityCaching ? await Store.FindByIdAsync(identifier, cancellationToken) : @@ -382,10 +361,7 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok /// public virtual async ValueTask FindByReferenceIdAsync(string identifier, CancellationToken cancellationToken = default) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); identifier = await ObfuscateReferenceIdAsync(identifier, cancellationToken); @@ -420,10 +396,7 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok public virtual IAsyncEnumerable FindBySubjectAsync( string subject, CancellationToken cancellationToken = default) { - if (string.IsNullOrEmpty(subject)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0198), nameof(subject)); - } + ArgumentException.ThrowIfNullOrEmpty(subject); var tokens = Options.CurrentValue.DisableEntityCaching ? Store.FindBySubjectAsync(subject, cancellationToken) : @@ -463,10 +436,7 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok /// public virtual ValueTask GetApplicationIdAsync(TToken token, CancellationToken cancellationToken = default) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); return Store.GetApplicationIdAsync(token, cancellationToken); } @@ -484,10 +454,7 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok public virtual ValueTask GetAsync( Func, IQueryable> query, CancellationToken cancellationToken = default) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); return GetAsync(static (tokens, query) => query(tokens), query, cancellationToken); } @@ -508,10 +475,7 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken = default) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); return Store.GetAsync(query, state, cancellationToken); } @@ -527,10 +491,7 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok /// public virtual ValueTask GetAuthorizationIdAsync(TToken token, CancellationToken cancellationToken = default) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); return Store.GetAuthorizationIdAsync(token, cancellationToken); } @@ -546,10 +507,7 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok /// public virtual ValueTask GetCreationDateAsync(TToken token, CancellationToken cancellationToken = default) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); return Store.GetCreationDateAsync(token, cancellationToken); } @@ -565,10 +523,7 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok /// public virtual ValueTask GetExpirationDateAsync(TToken token, CancellationToken cancellationToken = default) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); return Store.GetExpirationDateAsync(token, cancellationToken); } @@ -584,10 +539,7 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok /// public virtual ValueTask GetIdAsync(TToken token, CancellationToken cancellationToken = default) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); return Store.GetIdAsync(token, cancellationToken); } @@ -603,10 +555,7 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok /// public virtual ValueTask GetPayloadAsync(TToken token, CancellationToken cancellationToken = default) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); return Store.GetPayloadAsync(token, cancellationToken); } @@ -623,10 +572,7 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok public virtual ValueTask> GetPropertiesAsync( TToken token, CancellationToken cancellationToken = default) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); return Store.GetPropertiesAsync(token, cancellationToken); } @@ -642,10 +588,7 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok /// public virtual ValueTask GetRedemptionDateAsync(TToken token, CancellationToken cancellationToken = default) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); return Store.GetRedemptionDateAsync(token, cancellationToken); } @@ -663,10 +606,7 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok /// public virtual ValueTask GetReferenceIdAsync(TToken token, CancellationToken cancellationToken = default) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); return Store.GetReferenceIdAsync(token, cancellationToken); } @@ -682,10 +622,7 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok /// public virtual ValueTask GetStatusAsync(TToken token, CancellationToken cancellationToken = default) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); return Store.GetStatusAsync(token, cancellationToken); } @@ -701,10 +638,7 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok /// public virtual ValueTask GetSubjectAsync(TToken token, CancellationToken cancellationToken = default) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); return Store.GetSubjectAsync(token, cancellationToken); } @@ -720,10 +654,7 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok /// public virtual async ValueTask GetTypeAsync(TToken token, CancellationToken cancellationToken = default) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); return await Store.GetTypeAsync(token, cancellationToken) switch { @@ -752,15 +683,8 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok /// if the token has the specified status, otherwise. public virtual async ValueTask HasStatusAsync(TToken token, string status, CancellationToken cancellationToken = default) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } - - if (string.IsNullOrEmpty(status)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0199), nameof(status)); - } + ArgumentNullException.ThrowIfNull(token); + ArgumentException.ThrowIfNullOrEmpty(status); return string.Equals(await GetStatusAsync(token, cancellationToken), status, StringComparison.OrdinalIgnoreCase); } @@ -774,15 +698,8 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok /// if the token has the specified type, otherwise. public virtual async ValueTask HasTypeAsync(TToken token, string type, CancellationToken cancellationToken = default) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } - - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0200), nameof(type)); - } + ArgumentNullException.ThrowIfNull(token); + ArgumentException.ThrowIfNullOrEmpty(type); return string.Equals(await GetTypeAsync(token, cancellationToken), type, StringComparison.OrdinalIgnoreCase); } @@ -796,10 +713,7 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok /// if the token has any of the specified types, otherwise. public virtual async ValueTask HasTypeAsync(TToken token, ImmutableArray types, CancellationToken cancellationToken = default) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); var type = await GetTypeAsync(token, cancellationToken); if (string.IsNullOrEmpty(type)) @@ -839,10 +753,7 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok public virtual IAsyncEnumerable ListAsync( Func, IQueryable> query, CancellationToken cancellationToken = default) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); return ListAsync(static (tokens, query) => query(tokens), query, cancellationToken); } @@ -860,10 +771,7 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken = default) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); return Store.ListAsync(query, state, cancellationToken); } @@ -880,15 +788,8 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok public virtual async ValueTask PopulateAsync(TToken token, OpenIddictTokenDescriptor descriptor, CancellationToken cancellationToken = default) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } - - if (descriptor is null) - { - throw new ArgumentNullException(nameof(descriptor)); - } + ArgumentNullException.ThrowIfNull(token); + ArgumentNullException.ThrowIfNull(descriptor); await Store.SetApplicationIdAsync(token, descriptor.ApplicationId, cancellationToken); await Store.SetAuthorizationIdAsync(token, descriptor.AuthorizationId, cancellationToken); @@ -916,15 +817,8 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok OpenIddictTokenDescriptor descriptor, TToken token, CancellationToken cancellationToken = default) { - if (descriptor is null) - { - throw new ArgumentNullException(nameof(descriptor)); - } - - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(descriptor); + ArgumentNullException.ThrowIfNull(token); descriptor.ApplicationId = await Store.GetApplicationIdAsync(token, cancellationToken); descriptor.AuthorizationId = await Store.GetAuthorizationIdAsync(token, cancellationToken); @@ -974,10 +868,7 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok /// The number of tokens associated with the specified application that were marked as revoked. public virtual ValueTask RevokeByApplicationIdAsync(string identifier, CancellationToken cancellationToken = default) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); return Store.RevokeByApplicationIdAsync(identifier, cancellationToken); } @@ -990,10 +881,7 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok /// The number of tokens associated with the specified authorization that were marked as revoked. public virtual ValueTask RevokeByAuthorizationIdAsync(string identifier, CancellationToken cancellationToken = default) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); return Store.RevokeByAuthorizationIdAsync(identifier, cancellationToken); } @@ -1006,10 +894,7 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok /// The number of tokens associated with the specified subject that were marked as revoked. public virtual ValueTask RevokeBySubjectAsync(string subject, CancellationToken cancellationToken = default) { - if (string.IsNullOrEmpty(subject)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(subject)); - } + ArgumentException.ThrowIfNullOrEmpty(subject); return Store.RevokeBySubjectAsync(subject, cancellationToken); } @@ -1022,10 +907,7 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok /// if the token was successfully redemeed, otherwise. public virtual async ValueTask TryRedeemAsync(TToken token, CancellationToken cancellationToken = default) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); // If the token doesn't have a redemption date attached, this likely means it's // the first time the token is redeemed. In this case, attach the current date. @@ -1068,10 +950,7 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok /// if the token was successfully redemeed, otherwise. public virtual async ValueTask TryRejectAsync(TToken token, CancellationToken cancellationToken = default) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); await Store.SetStatusAsync(token, Statuses.Rejected, cancellationToken); @@ -1107,10 +986,7 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok /// if the token was successfully revoked, otherwise. public virtual async ValueTask TryRevokeAsync(TToken token, CancellationToken cancellationToken = default) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); await Store.SetStatusAsync(token, Statuses.Revoked, cancellationToken); @@ -1148,10 +1024,7 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok /// public virtual async ValueTask UpdateAsync(TToken token, CancellationToken cancellationToken = default) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); var results = await GetValidationResultsAsync(token, cancellationToken); if (results.Any(result => result != ValidationResult.Success)) @@ -1206,15 +1079,8 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok public virtual async ValueTask UpdateAsync(TToken token, OpenIddictTokenDescriptor descriptor, CancellationToken cancellationToken = default) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } - - if (descriptor is null) - { - throw new ArgumentNullException(nameof(descriptor)); - } + ArgumentNullException.ThrowIfNull(token); + ArgumentNullException.ThrowIfNull(descriptor); // Store the original reference identifier for later comparison. var comparand = await Store.GetReferenceIdAsync(token, cancellationToken); @@ -1239,10 +1105,7 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok /// The validation error encountered when validating the token. public virtual IAsyncEnumerable ValidateAsync(TToken token, CancellationToken cancellationToken = default) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); return ExecuteAsync(cancellationToken); @@ -1290,10 +1153,7 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok /// protected virtual ValueTask ObfuscateReferenceIdAsync(string identifier, CancellationToken cancellationToken = default) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); // Compute the digest of the generated identifier and use it as the hashed identifier of the reference token. // Doing that prevents token identifiers stolen from the database from being used as valid reference tokens. diff --git a/src/OpenIddict.Core/OpenIddictCoreBuilder.cs b/src/OpenIddict.Core/OpenIddictCoreBuilder.cs index ada6dd21..a468b7e7 100644 --- a/src/OpenIddict.Core/OpenIddictCoreBuilder.cs +++ b/src/OpenIddict.Core/OpenIddictCoreBuilder.cs @@ -37,10 +37,7 @@ public sealed class OpenIddictCoreBuilder /// The instance. public OpenIddictCoreBuilder Configure(Action configuration) { - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(configuration); Services.Configure(configuration); @@ -447,10 +444,7 @@ public sealed class OpenIddictCoreBuilder /// The instance. public OpenIddictCoreBuilder SetEntityCacheLimit(int limit) { - if (limit < 10) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0233), nameof(limit)); - } + ArgumentOutOfRangeException.ThrowIfLessThan(limit, 10); return Configure(options => options.EntityCacheLimit = limit); } diff --git a/src/OpenIddict.Core/OpenIddictCoreExtensions.cs b/src/OpenIddict.Core/OpenIddictCoreExtensions.cs index 6b262399..4a045114 100644 --- a/src/OpenIddict.Core/OpenIddictCoreExtensions.cs +++ b/src/OpenIddict.Core/OpenIddictCoreExtensions.cs @@ -23,10 +23,7 @@ public static class OpenIddictCoreExtensions /// The instance. public static OpenIddictCoreBuilder AddCore(this OpenIddictBuilder builder) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } + ArgumentNullException.ThrowIfNull(builder); builder.Services.AddLogging(); builder.Services.AddMemoryCache(); @@ -70,15 +67,8 @@ public static class OpenIddictCoreExtensions /// The instance. public static OpenIddictBuilder AddCore(this OpenIddictBuilder builder, Action configuration) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } - - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(builder); + ArgumentNullException.ThrowIfNull(configuration); configuration(builder.AddCore()); diff --git a/src/OpenIddict.EntityFramework.Models/OpenIddict.EntityFramework.Models.csproj b/src/OpenIddict.EntityFramework.Models/OpenIddict.EntityFramework.Models.csproj index d7dcafca..ef9f4a56 100644 --- a/src/OpenIddict.EntityFramework.Models/OpenIddict.EntityFramework.Models.csproj +++ b/src/OpenIddict.EntityFramework.Models/OpenIddict.EntityFramework.Models.csproj @@ -6,11 +6,7 @@ $(NetCoreTargetFrameworks); $(NetStandardTargetFrameworks) - - - false - System.Diagnostics.CodeAnalysis.StringSyntaxAttribute diff --git a/src/OpenIddict.EntityFramework/OpenIddictEntityFrameworkBuilder.cs b/src/OpenIddict.EntityFramework/OpenIddictEntityFrameworkBuilder.cs index 3daff43a..0982da57 100644 --- a/src/OpenIddict.EntityFramework/OpenIddictEntityFrameworkBuilder.cs +++ b/src/OpenIddict.EntityFramework/OpenIddictEntityFrameworkBuilder.cs @@ -39,10 +39,7 @@ public sealed class OpenIddictEntityFrameworkBuilder /// The instance. public OpenIddictEntityFrameworkBuilder Configure(Action configuration) { - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(configuration); Services.Configure(configuration); diff --git a/src/OpenIddict.EntityFramework/OpenIddictEntityFrameworkExtensions.cs b/src/OpenIddict.EntityFramework/OpenIddictEntityFrameworkExtensions.cs index 961b8b72..84fa66ee 100644 --- a/src/OpenIddict.EntityFramework/OpenIddictEntityFrameworkExtensions.cs +++ b/src/OpenIddict.EntityFramework/OpenIddictEntityFrameworkExtensions.cs @@ -24,10 +24,7 @@ public static class OpenIddictEntityFrameworkExtensions /// The instance. public static OpenIddictEntityFrameworkBuilder UseEntityFramework(this OpenIddictCoreBuilder builder) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } + ArgumentNullException.ThrowIfNull(builder); // Since Entity Framework 6.x may be used with databases performing case-insensitive // or culture-sensitive comparisons, ensure the additional filtering logic is enforced @@ -64,15 +61,8 @@ public static class OpenIddictEntityFrameworkExtensions public static OpenIddictCoreBuilder UseEntityFramework( this OpenIddictCoreBuilder builder, Action configuration) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } - - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(builder); + ArgumentNullException.ThrowIfNull(configuration); configuration(builder.UseEntityFramework()); diff --git a/src/OpenIddict.EntityFramework/OpenIddictEntityFrameworkHelpers.cs b/src/OpenIddict.EntityFramework/OpenIddictEntityFrameworkHelpers.cs index 46fef1e3..b34c713d 100644 --- a/src/OpenIddict.EntityFramework/OpenIddictEntityFrameworkHelpers.cs +++ b/src/OpenIddict.EntityFramework/OpenIddictEntityFrameworkHelpers.cs @@ -52,10 +52,7 @@ public static class OpenIddictEntityFrameworkHelpers where TToken : OpenIddictEntityFrameworkToken where TKey : notnull, IEquatable { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } + ArgumentNullException.ThrowIfNull(builder); builder.Configurations .Add(new OpenIddictEntityFrameworkApplicationConfiguration()) @@ -75,10 +72,7 @@ public static class OpenIddictEntityFrameworkHelpers /// The streamed async enumeration containing the results. internal static IAsyncEnumerable AsAsyncEnumerable(this IQueryable source, CancellationToken cancellationToken) { - if (source is null) - { - throw new ArgumentNullException(nameof(source)); - } + ArgumentNullException.ThrowIfNull(source); return ExecuteAsync(source, cancellationToken); @@ -101,10 +95,7 @@ public static class OpenIddictEntityFrameworkHelpers /// The if it could be created, otherwise. internal static DbContextTransaction? CreateTransaction(this DbContext context, IsolationLevel level) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); try { diff --git a/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkApplicationStore.cs b/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkApplicationStore.cs index 1803e3b1..1d9f9474 100644 --- a/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkApplicationStore.cs +++ b/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkApplicationStore.cs @@ -92,10 +92,7 @@ public class OpenIddictEntityFrameworkApplicationStore< /// public virtual async ValueTask CountAsync(Func, IQueryable> query, CancellationToken cancellationToken) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); var context = await Context.GetDbContextAsync(cancellationToken); @@ -105,10 +102,7 @@ public class OpenIddictEntityFrameworkApplicationStore< /// public virtual async ValueTask CreateAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); var context = await Context.GetDbContextAsync(cancellationToken); @@ -120,10 +114,7 @@ public class OpenIddictEntityFrameworkApplicationStore< /// public virtual async ValueTask DeleteAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); var context = await Context.GetDbContextAsync(cancellationToken); @@ -193,10 +184,7 @@ public class OpenIddictEntityFrameworkApplicationStore< /// public virtual async ValueTask FindByClientIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var context = await Context.GetDbContextAsync(cancellationToken); @@ -216,10 +204,7 @@ public class OpenIddictEntityFrameworkApplicationStore< /// public virtual async ValueTask FindByIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var context = await Context.GetDbContextAsync(cancellationToken); var key = ConvertIdentifierFromString(identifier); @@ -241,10 +226,7 @@ public class OpenIddictEntityFrameworkApplicationStore< public virtual IAsyncEnumerable FindByPostLogoutRedirectUriAsync( [StringSyntax(StringSyntaxAttribute.Uri)] string uri, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(uri)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0143), nameof(uri)); - } + ArgumentException.ThrowIfNullOrEmpty(uri); // To optimize the efficiency of the query a bit, only applications whose stringified // PostLogoutRedirectUris contains the specified URI are returned. Once the applications @@ -277,10 +259,7 @@ public class OpenIddictEntityFrameworkApplicationStore< public virtual IAsyncEnumerable FindByRedirectUriAsync( [StringSyntax(StringSyntaxAttribute.Uri)] string uri, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(uri)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0143), nameof(uri)); - } + ArgumentException.ThrowIfNullOrEmpty(uri); // To optimize the efficiency of the query a bit, only applications whose stringified // RedirectUris property contains the specified URI are returned. Once the applications @@ -312,10 +291,7 @@ public class OpenIddictEntityFrameworkApplicationStore< /// public virtual ValueTask GetApplicationTypeAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); return new(application.ApplicationType); } @@ -325,10 +301,7 @@ public class OpenIddictEntityFrameworkApplicationStore< Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); var context = await Context.GetDbContextAsync(cancellationToken); @@ -338,10 +311,7 @@ public class OpenIddictEntityFrameworkApplicationStore< /// public virtual ValueTask GetClientIdAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); return new(application.ClientId); } @@ -349,10 +319,7 @@ public class OpenIddictEntityFrameworkApplicationStore< /// public virtual ValueTask GetClientSecretAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); return new(application.ClientSecret); } @@ -360,10 +327,7 @@ public class OpenIddictEntityFrameworkApplicationStore< /// public virtual ValueTask GetClientTypeAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); return new(application.ClientType); } @@ -371,10 +335,7 @@ public class OpenIddictEntityFrameworkApplicationStore< /// public virtual ValueTask GetConsentTypeAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); return new(application.ConsentType); } @@ -382,10 +343,7 @@ public class OpenIddictEntityFrameworkApplicationStore< /// public virtual ValueTask GetDisplayNameAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); return new(application.DisplayName); } @@ -393,10 +351,7 @@ public class OpenIddictEntityFrameworkApplicationStore< /// public virtual ValueTask> GetDisplayNamesAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (string.IsNullOrEmpty(application.DisplayNames)) { @@ -434,10 +389,7 @@ public class OpenIddictEntityFrameworkApplicationStore< /// public virtual ValueTask GetIdAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); return new(ConvertIdentifierToString(application.Id)); } @@ -445,10 +397,7 @@ public class OpenIddictEntityFrameworkApplicationStore< /// public virtual ValueTask GetJsonWebKeySetAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (string.IsNullOrEmpty(application.JsonWebKeySet)) { @@ -472,10 +421,7 @@ public class OpenIddictEntityFrameworkApplicationStore< /// public virtual ValueTask> GetPermissionsAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (string.IsNullOrEmpty(application.Permissions)) { @@ -513,10 +459,7 @@ public class OpenIddictEntityFrameworkApplicationStore< /// public virtual ValueTask> GetPostLogoutRedirectUrisAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (string.IsNullOrEmpty(application.PostLogoutRedirectUris)) { @@ -554,10 +497,7 @@ public class OpenIddictEntityFrameworkApplicationStore< /// public virtual ValueTask> GetPropertiesAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (string.IsNullOrEmpty(application.Properties)) { @@ -589,10 +529,7 @@ public class OpenIddictEntityFrameworkApplicationStore< /// public virtual ValueTask> GetRedirectUrisAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (string.IsNullOrEmpty(application.RedirectUris)) { @@ -630,10 +567,7 @@ public class OpenIddictEntityFrameworkApplicationStore< /// public virtual ValueTask> GetRequirementsAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (string.IsNullOrEmpty(application.Requirements)) { @@ -671,10 +605,7 @@ public class OpenIddictEntityFrameworkApplicationStore< /// public virtual ValueTask> GetSettingsAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (string.IsNullOrEmpty(application.Settings)) { @@ -753,10 +684,7 @@ public class OpenIddictEntityFrameworkApplicationStore< Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); return ExecuteAsync(cancellationToken); @@ -775,10 +703,7 @@ public class OpenIddictEntityFrameworkApplicationStore< public virtual ValueTask SetApplicationTypeAsync(TApplication application, string? type, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); application.ApplicationType = type; @@ -788,10 +713,7 @@ public class OpenIddictEntityFrameworkApplicationStore< /// public virtual ValueTask SetClientIdAsync(TApplication application, string? identifier, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); application.ClientId = identifier; @@ -801,10 +723,7 @@ public class OpenIddictEntityFrameworkApplicationStore< /// public virtual ValueTask SetClientSecretAsync(TApplication application, string? secret, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); application.ClientSecret = secret; @@ -814,10 +733,7 @@ public class OpenIddictEntityFrameworkApplicationStore< /// public virtual ValueTask SetClientTypeAsync(TApplication application, string? type, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); application.ClientType = type; @@ -827,10 +743,7 @@ public class OpenIddictEntityFrameworkApplicationStore< /// public virtual ValueTask SetConsentTypeAsync(TApplication application, string? type, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); application.ConsentType = type; @@ -840,10 +753,7 @@ public class OpenIddictEntityFrameworkApplicationStore< /// public virtual ValueTask SetDisplayNameAsync(TApplication application, string? name, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); application.DisplayName = name; @@ -854,10 +764,7 @@ public class OpenIddictEntityFrameworkApplicationStore< public virtual ValueTask SetDisplayNamesAsync(TApplication application, ImmutableDictionary names, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (names is not { Count: > 0 }) { @@ -892,10 +799,7 @@ public class OpenIddictEntityFrameworkApplicationStore< /// public virtual ValueTask SetJsonWebKeySetAsync(TApplication application, JsonWebKeySet? set, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); application.JsonWebKeySet = set is not null ? JsonSerializer.Serialize(set, OpenIddictSerializer.Default.JsonWebKeySet) : null; @@ -905,10 +809,7 @@ public class OpenIddictEntityFrameworkApplicationStore< /// public virtual ValueTask SetPermissionsAsync(TApplication application, ImmutableArray permissions, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (permissions.IsDefaultOrEmpty) { @@ -943,10 +844,7 @@ public class OpenIddictEntityFrameworkApplicationStore< public virtual ValueTask SetPostLogoutRedirectUrisAsync(TApplication application, ImmutableArray uris, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (uris.IsDefaultOrEmpty) { @@ -981,10 +879,7 @@ public class OpenIddictEntityFrameworkApplicationStore< public virtual ValueTask SetPropertiesAsync(TApplication application, ImmutableDictionary properties, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (properties is not { Count: > 0 }) { @@ -1020,10 +915,7 @@ public class OpenIddictEntityFrameworkApplicationStore< public virtual ValueTask SetRedirectUrisAsync(TApplication application, ImmutableArray uris, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (uris.IsDefaultOrEmpty) { @@ -1057,10 +949,7 @@ public class OpenIddictEntityFrameworkApplicationStore< /// public virtual ValueTask SetRequirementsAsync(TApplication application, ImmutableArray requirements, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (requirements.IsDefaultOrEmpty) { @@ -1095,10 +984,7 @@ public class OpenIddictEntityFrameworkApplicationStore< public virtual ValueTask SetSettingsAsync(TApplication application, ImmutableDictionary settings, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (settings is not { Count: > 0 }) { @@ -1133,10 +1019,7 @@ public class OpenIddictEntityFrameworkApplicationStore< /// public virtual async ValueTask UpdateAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); var context = await Context.GetDbContextAsync(cancellationToken); diff --git a/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkAuthorizationStore.cs b/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkAuthorizationStore.cs index 902a5d68..2d1b70af 100644 --- a/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkAuthorizationStore.cs +++ b/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkAuthorizationStore.cs @@ -90,10 +90,7 @@ public class OpenIddictEntityFrameworkAuthorizationStore< /// public virtual async ValueTask CountAsync(Func, IQueryable> query, CancellationToken cancellationToken) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); var context = await Context.GetDbContextAsync(cancellationToken); @@ -103,10 +100,7 @@ public class OpenIddictEntityFrameworkAuthorizationStore< /// public virtual async ValueTask CreateAsync(TAuthorization authorization, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); var context = await Context.GetDbContextAsync(cancellationToken); @@ -118,10 +112,7 @@ public class OpenIddictEntityFrameworkAuthorizationStore< /// public virtual async ValueTask DeleteAsync(TAuthorization authorization, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); var context = await Context.GetDbContextAsync(cancellationToken); @@ -210,10 +201,7 @@ public class OpenIddictEntityFrameworkAuthorizationStore< /// public virtual IAsyncEnumerable FindByApplicationIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); return ExecuteAsync(cancellationToken); @@ -235,10 +223,7 @@ public class OpenIddictEntityFrameworkAuthorizationStore< /// public virtual async ValueTask FindByIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var context = await Context.GetDbContextAsync(cancellationToken); var key = ConvertIdentifierFromString(identifier); @@ -259,10 +244,7 @@ public class OpenIddictEntityFrameworkAuthorizationStore< /// public virtual IAsyncEnumerable FindBySubjectAsync(string subject, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(subject)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0198), nameof(subject)); - } + ArgumentException.ThrowIfNullOrEmpty(subject); return ExecuteAsync(cancellationToken); @@ -283,10 +265,7 @@ public class OpenIddictEntityFrameworkAuthorizationStore< /// public virtual async ValueTask GetApplicationIdAsync(TAuthorization authorization, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); var context = await Context.GetDbContextAsync(cancellationToken); @@ -315,10 +294,7 @@ public class OpenIddictEntityFrameworkAuthorizationStore< Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); var context = await Context.GetDbContextAsync(cancellationToken); @@ -329,10 +305,7 @@ public class OpenIddictEntityFrameworkAuthorizationStore< /// public virtual ValueTask GetCreationDateAsync(TAuthorization authorization, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); if (authorization.CreationDate is null) { @@ -345,10 +318,7 @@ public class OpenIddictEntityFrameworkAuthorizationStore< /// public virtual ValueTask GetIdAsync(TAuthorization authorization, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); return new(ConvertIdentifierToString(authorization.Id)); } @@ -356,10 +326,7 @@ public class OpenIddictEntityFrameworkAuthorizationStore< /// public virtual ValueTask> GetPropertiesAsync(TAuthorization authorization, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); if (string.IsNullOrEmpty(authorization.Properties)) { @@ -391,10 +358,7 @@ public class OpenIddictEntityFrameworkAuthorizationStore< /// public virtual ValueTask> GetScopesAsync(TAuthorization authorization, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); if (string.IsNullOrEmpty(authorization.Scopes)) { @@ -432,10 +396,7 @@ public class OpenIddictEntityFrameworkAuthorizationStore< /// public virtual ValueTask GetStatusAsync(TAuthorization authorization, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); return new(authorization.Status); } @@ -443,10 +404,7 @@ public class OpenIddictEntityFrameworkAuthorizationStore< /// public virtual ValueTask GetSubjectAsync(TAuthorization authorization, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); return new(authorization.Subject); } @@ -454,10 +412,7 @@ public class OpenIddictEntityFrameworkAuthorizationStore< /// public virtual ValueTask GetTypeAsync(TAuthorization authorization, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); return new(authorization.Type); } @@ -508,10 +463,7 @@ public class OpenIddictEntityFrameworkAuthorizationStore< Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); return ExecuteAsync(cancellationToken); @@ -670,10 +622,7 @@ public class OpenIddictEntityFrameworkAuthorizationStore< /// public virtual async ValueTask RevokeByApplicationIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var context = await Context.GetDbContextAsync(cancellationToken); var key = ConvertIdentifierFromString(identifier); @@ -719,10 +668,7 @@ public class OpenIddictEntityFrameworkAuthorizationStore< /// public virtual async ValueTask RevokeBySubjectAsync(string subject, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(subject)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(subject)); - } + ArgumentException.ThrowIfNullOrEmpty(subject); var context = await Context.GetDbContextAsync(cancellationToken); @@ -768,10 +714,7 @@ public class OpenIddictEntityFrameworkAuthorizationStore< public virtual async ValueTask SetApplicationIdAsync(TAuthorization authorization, string? identifier, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); var context = await Context.GetDbContextAsync(cancellationToken); @@ -804,10 +747,7 @@ public class OpenIddictEntityFrameworkAuthorizationStore< public virtual ValueTask SetCreationDateAsync(TAuthorization authorization, DateTimeOffset? date, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); authorization.CreationDate = date?.UtcDateTime; @@ -818,10 +758,7 @@ public class OpenIddictEntityFrameworkAuthorizationStore< public virtual ValueTask SetPropertiesAsync(TAuthorization authorization, ImmutableDictionary properties, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); if (properties is not { Count: > 0 }) { @@ -857,10 +794,7 @@ public class OpenIddictEntityFrameworkAuthorizationStore< public virtual ValueTask SetScopesAsync(TAuthorization authorization, ImmutableArray scopes, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); if (scopes.IsDefaultOrEmpty) { @@ -894,10 +828,7 @@ public class OpenIddictEntityFrameworkAuthorizationStore< /// public virtual ValueTask SetStatusAsync(TAuthorization authorization, string? status, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); authorization.Status = status; @@ -907,10 +838,7 @@ public class OpenIddictEntityFrameworkAuthorizationStore< /// public virtual ValueTask SetSubjectAsync(TAuthorization authorization, string? subject, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); authorization.Subject = subject; @@ -920,10 +848,7 @@ public class OpenIddictEntityFrameworkAuthorizationStore< /// public virtual ValueTask SetTypeAsync(TAuthorization authorization, string? type, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); authorization.Type = type; @@ -933,10 +858,7 @@ public class OpenIddictEntityFrameworkAuthorizationStore< /// public virtual async ValueTask UpdateAsync(TAuthorization authorization, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); var context = await Context.GetDbContextAsync(cancellationToken); diff --git a/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkScopeStore.cs b/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkScopeStore.cs index e68b1fa0..4022acc1 100644 --- a/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkScopeStore.cs +++ b/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkScopeStore.cs @@ -82,10 +82,7 @@ public class OpenIddictEntityFrameworkScopeStore< /// public virtual async ValueTask CountAsync(Func, IQueryable> query, CancellationToken cancellationToken) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); var context = await Context.GetDbContextAsync(cancellationToken); @@ -95,10 +92,7 @@ public class OpenIddictEntityFrameworkScopeStore< /// public virtual async ValueTask CreateAsync(TScope scope, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); var context = await Context.GetDbContextAsync(cancellationToken); @@ -110,10 +104,7 @@ public class OpenIddictEntityFrameworkScopeStore< /// public virtual async ValueTask DeleteAsync(TScope scope, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); var context = await Context.GetDbContextAsync(cancellationToken); @@ -136,10 +127,7 @@ public class OpenIddictEntityFrameworkScopeStore< /// public virtual async ValueTask FindByIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var context = await Context.GetDbContextAsync(cancellationToken); var key = ConvertIdentifierFromString(identifier); @@ -160,10 +148,7 @@ public class OpenIddictEntityFrameworkScopeStore< /// public virtual async ValueTask FindByNameAsync(string name, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(name)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0202), nameof(name)); - } + ArgumentException.ThrowIfNullOrEmpty(name); var context = await Context.GetDbContextAsync(cancellationToken); @@ -210,10 +195,7 @@ public class OpenIddictEntityFrameworkScopeStore< public virtual IAsyncEnumerable FindByResourceAsync( string resource, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(resource)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0062), nameof(resource)); - } + ArgumentException.ThrowIfNullOrEmpty(resource); // To optimize the efficiency of the query a bit, only scopes whose stringified // Resources column contains the specified resource are returned. Once the scopes @@ -247,10 +229,7 @@ public class OpenIddictEntityFrameworkScopeStore< Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); var context = await Context.GetDbContextAsync(cancellationToken); @@ -260,10 +239,7 @@ public class OpenIddictEntityFrameworkScopeStore< /// public virtual ValueTask GetDescriptionAsync(TScope scope, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); return new(scope.Description); } @@ -271,10 +247,7 @@ public class OpenIddictEntityFrameworkScopeStore< /// public virtual ValueTask> GetDescriptionsAsync(TScope scope, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); if (string.IsNullOrEmpty(scope.Descriptions)) { @@ -312,10 +285,7 @@ public class OpenIddictEntityFrameworkScopeStore< /// public virtual ValueTask GetDisplayNameAsync(TScope scope, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); return new(scope.DisplayName); } @@ -323,10 +293,7 @@ public class OpenIddictEntityFrameworkScopeStore< /// public virtual ValueTask> GetDisplayNamesAsync(TScope scope, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); if (string.IsNullOrEmpty(scope.DisplayNames)) { @@ -364,10 +331,7 @@ public class OpenIddictEntityFrameworkScopeStore< /// public virtual ValueTask GetIdAsync(TScope scope, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); return new(ConvertIdentifierToString(scope.Id)); } @@ -375,10 +339,7 @@ public class OpenIddictEntityFrameworkScopeStore< /// public virtual ValueTask GetNameAsync(TScope scope, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); return new(scope.Name); } @@ -386,10 +347,7 @@ public class OpenIddictEntityFrameworkScopeStore< /// public virtual ValueTask> GetPropertiesAsync(TScope scope, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); if (string.IsNullOrEmpty(scope.Properties)) { @@ -421,10 +379,7 @@ public class OpenIddictEntityFrameworkScopeStore< /// public virtual ValueTask> GetResourcesAsync(TScope scope, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); if (string.IsNullOrEmpty(scope.Resources)) { @@ -503,10 +458,7 @@ public class OpenIddictEntityFrameworkScopeStore< Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); return ExecuteAsync(cancellationToken); @@ -524,10 +476,7 @@ public class OpenIddictEntityFrameworkScopeStore< /// public virtual ValueTask SetDescriptionAsync(TScope scope, string? description, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); scope.Description = description; @@ -538,10 +487,7 @@ public class OpenIddictEntityFrameworkScopeStore< public virtual ValueTask SetDescriptionsAsync(TScope scope, ImmutableDictionary descriptions, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); if (descriptions is not { Count: > 0 }) { @@ -576,10 +522,7 @@ public class OpenIddictEntityFrameworkScopeStore< /// public virtual ValueTask SetDisplayNameAsync(TScope scope, string? name, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); scope.DisplayName = name; @@ -590,10 +533,7 @@ public class OpenIddictEntityFrameworkScopeStore< public virtual ValueTask SetDisplayNamesAsync(TScope scope, ImmutableDictionary names, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); if (names is not { Count: > 0 }) { @@ -628,10 +568,7 @@ public class OpenIddictEntityFrameworkScopeStore< /// public virtual ValueTask SetNameAsync(TScope scope, string? name, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); scope.Name = name; @@ -642,10 +579,7 @@ public class OpenIddictEntityFrameworkScopeStore< public virtual ValueTask SetPropertiesAsync(TScope scope, ImmutableDictionary properties, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); if (properties is not { Count: > 0 }) { @@ -680,10 +614,7 @@ public class OpenIddictEntityFrameworkScopeStore< /// public virtual ValueTask SetResourcesAsync(TScope scope, ImmutableArray resources, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); if (resources.IsDefaultOrEmpty) { @@ -717,10 +648,7 @@ public class OpenIddictEntityFrameworkScopeStore< /// public virtual async ValueTask UpdateAsync(TScope scope, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); var context = await Context.GetDbContextAsync(cancellationToken); diff --git a/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkTokenStore.cs b/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkTokenStore.cs index 38f564cb..adf4eb75 100644 --- a/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkTokenStore.cs +++ b/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkTokenStore.cs @@ -90,10 +90,7 @@ public class OpenIddictEntityFrameworkTokenStore< /// public virtual async ValueTask CountAsync(Func, IQueryable> query, CancellationToken cancellationToken) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); var context = await Context.GetDbContextAsync(cancellationToken); @@ -103,10 +100,7 @@ public class OpenIddictEntityFrameworkTokenStore< /// public virtual async ValueTask CreateAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); var context = await Context.GetDbContextAsync(cancellationToken); @@ -118,10 +112,7 @@ public class OpenIddictEntityFrameworkTokenStore< /// public virtual async ValueTask DeleteAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); var context = await Context.GetDbContextAsync(cancellationToken); @@ -183,10 +174,7 @@ public class OpenIddictEntityFrameworkTokenStore< /// public virtual IAsyncEnumerable FindByApplicationIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); return ExecuteAsync(cancellationToken); @@ -208,10 +196,7 @@ public class OpenIddictEntityFrameworkTokenStore< /// public virtual IAsyncEnumerable FindByAuthorizationIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); return ExecuteAsync(cancellationToken); @@ -233,10 +218,7 @@ public class OpenIddictEntityFrameworkTokenStore< /// public virtual async ValueTask FindByIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var context = await Context.GetDbContextAsync(cancellationToken); var key = ConvertIdentifierFromString(identifier); @@ -257,10 +239,7 @@ public class OpenIddictEntityFrameworkTokenStore< /// public virtual async ValueTask FindByReferenceIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var context = await Context.GetDbContextAsync(cancellationToken); @@ -280,10 +259,7 @@ public class OpenIddictEntityFrameworkTokenStore< /// public virtual IAsyncEnumerable FindBySubjectAsync(string subject, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(subject)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0198), nameof(subject)); - } + ArgumentException.ThrowIfNullOrEmpty(subject); return ExecuteAsync(cancellationToken); @@ -304,10 +280,7 @@ public class OpenIddictEntityFrameworkTokenStore< /// public virtual async ValueTask GetApplicationIdAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); var context = await Context.GetDbContextAsync(cancellationToken); @@ -336,10 +309,7 @@ public class OpenIddictEntityFrameworkTokenStore< Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); var context = await Context.GetDbContextAsync(cancellationToken); @@ -352,10 +322,7 @@ public class OpenIddictEntityFrameworkTokenStore< /// public virtual async ValueTask GetAuthorizationIdAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); var context = await Context.GetDbContextAsync(cancellationToken); @@ -382,10 +349,7 @@ public class OpenIddictEntityFrameworkTokenStore< /// public virtual ValueTask GetCreationDateAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); if (token.CreationDate is null) { @@ -398,10 +362,7 @@ public class OpenIddictEntityFrameworkTokenStore< /// public virtual ValueTask GetExpirationDateAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); if (token.ExpirationDate is null) { @@ -414,10 +375,7 @@ public class OpenIddictEntityFrameworkTokenStore< /// public virtual ValueTask GetIdAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); return new(ConvertIdentifierToString(token.Id)); } @@ -425,10 +383,7 @@ public class OpenIddictEntityFrameworkTokenStore< /// public virtual ValueTask GetPayloadAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); return new(token.Payload); } @@ -436,10 +391,7 @@ public class OpenIddictEntityFrameworkTokenStore< /// public virtual ValueTask> GetPropertiesAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); if (string.IsNullOrEmpty(token.Properties)) { @@ -471,10 +423,7 @@ public class OpenIddictEntityFrameworkTokenStore< /// public virtual ValueTask GetRedemptionDateAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); if (token.RedemptionDate is null) { @@ -487,10 +436,7 @@ public class OpenIddictEntityFrameworkTokenStore< /// public virtual ValueTask GetReferenceIdAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); return new(token.ReferenceId); } @@ -498,10 +444,7 @@ public class OpenIddictEntityFrameworkTokenStore< /// public virtual ValueTask GetStatusAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); return new(token.Status); } @@ -509,10 +452,7 @@ public class OpenIddictEntityFrameworkTokenStore< /// public virtual ValueTask GetSubjectAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); return new(token.Subject); } @@ -520,10 +460,7 @@ public class OpenIddictEntityFrameworkTokenStore< /// public virtual ValueTask GetTypeAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); return new(token.Type); } @@ -575,10 +512,7 @@ public class OpenIddictEntityFrameworkTokenStore< Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); return ExecuteAsync(cancellationToken); @@ -735,10 +669,7 @@ public class OpenIddictEntityFrameworkTokenStore< /// public virtual async ValueTask RevokeByApplicationIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var context = await Context.GetDbContextAsync(cancellationToken); var key = ConvertIdentifierFromString(identifier); @@ -786,10 +717,7 @@ public class OpenIddictEntityFrameworkTokenStore< /// public virtual async ValueTask RevokeByAuthorizationIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var context = await Context.GetDbContextAsync(cancellationToken); var key = ConvertIdentifierFromString(identifier); @@ -837,10 +765,7 @@ public class OpenIddictEntityFrameworkTokenStore< /// public virtual async ValueTask RevokeBySubjectAsync(string subject, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(subject)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(subject)); - } + ArgumentException.ThrowIfNullOrEmpty(subject); var context = await Context.GetDbContextAsync(cancellationToken); @@ -887,10 +812,7 @@ public class OpenIddictEntityFrameworkTokenStore< /// public virtual async ValueTask SetApplicationIdAsync(TToken token, string? identifier, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); var context = await Context.GetDbContextAsync(cancellationToken); @@ -922,10 +844,7 @@ public class OpenIddictEntityFrameworkTokenStore< /// public virtual async ValueTask SetAuthorizationIdAsync(TToken token, string? identifier, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); var context = await Context.GetDbContextAsync(cancellationToken); @@ -957,10 +876,7 @@ public class OpenIddictEntityFrameworkTokenStore< /// public virtual ValueTask SetCreationDateAsync(TToken token, DateTimeOffset? date, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); token.CreationDate = date?.UtcDateTime; @@ -970,10 +886,7 @@ public class OpenIddictEntityFrameworkTokenStore< /// public virtual ValueTask SetExpirationDateAsync(TToken token, DateTimeOffset? date, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); token.ExpirationDate = date?.UtcDateTime; @@ -983,10 +896,7 @@ public class OpenIddictEntityFrameworkTokenStore< /// public virtual ValueTask SetPayloadAsync(TToken token, string? payload, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); token.Payload = payload; @@ -997,10 +907,7 @@ public class OpenIddictEntityFrameworkTokenStore< public virtual ValueTask SetPropertiesAsync(TToken token, ImmutableDictionary properties, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); if (properties is not { Count: > 0 }) { @@ -1035,10 +942,7 @@ public class OpenIddictEntityFrameworkTokenStore< /// public virtual ValueTask SetRedemptionDateAsync(TToken token, DateTimeOffset? date, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); token.RedemptionDate = date?.UtcDateTime; @@ -1048,10 +952,7 @@ public class OpenIddictEntityFrameworkTokenStore< /// public virtual ValueTask SetReferenceIdAsync(TToken token, string? identifier, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); token.ReferenceId = identifier; @@ -1061,10 +962,7 @@ public class OpenIddictEntityFrameworkTokenStore< /// public virtual ValueTask SetStatusAsync(TToken token, string? status, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); token.Status = status; @@ -1074,10 +972,7 @@ public class OpenIddictEntityFrameworkTokenStore< /// public virtual ValueTask SetSubjectAsync(TToken token, string? subject, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); token.Subject = subject; @@ -1087,10 +982,7 @@ public class OpenIddictEntityFrameworkTokenStore< /// public virtual ValueTask SetTypeAsync(TToken token, string? type, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); token.Type = type; @@ -1100,10 +992,7 @@ public class OpenIddictEntityFrameworkTokenStore< /// public virtual async ValueTask UpdateAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); var context = await Context.GetDbContextAsync(cancellationToken); diff --git a/src/OpenIddict.EntityFrameworkCore.Models/OpenIddict.EntityFrameworkCore.Models.csproj b/src/OpenIddict.EntityFrameworkCore.Models/OpenIddict.EntityFrameworkCore.Models.csproj index 0e1b1448..5fab6cff 100644 --- a/src/OpenIddict.EntityFrameworkCore.Models/OpenIddict.EntityFrameworkCore.Models.csproj +++ b/src/OpenIddict.EntityFrameworkCore.Models/OpenIddict.EntityFrameworkCore.Models.csproj @@ -6,11 +6,7 @@ $(NetCoreTargetFrameworks); $(NetStandardTargetFrameworks) - - - false - System.Diagnostics.CodeAnalysis.StringSyntaxAttribute diff --git a/src/OpenIddict.EntityFrameworkCore/Configurations/OpenIddictEntityFrameworkCoreApplicationConfiguration.cs b/src/OpenIddict.EntityFrameworkCore/Configurations/OpenIddictEntityFrameworkCoreApplicationConfiguration.cs index b15f683d..5abf5010 100644 --- a/src/OpenIddict.EntityFrameworkCore/Configurations/OpenIddictEntityFrameworkCoreApplicationConfiguration.cs +++ b/src/OpenIddict.EntityFrameworkCore/Configurations/OpenIddictEntityFrameworkCoreApplicationConfiguration.cs @@ -31,10 +31,7 @@ public sealed class OpenIddictEntityFrameworkCoreApplicationConfiguration< { public void Configure(EntityTypeBuilder builder) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } + ArgumentNullException.ThrowIfNull(builder); // Warning: optional foreign keys MUST NOT be added as CLR properties because // Entity Framework would throw an exception due to the TKey generic parameter diff --git a/src/OpenIddict.EntityFrameworkCore/Configurations/OpenIddictEntityFrameworkCoreAuthorizationConfiguration.cs b/src/OpenIddict.EntityFrameworkCore/Configurations/OpenIddictEntityFrameworkCoreAuthorizationConfiguration.cs index e908075b..decaa59f 100644 --- a/src/OpenIddict.EntityFrameworkCore/Configurations/OpenIddictEntityFrameworkCoreAuthorizationConfiguration.cs +++ b/src/OpenIddict.EntityFrameworkCore/Configurations/OpenIddictEntityFrameworkCoreAuthorizationConfiguration.cs @@ -31,10 +31,7 @@ public sealed class OpenIddictEntityFrameworkCoreAuthorizationConfiguration< { public void Configure(EntityTypeBuilder builder) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } + ArgumentNullException.ThrowIfNull(builder); // Warning: optional foreign keys MUST NOT be added as CLR properties because // Entity Framework would throw an exception due to the TKey generic parameter diff --git a/src/OpenIddict.EntityFrameworkCore/Configurations/OpenIddictEntityFrameworkCoreScopeConfiguration.cs b/src/OpenIddict.EntityFrameworkCore/Configurations/OpenIddictEntityFrameworkCoreScopeConfiguration.cs index be278753..f26f2995 100644 --- a/src/OpenIddict.EntityFrameworkCore/Configurations/OpenIddictEntityFrameworkCoreScopeConfiguration.cs +++ b/src/OpenIddict.EntityFrameworkCore/Configurations/OpenIddictEntityFrameworkCoreScopeConfiguration.cs @@ -25,10 +25,7 @@ public sealed class OpenIddictEntityFrameworkCoreScopeConfiguration< { public void Configure(EntityTypeBuilder builder) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } + ArgumentNullException.ThrowIfNull(builder); // Warning: optional foreign keys MUST NOT be added as CLR properties because // Entity Framework would throw an exception due to the TKey generic parameter diff --git a/src/OpenIddict.EntityFrameworkCore/Configurations/OpenIddictEntityFrameworkCoreTokenConfiguration.cs b/src/OpenIddict.EntityFrameworkCore/Configurations/OpenIddictEntityFrameworkCoreTokenConfiguration.cs index c9483b35..30d4452a 100644 --- a/src/OpenIddict.EntityFrameworkCore/Configurations/OpenIddictEntityFrameworkCoreTokenConfiguration.cs +++ b/src/OpenIddict.EntityFrameworkCore/Configurations/OpenIddictEntityFrameworkCoreTokenConfiguration.cs @@ -31,10 +31,7 @@ public sealed class OpenIddictEntityFrameworkCoreTokenConfiguration< { public void Configure(EntityTypeBuilder builder) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } + ArgumentNullException.ThrowIfNull(builder); // Warning: optional foreign keys MUST NOT be added as CLR properties because // Entity Framework would throw an exception due to the TKey generic parameter diff --git a/src/OpenIddict.EntityFrameworkCore/OpenIddictEntityFrameworkCoreBuilder.cs b/src/OpenIddict.EntityFrameworkCore/OpenIddictEntityFrameworkCoreBuilder.cs index 1aeb2e61..164e903c 100644 --- a/src/OpenIddict.EntityFrameworkCore/OpenIddictEntityFrameworkCoreBuilder.cs +++ b/src/OpenIddict.EntityFrameworkCore/OpenIddictEntityFrameworkCoreBuilder.cs @@ -39,10 +39,7 @@ public sealed class OpenIddictEntityFrameworkCoreBuilder /// The instance. public OpenIddictEntityFrameworkCoreBuilder Configure(Action configuration) { - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(configuration); Services.Configure(configuration); diff --git a/src/OpenIddict.EntityFrameworkCore/OpenIddictEntityFrameworkCoreCustomizer.cs b/src/OpenIddict.EntityFrameworkCore/OpenIddictEntityFrameworkCoreCustomizer.cs index 4bf1b8ba..9789ebdb 100644 --- a/src/OpenIddict.EntityFrameworkCore/OpenIddictEntityFrameworkCoreCustomizer.cs +++ b/src/OpenIddict.EntityFrameworkCore/OpenIddictEntityFrameworkCoreCustomizer.cs @@ -33,15 +33,8 @@ public sealed class OpenIddictEntityFrameworkCoreCustomizer< /// public override void Customize(ModelBuilder modelBuilder, DbContext context) { - if (modelBuilder is null) - { - throw new ArgumentNullException(nameof(modelBuilder)); - } - - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(modelBuilder); + ArgumentNullException.ThrowIfNull(context); // Register the OpenIddict entity sets. modelBuilder.UseOpenIddict(); diff --git a/src/OpenIddict.EntityFrameworkCore/OpenIddictEntityFrameworkCoreExtensions.cs b/src/OpenIddict.EntityFrameworkCore/OpenIddictEntityFrameworkCoreExtensions.cs index 805cf1af..cd1a04bb 100644 --- a/src/OpenIddict.EntityFrameworkCore/OpenIddictEntityFrameworkCoreExtensions.cs +++ b/src/OpenIddict.EntityFrameworkCore/OpenIddictEntityFrameworkCoreExtensions.cs @@ -24,10 +24,7 @@ public static class OpenIddictEntityFrameworkCoreExtensions /// The instance. public static OpenIddictEntityFrameworkCoreBuilder UseEntityFrameworkCore(this OpenIddictCoreBuilder builder) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } + ArgumentNullException.ThrowIfNull(builder); // Since Entity Framework Core may be used with databases performing case-insensitive // or culture-sensitive comparisons, ensure the additional filtering logic is enforced @@ -64,15 +61,8 @@ public static class OpenIddictEntityFrameworkCoreExtensions public static OpenIddictCoreBuilder UseEntityFrameworkCore( this OpenIddictCoreBuilder builder, Action configuration) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } - - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(builder); + ArgumentNullException.ThrowIfNull(configuration); configuration(builder.UseEntityFrameworkCore()); diff --git a/src/OpenIddict.EntityFrameworkCore/OpenIddictEntityFrameworkCoreHelpers.cs b/src/OpenIddict.EntityFrameworkCore/OpenIddictEntityFrameworkCoreHelpers.cs index a60b702d..ee927da2 100644 --- a/src/OpenIddict.EntityFrameworkCore/OpenIddictEntityFrameworkCoreHelpers.cs +++ b/src/OpenIddict.EntityFrameworkCore/OpenIddictEntityFrameworkCoreHelpers.cs @@ -104,10 +104,7 @@ public static class OpenIddictEntityFrameworkCoreHelpers where TToken : OpenIddictEntityFrameworkCoreToken where TKey : notnull, IEquatable { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } + ArgumentNullException.ThrowIfNull(builder); return ReplaceService>(builder); @@ -199,10 +196,7 @@ public static class OpenIddictEntityFrameworkCoreHelpers where TToken : OpenIddictEntityFrameworkCoreToken where TKey : notnull, IEquatable { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } + ArgumentNullException.ThrowIfNull(builder); return builder .ApplyConfiguration(new OpenIddictEntityFrameworkCoreApplicationConfiguration()) @@ -230,10 +224,7 @@ public static class OpenIddictEntityFrameworkCoreHelpers #endif internal static IAsyncEnumerable AsAsyncEnumerable(this IQueryable source, CancellationToken cancellationToken) { - if (source is null) - { - throw new ArgumentNullException(nameof(source)); - } + ArgumentNullException.ThrowIfNull(source); return ExecuteAsync(source, cancellationToken); @@ -263,10 +254,7 @@ public static class OpenIddictEntityFrameworkCoreHelpers internal static async ValueTask CreateTransactionAsync( this DbContext context, IsolationLevel level, CancellationToken cancellationToken) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: transactions that specify an explicit isolation level are only supported by // relational providers and trying to use them with a different provider results in diff --git a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreApplicationStore.cs b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreApplicationStore.cs index 05a50112..58ac6190 100644 --- a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreApplicationStore.cs +++ b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreApplicationStore.cs @@ -111,10 +111,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< /// public virtual async ValueTask CountAsync(Func, IQueryable> query, CancellationToken cancellationToken) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); var context = await Context.GetDbContextAsync(cancellationToken); @@ -124,10 +121,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< /// public virtual async ValueTask CreateAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); var context = await Context.GetDbContextAsync(cancellationToken); context.Add(application); @@ -138,10 +132,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< /// public virtual async ValueTask DeleteAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); var context = await Context.GetDbContextAsync(cancellationToken); @@ -274,10 +265,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< /// public virtual async ValueTask FindByClientIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var context = await Context.GetDbContextAsync(cancellationToken); @@ -297,10 +285,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< /// public virtual async ValueTask FindByIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var context = await Context.GetDbContextAsync(cancellationToken); var key = ConvertIdentifierFromString(identifier); @@ -322,10 +307,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< public virtual IAsyncEnumerable FindByPostLogoutRedirectUriAsync( [StringSyntax(StringSyntaxAttribute.Uri)] string uri, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(uri)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0143), nameof(uri)); - } + ArgumentException.ThrowIfNullOrEmpty(uri); // To optimize the efficiency of the query a bit, only applications whose stringified // PostLogoutRedirectUris contains the specified URI are returned. Once the applications @@ -358,10 +340,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< public virtual IAsyncEnumerable FindByRedirectUriAsync( [StringSyntax(StringSyntaxAttribute.Uri)] string uri, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(uri)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0143), nameof(uri)); - } + ArgumentException.ThrowIfNullOrEmpty(uri); // To optimize the efficiency of the query a bit, only applications whose stringified // RedirectUris property contains the specified URI are returned. Once the applications @@ -393,10 +372,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< /// public virtual ValueTask GetApplicationTypeAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); return new(application.ApplicationType); } @@ -406,10 +382,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); var context = await Context.GetDbContextAsync(cancellationToken); @@ -419,10 +392,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< /// public virtual ValueTask GetClientIdAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); return new(application.ClientId); } @@ -430,10 +400,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< /// public virtual ValueTask GetClientSecretAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); return new(application.ClientSecret); } @@ -441,10 +408,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< /// public virtual ValueTask GetClientTypeAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); return new(application.ClientType); } @@ -452,10 +416,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< /// public virtual ValueTask GetConsentTypeAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); return new(application.ConsentType); } @@ -463,10 +424,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< /// public virtual ValueTask GetDisplayNameAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); return new(application.DisplayName); } @@ -474,10 +432,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< /// public virtual ValueTask> GetDisplayNamesAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (string.IsNullOrEmpty(application.DisplayNames)) { @@ -515,10 +470,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< /// public virtual ValueTask GetIdAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); return new(ConvertIdentifierToString(application.Id)); } @@ -526,10 +478,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< /// public virtual ValueTask GetJsonWebKeySetAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (string.IsNullOrEmpty(application.JsonWebKeySet)) { @@ -553,10 +502,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< /// public virtual ValueTask> GetPermissionsAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (string.IsNullOrEmpty(application.Permissions)) { @@ -594,10 +540,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< /// public virtual ValueTask> GetPostLogoutRedirectUrisAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (string.IsNullOrEmpty(application.PostLogoutRedirectUris)) { @@ -635,10 +578,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< /// public virtual ValueTask> GetPropertiesAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (string.IsNullOrEmpty(application.Properties)) { @@ -670,10 +610,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< /// public virtual ValueTask> GetRedirectUrisAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (string.IsNullOrEmpty(application.RedirectUris)) { @@ -711,10 +648,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< /// public virtual ValueTask> GetRequirementsAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (string.IsNullOrEmpty(application.Requirements)) { @@ -752,10 +686,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< /// public virtual ValueTask> GetSettingsAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (string.IsNullOrEmpty(application.Settings)) { @@ -834,10 +765,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); return ExecuteAsync(cancellationToken); @@ -856,10 +784,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< public virtual ValueTask SetApplicationTypeAsync(TApplication application, string? type, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); application.ApplicationType = type; @@ -869,10 +794,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< /// public virtual ValueTask SetClientIdAsync(TApplication application, string? identifier, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); application.ClientId = identifier; @@ -882,10 +804,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< /// public virtual ValueTask SetClientSecretAsync(TApplication application, string? secret, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); application.ClientSecret = secret; @@ -895,10 +814,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< /// public virtual ValueTask SetClientTypeAsync(TApplication application, string? type, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); application.ClientType = type; @@ -908,10 +824,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< /// public virtual ValueTask SetConsentTypeAsync(TApplication application, string? type, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); application.ConsentType = type; @@ -921,10 +834,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< /// public virtual ValueTask SetDisplayNameAsync(TApplication application, string? name, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); application.DisplayName = name; @@ -935,10 +845,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< public virtual ValueTask SetDisplayNamesAsync(TApplication application, ImmutableDictionary names, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (names is not { Count: > 0 }) { @@ -973,10 +880,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< /// public virtual ValueTask SetJsonWebKeySetAsync(TApplication application, JsonWebKeySet? set, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); application.JsonWebKeySet = set is not null ? JsonSerializer.Serialize(set, OpenIddictSerializer.Default.JsonWebKeySet) : null; @@ -986,10 +890,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< /// public virtual ValueTask SetPermissionsAsync(TApplication application, ImmutableArray permissions, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (permissions.IsDefaultOrEmpty) { @@ -1024,10 +925,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< public virtual ValueTask SetPostLogoutRedirectUrisAsync(TApplication application, ImmutableArray uris, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (uris.IsDefaultOrEmpty) { @@ -1062,10 +960,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< public virtual ValueTask SetPropertiesAsync(TApplication application, ImmutableDictionary properties, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (properties is not { Count: > 0 }) { @@ -1101,10 +996,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< public virtual ValueTask SetRedirectUrisAsync(TApplication application, ImmutableArray uris, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (uris.IsDefaultOrEmpty) { @@ -1138,10 +1030,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< /// public virtual ValueTask SetRequirementsAsync(TApplication application, ImmutableArray requirements, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (requirements.IsDefaultOrEmpty) { @@ -1176,10 +1065,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< public virtual ValueTask SetSettingsAsync(TApplication application, ImmutableDictionary settings, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (settings is not { Count: > 0 }) { @@ -1214,10 +1100,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore< /// public virtual async ValueTask UpdateAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); var context = await Context.GetDbContextAsync(cancellationToken); diff --git a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreAuthorizationStore.cs b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreAuthorizationStore.cs index 79820d0e..e18d3102 100644 --- a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreAuthorizationStore.cs +++ b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreAuthorizationStore.cs @@ -109,10 +109,7 @@ public class OpenIddictEntityFrameworkCoreAuthorizationStore< /// public virtual async ValueTask CountAsync(Func, IQueryable> query, CancellationToken cancellationToken) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); var context = await Context.GetDbContextAsync(cancellationToken); @@ -122,10 +119,7 @@ public class OpenIddictEntityFrameworkCoreAuthorizationStore< /// public virtual async ValueTask CreateAsync(TAuthorization authorization, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); var context = await Context.GetDbContextAsync(cancellationToken); @@ -137,10 +131,7 @@ public class OpenIddictEntityFrameworkCoreAuthorizationStore< /// public virtual async ValueTask DeleteAsync(TAuthorization authorization, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); var context = await Context.GetDbContextAsync(cancellationToken); @@ -288,10 +279,7 @@ public class OpenIddictEntityFrameworkCoreAuthorizationStore< /// public virtual IAsyncEnumerable FindByApplicationIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); return ExecuteAsync(cancellationToken); @@ -320,10 +308,7 @@ public class OpenIddictEntityFrameworkCoreAuthorizationStore< /// public virtual async ValueTask FindByIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var context = await Context.GetDbContextAsync(cancellationToken); var key = ConvertIdentifierFromString(identifier); @@ -344,10 +329,7 @@ public class OpenIddictEntityFrameworkCoreAuthorizationStore< /// public virtual IAsyncEnumerable FindBySubjectAsync(string subject, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(subject)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0198), nameof(subject)); - } + ArgumentException.ThrowIfNullOrEmpty(subject); return ExecuteAsync(cancellationToken); @@ -368,10 +350,7 @@ public class OpenIddictEntityFrameworkCoreAuthorizationStore< /// public virtual async ValueTask GetApplicationIdAsync(TAuthorization authorization, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); var context = await Context.GetDbContextAsync(cancellationToken); @@ -400,10 +379,7 @@ public class OpenIddictEntityFrameworkCoreAuthorizationStore< Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); var context = await Context.GetDbContextAsync(cancellationToken); @@ -415,10 +391,7 @@ public class OpenIddictEntityFrameworkCoreAuthorizationStore< /// public virtual ValueTask GetCreationDateAsync(TAuthorization authorization, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); if (authorization.CreationDate is null) { @@ -431,10 +404,7 @@ public class OpenIddictEntityFrameworkCoreAuthorizationStore< /// public virtual ValueTask GetIdAsync(TAuthorization authorization, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); return new(ConvertIdentifierToString(authorization.Id)); } @@ -442,10 +412,7 @@ public class OpenIddictEntityFrameworkCoreAuthorizationStore< /// public virtual ValueTask> GetPropertiesAsync(TAuthorization authorization, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); if (string.IsNullOrEmpty(authorization.Properties)) { @@ -477,10 +444,7 @@ public class OpenIddictEntityFrameworkCoreAuthorizationStore< /// public virtual ValueTask> GetScopesAsync(TAuthorization authorization, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); if (string.IsNullOrEmpty(authorization.Scopes)) { @@ -518,10 +482,7 @@ public class OpenIddictEntityFrameworkCoreAuthorizationStore< /// public virtual ValueTask GetStatusAsync(TAuthorization authorization, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); return new(authorization.Status); } @@ -529,10 +490,7 @@ public class OpenIddictEntityFrameworkCoreAuthorizationStore< /// public virtual ValueTask GetSubjectAsync(TAuthorization authorization, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); return new(authorization.Subject); } @@ -540,10 +498,7 @@ public class OpenIddictEntityFrameworkCoreAuthorizationStore< /// public virtual ValueTask GetTypeAsync(TAuthorization authorization, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); return new(authorization.Type); } @@ -595,10 +550,7 @@ public class OpenIddictEntityFrameworkCoreAuthorizationStore< Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); return ExecuteAsync(cancellationToken); @@ -822,10 +774,7 @@ public class OpenIddictEntityFrameworkCoreAuthorizationStore< /// public virtual async ValueTask RevokeByApplicationIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var context = await Context.GetDbContextAsync(cancellationToken); var key = ConvertIdentifierFromString(identifier); @@ -890,10 +839,7 @@ public class OpenIddictEntityFrameworkCoreAuthorizationStore< /// public virtual async ValueTask RevokeBySubjectAsync(string subject, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(subject)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(subject)); - } + ArgumentException.ThrowIfNullOrEmpty(subject); var context = await Context.GetDbContextAsync(cancellationToken); @@ -953,10 +899,7 @@ public class OpenIddictEntityFrameworkCoreAuthorizationStore< public virtual async ValueTask SetApplicationIdAsync(TAuthorization authorization, string? identifier, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); var context = await Context.GetDbContextAsync(cancellationToken); @@ -1008,10 +951,7 @@ public class OpenIddictEntityFrameworkCoreAuthorizationStore< public virtual ValueTask SetCreationDateAsync(TAuthorization authorization, DateTimeOffset? date, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); authorization.CreationDate = date?.UtcDateTime; @@ -1022,10 +962,7 @@ public class OpenIddictEntityFrameworkCoreAuthorizationStore< public virtual ValueTask SetPropertiesAsync(TAuthorization authorization, ImmutableDictionary properties, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); if (properties is not { Count: > 0 }) { @@ -1061,10 +998,7 @@ public class OpenIddictEntityFrameworkCoreAuthorizationStore< public virtual ValueTask SetScopesAsync(TAuthorization authorization, ImmutableArray scopes, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); if (scopes.IsDefaultOrEmpty) { @@ -1099,10 +1033,7 @@ public class OpenIddictEntityFrameworkCoreAuthorizationStore< public virtual ValueTask SetStatusAsync(TAuthorization authorization, string? status, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); authorization.Status = status; @@ -1113,10 +1044,7 @@ public class OpenIddictEntityFrameworkCoreAuthorizationStore< public virtual ValueTask SetSubjectAsync(TAuthorization authorization, string? subject, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); authorization.Subject = subject; @@ -1127,10 +1055,7 @@ public class OpenIddictEntityFrameworkCoreAuthorizationStore< public virtual ValueTask SetTypeAsync(TAuthorization authorization, string? type, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); authorization.Type = type; @@ -1140,10 +1065,7 @@ public class OpenIddictEntityFrameworkCoreAuthorizationStore< /// public virtual async ValueTask UpdateAsync(TAuthorization authorization, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); var context = await Context.GetDbContextAsync(cancellationToken); diff --git a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreScopeStore.cs b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreScopeStore.cs index db300a3d..3a821c51 100644 --- a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreScopeStore.cs +++ b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreScopeStore.cs @@ -97,10 +97,7 @@ public class OpenIddictEntityFrameworkCoreScopeStore< /// public virtual async ValueTask CountAsync(Func, IQueryable> query, CancellationToken cancellationToken) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); var context = await Context.GetDbContextAsync(cancellationToken); @@ -110,10 +107,7 @@ public class OpenIddictEntityFrameworkCoreScopeStore< /// public virtual async ValueTask CreateAsync(TScope scope, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); var context = await Context.GetDbContextAsync(cancellationToken); @@ -125,10 +119,7 @@ public class OpenIddictEntityFrameworkCoreScopeStore< /// public virtual async ValueTask DeleteAsync(TScope scope, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); var context = await Context.GetDbContextAsync(cancellationToken); @@ -151,10 +142,7 @@ public class OpenIddictEntityFrameworkCoreScopeStore< /// public virtual async ValueTask FindByIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var context = await Context.GetDbContextAsync(cancellationToken); var key = ConvertIdentifierFromString(identifier); @@ -175,10 +163,7 @@ public class OpenIddictEntityFrameworkCoreScopeStore< /// public virtual async ValueTask FindByNameAsync(string name, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(name)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0202), nameof(name)); - } + ArgumentException.ThrowIfNullOrEmpty(name); var context = await Context.GetDbContextAsync(cancellationToken); @@ -224,10 +209,7 @@ public class OpenIddictEntityFrameworkCoreScopeStore< public virtual IAsyncEnumerable FindByResourceAsync( string resource, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(resource)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0062), nameof(resource)); - } + ArgumentException.ThrowIfNullOrEmpty(resource); // To optimize the efficiency of the query a bit, only scopes whose stringified // Resources column contains the specified resource are returned. Once the scopes @@ -261,10 +243,7 @@ public class OpenIddictEntityFrameworkCoreScopeStore< Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); var context = await Context.GetDbContextAsync(cancellationToken); @@ -274,10 +253,7 @@ public class OpenIddictEntityFrameworkCoreScopeStore< /// public virtual ValueTask GetDescriptionAsync(TScope scope, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); return new(scope.Description); } @@ -285,10 +261,7 @@ public class OpenIddictEntityFrameworkCoreScopeStore< /// public virtual ValueTask> GetDescriptionsAsync(TScope scope, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); if (string.IsNullOrEmpty(scope.Descriptions)) { @@ -326,10 +299,7 @@ public class OpenIddictEntityFrameworkCoreScopeStore< /// public virtual ValueTask GetDisplayNameAsync(TScope scope, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); return new(scope.DisplayName); } @@ -337,10 +307,7 @@ public class OpenIddictEntityFrameworkCoreScopeStore< /// public virtual ValueTask> GetDisplayNamesAsync(TScope scope, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); if (string.IsNullOrEmpty(scope.DisplayNames)) { @@ -378,10 +345,7 @@ public class OpenIddictEntityFrameworkCoreScopeStore< /// public virtual ValueTask GetIdAsync(TScope scope, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); return new(ConvertIdentifierToString(scope.Id)); } @@ -389,10 +353,7 @@ public class OpenIddictEntityFrameworkCoreScopeStore< /// public virtual ValueTask GetNameAsync(TScope scope, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); return new(scope.Name); } @@ -400,10 +361,7 @@ public class OpenIddictEntityFrameworkCoreScopeStore< /// public virtual ValueTask> GetPropertiesAsync(TScope scope, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); if (string.IsNullOrEmpty(scope.Properties)) { @@ -435,10 +393,7 @@ public class OpenIddictEntityFrameworkCoreScopeStore< /// public virtual ValueTask> GetResourcesAsync(TScope scope, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); if (string.IsNullOrEmpty(scope.Resources)) { @@ -517,10 +472,7 @@ public class OpenIddictEntityFrameworkCoreScopeStore< Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); return ExecuteAsync(cancellationToken); @@ -538,10 +490,7 @@ public class OpenIddictEntityFrameworkCoreScopeStore< /// public virtual ValueTask SetDescriptionAsync(TScope scope, string? description, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); scope.Description = description; @@ -552,10 +501,7 @@ public class OpenIddictEntityFrameworkCoreScopeStore< public virtual ValueTask SetDescriptionsAsync(TScope scope, ImmutableDictionary descriptions, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); if (descriptions is not { Count: > 0 }) { @@ -590,10 +536,7 @@ public class OpenIddictEntityFrameworkCoreScopeStore< /// public virtual ValueTask SetDisplayNameAsync(TScope scope, string? name, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); scope.DisplayName = name; @@ -604,10 +547,7 @@ public class OpenIddictEntityFrameworkCoreScopeStore< public virtual ValueTask SetDisplayNamesAsync(TScope scope, ImmutableDictionary names, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); if (names is not { Count: > 0 }) { @@ -642,10 +582,7 @@ public class OpenIddictEntityFrameworkCoreScopeStore< /// public virtual ValueTask SetNameAsync(TScope scope, string? name, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); scope.Name = name; @@ -656,10 +593,7 @@ public class OpenIddictEntityFrameworkCoreScopeStore< public virtual ValueTask SetPropertiesAsync(TScope scope, ImmutableDictionary properties, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); if (properties is not { Count: > 0 }) { @@ -694,10 +628,7 @@ public class OpenIddictEntityFrameworkCoreScopeStore< /// public virtual ValueTask SetResourcesAsync(TScope scope, ImmutableArray resources, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); if (resources.IsDefaultOrEmpty) { @@ -731,10 +662,7 @@ public class OpenIddictEntityFrameworkCoreScopeStore< /// public virtual async ValueTask UpdateAsync(TScope scope, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); var context = await Context.GetDbContextAsync(cancellationToken); diff --git a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreTokenStore.cs b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreTokenStore.cs index 6971a7d3..a44cdce8 100644 --- a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreTokenStore.cs +++ b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreTokenStore.cs @@ -109,10 +109,7 @@ public class OpenIddictEntityFrameworkCoreTokenStore< /// public virtual async ValueTask CountAsync(Func, IQueryable> query, CancellationToken cancellationToken) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); var context = await Context.GetDbContextAsync(cancellationToken); @@ -122,10 +119,7 @@ public class OpenIddictEntityFrameworkCoreTokenStore< /// public virtual async ValueTask CreateAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); var context = await Context.GetDbContextAsync(cancellationToken); @@ -137,10 +131,7 @@ public class OpenIddictEntityFrameworkCoreTokenStore< /// public virtual async ValueTask DeleteAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); var context = await Context.GetDbContextAsync(cancellationToken); @@ -211,10 +202,7 @@ public class OpenIddictEntityFrameworkCoreTokenStore< /// public virtual IAsyncEnumerable FindByApplicationIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); return ExecuteAsync(cancellationToken); @@ -246,10 +234,7 @@ public class OpenIddictEntityFrameworkCoreTokenStore< /// public virtual IAsyncEnumerable FindByAuthorizationIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); return ExecuteAsync(cancellationToken); @@ -281,10 +266,7 @@ public class OpenIddictEntityFrameworkCoreTokenStore< /// public virtual async ValueTask FindByIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var context = await Context.GetDbContextAsync(cancellationToken); var key = ConvertIdentifierFromString(identifier); @@ -305,10 +287,7 @@ public class OpenIddictEntityFrameworkCoreTokenStore< /// public virtual async ValueTask FindByReferenceIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var context = await Context.GetDbContextAsync(cancellationToken); @@ -328,10 +307,7 @@ public class OpenIddictEntityFrameworkCoreTokenStore< /// public virtual IAsyncEnumerable FindBySubjectAsync(string subject, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(subject)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0198), nameof(subject)); - } + ArgumentException.ThrowIfNullOrEmpty(subject); return ExecuteAsync(cancellationToken); @@ -352,10 +328,7 @@ public class OpenIddictEntityFrameworkCoreTokenStore< /// public virtual async ValueTask GetApplicationIdAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); var context = await Context.GetDbContextAsync(cancellationToken); @@ -384,10 +357,7 @@ public class OpenIddictEntityFrameworkCoreTokenStore< Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); var context = await Context.GetDbContextAsync(cancellationToken); @@ -399,10 +369,7 @@ public class OpenIddictEntityFrameworkCoreTokenStore< /// public virtual async ValueTask GetAuthorizationIdAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); var context = await Context.GetDbContextAsync(cancellationToken); @@ -429,10 +396,7 @@ public class OpenIddictEntityFrameworkCoreTokenStore< /// public virtual ValueTask GetCreationDateAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); if (token.CreationDate is null) { @@ -445,10 +409,7 @@ public class OpenIddictEntityFrameworkCoreTokenStore< /// public virtual ValueTask GetExpirationDateAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); if (token.ExpirationDate is null) { @@ -461,10 +422,7 @@ public class OpenIddictEntityFrameworkCoreTokenStore< /// public virtual ValueTask GetIdAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); return new(ConvertIdentifierToString(token.Id)); } @@ -472,10 +430,7 @@ public class OpenIddictEntityFrameworkCoreTokenStore< /// public virtual ValueTask GetPayloadAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); return new(token.Payload); } @@ -483,10 +438,7 @@ public class OpenIddictEntityFrameworkCoreTokenStore< /// public virtual ValueTask> GetPropertiesAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); if (string.IsNullOrEmpty(token.Properties)) { @@ -518,10 +470,7 @@ public class OpenIddictEntityFrameworkCoreTokenStore< /// public virtual ValueTask GetRedemptionDateAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); if (token.RedemptionDate is null) { @@ -534,10 +483,7 @@ public class OpenIddictEntityFrameworkCoreTokenStore< /// public virtual ValueTask GetReferenceIdAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); return new(token.ReferenceId); } @@ -545,10 +491,7 @@ public class OpenIddictEntityFrameworkCoreTokenStore< /// public virtual ValueTask GetStatusAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); return new(token.Status); } @@ -556,10 +499,7 @@ public class OpenIddictEntityFrameworkCoreTokenStore< /// public virtual ValueTask GetSubjectAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); return new(token.Subject); } @@ -567,10 +507,7 @@ public class OpenIddictEntityFrameworkCoreTokenStore< /// public virtual ValueTask GetTypeAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); return new(token.Type); } @@ -623,10 +560,7 @@ public class OpenIddictEntityFrameworkCoreTokenStore< Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); return ExecuteAsync(cancellationToken); @@ -843,10 +777,7 @@ public class OpenIddictEntityFrameworkCoreTokenStore< /// public virtual async ValueTask RevokeByApplicationIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var context = await Context.GetDbContextAsync(cancellationToken); var key = ConvertIdentifierFromString(identifier); @@ -916,10 +847,7 @@ public class OpenIddictEntityFrameworkCoreTokenStore< /// public virtual async ValueTask RevokeByAuthorizationIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var context = await Context.GetDbContextAsync(cancellationToken); var key = ConvertIdentifierFromString(identifier); @@ -989,10 +917,7 @@ public class OpenIddictEntityFrameworkCoreTokenStore< /// public virtual async ValueTask RevokeBySubjectAsync(string subject, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(subject)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(subject)); - } + ArgumentException.ThrowIfNullOrEmpty(subject); var context = await Context.GetDbContextAsync(cancellationToken); @@ -1054,10 +979,7 @@ public class OpenIddictEntityFrameworkCoreTokenStore< /// public virtual async ValueTask SetApplicationIdAsync(TToken token, string? identifier, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); var context = await Context.GetDbContextAsync(cancellationToken); @@ -1108,10 +1030,7 @@ public class OpenIddictEntityFrameworkCoreTokenStore< /// public virtual async ValueTask SetAuthorizationIdAsync(TToken token, string? identifier, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); var context = await Context.GetDbContextAsync(cancellationToken); @@ -1162,10 +1081,7 @@ public class OpenIddictEntityFrameworkCoreTokenStore< /// public virtual ValueTask SetCreationDateAsync(TToken token, DateTimeOffset? date, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); token.CreationDate = date?.UtcDateTime; @@ -1175,10 +1091,7 @@ public class OpenIddictEntityFrameworkCoreTokenStore< /// public virtual ValueTask SetExpirationDateAsync(TToken token, DateTimeOffset? date, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); token.ExpirationDate = date?.UtcDateTime; @@ -1188,10 +1101,7 @@ public class OpenIddictEntityFrameworkCoreTokenStore< /// public virtual ValueTask SetPayloadAsync(TToken token, string? payload, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); token.Payload = payload; @@ -1202,10 +1112,7 @@ public class OpenIddictEntityFrameworkCoreTokenStore< public virtual ValueTask SetPropertiesAsync(TToken token, ImmutableDictionary properties, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); if (properties is not { Count: > 0 }) { @@ -1240,10 +1147,7 @@ public class OpenIddictEntityFrameworkCoreTokenStore< /// public virtual ValueTask SetRedemptionDateAsync(TToken token, DateTimeOffset? date, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); token.RedemptionDate = date?.UtcDateTime; @@ -1253,10 +1157,7 @@ public class OpenIddictEntityFrameworkCoreTokenStore< /// public virtual ValueTask SetReferenceIdAsync(TToken token, string? identifier, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); token.ReferenceId = identifier; @@ -1266,10 +1167,7 @@ public class OpenIddictEntityFrameworkCoreTokenStore< /// public virtual ValueTask SetStatusAsync(TToken token, string? status, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); token.Status = status; @@ -1279,10 +1177,7 @@ public class OpenIddictEntityFrameworkCoreTokenStore< /// public virtual ValueTask SetSubjectAsync(TToken token, string? subject, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); token.Subject = subject; @@ -1292,10 +1187,7 @@ public class OpenIddictEntityFrameworkCoreTokenStore< /// public virtual ValueTask SetTypeAsync(TToken token, string? type, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); token.Type = type; @@ -1305,10 +1197,7 @@ public class OpenIddictEntityFrameworkCoreTokenStore< /// public virtual async ValueTask UpdateAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); var context = await Context.GetDbContextAsync(cancellationToken); diff --git a/src/OpenIddict.MongoDb.Models/OpenIddict.MongoDb.Models.csproj b/src/OpenIddict.MongoDb.Models/OpenIddict.MongoDb.Models.csproj index b7d3b82d..0588bae2 100644 --- a/src/OpenIddict.MongoDb.Models/OpenIddict.MongoDb.Models.csproj +++ b/src/OpenIddict.MongoDb.Models/OpenIddict.MongoDb.Models.csproj @@ -7,7 +7,6 @@ $(NetCoreTargetFrameworks); netstandard2.1 - true false diff --git a/src/OpenIddict.MongoDb/OpenIddictMongoDbBuilder.cs b/src/OpenIddict.MongoDb/OpenIddictMongoDbBuilder.cs index 140c8e23..9896be74 100644 --- a/src/OpenIddict.MongoDb/OpenIddictMongoDbBuilder.cs +++ b/src/OpenIddict.MongoDb/OpenIddictMongoDbBuilder.cs @@ -39,10 +39,7 @@ public sealed class OpenIddictMongoDbBuilder /// The instance. public OpenIddictMongoDbBuilder Configure(Action configuration) { - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(configuration); Services.Configure(configuration); @@ -124,10 +121,7 @@ public sealed class OpenIddictMongoDbBuilder /// The instance. public OpenIddictMongoDbBuilder SetApplicationsCollectionName(string name) { - if (string.IsNullOrEmpty(name)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0261), nameof(name)); - } + ArgumentException.ThrowIfNullOrEmpty(name); return Configure(options => options.ApplicationsCollectionName = name); } @@ -139,10 +133,7 @@ public sealed class OpenIddictMongoDbBuilder /// The instance. public OpenIddictMongoDbBuilder SetAuthorizationsCollectionName(string name) { - if (string.IsNullOrEmpty(name)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0261), nameof(name)); - } + ArgumentException.ThrowIfNullOrEmpty(name); return Configure(options => options.AuthorizationsCollectionName = name); } @@ -154,10 +145,7 @@ public sealed class OpenIddictMongoDbBuilder /// The instance. public OpenIddictMongoDbBuilder SetScopesCollectionName(string name) { - if (string.IsNullOrEmpty(name)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0261), nameof(name)); - } + ArgumentException.ThrowIfNullOrEmpty(name); return Configure(options => options.ScopesCollectionName = name); } @@ -169,10 +157,7 @@ public sealed class OpenIddictMongoDbBuilder /// The instance. public OpenIddictMongoDbBuilder SetTokensCollectionName(string name) { - if (string.IsNullOrEmpty(name)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0261), nameof(name)); - } + ArgumentException.ThrowIfNullOrEmpty(name); return Configure(options => options.TokensCollectionName = name); } @@ -185,10 +170,7 @@ public sealed class OpenIddictMongoDbBuilder /// The instance. public OpenIddictMongoDbBuilder UseDatabase(IMongoDatabase database) { - if (database is null) - { - throw new ArgumentNullException(nameof(database)); - } + ArgumentNullException.ThrowIfNull(database); return Configure(options => options.Database = database); } diff --git a/src/OpenIddict.MongoDb/OpenIddictMongoDbExtensions.cs b/src/OpenIddict.MongoDb/OpenIddictMongoDbExtensions.cs index 579c5b54..0fa3c6d8 100644 --- a/src/OpenIddict.MongoDb/OpenIddictMongoDbExtensions.cs +++ b/src/OpenIddict.MongoDb/OpenIddictMongoDbExtensions.cs @@ -24,10 +24,7 @@ public static class OpenIddictMongoDbExtensions /// The instance. public static OpenIddictMongoDbBuilder UseMongoDb(this OpenIddictCoreBuilder builder) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } + ArgumentNullException.ThrowIfNull(builder); // Note: Mongo uses simple binary comparison checks by default so the additional // query filtering applied by the default OpenIddict managers can be safely disabled. @@ -61,15 +58,8 @@ public static class OpenIddictMongoDbExtensions public static OpenIddictCoreBuilder UseMongoDb( this OpenIddictCoreBuilder builder, Action configuration) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } - - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(builder); + ArgumentNullException.ThrowIfNull(configuration); configuration(builder.UseMongoDb()); diff --git a/src/OpenIddict.MongoDb/OpenIddictMongoDbHelpers.cs b/src/OpenIddict.MongoDb/OpenIddictMongoDbHelpers.cs index d0d38490..e0555172 100644 --- a/src/OpenIddict.MongoDb/OpenIddictMongoDbHelpers.cs +++ b/src/OpenIddict.MongoDb/OpenIddictMongoDbHelpers.cs @@ -22,10 +22,7 @@ internal static class OpenIddictMongoDbHelpers /// The streamed async enumeration containing the results. internal static IAsyncEnumerable ToAsyncEnumerable(this IAsyncCursorSource source, CancellationToken cancellationToken) { - if (source is null) - { - throw new ArgumentNullException(nameof(source)); - } + ArgumentNullException.ThrowIfNull(source); return ExecuteAsync(source, cancellationToken); @@ -52,10 +49,7 @@ internal static class OpenIddictMongoDbHelpers /// The streamed async enumeration containing the results. internal static IAsyncEnumerable ToAsyncEnumerable(this IQueryable source, CancellationToken cancellationToken) { - if (source is null) - { - throw new ArgumentNullException(nameof(source)); - } + ArgumentNullException.ThrowIfNull(source); return ((IAsyncCursorSource) source).ToAsyncEnumerable(cancellationToken); } diff --git a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbApplicationStore.cs b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbApplicationStore.cs index 9234ed4e..2be96e02 100644 --- a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbApplicationStore.cs +++ b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbApplicationStore.cs @@ -70,10 +70,7 @@ public class OpenIddictMongoDbApplicationStore< public virtual async ValueTask CountAsync( Func, IQueryable> query, CancellationToken cancellationToken) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); var database = await Context.GetDatabaseAsync(cancellationToken); var collection = database.GetCollection(Options.CurrentValue.ApplicationsCollectionName); @@ -84,10 +81,7 @@ public class OpenIddictMongoDbApplicationStore< /// public virtual async ValueTask CreateAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); var database = await Context.GetDatabaseAsync(cancellationToken); var collection = database.GetCollection(Options.CurrentValue.ApplicationsCollectionName); @@ -98,10 +92,7 @@ public class OpenIddictMongoDbApplicationStore< /// public virtual async ValueTask DeleteAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); var database = await Context.GetDatabaseAsync(cancellationToken); var collection = database.GetCollection(Options.CurrentValue.ApplicationsCollectionName); @@ -125,10 +116,7 @@ public class OpenIddictMongoDbApplicationStore< /// public virtual async ValueTask FindByClientIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var database = await Context.GetDatabaseAsync(cancellationToken); var collection = database.GetCollection(Options.CurrentValue.ApplicationsCollectionName); @@ -139,10 +127,7 @@ public class OpenIddictMongoDbApplicationStore< /// public virtual async ValueTask FindByIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var database = await Context.GetDatabaseAsync(cancellationToken); var collection = database.GetCollection(Options.CurrentValue.ApplicationsCollectionName); @@ -155,10 +140,7 @@ public class OpenIddictMongoDbApplicationStore< public virtual IAsyncEnumerable FindByPostLogoutRedirectUriAsync( [StringSyntax(StringSyntaxAttribute.Uri)] string uri, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(uri)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0143), nameof(uri)); - } + ArgumentException.ThrowIfNullOrEmpty(uri); return ExecuteAsync(cancellationToken); @@ -179,10 +161,7 @@ public class OpenIddictMongoDbApplicationStore< public virtual IAsyncEnumerable FindByRedirectUriAsync( [StringSyntax(StringSyntaxAttribute.Uri)] string uri, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(uri)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0143), nameof(uri)); - } + ArgumentException.ThrowIfNullOrEmpty(uri); return ExecuteAsync(cancellationToken); @@ -202,10 +181,7 @@ public class OpenIddictMongoDbApplicationStore< /// public virtual ValueTask GetApplicationTypeAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); return new(application.ApplicationType); } @@ -215,10 +191,7 @@ public class OpenIddictMongoDbApplicationStore< Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); var database = await Context.GetDatabaseAsync(cancellationToken); var collection = database.GetCollection(Options.CurrentValue.ApplicationsCollectionName); @@ -229,10 +202,7 @@ public class OpenIddictMongoDbApplicationStore< /// public virtual ValueTask GetClientIdAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); return new(application.ClientId); } @@ -240,10 +210,7 @@ public class OpenIddictMongoDbApplicationStore< /// public virtual ValueTask GetClientSecretAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); return new(application.ClientSecret); } @@ -251,10 +218,7 @@ public class OpenIddictMongoDbApplicationStore< /// public virtual ValueTask GetClientTypeAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); return new(application.ClientType); } @@ -262,10 +226,7 @@ public class OpenIddictMongoDbApplicationStore< /// public virtual ValueTask GetConsentTypeAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); return new(application.ConsentType); } @@ -273,10 +234,7 @@ public class OpenIddictMongoDbApplicationStore< /// public virtual ValueTask GetDisplayNameAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); return new(application.DisplayName); } @@ -284,10 +242,7 @@ public class OpenIddictMongoDbApplicationStore< /// public virtual ValueTask> GetDisplayNamesAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (application.DisplayNames is not { Count: > 0 }) { @@ -302,10 +257,7 @@ public class OpenIddictMongoDbApplicationStore< /// public virtual ValueTask GetIdAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); return new(application.Id.ToString()); } @@ -313,10 +265,7 @@ public class OpenIddictMongoDbApplicationStore< /// public virtual ValueTask GetJsonWebKeySetAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (application.JsonWebKeySet is null) { @@ -330,10 +279,7 @@ public class OpenIddictMongoDbApplicationStore< public virtual ValueTask> GetPermissionsAsync( TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (application.Permissions is not { Count: > 0 }) { @@ -347,10 +293,7 @@ public class OpenIddictMongoDbApplicationStore< public virtual ValueTask> GetPostLogoutRedirectUrisAsync( TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (application.PostLogoutRedirectUris is not { Count: > 0 }) { @@ -363,10 +306,7 @@ public class OpenIddictMongoDbApplicationStore< /// public virtual ValueTask> GetPropertiesAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (application.Properties is null) { @@ -388,10 +328,7 @@ public class OpenIddictMongoDbApplicationStore< public virtual ValueTask> GetRedirectUrisAsync( TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (application.RedirectUris is not { Count: > 0 }) { @@ -404,10 +341,7 @@ public class OpenIddictMongoDbApplicationStore< /// public virtual ValueTask> GetRequirementsAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (application.Requirements is not { Count: > 0 }) { @@ -420,10 +354,7 @@ public class OpenIddictMongoDbApplicationStore< /// public virtual ValueTask> GetSettingsAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (application.Settings is not { Count: > 0 }) { @@ -478,10 +409,7 @@ public class OpenIddictMongoDbApplicationStore< Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); return ExecuteAsync(cancellationToken); @@ -501,10 +429,7 @@ public class OpenIddictMongoDbApplicationStore< public virtual ValueTask SetApplicationTypeAsync(TApplication application, string? type, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); application.ApplicationType = type; @@ -515,10 +440,7 @@ public class OpenIddictMongoDbApplicationStore< public virtual ValueTask SetClientIdAsync(TApplication application, string? identifier, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); application.ClientId = identifier; @@ -529,10 +451,7 @@ public class OpenIddictMongoDbApplicationStore< public virtual ValueTask SetClientSecretAsync(TApplication application, string? secret, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); application.ClientSecret = secret; @@ -543,10 +462,7 @@ public class OpenIddictMongoDbApplicationStore< public virtual ValueTask SetClientTypeAsync(TApplication application, string? type, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); application.ClientType = type; @@ -557,10 +473,7 @@ public class OpenIddictMongoDbApplicationStore< public virtual ValueTask SetConsentTypeAsync(TApplication application, string? type, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); application.ConsentType = type; @@ -571,10 +484,7 @@ public class OpenIddictMongoDbApplicationStore< public virtual ValueTask SetDisplayNameAsync(TApplication application, string? name, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); application.DisplayName = name; @@ -585,10 +495,7 @@ public class OpenIddictMongoDbApplicationStore< public virtual ValueTask SetDisplayNamesAsync(TApplication application, ImmutableDictionary names, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (names is not { Count: > 0 }) { @@ -608,10 +515,7 @@ public class OpenIddictMongoDbApplicationStore< public virtual ValueTask SetJsonWebKeySetAsync(TApplication application, JsonWebKeySet? set, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); application.JsonWebKeySet = set is not null ? BsonDocument.Parse( JsonSerializer.Serialize(set, OpenIddictSerializer.Default.JsonWebKeySet)) : null; @@ -623,10 +527,7 @@ public class OpenIddictMongoDbApplicationStore< public virtual ValueTask SetPermissionsAsync(TApplication application, ImmutableArray permissions, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (permissions.IsDefaultOrEmpty) { @@ -644,10 +545,7 @@ public class OpenIddictMongoDbApplicationStore< public virtual ValueTask SetPostLogoutRedirectUrisAsync(TApplication application, ImmutableArray uris, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (uris.IsDefaultOrEmpty) { @@ -665,10 +563,7 @@ public class OpenIddictMongoDbApplicationStore< public virtual ValueTask SetPropertiesAsync(TApplication application, ImmutableDictionary properties, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (properties is not { Count: > 0 }) { @@ -704,10 +599,7 @@ public class OpenIddictMongoDbApplicationStore< public virtual ValueTask SetRedirectUrisAsync(TApplication application, ImmutableArray uris, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (uris.IsDefaultOrEmpty) { @@ -725,10 +617,7 @@ public class OpenIddictMongoDbApplicationStore< public virtual ValueTask SetRequirementsAsync(TApplication application, ImmutableArray requirements, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (requirements.IsDefaultOrEmpty) { @@ -746,10 +635,7 @@ public class OpenIddictMongoDbApplicationStore< public virtual ValueTask SetSettingsAsync(TApplication application, ImmutableDictionary settings, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); if (settings is not { Count: > 0 }) { @@ -766,10 +652,7 @@ public class OpenIddictMongoDbApplicationStore< /// public virtual async ValueTask UpdateAsync(TApplication application, CancellationToken cancellationToken) { - if (application is null) - { - throw new ArgumentNullException(nameof(application)); - } + ArgumentNullException.ThrowIfNull(application); // Generate a new concurrency token and attach it // to the application before persisting the changes. diff --git a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbAuthorizationStore.cs b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbAuthorizationStore.cs index 834b678c..58861e18 100644 --- a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbAuthorizationStore.cs +++ b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbAuthorizationStore.cs @@ -68,10 +68,7 @@ public class OpenIddictMongoDbAuthorizationStore< public virtual async ValueTask CountAsync( Func, IQueryable> query, CancellationToken cancellationToken) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); var database = await Context.GetDatabaseAsync(cancellationToken); var collection = database.GetCollection(Options.CurrentValue.AuthorizationsCollectionName); @@ -82,10 +79,7 @@ public class OpenIddictMongoDbAuthorizationStore< /// public virtual async ValueTask CreateAsync(TAuthorization authorization, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); var database = await Context.GetDatabaseAsync(cancellationToken); var collection = database.GetCollection(Options.CurrentValue.AuthorizationsCollectionName); @@ -96,10 +90,7 @@ public class OpenIddictMongoDbAuthorizationStore< /// public virtual async ValueTask DeleteAsync(TAuthorization authorization, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); var database = await Context.GetDatabaseAsync(cancellationToken); var collection = database.GetCollection(Options.CurrentValue.AuthorizationsCollectionName); @@ -164,10 +155,7 @@ public class OpenIddictMongoDbAuthorizationStore< public virtual IAsyncEnumerable FindByApplicationIdAsync( string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); return ExecuteAsync(cancellationToken); @@ -187,10 +175,7 @@ public class OpenIddictMongoDbAuthorizationStore< /// public virtual async ValueTask FindByIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var database = await Context.GetDatabaseAsync(cancellationToken); var collection = database.GetCollection(Options.CurrentValue.AuthorizationsCollectionName); @@ -203,10 +188,7 @@ public class OpenIddictMongoDbAuthorizationStore< public virtual IAsyncEnumerable FindBySubjectAsync( string subject, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(subject)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0198), nameof(subject)); - } + ArgumentException.ThrowIfNullOrEmpty(subject); return ExecuteAsync(cancellationToken); @@ -226,10 +208,7 @@ public class OpenIddictMongoDbAuthorizationStore< /// public virtual ValueTask GetApplicationIdAsync(TAuthorization authorization, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); if (authorization.ApplicationId == ObjectId.Empty) { @@ -244,10 +223,7 @@ public class OpenIddictMongoDbAuthorizationStore< Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); var database = await Context.GetDatabaseAsync(cancellationToken); var collection = database.GetCollection(Options.CurrentValue.AuthorizationsCollectionName); @@ -258,10 +234,7 @@ public class OpenIddictMongoDbAuthorizationStore< /// public virtual ValueTask GetCreationDateAsync(TAuthorization authorization, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); if (authorization.CreationDate is null) { @@ -274,10 +247,7 @@ public class OpenIddictMongoDbAuthorizationStore< /// public virtual ValueTask GetIdAsync(TAuthorization authorization, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); return new(authorization.Id.ToString()); } @@ -285,10 +255,7 @@ public class OpenIddictMongoDbAuthorizationStore< /// public virtual ValueTask> GetPropertiesAsync(TAuthorization authorization, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); if (authorization.Properties is null) { @@ -309,10 +276,7 @@ public class OpenIddictMongoDbAuthorizationStore< /// public virtual ValueTask> GetScopesAsync(TAuthorization authorization, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); if (authorization.Scopes is not { Count: > 0 }) { @@ -325,10 +289,7 @@ public class OpenIddictMongoDbAuthorizationStore< /// public virtual ValueTask GetStatusAsync(TAuthorization authorization, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); return new(authorization.Status); } @@ -336,10 +297,7 @@ public class OpenIddictMongoDbAuthorizationStore< /// public virtual ValueTask GetSubjectAsync(TAuthorization authorization, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); return new(authorization.Subject); } @@ -347,10 +305,7 @@ public class OpenIddictMongoDbAuthorizationStore< /// public virtual ValueTask GetTypeAsync(TAuthorization authorization, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); return new(authorization.Type); } @@ -400,10 +355,7 @@ public class OpenIddictMongoDbAuthorizationStore< Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); return ExecuteAsync(cancellationToken); @@ -490,10 +442,7 @@ public class OpenIddictMongoDbAuthorizationStore< /// public virtual async ValueTask RevokeByApplicationIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var database = await Context.GetDatabaseAsync(cancellationToken); var collection = database.GetCollection(Options.CurrentValue.AuthorizationsCollectionName); @@ -508,10 +457,7 @@ public class OpenIddictMongoDbAuthorizationStore< /// public virtual async ValueTask RevokeBySubjectAsync(string subject, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(subject)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(subject)); - } + ArgumentException.ThrowIfNullOrEmpty(subject); var database = await Context.GetDatabaseAsync(cancellationToken); var collection = database.GetCollection(Options.CurrentValue.AuthorizationsCollectionName); @@ -527,10 +473,7 @@ public class OpenIddictMongoDbAuthorizationStore< public virtual ValueTask SetApplicationIdAsync(TAuthorization authorization, string? identifier, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); if (!string.IsNullOrEmpty(identifier)) { @@ -549,10 +492,7 @@ public class OpenIddictMongoDbAuthorizationStore< public virtual ValueTask SetCreationDateAsync(TAuthorization authorization, DateTimeOffset? date, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); authorization.CreationDate = date?.UtcDateTime; @@ -563,10 +503,7 @@ public class OpenIddictMongoDbAuthorizationStore< public virtual ValueTask SetPropertiesAsync(TAuthorization authorization, ImmutableDictionary properties, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); if (properties is not { Count: > 0 }) { @@ -602,10 +539,7 @@ public class OpenIddictMongoDbAuthorizationStore< public virtual ValueTask SetScopesAsync(TAuthorization authorization, ImmutableArray scopes, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); if (scopes.IsDefaultOrEmpty) { @@ -622,10 +556,7 @@ public class OpenIddictMongoDbAuthorizationStore< /// public virtual ValueTask SetStatusAsync(TAuthorization authorization, string? status, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); authorization.Status = status; @@ -635,10 +566,7 @@ public class OpenIddictMongoDbAuthorizationStore< /// public virtual ValueTask SetSubjectAsync(TAuthorization authorization, string? subject, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); authorization.Subject = subject; @@ -648,10 +576,7 @@ public class OpenIddictMongoDbAuthorizationStore< /// public virtual ValueTask SetTypeAsync(TAuthorization authorization, string? type, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); authorization.Type = type; @@ -661,10 +586,7 @@ public class OpenIddictMongoDbAuthorizationStore< /// public virtual async ValueTask UpdateAsync(TAuthorization authorization, CancellationToken cancellationToken) { - if (authorization is null) - { - throw new ArgumentNullException(nameof(authorization)); - } + ArgumentNullException.ThrowIfNull(authorization); // Generate a new concurrency token and attach it // to the authorization before persisting the changes. diff --git a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbScopeStore.cs b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbScopeStore.cs index fc929dc3..38f36130 100644 --- a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbScopeStore.cs +++ b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbScopeStore.cs @@ -69,10 +69,7 @@ public class OpenIddictMongoDbScopeStore< public virtual async ValueTask CountAsync( Func, IQueryable> query, CancellationToken cancellationToken) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); var database = await Context.GetDatabaseAsync(cancellationToken); var collection = database.GetCollection(Options.CurrentValue.ScopesCollectionName); @@ -83,10 +80,7 @@ public class OpenIddictMongoDbScopeStore< /// public virtual async ValueTask CreateAsync(TScope scope, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); var database = await Context.GetDatabaseAsync(cancellationToken); var collection = database.GetCollection(Options.CurrentValue.ScopesCollectionName); @@ -97,10 +91,7 @@ public class OpenIddictMongoDbScopeStore< /// public virtual async ValueTask DeleteAsync(TScope scope, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); var database = await Context.GetDatabaseAsync(cancellationToken); var collection = database.GetCollection(Options.CurrentValue.ScopesCollectionName); @@ -116,10 +107,7 @@ public class OpenIddictMongoDbScopeStore< /// public virtual async ValueTask FindByIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var database = await Context.GetDatabaseAsync(cancellationToken); var collection = database.GetCollection(Options.CurrentValue.ScopesCollectionName); @@ -130,10 +118,7 @@ public class OpenIddictMongoDbScopeStore< /// public virtual async ValueTask FindByNameAsync(string name, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(name)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0202), nameof(name)); - } + ArgumentException.ThrowIfNullOrEmpty(name); var database = await Context.GetDatabaseAsync(cancellationToken); var collection = database.GetCollection(Options.CurrentValue.ScopesCollectionName); @@ -168,10 +153,7 @@ public class OpenIddictMongoDbScopeStore< /// public virtual IAsyncEnumerable FindByResourceAsync(string resource, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(resource)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0062), nameof(resource)); - } + ArgumentException.ThrowIfNullOrEmpty(resource); return ExecuteAsync(cancellationToken); @@ -192,10 +174,7 @@ public class OpenIddictMongoDbScopeStore< Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); var database = await Context.GetDatabaseAsync(cancellationToken); var collection = database.GetCollection(Options.CurrentValue.ScopesCollectionName); @@ -206,10 +185,7 @@ public class OpenIddictMongoDbScopeStore< /// public virtual ValueTask GetDescriptionAsync(TScope scope, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); return new(scope.Description); } @@ -217,10 +193,7 @@ public class OpenIddictMongoDbScopeStore< /// public virtual ValueTask> GetDescriptionsAsync(TScope scope, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); if (scope.Descriptions is not { Count: > 0 }) { @@ -235,10 +208,7 @@ public class OpenIddictMongoDbScopeStore< /// public virtual ValueTask GetDisplayNameAsync(TScope scope, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); return new(scope.DisplayName); } @@ -246,10 +216,7 @@ public class OpenIddictMongoDbScopeStore< /// public virtual ValueTask> GetDisplayNamesAsync(TScope scope, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); if (scope.DisplayNames is not { Count: > 0 }) { @@ -264,10 +231,7 @@ public class OpenIddictMongoDbScopeStore< /// public virtual ValueTask GetIdAsync(TScope scope, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); return new(scope.Id.ToString()); } @@ -275,10 +239,7 @@ public class OpenIddictMongoDbScopeStore< /// public virtual ValueTask GetNameAsync(TScope scope, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); return new(scope.Name); } @@ -286,10 +247,7 @@ public class OpenIddictMongoDbScopeStore< /// public virtual ValueTask> GetPropertiesAsync(TScope scope, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); if (scope.Properties is null) { @@ -310,10 +268,7 @@ public class OpenIddictMongoDbScopeStore< /// public virtual ValueTask> GetResourcesAsync(TScope scope, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); if (scope.Resources is not { Count: > 0 }) { @@ -368,10 +323,7 @@ public class OpenIddictMongoDbScopeStore< Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); return ExecuteAsync(cancellationToken); @@ -390,10 +342,7 @@ public class OpenIddictMongoDbScopeStore< /// public virtual ValueTask SetDescriptionAsync(TScope scope, string? description, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); scope.Description = description; @@ -404,10 +353,7 @@ public class OpenIddictMongoDbScopeStore< public virtual ValueTask SetDescriptionsAsync(TScope scope, ImmutableDictionary descriptions, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); if (descriptions is not { Count: > 0 }) { @@ -427,10 +373,7 @@ public class OpenIddictMongoDbScopeStore< public virtual ValueTask SetDisplayNamesAsync(TScope scope, ImmutableDictionary names, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); if (names is not { Count: > 0 }) { @@ -449,10 +392,7 @@ public class OpenIddictMongoDbScopeStore< /// public virtual ValueTask SetDisplayNameAsync(TScope scope, string? name, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); scope.DisplayName = name; @@ -462,10 +402,7 @@ public class OpenIddictMongoDbScopeStore< /// public virtual ValueTask SetNameAsync(TScope scope, string? name, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); scope.Name = name; @@ -476,10 +413,7 @@ public class OpenIddictMongoDbScopeStore< public virtual ValueTask SetPropertiesAsync(TScope scope, ImmutableDictionary properties, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); if (properties is not { Count: > 0 }) { @@ -514,10 +448,7 @@ public class OpenIddictMongoDbScopeStore< /// public virtual ValueTask SetResourcesAsync(TScope scope, ImmutableArray resources, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); if (resources.IsDefaultOrEmpty) { @@ -534,10 +465,7 @@ public class OpenIddictMongoDbScopeStore< /// public virtual async ValueTask UpdateAsync(TScope scope, CancellationToken cancellationToken) { - if (scope is null) - { - throw new ArgumentNullException(nameof(scope)); - } + ArgumentNullException.ThrowIfNull(scope); // Generate a new concurrency token and attach it // to the scope before persisting the changes. diff --git a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbTokenStore.cs b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbTokenStore.cs index 31e8305f..af24f193 100644 --- a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbTokenStore.cs +++ b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbTokenStore.cs @@ -68,10 +68,7 @@ public class OpenIddictMongoDbTokenStore< public virtual async ValueTask CountAsync( Func, IQueryable> query, CancellationToken cancellationToken) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); var database = await Context.GetDatabaseAsync(cancellationToken); var collection = database.GetCollection(Options.CurrentValue.TokensCollectionName); @@ -82,10 +79,7 @@ public class OpenIddictMongoDbTokenStore< /// public virtual async ValueTask CreateAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); var database = await Context.GetDatabaseAsync(cancellationToken); var collection = database.GetCollection(Options.CurrentValue.TokensCollectionName); @@ -96,10 +90,7 @@ public class OpenIddictMongoDbTokenStore< /// public virtual async ValueTask DeleteAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); var database = await Context.GetDatabaseAsync(cancellationToken); var collection = database.GetCollection(Options.CurrentValue.TokensCollectionName); @@ -151,10 +142,7 @@ public class OpenIddictMongoDbTokenStore< /// public virtual IAsyncEnumerable FindByApplicationIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); return ExecuteAsync(cancellationToken); @@ -174,10 +162,7 @@ public class OpenIddictMongoDbTokenStore< /// public virtual IAsyncEnumerable FindByAuthorizationIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); return ExecuteAsync(cancellationToken); @@ -197,10 +182,7 @@ public class OpenIddictMongoDbTokenStore< /// public virtual async ValueTask FindByIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var database = await Context.GetDatabaseAsync(cancellationToken); var collection = database.GetCollection(Options.CurrentValue.TokensCollectionName); @@ -211,10 +193,7 @@ public class OpenIddictMongoDbTokenStore< /// public virtual async ValueTask FindByReferenceIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var database = await Context.GetDatabaseAsync(cancellationToken); var collection = database.GetCollection(Options.CurrentValue.TokensCollectionName); @@ -225,10 +204,7 @@ public class OpenIddictMongoDbTokenStore< /// public virtual IAsyncEnumerable FindBySubjectAsync(string subject, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(subject)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0198), nameof(subject)); - } + ArgumentException.ThrowIfNullOrEmpty(subject); return ExecuteAsync(cancellationToken); @@ -247,10 +223,7 @@ public class OpenIddictMongoDbTokenStore< /// public virtual ValueTask GetApplicationIdAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); if (token.ApplicationId == ObjectId.Empty) { @@ -265,10 +238,7 @@ public class OpenIddictMongoDbTokenStore< Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); var database = await Context.GetDatabaseAsync(cancellationToken); var collection = database.GetCollection(Options.CurrentValue.TokensCollectionName); @@ -279,10 +249,7 @@ public class OpenIddictMongoDbTokenStore< /// public virtual ValueTask GetAuthorizationIdAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); if (token.AuthorizationId == ObjectId.Empty) { @@ -295,10 +262,7 @@ public class OpenIddictMongoDbTokenStore< /// public virtual ValueTask GetCreationDateAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); if (token.CreationDate is null) { @@ -311,10 +275,7 @@ public class OpenIddictMongoDbTokenStore< /// public virtual ValueTask GetExpirationDateAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); if (token.ExpirationDate is null) { @@ -327,10 +288,7 @@ public class OpenIddictMongoDbTokenStore< /// public virtual ValueTask GetIdAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); return new(token.Id.ToString()); } @@ -338,10 +296,7 @@ public class OpenIddictMongoDbTokenStore< /// public virtual ValueTask GetPayloadAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); return new(token.Payload); } @@ -349,10 +304,7 @@ public class OpenIddictMongoDbTokenStore< /// public virtual ValueTask> GetPropertiesAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); if (token.Properties is null) { @@ -373,10 +325,7 @@ public class OpenIddictMongoDbTokenStore< /// public virtual ValueTask GetRedemptionDateAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); if (token.RedemptionDate is null) { @@ -389,10 +338,7 @@ public class OpenIddictMongoDbTokenStore< /// public virtual ValueTask GetReferenceIdAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); return new(token.ReferenceId); } @@ -400,10 +346,7 @@ public class OpenIddictMongoDbTokenStore< /// public virtual ValueTask GetStatusAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); return new(token.Status); } @@ -411,10 +354,7 @@ public class OpenIddictMongoDbTokenStore< /// public virtual ValueTask GetSubjectAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); return new(token.Subject); } @@ -422,10 +362,7 @@ public class OpenIddictMongoDbTokenStore< /// public virtual ValueTask GetTypeAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); return new(token.Type); } @@ -475,10 +412,7 @@ public class OpenIddictMongoDbTokenStore< Func, TState, IQueryable> query, TState state, CancellationToken cancellationToken) { - if (query is null) - { - throw new ArgumentNullException(nameof(query)); - } + ArgumentNullException.ThrowIfNull(query); return ExecuteAsync(cancellationToken); @@ -566,10 +500,7 @@ public class OpenIddictMongoDbTokenStore< /// public virtual async ValueTask RevokeByApplicationIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var database = await Context.GetDatabaseAsync(cancellationToken); var collection = database.GetCollection(Options.CurrentValue.TokensCollectionName); @@ -584,10 +515,7 @@ public class OpenIddictMongoDbTokenStore< /// public virtual async ValueTask RevokeByAuthorizationIdAsync(string identifier, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); var database = await Context.GetDatabaseAsync(cancellationToken); var collection = database.GetCollection(Options.CurrentValue.TokensCollectionName); @@ -602,10 +530,7 @@ public class OpenIddictMongoDbTokenStore< /// public virtual async ValueTask RevokeBySubjectAsync(string subject, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(subject)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0195), nameof(subject)); - } + ArgumentException.ThrowIfNullOrEmpty(subject); var database = await Context.GetDatabaseAsync(cancellationToken); var collection = database.GetCollection(Options.CurrentValue.TokensCollectionName); @@ -620,10 +545,7 @@ public class OpenIddictMongoDbTokenStore< /// public virtual ValueTask SetApplicationIdAsync(TToken token, string? identifier, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); if (!string.IsNullOrEmpty(identifier)) { @@ -641,10 +563,7 @@ public class OpenIddictMongoDbTokenStore< /// public virtual ValueTask SetAuthorizationIdAsync(TToken token, string? identifier, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); if (!string.IsNullOrEmpty(identifier)) { @@ -662,10 +581,7 @@ public class OpenIddictMongoDbTokenStore< /// public virtual ValueTask SetCreationDateAsync(TToken token, DateTimeOffset? date, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); token.CreationDate = date?.UtcDateTime; @@ -675,10 +591,7 @@ public class OpenIddictMongoDbTokenStore< /// public virtual ValueTask SetExpirationDateAsync(TToken token, DateTimeOffset? date, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); token.ExpirationDate = date?.UtcDateTime; @@ -688,10 +601,7 @@ public class OpenIddictMongoDbTokenStore< /// public virtual ValueTask SetPayloadAsync(TToken token, string? payload, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); token.Payload = payload; @@ -702,10 +612,7 @@ public class OpenIddictMongoDbTokenStore< public virtual ValueTask SetPropertiesAsync(TToken token, ImmutableDictionary properties, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); if (properties is not { Count: > 0 }) { @@ -740,10 +647,7 @@ public class OpenIddictMongoDbTokenStore< /// public virtual ValueTask SetRedemptionDateAsync(TToken token, DateTimeOffset? date, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); token.RedemptionDate = date?.UtcDateTime; @@ -753,10 +657,7 @@ public class OpenIddictMongoDbTokenStore< /// public virtual ValueTask SetReferenceIdAsync(TToken token, string? identifier, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); token.ReferenceId = identifier; @@ -766,10 +667,7 @@ public class OpenIddictMongoDbTokenStore< /// public virtual ValueTask SetStatusAsync(TToken token, string? status, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); token.Status = status; @@ -779,10 +677,7 @@ public class OpenIddictMongoDbTokenStore< /// public virtual ValueTask SetSubjectAsync(TToken token, string? subject, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); token.Subject = subject; @@ -792,10 +687,7 @@ public class OpenIddictMongoDbTokenStore< /// public virtual ValueTask SetTypeAsync(TToken token, string? type, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); token.Type = type; @@ -805,10 +697,7 @@ public class OpenIddictMongoDbTokenStore< /// public virtual async ValueTask UpdateAsync(TToken token, CancellationToken cancellationToken) { - if (token is null) - { - throw new ArgumentNullException(nameof(token)); - } + ArgumentNullException.ThrowIfNull(token); // Generate a new concurrency token and attach it // to the token before persisting the changes. diff --git a/src/OpenIddict.Owin/OpenIddict.Owin.csproj b/src/OpenIddict.Owin/OpenIddict.Owin.csproj index 0e99088b..4fc52a06 100644 --- a/src/OpenIddict.Owin/OpenIddict.Owin.csproj +++ b/src/OpenIddict.Owin/OpenIddict.Owin.csproj @@ -4,6 +4,7 @@ $(NetFrameworkTargetFrameworks) false false + false false diff --git a/src/OpenIddict.Quartz/OpenIddictQuartzBuilder.cs b/src/OpenIddict.Quartz/OpenIddictQuartzBuilder.cs index 67b76586..3a57d795 100644 --- a/src/OpenIddict.Quartz/OpenIddictQuartzBuilder.cs +++ b/src/OpenIddict.Quartz/OpenIddictQuartzBuilder.cs @@ -35,10 +35,7 @@ public sealed class OpenIddictQuartzBuilder /// The instance. public OpenIddictQuartzBuilder Configure(Action configuration) { - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(configuration); Services.Configure(configuration); @@ -66,10 +63,7 @@ public sealed class OpenIddictQuartzBuilder /// The instance. public OpenIddictQuartzBuilder SetMaximumRefireCount(int count) { - if (count < 0) - { - throw new ArgumentOutOfRangeException(nameof(count), SR.GetResourceString(SR.ID0279)); - } + ArgumentOutOfRangeException.ThrowIfNegative(count); return Configure(options => options.MaximumRefireCount = count); } @@ -81,10 +75,7 @@ public sealed class OpenIddictQuartzBuilder /// The instance. public OpenIddictQuartzBuilder SetMinimumAuthorizationLifespan(TimeSpan lifespan) { - if (lifespan < TimeSpan.FromMinutes(10)) - { - throw new ArgumentOutOfRangeException(nameof(lifespan), SR.GetResourceString(SR.ID0280)); - } + ArgumentOutOfRangeException.ThrowIfLessThan(lifespan, TimeSpan.FromMinutes(10)); return Configure(options => options.MinimumAuthorizationLifespan = lifespan); } @@ -96,10 +87,7 @@ public sealed class OpenIddictQuartzBuilder /// The instance. public OpenIddictQuartzBuilder SetMinimumTokenLifespan(TimeSpan lifespan) { - if (lifespan < TimeSpan.FromMinutes(10)) - { - throw new ArgumentOutOfRangeException(nameof(lifespan), SR.GetResourceString(SR.ID0280)); - } + ArgumentOutOfRangeException.ThrowIfLessThan(lifespan, TimeSpan.FromMinutes(10)); return Configure(options => options.MinimumTokenLifespan = lifespan); } diff --git a/src/OpenIddict.Quartz/OpenIddictQuartzConfiguration.cs b/src/OpenIddict.Quartz/OpenIddictQuartzConfiguration.cs index 90ff5e15..94966814 100644 --- a/src/OpenIddict.Quartz/OpenIddictQuartzConfiguration.cs +++ b/src/OpenIddict.Quartz/OpenIddictQuartzConfiguration.cs @@ -28,10 +28,7 @@ public sealed class OpenIddictQuartzConfiguration : IConfigureOptions public void Configure(QuartzOptions options) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); options.AddJob(builder => { diff --git a/src/OpenIddict.Quartz/OpenIddictQuartzExtensions.cs b/src/OpenIddict.Quartz/OpenIddictQuartzExtensions.cs index ea0918ee..628cd50b 100644 --- a/src/OpenIddict.Quartz/OpenIddictQuartzExtensions.cs +++ b/src/OpenIddict.Quartz/OpenIddictQuartzExtensions.cs @@ -23,10 +23,7 @@ public static class OpenIddictQuartzExtensions /// The instance. public static OpenIddictQuartzBuilder UseQuartz(this OpenIddictCoreBuilder builder) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } + ArgumentNullException.ThrowIfNull(builder); builder.Services.AddQuartz(); @@ -54,15 +51,8 @@ public static class OpenIddictQuartzExtensions public static OpenIddictCoreBuilder UseQuartz( this OpenIddictCoreBuilder builder, Action configuration) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } - - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(builder); + ArgumentNullException.ThrowIfNull(configuration); configuration(builder.UseQuartz()); diff --git a/src/OpenIddict.Quartz/OpenIddictQuartzJob.cs b/src/OpenIddict.Quartz/OpenIddictQuartzJob.cs index a3ba16c9..98f69c6c 100644 --- a/src/OpenIddict.Quartz/OpenIddictQuartzJob.cs +++ b/src/OpenIddict.Quartz/OpenIddictQuartzJob.cs @@ -45,10 +45,7 @@ public sealed class OpenIddictQuartzJob : IJob /// public async Task Execute(IJobExecutionContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); List? exceptions = null; diff --git a/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreBuilder.cs b/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreBuilder.cs index 245bbb83..f5554df1 100644 --- a/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreBuilder.cs +++ b/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreBuilder.cs @@ -37,10 +37,7 @@ public sealed class OpenIddictServerAspNetCoreBuilder /// The instance. public OpenIddictServerAspNetCoreBuilder Configure(Action configuration) { - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(configuration); Services.Configure(configuration); @@ -140,10 +137,7 @@ public sealed class OpenIddictServerAspNetCoreBuilder /// The instance. public OpenIddictServerAspNetCoreBuilder SetRealm(string realm) { - if (string.IsNullOrEmpty(realm)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0107), nameof(realm)); - } + ArgumentException.ThrowIfNullOrEmpty(realm); return Configure(options => options.Realm = realm); } diff --git a/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreConfiguration.cs b/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreConfiguration.cs index 35454181..58052a70 100644 --- a/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreConfiguration.cs +++ b/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreConfiguration.cs @@ -21,10 +21,7 @@ public sealed class OpenIddictServerAspNetCoreConfiguration : IConfigureOptions< /// public void Configure(AuthenticationOptions options) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); // If a handler was already registered and the type doesn't correspond to the OpenIddict handler, throw an exception. if (options.SchemeMap.TryGetValue(OpenIddictServerAspNetCoreDefaults.AuthenticationScheme, out var builder) && @@ -40,10 +37,7 @@ public sealed class OpenIddictServerAspNetCoreConfiguration : IConfigureOptions< /// public void Configure(OpenIddictServerOptions options) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); // Register the built-in event handlers used by the OpenIddict ASP.NET Core server components. options.Handlers.AddRange(OpenIddictServerAspNetCoreHandlers.DefaultHandlers); @@ -55,10 +49,7 @@ public sealed class OpenIddictServerAspNetCoreConfiguration : IConfigureOptions< /// public void PostConfigure(string? name, AuthenticationOptions options) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); if (!TryValidate(options.SchemeMap, options.DefaultAuthenticateScheme) || !TryValidate(options.SchemeMap, options.DefaultChallengeScheme) || @@ -106,10 +97,7 @@ public sealed class OpenIddictServerAspNetCoreConfiguration : IConfigureOptions< /// public void PostConfigure(string? name, OpenIddictServerAspNetCoreOptions options) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); if (options.EnableErrorPassthrough && options.EnableStatusCodePagesIntegration) { diff --git a/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreExtensions.cs b/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreExtensions.cs index 0cebf2c1..ea6cebb6 100644 --- a/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreExtensions.cs +++ b/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreExtensions.cs @@ -24,10 +24,7 @@ public static class OpenIddictServerAspNetCoreExtensions /// The instance. public static OpenIddictServerAspNetCoreBuilder UseAspNetCore(this OpenIddictServerBuilder builder) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } + ArgumentNullException.ThrowIfNull(builder); builder.Services.AddAuthentication(); @@ -75,15 +72,8 @@ public static class OpenIddictServerAspNetCoreExtensions public static OpenIddictServerBuilder UseAspNetCore( this OpenIddictServerBuilder builder, Action configuration) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } - - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(builder); + ArgumentNullException.ThrowIfNull(configuration); configuration(builder.UseAspNetCore()); diff --git a/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreHandler.cs b/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreHandler.cs index c51ae42f..87034bda 100644 --- a/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreHandler.cs +++ b/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreHandler.cs @@ -29,7 +29,7 @@ public sealed class OpenIddictServerAspNetCoreHandler : AuthenticationHandler /// Creates a new instance of the class. /// -#if SUPPORTS_TIME_PROVIDER +#if SUPPORTS_AUTHENTICATION_HANDLER_TIME_PROVIDER_ARGUMENT public OpenIddictServerAspNetCoreHandler( IOpenIddictServerDispatcher dispatcher, IOpenIddictServerFactory factory, @@ -448,10 +448,7 @@ public sealed class OpenIddictServerAspNetCoreHandler : AuthenticationHandler public async Task SignInAsync(ClaimsPrincipal user, AuthenticationProperties? properties) { - if (user is null) - { - throw new ArgumentNullException(nameof(user)); - } + ArgumentNullException.ThrowIfNull(user); var transaction = Context.Features.Get()?.Transaction ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0112)); diff --git a/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreHandlerFilters.cs b/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreHandlerFilters.cs index b1755757..b22193cc 100644 --- a/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreHandlerFilters.cs +++ b/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreHandlerFilters.cs @@ -30,10 +30,7 @@ public static class OpenIddictServerAspNetCoreHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(_options.CurrentValue.EnableAuthorizationEndpointPassthrough); } @@ -53,10 +50,7 @@ public static class OpenIddictServerAspNetCoreHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(_options.CurrentValue.EnableEndSessionEndpointPassthrough); } @@ -75,10 +69,7 @@ public static class OpenIddictServerAspNetCoreHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(_options.CurrentValue.EnableErrorPassthrough); } @@ -92,10 +83,7 @@ public static class OpenIddictServerAspNetCoreHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.Transaction.GetHttpRequest() is not null); } @@ -114,10 +102,7 @@ public static class OpenIddictServerAspNetCoreHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(_options.CurrentValue.EnableStatusCodePagesIntegration); } @@ -136,10 +121,7 @@ public static class OpenIddictServerAspNetCoreHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!_options.CurrentValue.DisableTransportSecurityRequirement); } @@ -159,10 +141,7 @@ public static class OpenIddictServerAspNetCoreHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(_options.CurrentValue.EnableTokenEndpointPassthrough); } @@ -182,10 +161,7 @@ public static class OpenIddictServerAspNetCoreHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(_options.CurrentValue.EnableUserInfoEndpointPassthrough); } @@ -205,10 +181,7 @@ public static class OpenIddictServerAspNetCoreHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(_options.CurrentValue.EnableEndUserVerificationEndpointPassthrough); } diff --git a/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreHandlers.Authentication.cs b/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreHandlers.Authentication.cs index 44d38372..f7d75027 100644 --- a/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreHandlers.Authentication.cs +++ b/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreHandlers.Authentication.cs @@ -79,10 +79,7 @@ public static partial class OpenIddictServerAspNetCoreHandlers /// public ValueTask HandleAsync(ApplyAuthorizationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context is not { BaseUri.IsAbsoluteUri: true, RequestUri.IsAbsoluteUri: true }) { @@ -153,10 +150,7 @@ public static partial class OpenIddictServerAspNetCoreHandlers /// public async ValueTask HandleAsync(ApplyAuthorizationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to ASP.NET Core requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. @@ -239,10 +233,7 @@ public static partial class OpenIddictServerAspNetCoreHandlers /// public ValueTask HandleAsync(ApplyAuthorizationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to ASP.NET Core requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. @@ -310,10 +301,7 @@ public static partial class OpenIddictServerAspNetCoreHandlers /// public ValueTask HandleAsync(ApplyAuthorizationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to ASP.NET Core requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. diff --git a/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreHandlers.Device.cs b/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreHandlers.Device.cs index e62ca614..2f27242f 100644 --- a/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreHandlers.Device.cs +++ b/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreHandlers.Device.cs @@ -74,10 +74,7 @@ public static partial class OpenIddictServerAspNetCoreHandlers /// public ValueTask HandleAsync(ApplyEndUserVerificationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to ASP.NET Core requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. diff --git a/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreHandlers.Session.cs b/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreHandlers.Session.cs index 968033d2..ee677d8f 100644 --- a/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreHandlers.Session.cs +++ b/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreHandlers.Session.cs @@ -61,10 +61,7 @@ public static partial class OpenIddictServerAspNetCoreHandlers /// public ValueTask HandleAsync(ApplyEndSessionResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context is not { BaseUri.IsAbsoluteUri: true, RequestUri.IsAbsoluteUri: true }) { @@ -130,10 +127,7 @@ public static partial class OpenIddictServerAspNetCoreHandlers /// public ValueTask HandleAsync(ApplyEndSessionResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to ASP.NET Core requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. @@ -200,10 +194,7 @@ public static partial class OpenIddictServerAspNetCoreHandlers /// public ValueTask HandleAsync(ApplyEndSessionResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to ASP.NET Core requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. diff --git a/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreHandlers.cs b/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreHandlers.cs index 328120de..ece0221a 100644 --- a/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreHandlers.cs +++ b/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreHandlers.cs @@ -80,10 +80,7 @@ public static partial class OpenIddictServerAspNetCoreHandlers /// public ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to ASP.NET Core requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. @@ -134,10 +131,7 @@ public static partial class OpenIddictServerAspNetCoreHandlers /// public ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to ASP.NET Core requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. @@ -179,10 +173,7 @@ public static partial class OpenIddictServerAspNetCoreHandlers /// public ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to ASP.NET Core requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. @@ -225,10 +216,7 @@ public static partial class OpenIddictServerAspNetCoreHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var properties = context.Transaction.GetProperty(typeof(AuthenticationProperties).FullName!); if (properties is { Items.Count: > 0 }) @@ -287,10 +275,7 @@ public static partial class OpenIddictServerAspNetCoreHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var properties = context.Transaction.GetProperty(typeof(AuthenticationProperties).FullName!); if (properties is not null) @@ -326,10 +311,7 @@ public static partial class OpenIddictServerAspNetCoreHandlers /// public ValueTask HandleAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var properties = context.Transaction.GetProperty(typeof(AuthenticationProperties).FullName!); if (properties is { Items.Count: > 0 }) @@ -389,10 +371,7 @@ public static partial class OpenIddictServerAspNetCoreHandlers /// public ValueTask HandleAsync(ProcessSignOutContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var properties = context.Transaction.GetProperty(typeof(AuthenticationProperties).FullName!); if (properties is { Items.Count: > 0 }) @@ -451,10 +430,7 @@ public static partial class OpenIddictServerAspNetCoreHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to ASP.NET Core requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. @@ -502,10 +478,7 @@ public static partial class OpenIddictServerAspNetCoreHandlers /// public async ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to ASP.NET Core requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. @@ -582,10 +555,7 @@ public static partial class OpenIddictServerAspNetCoreHandlers /// public async ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to ASP.NET Core requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. @@ -658,10 +628,7 @@ public static partial class OpenIddictServerAspNetCoreHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Transaction.Request is not null, SR.GetResourceString(SR.ID4008)); @@ -727,10 +694,7 @@ public static partial class OpenIddictServerAspNetCoreHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Transaction.Request is not null, SR.GetResourceString(SR.ID4008)); @@ -826,10 +790,7 @@ public static partial class OpenIddictServerAspNetCoreHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to ASP.NET Core requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. @@ -874,10 +835,7 @@ public static partial class OpenIddictServerAspNetCoreHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); switch (context.EndpointType) { @@ -924,10 +882,7 @@ public static partial class OpenIddictServerAspNetCoreHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Transaction.Response is not null, SR.GetResourceString(SR.ID4007)); @@ -990,10 +945,7 @@ public static partial class OpenIddictServerAspNetCoreHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to ASP.NET Core requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. @@ -1034,10 +986,7 @@ public static partial class OpenIddictServerAspNetCoreHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Transaction.Response is not null, SR.GetResourceString(SR.ID4007)); @@ -1151,10 +1100,7 @@ public static partial class OpenIddictServerAspNetCoreHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to ASP.NET Core requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. @@ -1199,10 +1145,7 @@ public static partial class OpenIddictServerAspNetCoreHandlers /// public async ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Transaction.Response is not null, SR.GetResourceString(SR.ID4007)); @@ -1258,10 +1201,7 @@ public static partial class OpenIddictServerAspNetCoreHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Transaction.Response is not null, SR.GetResourceString(SR.ID4007)); @@ -1298,10 +1238,7 @@ public static partial class OpenIddictServerAspNetCoreHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to ASP.NET Core requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. @@ -1354,10 +1291,7 @@ public static partial class OpenIddictServerAspNetCoreHandlers /// public async ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to ASP.NET Core requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. @@ -1428,10 +1362,7 @@ public static partial class OpenIddictServerAspNetCoreHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.Logger.LogInformation(6145, SR.GetResourceString(SR.ID6145)); context.HandleRequest(); diff --git a/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreHelpers.cs b/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreHelpers.cs index 94198355..b441d1ce 100644 --- a/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreHelpers.cs +++ b/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreHelpers.cs @@ -21,10 +21,7 @@ public static class OpenIddictServerAspNetCoreHelpers /// The instance or if it couldn't be found. public static HttpRequest? GetHttpRequest(this OpenIddictServerTransaction transaction) { - if (transaction is null) - { - throw new ArgumentNullException(nameof(transaction)); - } + ArgumentNullException.ThrowIfNull(transaction); if (!transaction.Properties.TryGetValue(typeof(HttpRequest).FullName!, out object? property)) { @@ -46,10 +43,7 @@ public static class OpenIddictServerAspNetCoreHelpers /// The . public static OpenIddictServerEndpointType GetOpenIddictServerEndpointType(this HttpContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return context.Features.Get()?.Transaction?.EndpointType ?? default; } @@ -61,10 +55,7 @@ public static class OpenIddictServerAspNetCoreHelpers /// The instance or null if it couldn't be found. public static OpenIddictRequest? GetOpenIddictServerRequest(this HttpContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return context.Features.Get()?.Transaction?.Request; } @@ -76,10 +67,7 @@ public static class OpenIddictServerAspNetCoreHelpers /// The instance or null if it couldn't be found. public static OpenIddictResponse? GetOpenIddictServerResponse(this HttpContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return context.Features.Get()?.Transaction?.Response; } diff --git a/src/OpenIddict.Server.DataProtection/OpenIddictServerDataProtectionBuilder.cs b/src/OpenIddict.Server.DataProtection/OpenIddictServerDataProtectionBuilder.cs index 5a75d800..2d75eed1 100644 --- a/src/OpenIddict.Server.DataProtection/OpenIddictServerDataProtectionBuilder.cs +++ b/src/OpenIddict.Server.DataProtection/OpenIddictServerDataProtectionBuilder.cs @@ -37,10 +37,7 @@ public sealed class OpenIddictServerDataProtectionBuilder /// The instance. public OpenIddictServerDataProtectionBuilder Configure(Action configuration) { - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(configuration); Services.Configure(configuration); @@ -55,10 +52,7 @@ public sealed class OpenIddictServerDataProtectionBuilder /// The instance. public OpenIddictServerDataProtectionBuilder UseDataProtectionProvider(IDataProtectionProvider provider) { - if (provider is null) - { - throw new ArgumentNullException(nameof(provider)); - } + ArgumentNullException.ThrowIfNull(provider); return Configure(options => options.DataProtectionProvider = provider); } @@ -70,10 +64,7 @@ public sealed class OpenIddictServerDataProtectionBuilder /// The instance. public OpenIddictServerDataProtectionBuilder UseFormatter(IOpenIddictServerDataProtectionFormatter formatter) { - if (formatter is null) - { - throw new ArgumentNullException(nameof(formatter)); - } + ArgumentNullException.ThrowIfNull(formatter); return Configure(options => options.Formatter = formatter); } diff --git a/src/OpenIddict.Server.DataProtection/OpenIddictServerDataProtectionConfiguration.cs b/src/OpenIddict.Server.DataProtection/OpenIddictServerDataProtectionConfiguration.cs index 2b8f1830..021d6bee 100644 --- a/src/OpenIddict.Server.DataProtection/OpenIddictServerDataProtectionConfiguration.cs +++ b/src/OpenIddict.Server.DataProtection/OpenIddictServerDataProtectionConfiguration.cs @@ -29,10 +29,7 @@ public sealed class OpenIddictServerDataProtectionConfiguration : IConfigureOpti /// public void Configure(OpenIddictServerOptions options) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); // Register the built-in event handlers used by the OpenIddict Data Protection server components. options.Handlers.AddRange(OpenIddictServerDataProtectionHandlers.DefaultHandlers); @@ -41,10 +38,7 @@ public sealed class OpenIddictServerDataProtectionConfiguration : IConfigureOpti /// public void PostConfigure(string? name, OpenIddictServerDataProtectionOptions options) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); options.DataProtectionProvider ??= _dataProtectionProvider; } diff --git a/src/OpenIddict.Server.DataProtection/OpenIddictServerDataProtectionExtensions.cs b/src/OpenIddict.Server.DataProtection/OpenIddictServerDataProtectionExtensions.cs index 1825deae..2efd4805 100644 --- a/src/OpenIddict.Server.DataProtection/OpenIddictServerDataProtectionExtensions.cs +++ b/src/OpenIddict.Server.DataProtection/OpenIddictServerDataProtectionExtensions.cs @@ -25,10 +25,7 @@ public static class OpenIddictServerDataProtectionExtensions /// The instance. public static OpenIddictServerDataProtectionBuilder UseDataProtection(this OpenIddictServerBuilder builder) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } + ArgumentNullException.ThrowIfNull(builder); builder.Services.AddDataProtection(); @@ -60,15 +57,8 @@ public static class OpenIddictServerDataProtectionExtensions public static OpenIddictServerBuilder UseDataProtection( this OpenIddictServerBuilder builder, Action configuration) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } - - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(builder); + ArgumentNullException.ThrowIfNull(configuration); configuration(builder.UseDataProtection()); diff --git a/src/OpenIddict.Server.DataProtection/OpenIddictServerDataProtectionFormatter.cs b/src/OpenIddict.Server.DataProtection/OpenIddictServerDataProtectionFormatter.cs index 50afca1a..5ce6913a 100644 --- a/src/OpenIddict.Server.DataProtection/OpenIddictServerDataProtectionFormatter.cs +++ b/src/OpenIddict.Server.DataProtection/OpenIddictServerDataProtectionFormatter.cs @@ -20,10 +20,7 @@ public sealed class OpenIddictServerDataProtectionFormatter : IOpenIddictServerD { public ClaimsPrincipal ReadToken(BinaryReader reader) { - if (reader is null) - { - throw new ArgumentNullException(nameof(reader)); - } + ArgumentNullException.ThrowIfNull(reader); var (principal, properties) = Read(reader); @@ -191,15 +188,8 @@ public sealed class OpenIddictServerDataProtectionFormatter : IOpenIddictServerD public void WriteToken(BinaryWriter writer, ClaimsPrincipal principal) { - if (writer is null) - { - throw new ArgumentNullException(nameof(writer)); - } - - if (principal is null) - { - throw new ArgumentNullException(nameof(principal)); - } + ArgumentNullException.ThrowIfNull(writer); + ArgumentNullException.ThrowIfNull(principal); var properties = new Dictionary(); @@ -321,15 +311,8 @@ public sealed class OpenIddictServerDataProtectionFormatter : IOpenIddictServerD static void WriteClaim(BinaryWriter writer, Claim claim) { - if (writer is null) - { - throw new ArgumentNullException(nameof(writer)); - } - - if (claim is null) - { - throw new ArgumentNullException(nameof(claim)); - } + ArgumentNullException.ThrowIfNull(writer); + ArgumentNullException.ThrowIfNull(claim); WriteWithDefault(writer, claim.Type, claim.Subject?.NameClaimType ?? ClaimsIdentity.DefaultNameClaimType); writer.Write(claim.Value); diff --git a/src/OpenIddict.Server.DataProtection/OpenIddictServerDataProtectionHandlerFilters.cs b/src/OpenIddict.Server.DataProtection/OpenIddictServerDataProtectionHandlerFilters.cs index 12db3e80..10ce604d 100644 --- a/src/OpenIddict.Server.DataProtection/OpenIddictServerDataProtectionHandlerFilters.cs +++ b/src/OpenIddict.Server.DataProtection/OpenIddictServerDataProtectionHandlerFilters.cs @@ -20,10 +20,7 @@ public static class OpenIddictServerDataProtectionHandlerFilters /// public ValueTask IsActiveAsync(GenerateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.TokenFormat is TokenFormats.Private.DataProtection); } diff --git a/src/OpenIddict.Server.DataProtection/OpenIddictServerDataProtectionHandlers.Protection.cs b/src/OpenIddict.Server.DataProtection/OpenIddictServerDataProtectionHandlers.Protection.cs index f347854f..2d78e6a7 100644 --- a/src/OpenIddict.Server.DataProtection/OpenIddictServerDataProtectionHandlers.Protection.cs +++ b/src/OpenIddict.Server.DataProtection/OpenIddictServerDataProtectionHandlers.Protection.cs @@ -271,10 +271,7 @@ public static partial class OpenIddictServerDataProtectionHandlers /// public ValueTask HandleAsync(GenerateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // ASP.NET Core Data Protection can be used to format certain types of tokens in lieu // of the default token format (typically, JSON Web Token). By default, Data Protection @@ -333,10 +330,7 @@ public static partial class OpenIddictServerDataProtectionHandlers /// public ValueTask HandleAsync(GenerateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If an access token was already attached by another handler, don't overwrite it. if (!string.IsNullOrEmpty(context.Token)) diff --git a/src/OpenIddict.Server.Owin/OpenIddictServerOwinBuilder.cs b/src/OpenIddict.Server.Owin/OpenIddictServerOwinBuilder.cs index 76d41a91..cc83a7ec 100644 --- a/src/OpenIddict.Server.Owin/OpenIddictServerOwinBuilder.cs +++ b/src/OpenIddict.Server.Owin/OpenIddictServerOwinBuilder.cs @@ -129,10 +129,7 @@ public sealed class OpenIddictServerOwinBuilder /// The instance. public OpenIddictServerOwinBuilder SetRealm(string realm) { - if (string.IsNullOrEmpty(realm)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0107), nameof(realm)); - } + ArgumentException.ThrowIfNullOrEmpty(realm); return Configure(options => options.Realm = realm); } diff --git a/src/OpenIddict.Server/OpenIddictServerBuilder.cs b/src/OpenIddict.Server/OpenIddictServerBuilder.cs index b8882d96..f0ec51fd 100644 --- a/src/OpenIddict.Server/OpenIddictServerBuilder.cs +++ b/src/OpenIddict.Server/OpenIddictServerBuilder.cs @@ -8,7 +8,6 @@ using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Reflection; -using System.Runtime.InteropServices; using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; using Microsoft.Extensions.DependencyInjection.Extensions; @@ -46,10 +45,7 @@ public sealed class OpenIddictServerBuilder Action> configuration) where TContext : BaseContext { - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(configuration); // Note: handlers registered using this API are assumed to be custom handlers by default. var builder = OpenIddictServerHandlerDescriptor.CreateBuilder() @@ -68,10 +64,7 @@ public sealed class OpenIddictServerBuilder [EditorBrowsable(EditorBrowsableState.Advanced)] public OpenIddictServerBuilder AddEventHandler(OpenIddictServerHandlerDescriptor descriptor) { - if (descriptor is null) - { - throw new ArgumentNullException(nameof(descriptor)); - } + ArgumentNullException.ThrowIfNull(descriptor); // Register the handler in the services collection. Services.Add(descriptor.ServiceDescriptor); @@ -87,10 +80,7 @@ public sealed class OpenIddictServerBuilder [EditorBrowsable(EditorBrowsableState.Advanced)] public OpenIddictServerBuilder RemoveEventHandler(OpenIddictServerHandlerDescriptor descriptor) { - if (descriptor is null) - { - throw new ArgumentNullException(nameof(descriptor)); - } + ArgumentNullException.ThrowIfNull(descriptor); Services.RemoveAll(descriptor.ServiceDescriptor.ServiceType); @@ -116,10 +106,7 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder Configure(Action configuration) { - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(configuration); Services.Configure(configuration); @@ -142,10 +129,7 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder AddEncryptionCredentials(EncryptingCredentials credentials) { - if (credentials is null) - { - throw new ArgumentNullException(nameof(credentials)); - } + ArgumentNullException.ThrowIfNull(credentials); return Configure(options => options.EncryptionCredentials.Add(credentials)); } @@ -157,10 +141,7 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder AddEncryptionKey(SecurityKey key) { - if (key is null) - { - throw new ArgumentNullException(nameof(key)); - } + ArgumentNullException.ThrowIfNull(key); // If the encryption key is an asymmetric security key, ensure it has a private key. if (key is AsymmetricSecurityKey asymmetricSecurityKey && @@ -196,10 +177,7 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder AddEncryptionKeys(IEnumerable keys) { - if (keys is null) - { - throw new ArgumentNullException(nameof(keys)); - } + ArgumentNullException.ThrowIfNull(keys); return keys.Aggregate(this, static (builder, key) => builder.AddEncryptionKey(key)); } @@ -218,10 +196,7 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder AddDevelopmentEncryptionCertificate(X500DistinguishedName subject) { - if (subject is null) - { - throw new ArgumentNullException(nameof(subject)); - } + ArgumentNullException.ThrowIfNull(subject); Services.AddOptions().Configure((options, provider) => { @@ -251,7 +226,7 @@ public sealed class OpenIddictServerBuilder // Note: setting the friendly name is not supported on Unix machines (including Linux and macOS). // To ensure an exception is not thrown by the property setter, an OS runtime check is used here. - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + if (OperatingSystem.IsWindows()) { certificate.FriendlyName = "OpenIddict Server Development Encryption Certificate"; } @@ -268,7 +243,7 @@ public sealed class OpenIddictServerBuilder // Note: macOS requires marking the certificate private key as exportable. // If this flag is not set, a CryptographicException is thrown at runtime. - if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + if (OperatingSystem.IsMacOS()) { flags |= X509KeyStorageFlags.Exportable; } @@ -322,10 +297,7 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder AddEphemeralEncryptionKey(string algorithm) { - if (string.IsNullOrEmpty(algorithm)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0057), nameof(algorithm)); - } + ArgumentException.ThrowIfNullOrEmpty(algorithm); return algorithm switch { @@ -351,10 +323,7 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder AddEncryptionCertificate(X509Certificate2 certificate) { - if (certificate is null) - { - throw new ArgumentNullException(nameof(certificate)); - } + ArgumentNullException.ThrowIfNull(certificate); // If the certificate is a X.509v3 certificate that specifies at least one // key usage, ensure that the certificate key can be used for key encryption. @@ -386,7 +355,7 @@ public sealed class OpenIddictServerBuilder public OpenIddictServerBuilder AddEncryptionCertificate(Assembly assembly, string resource, string? password) #if SUPPORTS_EPHEMERAL_KEY_SETS // Note: ephemeral key sets are currently not supported on macOS. - => AddEncryptionCertificate(assembly, resource, password, RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? + => AddEncryptionCertificate(assembly, resource, password, OperatingSystem.IsMacOS() ? X509KeyStorageFlags.MachineKeySet : X509KeyStorageFlags.EphemeralKeySet); #else @@ -405,15 +374,8 @@ public sealed class OpenIddictServerBuilder Assembly assembly, string resource, string? password, X509KeyStorageFlags flags) { - if (assembly is null) - { - throw new ArgumentNullException(nameof(assembly)); - } - - if (string.IsNullOrEmpty(resource)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0062), nameof(resource)); - } + ArgumentNullException.ThrowIfNull(assembly); + ArgumentException.ThrowIfNullOrEmpty(resource); using var stream = assembly.GetManifestResourceStream(resource) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0064)); @@ -430,7 +392,7 @@ public sealed class OpenIddictServerBuilder public OpenIddictServerBuilder AddEncryptionCertificate(Stream stream, string? password) #if SUPPORTS_EPHEMERAL_KEY_SETS // Note: ephemeral key sets are currently not supported on macOS. - => AddEncryptionCertificate(stream, password, RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? + => AddEncryptionCertificate(stream, password, OperatingSystem.IsMacOS() ? X509KeyStorageFlags.MachineKeySet : X509KeyStorageFlags.EphemeralKeySet); #else @@ -446,10 +408,7 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder AddEncryptionCertificate(Stream stream, string? password, X509KeyStorageFlags flags) { - if (stream is null) - { - throw new ArgumentNullException(nameof(stream)); - } + ArgumentNullException.ThrowIfNull(stream); using var buffer = new MemoryStream(); stream.CopyTo(buffer); @@ -474,10 +433,7 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder AddEncryptionCertificate(string thumbprint) { - if (string.IsNullOrEmpty(thumbprint)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0065), nameof(thumbprint)); - } + ArgumentException.ThrowIfNullOrEmpty(thumbprint); return AddEncryptionCertificate( GetCertificate(StoreLocation.CurrentUser, thumbprint) ?? @@ -504,10 +460,7 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder AddEncryptionCertificate(string thumbprint, StoreName name, StoreLocation location) { - if (string.IsNullOrEmpty(thumbprint)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0065), nameof(thumbprint)); - } + ArgumentException.ThrowIfNullOrEmpty(thumbprint); using var store = new X509Store(name, location); store.Open(OpenFlags.ReadOnly); @@ -525,11 +478,8 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder AddEncryptionCertificates(IEnumerable certificates) { - if (certificates is null) - { - throw new ArgumentNullException(nameof(certificates)); - } - + ArgumentNullException.ThrowIfNull(certificates); + return certificates.Aggregate(this, static (builder, certificate) => builder.AddEncryptionCertificate(certificate)); } @@ -540,10 +490,7 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder AddSigningCredentials(SigningCredentials credentials) { - if (credentials is null) - { - throw new ArgumentNullException(nameof(credentials)); - } + ArgumentNullException.ThrowIfNull(credentials); return Configure(options => options.SigningCredentials.Add(credentials)); } @@ -555,10 +502,7 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder AddSigningKey(SecurityKey key) { - if (key is null) - { - throw new ArgumentNullException(nameof(key)); - } + ArgumentNullException.ThrowIfNull(key); // If the signing key is an asymmetric security key, ensure it has a private key. if (key is AsymmetricSecurityKey asymmetricSecurityKey && @@ -612,10 +556,7 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder AddSigningKeys(IEnumerable keys) { - if (keys is null) - { - throw new ArgumentNullException(nameof(keys)); - } + ArgumentNullException.ThrowIfNull(keys); return keys.Aggregate(this, static (builder, key) => builder.AddSigningKey(key)); } @@ -634,10 +575,7 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder AddDevelopmentSigningCertificate(X500DistinguishedName subject) { - if (subject is null) - { - throw new ArgumentNullException(nameof(subject)); - } + ArgumentNullException.ThrowIfNull(subject); Services.AddOptions().Configure((options, provider) => { @@ -668,7 +606,7 @@ public sealed class OpenIddictServerBuilder // Note: setting the friendly name is not supported on Unix machines (including Linux and macOS). // To ensure an exception is not thrown by the property setter, an OS runtime check is used here. - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + if (OperatingSystem.IsWindows()) { certificate.FriendlyName = "OpenIddict Server Development Signing Certificate"; } @@ -685,7 +623,7 @@ public sealed class OpenIddictServerBuilder // Note: macOS requires marking the certificate private key as exportable. // If this flag is not set, a CryptographicException is thrown at runtime. - if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + if (OperatingSystem.IsMacOS()) { flags |= X509KeyStorageFlags.Exportable; } @@ -738,10 +676,7 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder AddEphemeralSigningKey(string algorithm) { - if (string.IsNullOrEmpty(algorithm)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0057), nameof(algorithm)); - } + ArgumentException.ThrowIfNullOrEmpty(algorithm); return algorithm switch { @@ -796,10 +731,7 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder AddSigningCertificate(X509Certificate2 certificate) { - if (certificate is null) - { - throw new ArgumentNullException(nameof(certificate)); - } + ArgumentNullException.ThrowIfNull(certificate); // If the certificate is a X.509v3 certificate that specifies at least // one key usage, ensure that the certificate key can be used for signing. @@ -831,7 +763,7 @@ public sealed class OpenIddictServerBuilder public OpenIddictServerBuilder AddSigningCertificate(Assembly assembly, string resource, string? password) #if SUPPORTS_EPHEMERAL_KEY_SETS // Note: ephemeral key sets are currently not supported on macOS. - => AddSigningCertificate(assembly, resource, password, RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? + => AddSigningCertificate(assembly, resource, password, OperatingSystem.IsMacOS() ? X509KeyStorageFlags.MachineKeySet : X509KeyStorageFlags.EphemeralKeySet); #else @@ -850,15 +782,8 @@ public sealed class OpenIddictServerBuilder Assembly assembly, string resource, string? password, X509KeyStorageFlags flags) { - if (assembly is null) - { - throw new ArgumentNullException(nameof(assembly)); - } - - if (string.IsNullOrEmpty(resource)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0062), nameof(resource)); - } + ArgumentNullException.ThrowIfNull(assembly); + ArgumentException.ThrowIfNullOrEmpty(resource); using var stream = assembly.GetManifestResourceStream(resource) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0064)); @@ -875,7 +800,7 @@ public sealed class OpenIddictServerBuilder public OpenIddictServerBuilder AddSigningCertificate(Stream stream, string? password) #if SUPPORTS_EPHEMERAL_KEY_SETS // Note: ephemeral key sets are currently not supported on macOS. - => AddSigningCertificate(stream, password, RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? + => AddSigningCertificate(stream, password, OperatingSystem.IsMacOS() ? X509KeyStorageFlags.MachineKeySet : X509KeyStorageFlags.EphemeralKeySet); #else @@ -891,10 +816,7 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder AddSigningCertificate(Stream stream, string? password, X509KeyStorageFlags flags) { - if (stream is null) - { - throw new ArgumentNullException(nameof(stream)); - } + ArgumentNullException.ThrowIfNull(stream); using var buffer = new MemoryStream(); stream.CopyTo(buffer); @@ -919,10 +841,7 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder AddSigningCertificate(string thumbprint) { - if (string.IsNullOrEmpty(thumbprint)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0065), nameof(thumbprint)); - } + ArgumentException.ThrowIfNullOrEmpty(thumbprint); return AddSigningCertificate( GetCertificate(StoreLocation.CurrentUser, thumbprint) ?? @@ -949,10 +868,7 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder AddSigningCertificate(string thumbprint, StoreName name, StoreLocation location) { - if (string.IsNullOrEmpty(thumbprint)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0065), nameof(thumbprint)); - } + ArgumentException.ThrowIfNullOrEmpty(thumbprint); using var store = new X509Store(name, location); store.Open(OpenFlags.ReadOnly); @@ -970,11 +886,8 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder AddSigningCertificates(IEnumerable certificates) { - if (certificates is null) - { - throw new ArgumentNullException(nameof(certificates)); - } - + ArgumentNullException.ThrowIfNull(certificates); + return certificates.Aggregate(this, static (builder, certificate) => builder.AddSigningCertificate(certificate)); } @@ -1009,10 +922,7 @@ public sealed class OpenIddictServerBuilder [EditorBrowsable(EditorBrowsableState.Advanced)] public OpenIddictServerBuilder AllowCustomFlow(string type) { - if (string.IsNullOrEmpty(type)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0071), nameof(type)); - } + ArgumentException.ThrowIfNullOrEmpty(type); return Configure(options => options.GrantTypes.Add(type)); } @@ -1107,10 +1017,7 @@ public sealed class OpenIddictServerBuilder public OpenIddictServerBuilder SetAuthorizationEndpointUris( [StringSyntax(StringSyntaxAttribute.Uri)] params string[] uris) { - if (uris is null) - { - throw new ArgumentNullException(nameof(uris)); - } + ArgumentNullException.ThrowIfNull(uris); return SetAuthorizationEndpointUris([.. uris.Select(uri => new Uri(uri, UriKind.RelativeOrAbsolute))]); } @@ -1124,10 +1031,7 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder SetAuthorizationEndpointUris(params Uri[] uris) { - if (uris is null) - { - throw new ArgumentNullException(nameof(uris)); - } + ArgumentNullException.ThrowIfNull(uris); if (Array.Exists(uris, OpenIddictHelpers.IsImplicitFileUri)) { @@ -1156,10 +1060,7 @@ public sealed class OpenIddictServerBuilder public OpenIddictServerBuilder SetConfigurationEndpointUris( [StringSyntax(StringSyntaxAttribute.Uri)] params string[] uris) { - if (uris is null) - { - throw new ArgumentNullException(nameof(uris)); - } + ArgumentNullException.ThrowIfNull(uris); return SetConfigurationEndpointUris([.. uris.Select(uri => new Uri(uri, UriKind.RelativeOrAbsolute))]); } @@ -1173,10 +1074,7 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder SetConfigurationEndpointUris(params Uri[] uris) { - if (uris is null) - { - throw new ArgumentNullException(nameof(uris)); - } + ArgumentNullException.ThrowIfNull(uris); if (Array.Exists(uris, OpenIddictHelpers.IsImplicitFileUri)) { @@ -1205,10 +1103,7 @@ public sealed class OpenIddictServerBuilder public OpenIddictServerBuilder SetDeviceAuthorizationEndpointUris( [StringSyntax(StringSyntaxAttribute.Uri)] params string[] uris) { - if (uris is null) - { - throw new ArgumentNullException(nameof(uris)); - } + ArgumentNullException.ThrowIfNull(uris); return SetDeviceAuthorizationEndpointUris([.. uris.Select(uri => new Uri(uri, UriKind.RelativeOrAbsolute))]); } @@ -1222,10 +1117,7 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder SetDeviceAuthorizationEndpointUris(params Uri[] uris) { - if (uris is null) - { - throw new ArgumentNullException(nameof(uris)); - } + ArgumentNullException.ThrowIfNull(uris); if (Array.Exists(uris, OpenIddictHelpers.IsImplicitFileUri)) { @@ -1254,10 +1146,7 @@ public sealed class OpenIddictServerBuilder public OpenIddictServerBuilder SetEndSessionEndpointUris( [StringSyntax(StringSyntaxAttribute.Uri)] params string[] uris) { - if (uris is null) - { - throw new ArgumentNullException(nameof(uris)); - } + ArgumentNullException.ThrowIfNull(uris); return SetEndSessionEndpointUris([.. uris.Select(uri => new Uri(uri, UriKind.RelativeOrAbsolute))]); } @@ -1271,10 +1160,7 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder SetEndSessionEndpointUris(params Uri[] uris) { - if (uris is null) - { - throw new ArgumentNullException(nameof(uris)); - } + ArgumentNullException.ThrowIfNull(uris); if (Array.Exists(uris, OpenIddictHelpers.IsImplicitFileUri)) { @@ -1303,10 +1189,7 @@ public sealed class OpenIddictServerBuilder public OpenIddictServerBuilder SetIntrospectionEndpointUris( [StringSyntax(StringSyntaxAttribute.Uri)] params string[] uris) { - if (uris is null) - { - throw new ArgumentNullException(nameof(uris)); - } + ArgumentNullException.ThrowIfNull(uris); return SetIntrospectionEndpointUris([.. uris.Select(uri => new Uri(uri, UriKind.RelativeOrAbsolute))]); } @@ -1320,10 +1203,7 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder SetIntrospectionEndpointUris(params Uri[] uris) { - if (uris is null) - { - throw new ArgumentNullException(nameof(uris)); - } + ArgumentNullException.ThrowIfNull(uris); if (Array.Exists(uris, OpenIddictHelpers.IsImplicitFileUri)) { @@ -1352,10 +1232,7 @@ public sealed class OpenIddictServerBuilder public OpenIddictServerBuilder SetJsonWebKeySetEndpointUris( [StringSyntax(StringSyntaxAttribute.Uri)] params string[] uris) { - if (uris is null) - { - throw new ArgumentNullException(nameof(uris)); - } + ArgumentNullException.ThrowIfNull(uris); return SetJsonWebKeySetEndpointUris([.. uris.Select(uri => new Uri(uri, UriKind.RelativeOrAbsolute))]); } @@ -1369,10 +1246,7 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder SetJsonWebKeySetEndpointUris(params Uri[] uris) { - if (uris is null) - { - throw new ArgumentNullException(nameof(uris)); - } + ArgumentNullException.ThrowIfNull(uris); if (Array.Exists(uris, OpenIddictHelpers.IsImplicitFileUri)) { @@ -1401,10 +1275,7 @@ public sealed class OpenIddictServerBuilder public OpenIddictServerBuilder SetPushedAuthorizationEndpointUris( [StringSyntax(StringSyntaxAttribute.Uri)] params string[] uris) { - if (uris is null) - { - throw new ArgumentNullException(nameof(uris)); - } + ArgumentNullException.ThrowIfNull(uris); return SetPushedAuthorizationEndpointUris([.. uris.Select(uri => new Uri(uri, UriKind.RelativeOrAbsolute))]); } @@ -1418,10 +1289,7 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder SetPushedAuthorizationEndpointUris(params Uri[] uris) { - if (uris is null) - { - throw new ArgumentNullException(nameof(uris)); - } + ArgumentNullException.ThrowIfNull(uris); if (Array.Exists(uris, OpenIddictHelpers.IsImplicitFileUri)) { @@ -1450,10 +1318,7 @@ public sealed class OpenIddictServerBuilder public OpenIddictServerBuilder SetRevocationEndpointUris( [StringSyntax(StringSyntaxAttribute.Uri)] params string[] uris) { - if (uris is null) - { - throw new ArgumentNullException(nameof(uris)); - } + ArgumentNullException.ThrowIfNull(uris); return SetRevocationEndpointUris([.. uris.Select(uri => new Uri(uri, UriKind.RelativeOrAbsolute))]); } @@ -1467,10 +1332,7 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder SetRevocationEndpointUris(params Uri[] uris) { - if (uris is null) - { - throw new ArgumentNullException(nameof(uris)); - } + ArgumentNullException.ThrowIfNull(uris); if (Array.Exists(uris, OpenIddictHelpers.IsImplicitFileUri)) { @@ -1499,10 +1361,7 @@ public sealed class OpenIddictServerBuilder public OpenIddictServerBuilder SetTokenEndpointUris( [StringSyntax(StringSyntaxAttribute.Uri)] params string[] uris) { - if (uris is null) - { - throw new ArgumentNullException(nameof(uris)); - } + ArgumentNullException.ThrowIfNull(uris); return SetTokenEndpointUris([.. uris.Select(uri => new Uri(uri, UriKind.RelativeOrAbsolute))]); } @@ -1516,10 +1375,7 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder SetTokenEndpointUris(params Uri[] uris) { - if (uris is null) - { - throw new ArgumentNullException(nameof(uris)); - } + ArgumentNullException.ThrowIfNull(uris); if (Array.Exists(uris, OpenIddictHelpers.IsImplicitFileUri)) { @@ -1548,10 +1404,7 @@ public sealed class OpenIddictServerBuilder public OpenIddictServerBuilder SetUserInfoEndpointUris( [StringSyntax(StringSyntaxAttribute.Uri)] params string[] uris) { - if (uris is null) - { - throw new ArgumentNullException(nameof(uris)); - } + ArgumentNullException.ThrowIfNull(uris); return SetUserInfoEndpointUris([.. uris.Select(uri => new Uri(uri, UriKind.RelativeOrAbsolute))]); } @@ -1565,10 +1418,7 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder SetUserInfoEndpointUris(params Uri[] uris) { - if (uris is null) - { - throw new ArgumentNullException(nameof(uris)); - } + ArgumentNullException.ThrowIfNull(uris); if (Array.Exists(uris, OpenIddictHelpers.IsImplicitFileUri)) { @@ -1597,10 +1447,7 @@ public sealed class OpenIddictServerBuilder public OpenIddictServerBuilder SetEndUserVerificationEndpointUris( [StringSyntax(StringSyntaxAttribute.Uri)] params string[] uris) { - if (uris is null) - { - throw new ArgumentNullException(nameof(uris)); - } + ArgumentNullException.ThrowIfNull(uris); return SetEndUserVerificationEndpointUris([.. uris.Select(uri => new Uri(uri, UriKind.RelativeOrAbsolute))]); } @@ -1614,10 +1461,7 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder SetEndUserVerificationEndpointUris(params Uri[] uris) { - if (uris is null) - { - throw new ArgumentNullException(nameof(uris)); - } + ArgumentNullException.ThrowIfNull(uris); if (Array.Exists(uris, OpenIddictHelpers.IsImplicitFileUri)) { @@ -1772,10 +1616,7 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder RegisterAudiences(params string[] audiences) { - if (audiences is null) - { - throw new ArgumentNullException(nameof(audiences)); - } + ArgumentNullException.ThrowIfNull(audiences); if (Array.Exists(audiences, string.IsNullOrEmpty)) { @@ -1793,10 +1634,7 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder RegisterClaims(params string[] claims) { - if (claims is null) - { - throw new ArgumentNullException(nameof(claims)); - } + ArgumentNullException.ThrowIfNull(claims); if (Array.Exists(claims, string.IsNullOrEmpty)) { @@ -1814,10 +1652,7 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder RegisterPromptValues(params string[] values) { - if (values is null) - { - throw new ArgumentNullException(nameof(values)); - } + ArgumentNullException.ThrowIfNull(values); if (Array.Exists(values, string.IsNullOrEmpty)) { @@ -1836,10 +1671,7 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder RegisterResources(params string[] resources) { - if (resources is null) - { - throw new ArgumentNullException(nameof(resources)); - } + ArgumentNullException.ThrowIfNull(resources); return RegisterResources([.. resources.Select(resource => new Uri(resource, UriKind.Absolute))]); } @@ -1853,10 +1685,7 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder RegisterResources(params Uri[] resources) { - if (resources is null) - { - throw new ArgumentNullException(nameof(resources)); - } + ArgumentNullException.ThrowIfNull(resources); if (Array.Exists(resources, static resource => OpenIddictHelpers.IsImplicitFileUri(resource) || !string.IsNullOrEmpty(resource.Fragment))) @@ -1875,10 +1704,7 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder RegisterScopes(params string[] scopes) { - if (scopes is null) - { - throw new ArgumentNullException(nameof(scopes)); - } + ArgumentNullException.ThrowIfNull(scopes); if (Array.Exists(scopes, string.IsNullOrEmpty)) { @@ -1993,15 +1819,8 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder SetUserCodeCharset(params string[] charset) { - if (charset is null) - { - throw new ArgumentNullException(nameof(charset)); - } - - if (charset.Length is < 9) - { - throw new ArgumentOutOfRangeException(nameof(charset), SR.FormatID0440(9)); - } + ArgumentNullException.ThrowIfNull(charset); + ArgumentOutOfRangeException.ThrowIfLessThan(charset.Length, 9, nameof(charset)); if (charset.Length != charset.Distinct(StringComparer.Ordinal).Count()) { @@ -2055,10 +1874,7 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder SetUserCodeLength(int length) { - if (length is < 6) - { - throw new ArgumentOutOfRangeException(nameof(length), SR.FormatID0439(6)); - } + ArgumentOutOfRangeException.ThrowIfLessThan(length, 6); return Configure(options => options.UserCodeLength = length); } @@ -2081,10 +1897,7 @@ public sealed class OpenIddictServerBuilder /// The instance. public OpenIddictServerBuilder SetIssuer(Uri uri) { - if (uri is null) - { - throw new ArgumentNullException(nameof(uri)); - } + ArgumentNullException.ThrowIfNull(uri); return Configure(options => options.Issuer = uri); } @@ -2098,10 +1911,7 @@ public sealed class OpenIddictServerBuilder public OpenIddictServerBuilder SetIssuer( [StringSyntax(StringSyntaxAttribute.Uri, UriKind.Absolute)] string uri) { - if (string.IsNullOrEmpty(uri)) - { - throw new ArgumentException(SR.FormatID0366(nameof(uri)), nameof(uri)); - } + ArgumentException.ThrowIfNullOrEmpty(uri); if (!Uri.TryCreate(uri, UriKind.Absolute, out Uri? value) || OpenIddictHelpers.IsImplicitFileUri(value)) { diff --git a/src/OpenIddict.Server/OpenIddictServerConfiguration.cs b/src/OpenIddict.Server/OpenIddictServerConfiguration.cs index af8e7fbe..1bcc9e4e 100644 --- a/src/OpenIddict.Server/OpenIddictServerConfiguration.cs +++ b/src/OpenIddict.Server/OpenIddictServerConfiguration.cs @@ -32,10 +32,7 @@ public sealed class OpenIddictServerConfiguration : IPostConfigureOptions public void PostConfigure(string? name, OpenIddictServerOptions options) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); options.TimeProvider ??= _provider.GetService() ?? TimeProvider.System; diff --git a/src/OpenIddict.Server/OpenIddictServerDispatcher.cs b/src/OpenIddict.Server/OpenIddictServerDispatcher.cs index f44f4dab..c68f3b19 100644 --- a/src/OpenIddict.Server/OpenIddictServerDispatcher.cs +++ b/src/OpenIddict.Server/OpenIddictServerDispatcher.cs @@ -36,10 +36,7 @@ public sealed class OpenIddictServerDispatcher : IOpenIddictServerDispatcher /// public async ValueTask DispatchAsync(TContext context) where TContext : BaseContext { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); await foreach (var handler in GetHandlersAsync()) { diff --git a/src/OpenIddict.Server/OpenIddictServerEvents.Authentication.cs b/src/OpenIddict.Server/OpenIddictServerEvents.Authentication.cs index 62cc115a..0b466318 100644 --- a/src/OpenIddict.Server/OpenIddictServerEvents.Authentication.cs +++ b/src/OpenIddict.Server/OpenIddictServerEvents.Authentication.cs @@ -89,10 +89,7 @@ public static partial class OpenIddictServerEvents /// The redirect_uri to use when redirecting the user agent. public void SetRedirectUri([StringSyntax(StringSyntaxAttribute.Uri)] string uri) { - if (string.IsNullOrEmpty(uri)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0100), nameof(uri)); - } + ArgumentException.ThrowIfNullOrEmpty(uri); // Don't allow validation to alter the redirect_uri parameter extracted // from the request if the URI was explicitly provided by the client. @@ -299,10 +296,7 @@ public static partial class OpenIddictServerEvents /// The redirect_uri to use when redirecting the user agent. public void SetRedirectUri([StringSyntax(StringSyntaxAttribute.Uri)] string uri) { - if (string.IsNullOrEmpty(uri)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0100), nameof(uri)); - } + ArgumentException.ThrowIfNullOrEmpty(uri); // Don't allow validation to alter the redirect_uri parameter extracted // from the request if the URI was explicitly provided by the client. diff --git a/src/OpenIddict.Server/OpenIddictServerEvents.Session.cs b/src/OpenIddict.Server/OpenIddictServerEvents.Session.cs index 83ba0512..3b91d8bf 100644 --- a/src/OpenIddict.Server/OpenIddictServerEvents.Session.cs +++ b/src/OpenIddict.Server/OpenIddictServerEvents.Session.cs @@ -87,10 +87,7 @@ public static partial class OpenIddictServerEvents /// The post_logout_redirect_uri to use when redirecting the user agent. public void SetPostLogoutRedirectUri([StringSyntax(StringSyntaxAttribute.Uri)] string uri) { - if (string.IsNullOrEmpty(uri)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0102), nameof(uri)); - } + ArgumentException.ThrowIfNullOrEmpty(uri); // Don't allow validation to alter the post_logout_redirect_uri parameter extracted // from the request if the URI was explicitly provided by the client application. diff --git a/src/OpenIddict.Server/OpenIddictServerExtensions.cs b/src/OpenIddict.Server/OpenIddictServerExtensions.cs index 0a97e691..55449f73 100644 --- a/src/OpenIddict.Server/OpenIddictServerExtensions.cs +++ b/src/OpenIddict.Server/OpenIddictServerExtensions.cs @@ -24,10 +24,7 @@ public static class OpenIddictServerExtensions /// The instance. public static OpenIddictServerBuilder AddServer(this OpenIddictBuilder builder) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } + ArgumentNullException.ThrowIfNull(builder); builder.Services.AddLogging(); builder.Services.AddOptions(); @@ -115,15 +112,8 @@ public static class OpenIddictServerExtensions /// The instance. public static OpenIddictBuilder AddServer(this OpenIddictBuilder builder, Action configuration) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } - - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(builder); + ArgumentNullException.ThrowIfNull(configuration); configuration(builder.AddServer()); diff --git a/src/OpenIddict.Server/OpenIddictServerHandlerDescriptor.cs b/src/OpenIddict.Server/OpenIddictServerHandlerDescriptor.cs index c031a17d..8a8ad74b 100644 --- a/src/OpenIddict.Server/OpenIddictServerHandlerDescriptor.cs +++ b/src/OpenIddict.Server/OpenIddictServerHandlerDescriptor.cs @@ -88,10 +88,7 @@ public sealed class OpenIddictServerHandlerDescriptor /// The builder instance, so that calls can be easily chained. public Builder Import(OpenIddictServerHandlerDescriptor descriptor) { - if (descriptor is null) - { - throw new ArgumentNullException(nameof(descriptor)); - } + ArgumentNullException.ThrowIfNull(descriptor); if (descriptor.ContextType != typeof(TContext)) { @@ -114,10 +111,7 @@ public sealed class OpenIddictServerHandlerDescriptor /// The builder instance, so that calls can be easily chained. public Builder SetServiceDescriptor(ServiceDescriptor descriptor) { - if (descriptor is null) - { - throw new ArgumentNullException(nameof(descriptor)); - } + ArgumentNullException.ThrowIfNull(descriptor); if (!typeof(IOpenIddictServerHandler).IsAssignableFrom(descriptor.ServiceType)) { diff --git a/src/OpenIddict.Server/OpenIddictServerHandlerFilters.cs b/src/OpenIddict.Server/OpenIddictServerHandlerFilters.cs index 22aa607f..d2f091f9 100644 --- a/src/OpenIddict.Server/OpenIddictServerHandlerFilters.cs +++ b/src/OpenIddict.Server/OpenIddictServerHandlerFilters.cs @@ -19,10 +19,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.GenerateAccessToken); } @@ -36,10 +33,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.ValidateAccessToken); } @@ -53,10 +47,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.ValidateActorToken); } @@ -70,10 +61,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!context.Options.IgnoreAudiencePermissions); } @@ -87,10 +75,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!context.Options.DisableAudienceValidation); } @@ -104,10 +89,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.GenerateAuthorizationCode); } @@ -121,10 +103,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.ValidateAuthorizationCode); } @@ -138,10 +117,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!string.IsNullOrEmpty(context.AuthorizationId)); } @@ -155,10 +131,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.EndpointType is OpenIddictServerEndpointType.Authorization); } @@ -172,10 +145,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!context.Options.DisableAuthorizationStorage); } @@ -189,10 +159,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.ClientAssertionPrincipal is not null); } @@ -206,10 +173,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.ValidateClientAssertion); } @@ -223,10 +187,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!string.IsNullOrEmpty(context.Transaction.Request?.ClientId)); } @@ -240,10 +201,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!string.IsNullOrEmpty(context.Transaction.Request?.ClientSecret)); } @@ -257,10 +215,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.EndpointType is OpenIddictServerEndpointType.Configuration); } @@ -274,10 +229,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!context.Options.EnableDegradedMode); } @@ -291,10 +243,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.EndpointType is OpenIddictServerEndpointType.DeviceAuthorization); } @@ -308,10 +257,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.GenerateDeviceCode); } @@ -325,10 +271,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.ValidateDeviceCode); } @@ -342,10 +285,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!context.Options.IgnoreEndpointPermissions); } @@ -359,10 +299,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.EndpointType is OpenIddictServerEndpointType.EndSession); } @@ -376,10 +313,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.EndpointType is OpenIddictServerEndpointType.EndUserVerification); } @@ -393,10 +327,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.ValidateGenericToken); } @@ -410,10 +341,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!context.Options.IgnoreGrantTypePermissions); } @@ -427,10 +355,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.GenerateIdentityToken); } @@ -444,10 +369,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.ValidateIdentityToken); } @@ -461,10 +383,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.EndpointType is OpenIddictServerEndpointType.Introspection); } @@ -478,10 +397,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.GenerateIssuedToken); } @@ -495,10 +411,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.EndpointType is OpenIddictServerEndpointType.JsonWebKeySet); } @@ -512,10 +425,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(GenerateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.TokenFormat is TokenFormats.Private.JsonWebToken); } @@ -529,10 +439,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!string.IsNullOrEmpty(context.Transaction.Request?.PostLogoutRedirectUri)); } @@ -546,10 +453,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.EndpointType is OpenIddictServerEndpointType.PushedAuthorization); } @@ -563,10 +467,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.Options.UseReferenceAccessTokens); } @@ -580,10 +481,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.Options.UseReferenceRefreshTokens); } @@ -597,10 +495,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.GenerateRequestToken); } @@ -614,10 +509,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.RequestTokenPrincipal is not null); } @@ -631,10 +523,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.ValidateRequestToken); } @@ -648,10 +537,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.GenerateRefreshToken); } @@ -665,10 +551,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.ValidateRefreshToken); } @@ -682,10 +565,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!context.Options.IgnoreResourcePermissions); } @@ -699,10 +579,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!context.Options.DisableResourceValidation); } @@ -716,10 +593,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!context.Options.IgnoreResponseTypePermissions); } @@ -733,10 +607,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.EndpointType is OpenIddictServerEndpointType.Revocation); } @@ -750,10 +621,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!context.Options.IgnoreScopePermissions); } @@ -767,10 +635,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!context.Options.DisableScopeValidation); } @@ -784,10 +649,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!context.Options.DisableSlidingRefreshTokenExpiration); } @@ -801,10 +663,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.ValidateSubjectToken); } @@ -818,10 +677,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!context.DisableAudienceValidation); } @@ -835,10 +691,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!string.IsNullOrEmpty(context.TokenId)); } @@ -852,10 +705,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(GenerateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.CreateTokenEntry); } @@ -869,10 +719,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!context.DisableLifetimeValidation); } @@ -886,10 +733,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(GenerateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.PersistTokenPayload); } @@ -903,10 +747,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!context.DisablePresenterValidation); } @@ -920,10 +761,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.EndpointType is OpenIddictServerEndpointType.Token); } @@ -937,10 +775,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!context.Options.DisableTokenStorage); } @@ -954,10 +789,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.GenerateUserCode); } @@ -971,10 +803,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.ValidateUserCode); } @@ -988,10 +817,7 @@ public static class OpenIddictServerHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.EndpointType is OpenIddictServerEndpointType.UserInfo); } diff --git a/src/OpenIddict.Server/OpenIddictServerHandlers.Authentication.cs b/src/OpenIddict.Server/OpenIddictServerHandlers.Authentication.cs index c06a7a73..7ca85859 100644 --- a/src/OpenIddict.Server/OpenIddictServerHandlers.Authentication.cs +++ b/src/OpenIddict.Server/OpenIddictServerHandlers.Authentication.cs @@ -143,10 +143,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ExtractAuthorizationRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -205,10 +202,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ValidateAuthorizationRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -271,10 +265,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new HandleAuthorizationRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -409,10 +400,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ApplyAuthorizationResponseContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -451,10 +439,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Reject requests using the unsupported request parameter. if (!string.IsNullOrEmpty(context.Request.Request)) @@ -491,10 +476,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (string.IsNullOrEmpty(context.Request.RequestUri)) { @@ -565,10 +547,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // client_id is a required parameter and MUST cause an error when missing. // See http://openid.net/specs/openid-connect-core-1_0.html#AuthRequest. @@ -611,10 +590,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidateAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ProcessAuthenticationContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -668,10 +644,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var value = context.RequestTokenPrincipal?.GetClaim(Claims.Private.RequestParameters); if (string.IsNullOrEmpty(value)) @@ -738,10 +711,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // While redirect_uri was not mandatory in OAuth 2.0, this parameter // is now declared as REQUIRED and MUST cause an error when missing. @@ -838,10 +808,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Reject requests missing the mandatory response_type parameter. if (string.IsNullOrEmpty(context.Request.ResponseType)) @@ -947,10 +914,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // response_mode=query (explicit or not) and a response_type containing id_token // or token are not considered as a safe combination and MUST be rejected. @@ -1029,10 +993,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Reject authorization requests containing the id_token response_type if no openid scope has been received. if (context.Request.HasResponseType(ResponseTypes.IdToken) && !context.Request.HasScope(Scopes.OpenId)) @@ -1080,10 +1041,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Prevent audiences parameters from being attached to authorization requests, as the // standard "audience" parameter can only be used in OAuth 2.0 Token Exchange requests. @@ -1119,10 +1077,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); foreach (var resource in context.Request.GetResources()) { @@ -1177,10 +1132,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Reject OpenID Connect implicit/hybrid requests missing the mandatory nonce parameter. // See http://openid.net/specs/openid-connect-core-1_0.html#AuthRequest, @@ -1226,10 +1178,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (string.IsNullOrEmpty(context.Request.Prompt)) { @@ -1291,10 +1240,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If OpenIddict was configured to require PKCE, reject the request if the code challenge // is missing and if an authorization code was requested by the client application. @@ -1412,10 +1358,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidateAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: this handler is responsible for enforcing additional response_type requirements when // response type permissions are not used (and thus cannot be finely controlled per client). @@ -1502,10 +1445,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidateAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); @@ -1584,10 +1524,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidateAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If all the specified scopes are registered in the options, avoid making a database lookup. var scopes = context.Request.GetScopes().ToHashSet(StringComparer.Ordinal); @@ -1647,10 +1584,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If at least one resource was not recognized, return an error. var resources = context.Request.GetResources().ToHashSet(StringComparer.Ordinal); @@ -1700,10 +1634,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidateAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); @@ -1753,10 +1684,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidateAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); @@ -1851,10 +1779,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidateAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); @@ -1929,10 +1854,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidateAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); @@ -1993,10 +1915,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidateAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); @@ -2049,10 +1968,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidateAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); @@ -2119,10 +2035,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidateAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); @@ -2169,10 +2082,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.IdentityTokenHintPrincipal is null) { @@ -2219,10 +2129,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(HandleAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = context.Transaction.GetProperty( typeof(ValidateAuthorizationRequestContext).FullName!) ?? @@ -2253,10 +2160,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ApplyAuthorizationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If the authorization response contains a request token, do not use the // redirect_uri, as the user agent will be redirected to the same page. @@ -2298,10 +2202,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ApplyAuthorizationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.Request is null) { @@ -2340,10 +2241,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ApplyAuthorizationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If the user agent is expected to be redirected to the client application, attach the request // state to the authorization response to help the client mitigate CSRF/session fixation attacks. @@ -2377,10 +2275,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ApplyAuthorizationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If the user agent is expected to be redirected to the client application, attach the // issuer URI to the authorization response to help the client detect mix-up attacks. @@ -2429,10 +2324,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ExtractPushedAuthorizationRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -2492,10 +2384,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ValidatePushedAuthorizationRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -2559,10 +2448,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new HandlePushedAuthorizationRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -2661,10 +2547,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ApplyPushedAuthorizationResponseContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -2704,10 +2587,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidatePushedAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Reject requests using the unsupported request parameter. if (!string.IsNullOrEmpty(context.Request.Request)) @@ -2745,10 +2625,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidatePushedAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Reject requests using the request_uri parameter, as this parameter is explicitly forbidden // by the OAuth 2.0 Pushed Authorization Requests specification when used in PAR requests. @@ -2787,10 +2664,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidatePushedAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // client_id is a required parameter and MUST cause an error when missing. // See http://openid.net/specs/openid-connect-core-1_0.html#AuthRequest. @@ -2829,10 +2703,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidatePushedAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // While redirect_uri was not mandatory in OAuth 2.0, this parameter // is now declared as REQUIRED and MUST cause an error when missing. @@ -2930,10 +2801,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidatePushedAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Reject requests missing the mandatory response_type parameter. if (string.IsNullOrEmpty(context.Request.ResponseType)) @@ -3040,10 +2908,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidatePushedAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // response_mode=query (explicit or not) and a response_type containing id_token // or token are not considered as a safe combination and MUST be rejected. @@ -3123,10 +2988,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidatePushedAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Reject pushed authorization requests containing the id_token response_type if no openid scope has been received. if (context.Request.HasResponseType(ResponseTypes.IdToken) && !context.Request.HasScope(Scopes.OpenId)) @@ -3174,10 +3036,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidatePushedAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Prevent audiences parameters from being attached to pushed authorization requests, as the // standard "audience" parameter can only be used in OAuth 2.0 Token Exchange requests. @@ -3213,10 +3072,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidatePushedAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); foreach (var resource in context.Request.GetResources()) { @@ -3271,10 +3127,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidatePushedAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Reject OpenID Connect implicit/hybrid requests missing the mandatory nonce parameter. // See http://openid.net/specs/openid-connect-core-1_0.html#AuthRequest, @@ -3320,10 +3173,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidatePushedAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (string.IsNullOrEmpty(context.Request.Prompt)) { @@ -3385,10 +3235,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidatePushedAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If OpenIddict was configured to require PKCE, reject the request if the code challenge // is missing and if an authorization code was requested by the client application. @@ -3496,10 +3343,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidatePushedAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ProcessAuthenticationContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -3567,10 +3411,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidatePushedAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: this handler is responsible for enforcing additional response_type requirements when // response type permissions are not used (and thus cannot be finely controlled per client). @@ -3657,10 +3498,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidatePushedAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); @@ -3739,10 +3577,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidatePushedAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If all the specified scopes are registered in the options, avoid making a database lookup. var scopes = context.Request.GetScopes().ToHashSet(StringComparer.Ordinal); @@ -3802,10 +3637,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidatePushedAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If at least one resource was not recognized, return an error. var resources = context.Request.GetResources().ToHashSet(StringComparer.Ordinal); @@ -3855,10 +3687,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidatePushedAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); @@ -3908,10 +3737,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidatePushedAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); @@ -4006,10 +3832,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidatePushedAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); @@ -4084,10 +3907,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidatePushedAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); @@ -4148,10 +3968,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidatePushedAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); @@ -4204,10 +4021,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidatePushedAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); @@ -4254,10 +4068,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidatePushedAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.IdentityTokenHintPrincipal is null) { @@ -4304,10 +4115,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(HandlePushedAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = context.Transaction.GetProperty( typeof(ValidatePushedAuthorizationRequestContext).FullName!) ?? diff --git a/src/OpenIddict.Server/OpenIddictServerHandlers.Device.cs b/src/OpenIddict.Server/OpenIddictServerHandlers.Device.cs index 1149ec47..2a5da926 100644 --- a/src/OpenIddict.Server/OpenIddictServerHandlers.Device.cs +++ b/src/OpenIddict.Server/OpenIddictServerHandlers.Device.cs @@ -87,10 +87,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ExtractDeviceAuthorizationRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -149,10 +146,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ValidateDeviceAuthorizationRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -206,10 +200,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new HandleDeviceAuthorizationRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -307,10 +298,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ApplyDeviceAuthorizationResponseContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -349,10 +337,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateDeviceAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Reject device requests that specify scope=offline_access if the refresh token flow is not enabled. if (context.Request.HasScope(Scopes.OfflineAccess) && !context.Options.GrantTypes.Contains(GrantTypes.RefreshToken)) @@ -387,10 +372,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateDeviceAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Ensure a client_assertion_type is specified when a client_assertion was attached. if (!string.IsNullOrEmpty(context.Request.ClientAssertion) && @@ -483,10 +465,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidateDeviceAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If all the specified scopes are registered in the options, avoid making a database lookup. var scopes = context.Request.GetScopes().ToHashSet(StringComparer.Ordinal); @@ -550,10 +529,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidateDeviceAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ProcessAuthenticationContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -615,10 +591,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidateDeviceAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); @@ -671,10 +644,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidateDeviceAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); @@ -741,10 +711,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidateDeviceAuthorizationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); @@ -800,10 +767,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ExtractEndUserVerificationRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -862,10 +826,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ValidateEndUserVerificationRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -923,10 +884,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new HandleEndUserVerificationRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -1020,10 +978,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ApplyEndUserVerificationResponseContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -1067,10 +1022,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidateEndUserVerificationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ProcessAuthenticationContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -1123,10 +1075,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(HandleEndUserVerificationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = context.Transaction.GetProperty( typeof(ValidateEndUserVerificationRequestContext).FullName!) ?? diff --git a/src/OpenIddict.Server/OpenIddictServerHandlers.Discovery.cs b/src/OpenIddict.Server/OpenIddictServerHandlers.Discovery.cs index 3cd01e1b..2b572c28 100644 --- a/src/OpenIddict.Server/OpenIddictServerHandlers.Discovery.cs +++ b/src/OpenIddict.Server/OpenIddictServerHandlers.Discovery.cs @@ -85,10 +85,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ExtractConfigurationRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -147,10 +144,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ValidateConfigurationRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -204,10 +198,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new HandleConfigurationRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -295,10 +286,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ApplyConfigurationResponseContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -337,10 +325,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(HandleConfigurationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.Issuer = (context.Options.Issuer ?? context.BaseUri) switch { @@ -372,10 +357,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(HandleConfigurationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: while OpenIddict allows specifying multiple endpoint URIs, the OAuth 2.0 // and OpenID Connect discovery specifications only allow a single URI per endpoint. @@ -429,10 +411,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(HandleConfigurationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.GrantTypes.UnionWith(context.Options.GrantTypes); @@ -458,10 +437,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(HandleConfigurationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.ResponseTypes.UnionWith(context.Options.ResponseTypes); @@ -487,10 +463,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(HandleConfigurationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Only include the response modes if at least one response type is returned. if (context.ResponseTypes.Count is 0) @@ -536,10 +509,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(HandleConfigurationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // 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. @@ -593,10 +563,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(HandleConfigurationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Only include the code challenge methods if the authorization code grant type is enabled. if (context.GrantTypes.Contains(GrantTypes.AuthorizationCode)) @@ -626,10 +593,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(HandleConfigurationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.Scopes.UnionWith(context.Options.Scopes); @@ -655,10 +619,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(HandleConfigurationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.Claims.UnionWith(context.Options.Claims); @@ -684,10 +645,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(HandleConfigurationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.SubjectTypes.UnionWith(context.Options.SubjectTypes); @@ -713,10 +671,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(HandleConfigurationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.PromptValues.UnionWith(context.Options.PromptValues); @@ -742,10 +697,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(HandleConfigurationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); foreach (var credentials in context.Options.SigningCredentials) { @@ -808,10 +760,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(HandleConfigurationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.RequirePushedAuthorizationRequests = context.Options.RequirePushedAuthorizationRequests; @@ -837,10 +786,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(HandleConfigurationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: these optional features are not yet supported by OpenIddict, // so "false" is returned to encourage clients not to use them. @@ -882,10 +828,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ExtractJsonWebKeySetRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -944,10 +887,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ValidateJsonWebKeySetRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -1001,10 +941,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new HandleJsonWebKeySetRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -1129,10 +1066,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ApplyJsonWebKeySetResponseContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -1171,10 +1105,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(HandleJsonWebKeySetRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); foreach (var credentials in context.Options.SigningCredentials) { diff --git a/src/OpenIddict.Server/OpenIddictServerHandlers.Exchange.cs b/src/OpenIddict.Server/OpenIddictServerHandlers.Exchange.cs index 098a3a13..70a94601 100644 --- a/src/OpenIddict.Server/OpenIddictServerHandlers.Exchange.cs +++ b/src/OpenIddict.Server/OpenIddictServerHandlers.Exchange.cs @@ -98,10 +98,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ExtractTokenRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -160,10 +157,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ValidateTokenRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -221,10 +215,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new HandleTokenRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -318,10 +309,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ApplyTokenResponseContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -360,10 +348,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateTokenRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Reject token requests missing the mandatory grant_type parameter. if (string.IsNullOrEmpty(context.Request.GrantType)) @@ -425,10 +410,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateTokenRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Reject grant_type=authorization_code requests that don't specify a client_id or a client_assertion, // as the client identifier MUST be sent by the client application in the request body if it cannot @@ -489,10 +471,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateTokenRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Reject grant_type=authorization_code requests missing the authorization code. // See https://tools.ietf.org/html/rfc6749#section-4.1.3 for more information. @@ -530,10 +509,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateTokenRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Ensure a client_assertion_type is specified when a client_assertion was attached. if (!string.IsNullOrEmpty(context.Request.ClientAssertion) && @@ -625,10 +601,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateTokenRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Reject grant_type=urn:ietf:params:oauth:grant-type:device_code requests missing the device code. // See https://tools.ietf.org/html/rfc8628#section-3.4 for more information. @@ -665,10 +638,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateTokenRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Reject grant_type=refresh_token requests missing the refresh token. // See https://tools.ietf.org/html/rfc6749#section-6 for more information. @@ -707,10 +677,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateTokenRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (!context.Request.IsTokenExchangeGrantType()) { @@ -831,10 +798,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateTokenRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Reject grant_type=password requests missing username or password. // See https://tools.ietf.org/html/rfc6749#section-4.3.2 for more information. @@ -873,10 +837,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateTokenRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (!context.Request.IsAuthorizationCodeGrantType()) { @@ -921,10 +882,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateTokenRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Reject authorization code and device authorization code requests that contain a "scope" parameter. // @@ -968,10 +926,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateTokenRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.Request.Audiences is null or []) { @@ -1014,10 +969,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateTokenRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); foreach (var resource in context.Request.GetResources()) { @@ -1089,10 +1041,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidateTokenRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If all the specified scopes are registered in the options, avoid making a database lookup. var scopes = context.Request.GetScopes().ToHashSet(StringComparer.Ordinal); @@ -1152,10 +1101,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateTokenRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If at least one audience was not recognized, return an error. var audiences = context.Request.GetAudiences().ToHashSet(StringComparer.Ordinal); @@ -1196,10 +1142,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateTokenRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If at least one resource was not recognized, return an error. var resources = context.Request.GetResources().ToHashSet(StringComparer.Ordinal); @@ -1244,10 +1187,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidateTokenRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ProcessAuthenticationContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -1316,10 +1256,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidateTokenRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); @@ -1371,10 +1308,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidateTokenRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); @@ -1441,10 +1375,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidateTokenRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); @@ -1506,10 +1437,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidateTokenRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); @@ -1564,10 +1492,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidateTokenRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); @@ -1621,10 +1546,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidateTokenRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (!context.Request.IsAuthorizationCodeGrantType()) { @@ -1676,10 +1598,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateTokenRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.Request.IsAuthorizationCodeGrantType() || context.Request.IsDeviceCodeGrantType() || @@ -1962,10 +1881,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateTokenRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (!context.Request.IsAuthorizationCodeGrantType()) { @@ -2033,10 +1949,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateTokenRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (!context.Request.IsAuthorizationCodeGrantType()) { @@ -2137,10 +2050,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateTokenRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (string.IsNullOrEmpty(context.Request.Scope) || !context.Request.IsRefreshTokenGrantType()) { @@ -2204,10 +2114,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(HandleTokenRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (!context.Request.IsAuthorizationCodeGrantType() && !context.Request.IsDeviceCodeGrantType() && @@ -2268,10 +2175,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ApplyTokenResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (string.IsNullOrEmpty(context.Error)) { diff --git a/src/OpenIddict.Server/OpenIddictServerHandlers.Introspection.cs b/src/OpenIddict.Server/OpenIddictServerHandlers.Introspection.cs index bce7bc56..361d4903 100644 --- a/src/OpenIddict.Server/OpenIddictServerHandlers.Introspection.cs +++ b/src/OpenIddict.Server/OpenIddictServerHandlers.Introspection.cs @@ -79,10 +79,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ExtractIntrospectionRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -141,10 +138,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ValidateIntrospectionRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -202,10 +196,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new HandleIntrospectionRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -305,10 +296,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ApplyIntrospectionResponseContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -347,10 +335,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateIntrospectionRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Reject introspection requests missing the mandatory token parameter. if (string.IsNullOrEmpty(context.Request.Token)) @@ -387,10 +372,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateIntrospectionRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Ensure a client_assertion_type is specified when a client_assertion was attached. if (!string.IsNullOrEmpty(context.Request.ClientAssertion) && @@ -471,10 +453,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidateIntrospectionRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ProcessAuthenticationContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -539,10 +518,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidateIntrospectionRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); @@ -582,10 +558,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateIntrospectionRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.GenericTokenPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -629,10 +602,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateIntrospectionRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); Debug.Assert(context.GenericTokenPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -695,10 +665,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(HandleIntrospectionRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = context.Transaction.GetProperty( typeof(ValidateIntrospectionRequestContext).FullName!) ?? @@ -730,10 +697,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(HandleIntrospectionRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.GenericTokenPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -808,10 +772,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(HandleIntrospectionRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.Request.ClientId), SR.FormatID4000(Parameters.ClientId)); Debug.Assert(context.GenericTokenPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -966,10 +927,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ApplyIntrospectionResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (string.IsNullOrEmpty(context.Error)) { diff --git a/src/OpenIddict.Server/OpenIddictServerHandlers.Protection.cs b/src/OpenIddict.Server/OpenIddictServerHandlers.Protection.cs index ee926b07..494428dc 100644 --- a/src/OpenIddict.Server/OpenIddictServerHandlers.Protection.cs +++ b/src/OpenIddict.Server/OpenIddictServerHandlers.Protection.cs @@ -83,10 +83,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // The OpenIddict server is expected to validate tokens it creates (e.g access tokens) // and tokens that are created by one or multiple clients (e.g client assertions). @@ -285,10 +282,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If no character was explicitly added, all characters are considered valid. if (context.AllowedCharset.Count is 0) @@ -342,10 +336,7 @@ public static partial class OpenIddictServerHandlers public async ValueTask HandleAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: reference tokens are never used for client assertions. if (context.ValidTokenTypes.Count is 1 && @@ -442,10 +433,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If a principal was already attached, don't overwrite it. if (context.Principal is not null) @@ -624,10 +612,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.Principal is null) { @@ -669,10 +654,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.Principal is null) { @@ -781,10 +763,7 @@ public static partial class OpenIddictServerHandlers public async ValueTask HandleAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.Principal is null) { @@ -862,10 +841,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.Principal is null) { @@ -946,10 +922,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Principal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -1006,10 +979,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Principal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -1069,10 +1039,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Principal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -1142,10 +1109,7 @@ public static partial class OpenIddictServerHandlers public async ValueTask HandleAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Principal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); Debug.Assert(!string.IsNullOrEmpty(context.TokenId), SR.GetResourceString(SR.ID4017)); @@ -1322,10 +1286,7 @@ public static partial class OpenIddictServerHandlers public async ValueTask HandleAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Principal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); Debug.Assert(!string.IsNullOrEmpty(context.AuthorizationId), SR.GetResourceString(SR.ID4018)); @@ -1382,10 +1343,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(GenerateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.SecurityTokenHandler = context.Options.JsonWebTokenHandler; @@ -1447,10 +1405,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(GenerateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var descriptor = new OpenIddictTokenDescriptor { @@ -1521,10 +1476,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(GenerateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.Principal is not { Identity: ClaimsIdentity } principal) { @@ -1573,10 +1525,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(GenerateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var claims = context.SecurityTokenDescriptor.Claims is not null ? new Dictionary(context.SecurityTokenDescriptor.Claims, StringComparer.Ordinal) : @@ -1690,10 +1639,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(GenerateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If a token was already attached by another handler, don't overwrite it. if (!string.IsNullOrEmpty(context.Token)) @@ -1739,10 +1685,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(GenerateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var identifier = context.Principal.GetTokenId(); if (string.IsNullOrEmpty(identifier)) diff --git a/src/OpenIddict.Server/OpenIddictServerHandlers.Revocation.cs b/src/OpenIddict.Server/OpenIddictServerHandlers.Revocation.cs index eee2f768..3eff7074 100644 --- a/src/OpenIddict.Server/OpenIddictServerHandlers.Revocation.cs +++ b/src/OpenIddict.Server/OpenIddictServerHandlers.Revocation.cs @@ -72,10 +72,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ExtractRevocationRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -134,10 +131,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ValidateRevocationRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -195,10 +189,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new HandleRevocationRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -252,10 +243,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ApplyRevocationResponseContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -294,10 +282,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateRevocationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Reject revocation requests missing the mandatory token parameter. if (string.IsNullOrEmpty(context.Request.Token)) @@ -334,10 +319,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateRevocationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Ensure a client_assertion_type is specified when a client_assertion was attached. if (!string.IsNullOrEmpty(context.Request.ClientAssertion) && @@ -418,10 +400,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidateRevocationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ProcessAuthenticationContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -486,10 +465,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidateRevocationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); @@ -529,10 +505,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateRevocationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.GenericTokenPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -576,10 +549,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateRevocationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); Debug.Assert(context.GenericTokenPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -642,10 +612,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(HandleRevocationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = context.Transaction.GetProperty( typeof(ValidateRevocationRequestContext).FullName!) ?? @@ -686,10 +653,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(HandleRevocationRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.GenericTokenPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -751,10 +715,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ApplyRevocationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (string.IsNullOrEmpty(context.Error)) { diff --git a/src/OpenIddict.Server/OpenIddictServerHandlers.Session.cs b/src/OpenIddict.Server/OpenIddictServerHandlers.Session.cs index 7cbebdee..8680b135 100644 --- a/src/OpenIddict.Server/OpenIddictServerHandlers.Session.cs +++ b/src/OpenIddict.Server/OpenIddictServerHandlers.Session.cs @@ -79,10 +79,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ExtractEndSessionRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -141,10 +138,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ValidateEndSessionRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -202,10 +196,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new HandleEndSessionRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -339,10 +330,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ApplyEndSessionResponseContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -381,10 +369,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateEndSessionRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (string.IsNullOrEmpty(context.Request.RequestUri)) { @@ -429,10 +414,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateEndSessionRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (string.IsNullOrEmpty(context.PostLogoutRedirectUri)) { @@ -491,10 +473,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidateEndSessionRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ProcessAuthenticationContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -548,10 +527,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateEndSessionRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var value = context.RequestTokenPrincipal?.GetClaim(Claims.Private.RequestParameters); if (string.IsNullOrEmpty(value)) @@ -601,10 +577,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidateEndSessionRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.PostLogoutRedirectUri), SR.FormatID4000(Parameters.PostLogoutRedirectUri)); @@ -752,10 +725,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidateEndSessionRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); @@ -814,10 +784,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidateEndSessionRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.IdentityTokenHintPrincipal is null) { @@ -935,10 +902,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(HandleEndSessionRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = context.Transaction.GetProperty( typeof(ValidateEndSessionRequestContext).FullName!) ?? @@ -969,10 +933,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ApplyEndSessionResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If the end session response contains a request token, do not use the // post_logout_redirect_uri, as the user agent will be redirected to the same page. @@ -1013,10 +974,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ApplyEndSessionResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If the user agent is expected to be redirected to the client application, attach the request // state to the end session response to help the client mitigate CSRF/session fixation attacks. diff --git a/src/OpenIddict.Server/OpenIddictServerHandlers.Userinfo.cs b/src/OpenIddict.Server/OpenIddictServerHandlers.Userinfo.cs index 539be58a..1562fba0 100644 --- a/src/OpenIddict.Server/OpenIddictServerHandlers.Userinfo.cs +++ b/src/OpenIddict.Server/OpenIddictServerHandlers.Userinfo.cs @@ -65,10 +65,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ExtractUserInfoRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -127,10 +124,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ValidateUserInfoRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -188,10 +182,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new HandleUserInfoRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -280,10 +271,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ApplyUserInfoResponseContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -322,10 +310,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ValidateUserInfoRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (string.IsNullOrEmpty(context.Request.AccessToken)) { @@ -366,10 +351,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ValidateUserInfoRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new ProcessAuthenticationContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -423,10 +405,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(HandleUserInfoRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = context.Transaction.GetProperty( typeof(ValidateUserInfoRequestContext).FullName!) ?? @@ -458,10 +437,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(HandleUserInfoRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.AccessTokenPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -492,10 +468,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(HandleUserInfoRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.AccessTokenPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); diff --git a/src/OpenIddict.Server/OpenIddictServerHandlers.cs b/src/OpenIddict.Server/OpenIddictServerHandlers.cs index 97e95ad9..58ffa3af 100644 --- a/src/OpenIddict.Server/OpenIddictServerHandlers.cs +++ b/src/OpenIddict.Server/OpenIddictServerHandlers.cs @@ -150,10 +150,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If the base or request URIs couldn't be resolved, don't try to infer the endpoint type. if (context is not { BaseUri.IsAbsoluteUri: true, RequestUri.IsAbsoluteUri: true }) @@ -245,10 +242,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return context.EndpointType switch { @@ -282,10 +276,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); (context.ExtractAccessToken, context.RequireAccessToken, @@ -451,10 +442,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.AccessToken = context.EndpointType switch { @@ -583,10 +571,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if ((context.RequireAccessToken && string.IsNullOrEmpty(context.AccessToken)) || (context.RequireActorToken && string.IsNullOrEmpty(context.ActorToken)) || @@ -636,10 +621,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (string.IsNullOrEmpty(context.ClientAssertion)) { @@ -713,10 +695,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.ClientAssertionPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -831,10 +810,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.ClientAssertionPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -907,10 +883,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.ClientAssertionPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -1007,10 +980,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Don't validate the client identifier on endpoints that don't support client identification. if (context.EndpointType is OpenIddictServerEndpointType.EndUserVerification or @@ -1114,10 +1084,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); @@ -1222,10 +1189,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); Debug.Assert(!string.IsNullOrEmpty(context.ClientSecret), SR.FormatID4000(Parameters.ClientSecret)); @@ -1286,10 +1250,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (string.IsNullOrEmpty(context.RequestToken)) { @@ -1356,10 +1317,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.RequestTokenPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -1412,10 +1370,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (string.IsNullOrEmpty(context.AccessToken)) { @@ -1488,10 +1443,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (string.IsNullOrEmpty(context.AuthorizationCode)) { @@ -1570,10 +1522,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (string.IsNullOrEmpty(context.DeviceCode)) { @@ -1652,10 +1601,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (string.IsNullOrEmpty(context.GenericToken)) { @@ -1748,10 +1694,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (string.IsNullOrEmpty(context.IdentityToken)) { @@ -1837,10 +1780,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (string.IsNullOrEmpty(context.RefreshToken)) { @@ -1919,10 +1859,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (string.IsNullOrEmpty(context.SubjectToken)) { @@ -2028,10 +1965,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (string.IsNullOrEmpty(context.ActorToken)) { @@ -2137,10 +2071,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (string.IsNullOrEmpty(context.UserCode)) { @@ -2212,10 +2143,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var principal = context.EndpointType switch { @@ -2265,10 +2193,7 @@ public static partial class OpenIddictServerHandlers public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: unlike other tokens, user codes may be potentially entered manually by users in a web form. // To make that easier, characters that are not part of the allowed charset are generally ignored. @@ -2337,10 +2262,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.EndpointType is not (OpenIddictServerEndpointType.Authorization or OpenIddictServerEndpointType.EndUserVerification or @@ -2373,10 +2295,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.Response.Error ??= context.EndpointType switch { @@ -2449,10 +2368,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.EndpointType is not OpenIddictServerEndpointType.EndUserVerification) { @@ -2508,10 +2424,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.EndpointType is not OpenIddictServerEndpointType.EndUserVerification) { @@ -2558,10 +2471,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.Parameters.Count is > 0) { @@ -2594,10 +2504,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); switch (context.EndpointType) { @@ -2756,10 +2663,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); switch (context.EndpointType) { @@ -2866,10 +2770,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Principal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -2954,10 +2855,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Principal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -2985,10 +2883,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Principal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -3022,10 +2917,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Principal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -3058,10 +2950,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Principal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -3097,10 +2986,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Principal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -3290,10 +3176,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Principal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -3387,10 +3270,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Principal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -3551,10 +3431,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Principal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -3683,10 +3560,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Principal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -3802,10 +3676,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Principal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -4074,10 +3945,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Principal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -4211,10 +4079,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Principal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -4341,10 +4206,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Principal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -4501,10 +4363,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Principal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -4605,10 +4464,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new GenerateTokenContext(context.Transaction) { @@ -4674,10 +4530,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new GenerateTokenContext(context.Transaction) { @@ -4741,10 +4594,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new GenerateTokenContext(context.Transaction) { @@ -4822,10 +4672,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new GenerateTokenContext(context.Transaction) { @@ -4901,10 +4748,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new GenerateTokenContext(context.Transaction) { @@ -4968,10 +4812,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new GenerateTokenContext(context.Transaction) { @@ -5033,10 +4874,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.UserCodePrincipal is null) { @@ -5082,10 +4920,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.EndpointType is not OpenIddictServerEndpointType.EndUserVerification || string.IsNullOrEmpty(context.DeviceCode)) @@ -5148,10 +4983,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.IdentityTokenPrincipal is null) { @@ -5242,10 +5074,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new GenerateTokenContext(context.Transaction) { @@ -5309,10 +5138,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new GenerateTokenContext(context.Transaction) { @@ -5372,10 +5198,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // To make user codes easier to read and type by humans, the user is formatted // using a display format string specified by the user or created by OpenIddict @@ -5433,10 +5256,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.IncludeAccessToken) { @@ -5599,10 +5419,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ProcessSignInContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.Parameters.Count is > 0) { @@ -5635,10 +5452,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ProcessSignOutContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.EndpointType is not OpenIddictServerEndpointType.EndSession) { @@ -5681,10 +5495,7 @@ public static partial class OpenIddictServerHandlers /// public async ValueTask HandleAsync(ProcessSignOutContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = context.Transaction.GetProperty( typeof(ProcessAuthenticationContext).FullName!) ?? @@ -5734,10 +5545,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ProcessSignOutContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.Parameters.Count is > 0) { @@ -5769,10 +5577,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ProcessErrorContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.Response.Error = context.Error; context.Response.ErrorDescription = context.ErrorDescription; @@ -5801,10 +5606,7 @@ public static partial class OpenIddictServerHandlers /// public ValueTask HandleAsync(ProcessErrorContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.Parameters.Count is > 0) { diff --git a/src/OpenIddict.Server/OpenIddictServerHelpers.cs b/src/OpenIddict.Server/OpenIddictServerHelpers.cs index 7c693a66..8091234b 100644 --- a/src/OpenIddict.Server/OpenIddictServerHelpers.cs +++ b/src/OpenIddict.Server/OpenIddictServerHelpers.cs @@ -21,15 +21,8 @@ public static class OpenIddictServerHelpers public static TProperty? GetProperty( this OpenIddictServerTransaction transaction, string name) where TProperty : class { - if (transaction is null) - { - throw new ArgumentNullException(nameof(transaction)); - } - - if (string.IsNullOrEmpty(name)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0106), nameof(name)); - } + ArgumentNullException.ThrowIfNull(transaction); + ArgumentException.ThrowIfNullOrEmpty(name); if (transaction.Properties.TryGetValue(name, out var property) && property is TProperty result) { @@ -51,15 +44,8 @@ public static class OpenIddictServerHelpers this OpenIddictServerTransaction transaction, string name, TProperty? value) where TProperty : class { - if (transaction is null) - { - throw new ArgumentNullException(nameof(transaction)); - } - - if (string.IsNullOrEmpty(name)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0106), nameof(name)); - } + ArgumentNullException.ThrowIfNull(transaction); + ArgumentException.ThrowIfNullOrEmpty(name); if (value is null) { diff --git a/src/OpenIddict.Validation.AspNetCore/OpenIddictValidationAspNetCoreBuilder.cs b/src/OpenIddict.Validation.AspNetCore/OpenIddictValidationAspNetCoreBuilder.cs index bce7128d..b895089d 100644 --- a/src/OpenIddict.Validation.AspNetCore/OpenIddictValidationAspNetCoreBuilder.cs +++ b/src/OpenIddict.Validation.AspNetCore/OpenIddictValidationAspNetCoreBuilder.cs @@ -36,10 +36,7 @@ public sealed class OpenIddictValidationAspNetCoreBuilder /// The instance. public OpenIddictValidationAspNetCoreBuilder Configure(Action configuration) { - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(configuration); Services.Configure(configuration); @@ -77,10 +74,7 @@ public sealed class OpenIddictValidationAspNetCoreBuilder /// The instance. public OpenIddictValidationAspNetCoreBuilder SetRealm(string realm) { - if (string.IsNullOrEmpty(realm)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0107), nameof(realm)); - } + ArgumentException.ThrowIfNullOrEmpty(realm); return Configure(options => options.Realm = realm); } diff --git a/src/OpenIddict.Validation.AspNetCore/OpenIddictValidationAspNetCoreConfiguration.cs b/src/OpenIddict.Validation.AspNetCore/OpenIddictValidationAspNetCoreConfiguration.cs index ba3fc4c7..898a7073 100644 --- a/src/OpenIddict.Validation.AspNetCore/OpenIddictValidationAspNetCoreConfiguration.cs +++ b/src/OpenIddict.Validation.AspNetCore/OpenIddictValidationAspNetCoreConfiguration.cs @@ -20,10 +20,7 @@ public sealed class OpenIddictValidationAspNetCoreConfiguration : IConfigureOpti /// public void Configure(AuthenticationOptions options) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); // If a handler was already registered and the type doesn't correspond to the OpenIddict handler, throw an exception. if (options.SchemeMap.TryGetValue(OpenIddictValidationAspNetCoreDefaults.AuthenticationScheme, out var builder) && @@ -39,10 +36,7 @@ public sealed class OpenIddictValidationAspNetCoreConfiguration : IConfigureOpti /// public void Configure(OpenIddictValidationOptions options) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); // Register the built-in event handlers used by the OpenIddict ASP.NET Core validation components. options.Handlers.AddRange(OpenIddictValidationAspNetCoreHandlers.DefaultHandlers); @@ -51,10 +45,7 @@ public sealed class OpenIddictValidationAspNetCoreConfiguration : IConfigureOpti /// public void PostConfigure(string? name, AuthenticationOptions options) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); if (!TryValidate(options.SchemeMap, options.DefaultSignInScheme) || !TryValidate(options.SchemeMap, options.DefaultSignOutScheme)) diff --git a/src/OpenIddict.Validation.AspNetCore/OpenIddictValidationAspNetCoreExtensions.cs b/src/OpenIddict.Validation.AspNetCore/OpenIddictValidationAspNetCoreExtensions.cs index 5935d3c6..b40afd2b 100644 --- a/src/OpenIddict.Validation.AspNetCore/OpenIddictValidationAspNetCoreExtensions.cs +++ b/src/OpenIddict.Validation.AspNetCore/OpenIddictValidationAspNetCoreExtensions.cs @@ -24,10 +24,7 @@ public static class OpenIddictValidationAspNetCoreExtensions /// The instance. public static OpenIddictValidationAspNetCoreBuilder UseAspNetCore(this OpenIddictValidationBuilder builder) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } + ArgumentNullException.ThrowIfNull(builder); builder.Services.AddAuthentication(); @@ -67,15 +64,8 @@ public static class OpenIddictValidationAspNetCoreExtensions public static OpenIddictValidationBuilder UseAspNetCore( this OpenIddictValidationBuilder builder, Action configuration) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } - - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(builder); + ArgumentNullException.ThrowIfNull(configuration); configuration(builder.UseAspNetCore()); diff --git a/src/OpenIddict.Validation.AspNetCore/OpenIddictValidationAspNetCoreHandler.cs b/src/OpenIddict.Validation.AspNetCore/OpenIddictValidationAspNetCoreHandler.cs index b752b39d..b668f0c8 100644 --- a/src/OpenIddict.Validation.AspNetCore/OpenIddictValidationAspNetCoreHandler.cs +++ b/src/OpenIddict.Validation.AspNetCore/OpenIddictValidationAspNetCoreHandler.cs @@ -27,7 +27,7 @@ public sealed class OpenIddictValidationAspNetCoreHandler : AuthenticationHandle /// /// Creates a new instance of the class. /// -#if SUPPORTS_TIME_PROVIDER +#if SUPPORTS_AUTHENTICATION_HANDLER_TIME_PROVIDER_ARGUMENT public OpenIddictValidationAspNetCoreHandler( IOpenIddictValidationDispatcher dispatcher, IOpenIddictValidationFactory factory, diff --git a/src/OpenIddict.Validation.AspNetCore/OpenIddictValidationAspNetCoreHandlerFilters.cs b/src/OpenIddict.Validation.AspNetCore/OpenIddictValidationAspNetCoreHandlerFilters.cs index 29b54219..0cb1d1a2 100644 --- a/src/OpenIddict.Validation.AspNetCore/OpenIddictValidationAspNetCoreHandlerFilters.cs +++ b/src/OpenIddict.Validation.AspNetCore/OpenIddictValidationAspNetCoreHandlerFilters.cs @@ -30,10 +30,7 @@ public static class OpenIddictValidationAspNetCoreHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!_options.CurrentValue.DisableAccessTokenExtractionFromAuthorizationHeader); } @@ -53,10 +50,7 @@ public static class OpenIddictValidationAspNetCoreHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!_options.CurrentValue.DisableAccessTokenExtractionFromBodyForm); } @@ -76,10 +70,7 @@ public static class OpenIddictValidationAspNetCoreHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!_options.CurrentValue.DisableAccessTokenExtractionFromQueryString); } @@ -93,10 +84,7 @@ public static class OpenIddictValidationAspNetCoreHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.Transaction.GetHttpRequest() is not null); } diff --git a/src/OpenIddict.Validation.AspNetCore/OpenIddictValidationAspNetCoreHandlers.cs b/src/OpenIddict.Validation.AspNetCore/OpenIddictValidationAspNetCoreHandlers.cs index 539e2be0..7689e37e 100644 --- a/src/OpenIddict.Validation.AspNetCore/OpenIddictValidationAspNetCoreHandlers.cs +++ b/src/OpenIddict.Validation.AspNetCore/OpenIddictValidationAspNetCoreHandlers.cs @@ -79,10 +79,7 @@ public static partial class OpenIddictValidationAspNetCoreHandlers /// public ValueTask HandleAsync(ProcessRequestContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to ASP.NET Core requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. @@ -132,10 +129,7 @@ public static partial class OpenIddictValidationAspNetCoreHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to ASP.NET Core requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. @@ -179,10 +173,7 @@ public static partial class OpenIddictValidationAspNetCoreHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If a token was already resolved, don't overwrite it. if (!string.IsNullOrEmpty(context.AccessToken)) @@ -231,10 +222,7 @@ public static partial class OpenIddictValidationAspNetCoreHandlers /// public async ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If a token was already resolved, don't overwrite it. if (!string.IsNullOrEmpty(context.AccessToken)) @@ -287,10 +275,7 @@ public static partial class OpenIddictValidationAspNetCoreHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If a token was already resolved, don't overwrite it. if (!string.IsNullOrEmpty(context.AccessToken)) @@ -337,10 +322,7 @@ public static partial class OpenIddictValidationAspNetCoreHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var properties = context.Transaction.GetProperty(typeof(AuthenticationProperties).FullName!); if (properties is { Items.Count: > 0 }) @@ -399,10 +381,7 @@ public static partial class OpenIddictValidationAspNetCoreHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var properties = context.Transaction.GetProperty(typeof(AuthenticationProperties).FullName!); if (properties is not null) @@ -437,10 +416,7 @@ public static partial class OpenIddictValidationAspNetCoreHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Transaction.Response is not null, SR.GetResourceString(SR.ID4007)); @@ -487,10 +463,7 @@ public static partial class OpenIddictValidationAspNetCoreHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to ASP.NET Core requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. @@ -531,10 +504,7 @@ public static partial class OpenIddictValidationAspNetCoreHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Transaction.Response is not null, SR.GetResourceString(SR.ID4007)); @@ -630,10 +600,7 @@ public static partial class OpenIddictValidationAspNetCoreHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to ASP.NET Core requests. If the HTTP context cannot be resolved, // this may indicate that the request was incorrectly processed by another server stack. diff --git a/src/OpenIddict.Validation.AspNetCore/OpenIddictValidationAspNetCoreHelpers.cs b/src/OpenIddict.Validation.AspNetCore/OpenIddictValidationAspNetCoreHelpers.cs index ba115bd1..a51cfce3 100644 --- a/src/OpenIddict.Validation.AspNetCore/OpenIddictValidationAspNetCoreHelpers.cs +++ b/src/OpenIddict.Validation.AspNetCore/OpenIddictValidationAspNetCoreHelpers.cs @@ -21,10 +21,7 @@ public static class OpenIddictValidationAspNetCoreHelpers /// The instance or if it couldn't be found. public static HttpRequest? GetHttpRequest(this OpenIddictValidationTransaction transaction) { - if (transaction is null) - { - throw new ArgumentNullException(nameof(transaction)); - } + ArgumentNullException.ThrowIfNull(transaction); if (!transaction.Properties.TryGetValue(typeof(HttpRequest).FullName!, out object? property)) { @@ -46,10 +43,7 @@ public static class OpenIddictValidationAspNetCoreHelpers /// The . public static OpenIddictValidationEndpointType GetOpenIddictValidationEndpointType(this HttpContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return context.Features.Get()?.Transaction?.EndpointType ?? default; } @@ -61,10 +55,7 @@ public static class OpenIddictValidationAspNetCoreHelpers /// The instance or null if it couldn't be found. public static OpenIddictRequest? GetOpenIddictValidationRequest(this HttpContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return context.Features.Get()?.Transaction?.Request; } @@ -76,10 +67,7 @@ public static class OpenIddictValidationAspNetCoreHelpers /// The instance or null if it couldn't be found. public static OpenIddictResponse? GetOpenIddictValidationResponse(this HttpContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return context.Features.Get()?.Transaction?.Response; } diff --git a/src/OpenIddict.Validation.DataProtection/OpenIddictValidationDataProtectionBuilder.cs b/src/OpenIddict.Validation.DataProtection/OpenIddictValidationDataProtectionBuilder.cs index 7b2ff255..71736478 100644 --- a/src/OpenIddict.Validation.DataProtection/OpenIddictValidationDataProtectionBuilder.cs +++ b/src/OpenIddict.Validation.DataProtection/OpenIddictValidationDataProtectionBuilder.cs @@ -37,10 +37,7 @@ public sealed class OpenIddictValidationDataProtectionBuilder /// The instance. public OpenIddictValidationDataProtectionBuilder Configure(Action configuration) { - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(configuration); Services.Configure(configuration); @@ -55,10 +52,7 @@ public sealed class OpenIddictValidationDataProtectionBuilder /// The instance. public OpenIddictValidationDataProtectionBuilder UseDataProtectionProvider(IDataProtectionProvider provider) { - if (provider is null) - { - throw new ArgumentNullException(nameof(provider)); - } + ArgumentNullException.ThrowIfNull(provider); return Configure(options => options.DataProtectionProvider = provider); } @@ -70,10 +64,7 @@ public sealed class OpenIddictValidationDataProtectionBuilder /// The instance. public OpenIddictValidationDataProtectionBuilder UseFormatter(IOpenIddictValidationDataProtectionFormatter formatter) { - if (formatter is null) - { - throw new ArgumentNullException(nameof(formatter)); - } + ArgumentNullException.ThrowIfNull(formatter); return Configure(options => options.Formatter = formatter); } diff --git a/src/OpenIddict.Validation.DataProtection/OpenIddictValidationDataProtectionConfiguration.cs b/src/OpenIddict.Validation.DataProtection/OpenIddictValidationDataProtectionConfiguration.cs index 09c28cdc..1bf42a4e 100644 --- a/src/OpenIddict.Validation.DataProtection/OpenIddictValidationDataProtectionConfiguration.cs +++ b/src/OpenIddict.Validation.DataProtection/OpenIddictValidationDataProtectionConfiguration.cs @@ -29,10 +29,7 @@ public sealed class OpenIddictValidationDataProtectionConfiguration : IConfigure /// public void Configure(OpenIddictValidationOptions options) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); // Register the built-in event handlers used by the OpenIddict Data Protection validation components. options.Handlers.AddRange(OpenIddictValidationDataProtectionHandlers.DefaultHandlers); @@ -41,10 +38,7 @@ public sealed class OpenIddictValidationDataProtectionConfiguration : IConfigure /// public void PostConfigure(string? name, OpenIddictValidationDataProtectionOptions options) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); options.DataProtectionProvider ??= _dataProtectionProvider; } diff --git a/src/OpenIddict.Validation.DataProtection/OpenIddictValidationDataProtectionExtensions.cs b/src/OpenIddict.Validation.DataProtection/OpenIddictValidationDataProtectionExtensions.cs index 3f6cd870..f38c0651 100644 --- a/src/OpenIddict.Validation.DataProtection/OpenIddictValidationDataProtectionExtensions.cs +++ b/src/OpenIddict.Validation.DataProtection/OpenIddictValidationDataProtectionExtensions.cs @@ -24,10 +24,7 @@ public static class OpenIddictValidationDataProtectionExtensions /// The instance. public static OpenIddictValidationDataProtectionBuilder UseDataProtection(this OpenIddictValidationBuilder builder) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } + ArgumentNullException.ThrowIfNull(builder); builder.Services.AddDataProtection(); @@ -55,15 +52,8 @@ public static class OpenIddictValidationDataProtectionExtensions public static OpenIddictValidationBuilder UseDataProtection( this OpenIddictValidationBuilder builder, Action configuration) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } - - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(builder); + ArgumentNullException.ThrowIfNull(configuration); configuration(builder.UseDataProtection()); diff --git a/src/OpenIddict.Validation.DataProtection/OpenIddictValidationDataProtectionFormatter.cs b/src/OpenIddict.Validation.DataProtection/OpenIddictValidationDataProtectionFormatter.cs index 983a982f..fdca201b 100644 --- a/src/OpenIddict.Validation.DataProtection/OpenIddictValidationDataProtectionFormatter.cs +++ b/src/OpenIddict.Validation.DataProtection/OpenIddictValidationDataProtectionFormatter.cs @@ -16,10 +16,7 @@ public sealed class OpenIddictValidationDataProtectionFormatter : IOpenIddictVal { public ClaimsPrincipal ReadToken(BinaryReader reader) { - if (reader is null) - { - throw new ArgumentNullException(nameof(reader)); - } + ArgumentNullException.ThrowIfNull(reader); var (principal, properties) = Read(reader); diff --git a/src/OpenIddict.Validation.Owin/OpenIddictValidationOwinBuilder.cs b/src/OpenIddict.Validation.Owin/OpenIddictValidationOwinBuilder.cs index 75bd27c6..2aa3190d 100644 --- a/src/OpenIddict.Validation.Owin/OpenIddictValidationOwinBuilder.cs +++ b/src/OpenIddict.Validation.Owin/OpenIddictValidationOwinBuilder.cs @@ -91,10 +91,7 @@ public sealed class OpenIddictValidationOwinBuilder /// The instance. public OpenIddictValidationOwinBuilder SetRealm(string realm) { - if (string.IsNullOrEmpty(realm)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0107), nameof(realm)); - } + ArgumentException.ThrowIfNullOrEmpty(realm); return Configure(options => options.Realm = realm); } diff --git a/src/OpenIddict.Validation.ServerIntegration/OpenIddictValidationServerIntegrationBuilder.cs b/src/OpenIddict.Validation.ServerIntegration/OpenIddictValidationServerIntegrationBuilder.cs index 4165a3db..2e397a53 100644 --- a/src/OpenIddict.Validation.ServerIntegration/OpenIddictValidationServerIntegrationBuilder.cs +++ b/src/OpenIddict.Validation.ServerIntegration/OpenIddictValidationServerIntegrationBuilder.cs @@ -35,10 +35,7 @@ public sealed class OpenIddictValidationServerIntegrationBuilder /// The instance. public OpenIddictValidationServerIntegrationBuilder Configure(Action configuration) { - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(configuration); Services.Configure(configuration); diff --git a/src/OpenIddict.Validation.ServerIntegration/OpenIddictValidationServerIntegrationConfiguration.cs b/src/OpenIddict.Validation.ServerIntegration/OpenIddictValidationServerIntegrationConfiguration.cs index 18dae6af..2ccd3934 100644 --- a/src/OpenIddict.Validation.ServerIntegration/OpenIddictValidationServerIntegrationConfiguration.cs +++ b/src/OpenIddict.Validation.ServerIntegration/OpenIddictValidationServerIntegrationConfiguration.cs @@ -29,10 +29,7 @@ public sealed class OpenIddictValidationServerIntegrationConfiguration : IConfig /// public void Configure(OpenIddictValidationOptions options) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); // Note: the issuer may be null. In this case, it will be usually provided by // a validation handler registered by the host (e.g ASP.NET Core or OWIN/Katana). @@ -58,10 +55,7 @@ public sealed class OpenIddictValidationServerIntegrationConfiguration : IConfig /// public void PostConfigure(string? name, OpenIddictValidationOptions options) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); if (options.ValidationType is not OpenIddictValidationType.Direct) { diff --git a/src/OpenIddict.Validation.ServerIntegration/OpenIddictValidationServerIntegrationExtensions.cs b/src/OpenIddict.Validation.ServerIntegration/OpenIddictValidationServerIntegrationExtensions.cs index 99bee368..fd7ccd60 100644 --- a/src/OpenIddict.Validation.ServerIntegration/OpenIddictValidationServerIntegrationExtensions.cs +++ b/src/OpenIddict.Validation.ServerIntegration/OpenIddictValidationServerIntegrationExtensions.cs @@ -25,10 +25,7 @@ public static class OpenIddictValidationServerIntegrationExtensions /// The instance. public static OpenIddictValidationServerIntegrationBuilder UseLocalServer(this OpenIddictValidationBuilder builder) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } + ArgumentNullException.ThrowIfNull(builder); // Note: TryAddEnumerable() is used here to ensure the initializers are registered only once. builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< @@ -51,15 +48,8 @@ public static class OpenIddictValidationServerIntegrationExtensions public static OpenIddictValidationBuilder UseLocalServer( this OpenIddictValidationBuilder builder, Action configuration) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } - - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(builder); + ArgumentNullException.ThrowIfNull(configuration); configuration(builder.UseLocalServer()); diff --git a/src/OpenIddict.Validation.SystemNetHttp/OpenIddict.Validation.SystemNetHttp.csproj b/src/OpenIddict.Validation.SystemNetHttp/OpenIddict.Validation.SystemNetHttp.csproj index 3b2fcfcb..5ed8891f 100644 --- a/src/OpenIddict.Validation.SystemNetHttp/OpenIddict.Validation.SystemNetHttp.csproj +++ b/src/OpenIddict.Validation.SystemNetHttp/OpenIddict.Validation.SystemNetHttp.csproj @@ -6,6 +6,7 @@ $(NetCoreTargetFrameworks); $(NetStandardTargetFrameworks) + $(DefineConstants);FeatureRuntimeInformation diff --git a/src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpBuilder.cs b/src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpBuilder.cs index 7071d7ac..54d2c423 100644 --- a/src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpBuilder.cs +++ b/src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpBuilder.cs @@ -41,10 +41,7 @@ public sealed class OpenIddictValidationSystemNetHttpBuilder /// The instance. public OpenIddictValidationSystemNetHttpBuilder Configure(Action configuration) { - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(configuration); Services.Configure(configuration); @@ -59,10 +56,7 @@ public sealed class OpenIddictValidationSystemNetHttpBuilder [EditorBrowsable(EditorBrowsableState.Advanced)] public OpenIddictValidationSystemNetHttpBuilder ConfigureHttpClient(Action configuration) { - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(configuration); return Configure(options => options.HttpClientActions.Add(configuration)); } @@ -75,10 +69,7 @@ public sealed class OpenIddictValidationSystemNetHttpBuilder [EditorBrowsable(EditorBrowsableState.Advanced)] public OpenIddictValidationSystemNetHttpBuilder ConfigureHttpClientHandler(Action configuration) { - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(configuration); return Configure(options => options.HttpClientHandlerActions.Add(configuration)); } @@ -91,10 +82,7 @@ public sealed class OpenIddictValidationSystemNetHttpBuilder /// The instance. public OpenIddictValidationSystemNetHttpBuilder SetContactAddress(MailAddress address) { - if (address is null) - { - throw new ArgumentNullException(nameof(address)); - } + ArgumentNullException.ThrowIfNull(address); return Configure(options => options.ContactAddress = address); } @@ -107,10 +95,7 @@ public sealed class OpenIddictValidationSystemNetHttpBuilder /// The instance. public OpenIddictValidationSystemNetHttpBuilder SetContactAddress(string address) { - if (string.IsNullOrEmpty(address)) - { - throw new ArgumentException(SR.FormatID0366(nameof(address)), nameof(address)); - } + ArgumentException.ThrowIfNullOrEmpty(address); return SetContactAddress(new MailAddress(address)); } @@ -122,10 +107,7 @@ public sealed class OpenIddictValidationSystemNetHttpBuilder /// The instance. public OpenIddictValidationSystemNetHttpBuilder SetHttpErrorPolicy(IAsyncPolicy policy) { - if (policy is null) - { - throw new ArgumentNullException(nameof(policy)); - } + ArgumentNullException.ThrowIfNull(policy); return Configure(options => options.HttpErrorPolicy = policy); } @@ -145,10 +127,7 @@ public sealed class OpenIddictValidationSystemNetHttpBuilder public OpenIddictValidationSystemNetHttpBuilder SetHttpResiliencePipeline( Action> configuration) { - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(configuration); var builder = new ResiliencePipelineBuilder(); configuration(builder); @@ -167,10 +146,7 @@ public sealed class OpenIddictValidationSystemNetHttpBuilder /// The instance. public OpenIddictValidationSystemNetHttpBuilder SetHttpResiliencePipeline(ResiliencePipeline pipeline) { - if (pipeline is null) - { - throw new ArgumentNullException(nameof(pipeline)); - } + ArgumentNullException.ThrowIfNull(pipeline); return Configure(options => options.HttpResiliencePipeline = pipeline); } @@ -184,10 +160,7 @@ public sealed class OpenIddictValidationSystemNetHttpBuilder /// The instance. public OpenIddictValidationSystemNetHttpBuilder SetProductInformation(ProductInfoHeaderValue information) { - if (information is null) - { - throw new ArgumentNullException(nameof(information)); - } + ArgumentNullException.ThrowIfNull(information); return Configure(options => options.ProductInformation = information); } @@ -201,10 +174,7 @@ public sealed class OpenIddictValidationSystemNetHttpBuilder /// The instance. public OpenIddictValidationSystemNetHttpBuilder SetProductInformation(string name, string? version) { - if (string.IsNullOrEmpty(name)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0345), nameof(name)); - } + ArgumentException.ThrowIfNullOrEmpty(name); return SetProductInformation(new ProductInfoHeaderValue(name, version)); } @@ -218,10 +188,7 @@ public sealed class OpenIddictValidationSystemNetHttpBuilder /// The instance. public OpenIddictValidationSystemNetHttpBuilder SetProductInformation(Assembly assembly) { - if (assembly is null) - { - throw new ArgumentNullException(nameof(assembly)); - } + ArgumentNullException.ThrowIfNull(assembly); return SetProductInformation(new ProductInfoHeaderValue( productName: assembly.GetName().Name!, @@ -244,10 +211,7 @@ public sealed class OpenIddictValidationSystemNetHttpBuilder public OpenIddictValidationSystemNetHttpBuilder SetSelfSignedTlsClientAuthenticationCertificateSelector( Func selector) { - if (selector is null) - { - throw new ArgumentNullException(nameof(selector)); - } + ArgumentNullException.ThrowIfNull(selector); return Configure(options => options.SelfSignedTlsClientAuthenticationCertificateSelector = selector); } @@ -268,10 +232,7 @@ public sealed class OpenIddictValidationSystemNetHttpBuilder public OpenIddictValidationSystemNetHttpBuilder SetTlsClientAuthenticationCertificateSelector( Func selector) { - if (selector is null) - { - throw new ArgumentNullException(nameof(selector)); - } + ArgumentNullException.ThrowIfNull(selector); return Configure(options => options.TlsClientAuthenticationCertificateSelector = selector); } diff --git a/src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpConfiguration.cs b/src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpConfiguration.cs index 39d73cb1..ef8fcdfc 100644 --- a/src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpConfiguration.cs +++ b/src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpConfiguration.cs @@ -42,10 +42,7 @@ public sealed class OpenIddictValidationSystemNetHttpConfiguration : IConfigureO /// public void Configure(OpenIddictValidationOptions options) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); // Register the built-in event handlers used by the OpenIddict System.Net.Http validation components. options.Handlers.AddRange(OpenIddictValidationSystemNetHttpHandlers.DefaultHandlers); @@ -62,10 +59,7 @@ public sealed class OpenIddictValidationSystemNetHttpConfiguration : IConfigureO /// public void Configure(string? name, HttpClientFactoryOptions options) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); var assembly = typeof(OpenIddictValidationSystemNetHttpOptions).Assembly.GetName(); @@ -166,10 +160,7 @@ public sealed class OpenIddictValidationSystemNetHttpConfiguration : IConfigureO /// public void PostConfigure(string? name, HttpClientFactoryOptions options) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); var assembly = typeof(OpenIddictValidationSystemNetHttpOptions).Assembly.GetName(); @@ -251,10 +242,7 @@ public sealed class OpenIddictValidationSystemNetHttpConfiguration : IConfigureO public void PostConfigure(string? name, OpenIddictValidationSystemNetHttpOptions options) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); // If no client authentication certificate selector was provided, use fallback delegates that // automatically use the first X.509 signing certificate attached to the client registration diff --git a/src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpExtensions.cs b/src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpExtensions.cs index 76c79367..9ce061c2 100644 --- a/src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpExtensions.cs +++ b/src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpExtensions.cs @@ -25,10 +25,7 @@ public static class OpenIddictValidationSystemNetHttpExtensions /// The instance. public static OpenIddictValidationSystemNetHttpBuilder UseSystemNetHttp(this OpenIddictValidationBuilder builder) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } + ArgumentNullException.ThrowIfNull(builder); builder.Services.AddHttpClient(); @@ -65,15 +62,8 @@ public static class OpenIddictValidationSystemNetHttpExtensions public static OpenIddictValidationBuilder UseSystemNetHttp( this OpenIddictValidationBuilder builder, Action configuration) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } - - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(builder); + ArgumentNullException.ThrowIfNull(configuration); configuration(builder.UseSystemNetHttp()); diff --git a/src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpHandlerFilters.cs b/src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpHandlerFilters.cs index c47afa3e..e0e1aaca 100644 --- a/src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpHandlerFilters.cs +++ b/src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpHandlerFilters.cs @@ -19,10 +19,7 @@ public static class OpenIddictValidationSystemNetHttpHandlerFilters /// public ValueTask IsActiveAsync(BaseExternalContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new( string.Equals(context.RemoteUri?.Scheme, Uri.UriSchemeHttp, StringComparison.OrdinalIgnoreCase) || diff --git a/src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpHandlers.cs b/src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpHandlers.cs index fc1d6a14..27aab0df 100644 --- a/src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpHandlers.cs +++ b/src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpHandlers.cs @@ -10,7 +10,6 @@ using System.Diagnostics; using System.IO.Compression; using System.Net.Http; using System.Net.Http.Headers; -using System.Runtime.InteropServices; using System.Text; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; @@ -59,10 +58,7 @@ public static partial class OpenIddictValidationSystemNetHttpHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If an explicit client authentication method was attached, don't overwrite it. if (!string.IsNullOrEmpty(context.IntrospectionEndpointClientAuthenticationMethod)) @@ -174,10 +170,7 @@ public static partial class OpenIddictValidationSystemNetHttpHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: HttpClientFactory doesn't support flowing a list of properties that can be // accessed from the HttpClientAction or HttpMessageHandlerBuilderAction delegates @@ -239,10 +232,7 @@ public static partial class OpenIddictValidationSystemNetHttpHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Store the HttpRequestMessage in the transaction properties. context.Transaction.SetProperty(typeof(HttpRequestMessage).FullName!, @@ -271,10 +261,7 @@ public static partial class OpenIddictValidationSystemNetHttpHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Store the HttpRequestMessage in the transaction properties. context.Transaction.SetProperty(typeof(HttpRequestMessage).FullName!, @@ -303,10 +290,7 @@ public static partial class OpenIddictValidationSystemNetHttpHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); #if SUPPORTS_HTTP_CLIENT_DEFAULT_REQUEST_VERSION || SUPPORTS_HTTP_CLIENT_DEFAULT_REQUEST_VERSION_POLICY // This handler only applies to System.Net.Http requests. If the HTTP request cannot be resolved, @@ -351,10 +335,7 @@ public static partial class OpenIddictValidationSystemNetHttpHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to System.Net.Http requests. If the HTTP request cannot be resolved, // this may indicate that the request was incorrectly processed by another client stack. @@ -396,10 +377,7 @@ public static partial class OpenIddictValidationSystemNetHttpHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Transaction.Request is not null, SR.GetResourceString(SR.ID4008)); @@ -452,10 +430,7 @@ public static partial class OpenIddictValidationSystemNetHttpHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to System.Net.Http requests. If the HTTP request cannot be resolved, // this may indicate that the request was incorrectly processed by another client stack. @@ -488,10 +463,7 @@ public static partial class OpenIddictValidationSystemNetHttpHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Transaction.Request is not null, SR.GetResourceString(SR.ID4008)); @@ -545,10 +517,7 @@ public static partial class OpenIddictValidationSystemNetHttpHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Transaction.Request is not null, SR.GetResourceString(SR.ID4008)); @@ -605,10 +574,7 @@ public static partial class OpenIddictValidationSystemNetHttpHandlers /// public async ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to System.Net.Http requests. If the HTTP request cannot be resolved, // this may indicate that the request was incorrectly processed by another client stack. @@ -669,10 +635,7 @@ public static partial class OpenIddictValidationSystemNetHttpHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to System.Net.Http requests. If the HTTP request cannot be resolved, // this may indicate that the request was incorrectly processed by another client stack. @@ -707,10 +670,7 @@ public static partial class OpenIddictValidationSystemNetHttpHandlers /// public async ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: automatic content decompression can be enabled by constructing an HttpClient wrapping // a generic HttpClientHandler, a SocketsHttpHandler or a WinHttpHandler instance with the @@ -752,7 +712,7 @@ public static partial class OpenIddictValidationSystemNetHttpHandlers // Content-Encoding header from the response, which leads to incorrect results when trying // to decompress the content a second time. To avoid that, the entire logic used in this // handler is ignored on iOS if the native HTTP handler (NSUrlSessionHandler) is used. - if (RuntimeInformation.IsOSPlatform(OSPlatform.Create("ios")) && + if (OperatingSystem.IsIOS() && AppContext.TryGetSwitch("System.Net.Http.UseNativeHttpHandler", out bool value) && value) { return; @@ -853,10 +813,7 @@ public static partial class OpenIddictValidationSystemNetHttpHandlers /// public async ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Don't overwrite the response if one was already provided. if (context.Transaction.Response is not null) @@ -932,10 +889,7 @@ public static partial class OpenIddictValidationSystemNetHttpHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Don't overwrite the response if one was already provided. if (context.Transaction.Response is not null) @@ -1061,10 +1015,7 @@ public static partial class OpenIddictValidationSystemNetHttpHandlers /// public async ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to System.Net.Http requests. If the HTTP response cannot be resolved, // this may indicate that the request was incorrectly processed by another client stack. @@ -1131,10 +1082,7 @@ public static partial class OpenIddictValidationSystemNetHttpHandlers /// public ValueTask HandleAsync(TContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // This handler only applies to System.Net.Http requests. If the HTTP response cannot be resolved, // this may indicate that the request was incorrectly processed by another client stack. diff --git a/src/OpenIddict.Validation/OpenIddictValidationBuilder.cs b/src/OpenIddict.Validation/OpenIddictValidationBuilder.cs index 85b9ee63..3d1e7ea8 100644 --- a/src/OpenIddict.Validation/OpenIddictValidationBuilder.cs +++ b/src/OpenIddict.Validation/OpenIddictValidationBuilder.cs @@ -7,7 +7,6 @@ using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using System.Reflection; -using System.Runtime.InteropServices; using System.Security.Claims; using System.Security.Cryptography.X509Certificates; using Microsoft.Extensions.DependencyInjection.Extensions; @@ -45,10 +44,7 @@ public sealed class OpenIddictValidationBuilder Action> configuration) where TContext : BaseContext { - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(configuration); // Note: handlers registered using this API are assumed to be custom handlers by default. var builder = OpenIddictValidationHandlerDescriptor.CreateBuilder() @@ -67,10 +63,7 @@ public sealed class OpenIddictValidationBuilder [EditorBrowsable(EditorBrowsableState.Advanced)] public OpenIddictValidationBuilder AddEventHandler(OpenIddictValidationHandlerDescriptor descriptor) { - if (descriptor is null) - { - throw new ArgumentNullException(nameof(descriptor)); - } + ArgumentNullException.ThrowIfNull(descriptor); // Register the handler in the services collection. Services.Add(descriptor.ServiceDescriptor); @@ -86,10 +79,7 @@ public sealed class OpenIddictValidationBuilder [EditorBrowsable(EditorBrowsableState.Advanced)] public OpenIddictValidationBuilder RemoveEventHandler(OpenIddictValidationHandlerDescriptor descriptor) { - if (descriptor is null) - { - throw new ArgumentNullException(nameof(descriptor)); - } + ArgumentNullException.ThrowIfNull(descriptor); Services.RemoveAll(descriptor.ServiceDescriptor.ServiceType); @@ -115,10 +105,7 @@ public sealed class OpenIddictValidationBuilder /// The instance. public OpenIddictValidationBuilder Configure(Action configuration) { - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(configuration); Services.Configure(configuration); @@ -132,10 +119,7 @@ public sealed class OpenIddictValidationBuilder /// The instance. public OpenIddictValidationBuilder AddEncryptionCredentials(EncryptingCredentials credentials) { - if (credentials is null) - { - throw new ArgumentNullException(nameof(credentials)); - } + ArgumentNullException.ThrowIfNull(credentials); return Configure(options => options.EncryptionCredentials.Add(credentials)); } @@ -147,10 +131,7 @@ public sealed class OpenIddictValidationBuilder /// The instance. public OpenIddictValidationBuilder AddEncryptionKey(SecurityKey key) { - if (key is null) - { - throw new ArgumentNullException(nameof(key)); - } + ArgumentNullException.ThrowIfNull(key); // If the encryption key is an asymmetric security key, ensure it has a private key. if (key is AsymmetricSecurityKey asymmetricSecurityKey && @@ -181,10 +162,7 @@ public sealed class OpenIddictValidationBuilder /// The instance. public OpenIddictValidationBuilder AddEncryptionKeys(IEnumerable keys) { - if (keys is null) - { - throw new ArgumentNullException(nameof(keys)); - } + ArgumentNullException.ThrowIfNull(keys); return keys.Aggregate(this, static (builder, key) => builder.AddEncryptionKey(key)); } @@ -196,10 +174,7 @@ public sealed class OpenIddictValidationBuilder /// The instance. public OpenIddictValidationBuilder AddEncryptionCertificate(X509Certificate2 certificate) { - if (certificate is null) - { - throw new ArgumentNullException(nameof(certificate)); - } + ArgumentNullException.ThrowIfNull(certificate); // If the certificate is a X.509v3 certificate that specifies at least one // key usage, ensure that the certificate key can be used for key encryption. @@ -232,7 +207,7 @@ public sealed class OpenIddictValidationBuilder Assembly assembly, string resource, string? password) #if SUPPORTS_EPHEMERAL_KEY_SETS // Note: ephemeral key sets are currently not supported on macOS. - => AddEncryptionCertificate(assembly, resource, password, RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? + => AddEncryptionCertificate(assembly, resource, password, OperatingSystem.IsMacOS() ? X509KeyStorageFlags.MachineKeySet : X509KeyStorageFlags.EphemeralKeySet); #else @@ -251,15 +226,8 @@ public sealed class OpenIddictValidationBuilder Assembly assembly, string resource, string? password, X509KeyStorageFlags flags) { - if (assembly is null) - { - throw new ArgumentNullException(nameof(assembly)); - } - - if (string.IsNullOrEmpty(resource)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0062), nameof(resource)); - } + ArgumentNullException.ThrowIfNull(assembly); + ArgumentException.ThrowIfNullOrEmpty(resource); using var stream = assembly.GetManifestResourceStream(resource) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0064)); @@ -276,7 +244,7 @@ public sealed class OpenIddictValidationBuilder public OpenIddictValidationBuilder AddEncryptionCertificate(Stream stream, string? password) #if SUPPORTS_EPHEMERAL_KEY_SETS // Note: ephemeral key sets are currently not supported on macOS. - => AddEncryptionCertificate(stream, password, RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? + => AddEncryptionCertificate(stream, password, OperatingSystem.IsMacOS() ? X509KeyStorageFlags.MachineKeySet : X509KeyStorageFlags.EphemeralKeySet); #else @@ -293,10 +261,7 @@ public sealed class OpenIddictValidationBuilder public OpenIddictValidationBuilder AddEncryptionCertificate( Stream stream, string? password, X509KeyStorageFlags flags) { - if (stream is null) - { - throw new ArgumentNullException(nameof(stream)); - } + ArgumentNullException.ThrowIfNull(stream); using var buffer = new MemoryStream(); stream.CopyTo(buffer); @@ -321,10 +286,7 @@ public sealed class OpenIddictValidationBuilder /// The instance. public OpenIddictValidationBuilder AddEncryptionCertificate(string thumbprint) { - if (string.IsNullOrEmpty(thumbprint)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0065), nameof(thumbprint)); - } + ArgumentException.ThrowIfNullOrEmpty(thumbprint); return AddEncryptionCertificate( GetCertificate(StoreLocation.CurrentUser, thumbprint) ?? @@ -352,10 +314,7 @@ public sealed class OpenIddictValidationBuilder public OpenIddictValidationBuilder AddEncryptionCertificate( string thumbprint, StoreName name, StoreLocation location) { - if (string.IsNullOrEmpty(thumbprint)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0065), nameof(thumbprint)); - } + ArgumentException.ThrowIfNullOrEmpty(thumbprint); using var store = new X509Store(name, location); store.Open(OpenFlags.ReadOnly); @@ -373,11 +332,8 @@ public sealed class OpenIddictValidationBuilder /// The instance. public OpenIddictValidationBuilder AddEncryptionCertificates(IEnumerable certificates) { - if (certificates is null) - { - throw new ArgumentNullException(nameof(certificates)); - } - + ArgumentNullException.ThrowIfNull(certificates); + return certificates.Aggregate(this, static (builder, certificate) => builder.AddEncryptionCertificate(certificate)); } @@ -388,10 +344,7 @@ public sealed class OpenIddictValidationBuilder /// The instance. public OpenIddictValidationBuilder AddSigningCredentials(SigningCredentials credentials) { - if (credentials is null) - { - throw new ArgumentNullException(nameof(credentials)); - } + ArgumentNullException.ThrowIfNull(credentials); return Configure(options => options.SigningCredentials.Add(credentials)); } @@ -403,10 +356,7 @@ public sealed class OpenIddictValidationBuilder /// The instance. public OpenIddictValidationBuilder AddSigningKey(SecurityKey key) { - if (key is null) - { - throw new ArgumentNullException(nameof(key)); - } + ArgumentNullException.ThrowIfNull(key); // If the signing key is an asymmetric security key, ensure it has a private key. if (key is AsymmetricSecurityKey asymmetricSecurityKey && @@ -460,10 +410,7 @@ public sealed class OpenIddictValidationBuilder /// The instance. public OpenIddictValidationBuilder AddSigningKeys(IEnumerable keys) { - if (keys is null) - { - throw new ArgumentNullException(nameof(keys)); - } + ArgumentNullException.ThrowIfNull(keys); return keys.Aggregate(this, static (builder, key) => builder.AddSigningKey(key)); } @@ -475,10 +422,7 @@ public sealed class OpenIddictValidationBuilder /// The instance. public OpenIddictValidationBuilder AddSigningCertificate(X509Certificate2 certificate) { - if (certificate is null) - { - throw new ArgumentNullException(nameof(certificate)); - } + ArgumentNullException.ThrowIfNull(certificate); // If the certificate is a X.509v3 certificate that specifies at least // one key usage, ensure that the certificate key can be used for signing. @@ -510,7 +454,7 @@ public sealed class OpenIddictValidationBuilder public OpenIddictValidationBuilder AddSigningCertificate(Assembly assembly, string resource, string? password) #if SUPPORTS_EPHEMERAL_KEY_SETS // Note: ephemeral key sets are currently not supported on macOS. - => AddSigningCertificate(assembly, resource, password, RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? + => AddSigningCertificate(assembly, resource, password, OperatingSystem.IsMacOS() ? X509KeyStorageFlags.MachineKeySet : X509KeyStorageFlags.EphemeralKeySet); #else @@ -529,15 +473,8 @@ public sealed class OpenIddictValidationBuilder Assembly assembly, string resource, string? password, X509KeyStorageFlags flags) { - if (assembly is null) - { - throw new ArgumentNullException(nameof(assembly)); - } - - if (string.IsNullOrEmpty(resource)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0062), nameof(resource)); - } + ArgumentNullException.ThrowIfNull(assembly); + ArgumentException.ThrowIfNullOrEmpty(resource); using var stream = assembly.GetManifestResourceStream(resource) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0064)); @@ -554,7 +491,7 @@ public sealed class OpenIddictValidationBuilder public OpenIddictValidationBuilder AddSigningCertificate(Stream stream, string? password) #if SUPPORTS_EPHEMERAL_KEY_SETS // Note: ephemeral key sets are currently not supported on macOS. - => AddSigningCertificate(stream, password, RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? + => AddSigningCertificate(stream, password, OperatingSystem.IsMacOS() ? X509KeyStorageFlags.MachineKeySet : X509KeyStorageFlags.EphemeralKeySet); #else @@ -570,10 +507,7 @@ public sealed class OpenIddictValidationBuilder /// The instance. public OpenIddictValidationBuilder AddSigningCertificate(Stream stream, string? password, X509KeyStorageFlags flags) { - if (stream is null) - { - throw new ArgumentNullException(nameof(stream)); - } + ArgumentNullException.ThrowIfNull(stream); using var buffer = new MemoryStream(); stream.CopyTo(buffer); @@ -598,10 +532,7 @@ public sealed class OpenIddictValidationBuilder /// The instance. public OpenIddictValidationBuilder AddSigningCertificate(string thumbprint) { - if (string.IsNullOrEmpty(thumbprint)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0065), nameof(thumbprint)); - } + ArgumentException.ThrowIfNullOrEmpty(thumbprint); return AddSigningCertificate( GetCertificate(StoreLocation.CurrentUser, thumbprint) ?? @@ -628,10 +559,7 @@ public sealed class OpenIddictValidationBuilder /// The instance. public OpenIddictValidationBuilder AddSigningCertificate(string thumbprint, StoreName name, StoreLocation location) { - if (string.IsNullOrEmpty(thumbprint)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0065), nameof(thumbprint)); - } + ArgumentException.ThrowIfNullOrEmpty(thumbprint); using var store = new X509Store(name, location); store.Open(OpenFlags.ReadOnly); @@ -649,11 +577,8 @@ public sealed class OpenIddictValidationBuilder /// The instance. public OpenIddictValidationBuilder AddSigningCertificates(IEnumerable certificates) { - if (certificates is null) - { - throw new ArgumentNullException(nameof(certificates)); - } - + ArgumentNullException.ThrowIfNull(certificates); + return certificates.Aggregate(this, static (builder, certificate) => builder.AddSigningCertificate(certificate)); } @@ -665,10 +590,7 @@ public sealed class OpenIddictValidationBuilder /// The instance. public OpenIddictValidationBuilder AddAudiences(params string[] audiences) { - if (audiences is null) - { - throw new ArgumentNullException(nameof(audiences)); - } + ArgumentNullException.ThrowIfNull(audiences); if (Array.Exists(audiences, string.IsNullOrEmpty)) { @@ -717,10 +639,7 @@ public sealed class OpenIddictValidationBuilder /// The instance. public OpenIddictValidationBuilder SetConfiguration(OpenIddictConfiguration configuration) { - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(configuration); return Configure(options => options.Configuration = configuration); } @@ -733,10 +652,7 @@ public sealed class OpenIddictValidationBuilder /// The instance. public OpenIddictValidationBuilder SetClaimsIssuer(string issuer) { - if (string.IsNullOrEmpty(issuer)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0124), nameof(issuer)); - } + ArgumentException.ThrowIfNullOrEmpty(issuer); return Configure(options => options.ClaimsIssuer = issuer); } @@ -749,10 +665,7 @@ public sealed class OpenIddictValidationBuilder /// The instance. public OpenIddictValidationBuilder SetClientId(string identifier) { - if (string.IsNullOrEmpty(identifier)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0124), nameof(identifier)); - } + ArgumentException.ThrowIfNullOrEmpty(identifier); return Configure(options => options.ClientId = identifier); } @@ -765,10 +678,7 @@ public sealed class OpenIddictValidationBuilder /// The instance. public OpenIddictValidationBuilder SetClientSecret(string secret) { - if (string.IsNullOrEmpty(secret)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0125), nameof(secret)); - } + ArgumentException.ThrowIfNullOrEmpty(secret); return Configure(options => options.ClientSecret = secret); } @@ -781,10 +691,7 @@ public sealed class OpenIddictValidationBuilder /// The instance. public OpenIddictValidationBuilder SetIssuer(Uri uri) { - if (uri is null) - { - throw new ArgumentNullException(nameof(uri)); - } + ArgumentNullException.ThrowIfNull(uri); return Configure(options => options.Issuer = uri); } @@ -797,10 +704,7 @@ public sealed class OpenIddictValidationBuilder /// The instance. public OpenIddictValidationBuilder SetIssuer([StringSyntax(StringSyntaxAttribute.Uri)] string uri) { - if (string.IsNullOrEmpty(uri)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0126), nameof(uri)); - } + ArgumentException.ThrowIfNullOrEmpty(uri); if (!Uri.TryCreate(uri, UriKind.Absolute, out Uri? value) || OpenIddictHelpers.IsImplicitFileUri(value)) { diff --git a/src/OpenIddict.Validation/OpenIddictValidationConfiguration.cs b/src/OpenIddict.Validation/OpenIddictValidationConfiguration.cs index 75eb6126..8e4c73fa 100644 --- a/src/OpenIddict.Validation/OpenIddictValidationConfiguration.cs +++ b/src/OpenIddict.Validation/OpenIddictValidationConfiguration.cs @@ -33,10 +33,7 @@ public sealed class OpenIddictValidationConfiguration : IPostConfigureOptions public void PostConfigure(string? name, OpenIddictValidationOptions options) { - if (options is null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(options); options.TimeProvider ??= _provider.GetService() ?? TimeProvider.System; diff --git a/src/OpenIddict.Validation/OpenIddictValidationDispatcher.cs b/src/OpenIddict.Validation/OpenIddictValidationDispatcher.cs index eddd83dc..61ecfcc4 100644 --- a/src/OpenIddict.Validation/OpenIddictValidationDispatcher.cs +++ b/src/OpenIddict.Validation/OpenIddictValidationDispatcher.cs @@ -36,10 +36,7 @@ public sealed class OpenIddictValidationDispatcher : IOpenIddictValidationDispat /// public async ValueTask DispatchAsync(TContext context) where TContext : BaseContext { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); await foreach (var handler in GetHandlersAsync()) { diff --git a/src/OpenIddict.Validation/OpenIddictValidationExtensions.cs b/src/OpenIddict.Validation/OpenIddictValidationExtensions.cs index a299fcec..0b1528d0 100644 --- a/src/OpenIddict.Validation/OpenIddictValidationExtensions.cs +++ b/src/OpenIddict.Validation/OpenIddictValidationExtensions.cs @@ -24,10 +24,7 @@ public static class OpenIddictValidationExtensions /// The instance. public static OpenIddictValidationBuilder AddValidation(this OpenIddictBuilder builder) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } + ArgumentNullException.ThrowIfNull(builder); builder.Services.AddLogging(); builder.Services.AddOptions(); @@ -73,15 +70,8 @@ public static class OpenIddictValidationExtensions this OpenIddictBuilder builder, Action configuration) { - if (builder is null) - { - throw new ArgumentNullException(nameof(builder)); - } - - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } + ArgumentNullException.ThrowIfNull(builder); + ArgumentNullException.ThrowIfNull(configuration); configuration(builder.AddValidation()); diff --git a/src/OpenIddict.Validation/OpenIddictValidationHandlerDescriptor.cs b/src/OpenIddict.Validation/OpenIddictValidationHandlerDescriptor.cs index 0e3a5b92..23718380 100644 --- a/src/OpenIddict.Validation/OpenIddictValidationHandlerDescriptor.cs +++ b/src/OpenIddict.Validation/OpenIddictValidationHandlerDescriptor.cs @@ -88,10 +88,7 @@ public sealed class OpenIddictValidationHandlerDescriptor /// The builder instance, so that calls can be easily chained. public Builder Import(OpenIddictValidationHandlerDescriptor descriptor) { - if (descriptor is null) - { - throw new ArgumentNullException(nameof(descriptor)); - } + ArgumentNullException.ThrowIfNull(descriptor); if (descriptor.ContextType != typeof(TContext)) { @@ -114,10 +111,7 @@ public sealed class OpenIddictValidationHandlerDescriptor /// The builder instance, so that calls can be easily chained. public Builder SetServiceDescriptor(ServiceDescriptor descriptor) { - if (descriptor is null) - { - throw new ArgumentNullException(nameof(descriptor)); - } + ArgumentNullException.ThrowIfNull(descriptor); if (!typeof(IOpenIddictValidationHandler).IsAssignableFrom(descriptor.ServiceType)) { diff --git a/src/OpenIddict.Validation/OpenIddictValidationHandlerFilters.cs b/src/OpenIddict.Validation/OpenIddictValidationHandlerFilters.cs index 57005ab7..ff47dcf0 100644 --- a/src/OpenIddict.Validation/OpenIddictValidationHandlerFilters.cs +++ b/src/OpenIddict.Validation/OpenIddictValidationHandlerFilters.cs @@ -19,10 +19,7 @@ public static class OpenIddictValidationHandlerFilters /// public ValueTask IsActiveAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.ExtractAccessToken); } @@ -36,10 +33,7 @@ public static class OpenIddictValidationHandlerFilters /// public ValueTask IsActiveAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.ValidateAccessToken); } @@ -53,10 +47,7 @@ public static class OpenIddictValidationHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.Options.EnableAuthorizationEntryValidation); } @@ -70,10 +61,7 @@ public static class OpenIddictValidationHandlerFilters /// public ValueTask IsActiveAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!string.IsNullOrEmpty(context.AuthorizationId)); } @@ -87,10 +75,7 @@ public static class OpenIddictValidationHandlerFilters /// public ValueTask IsActiveAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.GenerateClientAssertion); } @@ -104,10 +89,7 @@ public static class OpenIddictValidationHandlerFilters /// public ValueTask IsActiveAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.SendIntrospectionRequest); } @@ -121,10 +103,7 @@ public static class OpenIddictValidationHandlerFilters /// public ValueTask IsActiveAsync(GenerateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.TokenFormat is TokenFormats.Private.JsonWebToken); } @@ -138,10 +117,7 @@ public static class OpenIddictValidationHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.Options.ValidationType is OpenIddictValidationType.Direct); } @@ -155,10 +131,7 @@ public static class OpenIddictValidationHandlerFilters /// public ValueTask IsActiveAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!context.DisableAudienceValidation); } @@ -172,10 +145,7 @@ public static class OpenIddictValidationHandlerFilters /// public ValueTask IsActiveAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!string.IsNullOrEmpty(context.TokenId)); } @@ -189,10 +159,7 @@ public static class OpenIddictValidationHandlerFilters /// public ValueTask IsActiveAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!context.DisableLifetimeValidation); } @@ -206,10 +173,7 @@ public static class OpenIddictValidationHandlerFilters /// public ValueTask IsActiveAsync(BaseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(context.Options.EnableTokenEntryValidation); } @@ -223,10 +187,7 @@ public static class OpenIddictValidationHandlerFilters /// public ValueTask IsActiveAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); return new(!context.DisablePresenterValidation); } diff --git a/src/OpenIddict.Validation/OpenIddictValidationHandlers.Discovery.cs b/src/OpenIddict.Validation/OpenIddictValidationHandlers.Discovery.cs index d1167f2c..d8970ada 100644 --- a/src/OpenIddict.Validation/OpenIddictValidationHandlers.Discovery.cs +++ b/src/OpenIddict.Validation/OpenIddictValidationHandlers.Discovery.cs @@ -54,10 +54,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); foreach (var parameter in context.Response.GetParameters()) { @@ -128,10 +125,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: the specification doesn't define a standard way to return an error other than // returning a 4xx status code. That said, some implementations are known to return @@ -171,10 +165,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: the issuer returned in the discovery document must exactly match the URI used to access it. // See https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfigurationValidation. @@ -235,10 +226,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: the jwks_uri node is required by the OpenID Connect discovery specification. // See https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfigurationValidation. @@ -287,10 +275,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var endpoint = (string?) context.Response[Metadata.IntrospectionEndpoint]; if (!string.IsNullOrEmpty(endpoint)) @@ -331,10 +316,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: as recommended by the specification, values present in the "mtls_endpoint_aliases" node // that can't be recognized as OAuth 2.0 endpoints or are not valid URIs are simply ignored. @@ -367,10 +349,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(HandleConfigurationResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Resolve the client authentication methods supported by the introspection endpoint, if available. foreach (var method in (ImmutableArray?) context.Response[Metadata.IntrospectionEndpointAuthMethodsSupported] ?? []) @@ -403,10 +382,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(HandleJsonWebKeySetResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); foreach (var parameter in context.Response.GetParameters()) { @@ -465,10 +441,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(HandleJsonWebKeySetResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: the specification doesn't define a standard way to return an error other than // returning a 4xx status code. That said, some implementations are known to return @@ -508,10 +481,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(HandleJsonWebKeySetResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var keys = context.Response[JsonWebKeySetParameterNames.Keys]?.GetUnnamedParameters(); if (keys is not { Count: > 0 }) diff --git a/src/OpenIddict.Validation/OpenIddictValidationHandlers.Introspection.cs b/src/OpenIddict.Validation/OpenIddictValidationHandlers.Introspection.cs index 91bb715c..c7170f65 100644 --- a/src/OpenIddict.Validation/OpenIddictValidationHandlers.Introspection.cs +++ b/src/OpenIddict.Validation/OpenIddictValidationHandlers.Introspection.cs @@ -50,10 +50,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(HandleIntrospectionResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); foreach (var parameter in context.Response.GetParameters()) { @@ -127,10 +124,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(HandleIntrospectionResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: the specification requires returning most errors (e.g invalid token errors) // as "active: false" responses instead of as proper OAuth 2.0 error responses. @@ -173,10 +167,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(HandleIntrospectionResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: the introspection specification requires that server return "active: false" instead of a proper // OAuth 2.0 error when the token is invalid, expired, revoked or invalid for any other reason. @@ -226,10 +217,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(HandleIntrospectionResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // The issuer claim is optional. If it's not null or empty, validate it to // ensure it matches the issuer registered in the server configuration. @@ -280,10 +268,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(HandleIntrospectionResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Note: in most cases, an expired token should lead to an errored or "active=false" response // being returned by the authorization server. Unfortunately, some implementations are known not @@ -327,10 +312,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(HandleIntrospectionResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // OpenIddict-based authorization servers always return the actual token type using // the special "token_usage" claim, that helps resource servers determine whether the @@ -383,10 +365,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(HandleIntrospectionResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Create a new claims-based identity using the same authentication type // and the name/role claims as the one used by IdentityModel for JWT tokens. @@ -466,10 +445,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(HandleIntrospectionResponseContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Principal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); diff --git a/src/OpenIddict.Validation/OpenIddictValidationHandlers.Protection.cs b/src/OpenIddict.Validation/OpenIddictValidationHandlers.Protection.cs index ffa63723..77590d8c 100644 --- a/src/OpenIddict.Validation/OpenIddictValidationHandlers.Protection.cs +++ b/src/OpenIddict.Validation/OpenIddictValidationHandlers.Protection.cs @@ -63,10 +63,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Clone the token validation parameters and set the issuer using the value found in the // OpenID Connect server configuration (that can be static or retrieved using discovery). @@ -154,10 +151,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If no character was explicitly added, all characters are considered valid. if (context.AllowedCharset.Count is 0) @@ -211,10 +205,7 @@ public static partial class OpenIddictValidationHandlers /// public async ValueTask HandleAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If the provided token is a JWT token, avoid making a database lookup. if (context.SecurityTokenHandler.CanReadToken(context.Token)) @@ -278,10 +269,7 @@ public static partial class OpenIddictValidationHandlers /// public async ValueTask HandleAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If a principal was already attached, don't overwrite it. if (context.Principal is not null) @@ -395,10 +383,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.Principal is null) { @@ -440,10 +425,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.Principal is null) { @@ -552,10 +534,7 @@ public static partial class OpenIddictValidationHandlers /// public async ValueTask HandleAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.Principal is null) { @@ -612,10 +591,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.Principal is null) { @@ -665,10 +641,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Principal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -708,10 +681,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Principal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -771,10 +741,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Principal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -843,10 +810,7 @@ public static partial class OpenIddictValidationHandlers /// public async ValueTask HandleAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Principal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); Debug.Assert(!string.IsNullOrEmpty(context.TokenId), SR.GetResourceString(SR.ID4017)); @@ -896,10 +860,7 @@ public static partial class OpenIddictValidationHandlers /// public async ValueTask HandleAsync(ValidateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Principal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); Debug.Assert(!string.IsNullOrEmpty(context.AuthorizationId), SR.GetResourceString(SR.ID4018)); @@ -937,10 +898,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(GenerateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.SecurityTokenDescriptor.SigningCredentials = context.Options.SigningCredentials.First(); context.SecurityTokenHandler = context.Options.JsonWebTokenHandler; @@ -967,10 +925,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(GenerateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.Principal is not { Identity: ClaimsIdentity } principal) { @@ -1014,10 +969,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(GenerateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var claims = context.SecurityTokenDescriptor.Claims is not null ? new Dictionary(context.SecurityTokenDescriptor.Claims, StringComparer.Ordinal) : @@ -1078,10 +1030,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(GenerateTokenContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If a token was already attached by another handler, don't overwrite it. if (!string.IsNullOrEmpty(context.Token)) diff --git a/src/OpenIddict.Validation/OpenIddictValidationHandlers.cs b/src/OpenIddict.Validation/OpenIddictValidationHandlers.cs index 5e3b7140..346f26e6 100644 --- a/src/OpenIddict.Validation/OpenIddictValidationHandlers.cs +++ b/src/OpenIddict.Validation/OpenIddictValidationHandlers.cs @@ -73,10 +73,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); (context.ExtractAccessToken, context.RequireAccessToken, @@ -123,10 +120,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.RequireAccessToken && string.IsNullOrEmpty(context.AccessToken)) { @@ -160,10 +154,7 @@ public static partial class OpenIddictValidationHandlers /// public async ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); try { @@ -207,10 +198,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.SendIntrospectionRequest = context.Options.ValidationType is OpenIddictValidationType.Introspection; @@ -238,10 +226,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If an explicit client authentication method was attached, don't overwrite it. if (!string.IsNullOrEmpty(context.IntrospectionEndpointClientAuthenticationMethod)) @@ -296,10 +281,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // If the URI of the introspection endpoint endpoint wasn't explicitly // set at this stage, try to extract it from the server configuration. @@ -341,10 +323,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Attach a new request instance if necessary. context.IntrospectionRequest ??= new OpenIddictRequest(); @@ -376,10 +355,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); (context.GenerateClientAssertion, context.IncludeClientAssertion) = context.IntrospectionEndpointClientAuthenticationMethod switch @@ -415,10 +391,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.Configuration.Issuer is { IsAbsoluteUri: true }, SR.GetResourceString(SR.ID4013)); @@ -482,10 +455,7 @@ public static partial class OpenIddictValidationHandlers /// public async ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); var notification = new GenerateTokenContext(context.Transaction) { @@ -550,10 +520,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.IntrospectionRequest is not null, SR.GetResourceString(SR.ID4008)); @@ -604,10 +571,7 @@ public static partial class OpenIddictValidationHandlers /// public async ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.IntrospectionRequest is not null, SR.GetResourceString(SR.ID4008)); @@ -661,10 +625,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.AccessTokenPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -711,10 +672,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); Debug.Assert(context.AccessTokenPrincipal is { Identity: ClaimsIdentity }, SR.GetResourceString(SR.ID4006)); @@ -785,10 +743,7 @@ public static partial class OpenIddictValidationHandlers /// public async ValueTask HandleAsync(ProcessAuthenticationContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (string.IsNullOrEmpty(context.AccessToken)) { @@ -855,10 +810,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); // Try to retrieve the authentication context from the validation transaction and use // the error details returned during the authentication processing, if available. @@ -897,10 +849,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(ProcessChallengeContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.Parameters.Count is > 0) { @@ -932,10 +881,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(ProcessErrorContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); context.Response.Error = context.Error; context.Response.ErrorDescription = context.ErrorDescription; @@ -964,10 +910,7 @@ public static partial class OpenIddictValidationHandlers /// public ValueTask HandleAsync(ProcessErrorContext context) { - if (context is null) - { - throw new ArgumentNullException(nameof(context)); - } + ArgumentNullException.ThrowIfNull(context); if (context.Parameters.Count is > 0) { diff --git a/src/OpenIddict.Validation/OpenIddictValidationHelpers.cs b/src/OpenIddict.Validation/OpenIddictValidationHelpers.cs index 6d869e9d..02e832f8 100644 --- a/src/OpenIddict.Validation/OpenIddictValidationHelpers.cs +++ b/src/OpenIddict.Validation/OpenIddictValidationHelpers.cs @@ -21,15 +21,8 @@ public static class OpenIddictValidationHelpers public static TProperty? GetProperty( this OpenIddictValidationTransaction transaction, string name) where TProperty : class { - if (transaction is null) - { - throw new ArgumentNullException(nameof(transaction)); - } - - if (string.IsNullOrEmpty(name)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0106), nameof(name)); - } + ArgumentNullException.ThrowIfNull(transaction); + ArgumentException.ThrowIfNullOrEmpty(name); if (transaction.Properties.TryGetValue(name, out var property) && property is TProperty result) { @@ -51,15 +44,8 @@ public static class OpenIddictValidationHelpers this OpenIddictValidationTransaction transaction, string name, TProperty? value) where TProperty : class { - if (transaction is null) - { - throw new ArgumentNullException(nameof(transaction)); - } - - if (string.IsNullOrEmpty(name)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0106), nameof(name)); - } + ArgumentNullException.ThrowIfNull(transaction); + ArgumentException.ThrowIfNullOrEmpty(name); if (value is null) { diff --git a/src/OpenIddict.Validation/OpenIddictValidationRetriever.cs b/src/OpenIddict.Validation/OpenIddictValidationRetriever.cs index 7fc0d13a..848a73ad 100644 --- a/src/OpenIddict.Validation/OpenIddictValidationRetriever.cs +++ b/src/OpenIddict.Validation/OpenIddictValidationRetriever.cs @@ -32,10 +32,7 @@ public sealed class OpenIddictValidationRetriever : IConfigurationRetriever IConfigurationRetriever.GetConfigurationAsync( [StringSyntax(StringSyntaxAttribute.Uri)] string address, IDocumentRetriever retriever, CancellationToken cancel) { - if (string.IsNullOrEmpty(address)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0143), nameof(address)); - } + ArgumentException.ThrowIfNullOrEmpty(address); if (!Uri.TryCreate(address, UriKind.Absolute, out Uri? uri) || OpenIddictHelpers.IsImplicitFileUri(uri)) { diff --git a/src/OpenIddict.Validation/OpenIddictValidationService.cs b/src/OpenIddict.Validation/OpenIddictValidationService.cs index e35ba501..ed65d0ff 100644 --- a/src/OpenIddict.Validation/OpenIddictValidationService.cs +++ b/src/OpenIddict.Validation/OpenIddictValidationService.cs @@ -35,10 +35,7 @@ public class OpenIddictValidationService /// The principal containing the claims extracted from the token. public async ValueTask ValidateAccessTokenAsync(string token, CancellationToken cancellationToken = default) { - if (string.IsNullOrEmpty(token)) - { - throw new ArgumentException(SR.GetResourceString(SR.ID0162), nameof(token)); - } + ArgumentException.ThrowIfNullOrEmpty(token); cancellationToken.ThrowIfCancellationRequested(); @@ -78,10 +75,7 @@ public class OpenIddictValidationService /// The OpenID Connect server configuration retrieved from the remote server. internal async ValueTask GetConfigurationAsync(Uri uri, CancellationToken cancellationToken = default) { - if (uri is null) - { - throw new ArgumentNullException(nameof(uri)); - } + ArgumentNullException.ThrowIfNull(uri); if (!uri.IsAbsoluteUri) { @@ -203,10 +197,7 @@ public class OpenIddictValidationService /// The security keys retrieved from the remote server. internal async ValueTask GetSecurityKeysAsync(Uri uri, CancellationToken cancellationToken = default) { - if (uri is null) - { - throw new ArgumentNullException(nameof(uri)); - } + ArgumentNullException.ThrowIfNull(uri); if (!uri.IsAbsoluteUri) { @@ -333,20 +324,9 @@ public class OpenIddictValidationService OpenIddictConfiguration configuration, OpenIddictRequest request, Uri uri, string? method, CancellationToken cancellationToken = default) { - if (configuration is null) - { - throw new ArgumentNullException(nameof(configuration)); - } - - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } - - if (uri is null) - { - throw new ArgumentNullException(nameof(uri)); - } + ArgumentNullException.ThrowIfNull(configuration); + ArgumentNullException.ThrowIfNull(request); + ArgumentNullException.ThrowIfNull(uri); if (!uri.IsAbsoluteUri || OpenIddictHelpers.IsImplicitFileUri(uri)) { diff --git a/src/OpenIddict/OpenIddict.csproj b/src/OpenIddict/OpenIddict.csproj index b88f3bda..6f41ae5d 100644 --- a/src/OpenIddict/OpenIddict.csproj +++ b/src/OpenIddict/OpenIddict.csproj @@ -24,6 +24,7 @@ Condition=" $([MSBuild]::GetTargetFrameworkIdentifier('$(TargetFramework)')) == '.NETCoreApp' ">.NETCoreApp false false + false false diff --git a/test/OpenIddict.Abstractions.Tests/Primitives/OpenIddictExtensionsTests.cs b/test/OpenIddict.Abstractions.Tests/Primitives/OpenIddictExtensionsTests.cs index 90687d47..d6b5785a 100644 --- a/test/OpenIddict.Abstractions.Tests/Primitives/OpenIddictExtensionsTests.cs +++ b/test/OpenIddict.Abstractions.Tests/Primitives/OpenIddictExtensionsTests.cs @@ -227,10 +227,9 @@ public class OpenIddictExtensionsTests var request = new OpenIddictRequest(); // Act and assert - var exception = Assert.Throws(() => request.HasAcrValue(value!)); + var exception = Assert.ThrowsAny(() => request.HasAcrValue(value!)); Assert.Equal("value", exception.ParamName); - Assert.StartsWith(SR.FormatID0366("value"), exception.Message); } [Theory] @@ -291,10 +290,9 @@ public class OpenIddictExtensionsTests var request = new OpenIddictRequest(); // Act and assert - var exception = Assert.Throws(() => request.HasAudience(resource!)); + var exception = Assert.ThrowsAny(() => request.HasAudience(resource!)); Assert.Equal("audience", exception.ParamName); - Assert.StartsWith(SR.FormatID0366("audience"), exception.Message); } [Fact] @@ -336,10 +334,9 @@ public class OpenIddictExtensionsTests var request = new OpenIddictRequest(); // Act and assert - var exception = Assert.Throws(() => request.HasPromptValue(value!)); + var exception = Assert.ThrowsAny(() => request.HasPromptValue(value!)); Assert.Equal("value", exception.ParamName); - Assert.StartsWith(SR.FormatID0366("value"), exception.Message); } [Theory] @@ -400,10 +397,9 @@ public class OpenIddictExtensionsTests var request = new OpenIddictRequest(); // Act and assert - var exception = Assert.Throws(() => request.HasResponseType(type!)); + var exception = Assert.ThrowsAny(() => request.HasResponseType(type!)); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.FormatID0366("type"), exception.Message); } [Theory] @@ -464,10 +460,9 @@ public class OpenIddictExtensionsTests var request = new OpenIddictRequest(); // Act and assert - var exception = Assert.Throws(() => request.HasResource(resource!)); + var exception = Assert.ThrowsAny(() => request.HasResource(resource!)); Assert.Equal("resource", exception.ParamName); - Assert.StartsWith(SR.FormatID0366("resource"), exception.Message); } [Fact] @@ -509,10 +504,9 @@ public class OpenIddictExtensionsTests var request = new OpenIddictRequest(); // Act and assert - var exception = Assert.Throws(() => request.HasScope(scope!)); + var exception = Assert.ThrowsAny(() => request.HasScope(scope!)); Assert.Equal("scope", exception.ParamName); - Assert.StartsWith(SR.FormatID0366("scope"), exception.Message); } [Theory] @@ -1119,10 +1113,9 @@ public class OpenIddictExtensionsTests var claim = new Claim(Claims.Name, "Bob le Bricoleur"); // Act and assert - var exception = Assert.Throws(() => claim.HasDestination(null!)); + var exception = Assert.ThrowsAny(() => claim.HasDestination(null!)); Assert.Equal("destination", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0181), exception.Message); } [Fact] @@ -1765,10 +1758,9 @@ public class OpenIddictExtensionsTests var identity = new ClaimsIdentity(); // Act and assert - var exception = Assert.Throws(() => identity.AddClaim(type!, "value")); + var exception = Assert.ThrowsAny(() => identity.AddClaim(type!, "value")); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Theory] @@ -1780,10 +1772,9 @@ public class OpenIddictExtensionsTests var principal = new ClaimsPrincipal(new ClaimsIdentity()); // Act and assert - var exception = Assert.Throws(() => principal.AddClaim(type!, "value")); + var exception = Assert.ThrowsAny(() => principal.AddClaim(type!, "value")); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Fact] @@ -1858,10 +1849,9 @@ public class OpenIddictExtensionsTests var identity = new ClaimsIdentity(); // Act and assert - var exception = Assert.Throws(() => identity.AddClaim(type!, true)); + var exception = Assert.ThrowsAny(() => identity.AddClaim(type!, true)); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Theory] @@ -1873,10 +1863,9 @@ public class OpenIddictExtensionsTests var principal = new ClaimsPrincipal(new ClaimsIdentity()); // Act and assert - var exception = Assert.Throws(() => principal.AddClaim(type!, true)); + var exception = Assert.ThrowsAny(() => principal.AddClaim(type!, true)); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Fact] @@ -1951,10 +1940,9 @@ public class OpenIddictExtensionsTests var identity = new ClaimsIdentity(); // Act and assert - var exception = Assert.Throws(() => identity.AddClaim(type!, 42L)); + var exception = Assert.ThrowsAny(() => identity.AddClaim(type!, 42L)); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Theory] @@ -1966,10 +1954,9 @@ public class OpenIddictExtensionsTests var principal = new ClaimsPrincipal(new ClaimsIdentity()); // Act and assert - var exception = Assert.Throws(() => principal.AddClaim(type!, 42L)); + var exception = Assert.ThrowsAny(() => principal.AddClaim(type!, 42L)); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Fact] @@ -2044,10 +2031,9 @@ public class OpenIddictExtensionsTests var identity = new ClaimsIdentity(); // Act and assert - var exception = Assert.Throws(() => identity.AddClaim(type!, new Dictionary())); + var exception = Assert.ThrowsAny(() => identity.AddClaim(type!, new Dictionary())); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Theory] @@ -2059,10 +2045,9 @@ public class OpenIddictExtensionsTests var principal = new ClaimsPrincipal(new ClaimsIdentity()); // Act and assert - var exception = Assert.Throws(() => principal.AddClaim(type!, new Dictionary())); + var exception = Assert.ThrowsAny(() => principal.AddClaim(type!, new Dictionary())); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Fact] @@ -2143,10 +2128,9 @@ public class OpenIddictExtensionsTests var identity = new ClaimsIdentity(); // Act and assert - var exception = Assert.Throws(() => identity.AddClaim(type!, default(JsonElement))); + var exception = Assert.ThrowsAny(() => identity.AddClaim(type!, default(JsonElement))); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Theory] @@ -2158,10 +2142,9 @@ public class OpenIddictExtensionsTests var principal = new ClaimsPrincipal(new ClaimsIdentity()); // Act and assert - var exception = Assert.Throws(() => principal.AddClaim(type!, default(JsonElement))); + var exception = Assert.ThrowsAny(() => principal.AddClaim(type!, default(JsonElement))); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Fact] @@ -2264,10 +2247,9 @@ public class OpenIddictExtensionsTests var identity = new ClaimsIdentity(); // Act and assert - var exception = Assert.Throws(() => identity.AddClaim(type!, (JsonNode) null!)); + var exception = Assert.ThrowsAny(() => identity.AddClaim(type!, (JsonNode) null!)); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Theory] @@ -2279,10 +2261,9 @@ public class OpenIddictExtensionsTests var principal = new ClaimsPrincipal(new ClaimsIdentity()); // Act and assert - var exception = Assert.Throws(() => principal.AddClaim(type!, (JsonNode) null!)); + var exception = Assert.ThrowsAny(() => principal.AddClaim(type!, (JsonNode) null!)); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Fact] @@ -2409,10 +2390,9 @@ public class OpenIddictExtensionsTests var identity = new ClaimsIdentity(); // Act and assert - var exception = Assert.Throws(() => identity.AddClaims(type!, [])); + var exception = Assert.ThrowsAny(() => identity.AddClaims(type!, [])); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Theory] @@ -2424,10 +2404,9 @@ public class OpenIddictExtensionsTests var principal = new ClaimsPrincipal(new ClaimsIdentity()); // Act and assert - var exception = Assert.Throws(() => principal.AddClaims(type!, [])); + var exception = Assert.ThrowsAny(() => principal.AddClaims(type!, [])); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Fact] @@ -2542,10 +2521,9 @@ public class OpenIddictExtensionsTests var identity = new ClaimsIdentity(); // Act and assert - var exception = Assert.Throws(() => identity.AddClaims(type!, default(JsonElement))); + var exception = Assert.ThrowsAny(() => identity.AddClaims(type!, default(JsonElement))); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Theory] @@ -2557,10 +2535,9 @@ public class OpenIddictExtensionsTests var principal = new ClaimsPrincipal(new ClaimsIdentity()); // Act and assert - var exception = Assert.Throws(() => principal.AddClaims(type!, default(JsonElement))); + var exception = Assert.ThrowsAny(() => principal.AddClaims(type!, default(JsonElement))); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Fact] @@ -2714,10 +2691,9 @@ public class OpenIddictExtensionsTests var identity = new ClaimsIdentity(); // Act and assert - var exception = Assert.Throws(() => identity.AddClaims(type!, (JsonArray) null!)); + var exception = Assert.ThrowsAny(() => identity.AddClaims(type!, new JsonArray())); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Theory] @@ -2729,10 +2705,9 @@ public class OpenIddictExtensionsTests var principal = new ClaimsPrincipal(new ClaimsIdentity()); // Act and assert - var exception = Assert.Throws(() => principal.AddClaims(type!, (JsonArray) null!)); + var exception = Assert.ThrowsAny(() => principal.AddClaims(type!, new JsonArray())); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Fact] @@ -2997,10 +2972,9 @@ public class OpenIddictExtensionsTests var identity = new ClaimsIdentity(); // Act and assert - var exception = Assert.Throws(() => identity.GetClaims(type!)); + var exception = Assert.ThrowsAny(() => identity.GetClaims(type!)); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Theory] @@ -3012,10 +2986,9 @@ public class OpenIddictExtensionsTests var principal = new ClaimsPrincipal(new ClaimsIdentity()); // Act and assert - var exception = Assert.Throws(() => principal.GetClaims(type!)); + var exception = Assert.ThrowsAny(() => principal.GetClaims(type!)); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Fact] @@ -3117,10 +3090,9 @@ public class OpenIddictExtensionsTests var identity = new ClaimsIdentity(); // Act and assert - var exception = Assert.Throws(() => identity.HasClaim(type!)); + var exception = Assert.ThrowsAny(() => identity.HasClaim(type!)); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Theory] @@ -3132,10 +3104,9 @@ public class OpenIddictExtensionsTests var principal = new ClaimsPrincipal(new ClaimsIdentity()); // Act and assert - var exception = Assert.Throws(() => principal.HasClaim(type!)); + var exception = Assert.ThrowsAny(() => principal.HasClaim(type!)); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Fact] @@ -3203,10 +3174,9 @@ public class OpenIddictExtensionsTests var identity = new ClaimsIdentity(); // Act and assert - var exception = Assert.Throws(() => identity.RemoveClaims(type!)); + var exception = Assert.ThrowsAny(() => identity.RemoveClaims(type!)); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Theory] @@ -3218,10 +3188,9 @@ public class OpenIddictExtensionsTests var principal = new ClaimsPrincipal(new ClaimsIdentity()); // Act and assert - var exception = Assert.Throws(() => principal.RemoveClaims(type!)); + var exception = Assert.ThrowsAny(() => principal.RemoveClaims(type!)); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Fact] @@ -3298,10 +3267,9 @@ public class OpenIddictExtensionsTests var identity = new ClaimsIdentity(); // Act and assert - var exception = Assert.Throws(() => identity.SetClaim(type!, "value")); + var exception = Assert.ThrowsAny(() => identity.SetClaim(type!, "value")); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Theory] @@ -3313,10 +3281,9 @@ public class OpenIddictExtensionsTests var principal = new ClaimsPrincipal(new ClaimsIdentity()); // Act and assert - var exception = Assert.Throws(() => principal.SetClaim(type!, "value")); + var exception = Assert.ThrowsAny(() => principal.SetClaim(type!, "value")); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Fact] @@ -3453,10 +3420,9 @@ public class OpenIddictExtensionsTests var identity = new ClaimsIdentity(); // Act and assert - var exception = Assert.Throws(() => identity.SetClaim(type!, true)); + var exception = Assert.ThrowsAny(() => identity.SetClaim(type!, true)); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Theory] @@ -3468,10 +3434,9 @@ public class OpenIddictExtensionsTests var principal = new ClaimsPrincipal(new ClaimsIdentity()); // Act and assert - var exception = Assert.Throws(() => principal.SetClaim(type!, "value")); + var exception = Assert.ThrowsAny(() => principal.SetClaim(type!, "value")); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Fact] @@ -3608,10 +3573,9 @@ public class OpenIddictExtensionsTests var identity = new ClaimsIdentity(); // Act and assert - var exception = Assert.Throws(() => identity.SetClaim(type!, 42L)); + var exception = Assert.ThrowsAny(() => identity.SetClaim(type!, 42L)); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Theory] @@ -3623,10 +3587,9 @@ public class OpenIddictExtensionsTests var principal = new ClaimsPrincipal(new ClaimsIdentity()); // Act and assert - var exception = Assert.Throws(() => principal.SetClaim(type!, 42L)); + var exception = Assert.ThrowsAny(() => principal.SetClaim(type!, 42L)); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Fact] @@ -3766,10 +3729,9 @@ public class OpenIddictExtensionsTests var identity = new ClaimsIdentity(); // Act and assert - var exception = Assert.Throws(() => identity.SetClaim(type!, new Dictionary())); + var exception = Assert.ThrowsAny(() => identity.SetClaim(type!, new Dictionary())); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Theory] @@ -3781,10 +3743,9 @@ public class OpenIddictExtensionsTests var principal = new ClaimsPrincipal(new ClaimsIdentity()); // Act and assert - var exception = Assert.Throws(() => principal.SetClaim(type!, new Dictionary())); + var exception = Assert.ThrowsAny(() => principal.SetClaim(type!, new Dictionary())); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Fact] @@ -3934,10 +3895,9 @@ public class OpenIddictExtensionsTests var identity = new ClaimsIdentity(); // Act and assert - var exception = Assert.Throws(() => identity.SetClaim(type!, default(JsonElement))); + var exception = Assert.ThrowsAny(() => identity.SetClaim(type!, default(JsonElement))); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Theory] @@ -3949,10 +3909,9 @@ public class OpenIddictExtensionsTests var principal = new ClaimsPrincipal(new ClaimsIdentity()); // Act and assert - var exception = Assert.Throws(() => principal.SetClaim(type!, default(JsonElement))); + var exception = Assert.ThrowsAny(() => principal.SetClaim(type!, default(JsonElement))); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Fact] @@ -4144,10 +4103,9 @@ public class OpenIddictExtensionsTests var identity = new ClaimsIdentity(); // Act and assert - var exception = Assert.Throws(() => identity.SetClaim(type!, (JsonNode) null!)); + var exception = Assert.ThrowsAny(() => identity.SetClaim(type!, (JsonNode) null!)); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Theory] @@ -4159,10 +4117,9 @@ public class OpenIddictExtensionsTests var principal = new ClaimsPrincipal(new ClaimsIdentity()); // Act and assert - var exception = Assert.Throws(() => principal.SetClaim(type!, (JsonNode) null!)); + var exception = Assert.ThrowsAny(() => principal.SetClaim(type!, (JsonNode) null!)); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Fact] @@ -4353,10 +4310,9 @@ public class OpenIddictExtensionsTests var identity = new ClaimsIdentity(); // Act and assert - var exception = Assert.Throws(() => identity.SetClaims(type!, [])); + var exception = Assert.ThrowsAny(() => identity.SetClaims(type!, [])); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Theory] @@ -4368,10 +4324,9 @@ public class OpenIddictExtensionsTests var principal = new ClaimsPrincipal(new ClaimsIdentity()); // Act and assert - var exception = Assert.Throws(() => principal.SetClaims(type!, [])); + var exception = Assert.ThrowsAny(() => principal.SetClaims(type!, [])); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Fact] @@ -4514,10 +4469,9 @@ public class OpenIddictExtensionsTests var identity = new ClaimsIdentity(); // Act and assert - var exception = Assert.Throws(() => identity.SetClaims(type!, default(JsonElement))); + var exception = Assert.ThrowsAny(() => identity.SetClaims(type!, default(JsonElement))); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Theory] @@ -4529,10 +4483,9 @@ public class OpenIddictExtensionsTests var principal = new ClaimsPrincipal(new ClaimsIdentity()); // Act and assert - var exception = Assert.Throws(() => principal.SetClaims(type!, default(JsonElement))); + var exception = Assert.ThrowsAny(() => principal.SetClaims(type!, default(JsonElement))); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Fact] @@ -4729,10 +4682,9 @@ public class OpenIddictExtensionsTests var identity = new ClaimsIdentity(); // Act and assert - var exception = Assert.Throws(() => identity.SetClaims(type!, (JsonArray) null!)); + var exception = Assert.ThrowsAny(() => identity.SetClaims(type!, new JsonArray())); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Theory] @@ -4744,10 +4696,9 @@ public class OpenIddictExtensionsTests var principal = new ClaimsPrincipal(new ClaimsIdentity()); // Act and assert - var exception = Assert.Throws(() => principal.SetClaims(type!, (JsonArray) null!)); + var exception = Assert.ThrowsAny(() => principal.SetClaims(type!, new JsonArray())); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0184), exception.Message); } [Fact] @@ -5862,10 +5813,9 @@ public class OpenIddictExtensionsTests var identity = new ClaimsIdentity(); // Act and assert - var exception = Assert.Throws(() => identity.HasAudience(audience!)); + var exception = Assert.ThrowsAny(() => identity.HasAudience(audience!)); Assert.Equal("audience", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0186), exception.Message); } [Theory] @@ -5877,10 +5827,9 @@ public class OpenIddictExtensionsTests var principal = new ClaimsPrincipal(new ClaimsIdentity()); // Act and assert - var exception = Assert.Throws(() => principal.HasAudience(audience!)); + var exception = Assert.ThrowsAny(() => principal.HasAudience(audience!)); Assert.Equal("audience", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0186), exception.Message); } [Theory] @@ -5956,10 +5905,9 @@ public class OpenIddictExtensionsTests var identity = new ClaimsIdentity(); // Act and assert - var exception = Assert.Throws(() => identity.HasPresenter(presenter!)); + var exception = Assert.ThrowsAny(() => identity.HasPresenter(presenter!)); Assert.Equal("presenter", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0187), exception.Message); } [Theory] @@ -5971,10 +5919,9 @@ public class OpenIddictExtensionsTests var principal = new ClaimsPrincipal(new ClaimsIdentity()); // Act and assert - var exception = Assert.Throws(() => principal.HasPresenter(presenter!)); + var exception = Assert.ThrowsAny(() => principal.HasPresenter(presenter!)); Assert.Equal("presenter", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0187), exception.Message); } [Theory] @@ -6050,10 +5997,9 @@ public class OpenIddictExtensionsTests var identity = new ClaimsIdentity(); // Act and assert - var exception = Assert.Throws(() => identity.HasResource(resource!)); + var exception = Assert.ThrowsAny(() => identity.HasResource(resource!)); Assert.Equal("resource", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0062), exception.Message); } [Theory] @@ -6065,10 +6011,9 @@ public class OpenIddictExtensionsTests var principal = new ClaimsPrincipal(new ClaimsIdentity()); // Act and assert - var exception = Assert.Throws(() => principal.HasResource(resource!)); + var exception = Assert.ThrowsAny(() => principal.HasResource(resource!)); Assert.Equal("resource", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0062), exception.Message); } [Theory] @@ -6144,10 +6089,9 @@ public class OpenIddictExtensionsTests var identity = new ClaimsIdentity(); // Act and assert - var exception = Assert.Throws(() => identity.HasScope(scope!)); + var exception = Assert.ThrowsAny(() => identity.HasScope(scope!)); Assert.Equal("scope", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0180), exception.Message); } [Theory] @@ -6159,10 +6103,9 @@ public class OpenIddictExtensionsTests var principal = new ClaimsPrincipal(new ClaimsIdentity()); // Act and assert - var exception = Assert.Throws(() => principal.HasScope(scope!)); + var exception = Assert.ThrowsAny(() => principal.HasScope(scope!)); Assert.Equal("scope", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0180), exception.Message); } [Theory] @@ -6238,10 +6181,9 @@ public class OpenIddictExtensionsTests var identity = new ClaimsIdentity(); // Act and assert - var exception = Assert.Throws(() => identity.HasTokenType(type!)); + var exception = Assert.ThrowsAny(() => identity.HasTokenType(type!)); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0188), exception.Message); } [Theory] @@ -6253,10 +6195,9 @@ public class OpenIddictExtensionsTests var principal = new ClaimsPrincipal(new ClaimsIdentity()); // Act and assert - var exception = Assert.Throws(() => principal.HasTokenType(type!)); + var exception = Assert.ThrowsAny(() => principal.HasTokenType(type!)); Assert.Equal("type", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0188), exception.Message); } [Fact] diff --git a/test/OpenIddict.Abstractions.Tests/Primitives/OpenIddictMessageTests.cs b/test/OpenIddict.Abstractions.Tests/Primitives/OpenIddictMessageTests.cs index 40571a16..d7eb1c5d 100644 --- a/test/OpenIddict.Abstractions.Tests/Primitives/OpenIddictMessageTests.cs +++ b/test/OpenIddict.Abstractions.Tests/Primitives/OpenIddictMessageTests.cs @@ -141,13 +141,12 @@ public class OpenIddictMessageTests var message = new OpenIddictMessage(); // Act and assert - var exception = Assert.Throws(() => + var exception = Assert.ThrowsAny(() => { message.AddParameter(name!, new OpenIddictParameter()); }); Assert.Equal("name", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0190), exception.Message); } [Fact] @@ -211,10 +210,9 @@ public class OpenIddictMessageTests var message = new OpenIddictMessage(); // Act and assert - var exception = Assert.Throws(() => message.GetParameter(name!)); + var exception = Assert.ThrowsAny(() => message.GetParameter(name!)); Assert.Equal("name", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0190), exception.Message); } [Fact] @@ -277,10 +275,9 @@ public class OpenIddictMessageTests var message = new OpenIddictMessage(); // Act and assert - var exception = Assert.Throws(() => message.HasParameter(name!)); + var exception = Assert.ThrowsAny(() => message.HasParameter(name!)); Assert.Equal("name", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0190), exception.Message); } [Theory] @@ -307,10 +304,9 @@ public class OpenIddictMessageTests var message = new OpenIddictMessage(); // Act and assert - var exception = Assert.Throws(() => message.RemoveParameter(name!)); + var exception = Assert.ThrowsAny(() => message.RemoveParameter(name!)); Assert.Equal("name", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0190), exception.Message); } [Fact] @@ -336,10 +332,9 @@ public class OpenIddictMessageTests var message = new OpenIddictMessage(); // Act and assert - var exception = Assert.Throws(() => message.SetParameter(name!, null)); + var exception = Assert.ThrowsAny(() => message.SetParameter(name!, null)); Assert.Equal("name", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0190), exception.Message); } [Fact] @@ -410,11 +405,10 @@ public class OpenIddictMessageTests var message = new OpenIddictMessage(); // Act - var exception = Assert.Throws(() => message.TryGetParameter(name!, out var parameter)); + var exception = Assert.ThrowsAny(() => message.TryGetParameter(name!, out var parameter)); // Assert Assert.Equal("name", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0190), exception.Message); } [Fact] diff --git a/test/OpenIddict.Abstractions.Tests/Primitives/OpenIddictParameterTests.cs b/test/OpenIddict.Abstractions.Tests/Primitives/OpenIddictParameterTests.cs index 1ff32961..b45932e4 100644 --- a/test/OpenIddict.Abstractions.Tests/Primitives/OpenIddictParameterTests.cs +++ b/test/OpenIddict.Abstractions.Tests/Primitives/OpenIddictParameterTests.cs @@ -581,11 +581,10 @@ public class OpenIddictParameterTests var parameter = new OpenIddictParameter(); // Act - var exception = Assert.Throws(() => parameter.GetNamedParameter(name!)); + var exception = Assert.ThrowsAny(() => parameter.GetNamedParameter(name!)); // Assert Assert.Equal("name", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0192), exception.Message); } [Fact] @@ -678,7 +677,6 @@ public class OpenIddictParameterTests // Assert Assert.Equal("index", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0193), exception.Message); } [Fact] @@ -1239,11 +1237,10 @@ public class OpenIddictParameterTests var parameter = new OpenIddictParameter(); // Act - var exception = Assert.Throws(() => parameter.TryGetNamedParameter(name!, out _)); + var exception = Assert.ThrowsAny(() => parameter.TryGetNamedParameter(name!, out _)); // Assert Assert.Equal("name", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0192), exception.Message); } [Fact] @@ -1343,7 +1340,6 @@ public class OpenIddictParameterTests // Assert Assert.Equal("index", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0193), exception.Message); } [Fact] diff --git a/test/OpenIddict.Core.Tests/OpenIddictCoreBuilderTests.cs b/test/OpenIddict.Core.Tests/OpenIddictCoreBuilderTests.cs index 6789dc4f..cfb4a8c6 100644 --- a/test/OpenIddict.Core.Tests/OpenIddictCoreBuilderTests.cs +++ b/test/OpenIddict.Core.Tests/OpenIddictCoreBuilderTests.cs @@ -318,10 +318,9 @@ public class OpenIddictCoreBuilderTests var builder = CreateBuilder(services); // Act and assert - var exception = Assert.Throws(() => builder.SetEntityCacheLimit(limit)); + var exception = Assert.Throws(() => builder.SetEntityCacheLimit(limit)); Assert.Equal("limit", exception.ParamName); - Assert.StartsWith("The cache size cannot be less than 10.", exception.Message); } [Fact] diff --git a/test/OpenIddict.MongoDb.Tests/OpenIddictMongoDbBuilderTests.cs b/test/OpenIddict.MongoDb.Tests/OpenIddictMongoDbBuilderTests.cs index ba8e64b5..66a9b8f6 100644 --- a/test/OpenIddict.MongoDb.Tests/OpenIddictMongoDbBuilderTests.cs +++ b/test/OpenIddict.MongoDb.Tests/OpenIddictMongoDbBuilderTests.cs @@ -105,10 +105,9 @@ public class OpenIddictMongoDbBuilderTests var builder = CreateBuilder(services); // Act and assert - var exception = Assert.Throws(() => builder.SetApplicationsCollectionName(name!)); + var exception = Assert.ThrowsAny(() => builder.SetApplicationsCollectionName(name!)); Assert.Equal("name", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0261), exception.Message); } [Fact] @@ -138,10 +137,9 @@ public class OpenIddictMongoDbBuilderTests var builder = CreateBuilder(services); // Act and assert - var exception = Assert.Throws(() => builder.SetAuthorizationsCollectionName(name!)); + var exception = Assert.ThrowsAny(() => builder.SetAuthorizationsCollectionName(name!)); Assert.Equal("name", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0261), exception.Message); } [Fact] @@ -171,10 +169,9 @@ public class OpenIddictMongoDbBuilderTests var builder = CreateBuilder(services); // Act and assert - var exception = Assert.Throws(() => builder.SetScopesCollectionName(name!)); + var exception = Assert.ThrowsAny(() => builder.SetScopesCollectionName(name!)); Assert.Equal("name", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0261), exception.Message); } [Fact] @@ -204,10 +201,9 @@ public class OpenIddictMongoDbBuilderTests var builder = CreateBuilder(services); // Act and assert - var exception = Assert.Throws(() => builder.SetTokensCollectionName(name!)); + var exception = Assert.ThrowsAny(() => builder.SetTokensCollectionName(name!)); Assert.Equal("name", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0261), exception.Message); } [Fact] diff --git a/test/OpenIddict.Quartz.Tests/OpenIddictQuartzBuilderTests.cs b/test/OpenIddict.Quartz.Tests/OpenIddictQuartzBuilderTests.cs index 91330763..e82d4110 100644 --- a/test/OpenIddict.Quartz.Tests/OpenIddictQuartzBuilderTests.cs +++ b/test/OpenIddict.Quartz.Tests/OpenIddictQuartzBuilderTests.cs @@ -90,7 +90,6 @@ public class OpenIddictQuartzBuilderTests var exception = Assert.Throws(() => builder.SetMaximumRefireCount(-1)); Assert.Equal("count", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0279), exception.Message); } [Fact] @@ -120,7 +119,6 @@ public class OpenIddictQuartzBuilderTests var exception = Assert.Throws(() => builder.SetMinimumAuthorizationLifespan(TimeSpan.FromSeconds(-1))); Assert.Equal("lifespan", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0280), exception.Message); } [Fact] @@ -150,7 +148,6 @@ public class OpenIddictQuartzBuilderTests var exception = Assert.Throws(() => builder.SetMinimumTokenLifespan(TimeSpan.FromSeconds(-1))); Assert.Equal("lifespan", exception.ParamName); - Assert.StartsWith(SR.GetResourceString(SR.ID0280), exception.Message); } [Fact] diff --git a/test/OpenIddict.Server.IntegrationTests/OpenIddictServerIntegrationTestClient.cs b/test/OpenIddict.Server.IntegrationTests/OpenIddictServerIntegrationTestClient.cs index 8b6d6d11..e56b44e2 100644 --- a/test/OpenIddict.Server.IntegrationTests/OpenIddictServerIntegrationTestClient.cs +++ b/test/OpenIddict.Server.IntegrationTests/OpenIddictServerIntegrationTestClient.cs @@ -86,15 +86,8 @@ public class OpenIddictServerIntegrationTestClient : IAsyncDisposable /// The OpenID Connect response returned by the server. public Task GetAsync(string uri, OpenIddictRequest request) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } - - if (string.IsNullOrEmpty(uri)) - { - throw new ArgumentException("The URI cannot be null or empty.", nameof(uri)); - } + ArgumentNullException.ThrowIfNull(request); + ArgumentException.ThrowIfNullOrEmpty(uri); return GetAsync(new Uri(uri, UriKind.RelativeOrAbsolute), request); } @@ -118,15 +111,8 @@ public class OpenIddictServerIntegrationTestClient : IAsyncDisposable /// The OpenID Connect response returned by the server. public Task PostAsync(string uri, OpenIddictRequest request) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } - - if (string.IsNullOrEmpty(uri)) - { - throw new ArgumentException("The URI cannot be null or empty.", nameof(uri)); - } + ArgumentNullException.ThrowIfNull(request); + ArgumentException.ThrowIfNullOrEmpty(uri); return PostAsync(new Uri(uri, UriKind.RelativeOrAbsolute), request); } @@ -151,20 +137,9 @@ public class OpenIddictServerIntegrationTestClient : IAsyncDisposable /// The OpenID Connect response returned by the server. public Task SendAsync(string method, string uri, OpenIddictRequest request) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } - - if (string.IsNullOrEmpty(method)) - { - throw new ArgumentException("The HTTP method cannot be null or empty.", nameof(method)); - } - - if (string.IsNullOrEmpty(uri)) - { - throw new ArgumentException("The URI cannot be null or empty.", nameof(uri)); - } + ArgumentNullException.ThrowIfNull(request); + ArgumentException.ThrowIfNullOrEmpty(method); + ArgumentException.ThrowIfNullOrEmpty(uri); return SendAsync(new HttpMethod(method), uri, request); } @@ -179,20 +154,9 @@ public class OpenIddictServerIntegrationTestClient : IAsyncDisposable /// The OpenID Connect response returned by the server. public Task SendAsync(HttpMethod method, string uri, OpenIddictRequest request) { - if (method is null) - { - throw new ArgumentNullException(nameof(method)); - } - - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } - - if (string.IsNullOrEmpty(uri)) - { - throw new ArgumentException("The URI cannot be null or empty.", nameof(uri)); - } + ArgumentNullException.ThrowIfNull(method); + ArgumentNullException.ThrowIfNull(request); + ArgumentException.ThrowIfNullOrEmpty(uri); return SendAsync(method, new Uri(uri, UriKind.RelativeOrAbsolute), request); } @@ -207,26 +171,9 @@ public class OpenIddictServerIntegrationTestClient : IAsyncDisposable /// The OpenID Connect response returned by the server. public virtual async Task SendAsync(HttpMethod method, Uri uri, OpenIddictRequest request) { - if (method is null) - { - throw new ArgumentNullException(nameof(method)); - } - - if (uri is null) - { - throw new ArgumentNullException(nameof(uri)); - } - - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } - - if (HttpClient.BaseAddress is null && !uri.IsAbsoluteUri) - { - throw new ArgumentException("The URI cannot be a relative URI when no base URI " + - "is associated with the HTTP client.", nameof(uri)); - } + ArgumentNullException.ThrowIfNull(method); + ArgumentNullException.ThrowIfNull(uri); + ArgumentNullException.ThrowIfNull(request); using var message = CreateRequestMessage(request, method, uri); using var response = await HttpClient.SendAsync(message); diff --git a/test/OpenIddict.Server.Tests/OpenIddictServerBuilderTests.cs b/test/OpenIddict.Server.Tests/OpenIddictServerBuilderTests.cs index efbd0101..e84a8482 100644 --- a/test/OpenIddict.Server.Tests/OpenIddictServerBuilderTests.cs +++ b/test/OpenIddict.Server.Tests/OpenIddictServerBuilderTests.cs @@ -530,10 +530,9 @@ public class OpenIddictServerBuilderTests var builder = CreateBuilder(services); // Act and assert - var exception = Assert.Throws(() => builder.AllowCustomFlow(type!)); + var exception = Assert.ThrowsAny(() => builder.AllowCustomFlow(type!)); Assert.Equal("type", exception.ParamName); - Assert.Contains("The grant type cannot be null or empty.", exception.Message); } [Fact] @@ -1920,7 +1919,6 @@ public class OpenIddictServerBuilderTests // Act and assert var exception = Assert.Throws(() => builder.SetUserCodeCharset(["0"])); - Assert.StartsWith(SR.FormatID0440(9), exception.Message); Assert.Equal("charset", exception.ParamName); } @@ -2018,7 +2016,6 @@ public class OpenIddictServerBuilderTests // Act and assert var exception = Assert.Throws(() => builder.SetUserCodeLength(length)); - Assert.StartsWith(SR.FormatID0439(6), exception.Message); Assert.Equal("length", exception.ParamName); } @@ -2125,10 +2122,9 @@ public class OpenIddictServerBuilderTests var builder = CreateBuilder(services); // Act and assert - var exception = Assert.Throws(() => builder.SetIssuer(uri!)); + var exception = Assert.ThrowsAny(() => builder.SetIssuer(uri!)); Assert.Equal("uri", exception.ParamName); - Assert.StartsWith(SR.FormatID0366("uri"), exception.Message); } [Fact] diff --git a/test/OpenIddict.Validation.IntegrationTests/OpenIddictValidationIntegrationTestClient.cs b/test/OpenIddict.Validation.IntegrationTests/OpenIddictValidationIntegrationTestClient.cs index 89999efa..8528faab 100644 --- a/test/OpenIddict.Validation.IntegrationTests/OpenIddictValidationIntegrationTestClient.cs +++ b/test/OpenIddict.Validation.IntegrationTests/OpenIddictValidationIntegrationTestClient.cs @@ -86,15 +86,8 @@ public class OpenIddictValidationIntegrationTestClient : IAsyncDisposable /// The OpenID Connect response returned by the server. public Task GetAsync(string uri, OpenIddictRequest request) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } - - if (string.IsNullOrEmpty(uri)) - { - throw new ArgumentException("The URI cannot be null or empty.", nameof(uri)); - } + ArgumentNullException.ThrowIfNull(request); + ArgumentException.ThrowIfNullOrEmpty(uri); return GetAsync(new Uri(uri, UriKind.RelativeOrAbsolute), request); } @@ -118,15 +111,8 @@ public class OpenIddictValidationIntegrationTestClient : IAsyncDisposable /// The OpenID Connect response returned by the server. public Task PostAsync(string uri, OpenIddictRequest request) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } - - if (string.IsNullOrEmpty(uri)) - { - throw new ArgumentException("The URI cannot be null or empty.", nameof(uri)); - } + ArgumentNullException.ThrowIfNull(request); + ArgumentException.ThrowIfNullOrEmpty(uri); return PostAsync(new Uri(uri, UriKind.RelativeOrAbsolute), request); } @@ -151,20 +137,9 @@ public class OpenIddictValidationIntegrationTestClient : IAsyncDisposable /// The OpenID Connect response returned by the server. public Task SendAsync(string method, string uri, OpenIddictRequest request) { - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } - - if (string.IsNullOrEmpty(method)) - { - throw new ArgumentException("The HTTP method cannot be null or empty.", nameof(method)); - } - - if (string.IsNullOrEmpty(uri)) - { - throw new ArgumentException("The URI cannot be null or empty.", nameof(uri)); - } + ArgumentNullException.ThrowIfNull(request); + ArgumentException.ThrowIfNullOrEmpty(method); + ArgumentException.ThrowIfNullOrEmpty(uri); return SendAsync(new HttpMethod(method), uri, request); } @@ -179,20 +154,9 @@ public class OpenIddictValidationIntegrationTestClient : IAsyncDisposable /// The OpenID Connect response returned by the server. public Task SendAsync(HttpMethod method, string uri, OpenIddictRequest request) { - if (method is null) - { - throw new ArgumentNullException(nameof(method)); - } - - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } - - if (string.IsNullOrEmpty(uri)) - { - throw new ArgumentException("The URI cannot be null or empty.", nameof(uri)); - } + ArgumentNullException.ThrowIfNull(method); + ArgumentNullException.ThrowIfNull(request); + ArgumentException.ThrowIfNullOrEmpty(uri); return SendAsync(method, new Uri(uri, UriKind.RelativeOrAbsolute), request); } @@ -207,26 +171,9 @@ public class OpenIddictValidationIntegrationTestClient : IAsyncDisposable /// The OpenID Connect response returned by the server. public virtual async Task SendAsync(HttpMethod method, Uri uri, OpenIddictRequest request) { - if (method is null) - { - throw new ArgumentNullException(nameof(method)); - } - - if (uri is null) - { - throw new ArgumentNullException(nameof(uri)); - } - - if (request is null) - { - throw new ArgumentNullException(nameof(request)); - } - - if (HttpClient.BaseAddress is null && !uri.IsAbsoluteUri) - { - throw new ArgumentException("The URI cannot be a relative URI when no base URI " + - "is associated with the HTTP client.", nameof(uri)); - } + ArgumentNullException.ThrowIfNull(method); + ArgumentNullException.ThrowIfNull(uri); + ArgumentNullException.ThrowIfNull(request); using var message = CreateRequestMessage(request, method, uri); using var response = await HttpClient.SendAsync(message);