| | | 1 | | using System.Diagnostics; |
| | | 2 | | using System.Security.Claims; |
| | | 3 | | using ProjectTemplate.Infrastructure.Data.Auditing; |
| | | 4 | | using ProjectTemplate.Web.Authentication.Claims; |
| | | 5 | | |
| | | 6 | | namespace ProjectTemplate.Web.Accessors; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Resolves structured application audit context from the active HTTP request. |
| | | 10 | | /// </summary> |
| | 14 | 11 | | public sealed class HttpContextApplicationAuditContextAccessor( |
| | 14 | 12 | | IHttpContextAccessor httpContextAccessor) |
| | | 13 | | : IApplicationAuditContextAccessor |
| | | 14 | | { |
| | | 15 | | private const string _subjectClaimType = "sub"; |
| | | 16 | | |
| | | 17 | | public ApplicationAuditContext Current |
| | | 18 | | { |
| | | 19 | | get |
| | | 20 | | { |
| | 4 | 21 | | HttpContext? httpContext = httpContextAccessor.HttpContext; |
| | 4 | 22 | | ClaimsPrincipal? user = httpContext?.User; |
| | | 23 | | |
| | | 24 | | // Prefer the normalized application claim: claims transformation may remove the provider claims. |
| | 4 | 25 | | string? subject = GetAuthenticatedClaim(user, ApplicationClaimTypes.Subject) |
| | 4 | 26 | | ?? GetAuthenticatedClaim(user, _subjectClaimType) |
| | 4 | 27 | | ?? GetAuthenticatedClaim(user, ClaimTypes.NameIdentifier); |
| | | 28 | | |
| | 4 | 29 | | if (!string.IsNullOrWhiteSpace(subject)) |
| | | 30 | | { |
| | 3 | 31 | | return new ApplicationAuditContext( |
| | 3 | 32 | | subject, |
| | 3 | 33 | | ApplicationAuditActorTypes.Human, |
| | 3 | 34 | | subject, |
| | 3 | 35 | | correlationId: httpContext?.TraceIdentifier, |
| | 3 | 36 | | traceId: Activity.Current?.TraceId.ToString(), |
| | 3 | 37 | | spanId: Activity.Current?.SpanId.ToString()); |
| | | 38 | | } |
| | | 39 | | |
| | 1 | 40 | | string? remoteIp = httpContext?.Connection.RemoteIpAddress?.ToString(); |
| | | 41 | | |
| | 1 | 42 | | return !string.IsNullOrWhiteSpace(remoteIp) |
| | 1 | 43 | | ? new ApplicationAuditContext( |
| | 1 | 44 | | remoteIp, |
| | 1 | 45 | | ApplicationAuditActorTypes.Network, |
| | 1 | 46 | | $"Remote IP: {remoteIp}", |
| | 1 | 47 | | correlationId: httpContext?.TraceIdentifier, |
| | 1 | 48 | | traceId: Activity.Current?.TraceId.ToString(), |
| | 1 | 49 | | spanId: Activity.Current?.SpanId.ToString()) |
| | 1 | 50 | | : new ApplicationAuditContext( |
| | 1 | 51 | | "Unknown", |
| | 1 | 52 | | ApplicationAuditActorTypes.Unknown, |
| | 1 | 53 | | "Unknown", |
| | 1 | 54 | | correlationId: httpContext?.TraceIdentifier, |
| | 1 | 55 | | traceId: Activity.Current?.TraceId.ToString(), |
| | 1 | 56 | | spanId: Activity.Current?.SpanId.ToString()); |
| | | 57 | | } |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | private static string? GetAuthenticatedClaim( |
| | | 61 | | ClaimsPrincipal? user, |
| | | 62 | | string claimType) |
| | | 63 | | { |
| | 7 | 64 | | if (user?.Identity?.IsAuthenticated != true) |
| | | 65 | | { |
| | 0 | 66 | | return null; |
| | | 67 | | } |
| | | 68 | | |
| | 7 | 69 | | string? value = user.FindFirst(claimType)?.Value?.Trim(); |
| | 7 | 70 | | return string.IsNullOrWhiteSpace(value) ? null : value; |
| | | 71 | | } |
| | | 72 | | } |