|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System.Security.Claims; |
| 5 | +using System.Text.Json; |
| 6 | + |
| 7 | +namespace Azure.DataApiBuilder.Core.AuthenticationHelpers; |
| 8 | + |
| 9 | +public static class JwtRoleClaimsTransformer |
| 10 | +{ |
| 11 | + public static void NormalizeRoleClaims( |
| 12 | + ClaimsPrincipal principal, |
| 13 | + string sourceRoleClaimType, |
| 14 | + string? separator) |
| 15 | + { |
| 16 | + foreach (ClaimsIdentity identity in principal.Identities) |
| 17 | + { |
| 18 | + if (!identity.IsAuthenticated) |
| 19 | + { |
| 20 | + continue; |
| 21 | + } |
| 22 | + |
| 23 | + List<Claim> sourceClaims = identity.Claims |
| 24 | + .Where(c => c.Type.Equals(sourceRoleClaimType, StringComparison.Ordinal)) |
| 25 | + .ToList(); |
| 26 | + |
| 27 | + if (sourceClaims.Count == 0) |
| 28 | + { |
| 29 | + continue; |
| 30 | + } |
| 31 | + |
| 32 | + HashSet<string> normalizedValues = new(StringComparer.OrdinalIgnoreCase); |
| 33 | + |
| 34 | + foreach (Claim claim in sourceClaims) |
| 35 | + { |
| 36 | + foreach (string expandedValue in ExpandClaimValues(claim.Value, separator)) |
| 37 | + { |
| 38 | + if (!string.IsNullOrWhiteSpace(expandedValue)) |
| 39 | + { |
| 40 | + normalizedValues.Add(expandedValue.Trim()); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + foreach (string normalizedValue in normalizedValues) |
| 46 | + { |
| 47 | + bool exactClaimAlreadyExists = identity.Claims.Any(c => |
| 48 | + c.Type.Equals(sourceRoleClaimType, StringComparison.Ordinal) && |
| 49 | + c.Value.Equals(normalizedValue, StringComparison.OrdinalIgnoreCase)); |
| 50 | + |
| 51 | + if (!exactClaimAlreadyExists) |
| 52 | + { |
| 53 | + identity.AddClaim(new Claim(sourceRoleClaimType, normalizedValue, ClaimValueTypes.String)); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private static IEnumerable<string> ExpandClaimValues(string rawValue, string? separator) |
| 60 | + { |
| 61 | + if (string.IsNullOrWhiteSpace(rawValue)) |
| 62 | + { |
| 63 | + yield break; |
| 64 | + } |
| 65 | + |
| 66 | + string trimmed = rawValue.Trim(); |
| 67 | + |
| 68 | + // 1. JSON array support |
| 69 | + if (trimmed.StartsWith('[') && trimmed.EndsWith(']')) |
| 70 | + { |
| 71 | + List<string>? values; |
| 72 | + try |
| 73 | + { |
| 74 | + values = JsonSerializer.Deserialize<List<string>>(trimmed); |
| 75 | + } |
| 76 | + catch (JsonException) |
| 77 | + { |
| 78 | + values = null; |
| 79 | + } |
| 80 | + |
| 81 | + if (values is not null) |
| 82 | + { |
| 83 | + foreach (string value in values) |
| 84 | + { |
| 85 | + if (!string.IsNullOrWhiteSpace(value)) |
| 86 | + { |
| 87 | + yield return value.Trim(); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + yield break; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // 2. Configurable separated string support |
| 96 | + if (!string.IsNullOrEmpty(separator)) |
| 97 | + { |
| 98 | + string[] splitValues; |
| 99 | + |
| 100 | + if (separator.Length == 1) |
| 101 | + { |
| 102 | + splitValues = trimmed.Split( |
| 103 | + separator[0], |
| 104 | + StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); |
| 105 | + } |
| 106 | + else |
| 107 | + { |
| 108 | + splitValues = trimmed.Split( |
| 109 | + new[] { separator }, |
| 110 | + StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); |
| 111 | + } |
| 112 | + |
| 113 | + foreach (string value in splitValues) |
| 114 | + { |
| 115 | + if (!string.IsNullOrWhiteSpace(value)) |
| 116 | + { |
| 117 | + yield return value; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + yield break; |
| 122 | + } |
| 123 | + |
| 124 | + // 3. Single scalar fallback |
| 125 | + yield return trimmed; |
| 126 | + } |
| 127 | +} |
0 commit comments