mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 20:30:50 +08:00 
			
		
		
		
	implemented adding claims as query strings to downstream route, removed some of the middleware injection optiosn as i have currently have no use case for them, general refactoring to use the OcelotMiddleware a bit more
This commit is contained in:
		@@ -4,7 +4,6 @@ using Microsoft.AspNetCore.Builder;
 | 
			
		||||
using Microsoft.AspNetCore.Http;
 | 
			
		||||
using Ocelot.Authentication.Handler.Factory;
 | 
			
		||||
using Ocelot.Configuration;
 | 
			
		||||
using Ocelot.DownstreamRouteFinder;
 | 
			
		||||
using Ocelot.Errors;
 | 
			
		||||
using Ocelot.Infrastructure.RequestData;
 | 
			
		||||
using Ocelot.Middleware;
 | 
			
		||||
@@ -14,7 +13,6 @@ namespace Ocelot.Authentication.Middleware
 | 
			
		||||
    public class AuthenticationMiddleware : OcelotMiddleware
 | 
			
		||||
    {
 | 
			
		||||
        private readonly RequestDelegate _next;
 | 
			
		||||
        private readonly IRequestScopedDataRepository _requestScopedDataRepository;
 | 
			
		||||
        private readonly IApplicationBuilder _app;
 | 
			
		||||
        private readonly IAuthenticationHandlerFactory _authHandlerFactory;
 | 
			
		||||
 | 
			
		||||
@@ -25,32 +23,23 @@ namespace Ocelot.Authentication.Middleware
 | 
			
		||||
            : base(requestScopedDataRepository)
 | 
			
		||||
        {
 | 
			
		||||
            _next = next;
 | 
			
		||||
            _requestScopedDataRepository = requestScopedDataRepository;
 | 
			
		||||
            _authHandlerFactory = authHandlerFactory;
 | 
			
		||||
            _app = app;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task Invoke(HttpContext context)
 | 
			
		||||
        {
 | 
			
		||||
            var downstreamRoute = _requestScopedDataRepository.Get<DownstreamRoute>("DownstreamRoute");
 | 
			
		||||
 | 
			
		||||
            if (downstreamRoute.IsError)
 | 
			
		||||
            if (IsAuthenticatedRoute(DownstreamRoute.ReRoute))
 | 
			
		||||
            {
 | 
			
		||||
                SetPipelineError(downstreamRoute.Errors);
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
                var authenticationHandler = _authHandlerFactory.Get(_app, DownstreamRoute.ReRoute.AuthenticationOptions);
 | 
			
		||||
 | 
			
		||||
            if (IsAuthenticatedRoute(downstreamRoute.Data.ReRoute))
 | 
			
		||||
            {
 | 
			
		||||
                var authenticationNext = _authHandlerFactory.Get(_app, downstreamRoute.Data.ReRoute.AuthenticationOptions);
 | 
			
		||||
 | 
			
		||||
                if (!authenticationNext.IsError)
 | 
			
		||||
                if (!authenticationHandler.IsError)
 | 
			
		||||
                {
 | 
			
		||||
                    await authenticationNext.Data.Handler.Invoke(context);
 | 
			
		||||
                    await authenticationHandler.Data.Handler.Invoke(context);
 | 
			
		||||
                }
 | 
			
		||||
                else
 | 
			
		||||
                {
 | 
			
		||||
                    SetPipelineError(authenticationNext.Errors);
 | 
			
		||||
                    SetPipelineError(authenticationHandler.Errors);
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                if (context.User.Identity.IsAuthenticated)
 | 
			
		||||
 
 | 
			
		||||
@@ -1,10 +1,10 @@
 | 
			
		||||
using Ocelot.Infrastructure.RequestData;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Authorisation.Middleware
 | 
			
		||||
{
 | 
			
		||||
    using System.Collections.Generic;
 | 
			
		||||
    using System.Threading.Tasks;
 | 
			
		||||
    using DownstreamRouteFinder;
 | 
			
		||||
    using Errors;
 | 
			
		||||
    using Microsoft.AspNetCore.Http;
 | 
			
		||||
    using Ocelot.Middleware;
 | 
			
		||||
@@ -12,7 +12,6 @@ namespace Ocelot.Authorisation.Middleware
 | 
			
		||||
    public class AuthorisationMiddleware : OcelotMiddleware
 | 
			
		||||
    {
 | 
			
		||||
        private readonly RequestDelegate _next;
 | 
			
		||||
        private readonly IRequestScopedDataRepository _requestScopedDataRepository;
 | 
			
		||||
        private readonly IAuthoriser _authoriser;
 | 
			
		||||
 | 
			
		||||
        public AuthorisationMiddleware(RequestDelegate next,
 | 
			
		||||
@@ -21,23 +20,14 @@ namespace Ocelot.Authorisation.Middleware
 | 
			
		||||
            : base(requestScopedDataRepository)
 | 
			
		||||
        {
 | 
			
		||||
            _next = next;
 | 
			
		||||
            _requestScopedDataRepository = requestScopedDataRepository;
 | 
			
		||||
            _authoriser = authoriser;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task Invoke(HttpContext context)
 | 
			
		||||
        {
 | 
			
		||||
            var downstreamRoute = _requestScopedDataRepository.Get<DownstreamRoute>("DownstreamRoute");
 | 
			
		||||
 | 
			
		||||
            if (downstreamRoute.IsError)
 | 
			
		||||
            if (DownstreamRoute.ReRoute.IsAuthorised)
 | 
			
		||||
            {
 | 
			
		||||
                SetPipelineError(downstreamRoute.Errors);
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (downstreamRoute.Data.ReRoute.IsAuthorised)
 | 
			
		||||
            {
 | 
			
		||||
                var authorised = _authoriser.Authorise(context.User, downstreamRoute.Data.ReRoute.RouteClaimsRequirement);
 | 
			
		||||
                var authorised = _authoriser.Authorise(context.User, DownstreamRoute.ReRoute.RouteClaimsRequirement);
 | 
			
		||||
 | 
			
		||||
                if (authorised.IsError)
 | 
			
		||||
                {
 | 
			
		||||
@@ -45,7 +35,7 @@ namespace Ocelot.Authorisation.Middleware
 | 
			
		||||
                    return;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                if (authorised.Data)
 | 
			
		||||
                if (IsAuthorised(authorised))
 | 
			
		||||
                {
 | 
			
		||||
                    await _next.Invoke(context);
 | 
			
		||||
                }
 | 
			
		||||
@@ -54,7 +44,7 @@ namespace Ocelot.Authorisation.Middleware
 | 
			
		||||
                    SetPipelineError(new List<Error>
 | 
			
		||||
                    {
 | 
			
		||||
                        new UnauthorisedError(
 | 
			
		||||
                            $"{context.User.Identity.Name} unable to access {downstreamRoute.Data.ReRoute.UpstreamTemplate}")
 | 
			
		||||
                            $"{context.User.Identity.Name} unable to access {DownstreamRoute.ReRoute.UpstreamTemplate}")
 | 
			
		||||
                    });
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
@@ -63,5 +53,10 @@ namespace Ocelot.Authorisation.Middleware
 | 
			
		||||
                await _next.Invoke(context);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private static bool IsAuthorised(Response<bool> authorised)
 | 
			
		||||
        {
 | 
			
		||||
            return authorised.Data;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,13 +1,13 @@
 | 
			
		||||
namespace Ocelot.ClaimsBuilder
 | 
			
		||||
{
 | 
			
		||||
    using System.Collections.Generic;
 | 
			
		||||
    using System.Linq;
 | 
			
		||||
    using System.Security.Claims;
 | 
			
		||||
    using Configuration;
 | 
			
		||||
    using Infrastructure.Claims.Parser;
 | 
			
		||||
    using Microsoft.AspNetCore.Http;
 | 
			
		||||
    using Responses;
 | 
			
		||||
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;
 | 
			
		||||
@@ -37,7 +37,7 @@
 | 
			
		||||
                    identity?.RemoveClaim(exists);
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                identity?.AddClaim(new Claim(config.ExistingKey, value.Data));
 | 
			
		||||
                identity?.AddClaim(new System.Security.Claims.Claim(config.ExistingKey, value.Data));
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            return new OkResponse();
 | 
			
		||||
@@ -1,10 +1,10 @@
 | 
			
		||||
namespace Ocelot.ClaimsBuilder
 | 
			
		||||
{
 | 
			
		||||
    using System.Collections.Generic;
 | 
			
		||||
    using Configuration;
 | 
			
		||||
    using Microsoft.AspNetCore.Http;
 | 
			
		||||
    using Responses;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using Microsoft.AspNetCore.Http;
 | 
			
		||||
using Ocelot.Configuration;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Claims
 | 
			
		||||
{
 | 
			
		||||
    public interface IAddClaimsToRequest
 | 
			
		||||
    {
 | 
			
		||||
        Response SetClaimsOnContext(List<ClaimToThing> claimsToThings,
 | 
			
		||||
@@ -1,18 +1,15 @@
 | 
			
		||||
using Ocelot.Infrastructure.RequestData;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
using Microsoft.AspNetCore.Http;
 | 
			
		||||
using Ocelot.Infrastructure.RequestData;
 | 
			
		||||
using Ocelot.Middleware;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.ClaimsBuilder.Middleware
 | 
			
		||||
namespace Ocelot.Claims.Middleware
 | 
			
		||||
{
 | 
			
		||||
    using System.Linq;
 | 
			
		||||
    using System.Threading.Tasks;
 | 
			
		||||
    using DownstreamRouteFinder;
 | 
			
		||||
    using Microsoft.AspNetCore.Http;
 | 
			
		||||
    using Ocelot.Middleware;
 | 
			
		||||
 | 
			
		||||
    public class ClaimsBuilderMiddleware : OcelotMiddleware
 | 
			
		||||
    {
 | 
			
		||||
        private readonly RequestDelegate _next;
 | 
			
		||||
        private readonly IAddClaimsToRequest _addClaimsToRequest;
 | 
			
		||||
        private readonly IRequestScopedDataRepository _requestScopedDataRepository;
 | 
			
		||||
 | 
			
		||||
        public ClaimsBuilderMiddleware(RequestDelegate next, 
 | 
			
		||||
            IRequestScopedDataRepository requestScopedDataRepository,
 | 
			
		||||
@@ -21,16 +18,13 @@ namespace Ocelot.ClaimsBuilder.Middleware
 | 
			
		||||
        {
 | 
			
		||||
            _next = next;
 | 
			
		||||
            _addClaimsToRequest = addClaimsToRequest;
 | 
			
		||||
            _requestScopedDataRepository = requestScopedDataRepository;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task Invoke(HttpContext context)
 | 
			
		||||
        {
 | 
			
		||||
            var downstreamRoute = _requestScopedDataRepository.Get<DownstreamRoute>("DownstreamRoute");
 | 
			
		||||
 | 
			
		||||
            if (downstreamRoute.Data.ReRoute.ClaimsToClaims.Any())
 | 
			
		||||
            if (DownstreamRoute.ReRoute.ClaimsToClaims.Any())
 | 
			
		||||
            {
 | 
			
		||||
                var result = _addClaimsToRequest.SetClaimsOnContext(downstreamRoute.Data.ReRoute.ClaimsToClaims, context);
 | 
			
		||||
                var result = _addClaimsToRequest.SetClaimsOnContext(DownstreamRoute.ReRoute.ClaimsToClaims, context);
 | 
			
		||||
 | 
			
		||||
                if (result.IsError)
 | 
			
		||||
                {
 | 
			
		||||
@@ -1,7 +1,7 @@
 | 
			
		||||
namespace Ocelot.ClaimsBuilder.Middleware
 | 
			
		||||
{
 | 
			
		||||
    using Microsoft.AspNetCore.Builder;
 | 
			
		||||
using Microsoft.AspNetCore.Builder;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Claims.Middleware
 | 
			
		||||
{
 | 
			
		||||
    public static class ClaimsBuilderMiddlewareExtensions
 | 
			
		||||
    {
 | 
			
		||||
        public static IApplicationBuilder UseClaimsBuilderMiddleware(this IApplicationBuilder builder)
 | 
			
		||||
@@ -19,6 +19,7 @@ namespace Ocelot.Configuration.Builder
 | 
			
		||||
        private List<ClaimToThing> _claimToClaims;
 | 
			
		||||
        private Dictionary<string, string> _routeClaimRequirement;
 | 
			
		||||
        private bool _isAuthorised;
 | 
			
		||||
        private List<ClaimToThing> _claimToQueries;
 | 
			
		||||
 | 
			
		||||
        public ReRouteBuilder()
 | 
			
		||||
        {
 | 
			
		||||
@@ -113,9 +114,15 @@ namespace Ocelot.Configuration.Builder
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public ReRouteBuilder WithClaimsToQueries(List<ClaimToThing> input)
 | 
			
		||||
        {
 | 
			
		||||
            _claimToQueries = input;
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public ReRoute Build()
 | 
			
		||||
        {
 | 
			
		||||
            return new ReRoute(_downstreamTemplate, _upstreamTemplate, _upstreamHttpMethod, _upstreamTemplatePattern, _isAuthenticated, new AuthenticationOptions(_authenticationProvider, _authenticationProviderUrl, _scopeName, _requireHttps, _additionalScopes, _scopeSecret), _configHeaderExtractorProperties, _claimToClaims, _routeClaimRequirement, _isAuthorised);
 | 
			
		||||
            return new ReRoute(_downstreamTemplate, _upstreamTemplate, _upstreamHttpMethod, _upstreamTemplatePattern, _isAuthenticated, new AuthenticationOptions(_authenticationProvider, _authenticationProviderUrl, _scopeName, _requireHttps, _additionalScopes, _scopeSecret), _configHeaderExtractorProperties, _claimToClaims, _routeClaimRequirement, _isAuthorised, _claimToQueries);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -100,15 +100,16 @@ namespace Ocelot.Configuration.Creator
 | 
			
		||||
 | 
			
		||||
                var claimsToHeaders = GetAddThingsToRequest(reRoute.AddHeadersToRequest);
 | 
			
		||||
                var claimsToClaims = GetAddThingsToRequest(reRoute.AddClaimsToRequest);
 | 
			
		||||
                var claimsToQueries = GetAddThingsToRequest(reRoute.AddQueriesToRequest);
 | 
			
		||||
 | 
			
		||||
                return new ReRoute(reRoute.DownstreamTemplate, reRoute.UpstreamTemplate,
 | 
			
		||||
                    reRoute.UpstreamHttpMethod, upstreamTemplate, isAuthenticated,
 | 
			
		||||
                    authOptionsForRoute, claimsToHeaders, claimsToClaims, reRoute.RouteClaimsRequirement, isAuthorised
 | 
			
		||||
                    authOptionsForRoute, claimsToHeaders, claimsToClaims, reRoute.RouteClaimsRequirement, isAuthorised, claimsToQueries
 | 
			
		||||
                    );
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            return new ReRoute(reRoute.DownstreamTemplate, reRoute.UpstreamTemplate, reRoute.UpstreamHttpMethod,
 | 
			
		||||
                upstreamTemplate, isAuthenticated, null, new List<ClaimToThing>(), new List<ClaimToThing>(), reRoute.RouteClaimsRequirement, isAuthorised);
 | 
			
		||||
                upstreamTemplate, isAuthenticated, null, new List<ClaimToThing>(), new List<ClaimToThing>(), reRoute.RouteClaimsRequirement, isAuthorised, new List<ClaimToThing>());
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private List<ClaimToThing> GetAddThingsToRequest(Dictionary<string,string> thingBeingAdded)
 | 
			
		||||
 
 | 
			
		||||
@@ -4,7 +4,7 @@ namespace Ocelot.Configuration
 | 
			
		||||
{
 | 
			
		||||
    public class ReRoute
 | 
			
		||||
    {
 | 
			
		||||
        public ReRoute(string downstreamTemplate, string upstreamTemplate, string upstreamHttpMethod, string upstreamTemplatePattern, bool isAuthenticated, AuthenticationOptions authenticationOptions, List<ClaimToThing> configurationHeaderExtractorProperties, List<ClaimToThing> claimsToClaims, Dictionary<string, string> routeClaimsRequirement, bool isAuthorised)
 | 
			
		||||
        public ReRoute(string downstreamTemplate, string upstreamTemplate, string upstreamHttpMethod, string upstreamTemplatePattern, bool isAuthenticated, AuthenticationOptions authenticationOptions, List<ClaimToThing> configurationHeaderExtractorProperties, List<ClaimToThing> claimsToClaims, Dictionary<string, string> routeClaimsRequirement, bool isAuthorised, List<ClaimToThing> claimsToQueries)
 | 
			
		||||
        {
 | 
			
		||||
            DownstreamTemplate = downstreamTemplate;
 | 
			
		||||
            UpstreamTemplate = upstreamTemplate;
 | 
			
		||||
@@ -14,6 +14,8 @@ namespace Ocelot.Configuration
 | 
			
		||||
            AuthenticationOptions = authenticationOptions;
 | 
			
		||||
            RouteClaimsRequirement = routeClaimsRequirement;
 | 
			
		||||
            IsAuthorised = isAuthorised;
 | 
			
		||||
            ClaimsToQueries = claimsToQueries
 | 
			
		||||
                ?? new List<ClaimToThing>();
 | 
			
		||||
            ClaimsToClaims = claimsToClaims 
 | 
			
		||||
                ?? new List<ClaimToThing>();
 | 
			
		||||
            ClaimsToHeaders = configurationHeaderExtractorProperties 
 | 
			
		||||
@@ -27,6 +29,7 @@ namespace Ocelot.Configuration
 | 
			
		||||
        public bool IsAuthenticated { get; private set; }
 | 
			
		||||
        public bool IsAuthorised { get; private set; }
 | 
			
		||||
        public AuthenticationOptions AuthenticationOptions { get; private set; }
 | 
			
		||||
        public List<ClaimToThing> ClaimsToQueries { get; private set; }
 | 
			
		||||
        public List<ClaimToThing> ClaimsToHeaders { get; private set; }
 | 
			
		||||
        public List<ClaimToThing> ClaimsToClaims { get; private set; }
 | 
			
		||||
        public Dictionary<string, string> RouteClaimsRequirement { get; private set; }
 | 
			
		||||
 
 | 
			
		||||
@@ -9,6 +9,7 @@ namespace Ocelot.Configuration.Yaml
 | 
			
		||||
            AddHeadersToRequest = new Dictionary<string, string>();
 | 
			
		||||
            AddClaimsToRequest = new Dictionary<string, string>();
 | 
			
		||||
            RouteClaimsRequirement = new Dictionary<string, string>();
 | 
			
		||||
            AddQueriesToRequest = new Dictionary<string, string>();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public string DownstreamTemplate { get; set; }
 | 
			
		||||
@@ -18,5 +19,6 @@ namespace Ocelot.Configuration.Yaml
 | 
			
		||||
        public Dictionary<string, string> AddHeadersToRequest { get; set; }
 | 
			
		||||
        public Dictionary<string, string> AddClaimsToRequest { get; set; }
 | 
			
		||||
        public Dictionary<string, string> RouteClaimsRequirement { get; set; }
 | 
			
		||||
        public Dictionary<string, string> AddQueriesToRequest { get; set; } 
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -5,6 +5,7 @@ using Microsoft.Extensions.DependencyInjection;
 | 
			
		||||
using Ocelot.Authentication.Handler.Creator;
 | 
			
		||||
using Ocelot.Authentication.Handler.Factory;
 | 
			
		||||
using Ocelot.Authorisation;
 | 
			
		||||
using Ocelot.Claims;
 | 
			
		||||
using Ocelot.Configuration.Creator;
 | 
			
		||||
using Ocelot.Configuration.Parser;
 | 
			
		||||
using Ocelot.Configuration.Provider;
 | 
			
		||||
@@ -14,15 +15,15 @@ using Ocelot.Configuration.Yaml;
 | 
			
		||||
using Ocelot.DownstreamRouteFinder.Finder;
 | 
			
		||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
 | 
			
		||||
using Ocelot.DownstreamUrlCreator.UrlTemplateReplacer;
 | 
			
		||||
using Ocelot.HeaderBuilder;
 | 
			
		||||
using Ocelot.Headers;
 | 
			
		||||
using Ocelot.Infrastructure.RequestData;
 | 
			
		||||
using Ocelot.RequestBuilder.Builder;
 | 
			
		||||
using Ocelot.QueryStrings;
 | 
			
		||||
using Ocelot.Request.Builder;
 | 
			
		||||
using Ocelot.Requester;
 | 
			
		||||
using Ocelot.Responder;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.DependencyInjection
 | 
			
		||||
{
 | 
			
		||||
    using ClaimsBuilder;
 | 
			
		||||
    using Infrastructure.Claims.Parser;
 | 
			
		||||
 | 
			
		||||
    public static class ServiceCollectionExtensions
 | 
			
		||||
@@ -52,6 +53,7 @@ namespace Ocelot.DependencyInjection
 | 
			
		||||
            services.AddSingleton<IAuthoriser, ClaimsAuthoriser>();
 | 
			
		||||
            services.AddSingleton<IAddClaimsToRequest, AddClaimsToRequest>();
 | 
			
		||||
            services.AddSingleton<IAddHeadersToRequest, AddHeadersToRequest>();
 | 
			
		||||
            services.AddSingleton<IAddQueriesToRequest, AddQueriesToRequest>();
 | 
			
		||||
            services.AddSingleton<IClaimsParser, ClaimsParser>();
 | 
			
		||||
            services.AddSingleton<IUrlPathToUrlTemplateMatcher, RegExUrlMatcher>();
 | 
			
		||||
            services.AddSingleton<IUrlPathPlaceholderNameAndValueFinder, UrlPathPlaceholderNameAndValueFinder>();
 | 
			
		||||
 
 | 
			
		||||
@@ -34,7 +34,7 @@ namespace Ocelot.DownstreamRouteFinder.Middleware
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            _requestScopedDataRepository.Add("DownstreamRoute", downstreamRoute.Data);
 | 
			
		||||
            SetDownstreamRouteForThisRequest(downstreamRoute.Data);
 | 
			
		||||
 | 
			
		||||
            await _next.Invoke(context);
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
@@ -11,7 +11,6 @@ namespace Ocelot.DownstreamUrlCreator.Middleware
 | 
			
		||||
    {
 | 
			
		||||
        private readonly RequestDelegate _next;
 | 
			
		||||
        private readonly IDownstreamUrlPathPlaceholderReplacer _urlReplacer;
 | 
			
		||||
        private readonly IRequestScopedDataRepository _requestScopedDataRepository;
 | 
			
		||||
 | 
			
		||||
        public DownstreamUrlCreatorMiddleware(RequestDelegate next, 
 | 
			
		||||
            IDownstreamUrlPathPlaceholderReplacer urlReplacer,
 | 
			
		||||
@@ -20,20 +19,11 @@ namespace Ocelot.DownstreamUrlCreator.Middleware
 | 
			
		||||
        {
 | 
			
		||||
            _next = next;
 | 
			
		||||
            _urlReplacer = urlReplacer;
 | 
			
		||||
            _requestScopedDataRepository = requestScopedDataRepository;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task Invoke(HttpContext context)
 | 
			
		||||
        {
 | 
			
		||||
            var downstreamRoute = _requestScopedDataRepository.Get<DownstreamRoute>("DownstreamRoute");
 | 
			
		||||
 | 
			
		||||
            if (downstreamRoute.IsError)
 | 
			
		||||
            {
 | 
			
		||||
                SetPipelineError(downstreamRoute.Errors);
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            var downstreamUrl = _urlReplacer.Replace(downstreamRoute.Data.ReRoute.DownstreamTemplate, downstreamRoute.Data.TemplatePlaceholderNameAndValues);
 | 
			
		||||
            var downstreamUrl = _urlReplacer.Replace(DownstreamRoute.ReRoute.DownstreamTemplate, DownstreamRoute.TemplatePlaceholderNameAndValues);
 | 
			
		||||
 | 
			
		||||
            if (downstreamUrl.IsError)
 | 
			
		||||
            {
 | 
			
		||||
@@ -41,8 +31,8 @@ namespace Ocelot.DownstreamUrlCreator.Middleware
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            _requestScopedDataRepository.Add("DownstreamUrl", downstreamUrl.Data.Value);
 | 
			
		||||
                
 | 
			
		||||
            SetDownstreamUrlForThisRequest(downstreamUrl.Data.Value);
 | 
			
		||||
 | 
			
		||||
            await _next.Invoke(context);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -3,12 +3,11 @@ using System.Linq;
 | 
			
		||||
using Microsoft.AspNetCore.Http;
 | 
			
		||||
using Microsoft.Extensions.Primitives;
 | 
			
		||||
using Ocelot.Configuration;
 | 
			
		||||
using Ocelot.Infrastructure.Claims.Parser;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.HeaderBuilder
 | 
			
		||||
namespace Ocelot.Headers
 | 
			
		||||
{
 | 
			
		||||
    using Infrastructure.Claims.Parser;
 | 
			
		||||
 | 
			
		||||
    public class AddHeadersToRequest : IAddHeadersToRequest
 | 
			
		||||
    {
 | 
			
		||||
        private readonly IClaimsParser _claimsParser;
 | 
			
		||||
@@ -3,7 +3,7 @@ using Microsoft.AspNetCore.Http;
 | 
			
		||||
using Ocelot.Configuration;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.HeaderBuilder
 | 
			
		||||
namespace Ocelot.Headers
 | 
			
		||||
{
 | 
			
		||||
    public interface IAddHeadersToRequest
 | 
			
		||||
    {
 | 
			
		||||
@@ -1,17 +1,15 @@
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
using Microsoft.AspNetCore.Http;
 | 
			
		||||
using Ocelot.DownstreamRouteFinder;
 | 
			
		||||
using Ocelot.Infrastructure.RequestData;
 | 
			
		||||
using Ocelot.Middleware;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.HeaderBuilder.Middleware
 | 
			
		||||
namespace Ocelot.Headers.Middleware
 | 
			
		||||
{
 | 
			
		||||
    public class HttpRequestHeadersBuilderMiddleware : OcelotMiddleware
 | 
			
		||||
    {
 | 
			
		||||
        private readonly RequestDelegate _next;
 | 
			
		||||
        private readonly IAddHeadersToRequest _addHeadersToRequest;
 | 
			
		||||
        private readonly IRequestScopedDataRepository _requestScopedDataRepository;
 | 
			
		||||
 | 
			
		||||
        public HttpRequestHeadersBuilderMiddleware(RequestDelegate next, 
 | 
			
		||||
            IRequestScopedDataRepository requestScopedDataRepository,
 | 
			
		||||
@@ -20,16 +18,13 @@ namespace Ocelot.HeaderBuilder.Middleware
 | 
			
		||||
        {
 | 
			
		||||
            _next = next;
 | 
			
		||||
            _addHeadersToRequest = addHeadersToRequest;
 | 
			
		||||
            _requestScopedDataRepository = requestScopedDataRepository;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task Invoke(HttpContext context)
 | 
			
		||||
        {
 | 
			
		||||
            var downstreamRoute = _requestScopedDataRepository.Get<DownstreamRoute>("DownstreamRoute");
 | 
			
		||||
 | 
			
		||||
            if (downstreamRoute.Data.ReRoute.ClaimsToHeaders.Any())
 | 
			
		||||
            if (DownstreamRoute.ReRoute.ClaimsToHeaders.Any())
 | 
			
		||||
            {
 | 
			
		||||
                _addHeadersToRequest.SetHeadersOnContext(downstreamRoute.Data.ReRoute.ClaimsToHeaders, context);
 | 
			
		||||
                _addHeadersToRequest.SetHeadersOnContext(DownstreamRoute.ReRoute.ClaimsToHeaders, context);
 | 
			
		||||
            }
 | 
			
		||||
            
 | 
			
		||||
            await _next.Invoke(context);
 | 
			
		||||
@@ -1,6 +1,6 @@
 | 
			
		||||
using Microsoft.AspNetCore.Builder;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.HeaderBuilder.Middleware
 | 
			
		||||
namespace Ocelot.Headers.Middleware
 | 
			
		||||
{
 | 
			
		||||
    public static class HttpRequestHeadersBuilderMiddlewareExtensions
 | 
			
		||||
    {
 | 
			
		||||
@@ -1,4 +1,5 @@
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using Ocelot.DownstreamRouteFinder;
 | 
			
		||||
using Ocelot.Errors;
 | 
			
		||||
using Ocelot.Infrastructure.RequestData;
 | 
			
		||||
 | 
			
		||||
@@ -30,5 +31,47 @@ namespace Ocelot.Middleware
 | 
			
		||||
            var response = _requestScopedDataRepository.Get<List<Error>>("OcelotMiddlewareErrors");
 | 
			
		||||
            return response.Data;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public DownstreamRoute DownstreamRoute
 | 
			
		||||
        {
 | 
			
		||||
            get
 | 
			
		||||
            {
 | 
			
		||||
                var downstreamRoute = _requestScopedDataRepository.Get<DownstreamRoute>("DownstreamRoute");
 | 
			
		||||
                return downstreamRoute.Data;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public string DownstreamUrl
 | 
			
		||||
        {
 | 
			
		||||
            get
 | 
			
		||||
            {
 | 
			
		||||
                var downstreamUrl = _requestScopedDataRepository.Get<string>("DownstreamUrl");
 | 
			
		||||
                return downstreamUrl.Data;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public Request.Request Request
 | 
			
		||||
        {
 | 
			
		||||
            get
 | 
			
		||||
            {
 | 
			
		||||
                var request = _requestScopedDataRepository.Get<Request.Request>("Request");
 | 
			
		||||
                return request.Data;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void SetDownstreamRouteForThisRequest(DownstreamRoute downstreamRoute)
 | 
			
		||||
        {
 | 
			
		||||
            _requestScopedDataRepository.Add("DownstreamRoute", downstreamRoute);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void SetDownstreamUrlForThisRequest(string downstreamUrl)
 | 
			
		||||
        {
 | 
			
		||||
            _requestScopedDataRepository.Add("DownstreamUrl", downstreamUrl);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public void SetUpstreamRequestForThisRequest(Request.Request request)
 | 
			
		||||
        {
 | 
			
		||||
            _requestScopedDataRepository.Add("Request", request);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -5,23 +5,10 @@
 | 
			
		||||
    using Microsoft.AspNetCore.Http;
 | 
			
		||||
 | 
			
		||||
    public class OcelotMiddlewareConfiguration
 | 
			
		||||
    {
 | 
			
		||||
        public Func<HttpContext, Func<Task>, Task> PreHttpResponderMiddleware { get; set; }
 | 
			
		||||
        public Func<HttpContext, Func<Task>, Task> PostHttpResponderMiddleware { get; set; }
 | 
			
		||||
        public Func<HttpContext, Func<Task>, Task> PreDownstreamRouteFinderMiddleware { get; set; }
 | 
			
		||||
        public Func<HttpContext, Func<Task>, Task> PostDownstreamRouteFinderMiddleware { get; set; }
 | 
			
		||||
    { 
 | 
			
		||||
        public Func<HttpContext, Func<Task>, Task> PreAuthenticationMiddleware { get; set; }
 | 
			
		||||
        public Func<HttpContext, Func<Task>, Task> PostAuthenticationMiddleware { get; set; }
 | 
			
		||||
        public Func<HttpContext, Func<Task>, Task> PreClaimsBuilderMiddleware { get; set; }
 | 
			
		||||
        public Func<HttpContext, Func<Task>, Task> PostClaimsBuilderMiddleware { get; set; }
 | 
			
		||||
        public Func<HttpContext, Func<Task>, Task> AuthenticationMiddleware { get; set; }
 | 
			
		||||
        public Func<HttpContext, Func<Task>, Task> PreAuthorisationMiddleware { get; set; }
 | 
			
		||||
        public Func<HttpContext, Func<Task>, Task> PostAuthorisationMiddleware { get; set; }
 | 
			
		||||
        public Func<HttpContext, Func<Task>, Task> PreHttpRequestHeadersBuilderMiddleware { get; set; }
 | 
			
		||||
        public Func<HttpContext, Func<Task>, Task> PostHttpRequestHeadersBuilderMiddleware { get; set; }
 | 
			
		||||
        public Func<HttpContext, Func<Task>, Task> PreDownstreamUrlCreatorMiddleware { get; set; }
 | 
			
		||||
        public Func<HttpContext, Func<Task>, Task> PostDownstreamUrlCreatorMiddleware { get; set; }
 | 
			
		||||
        public Func<HttpContext, Func<Task>, Task> PreHttpRequestBuilderMiddleware { get; set; }
 | 
			
		||||
        public Func<HttpContext, Func<Task>, Task> PostHttpRequestBuilderMiddleware { get; set; }
 | 
			
		||||
        public Func<HttpContext, Func<Task>, Task> PreHttpRequesterMiddleware { get; set; }
 | 
			
		||||
        public Func<HttpContext, Func<Task>, Task> AuthorisationMiddleware { get; set; }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,9 +1,11 @@
 | 
			
		||||
using Microsoft.AspNetCore.Builder;
 | 
			
		||||
using Ocelot.Authentication.Middleware;
 | 
			
		||||
using Ocelot.Claims.Middleware;
 | 
			
		||||
using Ocelot.DownstreamRouteFinder.Middleware;
 | 
			
		||||
using Ocelot.DownstreamUrlCreator.Middleware;
 | 
			
		||||
using Ocelot.HeaderBuilder.Middleware;
 | 
			
		||||
using Ocelot.RequestBuilder.Middleware;
 | 
			
		||||
using Ocelot.Headers.Middleware;
 | 
			
		||||
using Ocelot.QueryStrings.Middleware;
 | 
			
		||||
using Ocelot.Request.Middleware;
 | 
			
		||||
using Ocelot.Requester.Middleware;
 | 
			
		||||
using Ocelot.Responder.Middleware;
 | 
			
		||||
 | 
			
		||||
@@ -12,12 +14,17 @@ namespace Ocelot.Middleware
 | 
			
		||||
    using System;
 | 
			
		||||
    using System.Threading.Tasks;
 | 
			
		||||
    using Authorisation.Middleware;
 | 
			
		||||
    using ClaimsBuilder.Middleware;
 | 
			
		||||
    using Microsoft.AspNetCore.Http;
 | 
			
		||||
 | 
			
		||||
    public static class OcelotMiddlewareExtensions
 | 
			
		||||
    {
 | 
			
		||||
        public static IApplicationBuilder UseOcelot(this IApplicationBuilder builder)
 | 
			
		||||
        {
 | 
			
		||||
            builder.UseOcelot(new OcelotMiddlewareConfiguration());
 | 
			
		||||
            return builder;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public static IApplicationBuilder UseOcelot(this IApplicationBuilder builder, OcelotMiddlewareConfiguration middlewareConfiguration)
 | 
			
		||||
        {
 | 
			
		||||
            // This is registered to catch any global exceptions that are not handled
 | 
			
		||||
            builder.UseExceptionHandlerMiddleware();
 | 
			
		||||
@@ -28,18 +35,46 @@ namespace Ocelot.Middleware
 | 
			
		||||
            // Then we get the downstream route information
 | 
			
		||||
            builder.UseDownstreamRouteFinderMiddleware();
 | 
			
		||||
 | 
			
		||||
            // Now we know where the client is going to go we can authenticate them
 | 
			
		||||
            builder.UseAuthenticationMiddleware();
 | 
			
		||||
            // Allow pre authentication logic. The idea being people might want to run something custom before what is built in.
 | 
			
		||||
            builder.UseIfNotNull(middlewareConfiguration.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 (middlewareConfiguration.AuthenticationMiddleware == null)
 | 
			
		||||
            {
 | 
			
		||||
                builder.UseAuthenticationMiddleware();
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                builder.Use(middlewareConfiguration.AuthenticationMiddleware);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            // The next thing we do is look at any claims transforms in case this is important for authorisation
 | 
			
		||||
            builder.UseClaimsBuilderMiddleware();
 | 
			
		||||
 | 
			
		||||
            // Now we have authenticated and done any claims transformation we can authorise the request
 | 
			
		||||
            builder.UseAuthorisationMiddleware();
 | 
			
		||||
            // Allow pre authorisation logic. The idea being people might want to run something custom before what is built in.
 | 
			
		||||
            builder.UseIfNotNull(middlewareConfiguration.PreAuthorisationMiddleware);
 | 
			
		||||
 | 
			
		||||
            // Now we have authenticated and done any claims transformation we 
 | 
			
		||||
            // can authorise the request
 | 
			
		||||
            // We allow the ocelot middleware to be overriden by whatever the
 | 
			
		||||
            // user wants
 | 
			
		||||
            if (middlewareConfiguration.AuthorisationMiddleware == null)
 | 
			
		||||
            {
 | 
			
		||||
                builder.UseAuthorisationMiddleware();
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                builder.Use(middlewareConfiguration.AuthorisationMiddleware);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            // Now we can run any header transformation logic
 | 
			
		||||
            builder.UseHttpRequestHeadersBuilderMiddleware();
 | 
			
		||||
 | 
			
		||||
            // Now we can run any query string transformation logic
 | 
			
		||||
            builder.UseQueryStringBuilderMiddleware();
 | 
			
		||||
 | 
			
		||||
            // This takes the downstream route we retrieved earlier and replaces any placeholders with the variables that should be used
 | 
			
		||||
            builder.UseDownstreamUrlCreatorMiddleware();
 | 
			
		||||
 | 
			
		||||
@@ -52,63 +87,6 @@ namespace Ocelot.Middleware
 | 
			
		||||
            return builder;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public static IApplicationBuilder UseOcelot(this IApplicationBuilder builder, OcelotMiddlewareConfiguration middlewareConfiguration)
 | 
			
		||||
        {
 | 
			
		||||
            builder.UseIfNotNull(middlewareConfiguration.PreHttpResponderMiddleware);
 | 
			
		||||
 | 
			
		||||
            builder.UseHttpErrorResponderMiddleware();
 | 
			
		||||
 | 
			
		||||
            builder.UseIfNotNull(middlewareConfiguration.PostHttpResponderMiddleware);
 | 
			
		||||
 | 
			
		||||
            builder.UseIfNotNull(middlewareConfiguration.PreDownstreamRouteFinderMiddleware);
 | 
			
		||||
 | 
			
		||||
            builder.UseDownstreamRouteFinderMiddleware();
 | 
			
		||||
 | 
			
		||||
            builder.UseIfNotNull(middlewareConfiguration.PostDownstreamRouteFinderMiddleware);
 | 
			
		||||
 | 
			
		||||
            builder.UseIfNotNull(middlewareConfiguration.PreAuthenticationMiddleware);
 | 
			
		||||
 | 
			
		||||
            builder.UseAuthenticationMiddleware();
 | 
			
		||||
 | 
			
		||||
            builder.UseIfNotNull(middlewareConfiguration.PostAuthenticationMiddleware);
 | 
			
		||||
 | 
			
		||||
            builder.UseIfNotNull(middlewareConfiguration.PreClaimsBuilderMiddleware);
 | 
			
		||||
 | 
			
		||||
            builder.UseClaimsBuilderMiddleware();
 | 
			
		||||
 | 
			
		||||
            builder.UseIfNotNull(middlewareConfiguration.PostClaimsBuilderMiddleware);
 | 
			
		||||
 | 
			
		||||
            builder.UseIfNotNull(middlewareConfiguration.PreAuthorisationMiddleware);
 | 
			
		||||
 | 
			
		||||
            builder.UseAuthorisationMiddleware();
 | 
			
		||||
 | 
			
		||||
            builder.UseIfNotNull(middlewareConfiguration.PostAuthorisationMiddleware);
 | 
			
		||||
 | 
			
		||||
            builder.UseIfNotNull(middlewareConfiguration.PreHttpRequestHeadersBuilderMiddleware);
 | 
			
		||||
 | 
			
		||||
            builder.UseHttpRequestHeadersBuilderMiddleware();
 | 
			
		||||
 | 
			
		||||
            builder.UseIfNotNull(middlewareConfiguration.PostHttpRequestHeadersBuilderMiddleware);
 | 
			
		||||
 | 
			
		||||
            builder.UseIfNotNull(middlewareConfiguration.PreDownstreamUrlCreatorMiddleware);
 | 
			
		||||
 | 
			
		||||
            builder.UseDownstreamUrlCreatorMiddleware();
 | 
			
		||||
 | 
			
		||||
            builder.UseIfNotNull(middlewareConfiguration.PostDownstreamUrlCreatorMiddleware);
 | 
			
		||||
 | 
			
		||||
            builder.UseIfNotNull(middlewareConfiguration.PreHttpRequestBuilderMiddleware);
 | 
			
		||||
 | 
			
		||||
            builder.UseHttpRequestBuilderMiddleware();
 | 
			
		||||
 | 
			
		||||
            builder.UseIfNotNull(middlewareConfiguration.PostHttpRequestBuilderMiddleware);
 | 
			
		||||
 | 
			
		||||
            builder.UseIfNotNull(middlewareConfiguration.PreHttpRequesterMiddleware);
 | 
			
		||||
 | 
			
		||||
            builder.UseHttpRequesterMiddleware();
 | 
			
		||||
 | 
			
		||||
            return builder;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private static void UseIfNotNull(this IApplicationBuilder builder, Func<HttpContext, Func<Task>, Task> middleware)
 | 
			
		||||
        {
 | 
			
		||||
            if (middleware != null)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										62
									
								
								src/Ocelot/QueryStrings/AddQueriesToRequest.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										62
									
								
								src/Ocelot/QueryStrings/AddQueriesToRequest.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,62 @@
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using Microsoft.AspNetCore.Http;
 | 
			
		||||
using Ocelot.Configuration;
 | 
			
		||||
using Ocelot.Infrastructure.Claims.Parser;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.QueryStrings
 | 
			
		||||
{
 | 
			
		||||
    public class AddQueriesToRequest : IAddQueriesToRequest
 | 
			
		||||
    {
 | 
			
		||||
        private readonly IClaimsParser _claimsParser;
 | 
			
		||||
 | 
			
		||||
        public AddQueriesToRequest(IClaimsParser claimsParser)
 | 
			
		||||
        {
 | 
			
		||||
            _claimsParser = claimsParser;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public Response SetQueriesOnContext(List<ClaimToThing> claimsToThings, HttpContext context)
 | 
			
		||||
        {
 | 
			
		||||
            var queryDictionary = ConvertQueryStringToDictionary(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 = queryDictionary.FirstOrDefault(x => x.Key == config.ExistingKey);
 | 
			
		||||
 | 
			
		||||
                if (!string.IsNullOrEmpty(exists.Key))
 | 
			
		||||
                {
 | 
			
		||||
                    queryDictionary[exists.Key] = value.Data;
 | 
			
		||||
                }
 | 
			
		||||
                else
 | 
			
		||||
                {
 | 
			
		||||
                    queryDictionary.Add(config.ExistingKey, value.Data);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            context.Request.QueryString = ConvertDictionaryToQueryString(queryDictionary);
 | 
			
		||||
 | 
			
		||||
            return new OkResponse();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private Dictionary<string, string> ConvertQueryStringToDictionary(HttpContext context)
 | 
			
		||||
        {
 | 
			
		||||
            return Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(context.Request.QueryString.Value)
 | 
			
		||||
                .ToDictionary(q => q.Key, q => q.Value.FirstOrDefault() ?? string.Empty);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private Microsoft.AspNetCore.Http.QueryString ConvertDictionaryToQueryString(Dictionary<string, string> queryDictionary)
 | 
			
		||||
        {
 | 
			
		||||
            var newQueryString = Microsoft.AspNetCore.WebUtilities.QueryHelpers.AddQueryString("", queryDictionary);
 | 
			
		||||
 | 
			
		||||
            return new Microsoft.AspNetCore.Http.QueryString(newQueryString);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										13
									
								
								src/Ocelot/QueryStrings/IAddQueriesToRequest.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								src/Ocelot/QueryStrings/IAddQueriesToRequest.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,13 @@
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using Microsoft.AspNetCore.Http;
 | 
			
		||||
using Ocelot.Configuration;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.QueryStrings
 | 
			
		||||
{
 | 
			
		||||
    public interface IAddQueriesToRequest
 | 
			
		||||
    {
 | 
			
		||||
        Response SetQueriesOnContext(List<ClaimToThing> claimsToThings,
 | 
			
		||||
            HttpContext context);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,33 @@
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
using Microsoft.AspNetCore.Http;
 | 
			
		||||
using Ocelot.Infrastructure.RequestData;
 | 
			
		||||
using Ocelot.Middleware;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.QueryStrings.Middleware
 | 
			
		||||
{
 | 
			
		||||
    public class QueryStringBuilderMiddleware : OcelotMiddleware
 | 
			
		||||
    {
 | 
			
		||||
        private readonly RequestDelegate _next;
 | 
			
		||||
        private readonly IAddQueriesToRequest _addQueriesToRequest;
 | 
			
		||||
 | 
			
		||||
        public QueryStringBuilderMiddleware(RequestDelegate next, 
 | 
			
		||||
            IRequestScopedDataRepository requestScopedDataRepository,
 | 
			
		||||
            IAddQueriesToRequest addQueriesToRequest) 
 | 
			
		||||
            : base(requestScopedDataRepository)
 | 
			
		||||
        {
 | 
			
		||||
            _next = next;
 | 
			
		||||
            _addQueriesToRequest = addQueriesToRequest;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task Invoke(HttpContext context)
 | 
			
		||||
        {
 | 
			
		||||
            if (DownstreamRoute.ReRoute.ClaimsToQueries.Any())
 | 
			
		||||
            {
 | 
			
		||||
                _addQueriesToRequest.SetQueriesOnContext(DownstreamRoute.ReRoute.ClaimsToQueries, context);
 | 
			
		||||
            }
 | 
			
		||||
            
 | 
			
		||||
            await _next.Invoke(context);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,12 @@
 | 
			
		||||
using Microsoft.AspNetCore.Builder;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.QueryStrings.Middleware
 | 
			
		||||
{
 | 
			
		||||
    public static class QueryStringBuilderMiddlewareExtensions
 | 
			
		||||
    {
 | 
			
		||||
        public static IApplicationBuilder UseQueryStringBuilderMiddleware(this IApplicationBuilder builder)
 | 
			
		||||
        {
 | 
			
		||||
            return builder.UseMiddleware<QueryStringBuilderMiddleware>();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -2,23 +2,21 @@
 | 
			
		||||
using System.IO;
 | 
			
		||||
using System.Net;
 | 
			
		||||
using System.Net.Http;
 | 
			
		||||
using System.Net.Http.Headers;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
using Microsoft.AspNetCore.Http;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.RequestBuilder.Builder
 | 
			
		||||
namespace Ocelot.Request.Builder
 | 
			
		||||
{
 | 
			
		||||
    public class HttpRequestBuilder : IRequestBuilder
 | 
			
		||||
    {
 | 
			
		||||
        public async Task<Response<Request>> Build(string httpMethod, string downstreamUrl, Stream content, IHeaderDictionary headers,
 | 
			
		||||
            IRequestCookieCollection cookies, QueryString queryString, string contentType)
 | 
			
		||||
            IRequestCookieCollection cookies, Microsoft.AspNetCore.Http.QueryString queryString, string contentType)
 | 
			
		||||
        {
 | 
			
		||||
            var method = new HttpMethod(httpMethod);
 | 
			
		||||
 | 
			
		||||
            var uri = new Uri(string.Format("{0}{1}", downstreamUrl, queryString.ToUriComponent()));
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
            var httpRequestMessage = new HttpRequestMessage(method, uri);
 | 
			
		||||
 | 
			
		||||
            if (content != null)
 | 
			
		||||
@@ -3,7 +3,7 @@ using System.Threading.Tasks;
 | 
			
		||||
using Microsoft.AspNetCore.Http;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.RequestBuilder.Builder
 | 
			
		||||
namespace Ocelot.Request.Builder
 | 
			
		||||
{
 | 
			
		||||
    public interface IRequestBuilder
 | 
			
		||||
    {
 | 
			
		||||
@@ -12,7 +12,7 @@ namespace Ocelot.RequestBuilder.Builder
 | 
			
		||||
            Stream content,
 | 
			
		||||
            IHeaderDictionary headers,
 | 
			
		||||
            IRequestCookieCollection cookies,
 | 
			
		||||
            QueryString queryString,
 | 
			
		||||
            Microsoft.AspNetCore.Http.QueryString queryString,
 | 
			
		||||
            string contentType);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -2,14 +2,13 @@ using System.Threading.Tasks;
 | 
			
		||||
using Microsoft.AspNetCore.Http;
 | 
			
		||||
using Ocelot.Infrastructure.RequestData;
 | 
			
		||||
using Ocelot.Middleware;
 | 
			
		||||
using Ocelot.RequestBuilder.Builder;
 | 
			
		||||
using Ocelot.Request.Builder;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.RequestBuilder.Middleware
 | 
			
		||||
namespace Ocelot.Request.Middleware
 | 
			
		||||
{
 | 
			
		||||
    public class HttpRequestBuilderMiddleware : OcelotMiddleware
 | 
			
		||||
    {
 | 
			
		||||
        private readonly RequestDelegate _next;
 | 
			
		||||
        private readonly IRequestScopedDataRepository _requestScopedDataRepository;
 | 
			
		||||
        private readonly IRequestBuilder _requestBuilder;
 | 
			
		||||
 | 
			
		||||
        public HttpRequestBuilderMiddleware(RequestDelegate next, 
 | 
			
		||||
@@ -18,22 +17,13 @@ namespace Ocelot.RequestBuilder.Middleware
 | 
			
		||||
            :base(requestScopedDataRepository)
 | 
			
		||||
        {
 | 
			
		||||
            _next = next;
 | 
			
		||||
            _requestScopedDataRepository = requestScopedDataRepository;
 | 
			
		||||
            _requestBuilder = requestBuilder;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task Invoke(HttpContext context)
 | 
			
		||||
        {
 | 
			
		||||
            var downstreamUrl = _requestScopedDataRepository.Get<string>("DownstreamUrl");
 | 
			
		||||
 | 
			
		||||
            if (downstreamUrl.IsError)
 | 
			
		||||
            {
 | 
			
		||||
                SetPipelineError(downstreamUrl.Errors);
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            var request = await _requestBuilder
 | 
			
		||||
              .Build(context.Request.Method, downstreamUrl.Data, context.Request.Body,
 | 
			
		||||
              .Build(context.Request.Method, DownstreamUrl, context.Request.Body,
 | 
			
		||||
              context.Request.Headers, context.Request.Cookies, context.Request.QueryString, context.Request.ContentType);
 | 
			
		||||
 | 
			
		||||
            if (request.IsError)
 | 
			
		||||
@@ -42,7 +32,7 @@ namespace Ocelot.RequestBuilder.Middleware
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            _requestScopedDataRepository.Add("Request", request.Data);
 | 
			
		||||
            SetUpstreamRequestForThisRequest(request.Data);
 | 
			
		||||
 | 
			
		||||
            await _next.Invoke(context);
 | 
			
		||||
        }
 | 
			
		||||
@@ -1,6 +1,6 @@
 | 
			
		||||
using Microsoft.AspNetCore.Builder;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.RequestBuilder.Middleware
 | 
			
		||||
namespace Ocelot.Request.Middleware
 | 
			
		||||
{
 | 
			
		||||
    public static class HttpRequestBuilderMiddlewareExtensions
 | 
			
		||||
    {
 | 
			
		||||
@@ -1,7 +1,7 @@
 | 
			
		||||
using System.Net;
 | 
			
		||||
using System.Net.Http;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.RequestBuilder
 | 
			
		||||
namespace Ocelot.Request
 | 
			
		||||
{
 | 
			
		||||
    public class Request
 | 
			
		||||
    {
 | 
			
		||||
@@ -3,14 +3,13 @@ using System.Collections.Generic;
 | 
			
		||||
using System.Net.Http;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
using Ocelot.Errors;
 | 
			
		||||
using Ocelot.RequestBuilder;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Requester
 | 
			
		||||
{
 | 
			
		||||
    public class HttpClientHttpRequester : IHttpRequester
 | 
			
		||||
    {
 | 
			
		||||
        public async Task<Response<HttpResponseMessage>> GetResponse(Request request)
 | 
			
		||||
        public async Task<Response<HttpResponseMessage>> GetResponse(Request.Request request)
 | 
			
		||||
        {
 | 
			
		||||
            using (var handler = new HttpClientHandler { CookieContainer = request.CookieContainer })
 | 
			
		||||
            using (var httpClient = new HttpClient(handler))
 | 
			
		||||
 
 | 
			
		||||
@@ -1,12 +1,11 @@
 | 
			
		||||
using System.Net.Http;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
using Ocelot.RequestBuilder;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Requester
 | 
			
		||||
{
 | 
			
		||||
    public interface IHttpRequester
 | 
			
		||||
    {
 | 
			
		||||
        Task<Response<HttpResponseMessage>> GetResponse(Request request);
 | 
			
		||||
        Task<Response<HttpResponseMessage>> GetResponse(Request.Request request);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,6 @@ using System.Threading.Tasks;
 | 
			
		||||
using Microsoft.AspNetCore.Http;
 | 
			
		||||
using Ocelot.Infrastructure.RequestData;
 | 
			
		||||
using Ocelot.Middleware;
 | 
			
		||||
using Ocelot.RequestBuilder;
 | 
			
		||||
using Ocelot.Responder;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Requester.Middleware
 | 
			
		||||
@@ -11,7 +10,6 @@ namespace Ocelot.Requester.Middleware
 | 
			
		||||
    {
 | 
			
		||||
        private readonly RequestDelegate _next;
 | 
			
		||||
        private readonly IHttpRequester _requester;
 | 
			
		||||
        private readonly IRequestScopedDataRepository _requestScopedDataRepository;
 | 
			
		||||
        private readonly IHttpResponder _responder;
 | 
			
		||||
 | 
			
		||||
        public HttpRequesterMiddleware(RequestDelegate next, 
 | 
			
		||||
@@ -22,21 +20,13 @@ namespace Ocelot.Requester.Middleware
 | 
			
		||||
        {
 | 
			
		||||
            _next = next;
 | 
			
		||||
            _requester = requester;
 | 
			
		||||
            _requestScopedDataRepository = requestScopedDataRepository;
 | 
			
		||||
            _responder = responder;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task Invoke(HttpContext context)
 | 
			
		||||
        {
 | 
			
		||||
            var request = _requestScopedDataRepository.Get<Request>("Request");
 | 
			
		||||
 | 
			
		||||
            if (request.IsError)
 | 
			
		||||
            {
 | 
			
		||||
                SetPipelineError(request.Errors);
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            var response = await _requester.GetResponse(request.Data);
 | 
			
		||||
            var response = await _requester.GetResponse(Request);
 | 
			
		||||
 | 
			
		||||
            if (response.IsError)
 | 
			
		||||
            {
 | 
			
		||||
 
 | 
			
		||||
@@ -10,7 +10,6 @@ namespace Ocelot.Responder.Middleware
 | 
			
		||||
    {
 | 
			
		||||
        private readonly RequestDelegate _next;
 | 
			
		||||
        private readonly IHttpResponder _responder;
 | 
			
		||||
        private readonly IRequestScopedDataRepository _requestScopedDataRepository;
 | 
			
		||||
        private readonly IErrorsToHttpStatusCodeMapper _codeMapper;
 | 
			
		||||
 | 
			
		||||
        public HttpErrorResponderMiddleware(RequestDelegate next, 
 | 
			
		||||
@@ -21,7 +20,6 @@ namespace Ocelot.Responder.Middleware
 | 
			
		||||
        {
 | 
			
		||||
            _next = next;
 | 
			
		||||
            _responder = responder;
 | 
			
		||||
            _requestScopedDataRepository = requestScopedDataRepository;
 | 
			
		||||
            _codeMapper = codeMapper;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user