Browse Source

Introduce a new Import method in the handler descriptors

pull/1160/head
Kévin Chalet 5 years ago
parent
commit
7e1c9dd130
  1. 4
      src/OpenIddict.Abstractions/Resources/OpenIddictResources.resx
  2. 3
      src/OpenIddict.Server/OpenIddictServerBuilder.cs
  3. 33
      src/OpenIddict.Server/OpenIddictServerHandlerDescriptor.cs
  4. 8
      src/OpenIddict.Server/OpenIddictServerHandlers.Authentication.cs
  5. 8
      src/OpenIddict.Server/OpenIddictServerHandlers.Device.cs
  6. 8
      src/OpenIddict.Server/OpenIddictServerHandlers.Exchange.cs
  7. 33
      src/OpenIddict.Validation/OpenIddictValidationHandlerDescriptor.cs

4
src/OpenIddict.Abstractions/Resources/OpenIddictResources.resx

@ -1389,6 +1389,10 @@ To register the OpenIddict core services, reference the 'OpenIddict.Core' packag
<value>Provided symmetric key was incorrect size. Expected {0} bits, received {1}.</value>
<comment>{Locked}</comment>
</data>
<data name="ID0284" xml:space="preserve">
<value>The context type associated with the specified descriptor doesn't match the context type of this builder.</value>
<comment>{Locked}</comment>
</data>
<data name="ID2000" xml:space="preserve">
<value>The security token is missing.</value>
</data>

3
src/OpenIddict.Server/OpenIddictServerBuilder.cs

@ -157,8 +157,7 @@ namespace Microsoft.Extensions.DependencyInjection
/// <summary>
/// Registers an encryption key.
/// </summary>
/// <param name="key">The security key.
/// </param>
/// <param name="key">The security key.</param>
/// <returns>The <see cref="OpenIddictServerBuilder"/>.</returns>
public OpenIddictServerBuilder AddEncryptionKey(SecurityKey key)
{

33
src/OpenIddict.Server/OpenIddictServerHandlerDescriptor.cs

@ -68,7 +68,7 @@ namespace OpenIddict.Server
public class Builder<TContext> where TContext : BaseContext
{
private ServiceDescriptor? _descriptor;
private readonly List<Type> _filterTypes = new List<Type>();
private readonly List<Type> _filters = new();
private int _order;
private OpenIddictServerHandlerType _type;
@ -89,7 +89,7 @@ namespace OpenIddict.Server
throw new InvalidOperationException(SR.GetResourceString(SR.ID0104));
}
_filterTypes.Add(type);
_filters.Add(type);
return this;
}
@ -103,6 +103,33 @@ namespace OpenIddict.Server
where TFilter : IOpenIddictServerHandlerFilter<TContext>
=> AddFilter(typeof(TFilter));
/// <summary>
/// Imports the properties set on the specified descriptor.
/// </summary>
/// <param name="descriptor">The existing descriptor properties are copied from.</param>
/// <remarks>All the properties previously set on this instance are automatically replaced.</remarks>
/// <returns>The builder instance, so that calls can be easily chained.</returns>
public Builder<TContext> Import(OpenIddictServerHandlerDescriptor descriptor)
{
if (descriptor is null)
{
throw new ArgumentNullException(nameof(descriptor));
}
if (descriptor.ContextType != typeof(TContext))
{
throw new InvalidOperationException(SR.GetResourceString(SR.ID0284));
}
_descriptor = descriptor.ServiceDescriptor;
_filters.Clear();
_filters.AddRange(descriptor.FilterTypes);
_order = descriptor.Order;
_type = descriptor.Type;
return this;
}
/// <summary>
/// Sets the service descriptor.
/// </summary>
@ -250,7 +277,7 @@ namespace OpenIddict.Server
public OpenIddictServerHandlerDescriptor Build() => new OpenIddictServerHandlerDescriptor
{
ContextType = typeof(TContext),
FilterTypes = _filterTypes.ToImmutableArray(),
FilterTypes = _filters.ToImmutableArray(),
Order = _order,
ServiceDescriptor = _descriptor ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0105)),
Type = _type

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

@ -1150,11 +1150,7 @@ namespace OpenIddict.Server
{
private readonly IOpenIddictScopeManager? _scopeManager;
public ValidateScopes()
{
}
public ValidateScopes(IOpenIddictScopeManager scopeManager)
public ValidateScopes(IOpenIddictScopeManager? scopeManager = null)
=> _scopeManager = scopeManager;
/// <summary>
@ -1163,7 +1159,7 @@ namespace OpenIddict.Server
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<ValidateAuthorizationRequestContext>()
.AddFilter<RequireScopeValidationEnabled>()
.UseScopedHandler<ValidateScopes>(provider =>
.UseScopedHandler<ValidateScopes>(static provider =>
{
// Note: the scope manager is only resolved if the degraded mode was not enabled to ensure
// invalid core configuration exceptions are not thrown even if the managers were registered.

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

@ -372,11 +372,7 @@ namespace OpenIddict.Server
{
private readonly IOpenIddictScopeManager? _scopeManager;
public ValidateScopes()
{
}
public ValidateScopes(IOpenIddictScopeManager scopeManager)
public ValidateScopes(IOpenIddictScopeManager? scopeManager = null)
=> _scopeManager = scopeManager;
/// <summary>
@ -385,7 +381,7 @@ namespace OpenIddict.Server
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<ValidateDeviceRequestContext>()
.AddFilter<RequireScopeValidationEnabled>()
.UseScopedHandler<ValidateScopes>(provider =>
.UseScopedHandler<ValidateScopes>(static provider =>
{
// Note: the scope manager is only resolved if the degraded mode was not enabled to ensure
// invalid core configuration exceptions are not thrown even if the managers were registered.

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

@ -701,11 +701,7 @@ namespace OpenIddict.Server
{
private readonly IOpenIddictScopeManager? _scopeManager;
public ValidateScopes()
{
}
public ValidateScopes(IOpenIddictScopeManager scopeManager)
public ValidateScopes(IOpenIddictScopeManager? scopeManager = null)
=> _scopeManager = scopeManager;
/// <summary>
@ -714,7 +710,7 @@ namespace OpenIddict.Server
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<ValidateTokenRequestContext>()
.AddFilter<RequireScopeValidationEnabled>()
.UseScopedHandler<ValidateScopes>(provider =>
.UseScopedHandler<ValidateScopes>(static provider =>
{
// Note: the scope manager is only resolved if the degraded mode was not enabled to ensure
// invalid core configuration exceptions are not thrown even if the managers were registered.

33
src/OpenIddict.Validation/OpenIddictValidationHandlerDescriptor.cs

@ -68,7 +68,7 @@ namespace OpenIddict.Validation
public class Builder<TContext> where TContext : BaseContext
{
private ServiceDescriptor? _descriptor;
private readonly List<Type> _filterTypes = new List<Type>();
private readonly List<Type> _filters = new();
private int _order;
private OpenIddictValidationHandlerType _type;
@ -89,7 +89,7 @@ namespace OpenIddict.Validation
throw new InvalidOperationException(SR.GetResourceString(SR.ID0104));
}
_filterTypes.Add(type);
_filters.Add(type);
return this;
}
@ -103,6 +103,33 @@ namespace OpenIddict.Validation
where TFilter : IOpenIddictValidationHandlerFilter<TContext>
=> AddFilter(typeof(TFilter));
/// <summary>
/// Imports the properties set on the specified descriptor.
/// </summary>
/// <param name="descriptor">The existing descriptor properties are copied from.</param>
/// <remarks>All the properties previously set on this instance are automatically replaced.</remarks>
/// <returns>The builder instance, so that calls can be easily chained.</returns>
public Builder<TContext> Import(OpenIddictValidationHandlerDescriptor descriptor)
{
if (descriptor is null)
{
throw new ArgumentNullException(nameof(descriptor));
}
if (descriptor.ContextType != typeof(TContext))
{
throw new InvalidOperationException(SR.GetResourceString(SR.ID0284));
}
_descriptor = descriptor.ServiceDescriptor;
_filters.Clear();
_filters.AddRange(descriptor.FilterTypes);
_order = descriptor.Order;
_type = descriptor.Type;
return this;
}
/// <summary>
/// Sets the service descriptor.
/// </summary>
@ -250,7 +277,7 @@ namespace OpenIddict.Validation
public OpenIddictValidationHandlerDescriptor Build() => new OpenIddictValidationHandlerDescriptor
{
ContextType = typeof(TContext),
FilterTypes = _filterTypes.ToImmutableArray(),
FilterTypes = _filters.ToImmutableArray(),
Order = _order,
ServiceDescriptor = _descriptor ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0105)),
Type = _type

Loading…
Cancel
Save