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,4 +1,5 @@
using System.Collections.Generic;
using Ocelot.Library.Infrastructure.Errors;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Configuration.Yaml

View File

@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using Ocelot.Library.Infrastructure.Errors;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Configuration.Yaml

View File

@ -1,10 +1,11 @@
using Ocelot.Library.Infrastructure.Responses;
using Ocelot.Library.Infrastructure.Errors;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Configuration.Yaml
{
public class DownstreamTemplateAlreadyUsedError : Error
{
public DownstreamTemplateAlreadyUsedError(string message) : base(message)
public DownstreamTemplateAlreadyUsedError(string message) : base(message, OcelotErrorCode.DownstreamTemplateAlreadyUsedError)
{
}
}

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Options;
using Ocelot.Library.Infrastructure.Configuration;
using Ocelot.Library.Infrastructure.Errors;
using Ocelot.Library.Infrastructure.Responses;
using Ocelot.Library.Infrastructure.UrlMatcher;

View File

@ -2,13 +2,14 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Ocelot.Library.Infrastructure.Errors;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.DownstreamRouteFinder
{
public class UnableToFindDownstreamRouteError : Error
{
public UnableToFindDownstreamRouteError() : base("UnableToFindDownstreamRouteError")
public UnableToFindDownstreamRouteError() : base("UnableToFindDownstreamRouteError", OcelotErrorCode.UnableToFindDownstreamRouteError)
{
}
}

View File

@ -0,0 +1,14 @@
namespace Ocelot.Library.Infrastructure.Errors
{
public abstract class Error
{
protected Error(string message, OcelotErrorCode code)
{
Message = message;
Code = code;
}
public string Message { get; private set; }
public OcelotErrorCode Code { get; private set; }
}
}

View File

@ -0,0 +1,13 @@
namespace Ocelot.Library.Infrastructure.Errors
{
public enum OcelotErrorCode
{
UnauthenticatedError,
UnknownError,
DownstreamTemplateAlreadyUsedError,
UnableToFindDownstreamRouteError,
CannotAddDataError,
CannotFindDataError,
UnableToCompleteRequestError
}
}

View File

@ -0,0 +1,80 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Ocelot.Library.Infrastructure.Configuration;
using Ocelot.Library.Infrastructure.DownstreamRouteFinder;
using Ocelot.Library.Infrastructure.Errors;
using Ocelot.Library.Infrastructure.Repository;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Middleware
{
public class AuthenticationMiddleware : OcelotMiddleware
{
private readonly RequestDelegate _next;
private RequestDelegate _authenticationNext;
private readonly IScopedRequestDataRepository _scopedRequestDataRepository;
private readonly IApplicationBuilder _app;
public AuthenticationMiddleware(RequestDelegate next, IApplicationBuilder app,
IScopedRequestDataRepository scopedRequestDataRepository)
: base(scopedRequestDataRepository)
{
_next = next;
_scopedRequestDataRepository = scopedRequestDataRepository;
_app = app;
}
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?
//create new app builder
var builder = _app.New();
//set up any options for the authentication
var jwtBearerOptions = new JwtBearerOptions
{
AutomaticAuthenticate = true,
AutomaticChallenge = true,
RequireHttpsMetadata = false,
};
//set the authentication middleware
builder.UseJwtBearerAuthentication(jwtBearerOptions);
//use mvc so we hit the catch all authorised controller
builder.UseMvc();
//then build it
_authenticationNext = builder.Build();
//then call it
await _authenticationNext(context);
//check if the user is authenticated
if (context.User.Identity.IsAuthenticated)
{
await _next.Invoke(context);
}
else
{
SetPipelineError(new List<Error> {new UnauthenticatedError($"Request for authenticated route {context.Request.Path} by {context.User.Identity.Name} was unauthenticated")});
}
}
else
{
await _next.Invoke(context);
}
}
private static bool IsAuthenticatedRoute(ReRoute reRoute)
{
return reRoute.IsAuthenticated;
}
}
}

View File

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

View File

@ -3,7 +3,7 @@ using Microsoft.AspNetCore.Http;
using Ocelot.Library.Infrastructure.DownstreamRouteFinder;
using Ocelot.Library.Infrastructure.Repository;
namespace Ocelot.Library.Middleware
namespace Ocelot.Library.Infrastructure.Middleware
{
public class DownstreamRouteFinderMiddleware : OcelotMiddleware
{

View File

@ -1,6 +1,6 @@
using Microsoft.AspNetCore.Builder;
namespace Ocelot.Library.Middleware
namespace Ocelot.Library.Infrastructure.Middleware
{
public static class DownstreamRouteFinderMiddlewareExtensions
{

View File

@ -4,7 +4,7 @@ using Ocelot.Library.Infrastructure.DownstreamRouteFinder;
using Ocelot.Library.Infrastructure.Repository;
using Ocelot.Library.Infrastructure.UrlTemplateReplacer;
namespace Ocelot.Library.Middleware
namespace Ocelot.Library.Infrastructure.Middleware
{
public class DownstreamUrlCreatorMiddleware : OcelotMiddleware
{

View File

@ -1,6 +1,6 @@
using Microsoft.AspNetCore.Builder;
namespace Ocelot.Library.Middleware
namespace Ocelot.Library.Infrastructure.Middleware
{
public static class DownstreamUrlCreatorMiddlewareExtensions
{

View File

@ -3,7 +3,7 @@ using Microsoft.AspNetCore.Http;
using Ocelot.Library.Infrastructure.Repository;
using Ocelot.Library.Infrastructure.RequestBuilder;
namespace Ocelot.Library.Middleware
namespace Ocelot.Library.Infrastructure.Middleware
{
public class HttpRequestBuilderMiddleware : OcelotMiddleware
{

View File

@ -1,6 +1,6 @@
using Microsoft.AspNetCore.Builder;
namespace Ocelot.Library.Middleware
namespace Ocelot.Library.Infrastructure.Middleware
{
public static class HttpRequestBuilderMiddlewareExtensions
{

View File

@ -4,7 +4,7 @@ using Ocelot.Library.Infrastructure.Repository;
using Ocelot.Library.Infrastructure.RequestBuilder;
using Ocelot.Library.Infrastructure.Requester;
namespace Ocelot.Library.Middleware
namespace Ocelot.Library.Infrastructure.Middleware
{
public class HttpRequesterMiddleware : OcelotMiddleware
{

View File

@ -1,6 +1,6 @@
using Microsoft.AspNetCore.Builder;
namespace Ocelot.Library.Middleware
namespace Ocelot.Library.Infrastructure.Middleware
{
public static class HttpRequesterMiddlewareExtensions
{

View File

@ -4,22 +4,25 @@ using Microsoft.AspNetCore.Http;
using Ocelot.Library.Infrastructure.Repository;
using Ocelot.Library.Infrastructure.Responder;
namespace Ocelot.Library.Middleware
namespace Ocelot.Library.Infrastructure.Middleware
{
public class HttpResponderMiddleware : OcelotMiddleware
{
private readonly RequestDelegate _next;
private readonly IHttpResponder _responder;
private readonly IScopedRequestDataRepository _scopedRequestDataRepository;
private readonly IErrorsToHttpStatusCodeMapper _codeMapper;
public HttpResponderMiddleware(RequestDelegate next,
IHttpResponder responder,
IScopedRequestDataRepository scopedRequestDataRepository)
IScopedRequestDataRepository scopedRequestDataRepository,
IErrorsToHttpStatusCodeMapper codeMapper)
:base(scopedRequestDataRepository)
{
_next = next;
_responder = responder;
_scopedRequestDataRepository = scopedRequestDataRepository;
_codeMapper = codeMapper;
}
public async Task Invoke(HttpContext context)
@ -28,11 +31,19 @@ namespace Ocelot.Library.Middleware
if (PipelineError())
{
//todo obviously this needs to be better...prob look at response errors
// and make a decision i guess
var errors = GetPipelineErrors();
var statusCode = _codeMapper.Map(errors);
if (!statusCode.IsError)
{
await _responder.CreateErrorResponse(context, statusCode.Data);
}
else
{
await _responder.CreateErrorResponse(context, 500);
}
await _responder.CreateNotFoundResponse(context);
}
else
{

View File

@ -1,6 +1,6 @@
using Microsoft.AspNetCore.Builder;
namespace Ocelot.Library.Middleware
namespace Ocelot.Library.Infrastructure.Middleware
{
public static class HttpResponderMiddlewareExtensions
{

View File

@ -1,8 +1,9 @@
using System.Collections.Generic;
using Ocelot.Library.Infrastructure.Errors;
using Ocelot.Library.Infrastructure.Repository;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Middleware
namespace Ocelot.Library.Infrastructure.Middleware
{
public abstract class OcelotMiddleware
{

View File

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

View File

@ -1,10 +1,11 @@
using Ocelot.Library.Infrastructure.Responses;
using Ocelot.Library.Infrastructure.Errors;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Repository
{
public class CannotAddDataError : Error
{
public CannotAddDataError(string message) : base(message)
public CannotAddDataError(string message) : base(message, OcelotErrorCode.CannotAddDataError)
{
}
}

View File

@ -1,10 +1,11 @@
using Ocelot.Library.Infrastructure.Responses;
using Ocelot.Library.Infrastructure.Errors;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Repository
{
public class CannotFindDataError : Error
{
public CannotFindDataError(string message) : base(message)
public CannotFindDataError(string message) : base(message, OcelotErrorCode.CannotFindDataError)
{
}
}

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Http;
using Ocelot.Library.Infrastructure.Errors;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Repository

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Ocelot.Library.Infrastructure.Errors;
using Ocelot.Library.Infrastructure.RequestBuilder;
using Ocelot.Library.Infrastructure.Responses;

View File

@ -1,4 +1,5 @@
using System;
using Ocelot.Library.Infrastructure.Errors;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Requester
@ -6,7 +7,7 @@ namespace Ocelot.Library.Infrastructure.Requester
public class UnableToCompleteRequestError : Error
{
public UnableToCompleteRequestError(Exception exception)
: base($"Error making http request, exception: {exception.Message}")
: base($"Error making http request, exception: {exception.Message}", OcelotErrorCode.UnableToCompleteRequestError)
{
}
}

View File

@ -0,0 +1,20 @@
using System.Collections.Generic;
using System.Linq;
using Ocelot.Library.Infrastructure.Errors;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Responder
{
public class ErrorsToHttpStatusCodeMapper : IErrorsToHttpStatusCodeMapper
{
public Response<int> Map(List<Error> errors)
{
if (errors.Any(e => e.Code == OcelotErrorCode.UnauthenticatedError))
{
return new OkResponse<int>(401);
}
return new OkResponse<int>(404);
}
}
}

View File

@ -1,5 +1,4 @@
using System.Net;
using System.Net.Http;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
@ -23,11 +22,11 @@ namespace Ocelot.Library.Infrastructure.Responder
return context;
}
public async Task<HttpContext> CreateNotFoundResponse(HttpContext context)
public async Task<HttpContext> CreateErrorResponse(HttpContext context, int statusCode)
{
context.Response.OnStarting(x =>
{
context.Response.StatusCode = (int)HttpStatusCode.NotFound;
context.Response.StatusCode = statusCode;
return Task.CompletedTask;
}, context);
return context;

View File

@ -0,0 +1,11 @@
using System.Collections.Generic;
using Ocelot.Library.Infrastructure.Errors;
using Ocelot.Library.Infrastructure.Responses;
namespace Ocelot.Library.Infrastructure.Responder
{
public interface IErrorsToHttpStatusCodeMapper
{
Response<int> Map(List<Error> errors);
}
}

View File

@ -7,7 +7,6 @@ namespace Ocelot.Library.Infrastructure.Responder
public interface IHttpResponder
{
Task<HttpContext> CreateResponse(HttpContext context, HttpResponseMessage response);
Task<HttpContext> CreateNotFoundResponse(HttpContext context);
Task<HttpContext> CreateErrorResponse(HttpContext context, int statusCode);
}
}

View File

@ -1,12 +0,0 @@
namespace Ocelot.Library.Infrastructure.Responses
{
public abstract class Error
{
public Error(string message)
{
Message = message;
}
public string Message { get; private set; }
}
}

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using Ocelot.Library.Infrastructure.Errors;
namespace Ocelot.Library.Infrastructure.Responses
{

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using Ocelot.Library.Infrastructure.Errors;
namespace Ocelot.Library.Infrastructure.Responses
{

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using Ocelot.Library.Infrastructure.Errors;
namespace Ocelot.Library.Infrastructure.Responses
{

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using Ocelot.Library.Infrastructure.Errors;
namespace Ocelot.Library.Infrastructure.Responses
{

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;
}
}
}