From 255fc64cafa16ca4e7635d1525c6abe4175412f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Chalet?= Date: Fri, 8 Apr 2022 19:04:32 +0200 Subject: [PATCH] Fix incorrect error messages in the client stack and use ?? for null checks where appropriate --- .../OpenIddictResources.resx | 8 +++ .../OpenIddictClientAspNetCoreHandlers.cs | 2 +- .../OpenIddictClientSystemNetHttpHandlers.cs | 10 +--- .../OpenIddictClientBuilder.cs | 60 ++++++------------- .../OpenIddictClientConfiguration.cs | 2 +- .../OpenIddictClientDispatcher.cs | 15 +++-- .../OpenIddictClientHandlers.cs | 20 ++----- .../OpenIddictClientService.cs | 14 +---- .../Caches/OpenIddictApplicationCache.cs | 18 ++---- .../Caches/OpenIddictAuthorizationCache.cs | 18 ++---- .../Caches/OpenIddictScopeCache.cs | 18 ++---- .../Caches/OpenIddictTokenCache.cs | 18 ++---- .../Managers/OpenIddictApplicationManager.cs | 5 +- .../OpenIddictAuthorizationManager.cs | 5 +- .../Managers/OpenIddictScopeManager.cs | 5 +- .../Managers/OpenIddictTokenManager.cs | 5 +- src/OpenIddict.Core/OpenIddictCoreBuilder.cs | 40 +++---------- .../OpenIddictCoreExtensions.cs | 40 +++++-------- ...EntityFrameworkApplicationStoreResolver.cs | 10 +--- ...tityFrameworkAuthorizationStoreResolver.cs | 10 +--- ...IddictEntityFrameworkScopeStoreResolver.cs | 10 +--- ...IddictEntityFrameworkTokenStoreResolver.cs | 10 +--- ...enIddictEntityFrameworkApplicationStore.cs | 4 +- ...IddictEntityFrameworkAuthorizationStore.cs | 10 +--- .../OpenIddictEntityFrameworkScopeStore.cs | 6 +- .../OpenIddictEntityFrameworkTokenStore.cs | 16 ++--- ...tyFrameworkCoreApplicationStoreResolver.cs | 10 +--- ...FrameworkCoreAuthorizationStoreResolver.cs | 10 +--- ...ctEntityFrameworkCoreScopeStoreResolver.cs | 10 +--- ...ctEntityFrameworkCoreTokenStoreResolver.cs | 10 +--- ...dictEntityFrameworkCoreApplicationStore.cs | 4 +- ...ctEntityFrameworkCoreAuthorizationStore.cs | 15 ++--- ...OpenIddictEntityFrameworkCoreScopeStore.cs | 6 +- ...OpenIddictEntityFrameworkCoreTokenStore.cs | 26 ++------ .../OpenIddictMongoDbApplicationStore.cs | 2 +- .../OpenIddictMongoDbAuthorizationStore.cs | 2 +- .../Stores/OpenIddictMongoDbScopeStore.cs | 2 +- .../OpenIddictServerAspNetCoreHandlers.cs | 2 +- .../OpenIddictServerOwinHandler.cs | 5 +- .../OpenIddictServerOwinMiddlewareFactory.cs | 5 +- .../OpenIddictServerBuilder.cs | 59 ++++++------------ .../OpenIddictServerDispatcher.cs | 15 +++-- ...OpenIddictServerHandlers.Authentication.cs | 35 +++-------- .../OpenIddictServerHandlers.Device.cs | 25 ++------ .../OpenIddictServerHandlers.Discovery.cs | 2 +- .../OpenIddictServerHandlers.Exchange.cs | 30 ++-------- .../OpenIddictServerHandlers.Introspection.cs | 20 ++----- .../OpenIddictServerHandlers.Protection.cs | 20 ++----- .../OpenIddictServerHandlers.Revocation.cs | 15 +---- .../OpenIddictServerHandlers.cs | 22 ++----- ...enIddictValidationOwinMiddlewareFactory.cs | 5 +- ...enIddictValidationSystemNetHttpHandlers.cs | 10 +--- .../OpenIddictValidationBuilder.cs | 29 +++------ .../OpenIddictValidationDispatcher.cs | 15 +++-- ...OpenIddictValidationHandlers.Protection.cs | 5 +- .../OpenIddictValidationService.cs | 21 +------ .../OpenIddictServerIntegrationTestClient.cs | 2 +- 57 files changed, 221 insertions(+), 597 deletions(-) diff --git a/src/OpenIddict.Abstractions/OpenIddictResources.resx b/src/OpenIddict.Abstractions/OpenIddictResources.resx index 4df9cd32..3028e83e 100644 --- a/src/OpenIddict.Abstractions/OpenIddictResources.resx +++ b/src/OpenIddict.Abstractions/OpenIddictResources.resx @@ -1197,6 +1197,14 @@ To apply redirection responses, create a class implementing 'IOpenIddictClientHa A refresh token must be specified when using the refresh token grant. + + The event handler of type '{0}' couldn't be resolved. +This may indicate that it was not properly registered in the dependency injection container. To register an event handler, use 'services.AddOpenIddict().AddClient().AddEventHandler()'. + + + A discovery client must be registered when using server discovery. +Reference the 'OpenIddict.Client.SystemNetHttp' package and call 'services.AddOpenIddict().AddClient().UseSystemNetHttp()' to register the default System.Net.Http-based integration. + The security token is missing. diff --git a/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreHandlers.cs b/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreHandlers.cs index 902f45e6..d299d4eb 100644 --- a/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreHandlers.cs +++ b/src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreHandlers.cs @@ -698,7 +698,7 @@ public static partial class OpenIddictClientAspNetCoreHandlers // If it was not registered or enabled, let the default OpenIddict client handlers render // a default error page instead of delegating the rendering to the status code middleware. var feature = response.HttpContext.Features.Get(); - if (feature is null || !feature.Enabled) + if (feature is not { Enabled: true }) { return default; } diff --git a/src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpHandlers.cs b/src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpHandlers.cs index 682f4179..358935f3 100644 --- a/src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpHandlers.cs +++ b/src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpHandlers.cs @@ -222,21 +222,15 @@ public static partial class OpenIddictClientSystemNetHttpHandlers throw new InvalidOperationException(SR.GetResourceString(SR.ID0173)); var assembly = typeof(OpenIddictClientSystemNetHttpOptions).Assembly.GetName(); - using var client = _factory.CreateClient(assembly.Name!); - if (client is null) - { + using var client = _factory.CreateClient(assembly.Name!) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0174)); - } #if SUPPORTS_HTTP_CLIENT_DEFAULT_REQUEST_VERSION // If supported, import the HTTP version from the client instance. request.Version = client.DefaultRequestVersion; #endif - var response = await client.SendAsync(request, HttpCompletionOption.ResponseContentRead); - if (response is null) - { + var response = await client.SendAsync(request, HttpCompletionOption.ResponseContentRead) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0175)); - } // Store the HttpResponseMessage in the transaction properties. context.Transaction.SetProperty(typeof(HttpResponseMessage).FullName!, response); diff --git a/src/OpenIddict.Client/OpenIddictClientBuilder.cs b/src/OpenIddict.Client/OpenIddictClientBuilder.cs index ea800754..fd1edf1e 100644 --- a/src/OpenIddict.Client/OpenIddictClientBuilder.cs +++ b/src/OpenIddict.Client/OpenIddictClientBuilder.cs @@ -372,11 +372,8 @@ public class OpenIddictClientBuilder throw new ArgumentException(SR.GetResourceString(SR.ID0062), nameof(resource)); } - using var stream = assembly.GetManifestResourceStream(resource); - if (stream is null) - { + using var stream = assembly.GetManifestResourceStream(resource) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0064)); - } return AddEncryptionCertificate(stream, password, flags); } @@ -429,13 +426,10 @@ public class OpenIddictClientBuilder throw new ArgumentException(SR.GetResourceString(SR.ID0065), nameof(thumbprint)); } - var certificate = GetCertificate(StoreLocation.CurrentUser, thumbprint) ?? GetCertificate(StoreLocation.LocalMachine, thumbprint); - if (certificate is null) - { - throw new InvalidOperationException(SR.GetResourceString(SR.ID0066)); - } - - return AddEncryptionCertificate(certificate); + return AddEncryptionCertificate( + GetCertificate(StoreLocation.CurrentUser, thumbprint) ?? + GetCertificate(StoreLocation.LocalMachine, thumbprint) ?? + throw new InvalidOperationException(SR.GetResourceString(SR.ID0066))); static X509Certificate2? GetCertificate(StoreLocation location, string thumbprint) { @@ -465,16 +459,10 @@ public class OpenIddictClientBuilder using var store = new X509Store(name, location); store.Open(OpenFlags.ReadOnly); - var certificate = store.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, validOnly: false) - .OfType() - .SingleOrDefault(); - - if (certificate is null) - { - throw new InvalidOperationException(SR.GetResourceString(SR.ID0066)); - } - - return AddEncryptionCertificate(certificate); + return AddEncryptionCertificate( + store.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, validOnly: false) + .OfType() + .SingleOrDefault() ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0066))); } /// @@ -780,11 +768,8 @@ public class OpenIddictClientBuilder throw new ArgumentException(SR.GetResourceString(SR.ID0062), nameof(resource)); } - using var stream = assembly.GetManifestResourceStream(resource); - if (stream is null) - { + using var stream = assembly.GetManifestResourceStream(resource) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0064)); - } return AddSigningCertificate(stream, password, flags); } @@ -837,13 +822,10 @@ public class OpenIddictClientBuilder throw new ArgumentException(SR.GetResourceString(SR.ID0065), nameof(thumbprint)); } - var certificate = GetCertificate(StoreLocation.CurrentUser, thumbprint) ?? GetCertificate(StoreLocation.LocalMachine, thumbprint); - if (certificate is null) - { - throw new InvalidOperationException(SR.GetResourceString(SR.ID0066)); - } - - return AddSigningCertificate(certificate); + return AddSigningCertificate( + GetCertificate(StoreLocation.CurrentUser, thumbprint) ?? + GetCertificate(StoreLocation.LocalMachine, thumbprint) ?? + throw new InvalidOperationException(SR.GetResourceString(SR.ID0066))); static X509Certificate2? GetCertificate(StoreLocation location, string thumbprint) { @@ -873,16 +855,10 @@ public class OpenIddictClientBuilder using var store = new X509Store(name, location); store.Open(OpenFlags.ReadOnly); - var certificate = store.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, validOnly: false) - .OfType() - .SingleOrDefault(); - - if (certificate is null) - { - throw new InvalidOperationException(SR.GetResourceString(SR.ID0066)); - } - - return AddSigningCertificate(certificate); + return AddSigningCertificate( + store.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, validOnly: false) + .OfType() + .SingleOrDefault() ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0066))); } /// diff --git a/src/OpenIddict.Client/OpenIddictClientConfiguration.cs b/src/OpenIddict.Client/OpenIddictClientConfiguration.cs index 7c86dfa1..ce0934fd 100644 --- a/src/OpenIddict.Client/OpenIddictClientConfiguration.cs +++ b/src/OpenIddict.Client/OpenIddictClientConfiguration.cs @@ -49,7 +49,7 @@ public class OpenIddictClientConfiguration : IPostConfigureOptions descriptor.ContextType == typeof(ApplyConfigurationRequestContext)) || !options.Handlers.Any(descriptor => descriptor.ContextType == typeof(ApplyCryptographyRequestContext))) { - throw new InvalidOperationException(SR.GetResourceString(SR.ID0135)); + throw new InvalidOperationException(SR.GetResourceString(SR.ID0313)); } if (registration.MetadataAddress is null) diff --git a/src/OpenIddict.Client/OpenIddictClientDispatcher.cs b/src/OpenIddict.Client/OpenIddictClientDispatcher.cs index b32608a4..2dcf2831 100644 --- a/src/OpenIddict.Client/OpenIddictClientDispatcher.cs +++ b/src/OpenIddict.Client/OpenIddictClientDispatcher.cs @@ -93,16 +93,15 @@ public class OpenIddictClientDispatcher : IOpenIddictClientDispatcher continue; } - var handler = descriptor.ServiceDescriptor.ImplementationInstance is not null ? - descriptor.ServiceDescriptor.ImplementationInstance as IOpenIddictClientHandler : - _provider.GetService(descriptor.ServiceDescriptor.ServiceType) as IOpenIddictClientHandler; - - if (handler is null) + yield return descriptor.ServiceDescriptor switch { - throw new InvalidOperationException(SR.FormatID0138(descriptor.ServiceDescriptor.ServiceType)); - } + { ImplementationInstance: IOpenIddictClientHandler handler } => handler, + + _ when _provider.GetService(descriptor.ServiceDescriptor.ServiceType) + is IOpenIddictClientHandler handler => handler, - yield return handler; + _ => throw new InvalidOperationException(SR.FormatID0312(descriptor.ServiceDescriptor.ServiceType)) + }; } } diff --git a/src/OpenIddict.Client/OpenIddictClientHandlers.cs b/src/OpenIddict.Client/OpenIddictClientHandlers.cs index 8e52d92d..073f8fdd 100644 --- a/src/OpenIddict.Client/OpenIddictClientHandlers.cs +++ b/src/OpenIddict.Client/OpenIddictClientHandlers.cs @@ -362,11 +362,8 @@ public static partial class OpenIddictClientHandlers // Note: if the static registration cannot be found in the options, this may indicate // the client was removed after the authorization dance started and thus, can no longer // be used to authenticate users. In this case, throw an exception to abort the flow. - var registration = context.Options.Registrations.Find(registration => registration.Issuer == issuer); - if (registration is null) - { + var registration = context.Options.Registrations.Find(registration => registration.Issuer == issuer) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0292)); - } context.Issuer = issuer; context.Registration = registration; @@ -1096,11 +1093,7 @@ public static partial class OpenIddictClientHandlers // Resolve the hash algorithm corresponding to the signing algorithm. If an // instance of the BCL hash algorithm cannot be resolved, throw an exception. - var algorithm = GetHashAlgorithm(name); - if (algorithm is null) - { - throw new InvalidOperationException(SR.GetResourceString(SR.ID0293)); - } + var algorithm = GetHashAlgorithm(name) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0293)); // If a frontchannel access token was returned in the authorization response, // ensure the at_hash claim matches the hash of the actual access token. @@ -1988,11 +1981,7 @@ public static partial class OpenIddictClientHandlers // Resolve the hash algorithm corresponding to the signing algorithm. If an // instance of the BCL hash algorithm cannot be resolved, throw an exception. - var algorithm = GetHashAlgorithm(name); - if (algorithm is null) - { - throw new InvalidOperationException(SR.GetResourceString(SR.ID0295)); - } + var algorithm = GetHashAlgorithm(name) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0295)); var hash = context.BackchannelIdentityTokenPrincipal.GetClaim(Claims.AccessTokenHash); if (string.IsNullOrEmpty(hash)) @@ -2404,7 +2393,8 @@ public static partial class OpenIddictClientHandlers /// public async ValueTask HandleAsync(ProcessAuthenticationContext context!!) { - if (context.UserinfoTokenPrincipal is not null || string.IsNullOrEmpty(context.UserinfoToken)) + if (context.UserinfoTokenPrincipal is not null || + string.IsNullOrEmpty(context.UserinfoToken)) { return; } diff --git a/src/OpenIddict.Client/OpenIddictClientService.cs b/src/OpenIddict.Client/OpenIddictClientService.cs index 4d898e3b..c343bf79 100644 --- a/src/OpenIddict.Client/OpenIddictClientService.cs +++ b/src/OpenIddict.Client/OpenIddictClientService.cs @@ -55,13 +55,8 @@ public class OpenIddictClientService request = await ApplyConfigurationRequestAsync(); var response = await ExtractConfigurationResponseAsync(); - var configuration = await HandleConfigurationResponseAsync(); - if (configuration is null) - { + return await HandleConfigurationResponseAsync() ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0145)); - } - - return configuration; async ValueTask PrepareConfigurationRequestAsync() { @@ -195,13 +190,8 @@ public class OpenIddictClientService var response = await ExtractCryptographyResponseAsync(); - var keys = await HandleCryptographyResponseAsync(); - if (keys is null) - { + return await HandleCryptographyResponseAsync() ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0147)); - } - - return keys; async ValueTask PrepareCryptographyRequestAsync() { diff --git a/src/OpenIddict.Core/Caches/OpenIddictApplicationCache.cs b/src/OpenIddict.Core/Caches/OpenIddictApplicationCache.cs index 01e1af04..f097c0d7 100644 --- a/src/OpenIddict.Core/Caches/OpenIddictApplicationCache.cs +++ b/src/OpenIddict.Core/Caches/OpenIddictApplicationCache.cs @@ -272,13 +272,8 @@ public class OpenIddictApplicationCache : IOpenIddictApplicationCa if (application is not null) { - var signal = await CreateExpirationSignalAsync(application, cancellationToken); - if (signal is null) - { - throw new InvalidOperationException(SR.GetResourceString(SR.ID0197)); - } - - entry.AddExpirationToken(signal); + entry.AddExpirationToken(await CreateExpirationSignalAsync(application, cancellationToken) ?? + throw new InvalidOperationException(SR.GetResourceString(SR.ID0197))); } entry.SetSize(1L); @@ -299,13 +294,8 @@ public class OpenIddictApplicationCache : IOpenIddictApplicationCa foreach (var application in applications) { - var signal = await CreateExpirationSignalAsync(application, cancellationToken); - if (signal is null) - { - throw new InvalidOperationException(SR.GetResourceString(SR.ID0197)); - } - - entry.AddExpirationToken(signal); + entry.AddExpirationToken(await CreateExpirationSignalAsync(application, cancellationToken) ?? + throw new InvalidOperationException(SR.GetResourceString(SR.ID0197))); } entry.SetSize(applications.Length); diff --git a/src/OpenIddict.Core/Caches/OpenIddictAuthorizationCache.cs b/src/OpenIddict.Core/Caches/OpenIddictAuthorizationCache.cs index 3cd30957..f9da27b7 100644 --- a/src/OpenIddict.Core/Caches/OpenIddictAuthorizationCache.cs +++ b/src/OpenIddict.Core/Caches/OpenIddictAuthorizationCache.cs @@ -448,13 +448,8 @@ public class OpenIddictAuthorizationCache : IOpenIddictAuthoriza if (authorization is not null) { - var signal = await CreateExpirationSignalAsync(authorization, cancellationToken); - if (signal is null) - { - throw new InvalidOperationException(SR.GetResourceString(SR.ID0197)); - } - - entry.AddExpirationToken(signal); + entry.AddExpirationToken(await CreateExpirationSignalAsync(authorization, cancellationToken) ?? + throw new InvalidOperationException(SR.GetResourceString(SR.ID0197))); } entry.SetSize(1L); @@ -475,13 +470,8 @@ public class OpenIddictAuthorizationCache : IOpenIddictAuthoriza foreach (var authorization in authorizations) { - var signal = await CreateExpirationSignalAsync(authorization, cancellationToken); - if (signal is null) - { - throw new InvalidOperationException(SR.GetResourceString(SR.ID0197)); - } - - entry.AddExpirationToken(signal); + entry.AddExpirationToken(await CreateExpirationSignalAsync(authorization, cancellationToken) ?? + throw new InvalidOperationException(SR.GetResourceString(SR.ID0197))); } entry.SetSize(authorizations.Length); diff --git a/src/OpenIddict.Core/Caches/OpenIddictScopeCache.cs b/src/OpenIddict.Core/Caches/OpenIddictScopeCache.cs index 882b89ac..e795f873 100644 --- a/src/OpenIddict.Core/Caches/OpenIddictScopeCache.cs +++ b/src/OpenIddict.Core/Caches/OpenIddictScopeCache.cs @@ -245,13 +245,8 @@ public class OpenIddictScopeCache : IOpenIddictScopeCache, IDisp if (scope is not null) { - var signal = await CreateExpirationSignalAsync(scope, cancellationToken); - if (signal is null) - { - throw new InvalidOperationException(SR.GetResourceString(SR.ID0197)); - } - - entry.AddExpirationToken(signal); + entry.AddExpirationToken(await CreateExpirationSignalAsync(scope, cancellationToken) ?? + throw new InvalidOperationException(SR.GetResourceString(SR.ID0197))); } entry.SetSize(1L); @@ -272,13 +267,8 @@ public class OpenIddictScopeCache : IOpenIddictScopeCache, IDisp foreach (var scope in scopes) { - var signal = await CreateExpirationSignalAsync(scope, cancellationToken); - if (signal is null) - { - throw new InvalidOperationException(SR.GetResourceString(SR.ID0197)); - } - - entry.AddExpirationToken(signal); + entry.AddExpirationToken(await CreateExpirationSignalAsync(scope, cancellationToken) ?? + throw new InvalidOperationException(SR.GetResourceString(SR.ID0197))); } entry.SetSize(scopes.Length); diff --git a/src/OpenIddict.Core/Caches/OpenIddictTokenCache.cs b/src/OpenIddict.Core/Caches/OpenIddictTokenCache.cs index 9dfb0a17..13fa9477 100644 --- a/src/OpenIddict.Core/Caches/OpenIddictTokenCache.cs +++ b/src/OpenIddict.Core/Caches/OpenIddictTokenCache.cs @@ -500,13 +500,8 @@ public class OpenIddictTokenCache : IOpenIddictTokenCache, IDisp if (token is not null) { - var signal = await CreateExpirationSignalAsync(token, cancellationToken); - if (signal is null) - { - throw new InvalidOperationException(SR.GetResourceString(SR.ID0197)); - } - - entry.AddExpirationToken(signal); + entry.AddExpirationToken(await CreateExpirationSignalAsync(token, cancellationToken) ?? + throw new InvalidOperationException(SR.GetResourceString(SR.ID0197))); } entry.SetSize(1L); @@ -527,13 +522,8 @@ public class OpenIddictTokenCache : IOpenIddictTokenCache, IDisp foreach (var token in tokens) { - var signal = await CreateExpirationSignalAsync(token, cancellationToken); - if (signal is null) - { - throw new InvalidOperationException(SR.GetResourceString(SR.ID0197)); - } - - entry.AddExpirationToken(signal); + entry.AddExpirationToken(await CreateExpirationSignalAsync(token, cancellationToken) ?? + throw new InvalidOperationException(SR.GetResourceString(SR.ID0197))); } entry.SetSize(tokens.Length); diff --git a/src/OpenIddict.Core/Managers/OpenIddictApplicationManager.cs b/src/OpenIddict.Core/Managers/OpenIddictApplicationManager.cs index 7b302f88..39546df5 100644 --- a/src/OpenIddict.Core/Managers/OpenIddictApplicationManager.cs +++ b/src/OpenIddict.Core/Managers/OpenIddictApplicationManager.cs @@ -191,11 +191,8 @@ public class OpenIddictApplicationManager : IOpenIddictApplication public virtual async ValueTask CreateAsync( OpenIddictApplicationDescriptor descriptor!!, CancellationToken cancellationToken = default) { - var application = await Store.InstantiateAsync(cancellationToken); - if (application is null) - { + var application = await Store.InstantiateAsync(cancellationToken) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0208)); - } await PopulateAsync(application, descriptor, cancellationToken); diff --git a/src/OpenIddict.Core/Managers/OpenIddictAuthorizationManager.cs b/src/OpenIddict.Core/Managers/OpenIddictAuthorizationManager.cs index daa9e401..a426384f 100644 --- a/src/OpenIddict.Core/Managers/OpenIddictAuthorizationManager.cs +++ b/src/OpenIddict.Core/Managers/OpenIddictAuthorizationManager.cs @@ -147,11 +147,8 @@ public class OpenIddictAuthorizationManager : IOpenIddictAuthori public virtual async ValueTask CreateAsync( OpenIddictAuthorizationDescriptor descriptor!!, CancellationToken cancellationToken = default) { - var authorization = await Store.InstantiateAsync(cancellationToken); - if (authorization is null) - { + var authorization = await Store.InstantiateAsync(cancellationToken) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0220)); - } await PopulateAsync(authorization, descriptor, cancellationToken); await CreateAsync(authorization, cancellationToken); diff --git a/src/OpenIddict.Core/Managers/OpenIddictScopeManager.cs b/src/OpenIddict.Core/Managers/OpenIddictScopeManager.cs index c6dd6e27..832edca4 100644 --- a/src/OpenIddict.Core/Managers/OpenIddictScopeManager.cs +++ b/src/OpenIddict.Core/Managers/OpenIddictScopeManager.cs @@ -140,11 +140,8 @@ public class OpenIddictScopeManager : IOpenIddictScopeManager where TSco public virtual async ValueTask CreateAsync( OpenIddictScopeDescriptor descriptor!!, CancellationToken cancellationToken = default) { - var scope = await Store.InstantiateAsync(cancellationToken); - if (scope is null) - { + var scope = await Store.InstantiateAsync(cancellationToken) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0223)); - } await PopulateAsync(scope, descriptor, cancellationToken); await CreateAsync(scope, cancellationToken); diff --git a/src/OpenIddict.Core/Managers/OpenIddictTokenManager.cs b/src/OpenIddict.Core/Managers/OpenIddictTokenManager.cs index e803afba..ec556336 100644 --- a/src/OpenIddict.Core/Managers/OpenIddictTokenManager.cs +++ b/src/OpenIddict.Core/Managers/OpenIddictTokenManager.cs @@ -155,11 +155,8 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok public virtual async ValueTask CreateAsync( OpenIddictTokenDescriptor descriptor!!, CancellationToken cancellationToken = default) { - var token = await Store.InstantiateAsync(cancellationToken); - if (token is null) - { + var token = await Store.InstantiateAsync(cancellationToken) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0226)); - } await PopulateAsync(token, descriptor, cancellationToken); await CreateAsync(token, cancellationToken); diff --git a/src/OpenIddict.Core/OpenIddictCoreBuilder.cs b/src/OpenIddict.Core/OpenIddictCoreBuilder.cs index b9504aee..b370d34e 100644 --- a/src/OpenIddict.Core/OpenIddictCoreBuilder.cs +++ b/src/OpenIddict.Core/OpenIddictCoreBuilder.cs @@ -66,11 +66,8 @@ public class OpenIddictCoreBuilder /// The . public OpenIddictCoreBuilder AddApplicationStore(Type type!!, ServiceLifetime lifetime = ServiceLifetime.Scoped) { - var root = OpenIddictHelpers.FindGenericBaseType(type, typeof(IOpenIddictApplicationStore<>)); - if (root is null) - { + var root = OpenIddictHelpers.FindGenericBaseType(type, typeof(IOpenIddictApplicationStore<>)) ?? throw new ArgumentException(SR.GetResourceString(SR.ID0232), nameof(type)); - } // Note: managers can be either open generics (e.g OpenIddictApplicationStore<>) // or closed generics (e.g OpenIddictApplicationStore). @@ -117,11 +114,8 @@ public class OpenIddictCoreBuilder /// The . public OpenIddictCoreBuilder AddAuthorizationStore(Type type!!, ServiceLifetime lifetime = ServiceLifetime.Scoped) { - var root = OpenIddictHelpers.FindGenericBaseType(type, typeof(IOpenIddictAuthorizationStore<>)); - if (root is null) - { + var root = OpenIddictHelpers.FindGenericBaseType(type, typeof(IOpenIddictAuthorizationStore<>)) ?? throw new ArgumentException(SR.GetResourceString(SR.ID0232), nameof(type)); - } // Note: managers can be either open generics (e.g OpenIddictAuthorizationStore<>) // or closed generics (e.g OpenIddictAuthorizationStore). @@ -168,11 +162,8 @@ public class OpenIddictCoreBuilder /// The . public OpenIddictCoreBuilder AddScopeStore(Type type!!, ServiceLifetime lifetime = ServiceLifetime.Scoped) { - var root = OpenIddictHelpers.FindGenericBaseType(type, typeof(IOpenIddictScopeStore<>)); - if (root is null) - { + var root = OpenIddictHelpers.FindGenericBaseType(type, typeof(IOpenIddictScopeStore<>)) ?? throw new ArgumentException(SR.GetResourceString(SR.ID0232), nameof(type)); - } // Note: managers can be either open generics (e.g OpenIddictScopeStore<>) // or closed generics (e.g OpenIddictScopeStore). @@ -219,11 +210,8 @@ public class OpenIddictCoreBuilder /// The . public OpenIddictCoreBuilder AddTokenStore(Type type!!, ServiceLifetime lifetime = ServiceLifetime.Scoped) { - var root = OpenIddictHelpers.FindGenericBaseType(type, typeof(IOpenIddictTokenStore<>)); - if (root is null) - { + var root = OpenIddictHelpers.FindGenericBaseType(type, typeof(IOpenIddictTokenStore<>)) ?? throw new ArgumentException(SR.GetResourceString(SR.ID0232), nameof(type)); - } // Note: managers can be either open generics (e.g OpenIddictTokenStore<>) // or closed generics (e.g OpenIddictTokenStore). @@ -268,11 +256,8 @@ public class OpenIddictCoreBuilder /// The . public OpenIddictCoreBuilder ReplaceApplicationManager(Type type!!) { - var root = OpenIddictHelpers.FindGenericBaseType(type, typeof(OpenIddictApplicationManager<>)); - if (root is null) - { + var root = OpenIddictHelpers.FindGenericBaseType(type, typeof(OpenIddictApplicationManager<>)) ?? throw new ArgumentException(SR.GetResourceString(SR.ID0232), nameof(type)); - } // Note: managers can be either open generics (e.g OpenIddictApplicationManager<>) // or closed generics (e.g OpenIddictApplicationManager). @@ -352,11 +337,8 @@ public class OpenIddictCoreBuilder /// The . public OpenIddictCoreBuilder ReplaceAuthorizationManager(Type type!!) { - var root = OpenIddictHelpers.FindGenericBaseType(type, typeof(OpenIddictAuthorizationManager<>)); - if (root is null) - { + var root = OpenIddictHelpers.FindGenericBaseType(type, typeof(OpenIddictAuthorizationManager<>)) ?? throw new ArgumentException(SR.GetResourceString(SR.ID0232), nameof(type)); - } // Note: managers can be either open generics (e.g OpenIddictAuthorizationManager<>) // or closed generics (e.g OpenIddictAuthorizationManager). @@ -436,11 +418,8 @@ public class OpenIddictCoreBuilder /// The . public OpenIddictCoreBuilder ReplaceScopeManager(Type type!!) { - var root = OpenIddictHelpers.FindGenericBaseType(type, typeof(OpenIddictScopeManager<>)); - if (root is null) - { + var root = OpenIddictHelpers.FindGenericBaseType(type, typeof(OpenIddictScopeManager<>)) ?? throw new ArgumentException(SR.GetResourceString(SR.ID0232), nameof(type)); - } // Note: managers can be either open generics (e.g OpenIddictScopeManager<>) // or closed generics (e.g OpenIddictScopeManager). @@ -520,11 +499,8 @@ public class OpenIddictCoreBuilder /// The . public OpenIddictCoreBuilder ReplaceTokenManager(Type type!!) { - var root = OpenIddictHelpers.FindGenericBaseType(type, typeof(OpenIddictTokenManager<>)); - if (root is null) - { + var root = OpenIddictHelpers.FindGenericBaseType(type, typeof(OpenIddictTokenManager<>)) ?? throw new ArgumentException(SR.GetResourceString(SR.ID0232), nameof(type)); - } // Note: managers can be either open generics (e.g OpenIddictTokenManager<>) // or closed generics (e.g OpenIddictTokenManager). diff --git a/src/OpenIddict.Core/OpenIddictCoreExtensions.cs b/src/OpenIddict.Core/OpenIddictCoreExtensions.cs index bccdd52c..24a32f98 100644 --- a/src/OpenIddict.Core/OpenIddictCoreExtensions.cs +++ b/src/OpenIddict.Core/OpenIddictCoreExtensions.cs @@ -43,52 +43,44 @@ public static class OpenIddictCoreExtensions builder.Services.TryAddScoped(); builder.Services.TryAddScoped(); - builder.Services.TryAddScoped(provider => + builder.Services.TryAddScoped(static provider => { - var options = provider.GetRequiredService>().CurrentValue; - if (options.DefaultApplicationType is null) - { + var type = provider.GetRequiredService>() + .CurrentValue?.DefaultApplicationType ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0273)); - } return (IOpenIddictApplicationManager) provider.GetRequiredService( - typeof(OpenIddictApplicationManager<>).MakeGenericType(options.DefaultApplicationType)); + typeof(OpenIddictApplicationManager<>).MakeGenericType(type)); }); - builder.Services.TryAddScoped(provider => + builder.Services.TryAddScoped(static provider => { - var options = provider.GetRequiredService>().CurrentValue; - if (options.DefaultAuthorizationType is null) - { + var type = provider.GetRequiredService>() + .CurrentValue?.DefaultAuthorizationType ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0274)); - } return (IOpenIddictAuthorizationManager) provider.GetRequiredService( - typeof(OpenIddictAuthorizationManager<>).MakeGenericType(options.DefaultAuthorizationType)); + typeof(OpenIddictAuthorizationManager<>).MakeGenericType(type)); }); - builder.Services.TryAddScoped(provider => + builder.Services.TryAddScoped(static provider => { - var options = provider.GetRequiredService>().CurrentValue; - if (options.DefaultScopeType is null) - { + var type = provider.GetRequiredService>() + .CurrentValue?.DefaultScopeType ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0275)); - } return (IOpenIddictScopeManager) provider.GetRequiredService( - typeof(OpenIddictScopeManager<>).MakeGenericType(options.DefaultScopeType)); + typeof(OpenIddictScopeManager<>).MakeGenericType(type)); }); - builder.Services.TryAddScoped(provider => + builder.Services.TryAddScoped(static provider => { - var options = provider.GetRequiredService>().CurrentValue; - if (options.DefaultTokenType is null) - { + var type = provider.GetRequiredService>() + .CurrentValue?.DefaultTokenType ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0276)); - } return (IOpenIddictTokenManager) provider.GetRequiredService( - typeof(OpenIddictTokenManager<>).MakeGenericType(options.DefaultTokenType)); + typeof(OpenIddictTokenManager<>).MakeGenericType(type)); }); return new OpenIddictCoreBuilder(builder.Services); diff --git a/src/OpenIddict.EntityFramework/Resolvers/OpenIddictEntityFrameworkApplicationStoreResolver.cs b/src/OpenIddict.EntityFramework/Resolvers/OpenIddictEntityFrameworkApplicationStoreResolver.cs index 2c0aa2e4..3f5c6eb3 100644 --- a/src/OpenIddict.EntityFramework/Resolvers/OpenIddictEntityFrameworkApplicationStoreResolver.cs +++ b/src/OpenIddict.EntityFramework/Resolvers/OpenIddictEntityFrameworkApplicationStoreResolver.cs @@ -47,17 +47,11 @@ public class OpenIddictEntityFrameworkApplicationStoreResolver : IOpenIddictAppl var type = _cache.GetOrAdd(typeof(TApplication), key => { - var root = OpenIddictHelpers.FindGenericBaseType(key, typeof(OpenIddictEntityFrameworkApplication<,,>)); - if (root is null) - { + var root = OpenIddictHelpers.FindGenericBaseType(key, typeof(OpenIddictEntityFrameworkApplication<,,>)) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0234)); - } - var context = _options.CurrentValue.DbContextType; - if (context is null) - { + var context = _options.CurrentValue.DbContextType ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0235)); - } return typeof(OpenIddictEntityFrameworkApplicationStore<,,,,>).MakeGenericType( /* TApplication: */ key, diff --git a/src/OpenIddict.EntityFramework/Resolvers/OpenIddictEntityFrameworkAuthorizationStoreResolver.cs b/src/OpenIddict.EntityFramework/Resolvers/OpenIddictEntityFrameworkAuthorizationStoreResolver.cs index 8ae908ee..7ed3f682 100644 --- a/src/OpenIddict.EntityFramework/Resolvers/OpenIddictEntityFrameworkAuthorizationStoreResolver.cs +++ b/src/OpenIddict.EntityFramework/Resolvers/OpenIddictEntityFrameworkAuthorizationStoreResolver.cs @@ -47,17 +47,11 @@ public class OpenIddictEntityFrameworkAuthorizationStoreResolver : IOpenIddictAu var type = _cache.GetOrAdd(typeof(TAuthorization), key => { - var root = OpenIddictHelpers.FindGenericBaseType(key, typeof(OpenIddictEntityFrameworkAuthorization<,,>)); - if (root is null) - { + var root = OpenIddictHelpers.FindGenericBaseType(key, typeof(OpenIddictEntityFrameworkAuthorization<,,>)) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0236)); - } - var context = _options.CurrentValue.DbContextType; - if (context is null) - { + var context = _options.CurrentValue.DbContextType ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0235)); - } return typeof(OpenIddictEntityFrameworkAuthorizationStore<,,,,>).MakeGenericType( /* TAuthorization: */ key, diff --git a/src/OpenIddict.EntityFramework/Resolvers/OpenIddictEntityFrameworkScopeStoreResolver.cs b/src/OpenIddict.EntityFramework/Resolvers/OpenIddictEntityFrameworkScopeStoreResolver.cs index c483f7ed..1515084a 100644 --- a/src/OpenIddict.EntityFramework/Resolvers/OpenIddictEntityFrameworkScopeStoreResolver.cs +++ b/src/OpenIddict.EntityFramework/Resolvers/OpenIddictEntityFrameworkScopeStoreResolver.cs @@ -47,17 +47,11 @@ public class OpenIddictEntityFrameworkScopeStoreResolver : IOpenIddictScopeStore var type = _cache.GetOrAdd(typeof(TScope), key => { - var root = OpenIddictHelpers.FindGenericBaseType(key, typeof(OpenIddictEntityFrameworkScope<>)); - if (root is null) - { + var root = OpenIddictHelpers.FindGenericBaseType(key, typeof(OpenIddictEntityFrameworkScope<>)) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0237)); - } - var context = _options.CurrentValue.DbContextType; - if (context is null) - { + var context = _options.CurrentValue.DbContextType ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0235)); - } return typeof(OpenIddictEntityFrameworkScopeStore<,,>).MakeGenericType( /* TScope: */ key, diff --git a/src/OpenIddict.EntityFramework/Resolvers/OpenIddictEntityFrameworkTokenStoreResolver.cs b/src/OpenIddict.EntityFramework/Resolvers/OpenIddictEntityFrameworkTokenStoreResolver.cs index 3154c715..04f9711f 100644 --- a/src/OpenIddict.EntityFramework/Resolvers/OpenIddictEntityFrameworkTokenStoreResolver.cs +++ b/src/OpenIddict.EntityFramework/Resolvers/OpenIddictEntityFrameworkTokenStoreResolver.cs @@ -47,17 +47,11 @@ public class OpenIddictEntityFrameworkTokenStoreResolver : IOpenIddictTokenStore var type = _cache.GetOrAdd(typeof(TToken), key => { - var root = OpenIddictHelpers.FindGenericBaseType(key, typeof(OpenIddictEntityFrameworkToken<,,>)); - if (root is null) - { + var root = OpenIddictHelpers.FindGenericBaseType(key, typeof(OpenIddictEntityFrameworkToken<,,>)) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0238)); - } - var context = _options.CurrentValue.DbContextType; - if (context is null) - { + var context = _options.CurrentValue.DbContextType ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0235)); - } return typeof(OpenIddictEntityFrameworkTokenStore<,,,,>).MakeGenericType( /* TToken: */ key, diff --git a/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkApplicationStore.cs b/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkApplicationStore.cs index 15e27436..ad23b0f4 100644 --- a/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkApplicationStore.cs +++ b/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkApplicationStore.cs @@ -610,7 +610,7 @@ public class OpenIddictEntityFrameworkApplicationStore names, CancellationToken cancellationToken) { - if (names is null || names.IsEmpty) + if (names is not { Count: > 0 }) { application.DisplayNames = null; @@ -709,7 +709,7 @@ public class OpenIddictEntityFrameworkApplicationStore properties, CancellationToken cancellationToken) { - if (properties is not { IsEmpty: false }) + if (properties is not { Count: > 0 }) { application.Properties = null; diff --git a/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkAuthorizationStore.cs b/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkAuthorizationStore.cs index b3fad305..c7c9ea78 100644 --- a/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkAuthorizationStore.cs +++ b/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkAuthorizationStore.cs @@ -587,13 +587,9 @@ public class OpenIddictEntityFrameworkAuthorizationStore properties, CancellationToken cancellationToken) { - if (properties is not { IsEmpty: false }) + if (properties is not { Count: > 0 }) { authorization.Properties = null; diff --git a/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkScopeStore.cs b/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkScopeStore.cs index f510625e..f81c5618 100644 --- a/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkScopeStore.cs +++ b/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkScopeStore.cs @@ -402,7 +402,7 @@ public class OpenIddictEntityFrameworkScopeStore : IOpen public virtual ValueTask SetDescriptionsAsync(TScope scope!!, ImmutableDictionary descriptions, CancellationToken cancellationToken) { - if (descriptions is null || descriptions.IsEmpty) + if (descriptions is not { Count: > 0 }) { scope.Descriptions = null; @@ -444,7 +444,7 @@ public class OpenIddictEntityFrameworkScopeStore : IOpen public virtual ValueTask SetDisplayNamesAsync(TScope scope!!, ImmutableDictionary names, CancellationToken cancellationToken) { - if (names is null || names.IsEmpty) + if (names is not { Count: > 0 }) { scope.DisplayNames = null; @@ -486,7 +486,7 @@ public class OpenIddictEntityFrameworkScopeStore : IOpen public virtual ValueTask SetPropertiesAsync(TScope scope!!, ImmutableDictionary properties, CancellationToken cancellationToken) { - if (properties is not { IsEmpty: false }) + if (properties is not { Count: > 0 }) { scope.Properties = null; diff --git a/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkTokenStore.cs b/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkTokenStore.cs index 4f35080d..7ebf9d68 100644 --- a/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkTokenStore.cs +++ b/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkTokenStore.cs @@ -530,13 +530,9 @@ public class OpenIddictEntityFrameworkTokenStore { - var root = OpenIddictHelpers.FindGenericBaseType(key, typeof(OpenIddictEntityFrameworkCoreApplication<,,>)); - if (root is null) - { + var root = OpenIddictHelpers.FindGenericBaseType(key, typeof(OpenIddictEntityFrameworkCoreApplication<,,>)) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0252)); - } - var context = _options.CurrentValue.DbContextType; - if (context is null) - { + var context = _options.CurrentValue.DbContextType ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0253)); - } return typeof(OpenIddictEntityFrameworkCoreApplicationStore<,,,,>).MakeGenericType( /* TApplication: */ key, diff --git a/src/OpenIddict.EntityFrameworkCore/Resolvers/OpenIddictEntityFrameworkCoreAuthorizationStoreResolver.cs b/src/OpenIddict.EntityFrameworkCore/Resolvers/OpenIddictEntityFrameworkCoreAuthorizationStoreResolver.cs index 0b253f87..2212f801 100644 --- a/src/OpenIddict.EntityFrameworkCore/Resolvers/OpenIddictEntityFrameworkCoreAuthorizationStoreResolver.cs +++ b/src/OpenIddict.EntityFrameworkCore/Resolvers/OpenIddictEntityFrameworkCoreAuthorizationStoreResolver.cs @@ -47,17 +47,11 @@ public class OpenIddictEntityFrameworkCoreAuthorizationStoreResolver : IOpenIddi var type = _cache.GetOrAdd(typeof(TAuthorization), key => { - var root = OpenIddictHelpers.FindGenericBaseType(key, typeof(OpenIddictEntityFrameworkCoreAuthorization<,,>)); - if (root is null) - { + var root = OpenIddictHelpers.FindGenericBaseType(key, typeof(OpenIddictEntityFrameworkCoreAuthorization<,,>)) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0254)); - } - var context = _options.CurrentValue.DbContextType; - if (context is null) - { + var context = _options.CurrentValue.DbContextType ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0253)); - } return typeof(OpenIddictEntityFrameworkCoreAuthorizationStore<,,,,>).MakeGenericType( /* TAuthorization: */ key, diff --git a/src/OpenIddict.EntityFrameworkCore/Resolvers/OpenIddictEntityFrameworkCoreScopeStoreResolver.cs b/src/OpenIddict.EntityFrameworkCore/Resolvers/OpenIddictEntityFrameworkCoreScopeStoreResolver.cs index 691fbb72..ccb2cf97 100644 --- a/src/OpenIddict.EntityFrameworkCore/Resolvers/OpenIddictEntityFrameworkCoreScopeStoreResolver.cs +++ b/src/OpenIddict.EntityFrameworkCore/Resolvers/OpenIddictEntityFrameworkCoreScopeStoreResolver.cs @@ -47,17 +47,11 @@ public class OpenIddictEntityFrameworkCoreScopeStoreResolver : IOpenIddictScopeS var type = _cache.GetOrAdd(typeof(TScope), key => { - var root = OpenIddictHelpers.FindGenericBaseType(key, typeof(OpenIddictEntityFrameworkCoreScope<>)); - if (root is null) - { + var root = OpenIddictHelpers.FindGenericBaseType(key, typeof(OpenIddictEntityFrameworkCoreScope<>)) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0255)); - } - var context = _options.CurrentValue.DbContextType; - if (context is null) - { + var context = _options.CurrentValue.DbContextType ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0253)); - } return typeof(OpenIddictEntityFrameworkCoreScopeStore<,,>).MakeGenericType( /* TScope: */ key, diff --git a/src/OpenIddict.EntityFrameworkCore/Resolvers/OpenIddictEntityFrameworkCoreTokenStoreResolver.cs b/src/OpenIddict.EntityFrameworkCore/Resolvers/OpenIddictEntityFrameworkCoreTokenStoreResolver.cs index 91f13ba7..54e66729 100644 --- a/src/OpenIddict.EntityFrameworkCore/Resolvers/OpenIddictEntityFrameworkCoreTokenStoreResolver.cs +++ b/src/OpenIddict.EntityFrameworkCore/Resolvers/OpenIddictEntityFrameworkCoreTokenStoreResolver.cs @@ -47,17 +47,11 @@ public class OpenIddictEntityFrameworkCoreTokenStoreResolver : IOpenIddictTokenS var type = _cache.GetOrAdd(typeof(TToken), key => { - var root = OpenIddictHelpers.FindGenericBaseType(key, typeof(OpenIddictEntityFrameworkCoreToken<,,>)); - if (root is null) - { + var root = OpenIddictHelpers.FindGenericBaseType(key, typeof(OpenIddictEntityFrameworkCoreToken<,,>)) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0256)); - } - var context = _options.CurrentValue.DbContextType; - if (context is null) - { + var context = _options.CurrentValue.DbContextType ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0253)); - } return typeof(OpenIddictEntityFrameworkCoreTokenStore<,,,,>).MakeGenericType( /* TToken: */ key, diff --git a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreApplicationStore.cs b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreApplicationStore.cs index 96a60152..e644c35e 100644 --- a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreApplicationStore.cs +++ b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreApplicationStore.cs @@ -649,7 +649,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore names, CancellationToken cancellationToken) { - if (names is null || names.IsEmpty) + if (names is not { Count: > 0 }) { application.DisplayNames = null; @@ -748,7 +748,7 @@ public class OpenIddictEntityFrameworkCoreApplicationStore properties, CancellationToken cancellationToken) { - if (properties is not { IsEmpty: false }) + if (properties is not { Count: > 0 }) { application.Properties = null; diff --git a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreAuthorizationStore.cs b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreAuthorizationStore.cs index c903a0d4..46e39163 100644 --- a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreAuthorizationStore.cs +++ b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreAuthorizationStore.cs @@ -671,17 +671,10 @@ public class OpenIddictEntityFrameworkCoreAuthorizationStore application.Id!.Equals(key), cancellationToken); - - if (application is null) - { + authorization.Application = await Applications.AsQueryable() + .AsTracking() + .FirstOrDefaultAsync(application => application.Id!.Equals(key), cancellationToken) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0244)); - } - - authorization.Application = application; } else @@ -715,7 +708,7 @@ public class OpenIddictEntityFrameworkCoreAuthorizationStore properties, CancellationToken cancellationToken) { - if (properties is not { IsEmpty: false }) + if (properties is not { Count: > 0 }) { authorization.Properties = null; diff --git a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreScopeStore.cs b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreScopeStore.cs index 43b59e24..12590ba0 100644 --- a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreScopeStore.cs +++ b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreScopeStore.cs @@ -416,7 +416,7 @@ public class OpenIddictEntityFrameworkCoreScopeStore : I public virtual ValueTask SetDescriptionsAsync(TScope scope!!, ImmutableDictionary descriptions, CancellationToken cancellationToken) { - if (descriptions is null || descriptions.IsEmpty) + if (descriptions is not { Count: > 0 }) { scope.Descriptions = null; @@ -458,7 +458,7 @@ public class OpenIddictEntityFrameworkCoreScopeStore : I public virtual ValueTask SetDisplayNamesAsync(TScope scope!!, ImmutableDictionary names, CancellationToken cancellationToken) { - if (names is null || names.IsEmpty) + if (names is not { Count: > 0 }) { scope.DisplayNames = null; @@ -500,7 +500,7 @@ public class OpenIddictEntityFrameworkCoreScopeStore : I public virtual ValueTask SetPropertiesAsync(TScope scope!!, ImmutableDictionary properties, CancellationToken cancellationToken) { - if (properties is not { IsEmpty: false }) + if (properties is not { Count: > 0 }) { scope.Properties = null; diff --git a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreTokenStore.cs b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreTokenStore.cs index 8d23a2cc..bcddf543 100644 --- a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreTokenStore.cs +++ b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreTokenStore.cs @@ -594,17 +594,10 @@ public class OpenIddictEntityFrameworkCoreTokenStore application.Id!.Equals(key), cancellationToken); - - if (application is null) - { + token.Application = await Applications.AsQueryable() + .AsTracking() + .FirstOrDefaultAsync(application => application.Id!.Equals(key), cancellationToken) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0250)); - } - - token.Application = application; } else @@ -634,17 +627,10 @@ public class OpenIddictEntityFrameworkCoreTokenStore authorization.Id!.Equals(key), cancellationToken); - - if (authorization is null) - { + token.Authorization = await Authorizations.AsQueryable() + .AsTracking() + .FirstOrDefaultAsync(authorization => authorization.Id!.Equals(key), cancellationToken) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0251)); - } - - token.Authorization = authorization; } else diff --git a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbApplicationStore.cs b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbApplicationStore.cs index 5dc6d2ca..2b398f79 100644 --- a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbApplicationStore.cs +++ b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbApplicationStore.cs @@ -387,7 +387,7 @@ public class OpenIddictMongoDbApplicationStore : IOpenIddictApplic public virtual ValueTask SetPropertiesAsync(TApplication application!!, ImmutableDictionary properties, CancellationToken cancellationToken) { - if (properties is not { IsEmpty: false }) + if (properties is not { Count: > 0 }) { application.Properties = null; diff --git a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbAuthorizationStore.cs b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbAuthorizationStore.cs index d617d1e3..c50331bb 100644 --- a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbAuthorizationStore.cs +++ b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbAuthorizationStore.cs @@ -509,7 +509,7 @@ public class OpenIddictMongoDbAuthorizationStore : IOpenIddictAu public virtual ValueTask SetPropertiesAsync(TAuthorization authorization!!, ImmutableDictionary properties, CancellationToken cancellationToken) { - if (properties is not { IsEmpty: false }) + if (properties is not { Count: > 0 }) { authorization.Properties = null; diff --git a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbScopeStore.cs b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbScopeStore.cs index ba2ee92e..0f61a160 100644 --- a/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbScopeStore.cs +++ b/src/OpenIddict.MongoDb/Stores/OpenIddictMongoDbScopeStore.cs @@ -319,7 +319,7 @@ public class OpenIddictMongoDbScopeStore : IOpenIddictScopeStore public virtual ValueTask SetPropertiesAsync(TScope scope!!, ImmutableDictionary properties, CancellationToken cancellationToken) { - if (properties is not { IsEmpty: false }) + if (properties is not { Count: > 0 }) { scope.Properties = null; diff --git a/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreHandlers.cs b/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreHandlers.cs index cd8b5fe3..9d17b52c 100644 --- a/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreHandlers.cs +++ b/src/OpenIddict.Server.AspNetCore/OpenIddictServerAspNetCoreHandlers.cs @@ -1134,7 +1134,7 @@ public static partial class OpenIddictServerAspNetCoreHandlers // If it was not registered or enabled, let the default OpenIddict server handlers render // a default error page instead of delegating the rendering to the status code middleware. var feature = response.HttpContext.Features.Get(); - if (feature is null || !feature.Enabled) + if (feature is not { Enabled: true }) { return default; } diff --git a/src/OpenIddict.Server.Owin/OpenIddictServerOwinHandler.cs b/src/OpenIddict.Server.Owin/OpenIddictServerOwinHandler.cs index 1e00b1a7..1077d781 100644 --- a/src/OpenIddict.Server.Owin/OpenIddictServerOwinHandler.cs +++ b/src/OpenIddict.Server.Owin/OpenIddictServerOwinHandler.cs @@ -109,11 +109,8 @@ public class OpenIddictServerOwinHandler : AuthenticationHandler protected override async Task AuthenticateCoreAsync() { - var transaction = Context.Get(typeof(OpenIddictServerTransaction).FullName); - if (transaction is null) - { + var transaction = Context.Get(typeof(OpenIddictServerTransaction).FullName) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0112)); - } // Note: in many cases, the authentication token was already validated by the time this action is called // (generally later in the pipeline, when using the pass-through mode). To avoid having to re-validate it, diff --git a/src/OpenIddict.Server.Owin/OpenIddictServerOwinMiddlewareFactory.cs b/src/OpenIddict.Server.Owin/OpenIddictServerOwinMiddlewareFactory.cs index bf08dd2a..20b42032 100644 --- a/src/OpenIddict.Server.Owin/OpenIddictServerOwinMiddlewareFactory.cs +++ b/src/OpenIddict.Server.Owin/OpenIddictServerOwinMiddlewareFactory.cs @@ -35,11 +35,8 @@ public class OpenIddictServerOwinMiddlewareFactory : OwinMiddleware /// public override Task Invoke(IOwinContext context!!) { - var provider = context.Get(typeof(IServiceProvider).FullName); - if (provider is null) - { + var provider = context.Get(typeof(IServiceProvider).FullName) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0121)); - } // Note: the Microsoft.Extensions.DependencyInjection container doesn't support resolving services // with arbitrary parameters, which prevents the server OWIN middleware from being resolved directly diff --git a/src/OpenIddict.Server/OpenIddictServerBuilder.cs b/src/OpenIddict.Server/OpenIddictServerBuilder.cs index a1da48d2..33d88020 100644 --- a/src/OpenIddict.Server/OpenIddictServerBuilder.cs +++ b/src/OpenIddict.Server/OpenIddictServerBuilder.cs @@ -381,11 +381,8 @@ public class OpenIddictServerBuilder throw new ArgumentException(SR.GetResourceString(SR.ID0062), nameof(resource)); } - using var stream = assembly.GetManifestResourceStream(resource); - if (stream is null) - { + using var stream = assembly.GetManifestResourceStream(resource) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0064)); - } return AddEncryptionCertificate(stream, password, flags); } @@ -438,13 +435,10 @@ public class OpenIddictServerBuilder throw new ArgumentException(SR.GetResourceString(SR.ID0065), nameof(thumbprint)); } - var certificate = GetCertificate(StoreLocation.CurrentUser, thumbprint) ?? GetCertificate(StoreLocation.LocalMachine, thumbprint); - if (certificate is null) - { - throw new InvalidOperationException(SR.GetResourceString(SR.ID0066)); - } - - return AddEncryptionCertificate(certificate); + return AddEncryptionCertificate( + GetCertificate(StoreLocation.CurrentUser, thumbprint) ?? + GetCertificate(StoreLocation.LocalMachine, thumbprint) ?? + throw new InvalidOperationException(SR.GetResourceString(SR.ID0066))); static X509Certificate2? GetCertificate(StoreLocation location, string thumbprint) { @@ -474,16 +468,10 @@ public class OpenIddictServerBuilder using var store = new X509Store(name, location); store.Open(OpenFlags.ReadOnly); - var certificate = store.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, validOnly: false) - .OfType() - .SingleOrDefault(); - - if (certificate is null) - { - throw new InvalidOperationException(SR.GetResourceString(SR.ID0066)); - } - - return AddEncryptionCertificate(certificate); + return AddEncryptionCertificate( + store.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, validOnly: false) + .OfType() + .SingleOrDefault() ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0066))); } /// @@ -789,11 +777,8 @@ public class OpenIddictServerBuilder throw new ArgumentException(SR.GetResourceString(SR.ID0062), nameof(resource)); } - using var stream = assembly.GetManifestResourceStream(resource); - if (stream is null) - { + using var stream = assembly.GetManifestResourceStream(resource) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0064)); - } return AddSigningCertificate(stream, password, flags); } @@ -846,13 +831,10 @@ public class OpenIddictServerBuilder throw new ArgumentException(SR.GetResourceString(SR.ID0065), nameof(thumbprint)); } - var certificate = GetCertificate(StoreLocation.CurrentUser, thumbprint) ?? GetCertificate(StoreLocation.LocalMachine, thumbprint); - if (certificate is null) - { - throw new InvalidOperationException(SR.GetResourceString(SR.ID0066)); - } - - return AddSigningCertificate(certificate); + return AddSigningCertificate( + GetCertificate(StoreLocation.CurrentUser, thumbprint) ?? + GetCertificate(StoreLocation.LocalMachine, thumbprint) ?? + throw new InvalidOperationException(SR.GetResourceString(SR.ID0066))); static X509Certificate2? GetCertificate(StoreLocation location, string thumbprint) { @@ -882,16 +864,11 @@ public class OpenIddictServerBuilder using var store = new X509Store(name, location); store.Open(OpenFlags.ReadOnly); - var certificate = store.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, validOnly: false) - .OfType() - .SingleOrDefault(); - if (certificate is null) - { - throw new InvalidOperationException(SR.GetResourceString(SR.ID0066)); - } - - return AddSigningCertificate(certificate); + return AddSigningCertificate( + store.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, validOnly: false) + .OfType() + .SingleOrDefault() ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0066))); } /// diff --git a/src/OpenIddict.Server/OpenIddictServerDispatcher.cs b/src/OpenIddict.Server/OpenIddictServerDispatcher.cs index eee36712..63e49ad8 100644 --- a/src/OpenIddict.Server/OpenIddictServerDispatcher.cs +++ b/src/OpenIddict.Server/OpenIddictServerDispatcher.cs @@ -93,16 +93,15 @@ public class OpenIddictServerDispatcher : IOpenIddictServerDispatcher continue; } - var handler = descriptor.ServiceDescriptor.ImplementationInstance is not null ? - descriptor.ServiceDescriptor.ImplementationInstance as IOpenIddictServerHandler : - _provider.GetService(descriptor.ServiceDescriptor.ServiceType) as IOpenIddictServerHandler; - - if (handler is null) + yield return descriptor.ServiceDescriptor switch { - throw new InvalidOperationException(SR.FormatID0098(descriptor.ServiceDescriptor.ServiceType)); - } + { ImplementationInstance: IOpenIddictServerHandler handler } => handler, + + _ when _provider.GetService(descriptor.ServiceDescriptor.ServiceType) + is IOpenIddictServerHandler handler => handler, - yield return handler; + _ => throw new InvalidOperationException(SR.FormatID0098(descriptor.ServiceDescriptor.ServiceType)) + }; } } diff --git a/src/OpenIddict.Server/OpenIddictServerHandlers.Authentication.cs b/src/OpenIddict.Server/OpenIddictServerHandlers.Authentication.cs index b853f67b..34e75f54 100644 --- a/src/OpenIddict.Server/OpenIddictServerHandlers.Authentication.cs +++ b/src/OpenIddict.Server/OpenIddictServerHandlers.Authentication.cs @@ -1001,11 +1001,8 @@ public static partial class OpenIddictServerHandlers { Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); - var application = await _applicationManager.FindByClientIdAsync(context.ClientId); - if (application is null) - { + var application = await _applicationManager.FindByClientIdAsync(context.ClientId) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0032)); - } // To prevent downgrade attacks, ensure that authorization requests returning an access token directly // from the authorization endpoint are rejected if the client_id corresponds to a confidential application @@ -1061,11 +1058,8 @@ public static partial class OpenIddictServerHandlers { Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); - var application = await _applicationManager.FindByClientIdAsync(context.ClientId); - if (application is null) - { + var application = await _applicationManager.FindByClientIdAsync(context.ClientId) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0032)); - } // If no explicit redirect_uri was specified, retrieve the addresses associated with // the client and ensure exactly one redirect_uri was attached to the client definition. @@ -1208,11 +1202,8 @@ public static partial class OpenIddictServerHandlers { Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); - var application = await _applicationManager.FindByClientIdAsync(context.ClientId); - if (application is null) - { + var application = await _applicationManager.FindByClientIdAsync(context.ClientId) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0032)); - } // Reject the request if the application is not allowed to use the authorization endpoint. if (!await _applicationManager.HasPermissionAsync(application, Permissions.Endpoints.Authorization)) @@ -1259,11 +1250,8 @@ public static partial class OpenIddictServerHandlers { Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); - var application = await _applicationManager.FindByClientIdAsync(context.ClientId); - if (application is null) - { + var application = await _applicationManager.FindByClientIdAsync(context.ClientId) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0032)); - } // Reject the request if the application is not allowed to use the authorization code grant. if (context.Request.IsAuthorizationCodeFlow() && @@ -1355,11 +1343,8 @@ public static partial class OpenIddictServerHandlers { Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); - var application = await _applicationManager.FindByClientIdAsync(context.ClientId); - if (application is null) - { + var application = await _applicationManager.FindByClientIdAsync(context.ClientId) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0032)); - } // Reject requests that specify a response_type for which no permission was granted. if (!await HasPermissionAsync(context.Request.GetResponseTypes())) @@ -1433,11 +1418,8 @@ public static partial class OpenIddictServerHandlers { Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); - var application = await _applicationManager.FindByClientIdAsync(context.ClientId); - if (application is null) - { + var application = await _applicationManager.FindByClientIdAsync(context.ClientId) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0032)); - } foreach (var scope in context.Request.GetScopes()) { @@ -1501,11 +1483,8 @@ public static partial class OpenIddictServerHandlers return; } - var application = await _applicationManager.FindByClientIdAsync(context.ClientId); - if (application is null) - { + var application = await _applicationManager.FindByClientIdAsync(context.ClientId) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0032)); - } if (await _applicationManager.HasRequirementAsync(application, Requirements.Features.ProofKeyForCodeExchange)) { diff --git a/src/OpenIddict.Server/OpenIddictServerHandlers.Device.cs b/src/OpenIddict.Server/OpenIddictServerHandlers.Device.cs index 15e7b593..e3622a3f 100644 --- a/src/OpenIddict.Server/OpenIddictServerHandlers.Device.cs +++ b/src/OpenIddict.Server/OpenIddictServerHandlers.Device.cs @@ -527,11 +527,8 @@ public static partial class OpenIddictServerHandlers { Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); - var application = await _applicationManager.FindByClientIdAsync(context.ClientId); - if (application is null) - { + var application = await _applicationManager.FindByClientIdAsync(context.ClientId) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0032)); - } if (await _applicationManager.HasClientTypeAsync(application, ClientTypes.Public)) { @@ -596,11 +593,8 @@ public static partial class OpenIddictServerHandlers { Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); - var application = await _applicationManager.FindByClientIdAsync(context.ClientId); - if (application is null) - { + var application = await _applicationManager.FindByClientIdAsync(context.ClientId) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0032)); - } // If the application is a public client, don't validate the client secret. if (await _applicationManager.HasClientTypeAsync(application, ClientTypes.Public)) @@ -656,11 +650,8 @@ public static partial class OpenIddictServerHandlers { Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); - var application = await _applicationManager.FindByClientIdAsync(context.ClientId); - if (application is null) - { + var application = await _applicationManager.FindByClientIdAsync(context.ClientId) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0032)); - } // Reject the request if the application is not allowed to use the device endpoint. if (!await _applicationManager.HasPermissionAsync(application, Permissions.Endpoints.Device)) @@ -707,11 +698,8 @@ public static partial class OpenIddictServerHandlers { Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); - var application = await _applicationManager.FindByClientIdAsync(context.ClientId); - if (application is null) - { + var application = await _applicationManager.FindByClientIdAsync(context.ClientId) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0032)); - } // Reject the request if the application is not allowed to use the device code grant. if (!await _applicationManager.HasPermissionAsync(application, Permissions.GrantTypes.DeviceCode)) @@ -775,11 +763,8 @@ public static partial class OpenIddictServerHandlers { Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); - var application = await _applicationManager.FindByClientIdAsync(context.ClientId); - if (application is null) - { + var application = await _applicationManager.FindByClientIdAsync(context.ClientId) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0032)); - } foreach (var scope in context.Request.GetScopes()) { diff --git a/src/OpenIddict.Server/OpenIddictServerHandlers.Discovery.cs b/src/OpenIddict.Server/OpenIddictServerHandlers.Discovery.cs index 497f3702..f9ab7b44 100644 --- a/src/OpenIddict.Server/OpenIddictServerHandlers.Discovery.cs +++ b/src/OpenIddict.Server/OpenIddictServerHandlers.Discovery.cs @@ -1170,7 +1170,7 @@ public static partial class OpenIddictServerHandlers return certificate.GetCertHash(algorithm); #else using var hash = CryptoConfig.CreateFromName(algorithm.Name!) as HashAlgorithm; - if (hash is null || hash is KeyedHashAlgorithm) + if (hash is null or KeyedHashAlgorithm) { throw new InvalidOperationException(SR.GetResourceString(SR.ID0217)); } diff --git a/src/OpenIddict.Server/OpenIddictServerHandlers.Exchange.cs b/src/OpenIddict.Server/OpenIddictServerHandlers.Exchange.cs index ddf4d6af..fb695f06 100644 --- a/src/OpenIddict.Server/OpenIddictServerHandlers.Exchange.cs +++ b/src/OpenIddict.Server/OpenIddictServerHandlers.Exchange.cs @@ -801,11 +801,8 @@ public static partial class OpenIddictServerHandlers { Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); - var application = await _applicationManager.FindByClientIdAsync(context.ClientId); - if (application is null) - { + var application = await _applicationManager.FindByClientIdAsync(context.ClientId) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0032)); - } if (await _applicationManager.HasClientTypeAsync(application, ClientTypes.Public)) { @@ -883,11 +880,8 @@ public static partial class OpenIddictServerHandlers { Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); - var application = await _applicationManager.FindByClientIdAsync(context.ClientId); - if (application is null) - { + var application = await _applicationManager.FindByClientIdAsync(context.ClientId) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0032)); - } // If the application is a public client, don't validate the client secret. if (await _applicationManager.HasClientTypeAsync(application, ClientTypes.Public)) @@ -943,11 +937,8 @@ public static partial class OpenIddictServerHandlers { Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); - var application = await _applicationManager.FindByClientIdAsync(context.ClientId); - if (application is null) - { + var application = await _applicationManager.FindByClientIdAsync(context.ClientId) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0032)); - } // Reject the request if the application is not allowed to use the token endpoint. if (!await _applicationManager.HasPermissionAsync(application, Permissions.Endpoints.Token)) @@ -996,11 +987,8 @@ public static partial class OpenIddictServerHandlers { Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); - var application = await _applicationManager.FindByClientIdAsync(context.ClientId); - if (application is null) - { + var application = await _applicationManager.FindByClientIdAsync(context.ClientId) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0032)); - } // Reject the request if the application is not allowed to use the specified grant type. if (!await _applicationManager.HasPermissionAsync(application, Permissions.Prefixes.GrantType + context.Request.GrantType)) @@ -1064,11 +1052,8 @@ public static partial class OpenIddictServerHandlers { Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); - var application = await _applicationManager.FindByClientIdAsync(context.ClientId); - if (application is null) - { + var application = await _applicationManager.FindByClientIdAsync(context.ClientId) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0032)); - } foreach (var scope in context.Request.GetScopes()) { @@ -1138,11 +1123,8 @@ public static partial class OpenIddictServerHandlers Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); - var application = await _applicationManager.FindByClientIdAsync(context.ClientId); - if (application is null) - { + var application = await _applicationManager.FindByClientIdAsync(context.ClientId) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0032)); - } if (await _applicationManager.HasRequirementAsync(application, Requirements.Features.ProofKeyForCodeExchange)) { diff --git a/src/OpenIddict.Server/OpenIddictServerHandlers.Introspection.cs b/src/OpenIddict.Server/OpenIddictServerHandlers.Introspection.cs index f8c4d5fd..572597cb 100644 --- a/src/OpenIddict.Server/OpenIddictServerHandlers.Introspection.cs +++ b/src/OpenIddict.Server/OpenIddictServerHandlers.Introspection.cs @@ -458,11 +458,8 @@ public static partial class OpenIddictServerHandlers { Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); - var application = await _applicationManager.FindByClientIdAsync(context.ClientId); - if (application is null) - { + var application = await _applicationManager.FindByClientIdAsync(context.ClientId) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0032)); - } if (await _applicationManager.HasClientTypeAsync(application, ClientTypes.Public)) { @@ -527,11 +524,8 @@ public static partial class OpenIddictServerHandlers { Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); - var application = await _applicationManager.FindByClientIdAsync(context.ClientId); - if (application is null) - { + var application = await _applicationManager.FindByClientIdAsync(context.ClientId) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0032)); - } // If the application is a public client, don't validate the client secret. if (await _applicationManager.HasClientTypeAsync(application, ClientTypes.Public)) @@ -587,11 +581,8 @@ public static partial class OpenIddictServerHandlers { Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); - var application = await _applicationManager.FindByClientIdAsync(context.ClientId); - if (application is null) - { + var application = await _applicationManager.FindByClientIdAsync(context.ClientId) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0032)); - } // Reject the request if the application is not allowed to use the introspection endpoint. if (!await _applicationManager.HasPermissionAsync(application, Permissions.Endpoints.Introspection)) @@ -887,11 +878,8 @@ public static partial class OpenIddictServerHandlers return; } - var application = await _applicationManager.FindByClientIdAsync(context.Request.ClientId); - if (application is null) - { + var application = await _applicationManager.FindByClientIdAsync(context.Request.ClientId) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0032)); - } // Public clients are not allowed to access sensitive claims as authentication cannot be enforced. if (await _applicationManager.HasClientTypeAsync(application, ClientTypes.Public)) diff --git a/src/OpenIddict.Server/OpenIddictServerHandlers.Protection.cs b/src/OpenIddict.Server/OpenIddictServerHandlers.Protection.cs index 638ae209..617f826e 100644 --- a/src/OpenIddict.Server/OpenIddictServerHandlers.Protection.cs +++ b/src/OpenIddict.Server/OpenIddictServerHandlers.Protection.cs @@ -545,11 +545,8 @@ public static partial class OpenIddictServerHandlers return; } - var token = await _tokenManager.FindByIdAsync(context.TokenId); - if (token is null) - { + var token = await _tokenManager.FindByIdAsync(context.TokenId) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0021)); - } // Restore the creation/expiration dates/identifiers from the token entry metadata. context.Principal.SetCreationDate(await _tokenManager.GetCreationDateAsync(token)) @@ -1060,20 +1057,14 @@ public static partial class OpenIddictServerHandlers // If the client application is known, associate it with the token. if (!string.IsNullOrEmpty(context.ClientId)) { - var application = await _applicationManager.FindByClientIdAsync(context.ClientId); - if (application is null) - { + var application = await _applicationManager.FindByClientIdAsync(context.ClientId) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0017)); - } descriptor.ApplicationId = await _applicationManager.GetIdAsync(application); } - var token = await _tokenManager.CreateAsync(descriptor); - if (token is null) - { + var token = await _tokenManager.CreateAsync(descriptor) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0019)); - } var identifier = await _tokenManager.GetIdAsync(token); @@ -1237,11 +1228,8 @@ public static partial class OpenIddictServerHandlers throw new InvalidOperationException(SR.GetResourceString(SR.ID0009)); } - var token = await _tokenManager.FindByIdAsync(identifier); - if (token is null) - { + var token = await _tokenManager.FindByIdAsync(identifier) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0021)); - } var descriptor = new OpenIddictTokenDescriptor(); await _tokenManager.PopulateAsync(descriptor, token); diff --git a/src/OpenIddict.Server/OpenIddictServerHandlers.Revocation.cs b/src/OpenIddict.Server/OpenIddictServerHandlers.Revocation.cs index c7eccdb2..9c3962a7 100644 --- a/src/OpenIddict.Server/OpenIddictServerHandlers.Revocation.cs +++ b/src/OpenIddict.Server/OpenIddictServerHandlers.Revocation.cs @@ -405,11 +405,8 @@ public static partial class OpenIddictServerHandlers { Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); - var application = await _applicationManager.FindByClientIdAsync(context.ClientId); - if (application is null) - { + var application = await _applicationManager.FindByClientIdAsync(context.ClientId) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0032)); - } if (await _applicationManager.HasClientTypeAsync(application, ClientTypes.Public)) { @@ -474,11 +471,8 @@ public static partial class OpenIddictServerHandlers { Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); - var application = await _applicationManager.FindByClientIdAsync(context.ClientId); - if (application is null) - { + var application = await _applicationManager.FindByClientIdAsync(context.ClientId) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0032)); - } // If the application is a public client, don't validate the client secret. if (await _applicationManager.HasClientTypeAsync(application, ClientTypes.Public)) @@ -534,11 +528,8 @@ public static partial class OpenIddictServerHandlers { Debug.Assert(!string.IsNullOrEmpty(context.ClientId), SR.FormatID4000(Parameters.ClientId)); - var application = await _applicationManager.FindByClientIdAsync(context.ClientId); - if (application is null) - { + var application = await _applicationManager.FindByClientIdAsync(context.ClientId) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0032)); - } // Reject the request if the application is not allowed to use the revocation endpoint. if (!await _applicationManager.HasPermissionAsync(application, Permissions.Endpoints.Revocation)) diff --git a/src/OpenIddict.Server/OpenIddictServerHandlers.cs b/src/OpenIddict.Server/OpenIddictServerHandlers.cs index 27475abe..1e2cbb5a 100644 --- a/src/OpenIddict.Server/OpenIddictServerHandlers.cs +++ b/src/OpenIddict.Server/OpenIddictServerHandlers.cs @@ -1427,20 +1427,14 @@ public static partial class OpenIddictServerHandlers // If the client application is known, associate it to the authorization. if (!string.IsNullOrEmpty(context.Request.ClientId)) { - var application = await _applicationManager.FindByClientIdAsync(context.Request.ClientId); - if (application is null) - { + var application = await _applicationManager.FindByClientIdAsync(context.Request.ClientId) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0017)); - } descriptor.ApplicationId = await _applicationManager.GetIdAsync(application); } - var authorization = await _authorizationManager.CreateAsync(descriptor); - if (authorization is null) - { + var authorization = await _authorizationManager.CreateAsync(descriptor) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0018)); - } var identifier = await _authorizationManager.GetIdAsync(authorization); @@ -2368,11 +2362,8 @@ public static partial class OpenIddictServerHandlers throw new InvalidOperationException(SR.GetResourceString(SR.ID0008)); } - var token = await _tokenManager.FindByIdAsync(identifier); - if (token is null) - { + var token = await _tokenManager.FindByIdAsync(identifier) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0265)); - } // Replace the device code details by the payload derived from the new device code principal, // that includes all the user claims populated by the application after authenticating the user. @@ -2423,14 +2414,11 @@ public static partial class OpenIddictServerHandlers } var credentials = context.Options.SigningCredentials.Find( - credentials => credentials.Key is AsymmetricSecurityKey); - if (credentials is null) - { + credentials => credentials.Key is AsymmetricSecurityKey) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0266)); - } using var algorithm = GetHashAlgorithm(credentials); - if (algorithm is null || algorithm is KeyedHashAlgorithm) + if (algorithm is null or KeyedHashAlgorithm) { throw new InvalidOperationException(SR.GetResourceString(SR.ID0267)); } diff --git a/src/OpenIddict.Validation.Owin/OpenIddictValidationOwinMiddlewareFactory.cs b/src/OpenIddict.Validation.Owin/OpenIddictValidationOwinMiddlewareFactory.cs index cf230953..11403181 100644 --- a/src/OpenIddict.Validation.Owin/OpenIddictValidationOwinMiddlewareFactory.cs +++ b/src/OpenIddict.Validation.Owin/OpenIddictValidationOwinMiddlewareFactory.cs @@ -35,11 +35,8 @@ public class OpenIddictValidationOwinMiddlewareFactory : OwinMiddleware /// public override Task Invoke(IOwinContext context!!) { - var provider = context.Get(typeof(IServiceProvider).FullName); - if (provider is null) - { + var provider = context.Get(typeof(IServiceProvider).FullName) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0168)); - } // Note: the Microsoft.Extensions.DependencyInjection container doesn't support resolving services // with arbitrary parameters, which prevents the validation OWIN middleware from being resolved directly diff --git a/src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpHandlers.cs b/src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpHandlers.cs index ed019a78..fb99128e 100644 --- a/src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpHandlers.cs +++ b/src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpHandlers.cs @@ -221,21 +221,15 @@ public static partial class OpenIddictValidationSystemNetHttpHandlers throw new InvalidOperationException(SR.GetResourceString(SR.ID0173)); var assembly = typeof(OpenIddictValidationSystemNetHttpOptions).Assembly.GetName(); - using var client = _factory.CreateClient(assembly.Name!); - if (client is null) - { + using var client = _factory.CreateClient(assembly.Name!) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0174)); - } #if SUPPORTS_HTTP_CLIENT_DEFAULT_REQUEST_VERSION // If supported, import the HTTP version from the client instance. request.Version = client.DefaultRequestVersion; #endif - var response = await client.SendAsync(request, HttpCompletionOption.ResponseContentRead); - if (response is null) - { + var response = await client.SendAsync(request, HttpCompletionOption.ResponseContentRead) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0175)); - } // Store the HttpResponseMessage in the transaction properties. context.Transaction.SetProperty(typeof(HttpResponseMessage).FullName!, response); diff --git a/src/OpenIddict.Validation/OpenIddictValidationBuilder.cs b/src/OpenIddict.Validation/OpenIddictValidationBuilder.cs index 0e2c7a8a..b041aaca 100644 --- a/src/OpenIddict.Validation/OpenIddictValidationBuilder.cs +++ b/src/OpenIddict.Validation/OpenIddictValidationBuilder.cs @@ -202,11 +202,8 @@ public class OpenIddictValidationBuilder throw new ArgumentException(SR.GetResourceString(SR.ID0062), nameof(resource)); } - using var stream = assembly.GetManifestResourceStream(resource); - if (stream is null) - { + using var stream = assembly.GetManifestResourceStream(resource) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0064)); - } return AddEncryptionCertificate(stream, password, flags); } @@ -260,13 +257,10 @@ public class OpenIddictValidationBuilder throw new ArgumentException(SR.GetResourceString(SR.ID0065), nameof(thumbprint)); } - var certificate = GetCertificate(StoreLocation.CurrentUser, thumbprint) ?? GetCertificate(StoreLocation.LocalMachine, thumbprint); - if (certificate is null) - { - throw new InvalidOperationException(SR.GetResourceString(SR.ID0066)); - } - - return AddEncryptionCertificate(certificate); + return AddEncryptionCertificate( + GetCertificate(StoreLocation.CurrentUser, thumbprint) ?? + GetCertificate(StoreLocation.LocalMachine, thumbprint) ?? + throw new InvalidOperationException(SR.GetResourceString(SR.ID0066))); static X509Certificate2? GetCertificate(StoreLocation location, string thumbprint) { @@ -297,16 +291,11 @@ public class OpenIddictValidationBuilder using var store = new X509Store(name, location); store.Open(OpenFlags.ReadOnly); - var certificate = store.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, validOnly: false) - .OfType() - .SingleOrDefault(); - if (certificate is null) - { - throw new InvalidOperationException(SR.GetResourceString(SR.ID0066)); - } - - return AddEncryptionCertificate(certificate); + return AddEncryptionCertificate( + store.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, validOnly: false) + .OfType() + .SingleOrDefault() ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0066))); } /// diff --git a/src/OpenIddict.Validation/OpenIddictValidationDispatcher.cs b/src/OpenIddict.Validation/OpenIddictValidationDispatcher.cs index 0b718f14..f344e3cd 100644 --- a/src/OpenIddict.Validation/OpenIddictValidationDispatcher.cs +++ b/src/OpenIddict.Validation/OpenIddictValidationDispatcher.cs @@ -93,16 +93,15 @@ public class OpenIddictValidationDispatcher : IOpenIddictValidationDispatcher continue; } - var handler = descriptor.ServiceDescriptor.ImplementationInstance is not null ? - descriptor.ServiceDescriptor.ImplementationInstance as IOpenIddictValidationHandler : - _provider.GetService(descriptor.ServiceDescriptor.ServiceType) as IOpenIddictValidationHandler; - - if (handler is null) + yield return descriptor.ServiceDescriptor switch { - throw new InvalidOperationException(SR.FormatID0138(descriptor.ServiceDescriptor.ServiceType)); - } + { ImplementationInstance: IOpenIddictValidationHandler handler } => handler, + + _ when _provider.GetService(descriptor.ServiceDescriptor.ServiceType) + is IOpenIddictValidationHandler handler => handler, - yield return handler; + _ => throw new InvalidOperationException(SR.FormatID0138(descriptor.ServiceDescriptor.ServiceType)) + }; } } diff --git a/src/OpenIddict.Validation/OpenIddictValidationHandlers.Protection.cs b/src/OpenIddict.Validation/OpenIddictValidationHandlers.Protection.cs index e12f4748..56711cd3 100644 --- a/src/OpenIddict.Validation/OpenIddictValidationHandlers.Protection.cs +++ b/src/OpenIddict.Validation/OpenIddictValidationHandlers.Protection.cs @@ -545,11 +545,8 @@ public static partial class OpenIddictValidationHandlers return; } - var token = await _tokenManager.FindByIdAsync(context.TokenId); - if (token is null) - { + var token = await _tokenManager.FindByIdAsync(context.TokenId) ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0021)); - } // Restore the creation/expiration dates/identifiers from the token entry metadata. context.Principal.SetCreationDate(await _tokenManager.GetCreationDateAsync(token)) diff --git a/src/OpenIddict.Validation/OpenIddictValidationService.cs b/src/OpenIddict.Validation/OpenIddictValidationService.cs index 4160b935..fcee2ae6 100644 --- a/src/OpenIddict.Validation/OpenIddictValidationService.cs +++ b/src/OpenIddict.Validation/OpenIddictValidationService.cs @@ -55,13 +55,8 @@ public class OpenIddictValidationService request = await ApplyConfigurationRequestAsync(); var response = await ExtractConfigurationResponseAsync(); - var configuration = await HandleConfigurationResponseAsync(); - if (configuration is null) - { + return await HandleConfigurationResponseAsync() ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0145)); - } - - return configuration; async ValueTask PrepareConfigurationRequestAsync() { @@ -195,13 +190,8 @@ public class OpenIddictValidationService var response = await ExtractCryptographyResponseAsync(); - var keys = await HandleCryptographyResponseAsync(); - if (keys is null) - { + return await HandleCryptographyResponseAsync() ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0147)); - } - - return keys; async ValueTask PrepareCryptographyRequestAsync() { @@ -351,13 +341,8 @@ public class OpenIddictValidationService request = await ApplyIntrospectionRequestAsync(); var response = await ExtractIntrospectionResponseAsync(); - var principal = await HandleIntrospectionResponseAsync(); - if (principal is null) - { + return await HandleIntrospectionResponseAsync() ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0157)); - } - - return principal; async ValueTask PrepareIntrospectionRequestAsync() { diff --git a/test/OpenIddict.Server.IntegrationTests/OpenIddictServerIntegrationTestClient.cs b/test/OpenIddict.Server.IntegrationTests/OpenIddictServerIntegrationTestClient.cs index 008491d6..25b447a8 100644 --- a/test/OpenIddict.Server.IntegrationTests/OpenIddictServerIntegrationTestClient.cs +++ b/test/OpenIddict.Server.IntegrationTests/OpenIddictServerIntegrationTestClient.cs @@ -211,7 +211,7 @@ public class OpenIddictServerIntegrationTestClient : IAsyncDisposable } var values = (string?[]?) parameter.Value; - if (values is null || values.Length == 0) + if (values is not { Length: > 0 }) { continue; }