| | | 1 | | using System.Collections.ObjectModel; |
| | | 2 | | |
| | | 3 | | namespace AsiBackbone.Signing.ManagedKey; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Represents the result returned by a host-owned managed-key signing client. |
| | | 7 | | /// </summary> |
| | | 8 | | public sealed class ManagedKeySignResult |
| | | 9 | | { |
| | 0 | 10 | | private static readonly ReadOnlyDictionary<string, string> EmptyMetadata = |
| | 0 | 11 | | new(new Dictionary<string, string>(StringComparer.Ordinal)); |
| | | 12 | | |
| | 4 | 13 | | private ManagedKeySignResult( |
| | 4 | 14 | | string signature, |
| | 4 | 15 | | string signatureAlgorithm, |
| | 4 | 16 | | string keyId, |
| | 4 | 17 | | string? keyVersion, |
| | 4 | 18 | | string? providerOperationId, |
| | 4 | 19 | | DateTimeOffset signedUtc, |
| | 4 | 20 | | IReadOnlyDictionary<string, string> metadata) |
| | | 21 | | { |
| | 4 | 22 | | ArgumentException.ThrowIfNullOrWhiteSpace(signature); |
| | 4 | 23 | | ArgumentException.ThrowIfNullOrWhiteSpace(signatureAlgorithm); |
| | 4 | 24 | | ArgumentException.ThrowIfNullOrWhiteSpace(keyId); |
| | | 25 | | |
| | 4 | 26 | | Signature = signature.Trim(); |
| | 4 | 27 | | SignatureAlgorithm = signatureAlgorithm.Trim(); |
| | 4 | 28 | | KeyId = keyId.Trim(); |
| | 4 | 29 | | KeyVersion = NormalizeOptional(keyVersion); |
| | 4 | 30 | | ProviderOperationId = NormalizeOptional(providerOperationId); |
| | 4 | 31 | | SignedUtc = signedUtc.ToUniversalTime(); |
| | 4 | 32 | | Metadata = metadata; |
| | 4 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Gets the provider-neutral encoded signature value or provider signature reference. |
| | | 37 | | /// </summary> |
| | 4 | 38 | | public string Signature { get; } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Gets the provider-neutral signature algorithm descriptor. |
| | | 42 | | /// </summary> |
| | 4 | 43 | | public string SignatureAlgorithm { get; } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Gets the managed key identifier or key URI reference used to sign. |
| | | 47 | | /// </summary> |
| | 4 | 48 | | public string KeyId { get; } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Gets the managed key version used to sign, when supplied by the provider. |
| | | 52 | | /// </summary> |
| | 4 | 53 | | public string? KeyVersion { get; } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Gets a safe provider operation identifier, when supplied. |
| | | 57 | | /// </summary> |
| | 8 | 58 | | public string? ProviderOperationId { get; } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Gets the UTC timestamp when the provider completed signing. |
| | | 62 | | /// </summary> |
| | 4 | 63 | | public DateTimeOffset SignedUtc { get; } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Gets safe provider-neutral metadata returned by the managed-key client. |
| | | 67 | | /// </summary> |
| | 4 | 68 | | public IReadOnlyDictionary<string, string> Metadata { get; } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Creates a successful managed-key sign result. |
| | | 72 | | /// </summary> |
| | | 73 | | public static ManagedKeySignResult Create( |
| | | 74 | | string signature, |
| | | 75 | | string signatureAlgorithm, |
| | | 76 | | string keyId, |
| | | 77 | | string? keyVersion, |
| | | 78 | | DateTimeOffset signedUtc, |
| | | 79 | | string? providerOperationId = null, |
| | | 80 | | IReadOnlyDictionary<string, string>? metadata = null) |
| | | 81 | | { |
| | 4 | 82 | | return new ManagedKeySignResult( |
| | 4 | 83 | | signature, |
| | 4 | 84 | | signatureAlgorithm, |
| | 4 | 85 | | keyId, |
| | 4 | 86 | | keyVersion, |
| | 4 | 87 | | providerOperationId, |
| | 4 | 88 | | signedUtc, |
| | 4 | 89 | | NormalizeMetadata(metadata)); |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | private static string? NormalizeOptional(string? value) |
| | | 93 | | { |
| | 8 | 94 | | return string.IsNullOrWhiteSpace(value) ? null : value.Trim(); |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | private static ReadOnlyDictionary<string, string> NormalizeMetadata(IReadOnlyDictionary<string, string>? metadata) |
| | | 98 | | { |
| | 4 | 99 | | if (metadata is null || metadata.Count == 0) |
| | | 100 | | { |
| | 0 | 101 | | return EmptyMetadata; |
| | | 102 | | } |
| | | 103 | | |
| | 4 | 104 | | Dictionary<string, string> normalized = new(StringComparer.Ordinal); |
| | | 105 | | |
| | 24 | 106 | | foreach (KeyValuePair<string, string> item in metadata) |
| | | 107 | | { |
| | 8 | 108 | | if (!string.IsNullOrWhiteSpace(item.Key)) |
| | | 109 | | { |
| | 8 | 110 | | normalized[item.Key.Trim()] = item.Value?.Trim() ?? string.Empty; |
| | | 111 | | } |
| | | 112 | | } |
| | | 113 | | |
| | 4 | 114 | | return normalized.Count == 0 ? EmptyMetadata : new ReadOnlyDictionary<string, string>(normalized); |
| | | 115 | | } |
| | | 116 | | } |