mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 15:30:49 +08:00 
			
		
		
		
	Merge branch 'develop' of https://github.com/geffzhang/Ocelot into develop
This commit is contained in:
		@@ -19,11 +19,11 @@ namespace Ocelot.Authentication.Handler.Creator
 | 
			
		||||
            builder.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
 | 
			
		||||
            {
 | 
			
		||||
                Authority = authOptions.ProviderRootUrl,
 | 
			
		||||
                ApiName = authOptions.ScopeName,
 | 
			
		||||
                ApiName = authOptions.ApiName,
 | 
			
		||||
                RequireHttpsMetadata = authOptions.RequireHttps,
 | 
			
		||||
                AllowedScopes = authOptions.AdditionalScopes,
 | 
			
		||||
                AllowedScopes = authOptions.AllowedScopes,
 | 
			
		||||
                SupportedTokens = SupportedTokens.Both,
 | 
			
		||||
                ApiSecret = authOptions.ScopeSecret
 | 
			
		||||
                ApiSecret = authOptions.ApiSecret
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
            var authenticationNext = builder.Build();
 | 
			
		||||
 
 | 
			
		||||
@@ -4,22 +4,22 @@ namespace Ocelot.Configuration
 | 
			
		||||
{
 | 
			
		||||
    public class AuthenticationOptions
 | 
			
		||||
    {
 | 
			
		||||
        public AuthenticationOptions(string provider, string providerRootUrl, string scopeName, bool requireHttps, List<string> additionalScopes, string scopeSecret)
 | 
			
		||||
        public AuthenticationOptions(string provider, string providerRootUrl, string apiName, bool requireHttps, List<string> allowedScopes, string apiSecret)
 | 
			
		||||
        {
 | 
			
		||||
            Provider = provider;
 | 
			
		||||
            ProviderRootUrl = providerRootUrl;
 | 
			
		||||
            ScopeName = scopeName;
 | 
			
		||||
			ApiName = apiName;
 | 
			
		||||
            RequireHttps = requireHttps;
 | 
			
		||||
            AdditionalScopes = additionalScopes;
 | 
			
		||||
            ScopeSecret = scopeSecret;
 | 
			
		||||
			AllowedScopes = allowedScopes;
 | 
			
		||||
            ApiSecret = apiSecret;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public string Provider { get; private set; }
 | 
			
		||||
        public string ProviderRootUrl { get; private set; }
 | 
			
		||||
        public string ScopeName { get; private set; }
 | 
			
		||||
        public string ScopeSecret { get; private set; }
 | 
			
		||||
        public string ApiName { get; private set; }
 | 
			
		||||
        public string ApiSecret { get; private set; }
 | 
			
		||||
        public bool RequireHttps { get; private set; }
 | 
			
		||||
        public List<string> AdditionalScopes { get; private set; }
 | 
			
		||||
        public List<string> AllowedScopes { get; private set; }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -7,10 +7,10 @@ namespace Ocelot.Configuration.Builder
 | 
			
		||||
 | 
			
		||||
        private string _provider;
 | 
			
		||||
        private string _providerRootUrl;
 | 
			
		||||
        private string _scopeName;
 | 
			
		||||
        private string _scopeSecret;
 | 
			
		||||
        private string _apiName;
 | 
			
		||||
        private string _apiSecret;
 | 
			
		||||
        private bool _requireHttps;
 | 
			
		||||
        private List<string> _additionalScopes;
 | 
			
		||||
        private List<string> _allowedScopes;
 | 
			
		||||
 | 
			
		||||
        public AuthenticationOptionsBuilder WithProvider(string provider)
 | 
			
		||||
        {
 | 
			
		||||
@@ -24,15 +24,15 @@ namespace Ocelot.Configuration.Builder
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public AuthenticationOptionsBuilder WithScopeName(string scopeName)
 | 
			
		||||
        public AuthenticationOptionsBuilder WithApiName(string apiName)
 | 
			
		||||
        {
 | 
			
		||||
            _scopeName = scopeName;
 | 
			
		||||
            _apiName = apiName;
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public AuthenticationOptionsBuilder WithScopeSecret(string scopeSecret)
 | 
			
		||||
        public AuthenticationOptionsBuilder WithApiSecret(string apiSecret)
 | 
			
		||||
        {
 | 
			
		||||
            _scopeSecret = scopeSecret;
 | 
			
		||||
            _apiSecret = apiSecret;
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -42,15 +42,15 @@ namespace Ocelot.Configuration.Builder
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public AuthenticationOptionsBuilder WithAdditionalScopes(List<string> additionalScopes)
 | 
			
		||||
        public AuthenticationOptionsBuilder WithAllowedScopes(List<string> allowedScopes)
 | 
			
		||||
        {
 | 
			
		||||
            _additionalScopes = additionalScopes;
 | 
			
		||||
            _allowedScopes = allowedScopes;
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public AuthenticationOptions Build()
 | 
			
		||||
        {
 | 
			
		||||
            return new AuthenticationOptions(_provider, _providerRootUrl, _scopeName, _requireHttps, _additionalScopes, _scopeSecret);
 | 
			
		||||
            return new AuthenticationOptions(_provider, _providerRootUrl, _apiName, _requireHttps, _allowedScopes, _apiSecret);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -10,10 +10,10 @@ namespace Ocelot.Configuration.Creator
 | 
			
		||||
            return new AuthenticationOptionsBuilder()
 | 
			
		||||
                                        .WithProvider(fileReRoute.AuthenticationOptions?.Provider)
 | 
			
		||||
                                        .WithProviderRootUrl(fileReRoute.AuthenticationOptions?.ProviderRootUrl)
 | 
			
		||||
                                        .WithScopeName(fileReRoute.AuthenticationOptions?.ScopeName)
 | 
			
		||||
                                        .WithApiName(fileReRoute.AuthenticationOptions?.ApiName)
 | 
			
		||||
                                        .WithRequireHttps(fileReRoute.AuthenticationOptions.RequireHttps)
 | 
			
		||||
                                        .WithAdditionalScopes(fileReRoute.AuthenticationOptions?.AdditionalScopes)
 | 
			
		||||
                                        .WithScopeSecret(fileReRoute.AuthenticationOptions?.ScopeSecret)
 | 
			
		||||
                                        .WithAllowedScopes(fileReRoute.AuthenticationOptions?.AllowedScopes)
 | 
			
		||||
                                        .WithApiSecret(fileReRoute.AuthenticationOptions?.ApiSecret)
 | 
			
		||||
                                        .Build();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -6,14 +6,14 @@ namespace Ocelot.Configuration.File
 | 
			
		||||
    {
 | 
			
		||||
        public FileAuthenticationOptions()
 | 
			
		||||
        {
 | 
			
		||||
            AdditionalScopes = new List<string>();
 | 
			
		||||
			AllowedScopes = new List<string>();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public string Provider { get; set; }
 | 
			
		||||
        public string ProviderRootUrl { get; set; }
 | 
			
		||||
        public string ScopeName { get; set; }
 | 
			
		||||
        public string ApiName { get; set; }
 | 
			
		||||
        public bool RequireHttps { get; set; }
 | 
			
		||||
        public List<string> AdditionalScopes { get; set; }
 | 
			
		||||
        public string ScopeSecret { get; set; }
 | 
			
		||||
        public List<string> AllowedScopes { get; set; }
 | 
			
		||||
        public string ApiSecret { get; set; }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -25,7 +25,7 @@ namespace Ocelot.DownstreamRouteFinder.Finder
 | 
			
		||||
        {
 | 
			
		||||
            var configuration = _configProvider.Get();
 | 
			
		||||
 | 
			
		||||
            var applicableReRoutes = configuration.Data.ReRoutes.Where(r => string.Equals(r.UpstreamHttpMethod.Method, upstreamHttpMethod, StringComparison.CurrentCultureIgnoreCase));
 | 
			
		||||
            var applicableReRoutes = configuration.Data.ReRoutes.Where(r => string.Equals(r.UpstreamHttpMethod.Method.ToLower(), upstreamHttpMethod.ToLower(), StringComparison.CurrentCultureIgnoreCase));
 | 
			
		||||
 | 
			
		||||
            foreach (var reRoute in applicableReRoutes)
 | 
			
		||||
            {
 | 
			
		||||
 
 | 
			
		||||
@@ -1,17 +1,16 @@
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using Ocelot.Errors;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Responder
 | 
			
		||||
{
 | 
			
		||||
    public class ErrorsToHttpStatusCodeMapper : IErrorsToHttpStatusCodeMapper
 | 
			
		||||
    {
 | 
			
		||||
        public Response<int> Map(List<Error> errors)
 | 
			
		||||
        public int Map(List<Error> errors)
 | 
			
		||||
        {
 | 
			
		||||
            if (errors.Any(e => e.Code == OcelotErrorCode.UnauthenticatedError))
 | 
			
		||||
            {
 | 
			
		||||
                return new OkResponse<int>(401);
 | 
			
		||||
                return 401;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (errors.Any(e => e.Code == OcelotErrorCode.UnauthorizedError 
 | 
			
		||||
@@ -19,15 +18,15 @@ namespace Ocelot.Responder
 | 
			
		||||
                || e.Code == OcelotErrorCode.UserDoesNotHaveClaimError
 | 
			
		||||
                || e.Code == OcelotErrorCode.CannotFindClaimError))
 | 
			
		||||
            {
 | 
			
		||||
                return new OkResponse<int>(403);
 | 
			
		||||
                return 403;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (errors.Any(e => e.Code == OcelotErrorCode.RequestTimedOutError))
 | 
			
		||||
            {
 | 
			
		||||
                return new OkResponse<int>(503);
 | 
			
		||||
                return 503;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            return new OkResponse<int>(404);
 | 
			
		||||
            return 404;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,11 +1,13 @@
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using Ocelot.Errors;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Responder
 | 
			
		||||
{
 | 
			
		||||
{
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Map a list OceoltErrors to a single appropriate HTTP status code
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    public interface IErrorsToHttpStatusCodeMapper
 | 
			
		||||
    {
 | 
			
		||||
        Response<int> Map(List<Error> errors);
 | 
			
		||||
        int Map(List<Error> errors);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -16,12 +16,12 @@ namespace Ocelot.Responder.Middleware
 | 
			
		||||
        private readonly IErrorsToHttpStatusCodeMapper _codeMapper;
 | 
			
		||||
        private readonly IOcelotLogger _logger;
 | 
			
		||||
 | 
			
		||||
        public ResponderMiddleware(RequestDelegate next, 
 | 
			
		||||
        public ResponderMiddleware(RequestDelegate next,
 | 
			
		||||
            IHttpResponder responder,
 | 
			
		||||
            IOcelotLoggerFactory loggerFactory,
 | 
			
		||||
            IRequestScopedDataRepository requestScopedDataRepository, 
 | 
			
		||||
            IRequestScopedDataRepository requestScopedDataRepository,
 | 
			
		||||
            IErrorsToHttpStatusCodeMapper codeMapper)
 | 
			
		||||
            :base(requestScopedDataRepository)
 | 
			
		||||
            : base(requestScopedDataRepository)
 | 
			
		||||
        {
 | 
			
		||||
            _next = next;
 | 
			
		||||
            _responder = responder;
 | 
			
		||||
@@ -58,16 +58,9 @@ namespace Ocelot.Responder.Middleware
 | 
			
		||||
 | 
			
		||||
        private void SetErrorResponse(HttpContext context, List<Error> errors)
 | 
			
		||||
        {
 | 
			
		||||
            var statusCode = _codeMapper.Map(errors);
 | 
			
		||||
 | 
			
		||||
            if (!statusCode.IsError)
 | 
			
		||||
            {
 | 
			
		||||
                _responder.SetErrorResponseOnContext(context, statusCode.Data);
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                _responder.SetErrorResponseOnContext(context, 500);
 | 
			
		||||
            }
 | 
			
		||||
            var statusCode = _codeMapper.Map(errors);
 | 
			
		||||
 | 
			
		||||
            _responder.SetErrorResponseOnContext(context, statusCode);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user