-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
#1739 #2177 OpenIddict authentication: role section, override scope and role key #1431
Open
Eits-Ian
wants to merge
10
commits into
ThreeMammals:develop
Choose a base branch
from
Eits-Ian:OpenIddict
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
468bc97
OpenIddict implementation, role section, override scope and role key
Eits-Ian fb67ac0
Fix warnings
raman-m e4659f2
Fix CS0649: Field '_rolesAuthorizer' is never assigned to, and will a…
raman-m b285e98
Fix messages
raman-m 903d6bd
Fixed errors
Eits-Ian f1f8fe5
Fixed test
Eits-Ian 0079873
Refactor `AuthenticationOptions`
raman-m a3574e4
Fix compiling errors
raman-m 96c6191
Quick code review from @raman-m
raman-m 0cc2477
Fix unit tests
raman-m File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using Ocelot.Responses; | ||
using System.Security.Claims; | ||
|
||
namespace Ocelot.Authorization; | ||
|
||
public interface IRolesAuthorizer | ||
{ | ||
Response<bool> Authorize(ClaimsPrincipal claimsPrincipal, List<string> routeRequiredRole, string roleKey); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
using Ocelot.Responses; | ||
using System.Security.Claims; | ||
|
||
namespace Ocelot.Authorization | ||
namespace Ocelot.Authorization; | ||
|
||
public interface IScopesAuthorizer | ||
{ | ||
public interface IScopesAuthorizer | ||
{ | ||
Response<bool> Authorize(ClaimsPrincipal claimsPrincipal, List<string> routeAllowedScopes); | ||
} | ||
Response<bool> Authorize(ClaimsPrincipal claimsPrincipal, List<string> routeAllowedScopes, | ||
string scopeKey); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using Ocelot.Infrastructure.Claims.Parser; | ||
using Ocelot.Responses; | ||
using System.Security.Claims; | ||
|
||
namespace Ocelot.Authorization; | ||
|
||
public class RolesAuthorizer : IRolesAuthorizer | ||
{ | ||
private readonly IClaimsParser _claimsParser; | ||
|
||
public RolesAuthorizer(IClaimsParser claimsParser) | ||
{ | ||
_claimsParser = claimsParser; | ||
} | ||
|
||
public Response<bool> Authorize(ClaimsPrincipal claimsPrincipal, List<string> routeRequiredRole, string roleKey) | ||
{ | ||
if (routeRequiredRole == null || routeRequiredRole.Count == 0) | ||
{ | ||
return new OkResponse<bool>(true); | ||
} | ||
|
||
roleKey ??= "role"; | ||
|
||
var values = _claimsParser.GetValuesByClaimType(claimsPrincipal.Claims, roleKey); | ||
|
||
if (values.IsError) | ||
{ | ||
return new ErrorResponse<bool>(values.Errors); | ||
} | ||
|
||
var userRoles = values.Data; | ||
|
||
var matchedRoles = routeRequiredRole.Intersect(userRoles).ToList(); // Note this is an OR | ||
|
||
if (matchedRoles.Count == 0) | ||
{ | ||
return new ErrorResponse<bool>( | ||
new ScopeNotAuthorizedError($"no one user role: '{string.Join(",", userRoles)}' match with some allowed role: '{string.Join(",", routeRequiredRole)}'")); | ||
} | ||
|
||
return new OkResponse<bool>(true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 4 additions & 6 deletions
10
src/Ocelot/Configuration/Creator/AuthenticationOptionsCreator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,8 @@ | ||
using Ocelot.Configuration.File; | ||
|
||
namespace Ocelot.Configuration.Creator | ||
namespace Ocelot.Configuration.Creator; | ||
|
||
public class AuthenticationOptionsCreator : IAuthenticationOptionsCreator | ||
{ | ||
public class AuthenticationOptionsCreator : IAuthenticationOptionsCreator | ||
{ | ||
public AuthenticationOptions Create(FileRoute route) | ||
=> new(route?.AuthenticationOptions ?? new()); | ||
} | ||
public AuthenticationOptions Create(FileRoute route) => new(route?.AuthenticationOptions ?? new()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you copy whole
if
-block above?I strongly disapprove of any form of copying of blocks❗