mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 17:08:15 +08:00
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:
@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using Ocelot.Library.Infrastructure.Errors;
|
||||
using Ocelot.Library.Infrastructure.Responses;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.Configuration.Yaml
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
14
src/Ocelot.Library/Infrastructure/Errors/Error.cs
Normal file
14
src/Ocelot.Library/Infrastructure/Errors/Error.cs
Normal 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; }
|
||||
}
|
||||
}
|
13
src/Ocelot.Library/Infrastructure/Errors/OcelotErrorCode.cs
Normal file
13
src/Ocelot.Library/Infrastructure/Errors/OcelotErrorCode.cs
Normal file
@ -0,0 +1,13 @@
|
||||
namespace Ocelot.Library.Infrastructure.Errors
|
||||
{
|
||||
public enum OcelotErrorCode
|
||||
{
|
||||
UnauthenticatedError,
|
||||
UnknownError,
|
||||
DownstreamTemplateAlreadyUsedError,
|
||||
UnableToFindDownstreamRouteError,
|
||||
CannotAddDataError,
|
||||
CannotFindDataError,
|
||||
UnableToCompleteRequestError
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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
|
||||
{
|
@ -1,6 +1,6 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
|
||||
namespace Ocelot.Library.Middleware
|
||||
namespace Ocelot.Library.Infrastructure.Middleware
|
||||
{
|
||||
public static class DownstreamRouteFinderMiddlewareExtensions
|
||||
{
|
@ -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
|
||||
{
|
@ -1,6 +1,6 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
|
||||
namespace Ocelot.Library.Middleware
|
||||
namespace Ocelot.Library.Infrastructure.Middleware
|
||||
{
|
||||
public static class DownstreamUrlCreatorMiddlewareExtensions
|
||||
{
|
@ -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
|
||||
{
|
@ -1,6 +1,6 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
|
||||
namespace Ocelot.Library.Middleware
|
||||
namespace Ocelot.Library.Infrastructure.Middleware
|
||||
{
|
||||
public static class HttpRequestBuilderMiddlewareExtensions
|
||||
{
|
@ -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
|
||||
{
|
@ -1,6 +1,6 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
|
||||
namespace Ocelot.Library.Middleware
|
||||
namespace Ocelot.Library.Infrastructure.Middleware
|
||||
{
|
||||
public static class HttpRequesterMiddlewareExtensions
|
||||
{
|
@ -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
|
||||
{
|
@ -1,6 +1,6 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
|
||||
namespace Ocelot.Library.Middleware
|
||||
namespace Ocelot.Library.Infrastructure.Middleware
|
||||
{
|
||||
public static class HttpResponderMiddlewareExtensions
|
||||
{
|
@ -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
|
||||
{
|
@ -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)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -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)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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; }
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using Ocelot.Library.Infrastructure.Errors;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.Responses
|
||||
{
|
||||
|
@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using Ocelot.Library.Infrastructure.Errors;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.Responses
|
||||
{
|
||||
|
@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using Ocelot.Library.Infrastructure.Errors;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.Responses
|
||||
{
|
||||
|
@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using Ocelot.Library.Infrastructure.Errors;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.Responses
|
||||
{
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user