Added first authentiction code..we have a test that makes sure we are unauthenticed but i havent been able to get authenticated to work yet due to identity server usual madness when calling with their SDK!

This commit is contained in:
TomPallister
2016-10-15 11:17:18 +01:00
parent 67af8841b2
commit 34bac7e0d4
50 changed files with 545 additions and 144 deletions

View File

@ -1,48 +0,0 @@
namespace Ocelot.Library.Middleware
{
using System.Threading.Tasks;
using Infrastructure.Configuration;
using Infrastructure.DownstreamRouteFinder;
using Infrastructure.Repository;
using Microsoft.AspNetCore.Http;
public class AuthenticationMiddleware : OcelotMiddleware
{
private readonly RequestDelegate _next;
private readonly IScopedRequestDataRepository _scopedRequestDataRepository;
public AuthenticationMiddleware(RequestDelegate next,
IScopedRequestDataRepository scopedRequestDataRepository)
: base(scopedRequestDataRepository)
{
_next = next;
_scopedRequestDataRepository = scopedRequestDataRepository;
}
public async Task Invoke(HttpContext context)
{
var downstreamRoute = _scopedRequestDataRepository.Get<DownstreamRoute>("DownstreamRoute");
if (downstreamRoute.IsError)
{
SetPipelineError(downstreamRoute.Errors);
return;
}
if (IsAuthenticatedRoute(downstreamRoute.Data.ReRoute))
{
//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(ReRoute reRoute)
{
return reRoute.IsAuthenticated;
}
}
}

View File

@ -1,12 +0,0 @@
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

@ -1,41 +0,0 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Ocelot.Library.Infrastructure.DownstreamRouteFinder;
using Ocelot.Library.Infrastructure.Repository;
namespace Ocelot.Library.Middleware
{
public class DownstreamRouteFinderMiddleware : OcelotMiddleware
{
private readonly RequestDelegate _next;
private readonly IDownstreamRouteFinder _downstreamRouteFinder;
private readonly IScopedRequestDataRepository _scopedRequestDataRepository;
public DownstreamRouteFinderMiddleware(RequestDelegate next,
IDownstreamRouteFinder downstreamRouteFinder,
IScopedRequestDataRepository scopedRequestDataRepository)
:base(scopedRequestDataRepository)
{
_next = next;
_downstreamRouteFinder = downstreamRouteFinder;
_scopedRequestDataRepository = scopedRequestDataRepository;
}
public async Task Invoke(HttpContext context)
{
var upstreamUrlPath = context.Request.Path.ToString();
var downstreamRoute = _downstreamRouteFinder.FindDownstreamRoute(upstreamUrlPath, context.Request.Method);
if (downstreamRoute.IsError)
{
SetPipelineError(downstreamRoute.Errors);
return;
}
_scopedRequestDataRepository.Add("DownstreamRoute", downstreamRoute.Data);
await _next.Invoke(context);
}
}
}

View File

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

View File

@ -1,48 +0,0 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Ocelot.Library.Infrastructure.DownstreamRouteFinder;
using Ocelot.Library.Infrastructure.Repository;
using Ocelot.Library.Infrastructure.UrlTemplateReplacer;
namespace Ocelot.Library.Middleware
{
public class DownstreamUrlCreatorMiddleware : OcelotMiddleware
{
private readonly RequestDelegate _next;
private readonly IDownstreamUrlTemplateVariableReplacer _urlReplacer;
private readonly IScopedRequestDataRepository _scopedRequestDataRepository;
public DownstreamUrlCreatorMiddleware(RequestDelegate next,
IDownstreamUrlTemplateVariableReplacer urlReplacer,
IScopedRequestDataRepository scopedRequestDataRepository)
:base(scopedRequestDataRepository)
{
_next = next;
_urlReplacer = urlReplacer;
_scopedRequestDataRepository = scopedRequestDataRepository;
}
public async Task Invoke(HttpContext context)
{
var downstreamRoute = _scopedRequestDataRepository.Get<DownstreamRoute>("DownstreamRoute");
if (downstreamRoute.IsError)
{
SetPipelineError(downstreamRoute.Errors);
return;
}
var downstreamUrl = _urlReplacer.ReplaceTemplateVariables(downstreamRoute.Data);
if (downstreamUrl.IsError)
{
SetPipelineError(downstreamUrl.Errors);
return;
}
_scopedRequestDataRepository.Add("DownstreamUrl", downstreamUrl.Data);
await _next.Invoke(context);
}
}
}

View File

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

View File

@ -1,49 +0,0 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Ocelot.Library.Infrastructure.Repository;
using Ocelot.Library.Infrastructure.RequestBuilder;
namespace Ocelot.Library.Middleware
{
public class HttpRequestBuilderMiddleware : OcelotMiddleware
{
private readonly RequestDelegate _next;
private readonly IScopedRequestDataRepository _scopedRequestDataRepository;
private readonly IRequestBuilder _requestBuilder;
public HttpRequestBuilderMiddleware(RequestDelegate next,
IScopedRequestDataRepository scopedRequestDataRepository,
IRequestBuilder requestBuilder)
:base(scopedRequestDataRepository)
{
_next = next;
_scopedRequestDataRepository = scopedRequestDataRepository;
_requestBuilder = requestBuilder;
}
public async Task Invoke(HttpContext context)
{
var downstreamUrl = _scopedRequestDataRepository.Get<string>("DownstreamUrl");
if (downstreamUrl.IsError)
{
SetPipelineError(downstreamUrl.Errors);
return;
}
var request = await _requestBuilder
.Build(context.Request.Method, downstreamUrl.Data, context.Request.Body,
context.Request.Headers, context.Request.Cookies, context.Request.QueryString.Value, context.Request.ContentType);
if (request.IsError)
{
SetPipelineError(request.Errors);
return;
}
_scopedRequestDataRepository.Add("Request", request.Data);
await _next.Invoke(context);
}
}
}

View File

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

View File

@ -1,46 +0,0 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Ocelot.Library.Infrastructure.Repository;
using Ocelot.Library.Infrastructure.RequestBuilder;
using Ocelot.Library.Infrastructure.Requester;
namespace Ocelot.Library.Middleware
{
public class HttpRequesterMiddleware : OcelotMiddleware
{
private readonly RequestDelegate _next;
private readonly IHttpRequester _requester;
private readonly IScopedRequestDataRepository _scopedRequestDataRepository;
public HttpRequesterMiddleware(RequestDelegate next,
IHttpRequester requester,
IScopedRequestDataRepository scopedRequestDataRepository)
:base(scopedRequestDataRepository)
{
_next = next;
_requester = requester;
_scopedRequestDataRepository = scopedRequestDataRepository;
}
public async Task Invoke(HttpContext context)
{
var request = _scopedRequestDataRepository.Get<Request>("Request");
if (request.IsError)
{
SetPipelineError(request.Errors);
return;
}
var response = await _requester.GetResponse(request.Data);
if (response.IsError)
{
SetPipelineError(response.Errors);
return;
}
_scopedRequestDataRepository.Add("Response", response.Data);
}
}
}

View File

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

View File

@ -1,45 +0,0 @@
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Ocelot.Library.Infrastructure.Repository;
using Ocelot.Library.Infrastructure.Responder;
namespace Ocelot.Library.Middleware
{
public class HttpResponderMiddleware : OcelotMiddleware
{
private readonly RequestDelegate _next;
private readonly IHttpResponder _responder;
private readonly IScopedRequestDataRepository _scopedRequestDataRepository;
public HttpResponderMiddleware(RequestDelegate next,
IHttpResponder responder,
IScopedRequestDataRepository scopedRequestDataRepository)
:base(scopedRequestDataRepository)
{
_next = next;
_responder = responder;
_scopedRequestDataRepository = scopedRequestDataRepository;
}
public async Task Invoke(HttpContext context)
{
await _next.Invoke(context);
if (PipelineError())
{
//todo obviously this needs to be better...prob look at response errors
// and make a decision i guess
var errors = GetPipelineErrors();
await _responder.CreateNotFoundResponse(context);
}
else
{
var response = _scopedRequestDataRepository.Get<HttpResponseMessage>("Response");
await _responder.CreateResponse(context, response.Data);
}
}
}
}

View File

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

View File

@ -1,34 +0,0 @@
using System.Collections.Generic;
using Ocelot.Library.Infrastructure.Repository;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Middleware
{
public abstract class OcelotMiddleware
{
private readonly IScopedRequestDataRepository _scopedRequestDataRepository;
protected OcelotMiddleware(IScopedRequestDataRepository scopedRequestDataRepository)
{
_scopedRequestDataRepository = scopedRequestDataRepository;
}
public void SetPipelineError(List<Error> errors)
{
_scopedRequestDataRepository.Add("OcelotMiddlewareError", true);
_scopedRequestDataRepository.Add("OcelotMiddlewareErrors", errors);
}
public bool PipelineError()
{
var response = _scopedRequestDataRepository.Get<bool>("OcelotMiddlewareError");
return response.Data;
}
public List<Error> GetPipelineErrors()
{
var response = _scopedRequestDataRepository.Get<List<Error>>("OcelotMiddlewareErrors");
return response.Data;
}
}
}