| | | 1 | | namespace AsiBackbone.Core.Signing; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Represents the provider-neutral result of a signature verification operation. |
| | | 5 | | /// </summary> |
| | | 6 | | public sealed class SignatureVerificationResult |
| | | 7 | | { |
| | | 8 | | private SignatureVerificationResult( |
| | | 9 | | bool isValid, |
| | | 10 | | string status, |
| | | 11 | | string? failureCode, |
| | | 12 | | string? failureMessage) |
| | 48 | 13 | | : this(isValid, status, failureCode, failureMessage, category: null) |
| | | 14 | | { |
| | 48 | 15 | | } |
| | | 16 | | |
| | 139 | 17 | | private SignatureVerificationResult( |
| | 139 | 18 | | bool isValid, |
| | 139 | 19 | | string status, |
| | 139 | 20 | | string? failureCode, |
| | 139 | 21 | | string? failureMessage, |
| | 139 | 22 | | SignatureVerificationCategory? category) |
| | | 23 | | { |
| | 139 | 24 | | ArgumentException.ThrowIfNullOrWhiteSpace(status); |
| | | 25 | | |
| | 139 | 26 | | IsValid = isValid; |
| | 139 | 27 | | Status = status.Trim(); |
| | 139 | 28 | | FailureCode = NormalizeOptional(failureCode); |
| | 139 | 29 | | FailureMessage = NormalizeOptional(failureMessage); |
| | 139 | 30 | | Category = category; |
| | 139 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Gets a value indicating whether the signature was verified successfully. |
| | | 35 | | /// </summary> |
| | | 36 | | public bool IsValid { get; } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Gets a provider-neutral verification status. |
| | | 40 | | /// </summary> |
| | | 41 | | public string Status { get; } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Gets a provider-neutral failure code when verification did not succeed. |
| | | 45 | | /// </summary> |
| | | 46 | | public string? FailureCode { get; } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Gets a provider-neutral failure message when verification did not succeed. |
| | | 50 | | /// </summary> |
| | | 51 | | public string? FailureMessage { get; } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Gets the explicit provider-neutral category, when the result producer supplied one. |
| | | 55 | | /// </summary> |
| | | 56 | | public SignatureVerificationCategory? Category { get; } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Creates a successful verification result. |
| | | 60 | | /// </summary> |
| | | 61 | | public static SignatureVerificationResult Verified() |
| | | 62 | | { |
| | 49 | 63 | | return new SignatureVerificationResult(true, "Verified", null, null, SignatureVerificationCategory.Valid); |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Creates a failed verification result. |
| | | 68 | | /// </summary> |
| | | 69 | | public static SignatureVerificationResult Failed(string failureCode, string? failureMessage = null) |
| | | 70 | | { |
| | 46 | 71 | | return new SignatureVerificationResult(false, "Failed", failureCode, failureMessage); |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Creates a failed verification result with an explicit provider-neutral category. |
| | | 76 | | /// </summary> |
| | | 77 | | public static SignatureVerificationResult Failed( |
| | | 78 | | string failureCode, |
| | | 79 | | SignatureVerificationCategory category, |
| | | 80 | | string? failureMessage = null) |
| | | 81 | | { |
| | 38 | 82 | | return !Enum.IsDefined(category) || category is SignatureVerificationCategory.Valid |
| | 38 | 83 | | ? throw new ArgumentOutOfRangeException(nameof(category), category, "A failed verification result requires a |
| | 38 | 84 | | : new SignatureVerificationResult(false, "Failed", failureCode, failureMessage, category); |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Creates a result indicating that no signature metadata was available to verify. |
| | | 89 | | /// </summary> |
| | | 90 | | public static SignatureVerificationResult MissingSignature(string? failureMessage = null) |
| | | 91 | | { |
| | 5 | 92 | | return new SignatureVerificationResult( |
| | 5 | 93 | | false, |
| | 5 | 94 | | "MissingSignature", |
| | 5 | 95 | | "signature.missing", |
| | 5 | 96 | | failureMessage, |
| | 5 | 97 | | SignatureVerificationCategory.MissingSignature); |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | private static string? NormalizeOptional(string? value) |
| | | 101 | | { |
| | 278 | 102 | | return string.IsNullOrWhiteSpace(value) |
| | 278 | 103 | | ? null |
| | 278 | 104 | | : value.Trim(); |
| | | 105 | | } |
| | | 106 | | } |