Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#913 #1066 ScopesAuthorizer refactoring #1478

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from
18 changes: 13 additions & 5 deletions src/Ocelot/Authorization/ScopesAuthorizer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Ocelot.Infrastructure.Claims.Parser;
using Ocelot.Infrastructure.Claims.Parser;
using Ocelot.Responses;
using System.Security.Claims;

@@ -28,14 +28,22 @@ public Response<bool> Authorize(ClaimsPrincipal claimsPrincipal, List<string> ro
return new ErrorResponse<bool>(values.Errors);
}

var userScopes = values.Data;
IList<string> userScopes = values.Data;

var matchesScopes = routeAllowedScopes.Intersect(userScopes);
if (userScopes.Count == 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The if-block ought to be relocated to IClaimsParser to preserve the existing logic intact. Consequently, you must inject your specialized IClaimsParser service to generate the precise list of claims.

{
var scope = userScopes[0];

if (scope.Contains(' '))
{
userScopes = scope.Split(' ', StringSplitOptions.RemoveEmptyEntries);
}
Comment on lines +37 to +40
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's impossible to predict the body (serialized data) of the token from an unknown Auth-provider.
May I ask which Auth provider you utilize in your project?

}

if (!matchesScopes.Any())
if (routeAllowedScopes.Except(userScopes).Any())
Copy link
Member

@raman-m raman-m Nov 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic inversion is feasible, yet it appears redundant. It seems to be a minor refactoring aimed at reducing the number of lines in the code.
Finally, it is useless change!

Additionally, the valuable suggestion from the previous code review was overlooked. This recommendation is more logical than the favored Except helper.

{
return new ErrorResponse<bool>(
new ScopeNotAuthorizedError($"no one user scope: '{string.Join(',', userScopes)}' match with some allowed scope: '{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);
2 changes: 1 addition & 1 deletion test/Ocelot.AcceptanceTests/AuthorizationTests.cs
Original file line number Diff line number Diff line change
@@ -177,7 +177,7 @@ public void should_return_response_200_using_identity_server_with_allowed_scope(
AuthenticationOptions = new FileAuthenticationOptions
{
AuthenticationProviderKey = "Test",
AllowedScopes = new List<string>{ "api", "api.readOnly", "openid", "offline_access" },
AllowedScopes = new List<string>{ "api", "api.readOnly" },
},
},
},
2 changes: 1 addition & 1 deletion test/Ocelot.AcceptanceTests/ClaimsToDownstreamPathTests.cs
Original file line number Diff line number Diff line change
@@ -68,7 +68,7 @@ public void should_return_200_and_change_downstream_path()
AuthenticationProviderKey = "Test",
AllowedScopes = new List<string>
{
"openid", "offline_access", "api",
"api",
},
},
ChangeDownstreamPathTemplate =
Original file line number Diff line number Diff line change
@@ -76,7 +76,7 @@ public void should_return_response_200_and_foward_claim_as_header()
AuthenticationProviderKey = "Test",
AllowedScopes = new List<string>
{
"openid", "offline_access", "api",
"api",
},
},
AddHeadersToRequest =
Original file line number Diff line number Diff line change
@@ -74,7 +74,7 @@ public void should_return_response_200_and_foward_claim_as_query_string()
AuthenticationProviderKey = "Test",
AllowedScopes = new List<string>
{
"openid", "offline_access", "api",
"api",
},
},
AddQueriesToRequest =
@@ -140,7 +140,7 @@ public void should_return_response_200_and_foward_claim_as_query_string_and_pres
AuthenticationProviderKey = "Test",
AllowedScopes = new List<string>
{
"openid", "offline_access", "api",
"api",
},
},
AddQueriesToRequest =