mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 21:08:17 +08:00
Can authorise routes based on claims, there is also a claims transformation middleware
This commit is contained in:
@ -4,7 +4,6 @@ using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using Moq;
|
||||
using Ocelot.Claims.Parser;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Errors;
|
||||
using Ocelot.HeaderBuilder;
|
||||
@ -15,11 +14,13 @@ using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.HeaderBuilder
|
||||
{
|
||||
using Ocelot.Infrastructure.Claims.Parser;
|
||||
|
||||
public class AddHeadersToRequestTests
|
||||
{
|
||||
private readonly AddHeadersToRequest _addHeadersToRequest;
|
||||
private readonly Mock<IClaimsParser> _parser;
|
||||
private List<ClaimToHeader> _configuration;
|
||||
private List<ClaimToThing> _configuration;
|
||||
private HttpContext _context;
|
||||
private Response _result;
|
||||
private Response<string> _claimValue;
|
||||
@ -42,9 +43,9 @@ namespace Ocelot.UnitTests.HeaderBuilder
|
||||
};
|
||||
|
||||
this.Given(
|
||||
x => x.GivenConfigurationHeaderExtractorProperties(new List<ClaimToHeader>
|
||||
x => x.GivenConfigurationHeaderExtractorProperties(new List<ClaimToThing>
|
||||
{
|
||||
new ClaimToHeader("header-key", "", "", 0)
|
||||
new ClaimToThing("header-key", "", "", 0)
|
||||
}))
|
||||
.Given(x => x.GivenHttpContext(context))
|
||||
.And(x => x.GivenTheClaimParserReturns(new OkResponse<string>("value")))
|
||||
@ -68,9 +69,9 @@ namespace Ocelot.UnitTests.HeaderBuilder
|
||||
context.Request.Headers.Add("header-key", new StringValues("initial"));
|
||||
|
||||
this.Given(
|
||||
x => x.GivenConfigurationHeaderExtractorProperties(new List<ClaimToHeader>
|
||||
x => x.GivenConfigurationHeaderExtractorProperties(new List<ClaimToThing>
|
||||
{
|
||||
new ClaimToHeader("header-key", "", "", 0)
|
||||
new ClaimToThing("header-key", "", "", 0)
|
||||
}))
|
||||
.Given(x => x.GivenHttpContext(context))
|
||||
.And(x => x.GivenTheClaimParserReturns(new OkResponse<string>("value")))
|
||||
@ -84,9 +85,9 @@ namespace Ocelot.UnitTests.HeaderBuilder
|
||||
public void should_return_error()
|
||||
{
|
||||
this.Given(
|
||||
x => x.GivenConfigurationHeaderExtractorProperties(new List<ClaimToHeader>
|
||||
x => x.GivenConfigurationHeaderExtractorProperties(new List<ClaimToThing>
|
||||
{
|
||||
new ClaimToHeader("", "", "", 0)
|
||||
new ClaimToThing("", "", "", 0)
|
||||
}))
|
||||
.Given(x => x.GivenHttpContext(new DefaultHttpContext()))
|
||||
.And(x => x.GivenTheClaimParserReturns(new ErrorResponse<string>(new List<Error>
|
||||
@ -104,7 +105,7 @@ namespace Ocelot.UnitTests.HeaderBuilder
|
||||
header.Value.First().ShouldBe(_claimValue.Data);
|
||||
}
|
||||
|
||||
private void GivenConfigurationHeaderExtractorProperties(List<ClaimToHeader> configuration)
|
||||
private void GivenConfigurationHeaderExtractorProperties(List<ClaimToThing> configuration)
|
||||
{
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
@ -1,123 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Claims;
|
||||
using Ocelot.Claims.Parser;
|
||||
using Ocelot.Errors;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.HeaderBuilder
|
||||
{
|
||||
public class ClaimParserTests
|
||||
{
|
||||
private readonly IClaimsParser _claimsParser;
|
||||
private readonly List<Claim> _claims;
|
||||
private string _key;
|
||||
private Response<string> _result;
|
||||
private string _delimiter;
|
||||
private int _index;
|
||||
|
||||
public ClaimParserTests()
|
||||
{
|
||||
_claims = new List<Claim>();
|
||||
_claimsParser = new ClaimsParser();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_parse_claims_dictionary_access_string_returning_value_to_function()
|
||||
{
|
||||
this.Given(x => x.GivenAClaimOf(new Claim("CustomerId", "1234")))
|
||||
.And(x => x.GivenTheKeyIs("CustomerId"))
|
||||
.When(x => x.WhenICallTheParser())
|
||||
.Then(x => x.ThenTheResultIs(new OkResponse<string>("1234")))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error_response_when_cannot_find_requested_claim()
|
||||
{
|
||||
this.Given(x => x.GivenAClaimOf(new Claim("BallsId", "1234")))
|
||||
.And(x => x.GivenTheKeyIs("CustomerId"))
|
||||
.When(x => x.WhenICallTheParser())
|
||||
.Then(x => x.ThenTheResultIs(new ErrorResponse<string>(new List<Error>
|
||||
{
|
||||
new CannotFindClaimError($"Cannot find claim for key: {_key}")
|
||||
})))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_parse_claims_dictionary_access_string_using_delimiter_and_retuning_at_correct_index()
|
||||
{
|
||||
this.Given(x => x.GivenAClaimOf(new Claim("Subject", "registered|4321")))
|
||||
.And(x => x.GivenTheDelimiterIs("|"))
|
||||
.And(x => x.GivenTheIndexIs(1))
|
||||
.And(x => x.GivenTheKeyIs("Subject"))
|
||||
.When(x => x.WhenICallTheParser())
|
||||
.Then(x => x.ThenTheResultIs(new OkResponse<string>("4321")))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error_response_if_index_too_large()
|
||||
{
|
||||
this.Given(x => x.GivenAClaimOf(new Claim("Subject", "registered|4321")))
|
||||
.And(x => x.GivenTheDelimiterIs("|"))
|
||||
.And(x => x.GivenTheIndexIs(24))
|
||||
.And(x => x.GivenTheKeyIs("Subject"))
|
||||
.When(x => x.WhenICallTheParser())
|
||||
.Then(x => x.ThenTheResultIs(new ErrorResponse<string>(new List<Error>
|
||||
{
|
||||
new CannotFindClaimError($"Cannot find claim for key: {_key}, delimiter: {_delimiter}, index: {_index}")
|
||||
})))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error_response_if_index_too_small()
|
||||
{
|
||||
this.Given(x => x.GivenAClaimOf(new Claim("Subject", "registered|4321")))
|
||||
.And(x => x.GivenTheDelimiterIs("|"))
|
||||
.And(x => x.GivenTheIndexIs(-1))
|
||||
.And(x => x.GivenTheKeyIs("Subject"))
|
||||
.When(x => x.WhenICallTheParser())
|
||||
.Then(x => x.ThenTheResultIs(new ErrorResponse<string>(new List<Error>
|
||||
{
|
||||
new CannotFindClaimError($"Cannot find claim for key: {_key}, delimiter: {_delimiter}, index: {_index}")
|
||||
})))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheIndexIs(int index)
|
||||
{
|
||||
_index = index;
|
||||
}
|
||||
|
||||
private void GivenTheDelimiterIs(string delimiter)
|
||||
{
|
||||
_delimiter = delimiter;
|
||||
}
|
||||
|
||||
private void GivenAClaimOf(Claim claim)
|
||||
{
|
||||
_claims.Add(claim);
|
||||
}
|
||||
|
||||
private void GivenTheKeyIs(string key)
|
||||
{
|
||||
_key = key;
|
||||
}
|
||||
|
||||
private void WhenICallTheParser()
|
||||
{
|
||||
_result = _claimsParser.GetValue(_claims, _key, _delimiter, _index);
|
||||
}
|
||||
|
||||
private void ThenTheResultIs(Response<string> expected)
|
||||
{
|
||||
_result.Data.ShouldBe(expected.Data);
|
||||
_result.IsError.ShouldBe(expected.IsError);
|
||||
}
|
||||
}
|
||||
}
|
@ -61,9 +61,9 @@ namespace Ocelot.UnitTests.HeaderBuilder
|
||||
var downstreamRoute = new DownstreamRoute(new List<TemplateVariableNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamTemplate("any old string")
|
||||
.WithConfigurationHeaderExtractorProperties(new List<ClaimToHeader>
|
||||
.WithClaimsToHeaders(new List<ClaimToThing>
|
||||
{
|
||||
new ClaimToHeader("UserId", "Subject", "", 0)
|
||||
new ClaimToThing("UserId", "Subject", "", 0)
|
||||
})
|
||||
.Build());
|
||||
|
||||
@ -77,7 +77,7 @@ namespace Ocelot.UnitTests.HeaderBuilder
|
||||
private void GivenTheAddHeadersToRequestReturns(string claimValue)
|
||||
{
|
||||
_addHeaders
|
||||
.Setup(x => x.SetHeadersOnContext(It.IsAny<List<ClaimToHeader>>(),
|
||||
.Setup(x => x.SetHeadersOnContext(It.IsAny<List<ClaimToThing>>(),
|
||||
It.IsAny<HttpContext>()))
|
||||
.Returns(new OkResponse());
|
||||
}
|
||||
@ -85,7 +85,7 @@ namespace Ocelot.UnitTests.HeaderBuilder
|
||||
private void ThenTheAddHeadersToRequestIsCalledCorrectly()
|
||||
{
|
||||
_addHeaders
|
||||
.Verify(x => x.SetHeadersOnContext(It.IsAny<List<ClaimToHeader>>(),
|
||||
.Verify(x => x.SetHeadersOnContext(It.IsAny<List<ClaimToThing>>(),
|
||||
It.IsAny<HttpContext>()), Times.Once);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user