diff --git a/docs/features/middlewareinjection.rst b/docs/features/middlewareinjection.rst index d1ee78478..6a4594a6b 100644 --- a/docs/features/middlewareinjection.rst +++ b/docs/features/middlewareinjection.rst @@ -29,7 +29,8 @@ The user can set functions against the following (see more in the `OcelotPipelin * ``AuthenticationMiddleware`` overrides Ocelot authentication middleware. [#f1]_ * ``PreAuthorizationMiddleware`` injection allows the user to run pre authorization logic and then call Ocelot authorization middleware. * ``AuthorizationMiddleware`` overrides Ocelots authorization middleware. [#f1]_ -* ``PreQueryStringBuilderMiddleware`` injection allows the user to manipulate the query string on the http request before it is passed to Ocelot request creator. +* ``ClaimsToHeadersMiddleware`` injection overrides Ocelots claims to headers middleware. +* ``PreQueryStringBuilderMiddleware`` injection allows the user to manipulate the query string on the http request before it is passed to Ocelots request creator. Obviously you can just add mentioned Ocelot middleware overridings as normal before the call to ``app.UseOcelot()``. It cannot be added after as Ocelot does not call the next Ocelot middleware overridings based on specified middleware configuration. diff --git a/src/Ocelot/Authentication/Middleware/AuthenticationMiddlewareExtensions.cs b/src/Ocelot/Authentication/Middleware/AuthenticationMiddlewareExtensions.cs deleted file mode 100644 index d0715e844..000000000 --- a/src/Ocelot/Authentication/Middleware/AuthenticationMiddlewareExtensions.cs +++ /dev/null @@ -1,11 +0,0 @@ -using Microsoft.AspNetCore.Builder; - -namespace Ocelot.Authentication.Middleware; - -public static class AuthenticationMiddlewareExtensions -{ - public static IApplicationBuilder UseAuthenticationMiddleware(this IApplicationBuilder builder) - { - return builder.UseMiddleware(); - } -} diff --git a/src/Ocelot/Authorization/Middleware/AuthorizationMiddlewareMiddlewareExtensions.cs b/src/Ocelot/Authorization/Middleware/AuthorizationMiddlewareMiddlewareExtensions.cs deleted file mode 100644 index c53701098..000000000 --- a/src/Ocelot/Authorization/Middleware/AuthorizationMiddlewareMiddlewareExtensions.cs +++ /dev/null @@ -1,12 +0,0 @@ -using Microsoft.AspNetCore.Builder; - -namespace Ocelot.Authorization.Middleware -{ - public static class AuthorizationMiddlewareMiddlewareExtensions - { - public static IApplicationBuilder UseAuthorizationMiddleware(this IApplicationBuilder builder) - { - return builder.UseMiddleware(); - } - } -} diff --git a/src/Ocelot/Headers/Middleware/ClaimsToHeadersMiddlewareExtensions.cs b/src/Ocelot/Headers/Middleware/ClaimsToHeadersMiddlewareExtensions.cs deleted file mode 100644 index 4923c27f4..000000000 --- a/src/Ocelot/Headers/Middleware/ClaimsToHeadersMiddlewareExtensions.cs +++ /dev/null @@ -1,12 +0,0 @@ -using Microsoft.AspNetCore.Builder; - -namespace Ocelot.Headers.Middleware -{ - public static class ClaimsToHeadersMiddlewareExtensions - { - public static IApplicationBuilder UseClaimsToHeadersMiddleware(this IApplicationBuilder builder) - { - return builder.UseMiddleware(); - } - } -} diff --git a/src/Ocelot/Middleware/OcelotPipelineConfiguration.cs b/src/Ocelot/Middleware/OcelotPipelineConfiguration.cs index 403b6e844..0a18a810a 100644 --- a/src/Ocelot/Middleware/OcelotPipelineConfiguration.cs +++ b/src/Ocelot/Middleware/OcelotPipelineConfiguration.cs @@ -47,6 +47,10 @@ public class OcelotPipelineConfiguration /// public Func, Task> AuthorizationMiddleware { get; set; } + /// This allows the user to completely override the Ocelot's . + /// A delegate object. + public Func, Task> ClaimsToHeadersMiddleware { get; set; } + /// /// This allows the user to implement there own query string manipulation logic. /// diff --git a/src/Ocelot/Middleware/OcelotPipelineExtensions.cs b/src/Ocelot/Middleware/OcelotPipelineExtensions.cs index 16f4a5cff..00a96e624 100644 --- a/src/Ocelot/Middleware/OcelotPipelineExtensions.cs +++ b/src/Ocelot/Middleware/OcelotPipelineExtensions.cs @@ -24,8 +24,7 @@ namespace Ocelot.Middleware { public static class OcelotPipelineExtensions { - public static RequestDelegate BuildOcelotPipeline(this IApplicationBuilder app, - OcelotPipelineConfiguration pipelineConfiguration) + public static RequestDelegate BuildOcelotPipeline(this IApplicationBuilder app, OcelotPipelineConfiguration pipelineConfiguration) { // this sets up the downstream context and gets the config app.UseDownstreamContextMiddleware(); @@ -89,16 +88,8 @@ public static RequestDelegate BuildOcelotPipeline(this IApplicationBuilder app, app.UseIfNotNull(pipelineConfiguration.PreAuthenticationMiddleware); // Now we know where the client is going to go we can authenticate them. - // We allow the ocelot middleware to be overriden by whatever the - // user wants - if (pipelineConfiguration.AuthenticationMiddleware == null) - { - app.UseAuthenticationMiddleware(); - } - else - { - app.Use(pipelineConfiguration.AuthenticationMiddleware); - } + // We allow the Ocelot middleware to be overriden by whatever the user wants. + app.UseIfNotNull(pipelineConfiguration.AuthenticationMiddleware); // The next thing we do is look at any claims transforms in case this is important for authorization app.UseClaimsToClaimsMiddleware(); @@ -106,21 +97,12 @@ public static RequestDelegate BuildOcelotPipeline(this IApplicationBuilder app, // Allow pre authorization logic. The idea being people might want to run something custom before what is built in. app.UseIfNotNull(pipelineConfiguration.PreAuthorizationMiddleware); - // Now we have authenticated and done any claims transformation we - // can authorize the request - // We allow the ocelot middleware to be overriden by whatever the - // user wants - if (pipelineConfiguration.AuthorizationMiddleware == null) - { - app.UseAuthorizationMiddleware(); - } - else - { - app.Use(pipelineConfiguration.AuthorizationMiddleware); - } + // Now we have authenticated and done any claims transformation, we can authorize the request by AuthorizationMiddleware. + // We allow the Ocelot middleware to be overriden by whatever the user wants. + app.UseIfNotNull(pipelineConfiguration.AuthorizationMiddleware); - // Now we can run the claims to headers transformation middleware - app.UseClaimsToHeadersMiddleware(); + // Now we can run the ClaimsToHeadersMiddleware: we allow the Ocelot middleware to be overriden by whatever the user wants. + app.UseIfNotNull(pipelineConfiguration.ClaimsToHeadersMiddleware); // Allow the user to implement their own query string manipulation logic app.UseIfNotNull(pipelineConfiguration.PreQueryStringBuilderMiddleware); @@ -146,13 +128,12 @@ public static RequestDelegate BuildOcelotPipeline(this IApplicationBuilder app, return app.Build(); } - private static void UseIfNotNull(this IApplicationBuilder builder, - Func, Task> middleware) - { - if (middleware != null) - { - builder.Use(middleware); - } - } + private static IApplicationBuilder UseIfNotNull(this IApplicationBuilder builder, Func, Task> middleware) + => middleware != null ? builder.Use(middleware) : builder; + + private static IApplicationBuilder UseIfNotNull(this IApplicationBuilder builder, Func, Task> middleware) + where TMiddleware : OcelotMiddleware => middleware != null + ? builder.Use(middleware) + : builder.UseMiddleware(); } }