Browse Source

Introduce new filters to avoid instantiating scoped handlers when unnecessary

pull/1003/head
Kévin Chalet 6 years ago
parent
commit
791552025f
  1. 28
      src/OpenIddict.Server/OpenIddictServerDispatcher.cs
  2. 10
      src/OpenIddict.Server/OpenIddictServerExtensions.cs
  3. 160
      src/OpenIddict.Server/OpenIddictServerHandlerFilters.cs
  4. 24
      src/OpenIddict.Server/OpenIddictServerHandlers.Authentication.cs
  5. 48
      src/OpenIddict.Server/OpenIddictServerHandlers.Device.cs
  6. 49
      src/OpenIddict.Server/OpenIddictServerHandlers.Discovery.cs
  7. 24
      src/OpenIddict.Server/OpenIddictServerHandlers.Exchange.cs
  8. 24
      src/OpenIddict.Server/OpenIddictServerHandlers.Introspection.cs
  9. 24
      src/OpenIddict.Server/OpenIddictServerHandlers.Revocation.cs
  10. 24
      src/OpenIddict.Server/OpenIddictServerHandlers.Session.cs
  11. 25
      src/OpenIddict.Server/OpenIddictServerHandlers.Userinfo.cs
  12. 28
      src/OpenIddict.Validation/OpenIddictValidationDispatcher.cs

28
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;

10
src/OpenIddict.Server/OpenIddictServerExtensions.cs

@ -47,23 +47,33 @@ namespace Microsoft.Extensions.DependencyInjection
builder.Services.TryAddSingleton<RequireAccessTokenIncluded>();
builder.Services.TryAddSingleton<RequireAuthorizationCodeIncluded>();
builder.Services.TryAddSingleton<RequireAuthorizationStorageEnabled>();
builder.Services.TryAddSingleton<RequireAuthorizationRequest>();
builder.Services.TryAddSingleton<RequireClientIdParameter>();
builder.Services.TryAddSingleton<RequireConfigurationRequest>();
builder.Services.TryAddSingleton<RequireCryptographyRequest>();
builder.Services.TryAddSingleton<RequireDegradedModeDisabled>();
builder.Services.TryAddSingleton<RequireDeviceCodeIncluded>();
builder.Services.TryAddSingleton<RequireDeviceRequest>();
builder.Services.TryAddSingleton<RequireEndpointPermissionsEnabled>();
builder.Services.TryAddSingleton<RequireGrantTypePermissionsEnabled>();
builder.Services.TryAddSingleton<RequireIdentityTokenIncluded>();
builder.Services.TryAddSingleton<RequireIntrospectionRequest>();
builder.Services.TryAddSingleton<RequireLogoutRequest>();
builder.Services.TryAddSingleton<RequirePostLogoutRedirectUriParameter>();
builder.Services.TryAddSingleton<RequireReferenceAccessTokensEnabled>();
builder.Services.TryAddSingleton<RequireReferenceRefreshTokensEnabled>();
builder.Services.TryAddSingleton<RequireRefreshTokenIncluded>();
builder.Services.TryAddSingleton<RequireRevocationRequest>();
builder.Services.TryAddSingleton<RequireRollingTokensDisabled>();
builder.Services.TryAddSingleton<RequireRollingRefreshTokensEnabled>();
builder.Services.TryAddSingleton<RequireSlidingExpirationEnabled>();
builder.Services.TryAddSingleton<RequireScopePermissionsEnabled>();
builder.Services.TryAddSingleton<RequireScopeValidationEnabled>();
builder.Services.TryAddSingleton<RequireTokenStorageEnabled>();
builder.Services.TryAddSingleton<RequireTokenRequest>();
builder.Services.TryAddSingleton<RequireUserCodeIncluded>();
builder.Services.TryAddSingleton<RequireUserinfoRequest>();
builder.Services.TryAddSingleton<RequireVerificationRequest>();
// Note: TryAddEnumerable() is used here to ensure the initializer is registered only once.
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<

160
src/OpenIddict.Server/OpenIddictServerHandlerFilters.cs

@ -47,6 +47,22 @@ namespace OpenIddict.Server
}
}
/// <summary>
/// Represents a filter that excludes the associated handlers if the request is not an authorization request.
/// </summary>
public class RequireAuthorizationRequest : IOpenIddictServerHandlerFilter<BaseContext>
{
public ValueTask<bool> IsActiveAsync([NotNull] BaseContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
return new ValueTask<bool>(context.EndpointType == OpenIddictServerEndpointType.Authorization);
}
}
/// <summary>
/// Represents a filter that excludes the associated handlers if authorization storage was not enabled.
/// </summary>
@ -79,6 +95,38 @@ namespace OpenIddict.Server
}
}
/// <summary>
/// Represents a filter that excludes the associated handlers if the request is not a configuration request.
/// </summary>
public class RequireConfigurationRequest : IOpenIddictServerHandlerFilter<BaseContext>
{
public ValueTask<bool> IsActiveAsync([NotNull] BaseContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
return new ValueTask<bool>(context.EndpointType == OpenIddictServerEndpointType.Configuration);
}
}
/// <summary>
/// Represents a filter that excludes the associated handlers if the request is not a cryptography request.
/// </summary>
public class RequireCryptographyRequest : IOpenIddictServerHandlerFilter<BaseContext>
{
public ValueTask<bool> IsActiveAsync([NotNull] BaseContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
return new ValueTask<bool>(context.EndpointType == OpenIddictServerEndpointType.Cryptography);
}
}
/// <summary>
/// Represents a filter that excludes the associated handlers if the degraded mode was not enabled.
/// </summary>
@ -111,6 +159,22 @@ namespace OpenIddict.Server
}
}
/// <summary>
/// Represents a filter that excludes the associated handlers if the request is not a device request.
/// </summary>
public class RequireDeviceRequest : IOpenIddictServerHandlerFilter<BaseContext>
{
public ValueTask<bool> IsActiveAsync([NotNull] BaseContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
return new ValueTask<bool>(context.EndpointType == OpenIddictServerEndpointType.Device);
}
}
/// <summary>
/// Represents a filter that excludes the associated handlers if endpoint permissions were disabled.
/// </summary>
@ -159,6 +223,38 @@ namespace OpenIddict.Server
}
}
/// <summary>
/// Represents a filter that excludes the associated handlers if the request is not an introspection request.
/// </summary>
public class RequireIntrospectionRequest : IOpenIddictServerHandlerFilter<BaseContext>
{
public ValueTask<bool> IsActiveAsync([NotNull] BaseContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
return new ValueTask<bool>(context.EndpointType == OpenIddictServerEndpointType.Introspection);
}
}
/// <summary>
/// Represents a filter that excludes the associated handlers if the request is not a logout request.
/// </summary>
public class RequireLogoutRequest : IOpenIddictServerHandlerFilter<BaseContext>
{
public ValueTask<bool> IsActiveAsync([NotNull] BaseContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
return new ValueTask<bool>(context.EndpointType == OpenIddictServerEndpointType.Logout);
}
}
/// <summary>
/// Represents a filter that excludes the associated handlers when no post_logout_redirect_uri is received.
/// </summary>
@ -223,6 +319,22 @@ namespace OpenIddict.Server
}
}
/// <summary>
/// Represents a filter that excludes the associated handlers if the request is not a revocation request.
/// </summary>
public class RequireRevocationRequest : IOpenIddictServerHandlerFilter<BaseContext>
{
public ValueTask<bool> IsActiveAsync([NotNull] BaseContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
return new ValueTask<bool>(context.EndpointType == OpenIddictServerEndpointType.Revocation);
}
}
/// <summary>
/// Represents a filter that excludes the associated handlers if rolling tokens were enabled.
/// </summary>
@ -303,6 +415,22 @@ namespace OpenIddict.Server
}
}
/// <summary>
/// Represents a filter that excludes the associated handlers if the request is not a token request.
/// </summary>
public class RequireTokenRequest : IOpenIddictServerHandlerFilter<BaseContext>
{
public ValueTask<bool> IsActiveAsync([NotNull] BaseContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
return new ValueTask<bool>(context.EndpointType == OpenIddictServerEndpointType.Token);
}
}
/// <summary>
/// Represents a filter that excludes the associated handlers if token storage was not enabled.
/// </summary>
@ -334,5 +462,37 @@ namespace OpenIddict.Server
return new ValueTask<bool>(context.IncludeUserCode);
}
}
/// <summary>
/// Represents a filter that excludes the associated handlers if the request is not a userinfo request.
/// </summary>
public class RequireUserinfoRequest : IOpenIddictServerHandlerFilter<BaseContext>
{
public ValueTask<bool> IsActiveAsync([NotNull] BaseContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
return new ValueTask<bool>(context.EndpointType == OpenIddictServerEndpointType.Userinfo);
}
}
/// <summary>
/// Represents a filter that excludes the associated handlers if the request is not a verification request.
/// </summary>
public class RequireVerificationRequest : IOpenIddictServerHandlerFilter<BaseContext>
{
public ValueTask<bool> IsActiveAsync([NotNull] BaseContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
return new ValueTask<bool>(context.EndpointType == OpenIddictServerEndpointType.Verification);
}
}
}
}

24
src/OpenIddict.Server/OpenIddictServerHandlers.Authentication.cs

@ -79,6 +79,7 @@ namespace OpenIddict.Server
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<ProcessRequestContext>()
.AddFilter<RequireAuthorizationRequest>()
.UseScopedHandler<ExtractAuthorizationRequest>()
.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
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<ProcessRequestContext>()
.AddFilter<RequireAuthorizationRequest>()
.UseScopedHandler<ValidateAuthorizationRequest>()
.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
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<ProcessRequestContext>()
.AddFilter<RequireAuthorizationRequest>()
.UseScopedHandler<HandleAuthorizationRequest>()
.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
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<TContext>()
.AddFilter<RequireAuthorizationRequest>()
.UseScopedHandler<ApplyAuthorizationResponse<TContext>>()
.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);

48
src/OpenIddict.Server/OpenIddictServerHandlers.Device.cs

@ -77,6 +77,7 @@ namespace OpenIddict.Server
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<ProcessRequestContext>()
.AddFilter<RequireDeviceRequest>()
.UseScopedHandler<ExtractDeviceRequest>()
.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
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<ProcessRequestContext>()
.AddFilter<RequireDeviceRequest>()
.UseScopedHandler<ValidateDeviceRequest>()
.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
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<ProcessRequestContext>()
.AddFilter<RequireDeviceRequest>()
.UseScopedHandler<HandleDeviceRequest>()
.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
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<TContext>()
.AddFilter<RequireDeviceRequest>()
.UseScopedHandler<ApplyDeviceResponse<TContext>>()
.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
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<ProcessRequestContext>()
.AddFilter<RequireVerificationRequest>()
.UseScopedHandler<ExtractVerificationRequest>()
.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
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<ProcessRequestContext>()
.AddFilter<RequireVerificationRequest>()
.UseScopedHandler<ValidateVerificationRequest>()
.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
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<ProcessRequestContext>()
.AddFilter<RequireVerificationRequest>()
.UseScopedHandler<HandleVerificationRequest>()
.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
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<TContext>()
.AddFilter<RequireVerificationRequest>()
.UseScopedHandler<ApplyVerificationResponse<TContext>>()
.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);

49
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
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<ProcessRequestContext>()
.AddFilter<RequireConfigurationRequest>()
.UseScopedHandler<ExtractConfigurationRequest>()
.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
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<ProcessRequestContext>()
.AddFilter<RequireConfigurationRequest>()
.UseScopedHandler<ValidateConfigurationRequest>()
.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
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<ProcessRequestContext>()
.AddFilter<RequireConfigurationRequest>()
.UseScopedHandler<HandleConfigurationRequest>()
.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
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<TContext>()
.AddFilter<RequireConfigurationRequest>()
.UseScopedHandler<ApplyConfigurationResponse<TContext>>()
.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
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<ProcessRequestContext>()
.AddFilter<RequireCryptographyRequest>()
.UseScopedHandler<ExtractCryptographyRequest>()
.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
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<ProcessRequestContext>()
.AddFilter<RequireCryptographyRequest>()
.UseScopedHandler<ValidateCryptographyRequest>()
.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
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<ProcessRequestContext>()
.AddFilter<RequireCryptographyRequest>()
.UseScopedHandler<HandleCryptographyRequest>()
.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
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<TContext>()
.AddFilter<RequireCryptographyRequest>()
.UseScopedHandler<ApplyCryptographyResponse<TContext>>()
.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);

24
src/OpenIddict.Server/OpenIddictServerHandlers.Exchange.cs

@ -84,6 +84,7 @@ namespace OpenIddict.Server
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<ProcessRequestContext>()
.AddFilter<RequireTokenRequest>()
.UseScopedHandler<ExtractTokenRequest>()
.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
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<ProcessRequestContext>()
.AddFilter<RequireTokenRequest>()
.UseScopedHandler<ValidateTokenRequest>()
.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
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<ProcessRequestContext>()
.AddFilter<RequireTokenRequest>()
.UseScopedHandler<HandleTokenRequest>()
.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
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<TContext>()
.AddFilter<RequireTokenRequest>()
.UseScopedHandler<ApplyTokenResponse<TContext>>()
.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);

24
src/OpenIddict.Server/OpenIddictServerHandlers.Introspection.cs

@ -78,6 +78,7 @@ namespace OpenIddict.Server
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<ProcessRequestContext>()
.AddFilter<RequireIntrospectionRequest>()
.UseScopedHandler<ExtractIntrospectionRequest>()
.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
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<ProcessRequestContext>()
.AddFilter<RequireIntrospectionRequest>()
.UseScopedHandler<ValidateIntrospectionRequest>()
.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
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<ProcessRequestContext>()
.AddFilter<RequireIntrospectionRequest>()
.UseScopedHandler<HandleIntrospectionRequest>()
.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
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<TContext>()
.AddFilter<RequireIntrospectionRequest>()
.UseScopedHandler<ApplyIntrospectionResponse<TContext>>()
.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);

24
src/OpenIddict.Server/OpenIddictServerHandlers.Revocation.cs

@ -70,6 +70,7 @@ namespace OpenIddict.Server
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<ProcessRequestContext>()
.AddFilter<RequireRevocationRequest>()
.UseScopedHandler<ExtractRevocationRequest>()
.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
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<ProcessRequestContext>()
.AddFilter<RequireRevocationRequest>()
.UseScopedHandler<ValidateRevocationRequest>()
.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
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<ProcessRequestContext>()
.AddFilter<RequireRevocationRequest>()
.UseScopedHandler<HandleRevocationRequest>()
.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
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<TContext>()
.AddFilter<RequireRevocationRequest>()
.UseScopedHandler<ApplyRevocationResponse<TContext>>()
.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);

24
src/OpenIddict.Server/OpenIddictServerHandlers.Session.cs

@ -59,6 +59,7 @@ namespace OpenIddict.Server
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<ProcessRequestContext>()
.AddFilter<RequireLogoutRequest>()
.UseScopedHandler<ExtractLogoutRequest>()
.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
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<ProcessRequestContext>()
.AddFilter<RequireLogoutRequest>()
.UseScopedHandler<ValidateLogoutRequest>()
.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
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<ProcessRequestContext>()
.AddFilter<RequireLogoutRequest>()
.UseScopedHandler<HandleLogoutRequest>()
.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
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<TContext>()
.AddFilter<RequireLogoutRequest>()
.UseScopedHandler<ApplyLogoutResponse<TContext>>()
.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);

25
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
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<ProcessRequestContext>()
.AddFilter<RequireUserinfoRequest>()
.UseScopedHandler<ExtractUserinfoRequest>()
.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
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<ProcessRequestContext>()
.AddFilter<RequireUserinfoRequest>()
.UseScopedHandler<ValidateUserinfoRequest>()
.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
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<ProcessRequestContext>()
.AddFilter<RequireUserinfoRequest>()
.UseScopedHandler<HandleUserinfoRequest>()
.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
/// </summary>
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<TContext>()
.AddFilter<RequireUserinfoRequest>()
.UseScopedHandler<ApplyUserinfoResponse<TContext>>()
.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);

28
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;

Loading…
Cancel
Save