|
| 1 | +using ECER.Clients.PSPPortal.Server.Shared; |
| 2 | +using ECER.Managers.Registry.Contract.Registrants; |
| 3 | +using ECER.Utilities.Security; |
| 4 | +using MediatR; |
| 5 | +using Microsoft.Extensions.Caching.Distributed; |
| 6 | +using Microsoft.Extensions.Options; |
| 7 | +using System.Security.Claims; |
| 8 | + |
| 9 | +namespace ECER.Clients.PSPPortal.Server; |
| 10 | + |
| 11 | +public class AuthenticationService(IMediator messageBus, IDistributedCache cache, IOptions<ClaimCacheSettings> claimCacheSettings) |
| 12 | +{ |
| 13 | + public async Task<ClaimsPrincipal?> EnrichUserSecurityContext(ClaimsPrincipal principal, CancellationToken ct) |
| 14 | + { |
| 15 | + ArgumentNullException.ThrowIfNull(principal); |
| 16 | + |
| 17 | + var identityProvider = principal.FindFirst(RegistryPortalClaims.IdenityProvider)?.Value; |
| 18 | + var identityId = principal.FindFirst(ClaimTypes.Name)?.Value; |
| 19 | + |
| 20 | + if (string.IsNullOrEmpty(identityProvider) || string.IsNullOrEmpty(identityId)) return principal; |
| 21 | + |
| 22 | + // try and get the current user information |
| 23 | + var userClaims = await GetUserClaims(new UserIdentity(identityId, identityProvider), ct); |
| 24 | + |
| 25 | + if (userClaims == null) return principal; |
| 26 | + |
| 27 | + principal.AddIdentity(new ClaimsIdentity(userClaims)); |
| 28 | + |
| 29 | + return principal; |
| 30 | + } |
| 31 | + |
| 32 | + private async Task<Claim[]?> GetUserClaims(UserIdentity userIdentity, CancellationToken ct) |
| 33 | + { |
| 34 | + // try to find the registrant |
| 35 | + var registrant = await cache.GetAsync($"userinfo:{userIdentity.UserId}@{userIdentity.IdentityProvider}", |
| 36 | + async ct => (await messageBus.Send(new SearchRegistrantQuery { ByUserIdentity = userIdentity }, ct)).Items.SingleOrDefault(), |
| 37 | + new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(claimCacheSettings.Value.CacheTimeInSeconds) }, |
| 38 | + ct); |
| 39 | + |
| 40 | + if (registrant == null) return null; |
| 41 | + |
| 42 | + // add registrant claims |
| 43 | + var userId = new Claim("user_id", registrant.UserId); |
| 44 | + Claim verificationStatus = new Claim("verified", ""); |
| 45 | + if (registrant.Profile.Status == StatusCode.Verified) |
| 46 | + { |
| 47 | + verificationStatus = new Claim("verified", "true"); |
| 48 | + } |
| 49 | + else if (registrant.Profile.Status is StatusCode.Unverified or StatusCode.PendingforDocuments) |
| 50 | + { |
| 51 | + verificationStatus = new Claim("verified", "false"); |
| 52 | + } |
| 53 | + |
| 54 | + return [userId, verificationStatus]; |
| 55 | + } |
| 56 | +} |
0 commit comments