Skip to content

Commit

Permalink
Fix errors and messages
Browse files Browse the repository at this point in the history
  • Loading branch information
raman-m committed Aug 24, 2023
1 parent b389e86 commit dc6076f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
13 changes: 9 additions & 4 deletions src/Ocelot/Authorization/ScopesAuthorizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ namespace Ocelot.Authorization
public class ScopesAuthorizer : IScopesAuthorizer
{
private const string ScopeClaimKey = "scope";
private readonly IClaimsParser _claimsParser;
private readonly IClaimsParser _claimsParser;

public ScopesAuthorizer(IClaimsParser claimsParser)
{
_claimsParser = claimsParser;
}

public Response<bool> Authorize(ClaimsPrincipal claimsPrincipal, List<string> routeAllowedScopes)
{
Expand All @@ -33,16 +38,16 @@ public Response<bool> Authorize(ClaimsPrincipal claimsPrincipal, List<string> ro
{
var scope = userScopes[0];

if (scope.Contains(" "))
if (scope.Contains(' '))
{
userScopes = scope.Split(" ", StringSplitOptions.RemoveEmptyEntries);
userScopes = scope.Split(' ', StringSplitOptions.RemoveEmptyEntries);
}
}

if (routeAllowedScopes.Except(userScopes).Any())
{
return new ErrorResponse<bool>(
new ScopeNotAuthorizedError($"User scopes: '{string.Join(",", userScopes)}' do not have all allowed route scopes: '{string.Join(",", routeAllowedScopes)}'"));
new ScopeNotAuthorizedError($"User scopes: '{string.Join(',', userScopes)}' do not have all allowed route scopes: '{string.Join(',', routeAllowedScopes)}'"));
}

return new OkResponse<bool>(true);
Expand Down
10 changes: 5 additions & 5 deletions test/Ocelot.UnitTests/Infrastructure/ScopesAuthorizerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public ScopesAuthorizerTests()
}

[Fact]
public void should_return_ok_if_no_allowed_scopes()
public void Should_return_ok_if_no_allowed_scopes()
{
this.Given(_ => GivenTheFollowing(new ClaimsPrincipal()))
.And(_ => GivenTheFollowing(new List<string>()))
Expand All @@ -41,7 +41,7 @@ public void should_return_ok_if_no_allowed_scopes()
}

[Fact]
public void should_return_ok_if_null_allowed_scopes()
public void Should_return_ok_if_null_allowed_scopes()
{
this.Given(_ => GivenTheFollowing(new ClaimsPrincipal()))
.And(_ => GivenTheFollowing((List<string>)null))
Expand All @@ -51,7 +51,7 @@ public void should_return_ok_if_null_allowed_scopes()
}

[Fact]
public void should_return_error_if_claims_parser_returns_error()
public void Should_return_error_if_claims_parser_returns_error()
{
var fakeError = new FakeError();
this.Given(_ => GivenTheFollowing(new ClaimsPrincipal()))
Expand All @@ -63,7 +63,7 @@ public void should_return_error_if_claims_parser_returns_error()
}

[Fact]
public void should_match_scopes_and_return_ok_result()
public void Should_match_scopes_and_return_ok_result()
{
var claimsPrincipal = new ClaimsPrincipal();
var allowedScopes = new List<string> { "someScope" };
Expand All @@ -77,7 +77,7 @@ public void should_match_scopes_and_return_ok_result()
}

[Fact]
public void should_not_match_scopes_and_return_error_result()
public void Should_not_match_scopes_and_return_error_result()
{
var fakeError = new FakeError();
var claimsPrincipal = new ClaimsPrincipal();
Expand Down

0 comments on commit dc6076f

Please sign in to comment.