mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 09:38:14 +08:00
removed thing that checks if route is authorised cos we dont need it
This commit is contained in:
@ -1,11 +0,0 @@
|
||||
namespace Ocelot.Library.Infrastructure.Authentication
|
||||
{
|
||||
using Responses;
|
||||
public class CouldNotFindConfigurationError : Error
|
||||
{
|
||||
public CouldNotFindConfigurationError(string message)
|
||||
: base(message)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
namespace Ocelot.Library.Infrastructure.Authentication
|
||||
{
|
||||
using DownstreamRouteFinder;
|
||||
using Responses;
|
||||
|
||||
public interface IRouteRequiresAuthentication
|
||||
{
|
||||
Response<bool> IsAuthenticated(DownstreamRoute downstreamRoute, string httpMethod);
|
||||
}
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
namespace Ocelot.Library.Infrastructure.Authentication
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Configuration;
|
||||
using DownstreamRouteFinder;
|
||||
using Responses;
|
||||
|
||||
public class RouteRequiresAuthentication : IRouteRequiresAuthentication
|
||||
{
|
||||
private readonly IOcelotConfiguration _configuration;
|
||||
|
||||
public RouteRequiresAuthentication(IOcelotConfiguration configuration)
|
||||
{
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
||||
public Response<bool> IsAuthenticated(DownstreamRoute downstreamRoute, string httpMethod)
|
||||
{
|
||||
var reRoute =
|
||||
_configuration.ReRoutes.FirstOrDefault(
|
||||
x =>
|
||||
x.DownstreamTemplate == downstreamRoute.DownstreamUrlTemplate &&
|
||||
string.Equals(x.UpstreamHttpMethod, httpMethod, StringComparison.CurrentCultureIgnoreCase));
|
||||
|
||||
if (reRoute == null)
|
||||
{
|
||||
return new ErrorResponse<bool>(new List<Error> {new CouldNotFindConfigurationError($"Could not find configuration for {downstreamRoute.DownstreamUrlTemplate} using method {httpMethod}")});
|
||||
}
|
||||
|
||||
return new OkResponse<bool>(reRoute.IsAuthenticated);
|
||||
}
|
||||
}
|
||||
}
|
47
src/Ocelot.Library/Infrastructure/Builder/ReRouteBuilder.cs
Normal file
47
src/Ocelot.Library/Infrastructure/Builder/ReRouteBuilder.cs
Normal file
@ -0,0 +1,47 @@
|
||||
namespace Ocelot.Library.Infrastructure.Builder
|
||||
{
|
||||
using Configuration;
|
||||
|
||||
public class ReRouteBuilder
|
||||
{
|
||||
private string _downstreamTemplate;
|
||||
private string _upstreamTemplate;
|
||||
private string _upstreamTemplatePattern;
|
||||
private string _upstreamHttpMethod;
|
||||
private bool _isAuthenticated;
|
||||
private string _authenticationProvider;
|
||||
|
||||
public void WithDownstreamTemplate(string input)
|
||||
{
|
||||
_downstreamTemplate = input;
|
||||
}
|
||||
|
||||
public void WithUpstreamTemplate(string input)
|
||||
{
|
||||
_upstreamTemplate = input;
|
||||
}
|
||||
|
||||
public void WithUpstreamTemplatePattern(string input)
|
||||
{
|
||||
_upstreamTemplatePattern = input;
|
||||
}
|
||||
public void WithUpstreamHttpMethod(string input)
|
||||
{
|
||||
_upstreamHttpMethod = input;
|
||||
}
|
||||
public void WithIsAuthenticated(bool input)
|
||||
{
|
||||
_isAuthenticated = input;
|
||||
|
||||
}
|
||||
public void WithAuthenticationProvider(string input)
|
||||
{
|
||||
_authenticationProvider = input;
|
||||
}
|
||||
|
||||
public ReRoute Build()
|
||||
{
|
||||
return new ReRoute(_downstreamTemplate, _upstreamTemplate, _upstreamHttpMethod, _upstreamTemplatePattern, _isAuthenticated, _authenticationProvider);
|
||||
}
|
||||
}
|
||||
}
|
@ -45,7 +45,7 @@ namespace Ocelot.Library.Infrastructure.Configuration
|
||||
|
||||
var isAuthenticated = !string.IsNullOrEmpty(reRoute.Authentication);
|
||||
|
||||
_reRoutes.Add(new ReRoute(reRoute.DownstreamTemplate, reRoute.UpstreamTemplate, reRoute.UpstreamHttpMethod, upstreamTemplate, isAuthenticated));
|
||||
_reRoutes.Add(new ReRoute(reRoute.DownstreamTemplate, reRoute.UpstreamTemplate, reRoute.UpstreamHttpMethod, upstreamTemplate, isAuthenticated, reRoute.Authentication));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2,13 +2,14 @@
|
||||
{
|
||||
public class ReRoute
|
||||
{
|
||||
public ReRoute(string downstreamTemplate, string upstreamTemplate, string upstreamHttpMethod, string upstreamTemplatePattern, bool isAuthenticated)
|
||||
public ReRoute(string downstreamTemplate, string upstreamTemplate, string upstreamHttpMethod, string upstreamTemplatePattern, bool isAuthenticated, string authenticationProvider)
|
||||
{
|
||||
DownstreamTemplate = downstreamTemplate;
|
||||
UpstreamTemplate = upstreamTemplate;
|
||||
UpstreamHttpMethod = upstreamHttpMethod;
|
||||
UpstreamTemplatePattern = upstreamTemplatePattern;
|
||||
IsAuthenticated = isAuthenticated;
|
||||
AuthenticationProvider = authenticationProvider;
|
||||
}
|
||||
|
||||
public string DownstreamTemplate { get; private set; }
|
||||
@ -16,5 +17,6 @@
|
||||
public string UpstreamTemplatePattern { get; private set; }
|
||||
public string UpstreamHttpMethod { get; private set; }
|
||||
public bool IsAuthenticated { get; private set; }
|
||||
public string AuthenticationProvider { get; private set; }
|
||||
}
|
||||
}
|
@ -3,14 +3,16 @@ using Ocelot.Library.Infrastructure.UrlMatcher;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.DownstreamRouteFinder
|
||||
{
|
||||
using Configuration;
|
||||
|
||||
public class DownstreamRoute
|
||||
{
|
||||
public DownstreamRoute(List<TemplateVariableNameAndValue> templateVariableNameAndValues, string downstreamUrlTemplate)
|
||||
public DownstreamRoute(List<TemplateVariableNameAndValue> templateVariableNameAndValues, ReRoute reRoute)
|
||||
{
|
||||
TemplateVariableNameAndValues = templateVariableNameAndValues;
|
||||
DownstreamUrlTemplate = downstreamUrlTemplate;
|
||||
ReRoute = reRoute;
|
||||
}
|
||||
public List<TemplateVariableNameAndValue> TemplateVariableNameAndValues { get; private set; }
|
||||
public string DownstreamUrlTemplate { get; private set; }
|
||||
public ReRoute ReRoute { get; private set; }
|
||||
}
|
||||
}
|
@ -32,7 +32,7 @@ namespace Ocelot.Library.Infrastructure.DownstreamRouteFinder
|
||||
var templateVariableNameAndValues = _templateVariableNameAndValueFinder.Find(upstreamUrlPath,
|
||||
template.UpstreamTemplate);
|
||||
|
||||
return new OkResponse<DownstreamRoute>(new DownstreamRoute(templateVariableNameAndValues.Data, template.DownstreamTemplate));
|
||||
return new OkResponse<DownstreamRoute>(new DownstreamRoute(templateVariableNameAndValues.Data, template));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -10,7 +10,7 @@ namespace Ocelot.Library.Infrastructure.UrlTemplateReplacer
|
||||
{
|
||||
var upstreamUrl = new StringBuilder();
|
||||
|
||||
upstreamUrl.Append(downstreamRoute.DownstreamUrlTemplate);
|
||||
upstreamUrl.Append(downstreamRoute.ReRoute.DownstreamTemplate);
|
||||
|
||||
foreach (var templateVarAndValue in downstreamRoute.TemplateVariableNameAndValues)
|
||||
{
|
||||
|
@ -1,41 +1,35 @@
|
||||
namespace Ocelot.Library.Middleware
|
||||
{
|
||||
using System.Threading.Tasks;
|
||||
using Infrastructure.Authentication;
|
||||
using Infrastructure.Configuration;
|
||||
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)
|
||||
IScopedRequestDataRepository scopedRequestDataRepository)
|
||||
: 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)
|
||||
if (downstreamRoute.IsError)
|
||||
{
|
||||
SetPipelineError(downstreamRoute.Errors);
|
||||
return;
|
||||
}
|
||||
|
||||
if (IsAuthenticatedRoute(isAuthenticated))
|
||||
if (IsAuthenticatedRoute(downstreamRoute.Data.ReRoute))
|
||||
{
|
||||
//todo - build auth pipeline and then call normal pipeline if all good?
|
||||
await _next.Invoke(context);
|
||||
@ -46,9 +40,9 @@
|
||||
}
|
||||
}
|
||||
|
||||
private static bool IsAuthenticatedRoute(Response<bool> isAuthenticated)
|
||||
private static bool IsAuthenticatedRoute(ReRoute reRoute)
|
||||
{
|
||||
return isAuthenticated.Data;
|
||||
return reRoute.IsAuthenticated;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -20,8 +20,6 @@ using Ocelot.Library.Middleware;
|
||||
|
||||
namespace Ocelot
|
||||
{
|
||||
using Library.Infrastructure.Authentication;
|
||||
|
||||
public class Startup
|
||||
{
|
||||
public Startup(IHostingEnvironment env)
|
||||
@ -54,7 +52,6 @@ namespace Ocelot
|
||||
services.AddSingleton<IHttpRequester, HttpClientHttpRequester>();
|
||||
services.AddSingleton<IHttpResponder, HttpContextResponder>();
|
||||
services.AddSingleton<IRequestBuilder, HttpRequestBuilder>();
|
||||
services.AddSingleton<IRouteRequiresAuthentication, RouteRequiresAuthentication>();
|
||||
|
||||
// see this for why we register this as singleton http://stackoverflow.com/questions/37371264/invalidoperationexception-unable-to-resolve-service-for-type-microsoft-aspnetc
|
||||
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
||||
|
Reference in New Issue
Block a user