mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 15:18:14 +08:00
Feature/rename middleware (#637)
* #630 only set status code if response hasnt started, otherwise exception * #623 made {RemoteIpAddress} available as placeholder so you can do x-forwarded-for * #623 local address different on mac, windows and linux for integration test * renamed some middlewares so they make more sense * add downstreamroutefindermiddleware benchmark
This commit is contained in:
@ -1,46 +1,46 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Infrastructure.Claims.Parser;
|
||||
using Ocelot.Responses;
|
||||
|
||||
namespace Ocelot.Claims
|
||||
{
|
||||
public class AddClaimsToRequest : IAddClaimsToRequest
|
||||
{
|
||||
private readonly IClaimsParser _claimsParser;
|
||||
|
||||
public AddClaimsToRequest(IClaimsParser claimsParser)
|
||||
{
|
||||
_claimsParser = claimsParser;
|
||||
}
|
||||
|
||||
public Response SetClaimsOnContext(List<ClaimToThing> claimsToThings, HttpContext context)
|
||||
{
|
||||
foreach (var config in claimsToThings)
|
||||
{
|
||||
var value = _claimsParser.GetValue(context.User.Claims, config.NewKey, config.Delimiter, config.Index);
|
||||
|
||||
if (value.IsError)
|
||||
{
|
||||
return new ErrorResponse(value.Errors);
|
||||
}
|
||||
|
||||
var exists = context.User.Claims.FirstOrDefault(x => x.Type == config.ExistingKey);
|
||||
|
||||
var identity = context.User.Identity as ClaimsIdentity;
|
||||
|
||||
if (exists != null)
|
||||
{
|
||||
identity?.RemoveClaim(exists);
|
||||
}
|
||||
|
||||
identity?.AddClaim(new System.Security.Claims.Claim(config.ExistingKey, value.Data));
|
||||
}
|
||||
|
||||
return new OkResponse();
|
||||
}
|
||||
}
|
||||
}
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Infrastructure.Claims.Parser;
|
||||
using Ocelot.Responses;
|
||||
|
||||
namespace Ocelot.Claims
|
||||
{
|
||||
public class AddClaimsToRequest : IAddClaimsToRequest
|
||||
{
|
||||
private readonly IClaimsParser _claimsParser;
|
||||
|
||||
public AddClaimsToRequest(IClaimsParser claimsParser)
|
||||
{
|
||||
_claimsParser = claimsParser;
|
||||
}
|
||||
|
||||
public Response SetClaimsOnContext(List<ClaimToThing> claimsToThings, HttpContext context)
|
||||
{
|
||||
foreach (var config in claimsToThings)
|
||||
{
|
||||
var value = _claimsParser.GetValue(context.User.Claims, config.NewKey, config.Delimiter, config.Index);
|
||||
|
||||
if (value.IsError)
|
||||
{
|
||||
return new ErrorResponse(value.Errors);
|
||||
}
|
||||
|
||||
var exists = context.User.Claims.FirstOrDefault(x => x.Type == config.ExistingKey);
|
||||
|
||||
var identity = context.User.Identity as ClaimsIdentity;
|
||||
|
||||
if (exists != null)
|
||||
{
|
||||
identity?.RemoveClaim(exists);
|
||||
}
|
||||
|
||||
identity?.AddClaim(new System.Security.Claims.Claim(config.ExistingKey, value.Data));
|
||||
}
|
||||
|
||||
return new OkResponse();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,13 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Ocelot.Middleware.Pipeline;
|
||||
|
||||
namespace Ocelot.Claims.Middleware
|
||||
{
|
||||
public static class ClaimsBuilderMiddlewareExtensions
|
||||
{
|
||||
public static IOcelotPipelineBuilder UseClaimsBuilderMiddleware(this IOcelotPipelineBuilder builder)
|
||||
{
|
||||
return builder.UseMiddleware<ClaimsBuilderMiddleware>();
|
||||
}
|
||||
}
|
||||
}
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Ocelot.Middleware.Pipeline;
|
||||
|
||||
namespace Ocelot.Claims.Middleware
|
||||
{
|
||||
public static class ClaimsToClaimsMiddlewareExtensions
|
||||
{
|
||||
public static IOcelotPipelineBuilder UseClaimsToClaimsMiddleware(this IOcelotPipelineBuilder builder)
|
||||
{
|
||||
return builder.UseMiddleware<ClaimsToClaimsMiddleware>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,45 +1,45 @@
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.DownstreamRouteFinder.Middleware;
|
||||
using Ocelot.Infrastructure.RequestData;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Middleware;
|
||||
|
||||
namespace Ocelot.Claims.Middleware
|
||||
{
|
||||
public class ClaimsBuilderMiddleware : OcelotMiddleware
|
||||
{
|
||||
private readonly OcelotRequestDelegate _next;
|
||||
private readonly IAddClaimsToRequest _addClaimsToRequest;
|
||||
|
||||
public ClaimsBuilderMiddleware(OcelotRequestDelegate next,
|
||||
IOcelotLoggerFactory loggerFactory,
|
||||
IAddClaimsToRequest addClaimsToRequest)
|
||||
:base(loggerFactory.CreateLogger<ClaimsBuilderMiddleware>())
|
||||
{
|
||||
_next = next;
|
||||
_addClaimsToRequest = addClaimsToRequest;
|
||||
}
|
||||
|
||||
public async Task Invoke(DownstreamContext context)
|
||||
{
|
||||
if (context.DownstreamReRoute.ClaimsToClaims.Any())
|
||||
{
|
||||
Logger.LogDebug("this route has instructions to convert claims to other claims");
|
||||
|
||||
var result = _addClaimsToRequest.SetClaimsOnContext(context.DownstreamReRoute.ClaimsToClaims, context.HttpContext);
|
||||
|
||||
if (result.IsError)
|
||||
{
|
||||
Logger.LogDebug("error converting claims to other claims, setting pipeline error");
|
||||
|
||||
SetPipelineError(context, result.Errors);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
await _next.Invoke(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.DownstreamRouteFinder.Middleware;
|
||||
using Ocelot.Infrastructure.RequestData;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Middleware;
|
||||
|
||||
namespace Ocelot.Claims.Middleware
|
||||
{
|
||||
public class ClaimsToClaimsMiddleware : OcelotMiddleware
|
||||
{
|
||||
private readonly OcelotRequestDelegate _next;
|
||||
private readonly IAddClaimsToRequest _addClaimsToRequest;
|
||||
|
||||
public ClaimsToClaimsMiddleware(OcelotRequestDelegate next,
|
||||
IOcelotLoggerFactory loggerFactory,
|
||||
IAddClaimsToRequest addClaimsToRequest)
|
||||
:base(loggerFactory.CreateLogger<ClaimsToClaimsMiddleware>())
|
||||
{
|
||||
_next = next;
|
||||
_addClaimsToRequest = addClaimsToRequest;
|
||||
}
|
||||
|
||||
public async Task Invoke(DownstreamContext context)
|
||||
{
|
||||
if (context.DownstreamReRoute.ClaimsToClaims.Any())
|
||||
{
|
||||
Logger.LogDebug("this route has instructions to convert claims to other claims");
|
||||
|
||||
var result = _addClaimsToRequest.SetClaimsOnContext(context.DownstreamReRoute.ClaimsToClaims, context.HttpContext);
|
||||
|
||||
if (result.IsError)
|
||||
{
|
||||
Logger.LogDebug("error converting claims to other claims, setting pipeline error");
|
||||
|
||||
SetPipelineError(context, result.Errors);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
await _next.Invoke(context);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,83 +1,83 @@
|
||||
namespace Ocelot.Headers
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Infrastructure;
|
||||
using Logging;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Infrastructure.Claims.Parser;
|
||||
using Ocelot.Responses;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using Ocelot.Configuration.Creator;
|
||||
using Ocelot.Middleware;
|
||||
using Ocelot.Request.Middleware;
|
||||
|
||||
public class AddHeadersToRequest : IAddHeadersToRequest
|
||||
{
|
||||
private readonly IClaimsParser _claimsParser;
|
||||
private readonly IPlaceholders _placeholders;
|
||||
private readonly IOcelotLogger _logger;
|
||||
|
||||
public AddHeadersToRequest(IClaimsParser claimsParser, IPlaceholders placeholders, IOcelotLoggerFactory factory)
|
||||
{
|
||||
_logger = factory.CreateLogger<AddHeadersToRequest>();
|
||||
_claimsParser = claimsParser;
|
||||
_placeholders = placeholders;
|
||||
}
|
||||
|
||||
public Response SetHeadersOnDownstreamRequest(List<ClaimToThing> claimsToThings, IEnumerable<System.Security.Claims.Claim> claims, DownstreamRequest downstreamRequest)
|
||||
{
|
||||
foreach (var config in claimsToThings)
|
||||
{
|
||||
var value = _claimsParser.GetValue(claims, config.NewKey, config.Delimiter, config.Index);
|
||||
|
||||
if (value.IsError)
|
||||
{
|
||||
return new ErrorResponse(value.Errors);
|
||||
}
|
||||
|
||||
var exists = downstreamRequest.Headers.FirstOrDefault(x => x.Key == config.ExistingKey);
|
||||
|
||||
if (!string.IsNullOrEmpty(exists.Key))
|
||||
{
|
||||
downstreamRequest.Headers.Remove(exists.Key);
|
||||
}
|
||||
|
||||
downstreamRequest.Headers.Add(config.ExistingKey, value.Data);
|
||||
}
|
||||
|
||||
return new OkResponse();
|
||||
}
|
||||
|
||||
public void SetHeadersOnDownstreamRequest(IEnumerable<AddHeader> headers, HttpContext context)
|
||||
{
|
||||
var requestHeader = context.Request.Headers;
|
||||
|
||||
foreach (var header in headers)
|
||||
{
|
||||
if (requestHeader.ContainsKey(header.Key))
|
||||
{
|
||||
requestHeader.Remove(header.Key);
|
||||
}
|
||||
|
||||
if (header.Value.StartsWith("{") && header.Value.EndsWith("}"))
|
||||
{
|
||||
var value = _placeholders.Get(header.Value);
|
||||
|
||||
if (value.IsError)
|
||||
{
|
||||
_logger.LogWarning($"Unable to add header to response {header.Key}: {header.Value}");
|
||||
continue;
|
||||
}
|
||||
|
||||
requestHeader.Add(header.Key, new StringValues(value.Data));
|
||||
}
|
||||
else
|
||||
{
|
||||
requestHeader.Add(header.Key, header.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
namespace Ocelot.Headers
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Infrastructure;
|
||||
using Logging;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Infrastructure.Claims.Parser;
|
||||
using Ocelot.Responses;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using Ocelot.Configuration.Creator;
|
||||
using Ocelot.Middleware;
|
||||
using Ocelot.Request.Middleware;
|
||||
|
||||
public class AddHeadersToRequest : IAddHeadersToRequest
|
||||
{
|
||||
private readonly IClaimsParser _claimsParser;
|
||||
private readonly IPlaceholders _placeholders;
|
||||
private readonly IOcelotLogger _logger;
|
||||
|
||||
public AddHeadersToRequest(IClaimsParser claimsParser, IPlaceholders placeholders, IOcelotLoggerFactory factory)
|
||||
{
|
||||
_logger = factory.CreateLogger<AddHeadersToRequest>();
|
||||
_claimsParser = claimsParser;
|
||||
_placeholders = placeholders;
|
||||
}
|
||||
|
||||
public Response SetHeadersOnDownstreamRequest(List<ClaimToThing> claimsToThings, IEnumerable<System.Security.Claims.Claim> claims, DownstreamRequest downstreamRequest)
|
||||
{
|
||||
foreach (var config in claimsToThings)
|
||||
{
|
||||
var value = _claimsParser.GetValue(claims, config.NewKey, config.Delimiter, config.Index);
|
||||
|
||||
if (value.IsError)
|
||||
{
|
||||
return new ErrorResponse(value.Errors);
|
||||
}
|
||||
|
||||
var exists = downstreamRequest.Headers.FirstOrDefault(x => x.Key == config.ExistingKey);
|
||||
|
||||
if (!string.IsNullOrEmpty(exists.Key))
|
||||
{
|
||||
downstreamRequest.Headers.Remove(exists.Key);
|
||||
}
|
||||
|
||||
downstreamRequest.Headers.Add(config.ExistingKey, value.Data);
|
||||
}
|
||||
|
||||
return new OkResponse();
|
||||
}
|
||||
|
||||
public void SetHeadersOnDownstreamRequest(IEnumerable<AddHeader> headers, HttpContext context)
|
||||
{
|
||||
var requestHeader = context.Request.Headers;
|
||||
|
||||
foreach (var header in headers)
|
||||
{
|
||||
if (requestHeader.ContainsKey(header.Key))
|
||||
{
|
||||
requestHeader.Remove(header.Key);
|
||||
}
|
||||
|
||||
if (header.Value.StartsWith("{") && header.Value.EndsWith("}"))
|
||||
{
|
||||
var value = _placeholders.Get(header.Value);
|
||||
|
||||
if (value.IsError)
|
||||
{
|
||||
_logger.LogWarning($"Unable to add header to response {header.Key}: {header.Value}");
|
||||
continue;
|
||||
}
|
||||
|
||||
requestHeader.Add(header.Key, new StringValues(value.Data));
|
||||
}
|
||||
else
|
||||
{
|
||||
requestHeader.Add(header.Key, header.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,25 +1,25 @@
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Responses;
|
||||
|
||||
namespace Ocelot.Headers
|
||||
{
|
||||
public class HttpContextRequestHeaderReplacer : IHttpContextRequestHeaderReplacer
|
||||
{
|
||||
public Response Replace(HttpContext context, List<HeaderFindAndReplace> fAndRs)
|
||||
{
|
||||
foreach (var f in fAndRs)
|
||||
{
|
||||
if(context.Request.Headers.TryGetValue(f.Key, out var values))
|
||||
{
|
||||
var replaced = values[f.Index].Replace(f.Find, f.Replace);
|
||||
context.Request.Headers.Remove(f.Key);
|
||||
context.Request.Headers.Add(f.Key, replaced);
|
||||
}
|
||||
}
|
||||
|
||||
return new OkResponse();
|
||||
}
|
||||
}
|
||||
}
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Responses;
|
||||
|
||||
namespace Ocelot.Headers
|
||||
{
|
||||
public class HttpContextRequestHeaderReplacer : IHttpContextRequestHeaderReplacer
|
||||
{
|
||||
public Response Replace(HttpContext context, List<HeaderFindAndReplace> fAndRs)
|
||||
{
|
||||
foreach (var f in fAndRs)
|
||||
{
|
||||
if(context.Request.Headers.TryGetValue(f.Key, out var values))
|
||||
{
|
||||
var replaced = values[f.Index].Replace(f.Find, f.Replace);
|
||||
context.Request.Headers.Remove(f.Key);
|
||||
context.Request.Headers.Add(f.Key, replaced);
|
||||
}
|
||||
}
|
||||
|
||||
return new OkResponse();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,47 +1,47 @@
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.DownstreamRouteFinder.Middleware;
|
||||
using Ocelot.Infrastructure.RequestData;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Middleware;
|
||||
|
||||
namespace Ocelot.Headers.Middleware
|
||||
{
|
||||
public class HttpRequestHeadersBuilderMiddleware : OcelotMiddleware
|
||||
{
|
||||
private readonly OcelotRequestDelegate _next;
|
||||
private readonly IAddHeadersToRequest _addHeadersToRequest;
|
||||
|
||||
public HttpRequestHeadersBuilderMiddleware(OcelotRequestDelegate next,
|
||||
IOcelotLoggerFactory loggerFactory,
|
||||
IAddHeadersToRequest addHeadersToRequest)
|
||||
:base(loggerFactory.CreateLogger<HttpRequestHeadersBuilderMiddleware>())
|
||||
{
|
||||
_next = next;
|
||||
_addHeadersToRequest = addHeadersToRequest;
|
||||
}
|
||||
|
||||
public async Task Invoke(DownstreamContext context)
|
||||
{
|
||||
if (context.DownstreamReRoute.ClaimsToHeaders.Any())
|
||||
{
|
||||
Logger.LogInformation($"{context.DownstreamReRoute.DownstreamPathTemplate.Value} has instructions to convert claims to headers");
|
||||
|
||||
var response = _addHeadersToRequest.SetHeadersOnDownstreamRequest(context.DownstreamReRoute.ClaimsToHeaders, context.HttpContext.User.Claims, context.DownstreamRequest);
|
||||
|
||||
if (response.IsError)
|
||||
{
|
||||
Logger.LogWarning("Error setting headers on context, setting pipeline error");
|
||||
|
||||
SetPipelineError(context, response.Errors);
|
||||
return;
|
||||
}
|
||||
|
||||
Logger.LogInformation("headers have been set on context");
|
||||
}
|
||||
|
||||
await _next.Invoke(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.DownstreamRouteFinder.Middleware;
|
||||
using Ocelot.Infrastructure.RequestData;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Middleware;
|
||||
|
||||
namespace Ocelot.Headers.Middleware
|
||||
{
|
||||
public class ClaimsToHeadersMiddleware : OcelotMiddleware
|
||||
{
|
||||
private readonly OcelotRequestDelegate _next;
|
||||
private readonly IAddHeadersToRequest _addHeadersToRequest;
|
||||
|
||||
public ClaimsToHeadersMiddleware(OcelotRequestDelegate next,
|
||||
IOcelotLoggerFactory loggerFactory,
|
||||
IAddHeadersToRequest addHeadersToRequest)
|
||||
:base(loggerFactory.CreateLogger<ClaimsToHeadersMiddleware>())
|
||||
{
|
||||
_next = next;
|
||||
_addHeadersToRequest = addHeadersToRequest;
|
||||
}
|
||||
|
||||
public async Task Invoke(DownstreamContext context)
|
||||
{
|
||||
if (context.DownstreamReRoute.ClaimsToHeaders.Any())
|
||||
{
|
||||
Logger.LogInformation($"{context.DownstreamReRoute.DownstreamPathTemplate.Value} has instructions to convert claims to headers");
|
||||
|
||||
var response = _addHeadersToRequest.SetHeadersOnDownstreamRequest(context.DownstreamReRoute.ClaimsToHeaders, context.HttpContext.User.Claims, context.DownstreamRequest);
|
||||
|
||||
if (response.IsError)
|
||||
{
|
||||
Logger.LogWarning("Error setting headers on context, setting pipeline error");
|
||||
|
||||
SetPipelineError(context, response.Errors);
|
||||
return;
|
||||
}
|
||||
|
||||
Logger.LogInformation("headers have been set on context");
|
||||
}
|
||||
|
||||
await _next.Invoke(context);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Ocelot.Middleware.Pipeline;
|
||||
|
||||
namespace Ocelot.Headers.Middleware
|
||||
{
|
||||
public static class ClaimsToHeadersMiddlewareExtensions
|
||||
{
|
||||
public static IOcelotPipelineBuilder UseClaimsToHeadersMiddleware(this IOcelotPipelineBuilder builder)
|
||||
{
|
||||
return builder.UseMiddleware<ClaimsToHeadersMiddleware>();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Ocelot.Middleware.Pipeline;
|
||||
|
||||
namespace Ocelot.Headers.Middleware
|
||||
{
|
||||
public static class HttpRequestHeadersBuilderMiddlewareExtensions
|
||||
{
|
||||
public static IOcelotPipelineBuilder UseHttpRequestHeadersBuilderMiddleware(this IOcelotPipelineBuilder builder)
|
||||
{
|
||||
return builder.UseMiddleware<HttpRequestHeadersBuilderMiddleware>();
|
||||
}
|
||||
}
|
||||
}
|
@ -91,7 +91,7 @@ namespace Ocelot.Middleware.Pipeline
|
||||
}
|
||||
|
||||
// The next thing we do is look at any claims transforms in case this is important for authorisation
|
||||
builder.UseClaimsBuilderMiddleware();
|
||||
builder.UseClaimsToClaimsMiddleware();
|
||||
|
||||
// Allow pre authorisation logic. The idea being people might want to run something custom before what is built in.
|
||||
builder.UseIfNotNull(pipelineConfiguration.PreAuthorisationMiddleware);
|
||||
@ -109,14 +109,14 @@ namespace Ocelot.Middleware.Pipeline
|
||||
builder.Use(pipelineConfiguration.AuthorisationMiddleware);
|
||||
}
|
||||
|
||||
// Now we can run any header transformation logic
|
||||
builder.UseHttpRequestHeadersBuilderMiddleware();
|
||||
// Now we can run the claims to headers transformation middleware
|
||||
builder.UseClaimsToHeadersMiddleware();
|
||||
|
||||
// Allow the user to implement their own query string manipulation logic
|
||||
builder.UseIfNotNull(pipelineConfiguration.PreQueryStringBuilderMiddleware);
|
||||
|
||||
// Now we can run any query string transformation logic
|
||||
builder.UseQueryStringBuilderMiddleware();
|
||||
// Now we can run any claims to query string transformation middleware
|
||||
builder.UseClaimsToQueryStringMiddleware();
|
||||
|
||||
// Get the load balancer for this request
|
||||
builder.UseLoadBalancingMiddleware();
|
||||
|
@ -1,45 +1,45 @@
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.DownstreamRouteFinder.Middleware;
|
||||
using Ocelot.Infrastructure.RequestData;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Middleware;
|
||||
|
||||
namespace Ocelot.QueryStrings.Middleware
|
||||
{
|
||||
public class QueryStringBuilderMiddleware : OcelotMiddleware
|
||||
{
|
||||
private readonly OcelotRequestDelegate _next;
|
||||
private readonly IAddQueriesToRequest _addQueriesToRequest;
|
||||
|
||||
public QueryStringBuilderMiddleware(OcelotRequestDelegate next,
|
||||
IOcelotLoggerFactory loggerFactory,
|
||||
IAddQueriesToRequest addQueriesToRequest)
|
||||
: base(loggerFactory.CreateLogger<QueryStringBuilderMiddleware>())
|
||||
{
|
||||
_next = next;
|
||||
_addQueriesToRequest = addQueriesToRequest;
|
||||
}
|
||||
|
||||
public async Task Invoke(DownstreamContext context)
|
||||
{
|
||||
if (context.DownstreamReRoute.ClaimsToQueries.Any())
|
||||
{
|
||||
Logger.LogInformation($"{context.DownstreamReRoute.DownstreamPathTemplate.Value} has instructions to convert claims to queries");
|
||||
|
||||
var response = _addQueriesToRequest.SetQueriesOnDownstreamRequest(context.DownstreamReRoute.ClaimsToQueries, context.HttpContext.User.Claims, context.DownstreamRequest);
|
||||
|
||||
if (response.IsError)
|
||||
{
|
||||
Logger.LogWarning("there was an error setting queries on context, setting pipeline error");
|
||||
|
||||
SetPipelineError(context, response.Errors);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
await _next.Invoke(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
namespace Ocelot.QueryStrings.Middleware
|
||||
{
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.DownstreamRouteFinder.Middleware;
|
||||
using Ocelot.Infrastructure.RequestData;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Middleware;
|
||||
|
||||
public class ClaimsToQueryStringMiddleware : OcelotMiddleware
|
||||
{
|
||||
private readonly OcelotRequestDelegate _next;
|
||||
private readonly IAddQueriesToRequest _addQueriesToRequest;
|
||||
|
||||
public ClaimsToQueryStringMiddleware(OcelotRequestDelegate next,
|
||||
IOcelotLoggerFactory loggerFactory,
|
||||
IAddQueriesToRequest addQueriesToRequest)
|
||||
: base(loggerFactory.CreateLogger<ClaimsToQueryStringMiddleware>())
|
||||
{
|
||||
_next = next;
|
||||
_addQueriesToRequest = addQueriesToRequest;
|
||||
}
|
||||
|
||||
public async Task Invoke(DownstreamContext context)
|
||||
{
|
||||
if (context.DownstreamReRoute.ClaimsToQueries.Any())
|
||||
{
|
||||
Logger.LogInformation($"{context.DownstreamReRoute.DownstreamPathTemplate.Value} has instructions to convert claims to queries");
|
||||
|
||||
var response = _addQueriesToRequest.SetQueriesOnDownstreamRequest(context.DownstreamReRoute.ClaimsToQueries, context.HttpContext.User.Claims, context.DownstreamRequest);
|
||||
|
||||
if (response.IsError)
|
||||
{
|
||||
Logger.LogWarning("there was an error setting queries on context, setting pipeline error");
|
||||
|
||||
SetPipelineError(context, response.Errors);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
await _next.Invoke(context);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
namespace Ocelot.QueryStrings.Middleware
|
||||
{
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Ocelot.Middleware.Pipeline;
|
||||
|
||||
public static class ClaimsToQueryStringMiddlewareExtensions
|
||||
{
|
||||
public static IOcelotPipelineBuilder UseClaimsToQueryStringMiddleware(this IOcelotPipelineBuilder builder)
|
||||
{
|
||||
return builder.UseMiddleware<ClaimsToQueryStringMiddleware>();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Ocelot.Middleware.Pipeline;
|
||||
|
||||
namespace Ocelot.QueryStrings.Middleware
|
||||
{
|
||||
public static class QueryStringBuilderMiddlewareExtensions
|
||||
{
|
||||
public static IOcelotPipelineBuilder UseQueryStringBuilderMiddleware(this IOcelotPipelineBuilder builder)
|
||||
{
|
||||
return builder.UseMiddleware<QueryStringBuilderMiddleware>();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user