1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-05 05:00:19 -05:00

Only parse guids on demand

This commit is contained in:
Thomas Rittson 2025-04-02 11:18:09 +10:00
parent bead69f788
commit 48697c4900
No known key found for this signature in database
GPG Key ID: CDDDA03861C35E27

View File

@ -51,30 +51,31 @@ public static class ClaimsExtensions
/// </summary> /// </summary>
private static HasClaim GetClaimsParser(ClaimsPrincipal user, Guid organizationId) private static HasClaim GetClaimsParser(ClaimsPrincipal user, Guid organizationId)
{ {
// Transform into a dict based on the claim type // Group claims by ClaimType
var claimsDict = user.Claims var claimsDict = user.Claims
.GetGuidClaims()
.GroupBy(c => c.Type) .GroupBy(c => c.Type)
.ToDictionary( .ToDictionary(
c => c.Key, c => c.Key,
c => c.Select(v => v.Value)); c => c.ToList());
return claimType return claimType
=> claimsDict.TryGetValue(claimType, out var claimValue) && => claimsDict.TryGetValue(claimType, out var claims) &&
claimValue.Any(v => v == organizationId); claims
.ParseGuids()
.Any(v => v == organizationId);
} }
/// <summary> /// <summary>
/// Parses all claims into proper Guids, or ignore them if they are not valid guids. /// Parses the provided claims into proper Guids, or ignore them if they are not valid guids.
/// </summary> /// </summary>
private static List<(string Type, Guid Value)> GetGuidClaims(this IEnumerable<Claim> claims) private static List<Guid> ParseGuids(this IEnumerable<Claim> claims)
{ {
List<(string Type, Guid Value)> result = []; List<Guid> result = [];
foreach (var claim in claims) foreach (var claim in claims)
{ {
if (Guid.TryParse(claim.Value, out var guid)) if (Guid.TryParse(claim.Value, out var guid))
{ {
result.Add((claim.Type, guid)); result.Add(guid);
} }
} }