From b6636d2dbcd416a435e93d88ffc25d7ece6593f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Chalet?= Date: Sun, 13 Dec 2015 19:29:18 +0100 Subject: [PATCH] Introduce module naming to prevent multiple registrations --- src/OpenIddict.Assets/OpenIddictExtensions.cs | 2 +- src/OpenIddict.Core/OpenIddictExtensions.cs | 13 ++++++++++--- src/OpenIddict.Core/OpenIddictModule.cs | 5 +++++ src/OpenIddict.Mvc/OpenIddictController.cs | 2 +- src/OpenIddict.Mvc/OpenIddictExtensions.cs | 7 ++++--- 5 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/OpenIddict.Assets/OpenIddictExtensions.cs b/src/OpenIddict.Assets/OpenIddictExtensions.cs index adfd7431..c7fe4abf 100644 --- a/src/OpenIddict.Assets/OpenIddictExtensions.cs +++ b/src/OpenIddict.Assets/OpenIddictExtensions.cs @@ -12,7 +12,7 @@ using Microsoft.Extensions.Internal; namespace Microsoft.AspNet.Builder { public static class OpenIddictExtensions { public static OpenIddictBuilder UseAssets([NotNull] this OpenIddictBuilder builder) { - return builder.AddModule(-20, app => app.UseStaticFiles(new StaticFileOptions { + return builder.AddModule("Assets", -20, app => app.UseStaticFiles(new StaticFileOptions { FileProvider = new EmbeddedFileProvider( assembly: Assembly.Load(new AssemblyName("OpenIddict.Assets")), baseNamespace: "OpenIddict.Assets") diff --git a/src/OpenIddict.Core/OpenIddictExtensions.cs b/src/OpenIddict.Core/OpenIddictExtensions.cs index b0df222c..3c037ab1 100644 --- a/src/OpenIddict.Core/OpenIddictExtensions.cs +++ b/src/OpenIddict.Core/OpenIddictExtensions.cs @@ -46,9 +46,16 @@ namespace Microsoft.AspNet.Builder { } public static OpenIddictBuilder AddModule( - [NotNull] this OpenIddictBuilder builder, int position, + [NotNull] this OpenIddictBuilder builder, + [NotNull] string name, int position, [NotNull] Action registration) { + // By default, prevent duplicate registrations. + if (builder.Modules.Any(module => string.Equals(module.Name, name))) { + return builder; + } + builder.Modules.Add(new OpenIddictModule { + Name = name, Position = position, Registration = registration }); @@ -74,7 +81,7 @@ namespace Microsoft.AspNet.Builder { configuration(builder); - builder.AddModule(-10, map => map.UseCors(options => { + builder.AddModule("CORS", -10, map => map.UseCors(options => { options.AllowAnyHeader(); options.AllowAnyMethod(); options.AllowAnyOrigin(); @@ -82,7 +89,7 @@ namespace Microsoft.AspNet.Builder { })); // Add OpenIdConnectServerMiddleware to the ASP.NET 5 pipeline. - builder.AddModule(0, map => map.UseOpenIdConnectServer(builder.Options)); + builder.AddModule("ASOS", 0, map => map.UseOpenIdConnectServer(builder.Options)); // Register the OpenIddict modules in the ASP.NET 5 pipeline. foreach (var module in builder.Modules.OrderBy(module => module.Position)) { diff --git a/src/OpenIddict.Core/OpenIddictModule.cs b/src/OpenIddict.Core/OpenIddictModule.cs index 040e8ebf..a8bac69f 100644 --- a/src/OpenIddict.Core/OpenIddictModule.cs +++ b/src/OpenIddict.Core/OpenIddictModule.cs @@ -6,6 +6,11 @@ namespace OpenIddict { /// Defines an OpenIddict module. /// public class OpenIddictModule { + /// + /// Gets or sets the name of the module. + /// + public string Name { get; set; } + /// /// Gets or sets the position of the module in the ASP.NET pipeline. /// diff --git a/src/OpenIddict.Mvc/OpenIddictController.cs b/src/OpenIddict.Mvc/OpenIddictController.cs index 2c3b6a52..b01def11 100644 --- a/src/OpenIddict.Mvc/OpenIddictController.cs +++ b/src/OpenIddict.Mvc/OpenIddictController.cs @@ -18,7 +18,7 @@ using Microsoft.AspNet.Mvc; using Microsoft.Extensions.Internal; using Microsoft.IdentityModel.Protocols.OpenIdConnect; -namespace OpenIddict { +namespace OpenIddict.Mvc { // Note: this controller is generic and doesn't need to be marked as internal to prevent MVC from discovering it. public class OpenIddictController : Controller where TUser : class where TApplication : class { public OpenIddictController( diff --git a/src/OpenIddict.Mvc/OpenIddictExtensions.cs b/src/OpenIddict.Mvc/OpenIddictExtensions.cs index 2bb63321..696ecce7 100644 --- a/src/OpenIddict.Mvc/OpenIddictExtensions.cs +++ b/src/OpenIddict.Mvc/OpenIddictExtensions.cs @@ -15,11 +15,12 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Internal; using NWebsec.Middleware; using OpenIddict; +using OpenIddict.Mvc; namespace Microsoft.AspNet.Builder { public static class OpenIddictExtensions { public static OpenIddictBuilder UseMvc([NotNull] this OpenIddictBuilder builder) { - builder.AddModule(-20, app => { + builder.AddModule("NWebsec", -20, app => { // Insert a new middleware responsible of setting the Content-Security-Policy header. // See https://nwebsec.codeplex.com/wikipage?title=Configuring%20Content%20Security%20Policy&referringTitle=NWebsec app.UseCsp(options => options.DefaultSources(directive => directive.Self()) @@ -41,7 +42,7 @@ namespace Microsoft.AspNet.Builder { }); // Run the rest of the pipeline in an isolated environment. - builder.AddModule(10, app => app.Isolate(map => map.UseMvc(routes => { + builder.AddModule("MVC", 10, app => app.Isolate(map => map.UseMvc(routes => { // Register the actions corresponding to the authorization endpoint. if (builder.Options.AuthorizationEndpointPath.HasValue) { routes.MapRoute("{D97891B4}", builder.Options.AuthorizationEndpointPath.Value.Substring(1), new { @@ -83,7 +84,7 @@ namespace Microsoft.AspNet.Builder { options.FileProvider, new EmbeddedFileProvider( assembly: typeof(OpenIddictController<,>).GetTypeInfo().Assembly, - baseNamespace: "OpenIddict.Mvc")); + baseNamespace: typeof(OpenIddictController<,>).Namespace)); }); // Register the sign-in manager in the isolated container.