using System; using System.Linq; using System.Security.Claims; using System.Threading; using System.Threading.Tasks; using StellaOps.Authority.Plugins.Abstractions; namespace StellaOps.Authority.Plugin.Standard; internal sealed class StandardClaimsEnricher : IClaimsEnricher { public ValueTask EnrichAsync( ClaimsIdentity identity, AuthorityClaimsEnrichmentContext context, CancellationToken cancellationToken) { if (identity is null) { throw new ArgumentNullException(nameof(identity)); } if (context.User is { } user) { foreach (var role in user.Roles.Where(static r => !string.IsNullOrWhiteSpace(r))) { if (!identity.HasClaim(ClaimTypes.Role, role)) { identity.AddClaim(new Claim(ClaimTypes.Role, role)); } } foreach (var pair in user.Attributes) { if (!string.IsNullOrWhiteSpace(pair.Key) && !identity.HasClaim(pair.Key, pair.Value ?? string.Empty)) { identity.AddClaim(new Claim(pair.Key, pair.Value ?? string.Empty)); } } } return ValueTask.CompletedTask; } }