| | | 1 | | using System.Globalization; |
| | | 2 | | using System.Security.Cryptography; |
| | | 3 | | using System.Text; |
| | | 4 | | |
| | | 5 | | namespace ProjectTemplate.Infrastructure.Data.Auditing; |
| | | 6 | | |
| | | 7 | | internal static class ApplicationAuditValueProtector |
| | | 8 | | { |
| | | 9 | | private const string _maskedValue = "***"; |
| | | 10 | | |
| | | 11 | | internal static bool TryProtect( |
| | | 12 | | IApplicationAuditValuePolicy policy, |
| | | 13 | | Type entityType, |
| | | 14 | | string propertyName, |
| | | 15 | | object? value, |
| | | 16 | | out object protectedValue) |
| | | 17 | | { |
| | 652 | 18 | | ArgumentNullException.ThrowIfNull(policy); |
| | 652 | 19 | | ArgumentNullException.ThrowIfNull(entityType); |
| | 652 | 20 | | ArgumentException.ThrowIfNullOrWhiteSpace(propertyName); |
| | | 21 | | |
| | 652 | 22 | | ApplicationAuditValueDecision decision = policy.Evaluate(entityType, propertyName, value) |
| | 652 | 23 | | ?? throw new InvalidOperationException("The application audit value policy returned no decision."); |
| | | 24 | | |
| | 652 | 25 | | switch (decision.Disposition) |
| | | 26 | | { |
| | | 27 | | case ApplicationAuditValueDisposition.Include: |
| | 636 | 28 | | protectedValue = value ?? string.Empty; |
| | 636 | 29 | | return true; |
| | | 30 | | case ApplicationAuditValueDisposition.Mask: |
| | 4 | 31 | | protectedValue = _maskedValue; |
| | 4 | 32 | | return true; |
| | | 33 | | case ApplicationAuditValueDisposition.Hash: |
| | 4 | 34 | | protectedValue = Hash(value); |
| | 4 | 35 | | return true; |
| | | 36 | | case ApplicationAuditValueDisposition.Omit: |
| | 4 | 37 | | protectedValue = string.Empty; |
| | 4 | 38 | | return false; |
| | | 39 | | case ApplicationAuditValueDisposition.Truncate: |
| | 4 | 40 | | protectedValue = Truncate(value, decision.MaximumLength); |
| | 4 | 41 | | return true; |
| | | 42 | | default: |
| | 0 | 43 | | throw new InvalidOperationException($"Unsupported audit value disposition '{decision.Disposition}'."); |
| | | 44 | | } |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | private static string Hash(object? value) |
| | | 48 | | { |
| | 4 | 49 | | string canonicalValue = Convert.ToString(value, CultureInfo.InvariantCulture) ?? string.Empty; |
| | 4 | 50 | | byte[] hash = SHA256.HashData(Encoding.UTF8.GetBytes(canonicalValue)); |
| | 4 | 51 | | return Convert.ToHexString(hash); |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | private static string Truncate(object? value, int? maximumLength) |
| | | 55 | | { |
| | 4 | 56 | | if (maximumLength is null or <= 0) |
| | | 57 | | { |
| | 0 | 58 | | throw new InvalidOperationException("Truncated audit values require a positive maximum length."); |
| | | 59 | | } |
| | | 60 | | |
| | 4 | 61 | | string text = Convert.ToString(value, CultureInfo.InvariantCulture) ?? string.Empty; |
| | 4 | 62 | | return text.Length <= maximumLength.Value |
| | 4 | 63 | | ? text |
| | 4 | 64 | | : text[..maximumLength.Value]; |
| | | 65 | | } |
| | | 66 | | } |