mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-12-24 21:35:47 +08:00
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:
@@ -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; }
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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}")
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user