| | | 1 | | using System.Security.Claims; |
| | | 2 | | using AsiBackbone.Core.Actors; |
| | | 3 | | using Microsoft.AspNetCore.Http; |
| | | 4 | | using Microsoft.Extensions.Options; |
| | | 5 | | |
| | | 6 | | namespace AsiBackbone.AspNetCore.Actors; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Maps the current ASP.NET Core HTTP context into a framework-neutral AsiBackbone actor context. |
| | | 10 | | /// </summary> |
| | | 11 | | public sealed class HttpContextAsiBackboneActorContextResolver : IAsiBackboneHttpActorContextResolver |
| | | 12 | | { |
| | | 13 | | private readonly IHttpContextAccessor httpContextAccessor; |
| | | 14 | | private readonly AsiBackboneHttpActorContextOptions options; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Initializes a new instance of the <see cref="HttpContextAsiBackboneActorContextResolver" /> class. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <param name="httpContextAccessor">The ASP.NET Core HTTP context accessor.</param> |
| | | 20 | | /// <param name="options">The actor mapping options.</param> |
| | 39 | 21 | | public HttpContextAsiBackboneActorContextResolver( |
| | 39 | 22 | | IHttpContextAccessor httpContextAccessor, |
| | 39 | 23 | | IOptions<AsiBackboneHttpActorContextOptions> options) |
| | | 24 | | { |
| | 39 | 25 | | this.httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor)); |
| | 37 | 26 | | this.options = options?.Value ?? throw new ArgumentNullException(nameof(options)); |
| | 35 | 27 | | this.options.Validate(); |
| | 35 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <inheritdoc /> |
| | | 31 | | public IAsiBackboneActorContext ResolveActorContext() |
| | | 32 | | { |
| | 29 | 33 | | ClaimsPrincipal? user = httpContextAccessor.HttpContext?.User; |
| | | 34 | | |
| | 29 | 35 | | if (user?.Identity?.IsAuthenticated != true) |
| | | 36 | | { |
| | 11 | 37 | | return ResolveUnauthenticatedActor; |
| | | 38 | | } |
| | | 39 | | |
| | 18 | 40 | | string? actorId = FindFirstNonEmptyClaimValue(user, options.ActorIdClaimTypes); |
| | 18 | 41 | | if (string.IsNullOrWhiteSpace(actorId)) |
| | | 42 | | { |
| | 2 | 43 | | return ResolveUnauthenticatedActor; |
| | | 44 | | } |
| | | 45 | | |
| | 16 | 46 | | string? displayName = FindFirstNonEmptyClaimValue(user, options.DisplayNameClaimTypes); |
| | 16 | 47 | | AsiBackboneActorType actorType = ResolveActorType(user); |
| | | 48 | | |
| | 16 | 49 | | return actorType switch |
| | 16 | 50 | | { |
| | 2 | 51 | | AsiBackboneActorType.Service => AsiBackboneActorContext.Service(actorId, displayName), |
| | 2 | 52 | | AsiBackboneActorType.System => AsiBackboneActorContext.System, |
| | 4 | 53 | | AsiBackboneActorType.Agent => AsiBackboneActorContext.Agent(actorId, displayName), |
| | 4 | 54 | | AsiBackboneActorType.Human => AsiBackboneActorContext.Human(actorId, displayName), |
| | 2 | 55 | | AsiBackboneActorType.Unknown => AsiBackboneActorContext.Unknown, |
| | 2 | 56 | | _ => AsiBackboneActorContext.Human(actorId, displayName), |
| | 16 | 57 | | }; |
| | | 58 | | } |
| | | 59 | | |
| | 13 | 60 | | private IAsiBackboneActorContext ResolveUnauthenticatedActor => string.IsNullOrWhiteSpace(options.UnauthenticatedDis |
| | 13 | 61 | | ? AsiBackboneActorContext.Unknown |
| | 13 | 62 | | : AsiBackboneActorContext.Human( |
| | 13 | 63 | | AsiBackboneActorContext.UnknownActorId, |
| | 13 | 64 | | options.UnauthenticatedDisplayName, |
| | 13 | 65 | | isAuthenticated: false); |
| | | 66 | | |
| | | 67 | | private AsiBackboneActorType ResolveActorType(ClaimsPrincipal user) |
| | | 68 | | { |
| | 16 | 69 | | string? actorTypeValue = FindFirstNonEmptyClaimValue(user, new[] { options.ActorTypeClaimType }); |
| | | 70 | | |
| | 16 | 71 | | return Enum.TryParse(actorTypeValue, ignoreCase: true, out AsiBackboneActorType actorType) |
| | 16 | 72 | | ? actorType |
| | 16 | 73 | | : options.DefaultAuthenticatedActorType; |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | private static string? FindFirstNonEmptyClaimValue(ClaimsPrincipal user, IEnumerable<string> claimTypes) |
| | | 77 | | { |
| | 356 | 78 | | foreach (string claimType in claimTypes.Where(static claimType => !string.IsNullOrWhiteSpace(claimType))) |
| | | 79 | | { |
| | 96 | 80 | | Claim? claim = user.FindFirst(claimType); |
| | 96 | 81 | | if (!string.IsNullOrWhiteSpace(claim?.Value)) |
| | | 82 | | { |
| | 36 | 83 | | return claim.Value.Trim(); |
| | | 84 | | } |
| | | 85 | | } |
| | | 86 | | |
| | 14 | 87 | | return null; |
| | 36 | 88 | | } |
| | | 89 | | } |