| | | 1 | | using System.Collections.ObjectModel; |
| | | 2 | | using AsiBackbone.Core.Signing; |
| | | 3 | | |
| | | 4 | | namespace AsiBackbone.Core.CapabilityGrants; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Represents the outcome of validating a capability grant, including the policy action the host should take. |
| | | 8 | | /// </summary> |
| | | 9 | | /// <remarks> |
| | | 10 | | /// Hosts should gate execution on <see cref="ShouldAllow" /> rather than <see cref="IsValid" /> alone, and act on |
| | | 11 | | /// <see cref="Action" /> for failed results, because some failures defer or require acknowledgment instead of denying. |
| | | 12 | | /// </remarks> |
| | | 13 | | public sealed class CapabilityGrantValidationResult |
| | | 14 | | { |
| | 0 | 15 | | private static readonly IReadOnlyDictionary<string, string> EmptyMetadata = |
| | 0 | 16 | | new ReadOnlyDictionary<string, string>(new Dictionary<string, string>(StringComparer.Ordinal)); |
| | | 17 | | |
| | 237 | 18 | | private CapabilityGrantValidationResult( |
| | 237 | 19 | | bool isValid, |
| | 237 | 20 | | CapabilityGrantValidationCategory category, |
| | 237 | 21 | | VerificationPolicyAction action, |
| | 237 | 22 | | string status, |
| | 237 | 23 | | string? failureCode, |
| | 237 | 24 | | string? failureMessage, |
| | 237 | 25 | | string tokenId, |
| | 237 | 26 | | IReadOnlyDictionary<string, string> safeMetadata) |
| | | 27 | | { |
| | 237 | 28 | | ArgumentException.ThrowIfNullOrWhiteSpace(status); |
| | 237 | 29 | | ArgumentException.ThrowIfNullOrWhiteSpace(tokenId); |
| | | 30 | | |
| | 237 | 31 | | if (!Enum.IsDefined(category)) |
| | | 32 | | { |
| | 1 | 33 | | throw new ArgumentOutOfRangeException(nameof(category), category, "Validation category must be defined."); |
| | | 34 | | } |
| | | 35 | | |
| | 236 | 36 | | if (!Enum.IsDefined(action)) |
| | | 37 | | { |
| | 1 | 38 | | throw new ArgumentOutOfRangeException(nameof(action), action, "Validation action must be defined."); |
| | | 39 | | } |
| | | 40 | | |
| | 235 | 41 | | IsValid = isValid; |
| | 235 | 42 | | Category = category; |
| | 235 | 43 | | Action = action; |
| | 235 | 44 | | Status = status.Trim(); |
| | 235 | 45 | | FailureCode = NormalizeOptional(failureCode); |
| | 235 | 46 | | FailureMessage = NormalizeOptional(failureMessage); |
| | 235 | 47 | | TokenId = tokenId.Trim(); |
| | 235 | 48 | | SafeMetadata = safeMetadata; |
| | 235 | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Gets a value indicating whether the grant satisfied every configured validation check. |
| | | 53 | | /// </summary> |
| | | 54 | | public bool IsValid { get; } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Gets the validation category that describes why the grant passed or failed. |
| | | 58 | | /// </summary> |
| | | 59 | | public CapabilityGrantValidationCategory Category { get; } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Gets the policy action the host should take for this result. |
| | | 63 | | /// </summary> |
| | | 64 | | public VerificationPolicyAction Action { get; } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Gets a value indicating whether the host may proceed with the governed operation. |
| | | 68 | | /// </summary> |
| | 78 | 69 | | public bool ShouldAllow => Action is VerificationPolicyAction.Allow; |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Gets the result status, either <c>Valid</c> or <c>Failed</c>. |
| | | 73 | | /// </summary> |
| | | 74 | | public string Status { get; } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Gets the machine-readable failure code, such as <c>capability.expired</c>, when validation failed. |
| | | 78 | | /// </summary> |
| | | 79 | | public string? FailureCode { get; } |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Gets the human-readable failure message, when one was supplied. |
| | | 83 | | /// </summary> |
| | | 84 | | public string? FailureMessage { get; } |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Gets the token identifier of the validated grant. |
| | | 88 | | /// </summary> |
| | | 89 | | public string TokenId { get; } |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Gets non-secret grant and outcome metadata, such as issuer, audience, policy action, and failure code, that is |
| | | 93 | | /// safe to log or attach to audit records. |
| | | 94 | | /// </summary> |
| | | 95 | | public IReadOnlyDictionary<string, string> SafeMetadata { get; } |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Creates a successful result that allows the governed operation. |
| | | 99 | | /// </summary> |
| | | 100 | | /// <param name="grant">The grant that passed validation.</param> |
| | | 101 | | /// <returns>A valid result with <see cref="VerificationPolicyAction.Allow" />.</returns> |
| | | 102 | | /// <exception cref="ArgumentNullException"><paramref name="grant" /> is <see langword="null" />.</exception> |
| | | 103 | | public static CapabilityGrantValidationResult Valid(CapabilityGrant grant) |
| | | 104 | | { |
| | 34 | 105 | | ArgumentNullException.ThrowIfNull(grant); |
| | | 106 | | |
| | 33 | 107 | | return new CapabilityGrantValidationResult( |
| | 33 | 108 | | true, |
| | 33 | 109 | | CapabilityGrantValidationCategory.Valid, |
| | 33 | 110 | | VerificationPolicyAction.Allow, |
| | 33 | 111 | | "Valid", |
| | 33 | 112 | | null, |
| | 33 | 113 | | null, |
| | 33 | 114 | | grant.TokenId, |
| | 33 | 115 | | BuildSafeMetadata(grant, CapabilityGrantValidationCategory.Valid, VerificationPolicyAction.Allow, null)); |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | /// <summary> |
| | | 119 | | /// Creates a failed result with the category and policy action the host should apply. |
| | | 120 | | /// </summary> |
| | | 121 | | /// <param name="grant">The grant that failed validation.</param> |
| | | 122 | | /// <param name="category">The validation category that describes the failure.</param> |
| | | 123 | | /// <param name="action">The policy action the host should take, such as deny, defer, or require acknowledgment.</pa |
| | | 124 | | /// <param name="failureCode">The machine-readable failure code.</param> |
| | | 125 | | /// <param name="failureMessage">An optional human-readable failure message.</param> |
| | | 126 | | /// <returns>A failed result.</returns> |
| | | 127 | | /// <exception cref="ArgumentNullException"><paramref name="grant" /> or <paramref name="failureCode" /> is <see lan |
| | | 128 | | /// <exception cref="ArgumentException"><paramref name="failureCode" /> is empty or whitespace.</exception> |
| | | 129 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="category" /> or <paramref name="action" /> is not |
| | | 130 | | public static CapabilityGrantValidationResult Failed( |
| | | 131 | | CapabilityGrant grant, |
| | | 132 | | CapabilityGrantValidationCategory category, |
| | | 133 | | VerificationPolicyAction action, |
| | | 134 | | string failureCode, |
| | | 135 | | string? failureMessage = null) |
| | | 136 | | { |
| | 206 | 137 | | ArgumentNullException.ThrowIfNull(grant); |
| | 205 | 138 | | ArgumentException.ThrowIfNullOrWhiteSpace(failureCode); |
| | | 139 | | |
| | 204 | 140 | | return new CapabilityGrantValidationResult( |
| | 204 | 141 | | false, |
| | 204 | 142 | | category, |
| | 204 | 143 | | action, |
| | 204 | 144 | | "Failed", |
| | 204 | 145 | | failureCode, |
| | 204 | 146 | | failureMessage, |
| | 204 | 147 | | grant.TokenId, |
| | 204 | 148 | | BuildSafeMetadata(grant, category, action, failureCode)); |
| | | 149 | | } |
| | | 150 | | |
| | | 151 | | private static IReadOnlyDictionary<string, string> BuildSafeMetadata( |
| | | 152 | | CapabilityGrant grant, |
| | | 153 | | CapabilityGrantValidationCategory category, |
| | | 154 | | VerificationPolicyAction action, |
| | | 155 | | string? failureCode) |
| | | 156 | | { |
| | 237 | 157 | | Dictionary<string, string> metadata = new(StringComparer.Ordinal) |
| | 237 | 158 | | { |
| | 237 | 159 | | ["audience"] = grant.Audience, |
| | 237 | 160 | | ["category"] = category.ToString(), |
| | 237 | 161 | | ["grant_id"] = grant.TokenId, |
| | 237 | 162 | | ["issuer"] = grant.Issuer, |
| | 237 | 163 | | ["policy_action"] = action.ToString() |
| | 237 | 164 | | }; |
| | | 165 | | |
| | 237 | 166 | | AddIfPresent(metadata, "acknowledgment_id", grant.AcknowledgmentId); |
| | 237 | 167 | | AddIfPresent(metadata, "failure_code", failureCode); |
| | 237 | 168 | | AddIfPresent(metadata, "handshake_id", grant.HandshakeId); |
| | 237 | 169 | | AddIfPresent(metadata, "policy_hash", grant.PolicyHash); |
| | 237 | 170 | | AddIfPresent(metadata, "policy_version", grant.PolicyVersion); |
| | 237 | 171 | | AddIfPresent(metadata, "resource_binding", grant.ResourceBinding); |
| | | 172 | | |
| | 237 | 173 | | return metadata.Count == 0 |
| | 237 | 174 | | ? EmptyMetadata |
| | 237 | 175 | | : new ReadOnlyDictionary<string, string>(metadata); |
| | | 176 | | } |
| | | 177 | | |
| | | 178 | | private static void AddIfPresent(Dictionary<string, string> metadata, string key, string? value) |
| | | 179 | | { |
| | 1422 | 180 | | if (!string.IsNullOrWhiteSpace(value)) |
| | | 181 | | { |
| | 982 | 182 | | metadata[key] = value.Trim(); |
| | | 183 | | } |
| | 1422 | 184 | | } |
| | | 185 | | |
| | | 186 | | private static string? NormalizeOptional(string? value) |
| | | 187 | | { |
| | 470 | 188 | | return string.IsNullOrWhiteSpace(value) ? null : value.Trim(); |
| | | 189 | | } |
| | | 190 | | } |