From 791552025fc2f8bc362efa0a1983174087be04c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Chalet?= Date: Fri, 26 Jun 2020 03:35:25 +0200 Subject: [PATCH] Introduce new filters to avoid instantiating scoped handlers when unnecessary --- .../OpenIddictServerDispatcher.cs | 28 ++- .../OpenIddictServerExtensions.cs | 10 ++ .../OpenIddictServerHandlerFilters.cs | 160 ++++++++++++++++++ ...OpenIddictServerHandlers.Authentication.cs | 24 +-- .../OpenIddictServerHandlers.Device.cs | 48 +----- .../OpenIddictServerHandlers.Discovery.cs | 49 +----- .../OpenIddictServerHandlers.Exchange.cs | 24 +-- .../OpenIddictServerHandlers.Introspection.cs | 24 +-- .../OpenIddictServerHandlers.Revocation.cs | 24 +-- .../OpenIddictServerHandlers.Session.cs | 24 +-- .../OpenIddictServerHandlers.Userinfo.cs | 25 +-- .../OpenIddictValidationDispatcher.cs | 28 ++- 12 files changed, 260 insertions(+), 208 deletions(-) diff --git a/src/OpenIddict.Server/OpenIddictServerDispatcher.cs b/src/OpenIddict.Server/OpenIddictServerDispatcher.cs index 6b7a04b5..9bae35c0 100644 --- a/src/OpenIddict.Server/OpenIddictServerDispatcher.cs +++ b/src/OpenIddict.Server/OpenIddictServerDispatcher.cs @@ -43,28 +43,48 @@ namespace OpenIddict.Server await foreach (var handler in GetHandlersAsync()) { - await handler.HandleAsync(context); + try + { + await handler.HandleAsync(context); + } + + catch (Exception exception) when (_logger.IsEnabled(LogLevel.Debug)) + { + _logger.LogDebug(exception, "An exception was thrown by {FullName} while handling the {Event} event.", + handler.GetType().FullName, typeof(TContext).FullName); + + throw; + } + + if (_logger.IsEnabled(LogLevel.Debug)) + { + _logger.LogDebug("The event {Event} was successfully processed by {FullName}.", + typeof(TContext).FullName, handler.GetType().FullName); + } switch (context) { case BaseRequestContext notification when notification.IsRequestHandled: if (_logger.IsEnabled(LogLevel.Debug)) { - _logger.LogDebug("The event context was marked as handled by {Name}.", handler.GetType().FullName); + _logger.LogDebug("The event {Event} was marked as handled by {FullName}.", + typeof(TContext).FullName, handler.GetType().FullName); } return; case BaseRequestContext notification when notification.IsRequestSkipped: if (_logger.IsEnabled(LogLevel.Debug)) { - _logger.LogDebug("The event context was marked as skipped by {Name}.", handler.GetType().FullName); + _logger.LogDebug("The event {Event} was marked as skipped by {FullName}.", + typeof(TContext).FullName, handler.GetType().FullName); } return; case BaseValidatingContext notification when notification.IsRejected: if (_logger.IsEnabled(LogLevel.Debug)) { - _logger.LogDebug("The event context was marked as rejected by {Name}.", handler.GetType().FullName); + _logger.LogDebug("The event {Event} was marked as rejected by {FullName}.", + typeof(TContext).FullName, handler.GetType().FullName); } return; diff --git a/src/OpenIddict.Server/OpenIddictServerExtensions.cs b/src/OpenIddict.Server/OpenIddictServerExtensions.cs index c72962de..3d6666db 100644 --- a/src/OpenIddict.Server/OpenIddictServerExtensions.cs +++ b/src/OpenIddict.Server/OpenIddictServerExtensions.cs @@ -47,23 +47,33 @@ namespace Microsoft.Extensions.DependencyInjection builder.Services.TryAddSingleton(); builder.Services.TryAddSingleton(); builder.Services.TryAddSingleton(); + builder.Services.TryAddSingleton(); builder.Services.TryAddSingleton(); + builder.Services.TryAddSingleton(); + builder.Services.TryAddSingleton(); builder.Services.TryAddSingleton(); builder.Services.TryAddSingleton(); + builder.Services.TryAddSingleton(); builder.Services.TryAddSingleton(); builder.Services.TryAddSingleton(); builder.Services.TryAddSingleton(); + builder.Services.TryAddSingleton(); + builder.Services.TryAddSingleton(); builder.Services.TryAddSingleton(); builder.Services.TryAddSingleton(); builder.Services.TryAddSingleton(); builder.Services.TryAddSingleton(); + builder.Services.TryAddSingleton(); builder.Services.TryAddSingleton(); builder.Services.TryAddSingleton(); builder.Services.TryAddSingleton(); builder.Services.TryAddSingleton(); builder.Services.TryAddSingleton(); builder.Services.TryAddSingleton(); + builder.Services.TryAddSingleton(); builder.Services.TryAddSingleton(); + builder.Services.TryAddSingleton(); + builder.Services.TryAddSingleton(); // Note: TryAddEnumerable() is used here to ensure the initializer is registered only once. builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< diff --git a/src/OpenIddict.Server/OpenIddictServerHandlerFilters.cs b/src/OpenIddict.Server/OpenIddictServerHandlerFilters.cs index 85bf1572..1824a8fe 100644 --- a/src/OpenIddict.Server/OpenIddictServerHandlerFilters.cs +++ b/src/OpenIddict.Server/OpenIddictServerHandlerFilters.cs @@ -47,6 +47,22 @@ namespace OpenIddict.Server } } + /// + /// Represents a filter that excludes the associated handlers if the request is not an authorization request. + /// + public class RequireAuthorizationRequest : IOpenIddictServerHandlerFilter + { + public ValueTask IsActiveAsync([NotNull] BaseContext context) + { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + return new ValueTask(context.EndpointType == OpenIddictServerEndpointType.Authorization); + } + } + /// /// Represents a filter that excludes the associated handlers if authorization storage was not enabled. /// @@ -79,6 +95,38 @@ namespace OpenIddict.Server } } + /// + /// Represents a filter that excludes the associated handlers if the request is not a configuration request. + /// + public class RequireConfigurationRequest : IOpenIddictServerHandlerFilter + { + public ValueTask IsActiveAsync([NotNull] BaseContext context) + { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + return new ValueTask(context.EndpointType == OpenIddictServerEndpointType.Configuration); + } + } + + /// + /// Represents a filter that excludes the associated handlers if the request is not a cryptography request. + /// + public class RequireCryptographyRequest : IOpenIddictServerHandlerFilter + { + public ValueTask IsActiveAsync([NotNull] BaseContext context) + { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + return new ValueTask(context.EndpointType == OpenIddictServerEndpointType.Cryptography); + } + } + /// /// Represents a filter that excludes the associated handlers if the degraded mode was not enabled. /// @@ -111,6 +159,22 @@ namespace OpenIddict.Server } } + /// + /// Represents a filter that excludes the associated handlers if the request is not a device request. + /// + public class RequireDeviceRequest : IOpenIddictServerHandlerFilter + { + public ValueTask IsActiveAsync([NotNull] BaseContext context) + { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + return new ValueTask(context.EndpointType == OpenIddictServerEndpointType.Device); + } + } + /// /// Represents a filter that excludes the associated handlers if endpoint permissions were disabled. /// @@ -159,6 +223,38 @@ namespace OpenIddict.Server } } + /// + /// Represents a filter that excludes the associated handlers if the request is not an introspection request. + /// + public class RequireIntrospectionRequest : IOpenIddictServerHandlerFilter + { + public ValueTask IsActiveAsync([NotNull] BaseContext context) + { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + return new ValueTask(context.EndpointType == OpenIddictServerEndpointType.Introspection); + } + } + + /// + /// Represents a filter that excludes the associated handlers if the request is not a logout request. + /// + public class RequireLogoutRequest : IOpenIddictServerHandlerFilter + { + public ValueTask IsActiveAsync([NotNull] BaseContext context) + { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + return new ValueTask(context.EndpointType == OpenIddictServerEndpointType.Logout); + } + } + /// /// Represents a filter that excludes the associated handlers when no post_logout_redirect_uri is received. /// @@ -223,6 +319,22 @@ namespace OpenIddict.Server } } + /// + /// Represents a filter that excludes the associated handlers if the request is not a revocation request. + /// + public class RequireRevocationRequest : IOpenIddictServerHandlerFilter + { + public ValueTask IsActiveAsync([NotNull] BaseContext context) + { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + return new ValueTask(context.EndpointType == OpenIddictServerEndpointType.Revocation); + } + } + /// /// Represents a filter that excludes the associated handlers if rolling tokens were enabled. /// @@ -303,6 +415,22 @@ namespace OpenIddict.Server } } + /// + /// Represents a filter that excludes the associated handlers if the request is not a token request. + /// + public class RequireTokenRequest : IOpenIddictServerHandlerFilter + { + public ValueTask IsActiveAsync([NotNull] BaseContext context) + { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + return new ValueTask(context.EndpointType == OpenIddictServerEndpointType.Token); + } + } + /// /// Represents a filter that excludes the associated handlers if token storage was not enabled. /// @@ -334,5 +462,37 @@ namespace OpenIddict.Server return new ValueTask(context.IncludeUserCode); } } + + /// + /// Represents a filter that excludes the associated handlers if the request is not a userinfo request. + /// + public class RequireUserinfoRequest : IOpenIddictServerHandlerFilter + { + public ValueTask IsActiveAsync([NotNull] BaseContext context) + { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + return new ValueTask(context.EndpointType == OpenIddictServerEndpointType.Userinfo); + } + } + + /// + /// Represents a filter that excludes the associated handlers if the request is not a verification request. + /// + public class RequireVerificationRequest : IOpenIddictServerHandlerFilter + { + public ValueTask IsActiveAsync([NotNull] BaseContext context) + { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + return new ValueTask(context.EndpointType == OpenIddictServerEndpointType.Verification); + } + } } } diff --git a/src/OpenIddict.Server/OpenIddictServerHandlers.Authentication.cs b/src/OpenIddict.Server/OpenIddictServerHandlers.Authentication.cs index 4bd48821..57566db8 100644 --- a/src/OpenIddict.Server/OpenIddictServerHandlers.Authentication.cs +++ b/src/OpenIddict.Server/OpenIddictServerHandlers.Authentication.cs @@ -79,6 +79,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler() .SetOrder(100_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -98,11 +99,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Authorization) - { - return; - } - var notification = new ExtractAuthorizationRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -155,6 +151,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler() .SetOrder(ExtractAuthorizationRequest.Descriptor.Order + 1_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -174,11 +171,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Authorization) - { - return; - } - var notification = new ValidateAuthorizationRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -231,6 +223,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler() .SetOrder(ValidateAuthorizationRequest.Descriptor.Order + 1_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -250,11 +243,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Authorization) - { - return; - } - var notification = new HandleAuthorizationRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -335,6 +323,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler>() .SetOrder(int.MaxValue - 100_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -354,11 +343,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Authorization) - { - return; - } - var notification = new ApplyAuthorizationResponseContext(context.Transaction); await _dispatcher.DispatchAsync(notification); diff --git a/src/OpenIddict.Server/OpenIddictServerHandlers.Device.cs b/src/OpenIddict.Server/OpenIddictServerHandlers.Device.cs index 4deae724..c54dd088 100644 --- a/src/OpenIddict.Server/OpenIddictServerHandlers.Device.cs +++ b/src/OpenIddict.Server/OpenIddictServerHandlers.Device.cs @@ -77,6 +77,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler() .SetOrder(100_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -96,11 +97,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Device) - { - return; - } - var notification = new ExtractDeviceRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -153,6 +149,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler() .SetOrder(ExtractDeviceRequest.Descriptor.Order + 1_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -172,11 +169,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Device) - { - return; - } - var notification = new ValidateDeviceRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -220,6 +212,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler() .SetOrder(ValidateDeviceRequest.Descriptor.Order + 1_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -239,11 +232,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Device) - { - return; - } - var notification = new HandleDeviceRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -330,6 +318,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler>() .SetOrder(int.MaxValue - 100_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -349,11 +338,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Device) - { - return; - } - var notification = new ApplyDeviceResponseContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -870,6 +854,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler() .SetOrder(100_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -889,11 +874,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Verification) - { - return; - } - var notification = new ExtractVerificationRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -946,6 +926,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler() .SetOrder(ExtractVerificationRequest.Descriptor.Order + 1_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -965,11 +946,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Verification) - { - return; - } - var notification = new ValidateVerificationRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -1013,6 +989,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler() .SetOrder(ValidateVerificationRequest.Descriptor.Order + 1_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -1032,11 +1009,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Verification) - { - return; - } - var notification = new HandleVerificationRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -1117,6 +1089,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler>() .SetOrder(int.MaxValue - 100_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -1136,11 +1109,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Verification) - { - return; - } - var notification = new ApplyVerificationResponseContext(context.Transaction); await _dispatcher.DispatchAsync(notification); diff --git a/src/OpenIddict.Server/OpenIddictServerHandlers.Discovery.cs b/src/OpenIddict.Server/OpenIddictServerHandlers.Discovery.cs index 62f5afff..9cb8bc3a 100644 --- a/src/OpenIddict.Server/OpenIddictServerHandlers.Discovery.cs +++ b/src/OpenIddict.Server/OpenIddictServerHandlers.Discovery.cs @@ -20,6 +20,7 @@ using Microsoft.IdentityModel.Tokens; using OpenIddict.Abstractions; using static OpenIddict.Abstractions.OpenIddictConstants; using static OpenIddict.Server.OpenIddictServerEvents; +using static OpenIddict.Server.OpenIddictServerHandlerFilters; namespace OpenIddict.Server { @@ -81,6 +82,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler() .SetOrder(100_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -100,11 +102,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Configuration) - { - return; - } - var notification = new ExtractConfigurationRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -157,6 +154,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler() .SetOrder(ExtractConfigurationRequest.Descriptor.Order + 1_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -176,11 +174,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Configuration) - { - return; - } - var notification = new ValidateConfigurationRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -224,6 +217,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler() .SetOrder(ValidateConfigurationRequest.Descriptor.Order + 1_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -243,11 +237,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Configuration) - { - return; - } - var notification = new HandleConfigurationRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -320,6 +309,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler>() .SetOrder(int.MaxValue - 100_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -339,11 +329,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Configuration) - { - return; - } - var notification = new ApplyConfigurationResponseContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -888,6 +873,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler() .SetOrder(100_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -907,11 +893,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Cryptography) - { - return; - } - var notification = new ExtractCryptographyRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -964,6 +945,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler() .SetOrder(ExtractCryptographyRequest.Descriptor.Order + 1_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -983,11 +965,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Cryptography) - { - return; - } - var notification = new ValidateCryptographyRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -1031,6 +1008,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler() .SetOrder(ValidateCryptographyRequest.Descriptor.Order + 1_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -1050,11 +1028,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Cryptography) - { - return; - } - var notification = new HandleCryptographyRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -1170,6 +1143,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler>() .SetOrder(int.MaxValue - 100_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -1189,11 +1163,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Cryptography) - { - return; - } - var notification = new ApplyCryptographyResponseContext(context.Transaction); await _dispatcher.DispatchAsync(notification); diff --git a/src/OpenIddict.Server/OpenIddictServerHandlers.Exchange.cs b/src/OpenIddict.Server/OpenIddictServerHandlers.Exchange.cs index 5b76ef5a..0d51e45f 100644 --- a/src/OpenIddict.Server/OpenIddictServerHandlers.Exchange.cs +++ b/src/OpenIddict.Server/OpenIddictServerHandlers.Exchange.cs @@ -84,6 +84,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler() .SetOrder(100_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -103,11 +104,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Token) - { - return; - } - var notification = new ExtractTokenRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -160,6 +156,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler() .SetOrder(ExtractTokenRequest.Descriptor.Order + 1_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -179,11 +176,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Token) - { - return; - } - var notification = new ValidateTokenRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -231,6 +223,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler() .SetOrder(ValidateTokenRequest.Descriptor.Order + 1_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -250,11 +243,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Token) - { - return; - } - var notification = new HandleTokenRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -335,6 +323,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler>() .SetOrder(int.MaxValue - 100_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -354,11 +343,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Token) - { - return; - } - var notification = new ApplyTokenResponseContext(context.Transaction); await _dispatcher.DispatchAsync(notification); diff --git a/src/OpenIddict.Server/OpenIddictServerHandlers.Introspection.cs b/src/OpenIddict.Server/OpenIddictServerHandlers.Introspection.cs index 553f433f..22258403 100644 --- a/src/OpenIddict.Server/OpenIddictServerHandlers.Introspection.cs +++ b/src/OpenIddict.Server/OpenIddictServerHandlers.Introspection.cs @@ -78,6 +78,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler() .SetOrder(100_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -97,11 +98,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Introspection) - { - return; - } - var notification = new ExtractIntrospectionRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -154,6 +150,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler() .SetOrder(ExtractIntrospectionRequest.Descriptor.Order + 1_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -173,11 +170,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Introspection) - { - return; - } - var notification = new ValidateIntrospectionRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -225,6 +217,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler() .SetOrder(ValidateIntrospectionRequest.Descriptor.Order + 1_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -244,11 +237,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Introspection) - { - return; - } - var notification = new HandleIntrospectionRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -338,6 +326,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler>() .SetOrder(int.MaxValue - 100_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -357,11 +346,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Introspection) - { - return; - } - var notification = new ApplyIntrospectionResponseContext(context.Transaction); await _dispatcher.DispatchAsync(notification); diff --git a/src/OpenIddict.Server/OpenIddictServerHandlers.Revocation.cs b/src/OpenIddict.Server/OpenIddictServerHandlers.Revocation.cs index ab35a787..24efc497 100644 --- a/src/OpenIddict.Server/OpenIddictServerHandlers.Revocation.cs +++ b/src/OpenIddict.Server/OpenIddictServerHandlers.Revocation.cs @@ -70,6 +70,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler() .SetOrder(100_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -89,11 +90,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Revocation) - { - return; - } - var notification = new ExtractRevocationRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -146,6 +142,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler() .SetOrder(ExtractRevocationRequest.Descriptor.Order + 1_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -165,11 +162,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Revocation) - { - return; - } - var notification = new ValidateRevocationRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -217,6 +209,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler() .SetOrder(ValidateRevocationRequest.Descriptor.Order + 1_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -236,11 +229,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Revocation) - { - return; - } - var notification = new HandleRevocationRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -284,6 +272,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler>() .SetOrder(int.MaxValue - 100_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -303,11 +292,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Revocation) - { - return; - } - var notification = new ApplyRevocationResponseContext(context.Transaction); await _dispatcher.DispatchAsync(notification); diff --git a/src/OpenIddict.Server/OpenIddictServerHandlers.Session.cs b/src/OpenIddict.Server/OpenIddictServerHandlers.Session.cs index 8905bf22..f2d0749f 100644 --- a/src/OpenIddict.Server/OpenIddictServerHandlers.Session.cs +++ b/src/OpenIddict.Server/OpenIddictServerHandlers.Session.cs @@ -59,6 +59,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler() .SetOrder(100_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -78,11 +79,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Logout) - { - return; - } - var notification = new ExtractLogoutRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -135,6 +131,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler() .SetOrder(ExtractLogoutRequest.Descriptor.Order + 1_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -154,11 +151,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Logout) - { - return; - } - var notification = new ValidateLogoutRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -206,6 +198,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler() .SetOrder(ValidateLogoutRequest.Descriptor.Order + 1_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -225,11 +218,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Logout) - { - return; - } - var notification = new HandleLogoutRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -306,6 +294,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler>() .SetOrder(int.MaxValue - 100_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -325,11 +314,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Logout) - { - return; - } - var notification = new ApplyLogoutResponseContext(context.Transaction); await _dispatcher.DispatchAsync(notification); diff --git a/src/OpenIddict.Server/OpenIddictServerHandlers.Userinfo.cs b/src/OpenIddict.Server/OpenIddictServerHandlers.Userinfo.cs index d37462ec..694a04f5 100644 --- a/src/OpenIddict.Server/OpenIddictServerHandlers.Userinfo.cs +++ b/src/OpenIddict.Server/OpenIddictServerHandlers.Userinfo.cs @@ -14,6 +14,7 @@ using Microsoft.Extensions.Logging; using OpenIddict.Abstractions; using static OpenIddict.Abstractions.OpenIddictConstants; using static OpenIddict.Server.OpenIddictServerEvents; +using static OpenIddict.Server.OpenIddictServerHandlerFilters; namespace OpenIddict.Server { @@ -60,6 +61,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler() .SetOrder(100_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -79,11 +81,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Userinfo) - { - return; - } - var notification = new ExtractUserinfoRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -136,6 +133,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler() .SetOrder(ExtractUserinfoRequest.Descriptor.Order + 1_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -155,11 +153,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Userinfo) - { - return; - } - var notification = new ValidateUserinfoRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -207,6 +200,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler() .SetOrder(ValidateUserinfoRequest.Descriptor.Order + 1_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -226,11 +220,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Userinfo) - { - return; - } - var notification = new HandleUserinfoRequestContext(context.Transaction); await _dispatcher.DispatchAsync(notification); @@ -309,6 +298,7 @@ namespace OpenIddict.Server /// public static OpenIddictServerHandlerDescriptor Descriptor { get; } = OpenIddictServerHandlerDescriptor.CreateBuilder() + .AddFilter() .UseScopedHandler>() .SetOrder(int.MaxValue - 100_000) .SetType(OpenIddictServerHandlerType.BuiltIn) @@ -328,11 +318,6 @@ namespace OpenIddict.Server throw new ArgumentNullException(nameof(context)); } - if (context.EndpointType != OpenIddictServerEndpointType.Userinfo) - { - return; - } - var notification = new ApplyUserinfoResponseContext(context.Transaction); await _dispatcher.DispatchAsync(notification); diff --git a/src/OpenIddict.Validation/OpenIddictValidationDispatcher.cs b/src/OpenIddict.Validation/OpenIddictValidationDispatcher.cs index 90f42eb7..9caf23cc 100644 --- a/src/OpenIddict.Validation/OpenIddictValidationDispatcher.cs +++ b/src/OpenIddict.Validation/OpenIddictValidationDispatcher.cs @@ -43,28 +43,48 @@ namespace OpenIddict.Validation await foreach (var handler in GetHandlersAsync()) { - await handler.HandleAsync(context); + try + { + await handler.HandleAsync(context); + } + + catch (Exception exception) when (_logger.IsEnabled(LogLevel.Debug)) + { + _logger.LogDebug(exception, "An exception was thrown by {FullName} while handling the {Event} event.", + handler.GetType().FullName, typeof(TContext).FullName); + + throw; + } + + if (_logger.IsEnabled(LogLevel.Debug)) + { + _logger.LogDebug("The event {Event} was successfully processed by {FullName}.", + typeof(TContext).FullName, handler.GetType().FullName); + } switch (context) { case BaseRequestContext notification when notification.IsRequestHandled: if (_logger.IsEnabled(LogLevel.Debug)) { - _logger.LogDebug("The event context was marked as handled by {Name}.", handler.GetType().FullName); + _logger.LogDebug("The event {Event} was marked as handled by {FullName}.", + typeof(TContext).FullName, handler.GetType().FullName); } return; case BaseRequestContext notification when notification.IsRequestSkipped: if (_logger.IsEnabled(LogLevel.Debug)) { - _logger.LogDebug("The event context was marked as skipped by {Name}.", handler.GetType().FullName); + _logger.LogDebug("The event {Event} was marked as skipped by {FullName}.", + typeof(TContext).FullName, handler.GetType().FullName); } return; case BaseValidatingContext notification when notification.IsRejected: if (_logger.IsEnabled(LogLevel.Debug)) { - _logger.LogDebug("The event context was marked as rejected by {Name}.", handler.GetType().FullName); + _logger.LogDebug("The event {Event} was marked as rejected by {FullName}.", + typeof(TContext).FullName, handler.GetType().FullName); } return;