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