Some checks failed
		
		
	
	Build Test Deploy / authority-container (push) Has been cancelled
				
			Build Test Deploy / docs (push) Has been cancelled
				
			Build Test Deploy / deploy (push) Has been cancelled
				
			Build Test Deploy / build-test (push) Has been cancelled
				
			Docs CI / lint-and-preview (push) Has been cancelled
				
			
		
			
				
	
	
		
			44 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			44 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| 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;
 | |
|     }
 | |
| }
 |