Skip to content

Commit 5132107

Browse files
author
Tiago Brenck
committed
Refactored scopes policy
1 parent 5a0e144 commit 5132107

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

4-WebApp-your-API/4-2-B2C/TodoListService/AuthorizationPolicies/OperationScopeHandler.cs renamed to 4-WebApp-your-API/4-2-B2C/TodoListService/AuthorizationPolicies/ScopesRequirement.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,34 @@
88

99
namespace TodoListService.AuthorizationPolicies
1010
{
11-
/// <summary>
12-
/// AuthorizationHandler that will check if the scope claim has the requirement value
13-
/// </summary>
14-
public class OperationScopeHandler : AuthorizationHandler<OperationAuthorizationRequirement>
11+
public class ScopesRequirement : AuthorizationHandler<ScopesRequirement>, IAuthorizationRequirement
1512
{
13+
string[] _scopes;
14+
15+
public ScopesRequirement(params string[] scopes)
16+
{
17+
_scopes = scopes;
18+
}
19+
20+
/// <summary>
21+
/// AuthorizationHandler that will check if the scope claim has the requirement value
22+
/// </summary>
1623
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context,
17-
OperationAuthorizationRequirement requirement)
24+
ScopesRequirement requirement)
1825
{
26+
// If there are no scopes, do not process
1927
if (!context.User.Claims.Any(x => x.Type == ClaimConstants.Scope)
20-
&& !context.User.Claims.Any(y => y.Type == ClaimConstants.Scp))
28+
&& !context.User.Claims.Any(y => y.Type == ClaimConstants.Scp))
2129
{
2230
return Task.CompletedTask;
2331
}
2432

2533
Claim scopeClaim = context?.User?.FindFirst(ClaimConstants.Scp);
2634

27-
if(scopeClaim == null)
35+
if (scopeClaim == null)
2836
scopeClaim = context?.User?.FindFirst(ClaimConstants.Scope);
2937

30-
if (scopeClaim != null && scopeClaim.Value.Split(' ').Contains(requirement.Name))
38+
if (scopeClaim != null && scopeClaim.Value.Split(' ').Intersect(requirement._scopes).Any())
3139
{
3240
context.Succeed(requirement);
3341
}

4-WebApp-your-API/4-2-B2C/TodoListService/Startup.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ public void ConfigureServices(IServiceCollection services)
4646
{
4747
// Create policy to check for the scope 'read'
4848
options.AddPolicy("ReadScope",
49-
policy => policy.Requirements.Add(new OperationAuthorizationRequirement { Name = "read" }));
49+
policy => policy.Requirements.Add(new ScopesRequirement("read")));
5050
});
5151

5252
// Registering an authorization handler that will check if the scope claim has the requirement specified by the policy
53-
services.AddSingleton<IAuthorizationHandler, OperationScopeHandler>();
53+
//services.AddSingleton<IAuthorizationHandler, ScopesRequirement>();
5454
}
5555

5656
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

0 commit comments

Comments
 (0)