-
Notifications
You must be signed in to change notification settings - Fork 9
/
RptRequirementHandler.cs
56 lines (53 loc) · 1.92 KB
/
RptRequirementHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System;
using System.Linq;
using System.Security.Claims;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
namespace API.Authorization.RPT
{
public class RptRequirementHandler : AuthorizationHandler<RptRequirement>
{
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, RptRequirement requirement)
{
var authorizationClaim = context.User.FindFirstValue("authorization");
if (String.IsNullOrWhiteSpace(authorizationClaim))
{
return Task.CompletedTask;
}
/* Sample value for authorizationClaim
{
"permissions":[
{
"scopes":["read"],
"rsid":"deb61104-5008-4001-8792-ac5734b1235b",
"rsname":"customers"
},
{
"scopes":["read","create","update","archive"],
"rsid":"cca3a1af-b8c5-478f-acee-01044961db50",
"rsname":"projects"
}
]
}
*/
var json = JsonDocument.Parse(authorizationClaim);
var permissions = json.RootElement.GetProperty("permissions");
foreach (var permission in permissions.EnumerateArray())
{
if (permission.GetProperty("rsname").GetString() != requirement.Resource)
{
continue;
}
if (permission.GetProperty("scopes")
.EnumerateArray()
.Any(scope => scope.GetString() == requirement.Scope))
{
context.Succeed(requirement);
return Task.CompletedTask;
}
}
return Task.CompletedTask;
}
}
}