From 967f0f7128a89f34f0fcaafc89f7987ad2fffb5f Mon Sep 17 00:00:00 2001 From: Tom Pallister Date: Wed, 1 Nov 2017 13:37:03 +0000 Subject: [PATCH] all tests passing but i think im going to take registering auth our of the config --- .../Handler/AuthenticationHandler.cs | 14 --- .../Creator/AuthenticationHandlerCreator.cs | 57 ---------- .../Creator/IAuthenticationHandlerCreator.cs | 13 --- .../Factory/AuthenticationHandlerFactory.cs | 36 ------- .../Factory/IAuthenticationHandlerFactory.cs | 12 --- ...nableToCreateAuthenticationHandlerError.cs | 12 --- src/Ocelot/Authentication/Handler/IHandler.cs | 10 -- .../Handler/RequestDelegateHandler.cs | 20 ---- .../Middleware/AuthenticationMiddleware.cs | 4 - .../ServiceCollectionExtensions.cs | 13 +-- .../AuthenticationHandlerFactoryTests.cs | 100 ------------------ .../AuthenticationMiddlewareTests.cs | 6 -- 12 files changed, 7 insertions(+), 290 deletions(-) delete mode 100644 src/Ocelot/Authentication/Handler/AuthenticationHandler.cs delete mode 100644 src/Ocelot/Authentication/Handler/Creator/AuthenticationHandlerCreator.cs delete mode 100644 src/Ocelot/Authentication/Handler/Creator/IAuthenticationHandlerCreator.cs delete mode 100644 src/Ocelot/Authentication/Handler/Factory/AuthenticationHandlerFactory.cs delete mode 100644 src/Ocelot/Authentication/Handler/Factory/IAuthenticationHandlerFactory.cs delete mode 100644 src/Ocelot/Authentication/Handler/Factory/UnableToCreateAuthenticationHandlerError.cs delete mode 100644 src/Ocelot/Authentication/Handler/IHandler.cs delete mode 100644 src/Ocelot/Authentication/Handler/RequestDelegateHandler.cs delete mode 100644 test/Ocelot.UnitTests/Authentication/AuthenticationHandlerFactoryTests.cs diff --git a/src/Ocelot/Authentication/Handler/AuthenticationHandler.cs b/src/Ocelot/Authentication/Handler/AuthenticationHandler.cs deleted file mode 100644 index 3cb662b8..00000000 --- a/src/Ocelot/Authentication/Handler/AuthenticationHandler.cs +++ /dev/null @@ -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; } - } -} \ No newline at end of file diff --git a/src/Ocelot/Authentication/Handler/Creator/AuthenticationHandlerCreator.cs b/src/Ocelot/Authentication/Handler/Creator/AuthenticationHandlerCreator.cs deleted file mode 100644 index 1e0a4f72..00000000 --- a/src/Ocelot/Authentication/Handler/Creator/AuthenticationHandlerCreator.cs +++ /dev/null @@ -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; - - /// - /// Cannot unit test things in this class due to use of extension methods - /// - public class AuthenticationHandlerCreator : IAuthenticationHandlerCreator - { - public Response 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(authenticationNext); - } - } -} \ No newline at end of file diff --git a/src/Ocelot/Authentication/Handler/Creator/IAuthenticationHandlerCreator.cs b/src/Ocelot/Authentication/Handler/Creator/IAuthenticationHandlerCreator.cs deleted file mode 100644 index 9d92c81d..00000000 --- a/src/Ocelot/Authentication/Handler/Creator/IAuthenticationHandlerCreator.cs +++ /dev/null @@ -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 Create(IApplicationBuilder app, AuthenticationOptions authOptions); - } -} diff --git a/src/Ocelot/Authentication/Handler/Factory/AuthenticationHandlerFactory.cs b/src/Ocelot/Authentication/Handler/Factory/AuthenticationHandlerFactory.cs deleted file mode 100644 index 60253816..00000000 --- a/src/Ocelot/Authentication/Handler/Factory/AuthenticationHandlerFactory.cs +++ /dev/null @@ -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 Get(IApplicationBuilder app, AuthenticationOptions authOptions) - { - var handler = _creator.Create(app, authOptions); - - if (!handler.IsError) - { - return new OkResponse( - new AuthenticationHandler(authOptions.Provider, new RequestDelegateHandler(handler.Data))); - } - - return new ErrorResponse(new List - { - new UnableToCreateAuthenticationHandlerError($"Unable to create authentication handler for {authOptions.Provider}") - }); - } - } -} \ No newline at end of file diff --git a/src/Ocelot/Authentication/Handler/Factory/IAuthenticationHandlerFactory.cs b/src/Ocelot/Authentication/Handler/Factory/IAuthenticationHandlerFactory.cs deleted file mode 100644 index abc09ed8..00000000 --- a/src/Ocelot/Authentication/Handler/Factory/IAuthenticationHandlerFactory.cs +++ /dev/null @@ -1,12 +0,0 @@ -using Microsoft.AspNetCore.Builder; -using Ocelot.Responses; - -namespace Ocelot.Authentication.Handler.Factory -{ - using AuthenticationOptions = Configuration.AuthenticationOptions; - - public interface IAuthenticationHandlerFactory - { - Response Get(IApplicationBuilder app, AuthenticationOptions authOptions); - } -} diff --git a/src/Ocelot/Authentication/Handler/Factory/UnableToCreateAuthenticationHandlerError.cs b/src/Ocelot/Authentication/Handler/Factory/UnableToCreateAuthenticationHandlerError.cs deleted file mode 100644 index 7e18b203..00000000 --- a/src/Ocelot/Authentication/Handler/Factory/UnableToCreateAuthenticationHandlerError.cs +++ /dev/null @@ -1,12 +0,0 @@ -using Ocelot.Errors; - -namespace Ocelot.Authentication.Handler.Factory -{ - public class UnableToCreateAuthenticationHandlerError : Error - { - public UnableToCreateAuthenticationHandlerError(string message) - : base(message, OcelotErrorCode.UnableToCreateAuthenticationHandlerError) - { - } - } -} diff --git a/src/Ocelot/Authentication/Handler/IHandler.cs b/src/Ocelot/Authentication/Handler/IHandler.cs deleted file mode 100644 index 99d240e8..00000000 --- a/src/Ocelot/Authentication/Handler/IHandler.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System.Threading.Tasks; -using Microsoft.AspNetCore.Http; - -namespace Ocelot.Authentication.Handler -{ - public interface IHandler - { - Task Handle(HttpContext context); - } -} \ No newline at end of file diff --git a/src/Ocelot/Authentication/Handler/RequestDelegateHandler.cs b/src/Ocelot/Authentication/Handler/RequestDelegateHandler.cs deleted file mode 100644 index 291e8ec3..00000000 --- a/src/Ocelot/Authentication/Handler/RequestDelegateHandler.cs +++ /dev/null @@ -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); - } - } -} \ No newline at end of file diff --git a/src/Ocelot/Authentication/Middleware/AuthenticationMiddleware.cs b/src/Ocelot/Authentication/Middleware/AuthenticationMiddleware.cs index 8b8fb640..8df1757c 100644 --- a/src/Ocelot/Authentication/Middleware/AuthenticationMiddleware.cs +++ b/src/Ocelot/Authentication/Middleware/AuthenticationMiddleware.cs @@ -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(); } diff --git a/src/Ocelot/DependencyInjection/ServiceCollectionExtensions.cs b/src/Ocelot/DependencyInjection/ServiceCollectionExtensions.cs index c38d3699..567e3659 100644 --- a/src/Ocelot/DependencyInjection/ServiceCollectionExtensions.cs +++ b/src/Ocelot/DependencyInjection/ServiceCollectionExtensions.cs @@ -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(); services.TryAddSingleton(); services.TryAddSingleton(); - services.TryAddSingleton(); - services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(); @@ -162,7 +158,7 @@ namespace Ocelot.DependencyInjection { if(authOptions.Provider.ToLower() == "identityserver") { - Action options = o => + Action 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; + }); } } diff --git a/test/Ocelot.UnitTests/Authentication/AuthenticationHandlerFactoryTests.cs b/test/Ocelot.UnitTests/Authentication/AuthenticationHandlerFactoryTests.cs deleted file mode 100644 index 55e1a05c..00000000 --- a/test/Ocelot.UnitTests/Authentication/AuthenticationHandlerFactoryTests.cs +++ /dev/null @@ -1,100 +0,0 @@ -using System.Collections.Generic; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Http; -using Moq; -using Ocelot.Authentication.Handler; -using Ocelot.Authentication.Handler.Creator; -using Ocelot.Authentication.Handler.Factory; -using Ocelot.Configuration.Builder; -using Ocelot.Errors; -using Ocelot.Responses; -using Shouldly; -using TestStack.BDDfy; -using Xunit; -using AuthenticationOptions = Ocelot.Configuration.AuthenticationOptions; - -namespace Ocelot.UnitTests.Authentication -{ - public class AuthenticationHandlerFactoryTests - { - private readonly IAuthenticationHandlerFactory _authenticationHandlerFactory; - private readonly Mock _app; - private readonly Mock _creator; - private AuthenticationOptions _authenticationOptions; - private Response _result; - - public AuthenticationHandlerFactoryTests() - { - _app = new Mock(); - _creator = new Mock(); - _authenticationHandlerFactory = new AuthenticationHandlerFactory(_creator.Object); - } - - [Theory] - [InlineData("IdentityServer")] - [InlineData("Jwt")] - public void should_return_access_token_handler(string provider) - { - var authenticationOptions = new AuthenticationOptionsBuilder() - .WithProvider(provider) - .Build(); - - this.Given(x => x.GivenTheAuthenticationOptionsAre(authenticationOptions)) - .And(x => x.GivenTheCreatorReturns()) - .When(x => x.WhenIGetFromTheFactory()) - .Then(x => x.ThenTheHandlerIsReturned(provider)) - .BDDfy(); - } - - [Fact] - public void should_return_error_if_cannot_create_handler() - { - var authenticationOptions = new AuthenticationOptionsBuilder() - .Build(); - - this.Given(x => x.GivenTheAuthenticationOptionsAre(authenticationOptions)) - .And(x => x.GivenTheCreatorReturnsAnError()) - .When(x => x.WhenIGetFromTheFactory()) - .Then(x => x.ThenAnErrorResponseIsReturned()) - .BDDfy(); - } - - private void GivenTheAuthenticationOptionsAre(AuthenticationOptions authenticationOptions) - { - _authenticationOptions = authenticationOptions; - } - - private void GivenTheCreatorReturnsAnError() - { - _creator - .Setup(x => x.Create(It.IsAny(), It.IsAny())) - .Returns(new ErrorResponse(new List - { - new UnableToCreateAuthenticationHandlerError($"Unable to create authentication handler for xxx") - })); - } - - private void GivenTheCreatorReturns() - { - _creator - .Setup(x => x.Create(It.IsAny(), It.IsAny())) - .Returns(new OkResponse(x => Task.CompletedTask)); - } - - private void WhenIGetFromTheFactory() - { - _result = _authenticationHandlerFactory.Get(_app.Object, _authenticationOptions); - } - - private void ThenTheHandlerIsReturned(string expected) - { - _result.Data.Provider.ShouldBe(expected); - } - - private void ThenAnErrorResponseIsReturned() - { - _result.IsError.ShouldBeTrue(); - } - } -} diff --git a/test/Ocelot.UnitTests/Authentication/AuthenticationMiddlewareTests.cs b/test/Ocelot.UnitTests/Authentication/AuthenticationMiddlewareTests.cs index 819c48d7..e6bcc500 100644 --- a/test/Ocelot.UnitTests/Authentication/AuthenticationMiddlewareTests.cs +++ b/test/Ocelot.UnitTests/Authentication/AuthenticationMiddlewareTests.cs @@ -2,11 +2,9 @@ { using System.Collections.Generic; using Microsoft.AspNetCore.Builder; - using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using Moq; - using Ocelot.Authentication.Handler.Factory; using Ocelot.Authentication.Middleware; using Ocelot.Configuration.Builder; using Ocelot.DownstreamRouteFinder; @@ -19,13 +17,10 @@ public class AuthenticationMiddlewareTests : ServerHostedMiddlewareTest { - private readonly Mock _authFactory; private OkResponse _downstreamRoute; public AuthenticationMiddlewareTests() { - _authFactory = new Mock(); - GivenTheTestServerIsConfigured(); } @@ -45,7 +40,6 @@ { services.AddSingleton(); services.AddLogging(); - services.AddSingleton(_authFactory.Object); services.AddSingleton(ScopedRepository.Object); }