mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 20:30:50 +08:00 
			
		
		
		
	renamed and removed some stuff that wasnt needed
This commit is contained in:
		@@ -25,8 +25,6 @@ namespace Ocelot.Library.Authentication
 | 
			
		||||
                ScopeSecret = authOptions.ScopeSecret
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
            builder.UseMvc();
 | 
			
		||||
 | 
			
		||||
            var authenticationNext = builder.Build();
 | 
			
		||||
 | 
			
		||||
            return new OkResponse<RequestDelegate>(authenticationNext);
 | 
			
		||||
 
 | 
			
		||||
@@ -1,10 +1,8 @@
 | 
			
		||||
using Ocelot.Library.RequestBuilder;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using Ocelot.Library.Configuration;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Library.Builder
 | 
			
		||||
{
 | 
			
		||||
    using System.Collections.Generic;
 | 
			
		||||
    using Configuration;
 | 
			
		||||
 | 
			
		||||
    public class ReRouteBuilder
 | 
			
		||||
    {
 | 
			
		||||
        private string _downstreamTemplate;
 | 
			
		||||
@@ -18,7 +16,7 @@ namespace Ocelot.Library.Builder
 | 
			
		||||
        private List<string> _additionalScopes;
 | 
			
		||||
        private bool _requireHttps;
 | 
			
		||||
        private string _scopeSecret;
 | 
			
		||||
        private List<ConfigurationHeaderExtractorProperties> _configHeaderExtractorProperties;
 | 
			
		||||
        private List<ClaimToHeader> _configHeaderExtractorProperties;
 | 
			
		||||
 | 
			
		||||
        public ReRouteBuilder()
 | 
			
		||||
        {
 | 
			
		||||
@@ -89,7 +87,7 @@ namespace Ocelot.Library.Builder
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public ReRouteBuilder WithConfigurationHeaderExtractorProperties(List<ConfigurationHeaderExtractorProperties> input)
 | 
			
		||||
        public ReRouteBuilder WithConfigurationHeaderExtractorProperties(List<ClaimToHeader> input)
 | 
			
		||||
        {
 | 
			
		||||
            _configHeaderExtractorProperties = input;
 | 
			
		||||
            return this;
 | 
			
		||||
 
 | 
			
		||||
@@ -1,8 +1,8 @@
 | 
			
		||||
namespace Ocelot.Library.RequestBuilder
 | 
			
		||||
namespace Ocelot.Library.Configuration
 | 
			
		||||
{
 | 
			
		||||
    public class ConfigurationHeaderExtractorProperties
 | 
			
		||||
    public class ClaimToHeader
 | 
			
		||||
    {
 | 
			
		||||
        public ConfigurationHeaderExtractorProperties(string headerKey, string claimKey, string delimiter, int index)
 | 
			
		||||
        public ClaimToHeader(string headerKey, string claimKey, string delimiter, int index)
 | 
			
		||||
        {
 | 
			
		||||
            ClaimKey = claimKey;
 | 
			
		||||
            Delimiter = delimiter;
 | 
			
		||||
@@ -2,17 +2,18 @@
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Text.RegularExpressions;
 | 
			
		||||
using Ocelot.Library.Errors;
 | 
			
		||||
using Ocelot.Library.RequestBuilder;
 | 
			
		||||
using Ocelot.Library.Responses;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Library.RequestBuilder
 | 
			
		||||
namespace Ocelot.Library.Configuration
 | 
			
		||||
{
 | 
			
		||||
    public class ConfigurationHeaderExtrator : IConfigurationHeaderExtrator
 | 
			
		||||
    public class ClaimToHeaderConfigurationParser : IClaimToHeaderConfigurationParser
 | 
			
		||||
    {
 | 
			
		||||
        private readonly Regex _claimRegex = new Regex("Claims\\[.*\\]");
 | 
			
		||||
        private readonly Regex _indexRegex = new Regex("value\\[.*\\]");
 | 
			
		||||
        private const string SplitToken = ">";
 | 
			
		||||
 | 
			
		||||
        public Response<ConfigurationHeaderExtractorProperties> Extract(string headerKey, string value)
 | 
			
		||||
        public Response<ClaimToHeader> Extract(string headerKey, string value)
 | 
			
		||||
        {
 | 
			
		||||
            try
 | 
			
		||||
            {
 | 
			
		||||
@@ -20,7 +21,7 @@ namespace Ocelot.Library.RequestBuilder
 | 
			
		||||
 | 
			
		||||
                if (instructions.Length <= 1)
 | 
			
		||||
                {
 | 
			
		||||
                    return new ErrorResponse<ConfigurationHeaderExtractorProperties>(
 | 
			
		||||
                    return new ErrorResponse<ClaimToHeader>(
 | 
			
		||||
                        new List<Error>
 | 
			
		||||
                    {
 | 
			
		||||
                        new NoInstructionsError(SplitToken)
 | 
			
		||||
@@ -31,7 +32,7 @@ namespace Ocelot.Library.RequestBuilder
 | 
			
		||||
 | 
			
		||||
                if (!claimMatch)
 | 
			
		||||
                {
 | 
			
		||||
                    return new ErrorResponse<ConfigurationHeaderExtractorProperties>(
 | 
			
		||||
                    return new ErrorResponse<ClaimToHeader>(
 | 
			
		||||
                        new List<Error>
 | 
			
		||||
                        {
 | 
			
		||||
                            new InstructionNotForClaimsError()
 | 
			
		||||
@@ -48,12 +49,12 @@ namespace Ocelot.Library.RequestBuilder
 | 
			
		||||
                    delimiter = instructions[2].Trim();
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                return new OkResponse<ConfigurationHeaderExtractorProperties>(
 | 
			
		||||
                               new ConfigurationHeaderExtractorProperties(headerKey, claimKey, delimiter, index));
 | 
			
		||||
                return new OkResponse<ClaimToHeader>(
 | 
			
		||||
                               new ClaimToHeader(headerKey, claimKey, delimiter, index));
 | 
			
		||||
            }
 | 
			
		||||
            catch (Exception exception)
 | 
			
		||||
            {
 | 
			
		||||
                return new ErrorResponse<ConfigurationHeaderExtractorProperties>(
 | 
			
		||||
                return new ErrorResponse<ClaimToHeader>(
 | 
			
		||||
                    new List<Error>
 | 
			
		||||
                    {
 | 
			
		||||
                        new ParsingConfigurationHeaderError(exception)
 | 
			
		||||
@@ -0,0 +1,9 @@
 | 
			
		||||
using Ocelot.Library.Responses;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Library.Configuration
 | 
			
		||||
{
 | 
			
		||||
    public interface IClaimToHeaderConfigurationParser
 | 
			
		||||
    {
 | 
			
		||||
        Response<ClaimToHeader> Extract(string headerKey, string value);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -16,17 +16,17 @@ namespace Ocelot.Library.Configuration
 | 
			
		||||
        private readonly List<ReRoute> _reRoutes;
 | 
			
		||||
        private const string RegExMatchEverything = ".*";
 | 
			
		||||
        private const string RegExMatchEndString = "$";
 | 
			
		||||
        private readonly IConfigurationHeaderExtrator _configurationHeaderExtrator;
 | 
			
		||||
        private readonly IClaimToHeaderConfigurationParser _claimToHeaderConfigurationParser;
 | 
			
		||||
        private readonly ILogger<OcelotConfiguration> _logger;
 | 
			
		||||
 | 
			
		||||
        public OcelotConfiguration(IOptions<YamlConfiguration> options, 
 | 
			
		||||
            IConfigurationValidator configurationValidator, 
 | 
			
		||||
            IConfigurationHeaderExtrator configurationHeaderExtrator, 
 | 
			
		||||
            IClaimToHeaderConfigurationParser claimToHeaderConfigurationParser, 
 | 
			
		||||
            ILogger<OcelotConfiguration> logger)
 | 
			
		||||
        {
 | 
			
		||||
            _options = options;
 | 
			
		||||
            _configurationValidator = configurationValidator;
 | 
			
		||||
            _configurationHeaderExtrator = configurationHeaderExtrator;
 | 
			
		||||
            _claimToHeaderConfigurationParser = claimToHeaderConfigurationParser;
 | 
			
		||||
            _logger = logger;
 | 
			
		||||
            _reRoutes = new List<ReRoute>();
 | 
			
		||||
            SetUpConfiguration();
 | 
			
		||||
@@ -92,17 +92,17 @@ namespace Ocelot.Library.Configuration
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                _reRoutes.Add(new ReRoute(reRoute.DownstreamTemplate, reRoute.UpstreamTemplate, reRoute.UpstreamHttpMethod,
 | 
			
		||||
                    upstreamTemplate, isAuthenticated, null, new List<ConfigurationHeaderExtractorProperties>()));
 | 
			
		||||
                    upstreamTemplate, isAuthenticated, null, new List<ClaimToHeader>()));
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private List<ConfigurationHeaderExtractorProperties> GetHeadersToAddToRequest(YamlReRoute reRoute)
 | 
			
		||||
        private List<ClaimToHeader> GetHeadersToAddToRequest(YamlReRoute reRoute)
 | 
			
		||||
        {
 | 
			
		||||
            var configHeaders = new List<ConfigurationHeaderExtractorProperties>();
 | 
			
		||||
            var configHeaders = new List<ClaimToHeader>();
 | 
			
		||||
 | 
			
		||||
            foreach (var add in reRoute.AddHeadersToRequest)
 | 
			
		||||
            {
 | 
			
		||||
                var configurationHeader = _configurationHeaderExtrator.Extract(add.Key, add.Value);
 | 
			
		||||
                var configurationHeader = _claimToHeaderConfigurationParser.Extract(add.Key, add.Value);
 | 
			
		||||
 | 
			
		||||
                if (configurationHeader.IsError)
 | 
			
		||||
                {
 | 
			
		||||
 
 | 
			
		||||
@@ -1,11 +1,10 @@
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using Ocelot.Library.RequestBuilder;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Library.Configuration
 | 
			
		||||
{
 | 
			
		||||
    public class ReRoute
 | 
			
		||||
    {
 | 
			
		||||
        public ReRoute(string downstreamTemplate, string upstreamTemplate, string upstreamHttpMethod, string upstreamTemplatePattern, bool isAuthenticated, AuthenticationOptions authenticationOptions, List<ConfigurationHeaderExtractorProperties> configurationHeaderExtractorProperties)
 | 
			
		||||
        public ReRoute(string downstreamTemplate, string upstreamTemplate, string upstreamHttpMethod, string upstreamTemplatePattern, bool isAuthenticated, AuthenticationOptions authenticationOptions, List<ClaimToHeader> configurationHeaderExtractorProperties)
 | 
			
		||||
        {
 | 
			
		||||
            DownstreamTemplate = downstreamTemplate;
 | 
			
		||||
            UpstreamTemplate = upstreamTemplate;
 | 
			
		||||
@@ -13,8 +12,8 @@ namespace Ocelot.Library.Configuration
 | 
			
		||||
            UpstreamTemplatePattern = upstreamTemplatePattern;
 | 
			
		||||
            IsAuthenticated = isAuthenticated;
 | 
			
		||||
            AuthenticationOptions = authenticationOptions;
 | 
			
		||||
            ConfigurationHeaderExtractorProperties = configurationHeaderExtractorProperties 
 | 
			
		||||
                ?? new List<ConfigurationHeaderExtractorProperties>();
 | 
			
		||||
            ClaimsToHeaders = configurationHeaderExtractorProperties 
 | 
			
		||||
                ?? new List<ClaimToHeader>();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public string DownstreamTemplate { get; private set; }
 | 
			
		||||
@@ -23,6 +22,6 @@ namespace Ocelot.Library.Configuration
 | 
			
		||||
        public string UpstreamHttpMethod { get; private set; }
 | 
			
		||||
        public bool IsAuthenticated { get; private set; }
 | 
			
		||||
        public AuthenticationOptions AuthenticationOptions { get; private set; }
 | 
			
		||||
        public List<ConfigurationHeaderExtractorProperties> ConfigurationHeaderExtractorProperties { get; private set; } 
 | 
			
		||||
        public List<ClaimToHeader> ClaimsToHeaders { get; private set; } 
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -18,12 +18,18 @@
 | 
			
		||||
    {
 | 
			
		||||
        public static IServiceCollection AddOcelot(this IServiceCollection services, IConfigurationRoot configurationRoot)
 | 
			
		||||
        {
 | 
			
		||||
            // framework services
 | 
			
		||||
            services.AddOptions();
 | 
			
		||||
            services.AddMvcCore().AddJsonFormatters();
 | 
			
		||||
            services.AddLogging();
 | 
			
		||||
 | 
			
		||||
            // initial configuration from yaml
 | 
			
		||||
            services.Configure<YamlConfiguration>(configurationRoot);
 | 
			
		||||
 | 
			
		||||
            // Add framework services.
 | 
			
		||||
            // ocelot services.
 | 
			
		||||
            services.AddSingleton<IAddHeadersToRequest, AddHeadersToRequest>();
 | 
			
		||||
            services.AddSingleton<IClaimsParser, ClaimsParser>();
 | 
			
		||||
            services.AddSingleton<IConfigurationHeaderExtrator, ConfigurationHeaderExtrator>();
 | 
			
		||||
            services.AddSingleton<IClaimToHeaderConfigurationParser, ClaimToHeaderConfigurationParser>();
 | 
			
		||||
            services.AddSingleton<IConfigurationValidator, ConfigurationValidator>();
 | 
			
		||||
            services.AddSingleton<IOcelotConfiguration, OcelotConfiguration>();
 | 
			
		||||
            services.AddSingleton<IUrlPathToUrlTemplateMatcher, RegExUrlMatcher>();
 | 
			
		||||
@@ -38,6 +44,7 @@
 | 
			
		||||
            services.AddSingleton<IAuthenticationHandlerCreator, AuthenticationHandlerCreator>();
 | 
			
		||||
 | 
			
		||||
            // see this for why we register this as singleton http://stackoverflow.com/questions/37371264/invalidoperationexception-unable-to-resolve-service-for-type-microsoft-aspnetc
 | 
			
		||||
            // could maybe use a scoped data repository
 | 
			
		||||
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
 | 
			
		||||
            services.AddScoped<IScopedRequestDataRepository, ScopedRequestDataRepository>();
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -30,9 +30,9 @@ namespace Ocelot.Library.Middleware
 | 
			
		||||
        {
 | 
			
		||||
            var downstreamRoute = _scopedRequestDataRepository.Get<DownstreamRoute>("DownstreamRoute");
 | 
			
		||||
 | 
			
		||||
            if (downstreamRoute.Data.ReRoute.ConfigurationHeaderExtractorProperties.Any())
 | 
			
		||||
            if (downstreamRoute.Data.ReRoute.ClaimsToHeaders.Any())
 | 
			
		||||
            {
 | 
			
		||||
                _addHeadersToRequest.SetHeadersOnContext(downstreamRoute.Data.ReRoute.ConfigurationHeaderExtractorProperties, context);
 | 
			
		||||
                _addHeadersToRequest.SetHeadersOnContext(downstreamRoute.Data.ReRoute.ClaimsToHeaders, context);
 | 
			
		||||
            }
 | 
			
		||||
            
 | 
			
		||||
            await _next.Invoke(context);
 | 
			
		||||
 
 | 
			
		||||
@@ -2,6 +2,7 @@
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using Microsoft.AspNetCore.Http;
 | 
			
		||||
using Microsoft.Extensions.Primitives;
 | 
			
		||||
using Ocelot.Library.Configuration;
 | 
			
		||||
using Ocelot.Library.Responses;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Library.RequestBuilder
 | 
			
		||||
@@ -15,7 +16,7 @@ namespace Ocelot.Library.RequestBuilder
 | 
			
		||||
            _claimsParser = claimsParser;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public Response SetHeadersOnContext(List<ConfigurationHeaderExtractorProperties> configurationHeaderExtractorProperties, HttpContext context)
 | 
			
		||||
        public Response SetHeadersOnContext(List<ClaimToHeader> configurationHeaderExtractorProperties, HttpContext context)
 | 
			
		||||
        {
 | 
			
		||||
            foreach (var config in configurationHeaderExtractorProperties)
 | 
			
		||||
            {
 | 
			
		||||
 
 | 
			
		||||
@@ -1,12 +1,13 @@
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using Microsoft.AspNetCore.Http;
 | 
			
		||||
using Ocelot.Library.Configuration;
 | 
			
		||||
using Ocelot.Library.Responses;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Library.RequestBuilder
 | 
			
		||||
{
 | 
			
		||||
    public interface IAddHeadersToRequest
 | 
			
		||||
    {
 | 
			
		||||
        Response SetHeadersOnContext(List<ConfigurationHeaderExtractorProperties> configurationHeaderExtractorProperties,
 | 
			
		||||
        Response SetHeadersOnContext(List<ClaimToHeader> configurationHeaderExtractorProperties,
 | 
			
		||||
            HttpContext context);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,10 +0,0 @@
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using Ocelot.Library.Responses;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Library.RequestBuilder
 | 
			
		||||
{
 | 
			
		||||
    public interface IConfigurationHeaderExtrator
 | 
			
		||||
    {
 | 
			
		||||
        Response<ConfigurationHeaderExtractorProperties> Extract(string headerKey, string value);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,22 +0,0 @@
 | 
			
		||||
using Microsoft.AspNetCore.Authorization;
 | 
			
		||||
using Microsoft.AspNetCore.Mvc;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Controllers
 | 
			
		||||
{
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// This controller is a catch all for requests so that if we build it into a pipeline
 | 
			
		||||
    /// the requests get authorised.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    public class HomeController : Controller
 | 
			
		||||
    {
 | 
			
		||||
        //[Authorize]
 | 
			
		||||
        [Route("{*url}")]
 | 
			
		||||
        public void Index()
 | 
			
		||||
        {
 | 
			
		||||
            if (true == true)
 | 
			
		||||
            {
 | 
			
		||||
                
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -28,11 +28,6 @@ namespace Ocelot
 | 
			
		||||
        // This method gets called by the runtime. Use this method to add services to the container.
 | 
			
		||||
        public void ConfigureServices(IServiceCollection services)
 | 
			
		||||
        {
 | 
			
		||||
            services.AddOptions();
 | 
			
		||||
            services.AddMvc();
 | 
			
		||||
            services.AddMvcCore().AddAuthorization().AddJsonFormatters();
 | 
			
		||||
            services.AddAuthentication();
 | 
			
		||||
            services.AddLogging();
 | 
			
		||||
            services.AddOcelot(Configuration);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user