| | | 1 | | using System.Collections.ObjectModel; |
| | | 2 | | |
| | | 3 | | namespace AsiBackbone.Core.Signing; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Configures canonical payload construction and hashing behavior. |
| | | 7 | | /// </summary> |
| | | 8 | | /// <remarks> |
| | | 9 | | /// Metadata is excluded from canonical payloads unless an explicit allow-list is supplied. This prevents host-specific |
| | | 10 | | /// </remarks> |
| | | 11 | | public sealed class CanonicalPayloadOptions |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Gets the default canonicalization version for deterministic AsiBackbone JSON signing payloads. |
| | | 15 | | /// </summary> |
| | | 16 | | public const string DefaultCanonicalizationVersion = "asibackbone.canonical-json.v1"; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Gets the default hash algorithm used by the provider-neutral payload hasher. |
| | | 20 | | /// </summary> |
| | | 21 | | public const string DefaultHashAlgorithm = "SHA-256"; |
| | | 22 | | |
| | 2 | 23 | | private static readonly IReadOnlyCollection<string> EmptyMetadataKeyAllowList = |
| | 2 | 24 | | new ReadOnlyCollection<string>(Array.Empty<string>()); |
| | | 25 | | |
| | 16 | 26 | | private CanonicalPayloadOptions( |
| | 16 | 27 | | string canonicalizationVersion, |
| | 16 | 28 | | string hashAlgorithm, |
| | 16 | 29 | | IReadOnlyCollection<string> metadataKeyAllowList) |
| | | 30 | | { |
| | 16 | 31 | | ArgumentException.ThrowIfNullOrWhiteSpace(canonicalizationVersion); |
| | 16 | 32 | | ArgumentException.ThrowIfNullOrWhiteSpace(hashAlgorithm); |
| | | 33 | | |
| | 16 | 34 | | CanonicalizationVersion = canonicalizationVersion.Trim(); |
| | 16 | 35 | | HashAlgorithm = hashAlgorithm.Trim(); |
| | 16 | 36 | | MetadataKeyAllowList = metadataKeyAllowList; |
| | 16 | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Gets default canonical payload options. |
| | | 41 | | /// </summary> |
| | 13 | 42 | | public static CanonicalPayloadOptions Default { get; } = Create(); |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Gets the canonicalization version stamped onto payloads. |
| | | 46 | | /// </summary> |
| | 31 | 47 | | public string CanonicalizationVersion { get; } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Gets the hash algorithm descriptor used by <see cref="CanonicalPayloadHasher" />. |
| | | 51 | | /// </summary> |
| | 2 | 52 | | public string HashAlgorithm { get; } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Gets the metadata keys that may be included in canonical payloads. |
| | | 56 | | /// </summary> |
| | 45 | 57 | | public IReadOnlyCollection<string> MetadataKeyAllowList { get; } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Creates canonical payload options. |
| | | 61 | | /// </summary> |
| | | 62 | | public static CanonicalPayloadOptions Create( |
| | | 63 | | IEnumerable<string>? metadataKeyAllowList = null, |
| | | 64 | | string? canonicalizationVersion = null, |
| | | 65 | | string? hashAlgorithm = null) |
| | | 66 | | { |
| | 16 | 67 | | return new CanonicalPayloadOptions( |
| | 16 | 68 | | string.IsNullOrWhiteSpace(canonicalizationVersion) |
| | 16 | 69 | | ? DefaultCanonicalizationVersion |
| | 16 | 70 | | : canonicalizationVersion, |
| | 16 | 71 | | string.IsNullOrWhiteSpace(hashAlgorithm) |
| | 16 | 72 | | ? DefaultHashAlgorithm |
| | 16 | 73 | | : hashAlgorithm, |
| | 16 | 74 | | NormalizeMetadataKeyAllowList(metadataKeyAllowList)); |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Determines whether the supplied metadata key may be included in a canonical payload. |
| | | 79 | | /// </summary> |
| | | 80 | | public bool AllowsMetadataKey(string key) |
| | | 81 | | { |
| | 44 | 82 | | return !string.IsNullOrWhiteSpace(key) |
| | 44 | 83 | | && MetadataKeyAllowList.Contains(key.Trim(), StringComparer.Ordinal); |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | private static IReadOnlyCollection<string> NormalizeMetadataKeyAllowList(IEnumerable<string>? metadataKeyAllowList) |
| | | 87 | | { |
| | 16 | 88 | | if (metadataKeyAllowList is null) |
| | | 89 | | { |
| | 2 | 90 | | return EmptyMetadataKeyAllowList; |
| | | 91 | | } |
| | | 92 | | |
| | 14 | 93 | | string[] normalizedKeys = [.. metadataKeyAllowList |
| | 19 | 94 | | .Where(key => !string.IsNullOrWhiteSpace(key)) |
| | 16 | 95 | | .Select(key => key.Trim()) |
| | 14 | 96 | | .Distinct(StringComparer.Ordinal) |
| | 18 | 97 | | .OrderBy(key => key, StringComparer.Ordinal)]; |
| | | 98 | | |
| | 14 | 99 | | return normalizedKeys.Length == 0 |
| | 14 | 100 | | ? EmptyMetadataKeyAllowList |
| | 14 | 101 | | : new ReadOnlyCollection<string>(normalizedKeys); |
| | | 102 | | } |
| | | 103 | | } |