Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowed to override the ClaimsToHeaders middleware #1403

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from
Open
2 changes: 2 additions & 0 deletions docs/features/middlewareinjection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ The user can set functions against the following.

* AuthorizationMiddleware - This overrides Ocelots authorization middleware.

* ClaimsToHeadersMiddleware - This overrides Ocelots claims to headers middleware.

* PreQueryStringBuilderMiddleware - This 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 middleware as normal before the call to app.UseOcelot() It cannot be added
Expand Down
15 changes: 11 additions & 4 deletions src/Ocelot/Middleware/OcelotPipelineConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;

namespace Ocelot.Middleware
{
public class OcelotPipelineConfiguration
Expand Down Expand Up @@ -59,6 +58,14 @@ public class OcelotPipelineConfiguration
/// </value>
public Func<HttpContext, Func<Task>, Task> PreQueryStringBuilderMiddleware { get; set; }

/// <summary>
/// This allows the user to completely override the Ocelot's Claims to Headers middleware.
/// </summary>
/// <value>
/// A <see cref="Func{HttpContext, TFunc, Task}"/> delegate object.
/// </value>
public Func<HttpContext, Func<Task>, Task> ClaimsToHeadersMiddleware { get; set; }

/// <summary>
/// This is an extension that will branch to different pipes.
/// </summary>
Expand Down
23 changes: 14 additions & 9 deletions src/Ocelot/Middleware/OcelotPipelineExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
using System;
using System.Threading.Tasks;

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;

using Ocelot.Multiplexer;

using Ocelot.Authentication.Middleware;
using Ocelot.Authorization.Middleware;
using Ocelot.Cache.Middleware;
Expand All @@ -16,6 +10,7 @@
using Ocelot.Errors.Middleware;
using Ocelot.Headers.Middleware;
using Ocelot.LoadBalancer.Middleware;
using Ocelot.Multiplexer;
using Ocelot.QueryStrings.Middleware;
using Ocelot.RateLimit.Middleware;
using Ocelot.Request.Middleware;
Expand All @@ -24,6 +19,8 @@
using Ocelot.Responder.Middleware;
using Ocelot.Security.Middleware;
using Ocelot.WebSockets.Middleware;
using System;
using System.Threading.Tasks;

namespace Ocelot.Middleware
{
Expand Down Expand Up @@ -124,8 +121,16 @@ public static class OcelotPipelineExtensions
app.Use(pipelineConfiguration.AuthorizationMiddleware);
}

// Now we can run the claims to headers transformation middleware
app.UseClaimsToHeadersMiddleware();
// Now we can run the claims to headers transformation middleware.
// We allow the ocelot middleware to be overriden by whatever the user wants
if (pipelineConfiguration.ClaimsToHeadersMiddleware == null)
{
app.UseClaimsToHeadersMiddleware();
}
else
{
app.Use(pipelineConfiguration.ClaimsToHeadersMiddleware);
}

// Allow the user to implement their own query string manipulation logic
app.UseIfNotNull(pipelineConfiguration.PreQueryStringBuilderMiddleware);
Expand Down