10 changed files with 158 additions and 23 deletions
@ -0,0 +1,39 @@ |
|||
/* |
|||
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
|
|||
* See https://github.com/openiddict/openiddict-core for more information concerning
|
|||
* the license and the contributors participating to this project. |
|||
*/ |
|||
|
|||
using System; |
|||
using AspNet.Security.OpenIdConnect.Primitives; |
|||
using JetBrains.Annotations; |
|||
using Microsoft.AspNetCore.Mvc; |
|||
using Microsoft.AspNetCore.Mvc.ModelBinding; |
|||
using Microsoft.Extensions.Options; |
|||
|
|||
namespace OpenIddict.Mvc.Internal |
|||
{ |
|||
/// <summary>
|
|||
/// Contains the methods required to ensure that the OpenIddict MVC configuration is valid.
|
|||
/// Note: this API supports the OpenIddict infrastructure and is not intended to be used
|
|||
/// directly from your code. This API may change or be removed in future minor releases.
|
|||
/// </summary>
|
|||
public class OpenIddictMvcConfiguration : IConfigureOptions<MvcOptions> |
|||
{ |
|||
/// <summary>
|
|||
/// Registers the OpenIddict MVC components in the MVC options.
|
|||
/// </summary>
|
|||
/// <param name="options">The options instance to initialize.</param>
|
|||
public void Configure([NotNull] MvcOptions options) |
|||
{ |
|||
if (options == null) |
|||
{ |
|||
throw new ArgumentNullException(nameof(options)); |
|||
} |
|||
|
|||
options.ModelBinderProviders.Insert(0, new OpenIddictMvcBinderProvider()); |
|||
options.ModelMetadataDetailsProviders.Add(new SuppressChildValidationMetadataProvider(typeof(OpenIdConnectRequest))); |
|||
options.ModelMetadataDetailsProviders.Add(new SuppressChildValidationMetadataProvider(typeof(OpenIdConnectResponse))); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,71 @@ |
|||
/* |
|||
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
|
|||
* See https://github.com/openiddict/openiddict-core for more information concerning
|
|||
* the license and the contributors participating to this project. |
|||
*/ |
|||
|
|||
using System; |
|||
using System.Linq; |
|||
using AspNet.Security.OpenIdConnect.Primitives; |
|||
using Microsoft.AspNetCore.Mvc; |
|||
using Microsoft.AspNetCore.Mvc.ModelBinding; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using OpenIddict.Mvc.Internal; |
|||
using Xunit; |
|||
|
|||
namespace OpenIddict.Mvc.Tests |
|||
{ |
|||
public class OpenIddictConfigurationExtensionsTests |
|||
{ |
|||
[Fact] |
|||
public void Configure_ThrowsAnExceptionForNullOptions() |
|||
{ |
|||
// Arrange
|
|||
var configuration = new OpenIddictMvcConfiguration(); |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<ArgumentNullException>(() => configuration.Configure(null)); |
|||
|
|||
Assert.Equal("options", exception.ParamName); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Configure_RegistersModelBinderProvider() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddOptions(); |
|||
|
|||
var builder = new OpenIddictServerBuilder(services); |
|||
|
|||
// Act
|
|||
builder.UseMvc(); |
|||
|
|||
var options = services.BuildServiceProvider().GetRequiredService<IOptions<MvcOptions>>(); |
|||
|
|||
// Assert
|
|||
Assert.Contains(options.Value.ModelBinderProviders, binder => binder is OpenIddictMvcBinderProvider); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Configure_RegistersModelMetadataDetailsProviders() |
|||
{ |
|||
// Arrange
|
|||
var services = new ServiceCollection(); |
|||
services.AddOptions(); |
|||
|
|||
var builder = new OpenIddictServerBuilder(services); |
|||
|
|||
// Act
|
|||
builder.UseMvc(); |
|||
|
|||
var options = services.BuildServiceProvider().GetRequiredService<IOptions<MvcOptions>>(); |
|||
|
|||
// Assert
|
|||
var providers = options.Value.ModelMetadataDetailsProviders.OfType<SuppressChildValidationMetadataProvider>(); |
|||
Assert.Contains(providers, provider => provider.Type == typeof(OpenIdConnectRequest)); |
|||
Assert.Contains(providers, provider => provider.Type == typeof(OpenIdConnectResponse)); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue