-
Notifications
You must be signed in to change notification settings - Fork 349
Expand file tree
/
Copy pathJwtRoleClaimsTransformer.cs
More file actions
127 lines (108 loc) · 3.63 KB
/
Copy pathJwtRoleClaimsTransformer.cs
File metadata and controls
127 lines (108 loc) · 3.63 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Security.Claims;
using System.Text.Json;
namespace Azure.DataApiBuilder.Core.AuthenticationHelpers;
public static class JwtRoleClaimsTransformer
{
public static void NormalizeRoleClaims(
ClaimsPrincipal principal,
string sourceRoleClaimType,
string? separator)
{
foreach (ClaimsIdentity identity in principal.Identities)
{
if (!identity.IsAuthenticated)
{
continue;
}
List<Claim> sourceClaims = identity.Claims
.Where(c => c.Type.Equals(sourceRoleClaimType, StringComparison.Ordinal))
.ToList();
if (sourceClaims.Count == 0)
{
continue;
}
HashSet<string> normalizedValues = new(StringComparer.OrdinalIgnoreCase);
foreach (Claim claim in sourceClaims)
{
foreach (string expandedValue in ExpandClaimValues(claim.Value, separator))
{
if (!string.IsNullOrWhiteSpace(expandedValue))
{
normalizedValues.Add(expandedValue.Trim());
}
}
}
foreach (string normalizedValue in normalizedValues)
{
bool exactClaimAlreadyExists = identity.Claims.Any(c =>
c.Type.Equals(sourceRoleClaimType, StringComparison.Ordinal) &&
c.Value.Equals(normalizedValue, StringComparison.OrdinalIgnoreCase));
if (!exactClaimAlreadyExists)
{
identity.AddClaim(new Claim(sourceRoleClaimType, normalizedValue, ClaimValueTypes.String));
}
}
}
}
private static IEnumerable<string> ExpandClaimValues(string rawValue, string? separator)
{
if (string.IsNullOrWhiteSpace(rawValue))
{
yield break;
}
string trimmed = rawValue.Trim();
// 1. JSON array support
if (trimmed.StartsWith('[') && trimmed.EndsWith(']'))
{
List<string>? values;
try
{
values = JsonSerializer.Deserialize<List<string>>(trimmed);
}
catch (JsonException)
{
values = null;
}
if (values is not null)
{
foreach (string value in values)
{
if (!string.IsNullOrWhiteSpace(value))
{
yield return value.Trim();
}
}
yield break;
}
}
// 2. Configurable separated string support
if (!string.IsNullOrEmpty(separator))
{
string[] splitValues;
if (separator.Length == 1)
{
splitValues = trimmed.Split(
separator[0],
StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
}
else
{
splitValues = trimmed.Split(
new[] { separator },
StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
}
foreach (string value in splitValues)
{
if (!string.IsNullOrWhiteSpace(value))
{
yield return value;
}
}
yield break;
}
// 3. Single scalar fallback
yield return trimmed;
}
}