started adding authentication stack, thing that decides if we should be authenticated is in

This commit is contained in:
tom.pallister
2016-10-12 13:40:46 +01:00
parent 1fddcf0836
commit 58393f07ec
15 changed files with 241 additions and 13 deletions

View File

@ -0,0 +1,54 @@
namespace Ocelot.Library.Middleware
{
using System.Threading.Tasks;
using Infrastructure.Authentication;
using Infrastructure.DownstreamRouteFinder;
using Infrastructure.Repository;
using Infrastructure.Responses;
using Microsoft.AspNetCore.Http;
public class AuthenticationMiddleware : OcelotMiddleware
{
private readonly RequestDelegate _next;
private readonly IScopedRequestDataRepository _scopedRequestDataRepository;
private readonly IRouteRequiresAuthentication _requiresAuthentication;
public AuthenticationMiddleware(RequestDelegate next,
IScopedRequestDataRepository scopedRequestDataRepository,
IRouteRequiresAuthentication requiresAuthentication)
: base(scopedRequestDataRepository)
{
_next = next;
_scopedRequestDataRepository = scopedRequestDataRepository;
_requiresAuthentication = requiresAuthentication;
}
public async Task Invoke(HttpContext context)
{
var downstreamRoute = _scopedRequestDataRepository.Get<DownstreamRoute>("DownstreamRoute");
var isAuthenticated = _requiresAuthentication.IsAuthenticated(downstreamRoute.Data, context.Request.Method);
if (isAuthenticated.IsError)
{
SetPipelineError(downstreamRoute.Errors);
return;
}
if (IsAuthenticatedRoute(isAuthenticated))
{
//todo - build auth pipeline and then call normal pipeline if all good?
await _next.Invoke(context);
}
else
{
await _next.Invoke(context);
}
}
private static bool IsAuthenticatedRoute(Response<bool> isAuthenticated)
{
return isAuthenticated.Data;
}
}
}

View File

@ -0,0 +1,12 @@
using Microsoft.AspNetCore.Builder;
namespace Ocelot.Library.Middleware
{
public static class AuthenticationMiddlewareMiddlewareExtensions
{
public static IApplicationBuilder UseAuthenticationMiddleware(this IApplicationBuilder builder)
{
return builder.UseMiddleware<AuthenticationMiddleware>();
}
}
}

View File

@ -2,7 +2,6 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Ocelot.Library.Infrastructure.DownstreamRouteFinder;
using Ocelot.Library.Infrastructure.Repository;
using Ocelot.Library.Infrastructure.Responder;
using Ocelot.Library.Infrastructure.UrlTemplateReplacer;
namespace Ocelot.Library.Middleware

View File

@ -4,7 +4,7 @@ namespace Ocelot.Library.Middleware
{
public static class DownstreamUrlCreatorMiddlewareExtensions
{
public static IApplicationBuilder UserDownstreamUrlCreatorMiddleware(this IApplicationBuilder builder)
public static IApplicationBuilder UseDownstreamUrlCreatorMiddleware(this IApplicationBuilder builder)
{
return builder.UseMiddleware<DownstreamUrlCreatorMiddleware>();
}