mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 06:22:50 +08:00
all tests passing but i think im going to take registering auth our of the config
This commit is contained in:
parent
6fcede701a
commit
967f0f7128
@ -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.Authentication;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Ocelot.Authentication.Handler.Factory;
|
|
||||||
using Ocelot.Configuration;
|
using Ocelot.Configuration;
|
||||||
using Ocelot.Errors;
|
using Ocelot.Errors;
|
||||||
using Ocelot.Infrastructure.Extensions;
|
using Ocelot.Infrastructure.Extensions;
|
||||||
@ -18,18 +17,15 @@ namespace Ocelot.Authentication.Middleware
|
|||||||
private readonly RequestDelegate _next;
|
private readonly RequestDelegate _next;
|
||||||
private readonly IApplicationBuilder _app;
|
private readonly IApplicationBuilder _app;
|
||||||
private readonly IAuthenticationSchemeProvider _authSchemeProvider;
|
private readonly IAuthenticationSchemeProvider _authSchemeProvider;
|
||||||
private readonly IAuthenticationHandlerFactory _authHandlerFactory;
|
|
||||||
private readonly IOcelotLogger _logger;
|
private readonly IOcelotLogger _logger;
|
||||||
|
|
||||||
public AuthenticationMiddleware(RequestDelegate next,
|
public AuthenticationMiddleware(RequestDelegate next,
|
||||||
IApplicationBuilder app,
|
IApplicationBuilder app,
|
||||||
IRequestScopedDataRepository requestScopedDataRepository,
|
IRequestScopedDataRepository requestScopedDataRepository,
|
||||||
IAuthenticationHandlerFactory authHandlerFactory,
|
|
||||||
IOcelotLoggerFactory loggerFactory)
|
IOcelotLoggerFactory loggerFactory)
|
||||||
: base(requestScopedDataRepository)
|
: base(requestScopedDataRepository)
|
||||||
{
|
{
|
||||||
_next = next;
|
_next = next;
|
||||||
_authHandlerFactory = authHandlerFactory;
|
|
||||||
_app = app;
|
_app = app;
|
||||||
_logger = loggerFactory.CreateLogger<AuthenticationMiddleware>();
|
_logger = loggerFactory.CreateLogger<AuthenticationMiddleware>();
|
||||||
}
|
}
|
||||||
|
@ -4,8 +4,6 @@ using Microsoft.AspNetCore.Http;
|
|||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||||
using Ocelot.Authentication.Handler.Creator;
|
|
||||||
using Ocelot.Authentication.Handler.Factory;
|
|
||||||
using Ocelot.Authorisation;
|
using Ocelot.Authorisation;
|
||||||
using Ocelot.Cache;
|
using Ocelot.Cache;
|
||||||
using Ocelot.Claims;
|
using Ocelot.Claims;
|
||||||
@ -129,8 +127,6 @@ namespace Ocelot.DependencyInjection
|
|||||||
services.TryAddSingleton<IHttpResponder, HttpContextResponder>();
|
services.TryAddSingleton<IHttpResponder, HttpContextResponder>();
|
||||||
services.TryAddSingleton<IRequestCreator, HttpRequestCreator>();
|
services.TryAddSingleton<IRequestCreator, HttpRequestCreator>();
|
||||||
services.TryAddSingleton<IErrorsToHttpStatusCodeMapper, ErrorsToHttpStatusCodeMapper>();
|
services.TryAddSingleton<IErrorsToHttpStatusCodeMapper, ErrorsToHttpStatusCodeMapper>();
|
||||||
services.TryAddSingleton<IAuthenticationHandlerFactory, AuthenticationHandlerFactory>();
|
|
||||||
services.TryAddSingleton<IAuthenticationHandlerCreator, AuthenticationHandlerCreator>();
|
|
||||||
services.TryAddSingleton<IRateLimitCounterHandler, MemoryCacheRateLimitCounterHandler>();
|
services.TryAddSingleton<IRateLimitCounterHandler, MemoryCacheRateLimitCounterHandler>();
|
||||||
services.TryAddSingleton<IHttpClientCache, MemoryHttpClientCache>();
|
services.TryAddSingleton<IHttpClientCache, MemoryHttpClientCache>();
|
||||||
services.TryAddSingleton<IRequestMapper, RequestMapper>();
|
services.TryAddSingleton<IRequestMapper, RequestMapper>();
|
||||||
@ -162,7 +158,7 @@ namespace Ocelot.DependencyInjection
|
|||||||
{
|
{
|
||||||
if(authOptions.Provider.ToLower() == "identityserver")
|
if(authOptions.Provider.ToLower() == "identityserver")
|
||||||
{
|
{
|
||||||
Action<IdentityServerAuthenticationOptions> options = o =>
|
Action<IdentityServerAuthenticationOptions> options = o =>
|
||||||
{
|
{
|
||||||
o.Authority = authOptions.IdentityServerConfig.ProviderRootUrl;
|
o.Authority = authOptions.IdentityServerConfig.ProviderRootUrl;
|
||||||
o.ApiName = authOptions.IdentityServerConfig.ApiName;
|
o.ApiName = authOptions.IdentityServerConfig.ApiName;
|
||||||
@ -176,7 +172,12 @@ namespace Ocelot.DependencyInjection
|
|||||||
}
|
}
|
||||||
else if (authOptions.Provider.ToLower() == "jwt")
|
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;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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<IApplicationBuilder> _app;
|
|
||||||
private readonly Mock<IAuthenticationHandlerCreator> _creator;
|
|
||||||
private AuthenticationOptions _authenticationOptions;
|
|
||||||
private Response<AuthenticationHandler> _result;
|
|
||||||
|
|
||||||
public AuthenticationHandlerFactoryTests()
|
|
||||||
{
|
|
||||||
_app = new Mock<IApplicationBuilder>();
|
|
||||||
_creator = new Mock<IAuthenticationHandlerCreator>();
|
|
||||||
_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<IApplicationBuilder>(), It.IsAny<AuthenticationOptions>()))
|
|
||||||
.Returns(new ErrorResponse<RequestDelegate>(new List<Error>
|
|
||||||
{
|
|
||||||
new UnableToCreateAuthenticationHandlerError($"Unable to create authentication handler for xxx")
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GivenTheCreatorReturns()
|
|
||||||
{
|
|
||||||
_creator
|
|
||||||
.Setup(x => x.Create(It.IsAny<IApplicationBuilder>(), It.IsAny<AuthenticationOptions>()))
|
|
||||||
.Returns(new OkResponse<RequestDelegate>(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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -2,11 +2,9 @@
|
|||||||
{
|
{
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Moq;
|
using Moq;
|
||||||
using Ocelot.Authentication.Handler.Factory;
|
|
||||||
using Ocelot.Authentication.Middleware;
|
using Ocelot.Authentication.Middleware;
|
||||||
using Ocelot.Configuration.Builder;
|
using Ocelot.Configuration.Builder;
|
||||||
using Ocelot.DownstreamRouteFinder;
|
using Ocelot.DownstreamRouteFinder;
|
||||||
@ -19,13 +17,10 @@
|
|||||||
|
|
||||||
public class AuthenticationMiddlewareTests : ServerHostedMiddlewareTest
|
public class AuthenticationMiddlewareTests : ServerHostedMiddlewareTest
|
||||||
{
|
{
|
||||||
private readonly Mock<IAuthenticationHandlerFactory> _authFactory;
|
|
||||||
private OkResponse<DownstreamRoute> _downstreamRoute;
|
private OkResponse<DownstreamRoute> _downstreamRoute;
|
||||||
|
|
||||||
public AuthenticationMiddlewareTests()
|
public AuthenticationMiddlewareTests()
|
||||||
{
|
{
|
||||||
_authFactory = new Mock<IAuthenticationHandlerFactory>();
|
|
||||||
|
|
||||||
GivenTheTestServerIsConfigured();
|
GivenTheTestServerIsConfigured();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,7 +40,6 @@
|
|||||||
{
|
{
|
||||||
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||||
services.AddLogging();
|
services.AddLogging();
|
||||||
services.AddSingleton(_authFactory.Object);
|
|
||||||
services.AddSingleton(ScopedRepository.Object);
|
services.AddSingleton(ScopedRepository.Object);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user