mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 09:15:27 +08:00 
			
		
		
		
	all tests passing but i think im going to take registering auth our of the config
This commit is contained in:
		@@ -1,14 +0,0 @@
 | 
			
		||||
namespace Ocelot.Authentication.Handler
 | 
			
		||||
{
 | 
			
		||||
    public class AuthenticationHandler
 | 
			
		||||
    {
 | 
			
		||||
        public AuthenticationHandler(string provider, IHandler handler)
 | 
			
		||||
        {
 | 
			
		||||
            Provider = provider;
 | 
			
		||||
            Handler = handler;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public string Provider { get; private set; }
 | 
			
		||||
        public IHandler Handler { get; private set; }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,57 +0,0 @@
 | 
			
		||||
using System;
 | 
			
		||||
using IdentityServer4.AccessTokenValidation;
 | 
			
		||||
using Microsoft.AspNetCore.Authentication;
 | 
			
		||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
 | 
			
		||||
using Microsoft.AspNetCore.Builder;
 | 
			
		||||
using Microsoft.AspNetCore.Http;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Authentication.Handler.Creator
 | 
			
		||||
{
 | 
			
		||||
    using Ocelot.Configuration;
 | 
			
		||||
 | 
			
		||||
    using AuthenticationOptions = Configuration.AuthenticationOptions;
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Cannot unit test things in this class due to use of extension methods
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    public class AuthenticationHandlerCreator : IAuthenticationHandlerCreator
 | 
			
		||||
    {
 | 
			
		||||
        public Response<RequestDelegate> Create(IApplicationBuilder app, AuthenticationOptions authOptions)
 | 
			
		||||
        {
 | 
			
		||||
            throw new NotImplementedException();
 | 
			
		||||
            var builder = app.New();
 | 
			
		||||
 | 
			
		||||
         /*   if (authOptions.Provider.ToLower() == "jwt")
 | 
			
		||||
            {
 | 
			
		||||
                var authenticationConfig = authOptions.Config as JwtConfig;
 | 
			
		||||
 | 
			
		||||
                builder.UseJwtBearerAuthentication(
 | 
			
		||||
                    new JwtBearerOptions()
 | 
			
		||||
                        {
 | 
			
		||||
                            Authority = authenticationConfig.Authority,
 | 
			
		||||
                            Audience = authenticationConfig.Audience
 | 
			
		||||
                        });
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                var authenticationConfig = authOptions.Config as IdentityServerConfig;
 | 
			
		||||
 | 
			
		||||
                builder.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
 | 
			
		||||
                {
 | 
			
		||||
                    Authority = authenticationConfig.ProviderRootUrl,
 | 
			
		||||
                    ApiName = authenticationConfig.ApiName,
 | 
			
		||||
                    RequireHttpsMetadata = authenticationConfig.RequireHttps,
 | 
			
		||||
                    AllowedScopes = authOptions.AllowedScopes,
 | 
			
		||||
                    SupportedTokens = SupportedTokens.Both,
 | 
			
		||||
                    ApiSecret = authenticationConfig.ApiSecret
 | 
			
		||||
                });
 | 
			
		||||
            }*/
 | 
			
		||||
 | 
			
		||||
            var authenticationNext = builder.Build();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
            return new OkResponse<RequestDelegate>(authenticationNext);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,13 +0,0 @@
 | 
			
		||||
using Microsoft.AspNetCore.Builder;
 | 
			
		||||
using Microsoft.AspNetCore.Http;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Authentication.Handler.Creator
 | 
			
		||||
{
 | 
			
		||||
    using AuthenticationOptions = Configuration.AuthenticationOptions;
 | 
			
		||||
 | 
			
		||||
    public interface IAuthenticationHandlerCreator
 | 
			
		||||
    {
 | 
			
		||||
        Response<RequestDelegate> Create(IApplicationBuilder app, AuthenticationOptions authOptions);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,36 +0,0 @@
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using Microsoft.AspNetCore.Builder;
 | 
			
		||||
using Ocelot.Authentication.Handler.Creator;
 | 
			
		||||
using Ocelot.Errors;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Authentication.Handler.Factory
 | 
			
		||||
{
 | 
			
		||||
    using AuthenticationOptions = Configuration.AuthenticationOptions;
 | 
			
		||||
 | 
			
		||||
    public class AuthenticationHandlerFactory : IAuthenticationHandlerFactory
 | 
			
		||||
    {
 | 
			
		||||
        private readonly IAuthenticationHandlerCreator _creator;
 | 
			
		||||
 | 
			
		||||
        public AuthenticationHandlerFactory(IAuthenticationHandlerCreator creator)
 | 
			
		||||
        {
 | 
			
		||||
            _creator = creator;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public Response<AuthenticationHandler> Get(IApplicationBuilder app, AuthenticationOptions authOptions)
 | 
			
		||||
        {
 | 
			
		||||
            var handler = _creator.Create(app, authOptions);
 | 
			
		||||
 | 
			
		||||
            if (!handler.IsError)
 | 
			
		||||
            {
 | 
			
		||||
                return new OkResponse<AuthenticationHandler>(
 | 
			
		||||
                    new AuthenticationHandler(authOptions.Provider, new RequestDelegateHandler(handler.Data)));
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            return new ErrorResponse<AuthenticationHandler>(new List<Error>
 | 
			
		||||
            {
 | 
			
		||||
                new UnableToCreateAuthenticationHandlerError($"Unable to create authentication handler for {authOptions.Provider}")
 | 
			
		||||
            });
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,12 +0,0 @@
 | 
			
		||||
using Microsoft.AspNetCore.Builder;
 | 
			
		||||
using Ocelot.Responses;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Authentication.Handler.Factory
 | 
			
		||||
{
 | 
			
		||||
    using AuthenticationOptions = Configuration.AuthenticationOptions;
 | 
			
		||||
 | 
			
		||||
    public interface IAuthenticationHandlerFactory
 | 
			
		||||
    {
 | 
			
		||||
        Response<AuthenticationHandler> Get(IApplicationBuilder app, AuthenticationOptions authOptions);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,12 +0,0 @@
 | 
			
		||||
using Ocelot.Errors;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Authentication.Handler.Factory
 | 
			
		||||
{
 | 
			
		||||
    public class UnableToCreateAuthenticationHandlerError : Error
 | 
			
		||||
    {
 | 
			
		||||
        public UnableToCreateAuthenticationHandlerError(string message) 
 | 
			
		||||
            : base(message, OcelotErrorCode.UnableToCreateAuthenticationHandlerError)
 | 
			
		||||
        {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,10 +0,0 @@
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
using Microsoft.AspNetCore.Http;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Authentication.Handler
 | 
			
		||||
{
 | 
			
		||||
    public interface IHandler
 | 
			
		||||
    {
 | 
			
		||||
        Task Handle(HttpContext context);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,20 +0,0 @@
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
using Microsoft.AspNetCore.Http;
 | 
			
		||||
 | 
			
		||||
namespace Ocelot.Authentication.Handler
 | 
			
		||||
{
 | 
			
		||||
    public class RequestDelegateHandler : IHandler
 | 
			
		||||
    {
 | 
			
		||||
        private readonly RequestDelegate _requestDelegate;
 | 
			
		||||
 | 
			
		||||
        public RequestDelegateHandler(RequestDelegate requestDelegate)
 | 
			
		||||
        {
 | 
			
		||||
            _requestDelegate = requestDelegate;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task Handle(HttpContext context)
 | 
			
		||||
        {
 | 
			
		||||
            await _requestDelegate.Invoke(context);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -3,7 +3,6 @@ using System.Threading.Tasks;
 | 
			
		||||
using Microsoft.AspNetCore.Authentication;
 | 
			
		||||
using Microsoft.AspNetCore.Builder;
 | 
			
		||||
using Microsoft.AspNetCore.Http;
 | 
			
		||||
using Ocelot.Authentication.Handler.Factory;
 | 
			
		||||
using Ocelot.Configuration;
 | 
			
		||||
using Ocelot.Errors;
 | 
			
		||||
using Ocelot.Infrastructure.Extensions;
 | 
			
		||||
@@ -18,18 +17,15 @@ namespace Ocelot.Authentication.Middleware
 | 
			
		||||
        private readonly RequestDelegate _next;
 | 
			
		||||
        private readonly IApplicationBuilder _app;
 | 
			
		||||
        private readonly IAuthenticationSchemeProvider _authSchemeProvider;
 | 
			
		||||
        private readonly IAuthenticationHandlerFactory _authHandlerFactory;
 | 
			
		||||
        private readonly IOcelotLogger _logger;
 | 
			
		||||
 | 
			
		||||
        public AuthenticationMiddleware(RequestDelegate next,
 | 
			
		||||
            IApplicationBuilder app,
 | 
			
		||||
            IRequestScopedDataRepository requestScopedDataRepository,
 | 
			
		||||
            IAuthenticationHandlerFactory authHandlerFactory,
 | 
			
		||||
            IOcelotLoggerFactory loggerFactory)
 | 
			
		||||
            : base(requestScopedDataRepository)
 | 
			
		||||
        {
 | 
			
		||||
            _next = next;
 | 
			
		||||
            _authHandlerFactory = authHandlerFactory;
 | 
			
		||||
            _app = app;
 | 
			
		||||
            _logger = loggerFactory.CreateLogger<AuthenticationMiddleware>();
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
@@ -4,8 +4,6 @@ using Microsoft.AspNetCore.Http;
 | 
			
		||||
using Microsoft.Extensions.Configuration;
 | 
			
		||||
using Microsoft.Extensions.DependencyInjection;
 | 
			
		||||
using Microsoft.Extensions.DependencyInjection.Extensions;
 | 
			
		||||
using Ocelot.Authentication.Handler.Creator;
 | 
			
		||||
using Ocelot.Authentication.Handler.Factory;
 | 
			
		||||
using Ocelot.Authorisation;
 | 
			
		||||
using Ocelot.Cache;
 | 
			
		||||
using Ocelot.Claims;
 | 
			
		||||
@@ -129,8 +127,6 @@ namespace Ocelot.DependencyInjection
 | 
			
		||||
            services.TryAddSingleton<IHttpResponder, HttpContextResponder>();
 | 
			
		||||
            services.TryAddSingleton<IRequestCreator, HttpRequestCreator>();
 | 
			
		||||
            services.TryAddSingleton<IErrorsToHttpStatusCodeMapper, ErrorsToHttpStatusCodeMapper>();
 | 
			
		||||
            services.TryAddSingleton<IAuthenticationHandlerFactory, AuthenticationHandlerFactory>();
 | 
			
		||||
            services.TryAddSingleton<IAuthenticationHandlerCreator, AuthenticationHandlerCreator>();
 | 
			
		||||
            services.TryAddSingleton<IRateLimitCounterHandler, MemoryCacheRateLimitCounterHandler>();
 | 
			
		||||
            services.TryAddSingleton<IHttpClientCache, MemoryHttpClientCache>();
 | 
			
		||||
            services.TryAddSingleton<IRequestMapper, RequestMapper>();
 | 
			
		||||
@@ -162,7 +158,7 @@ namespace Ocelot.DependencyInjection
 | 
			
		||||
            {
 | 
			
		||||
                if(authOptions.Provider.ToLower() == "identityserver")
 | 
			
		||||
                {
 | 
			
		||||
                     Action<IdentityServerAuthenticationOptions> options = o =>
 | 
			
		||||
                    Action<IdentityServerAuthenticationOptions> options = o =>
 | 
			
		||||
                    {
 | 
			
		||||
                        o.Authority = authOptions.IdentityServerConfig.ProviderRootUrl;
 | 
			
		||||
                        o.ApiName = authOptions.IdentityServerConfig.ApiName;
 | 
			
		||||
@@ -176,7 +172,12 @@ namespace Ocelot.DependencyInjection
 | 
			
		||||
                }
 | 
			
		||||
                else if (authOptions.Provider.ToLower() == "jwt")
 | 
			
		||||
                {
 | 
			
		||||
                    //todo - make this work for nick..
 | 
			
		||||
                    services.AddAuthentication()
 | 
			
		||||
                        .AddJwtBearer(x =>
 | 
			
		||||
                        {
 | 
			
		||||
                            x.Authority = authOptions.JwtConfig.Authority;
 | 
			
		||||
                            x.Audience = authOptions.JwtConfig.Audience;
 | 
			
		||||
                        });
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user