got identity server access token acceptance test working, created factory for choosing auth handlers, a creator for making the auth handlers, some general refactoring...next step is injecting the config for the auth handler creator in some way or just passing it in

This commit is contained in:
TomPallister
2016-10-15 13:50:43 +01:00
parent 34bac7e0d4
commit 320b442526
16 changed files with 369 additions and 95 deletions

View File

@@ -0,0 +1,16 @@
using Microsoft.AspNetCore.Http;
namespace Ocelot.Library.Infrastructure.Authentication
{
public class AuthenticationHandler
{
public AuthenticationHandler(string provider, RequestDelegate handler)
{
Provider = provider;
Handler = handler;
}
public string Provider { get; private set; }
public RequestDelegate Handler { get; private set; }
}
}

View File

@@ -0,0 +1,32 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Authentication
{
/// <summary>
/// Cannot unit test things in this class due to use of extension methods
/// </summary>
public class AuthenticationHandlerCreator : IAuthenticationHandlerCreator
{
public Response<RequestDelegate> CreateIdentityServerAuthenticationHandler(IApplicationBuilder app)
{
var builder = app.New();
builder.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
{
//todo sort these options out
Authority = "http://localhost:51888",
ScopeName = "api",
RequireHttpsMetadata = false
});
builder.UseMvc();
var authenticationNext = builder.Build();
return new OkResponse<RequestDelegate>(authenticationNext);
}
}
}

View File

@@ -0,0 +1,32 @@
using System.Collections.Generic;
using Microsoft.AspNetCore.Builder;
using Ocelot.Library.Infrastructure.Errors;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Authentication
{
public class AuthenticationProviderFactory : IAuthenticationProviderFactory
{
private readonly IAuthenticationHandlerCreator _creator;
public AuthenticationProviderFactory(IAuthenticationHandlerCreator creator)
{
_creator = creator;
}
public Response<AuthenticationHandler> Get(string provider, IApplicationBuilder app)
{
var handler = _creator.CreateIdentityServerAuthenticationHandler(app);
if (!handler.IsError)
{
return new OkResponse<AuthenticationHandler>(new AuthenticationHandler(provider, handler.Data));
}
return new ErrorResponse<AuthenticationHandler>(new List<Error>
{
new UnableToCreateAuthenticationHandlerError($"Unable to create authentication handler for {provider}")
});
}
}
}

View File

@@ -0,0 +1,11 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Authentication
{
public interface IAuthenticationHandlerCreator
{
Response<RequestDelegate> CreateIdentityServerAuthenticationHandler(IApplicationBuilder app);
}
}

View File

@@ -0,0 +1,10 @@
using Microsoft.AspNetCore.Builder;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Authentication
{
public interface IAuthenticationProviderFactory
{
Response<AuthenticationHandler> Get(string provider, IApplicationBuilder app);
}
}

View File

@@ -0,0 +1,12 @@
using Ocelot.Library.Infrastructure.Errors;
namespace Ocelot.Library.Infrastructure.Authentication
{
public class UnableToCreateAuthenticationHandlerError : Error
{
public UnableToCreateAuthenticationHandlerError(string message)
: base(message, OcelotErrorCode.UnableToCreateAuthenticationHandlerError)
{
}
}
}