From e1a7832a6cd3645c12b910eb5e51f93cc66f345c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Chalet?= Date: Tue, 30 Oct 2018 20:13:42 +0100 Subject: [PATCH] Update OpenIddictMvcConfiguration to use BindingSourceMetadataProvider --- .../Managers/IOpenIddictApplicationManager.cs | 17 ++++++--- .../IOpenIddictAuthorizationManager.cs | 37 ++++++++++++++----- .../Managers/IOpenIddictScopeManager.cs | 17 ++++++--- .../Managers/IOpenIddictTokenManager.cs | 17 ++++++--- .../Internal/OpenIddictMvcConfiguration.cs | 13 ++++++- src/OpenIddict.Mvc/OpenIddictMvcExtensions.cs | 3 +- .../OpenIddictMvcConfigurationTests.cs | 19 ++++++++-- 7 files changed, 92 insertions(+), 31 deletions(-) diff --git a/src/OpenIddict.Abstractions/Managers/IOpenIddictApplicationManager.cs b/src/OpenIddict.Abstractions/Managers/IOpenIddictApplicationManager.cs index ddab1219..0436b149 100644 --- a/src/OpenIddict.Abstractions/Managers/IOpenIddictApplicationManager.cs +++ b/src/OpenIddict.Abstractions/Managers/IOpenIddictApplicationManager.cs @@ -140,7 +140,8 @@ namespace OpenIddict.Abstractions /// A that can be used to monitor the asynchronous operation, /// whose result returns the first element returned when executing the query. /// - Task GetAsync([NotNull] Func, IQueryable> query, CancellationToken cancellationToken = default); + Task GetAsync( + [NotNull] Func, IQueryable> query, CancellationToken cancellationToken = default); /// /// Executes the specified query and returns the first element. @@ -154,7 +155,9 @@ namespace OpenIddict.Abstractions /// A that can be used to monitor the asynchronous operation, /// whose result returns the first element returned when executing the query. /// - Task GetAsync([NotNull] Func, TState, IQueryable> query, [CanBeNull] TState state, CancellationToken cancellationToken = default); + Task GetAsync( + [NotNull] Func, TState, IQueryable> query, + [CanBeNull] TState state, CancellationToken cancellationToken = default); /// /// Retrieves the client identifier associated with an application. @@ -287,7 +290,8 @@ namespace OpenIddict.Abstractions /// A that can be used to monitor the asynchronous operation, /// whose result returns all the elements returned when executing the specified query. /// - Task> ListAsync([CanBeNull] int? count = null, [CanBeNull] int? offset = null, CancellationToken cancellationToken = default); + Task> ListAsync( + [CanBeNull] int? count = null, [CanBeNull] int? offset = null, CancellationToken cancellationToken = default); /// /// Executes the specified query and returns all the corresponding elements. @@ -299,7 +303,8 @@ namespace OpenIddict.Abstractions /// A that can be used to monitor the asynchronous operation, /// whose result returns all the elements returned when executing the specified query. /// - Task> ListAsync([NotNull] Func, IQueryable> query, CancellationToken cancellationToken = default); + Task> ListAsync( + [NotNull] Func, IQueryable> query, CancellationToken cancellationToken = default); /// /// Executes the specified query and returns all the corresponding elements. @@ -313,7 +318,9 @@ namespace OpenIddict.Abstractions /// A that can be used to monitor the asynchronous operation, /// whose result returns all the elements returned when executing the specified query. /// - Task> ListAsync([NotNull] Func, TState, IQueryable> query, [CanBeNull] TState state, CancellationToken cancellationToken = default); + Task> ListAsync( + [NotNull] Func, TState, IQueryable> query, + [CanBeNull] TState state, CancellationToken cancellationToken = default); /// /// Populates the specified descriptor using the properties exposed by the application. diff --git a/src/OpenIddict.Abstractions/Managers/IOpenIddictAuthorizationManager.cs b/src/OpenIddict.Abstractions/Managers/IOpenIddictAuthorizationManager.cs index 210dc783..2367b029 100644 --- a/src/OpenIddict.Abstractions/Managers/IOpenIddictAuthorizationManager.cs +++ b/src/OpenIddict.Abstractions/Managers/IOpenIddictAuthorizationManager.cs @@ -39,7 +39,8 @@ namespace OpenIddict.Abstractions /// A that can be used to monitor the asynchronous operation, /// whose result returns the number of authorizations that match the specified query. /// - Task CountAsync([NotNull] Func, IQueryable> query, CancellationToken cancellationToken = default); + Task CountAsync( + [NotNull] Func, IQueryable> query, CancellationToken cancellationToken = default); /// /// Creates a new permanent authorization based on the specified parameters. @@ -54,7 +55,10 @@ namespace OpenIddict.Abstractions /// /// A that can be used to monitor the asynchronous operation, whose result returns the authorization. /// - Task CreateAsync([NotNull] ClaimsPrincipal principal, [NotNull] string subject, [NotNull] string client, [NotNull] string type, ImmutableArray scopes, [CanBeNull] ImmutableDictionary properties, CancellationToken cancellationToken = default); + Task CreateAsync( + [NotNull] ClaimsPrincipal principal, [NotNull] string subject, [NotNull] string client, + [NotNull] string type, ImmutableArray scopes, + [CanBeNull] ImmutableDictionary properties, CancellationToken cancellationToken = default); /// /// Creates a new authorization based on the specified descriptor. @@ -110,7 +114,9 @@ namespace OpenIddict.Abstractions /// A that can be used to monitor the asynchronous operation, /// whose result returns the authorizations corresponding to the criteria. /// - Task> FindAsync([NotNull] string subject, [NotNull] string client, [NotNull] string status, CancellationToken cancellationToken = default); + Task> FindAsync( + [NotNull] string subject, [NotNull] string client, + [NotNull] string status, CancellationToken cancellationToken = default); /// /// Retrieves the authorizations matching the specified parameters. @@ -124,7 +130,9 @@ namespace OpenIddict.Abstractions /// A that can be used to monitor the asynchronous operation, /// whose result returns the authorizations corresponding to the criteria. /// - Task> FindAsync([NotNull] string subject, [NotNull] string client, [NotNull] string status, [NotNull] string type, CancellationToken cancellationToken = default); + Task> FindAsync( + [NotNull] string subject, [NotNull] string client, + [NotNull] string status, [NotNull] string type, CancellationToken cancellationToken = default); /// /// Retrieves the authorizations matching the specified parameters. @@ -139,7 +147,9 @@ namespace OpenIddict.Abstractions /// A that can be used to monitor the asynchronous operation, /// whose result returns the authorizations corresponding to the criteria. /// - Task> FindAsync([NotNull] string subject, [NotNull] string client, [NotNull] string status, [NotNull] string type, ImmutableArray scopes, CancellationToken cancellationToken = default); + Task> FindAsync( + [NotNull] string subject, [NotNull] string client, [NotNull] string status, + [NotNull] string type, ImmutableArray scopes, CancellationToken cancellationToken = default); /// /// Retrieves the list of authorizations corresponding to the specified application identifier. @@ -195,7 +205,8 @@ namespace OpenIddict.Abstractions /// A that can be used to monitor the asynchronous operation, /// whose result returns the first element returned when executing the query. /// - Task GetAsync([NotNull] Func, IQueryable> query, CancellationToken cancellationToken = default); + Task GetAsync( + [NotNull] Func, IQueryable> query, CancellationToken cancellationToken = default); /// /// Executes the specified query and returns the first element. @@ -209,7 +220,9 @@ namespace OpenIddict.Abstractions /// A that can be used to monitor the asynchronous operation, /// whose result returns the first element returned when executing the query. /// - Task GetAsync([NotNull] Func, TState, IQueryable> query, [CanBeNull] TState state, CancellationToken cancellationToken = default); + Task GetAsync( + [NotNull] Func, TState, IQueryable> query, + [CanBeNull] TState state, CancellationToken cancellationToken = default); /// /// Retrieves the unique identifier associated with an authorization. @@ -317,7 +330,8 @@ namespace OpenIddict.Abstractions /// A that can be used to monitor the asynchronous operation, /// whose result returns all the elements returned when executing the specified query. /// - Task> ListAsync([CanBeNull] int? count = null, [CanBeNull] int? offset = null, CancellationToken cancellationToken = default); + Task> ListAsync( + [CanBeNull] int? count = null, [CanBeNull] int? offset = null, CancellationToken cancellationToken = default); /// /// Executes the specified query and returns all the corresponding elements. @@ -329,7 +343,8 @@ namespace OpenIddict.Abstractions /// A that can be used to monitor the asynchronous operation, /// whose result returns all the elements returned when executing the specified query. /// - Task> ListAsync([NotNull] Func, IQueryable> query, CancellationToken cancellationToken = default); + Task> ListAsync( + [NotNull] Func, IQueryable> query, CancellationToken cancellationToken = default); /// /// Executes the specified query and returns all the corresponding elements. @@ -343,7 +358,9 @@ namespace OpenIddict.Abstractions /// A that can be used to monitor the asynchronous operation, /// whose result returns all the elements returned when executing the specified query. /// - Task> ListAsync([NotNull] Func, TState, IQueryable> query, [CanBeNull] TState state, CancellationToken cancellationToken = default); + Task> ListAsync( + [NotNull] Func, TState, IQueryable> query, + [CanBeNull] TState state, CancellationToken cancellationToken = default); /// /// Populates the specified descriptor using the properties exposed by the authorization. diff --git a/src/OpenIddict.Abstractions/Managers/IOpenIddictScopeManager.cs b/src/OpenIddict.Abstractions/Managers/IOpenIddictScopeManager.cs index fee44974..fd3dd2fa 100644 --- a/src/OpenIddict.Abstractions/Managers/IOpenIddictScopeManager.cs +++ b/src/OpenIddict.Abstractions/Managers/IOpenIddictScopeManager.cs @@ -124,7 +124,8 @@ namespace OpenIddict.Abstractions /// A that can be used to monitor the asynchronous operation, /// whose result returns the first element returned when executing the query. /// - Task GetAsync([NotNull] Func, IQueryable> query, CancellationToken cancellationToken = default); + Task GetAsync( + [NotNull] Func, IQueryable> query, CancellationToken cancellationToken = default); /// /// Executes the specified query and returns the first element. @@ -138,7 +139,9 @@ namespace OpenIddict.Abstractions /// A that can be used to monitor the asynchronous operation, /// whose result returns the first element returned when executing the query. /// - Task GetAsync([NotNull] Func, TState, IQueryable> query, [CanBeNull] TState state, CancellationToken cancellationToken = default); + Task GetAsync( + [NotNull] Func, TState, IQueryable> query, + [CanBeNull] TState state, CancellationToken cancellationToken = default); /// /// Retrieves the description associated with a scope. @@ -205,7 +208,8 @@ namespace OpenIddict.Abstractions /// A that can be used to monitor the asynchronous operation, /// whose result returns all the elements returned when executing the specified query. /// - Task> ListAsync([CanBeNull] int? count = null, [CanBeNull] int? offset = null, CancellationToken cancellationToken = default); + Task> ListAsync( + [CanBeNull] int? count = null, [CanBeNull] int? offset = null, CancellationToken cancellationToken = default); /// /// Executes the specified query and returns all the corresponding elements. @@ -217,7 +221,8 @@ namespace OpenIddict.Abstractions /// A that can be used to monitor the asynchronous operation, /// whose result returns all the elements returned when executing the specified query. /// - Task> ListAsync([NotNull] Func, IQueryable> query, CancellationToken cancellationToken = default); + Task> ListAsync( + [NotNull] Func, IQueryable> query, CancellationToken cancellationToken = default); /// /// Executes the specified query and returns all the corresponding elements. @@ -231,7 +236,9 @@ namespace OpenIddict.Abstractions /// A that can be used to monitor the asynchronous operation, /// whose result returns all the elements returned when executing the specified query. /// - Task> ListAsync([NotNull] Func, TState, IQueryable> query, [CanBeNull] TState state, CancellationToken cancellationToken = default); + Task> ListAsync( + [NotNull] Func, TState, IQueryable> query, + [CanBeNull] TState state, CancellationToken cancellationToken = default); /// /// Lists all the resources associated with the specified scopes. diff --git a/src/OpenIddict.Abstractions/Managers/IOpenIddictTokenManager.cs b/src/OpenIddict.Abstractions/Managers/IOpenIddictTokenManager.cs index 20ecd1d7..37421f0c 100644 --- a/src/OpenIddict.Abstractions/Managers/IOpenIddictTokenManager.cs +++ b/src/OpenIddict.Abstractions/Managers/IOpenIddictTokenManager.cs @@ -203,7 +203,8 @@ namespace OpenIddict.Abstractions /// A that can be used to monitor the asynchronous operation, /// whose result returns the first element returned when executing the query. /// - Task GetAsync([NotNull] Func, IQueryable> query, CancellationToken cancellationToken = default); + Task GetAsync( + [NotNull] Func, IQueryable> query, CancellationToken cancellationToken = default); /// /// Executes the specified query and returns the first element. @@ -217,7 +218,9 @@ namespace OpenIddict.Abstractions /// A that can be used to monitor the asynchronous operation, /// whose result returns the first element returned when executing the query. /// - Task GetAsync([NotNull] Func, TState, IQueryable> query, [CanBeNull] TState state, CancellationToken cancellationToken = default); + Task GetAsync( + [NotNull] Func, TState, IQueryable> query, + [CanBeNull] TState state, CancellationToken cancellationToken = default); /// /// Retrieves the optional authorization identifier associated with a token. @@ -354,7 +357,8 @@ namespace OpenIddict.Abstractions /// A that can be used to monitor the asynchronous operation, /// whose result returns all the elements returned when executing the specified query. /// - Task> ListAsync([CanBeNull] int? count = null, [CanBeNull] int? offset = null, CancellationToken cancellationToken = default); + Task> ListAsync( + [CanBeNull] int? count = null, [CanBeNull] int? offset = null, CancellationToken cancellationToken = default); /// /// Executes the specified query and returns all the corresponding elements. @@ -366,7 +370,8 @@ namespace OpenIddict.Abstractions /// A that can be used to monitor the asynchronous operation, /// whose result returns all the elements returned when executing the specified query. /// - Task> ListAsync([NotNull] Func, IQueryable> query, CancellationToken cancellationToken = default); + Task> ListAsync( + [NotNull] Func, IQueryable> query, CancellationToken cancellationToken = default); /// /// Executes the specified query and returns all the corresponding elements. @@ -380,7 +385,9 @@ namespace OpenIddict.Abstractions /// A that can be used to monitor the asynchronous operation, /// whose result returns all the elements returned when executing the specified query. /// - Task> ListAsync([NotNull] Func, TState, IQueryable> query, [CanBeNull] TState state, CancellationToken cancellationToken = default); + Task> ListAsync( + [NotNull] Func, TState, IQueryable> query, + [CanBeNull] TState state, CancellationToken cancellationToken = default); /// /// Populates the specified descriptor using the properties exposed by the token. diff --git a/src/OpenIddict.Mvc/Internal/OpenIddictMvcConfiguration.cs b/src/OpenIddict.Mvc/Internal/OpenIddictMvcConfiguration.cs index 23b1ba74..7f818655 100644 --- a/src/OpenIddict.Mvc/Internal/OpenIddictMvcConfiguration.cs +++ b/src/OpenIddict.Mvc/Internal/OpenIddictMvcConfiguration.cs @@ -9,6 +9,7 @@ using AspNet.Security.OpenIdConnect.Primitives; using JetBrains.Annotations; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.ModelBinding; +using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata; using Microsoft.Extensions.Options; namespace OpenIddict.Mvc.Internal @@ -32,8 +33,16 @@ namespace OpenIddict.Mvc.Internal } options.ModelBinderProviders.Insert(0, new OpenIddictMvcBinderProvider()); - options.ModelMetadataDetailsProviders.Add(new SuppressChildValidationMetadataProvider(typeof(OpenIdConnectRequest))); - options.ModelMetadataDetailsProviders.Add(new SuppressChildValidationMetadataProvider(typeof(OpenIdConnectResponse))); + + options.ModelMetadataDetailsProviders.Add( + new BindingSourceMetadataProvider(typeof(OpenIdConnectRequest), BindingSource.Special)); + options.ModelMetadataDetailsProviders.Add( + new BindingSourceMetadataProvider(typeof(OpenIdConnectResponse), BindingSource.Special)); + + options.ModelMetadataDetailsProviders.Add( + new SuppressChildValidationMetadataProvider(typeof(OpenIdConnectRequest))); + options.ModelMetadataDetailsProviders.Add( + new SuppressChildValidationMetadataProvider(typeof(OpenIdConnectResponse))); } } } diff --git a/src/OpenIddict.Mvc/OpenIddictMvcExtensions.cs b/src/OpenIddict.Mvc/OpenIddictMvcExtensions.cs index ff22394a..1ea2dda0 100644 --- a/src/OpenIddict.Mvc/OpenIddictMvcExtensions.cs +++ b/src/OpenIddict.Mvc/OpenIddictMvcExtensions.cs @@ -33,7 +33,8 @@ namespace Microsoft.Extensions.DependencyInjection // Register the options initializer used by the OpenIddict MVC binding/validation components. // Note: TryAddEnumerable() is used here to ensure the initializer is only registered once. - builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton, OpenIddictMvcConfiguration>()); + builder.Services.TryAddEnumerable( + ServiceDescriptor.Singleton, OpenIddictMvcConfiguration>()); return new OpenIddictMvcBuilder(builder.Services); } diff --git a/test/OpenIddict.Mvc.Tests/Internal/OpenIddictMvcConfigurationTests.cs b/test/OpenIddict.Mvc.Tests/Internal/OpenIddictMvcConfigurationTests.cs index 8b76a50b..cdd41876 100644 --- a/test/OpenIddict.Mvc.Tests/Internal/OpenIddictMvcConfigurationTests.cs +++ b/test/OpenIddict.Mvc.Tests/Internal/OpenIddictMvcConfigurationTests.cs @@ -9,6 +9,7 @@ using System.Linq; using AspNet.Security.OpenIdConnect.Primitives; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.ModelBinding; +using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using OpenIddict.Mvc.Internal; @@ -63,9 +64,21 @@ namespace OpenIddict.Mvc.Tests var options = services.BuildServiceProvider().GetRequiredService>(); // Assert - var providers = options.Value.ModelMetadataDetailsProviders.OfType(); - Assert.Contains(providers, provider => provider.Type == typeof(OpenIdConnectRequest)); - Assert.Contains(providers, provider => provider.Type == typeof(OpenIdConnectResponse)); + Assert.Contains( + options.Value.ModelMetadataDetailsProviders.OfType(), + provider => provider.Type == typeof(OpenIdConnectRequest) && + provider.BindingSource == BindingSource.Special); + Assert.Contains( + options.Value.ModelMetadataDetailsProviders.OfType(), + provider => provider.Type == typeof(OpenIdConnectResponse) && + provider.BindingSource == BindingSource.Special); + + Assert.Contains( + options.Value.ModelMetadataDetailsProviders.OfType(), + provider => provider.Type == typeof(OpenIdConnectRequest)); + Assert.Contains( + options.Value.ModelMetadataDetailsProviders.OfType(), + provider => provider.Type == typeof(OpenIdConnectResponse)); } } }