| | | 1 | | using System.Collections.ObjectModel; |
| | | 2 | | |
| | | 3 | | namespace AsiBackbone.Core.Signing; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Represents a provider-neutral request to sign a precomputed artifact hash. |
| | | 7 | | /// </summary> |
| | | 8 | | /// <remarks> |
| | | 9 | | /// The request is intentionally hash-oriented so production providers can use key-based signing APIs without exposing r |
| | | 10 | | /// </remarks> |
| | | 11 | | public sealed class SigningRequest |
| | | 12 | | { |
| | 5 | 13 | | private static readonly IReadOnlyDictionary<string, string> EmptyMetadata = |
| | 5 | 14 | | new ReadOnlyDictionary<string, string>( |
| | 5 | 15 | | new Dictionary<string, string>(StringComparer.Ordinal)); |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Initializes a new instance of the <see cref="SigningRequest" /> class. |
| | | 19 | | /// </summary> |
| | 18 | 20 | | public SigningRequest( |
| | 18 | 21 | | string signingHash, |
| | 18 | 22 | | string? hashAlgorithm = null, |
| | 18 | 23 | | string? purpose = null, |
| | 18 | 24 | | string? keyId = null, |
| | 18 | 25 | | string? keyVersion = null, |
| | 18 | 26 | | IReadOnlyDictionary<string, string>? metadata = null) |
| | | 27 | | { |
| | 18 | 28 | | ArgumentException.ThrowIfNullOrWhiteSpace(signingHash); |
| | | 29 | | |
| | 18 | 30 | | SigningHash = signingHash.Trim(); |
| | 18 | 31 | | HashAlgorithm = NormalizeOptional(hashAlgorithm); |
| | 18 | 32 | | Purpose = NormalizeOptional(purpose); |
| | 18 | 33 | | KeyId = NormalizeOptional(keyId); |
| | 18 | 34 | | KeyVersion = NormalizeOptional(keyVersion); |
| | 18 | 35 | | Metadata = NormalizeMetadata(metadata); |
| | 18 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Gets the precomputed artifact hash to sign. |
| | | 40 | | /// </summary> |
| | 29 | 41 | | public string SigningHash { get; } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Gets the hash algorithm or descriptor associated with <see cref="SigningHash" />, when supplied. |
| | | 45 | | /// </summary> |
| | 36 | 46 | | public string? HashAlgorithm { get; } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Gets the host-defined signing purpose, when supplied. |
| | | 50 | | /// </summary> |
| | 32 | 51 | | public string? Purpose { get; } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Gets the requested signing key identifier, when supplied. |
| | | 55 | | /// </summary> |
| | 32 | 56 | | public string? KeyId { get; } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Gets the requested signing key version, when supplied. |
| | | 60 | | /// </summary> |
| | 34 | 61 | | public string? KeyVersion { get; } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Gets additional provider-neutral request metadata. |
| | | 65 | | /// </summary> |
| | 24 | 66 | | public IReadOnlyDictionary<string, string> Metadata { get; } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Gets a value indicating whether metadata is present. |
| | | 70 | | /// </summary> |
| | 1 | 71 | | public bool HasMetadata => Metadata.Count > 0; |
| | | 72 | | |
| | | 73 | | private static string? NormalizeOptional(string? value) |
| | | 74 | | { |
| | 72 | 75 | | return string.IsNullOrWhiteSpace(value) |
| | 72 | 76 | | ? null |
| | 72 | 77 | | : value.Trim(); |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | private static IReadOnlyDictionary<string, string> NormalizeMetadata( |
| | | 81 | | IReadOnlyDictionary<string, string>? metadata) |
| | | 82 | | { |
| | 18 | 83 | | if (metadata is null || metadata.Count == 0) |
| | | 84 | | { |
| | 12 | 85 | | return EmptyMetadata; |
| | | 86 | | } |
| | | 87 | | |
| | 6 | 88 | | Dictionary<string, string> normalizedMetadata = new(StringComparer.Ordinal); |
| | | 89 | | |
| | 50 | 90 | | foreach (KeyValuePair<string, string> item in metadata) |
| | | 91 | | { |
| | 19 | 92 | | if (string.IsNullOrWhiteSpace(item.Key)) |
| | | 93 | | { |
| | | 94 | | continue; |
| | | 95 | | } |
| | | 96 | | |
| | 18 | 97 | | normalizedMetadata[item.Key.Trim()] = item.Value?.Trim() ?? string.Empty; |
| | | 98 | | } |
| | | 99 | | |
| | 6 | 100 | | return normalizedMetadata.Count == 0 |
| | 6 | 101 | | ? EmptyMetadata |
| | 6 | 102 | | : new ReadOnlyDictionary<string, string>(normalizedMetadata); |
| | | 103 | | } |
| | | 104 | | } |