| | | 1 | | using AsiBackbone.Core.Signing; |
| | | 2 | | |
| | | 3 | | namespace AsiBackbone.Core.CapabilityGrants; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Validates signed capability grants against host-supplied expectations before a governed operation executes. |
| | | 7 | | /// </summary> |
| | | 8 | | public static class CapabilityGrantValidator |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Validates a signed capability grant against the supplied options. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <remarks> |
| | | 14 | | /// <para> |
| | | 15 | | /// Validation runs in a fixed order and returns the first failure: proof binding and signature verification when |
| | | 16 | | /// <see cref="CapabilityGrantValidationOptions.RequireProof" /> is set; then issuer, audience, subject, operation, |
| | | 17 | | /// validity window, scope, policy, acknowledgment, handshake, gateway, and resource expectations; and finally use |
| | | 18 | | /// consumption when <see cref="CapabilityGrantValidationOptions.RequireUseCheck" /> is set. |
| | | 19 | | /// </para> |
| | | 20 | | /// <para> |
| | | 21 | | /// Validation fails closed. Omitted options, a missing verifier when proof is required, or a missing use store |
| | | 22 | | /// when a use check is required each produce a failed result rather than a permissive one. Use consumption runs |
| | | 23 | | /// last so a grant that fails any other check does not consume a use. |
| | | 24 | | /// </para> |
| | | 25 | | /// </remarks> |
| | | 26 | | /// <param name="signedGrant">The signed capability grant to validate.</param> |
| | | 27 | | /// <param name="options"> |
| | | 28 | | /// The expectations the grant is validated against. Validation fails when <see langword="null" />; use |
| | | 29 | | /// <see cref="CapabilityGrantValidationOptions.CreateBoundExecutionBoundary" /> at an execution boundary. |
| | | 30 | | /// </param> |
| | | 31 | | /// <param name="verificationService">The signature verification service, required when proof is required.</param> |
| | | 32 | | /// <param name="useStore">The use store that consumes grant uses, required when a use check is required.</param> |
| | | 33 | | /// <param name="cancellationToken">A token used to observe cancellation.</param> |
| | | 34 | | /// <returns> |
| | | 35 | | /// A result whose <see cref="CapabilityGrantValidationResult.Action" /> states whether the host should allow, |
| | | 36 | | /// deny, defer, escalate, or require acknowledgment. |
| | | 37 | | /// </returns> |
| | | 38 | | /// <exception cref="ArgumentNullException"><paramref name="signedGrant" /> is <see langword="null" />.</exception> |
| | | 39 | | /// <exception cref="OperationCanceledException"><paramref name="cancellationToken" /> is cancelled.</exception> |
| | | 40 | | public static async ValueTask<CapabilityGrantValidationResult> ValidateAsync( |
| | | 41 | | SignedGovernanceArtifact<CapabilityGrant> signedGrant, |
| | | 42 | | CapabilityGrantValidationOptions? options = null, |
| | | 43 | | IGovernanceSignatureVerificationService? verificationService = null, |
| | | 44 | | ICapabilityGrantUseStore? useStore = null, |
| | | 45 | | CancellationToken cancellationToken = default) |
| | | 46 | | { |
| | 134 | 47 | | ArgumentNullException.ThrowIfNull(signedGrant); |
| | 133 | 48 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 49 | | |
| | 132 | 50 | | CapabilityGrant grant = signedGrant.Artifact; |
| | | 51 | | |
| | | 52 | | // The previous default built permissive options: no proof, no use check, and no issuer, audience, or scope |
| | | 53 | | // expectations, so the simplest call was the least safe one and returned Valid for anything unexpired. Validati |
| | | 54 | | // now requires the caller to state what it is validating against. |
| | 132 | 55 | | if (options is null) |
| | | 56 | | { |
| | 1 | 57 | | return CapabilityGrantValidationResult.Failed( |
| | 1 | 58 | | grant, |
| | 1 | 59 | | CapabilityGrantValidationCategory.Failed, |
| | 1 | 60 | | VerificationPolicyAction.Deny, |
| | 1 | 61 | | "capability.validation-options-required", |
| | 1 | 62 | | "Capability grant validation requires explicit options describing what the grant is validated against.") |
| | | 63 | | } |
| | | 64 | | |
| | 131 | 65 | | CapabilityGrantValidationOptions effectiveOptions = options; |
| | 131 | 66 | | DateTimeOffset validationUtc = (effectiveOptions.ValidationUtc ?? DateTimeOffset.UtcNow).ToUniversalTime(); |
| | | 67 | | |
| | 131 | 68 | | if (effectiveOptions.RequireProof) |
| | | 69 | | { |
| | 82 | 70 | | CapabilityGrantValidationResult? proofResult = await ValidateProofAsync( |
| | 82 | 71 | | signedGrant, |
| | 82 | 72 | | grant, |
| | 82 | 73 | | effectiveOptions, |
| | 82 | 74 | | verificationService, |
| | 82 | 75 | | cancellationToken).ConfigureAwait(false); |
| | | 76 | | |
| | 82 | 77 | | if (proofResult is not null) |
| | | 78 | | { |
| | 42 | 79 | | return proofResult; |
| | | 80 | | } |
| | | 81 | | } |
| | | 82 | | |
| | 89 | 83 | | CapabilityGrantValidationResult? metadataResult = ValidateMetadata(grant, effectiveOptions, validationUtc); |
| | | 84 | | |
| | 89 | 85 | | if (metadataResult is not null) |
| | | 86 | | { |
| | 40 | 87 | | return metadataResult; |
| | | 88 | | } |
| | | 89 | | |
| | 49 | 90 | | if (effectiveOptions.RequireUseCheck) |
| | | 91 | | { |
| | 39 | 92 | | CapabilityGrantValidationResult? useResult = await ValidateUseAsync( |
| | 39 | 93 | | grant, |
| | 39 | 94 | | effectiveOptions, |
| | 39 | 95 | | useStore, |
| | 39 | 96 | | validationUtc, |
| | 39 | 97 | | cancellationToken).ConfigureAwait(false); |
| | | 98 | | |
| | 38 | 99 | | if (useResult is not null) |
| | | 100 | | { |
| | 17 | 101 | | return useResult; |
| | | 102 | | } |
| | | 103 | | } |
| | | 104 | | |
| | 31 | 105 | | return CapabilityGrantValidationResult.Valid(grant); |
| | 131 | 106 | | } |
| | | 107 | | |
| | | 108 | | private static async ValueTask<CapabilityGrantValidationResult?> ValidateProofAsync( |
| | | 109 | | SignedGovernanceArtifact<CapabilityGrant> signedGrant, |
| | | 110 | | CapabilityGrant grant, |
| | | 111 | | CapabilityGrantValidationOptions options, |
| | | 112 | | IGovernanceSignatureVerificationService? verificationService, |
| | | 113 | | CancellationToken cancellationToken) |
| | | 114 | | { |
| | 82 | 115 | | if (verificationService is null) |
| | | 116 | | { |
| | 3 | 117 | | return CapabilityGrantValidationResult.Failed( |
| | 3 | 118 | | grant, |
| | 3 | 119 | | CapabilityGrantValidationCategory.MissingProof, |
| | 3 | 120 | | VerificationPolicyAction.Deny, |
| | 3 | 121 | | "capability.proof-verifier-missing", |
| | 3 | 122 | | "A proof verifier is required for this validation context."); |
| | | 123 | | } |
| | | 124 | | |
| | 79 | 125 | | CapabilityGrantValidationResult? bindingResult = ValidateProofBinding(signedGrant, grant, options); |
| | | 126 | | |
| | 79 | 127 | | if (bindingResult is not null) |
| | | 128 | | { |
| | 4 | 129 | | return bindingResult; |
| | | 130 | | } |
| | | 131 | | |
| | 75 | 132 | | var verificationContext = VerificationPolicyContext.Create( |
| | 75 | 133 | | purpose: CanonicalArtifactTypes.CapabilityGrant, |
| | 75 | 134 | | expectedKeyId: options.ExpectedProofKeyId, |
| | 75 | 135 | | expectedKeyVersion: options.ExpectedProofKeyVersion, |
| | 75 | 136 | | expectedPolicyVersion: options.ExpectedProofPolicyVersion, |
| | 75 | 137 | | expectedPolicyHash: options.ExpectedProofPolicyHash, |
| | 75 | 138 | | requiredProvider: options.RequiredProofProvider, |
| | 75 | 139 | | requiredHashAlgorithm: options.RequiredProofHashAlgorithm); |
| | | 140 | | |
| | 75 | 141 | | VerificationPolicyOutcome verificationOutcome = await GovernanceArtifactVerifier.VerifyAsync( |
| | 75 | 142 | | signedGrant, |
| | 75 | 143 | | verificationService, |
| | 75 | 144 | | context: verificationContext, |
| | 75 | 145 | | cancellationToken: cancellationToken).ConfigureAwait(false); |
| | | 146 | | |
| | 75 | 147 | | if (verificationOutcome.ShouldAllow) |
| | | 148 | | { |
| | 40 | 149 | | return null; |
| | | 150 | | } |
| | | 151 | | |
| | | 152 | | // A grant whose signature was stripped is not a grant awaiting acknowledgment. Where proof is required, absent |
| | | 153 | | // proof denies, rather than inviting a host that treats RequireAcknowledgment as "proceed after a click" to |
| | | 154 | | // continue on a grant carrying no proof at all. The default verification policy now also denies a missing |
| | | 155 | | // signature; this override is retained so grant validation cannot be weakened by a future default change. |
| | 35 | 156 | | VerificationPolicyAction action = verificationOutcome.Category is SignatureVerificationCategory.MissingSignature |
| | 35 | 157 | | ? VerificationPolicyAction.Deny |
| | 35 | 158 | | : verificationOutcome.Action; |
| | | 159 | | |
| | 35 | 160 | | return CapabilityGrantValidationResult.Failed( |
| | 35 | 161 | | grant, |
| | 35 | 162 | | MapVerificationCategory(verificationOutcome.Category), |
| | 35 | 163 | | action, |
| | 35 | 164 | | verificationOutcome.FailureCode ?? "capability.proof-invalid", |
| | 35 | 165 | | verificationOutcome.FailureMessage); |
| | 82 | 166 | | } |
| | | 167 | | |
| | | 168 | | /// <summary> |
| | | 169 | | /// Binds the signed proof to the grant being validated by rebuilding the canonical payload from the grant itself. |
| | | 170 | | /// </summary> |
| | | 171 | | /// <remarks> |
| | | 172 | | /// Signature verification establishes that a signature covers a hash. It does not establish that the hash describes |
| | | 173 | | /// grant whose fields are about to be evaluated. Rebuilding the payload from <see cref="SignedGovernanceArtifact{TA |
| | | 174 | | /// and comparing hashes closes that gap, and the artifact descriptors are asserted so a proof issued for another ar |
| | | 175 | | /// type or token identifier cannot be presented alongside this grant. |
| | | 176 | | /// </remarks> |
| | | 177 | | private static CapabilityGrantValidationResult? ValidateProofBinding( |
| | | 178 | | SignedGovernanceArtifact<CapabilityGrant> signedGrant, |
| | | 179 | | CapabilityGrant grant, |
| | | 180 | | CapabilityGrantValidationOptions options) |
| | | 181 | | { |
| | 79 | 182 | | if (!string.Equals(signedGrant.ArtifactType, CanonicalArtifactTypes.CapabilityGrant, StringComparison.Ordinal)) |
| | | 183 | | { |
| | 1 | 184 | | return CapabilityGrantValidationResult.Failed( |
| | 1 | 185 | | grant, |
| | 1 | 186 | | CapabilityGrantValidationCategory.InvalidProof, |
| | 1 | 187 | | VerificationPolicyAction.Deny, |
| | 1 | 188 | | "capability.proof-artifact-type-mismatch", |
| | 1 | 189 | | "The signed artifact type is not a capability token grant."); |
| | | 190 | | } |
| | | 191 | | |
| | 78 | 192 | | if (!string.Equals(signedGrant.ArtifactId, grant.TokenId, StringComparison.Ordinal)) |
| | | 193 | | { |
| | 1 | 194 | | return CapabilityGrantValidationResult.Failed( |
| | 1 | 195 | | grant, |
| | 1 | 196 | | CapabilityGrantValidationCategory.InvalidProof, |
| | 1 | 197 | | VerificationPolicyAction.Deny, |
| | 1 | 198 | | "capability.proof-artifact-id-mismatch", |
| | 1 | 199 | | "The signed artifact identifier does not match the grant token identifier."); |
| | | 200 | | } |
| | | 201 | | |
| | | 202 | | CanonicalPayloadHash recomputedHash; |
| | | 203 | | |
| | | 204 | | try |
| | | 205 | | { |
| | 77 | 206 | | recomputedHash = CanonicalPayloadHasher.ComputeHash( |
| | 77 | 207 | | CanonicalPayloadBuilder.ForCapabilityGrant(grant, options.ProofPayloadOptions), |
| | 77 | 208 | | signedGrant.HashAlgorithm); |
| | 77 | 209 | | } |
| | 0 | 210 | | catch (NotSupportedException) |
| | | 211 | | { |
| | 0 | 212 | | return CapabilityGrantValidationResult.Failed( |
| | 0 | 213 | | grant, |
| | 0 | 214 | | CapabilityGrantValidationCategory.InvalidProof, |
| | 0 | 215 | | VerificationPolicyAction.Deny, |
| | 0 | 216 | | "capability.proof-hash-algorithm-unsupported", |
| | 0 | 217 | | "The grant canonical payload cannot be rebuilt with the built-in hasher, so the proof is not bound to th |
| | | 218 | | } |
| | | 219 | | |
| | 77 | 220 | | return string.Equals(recomputedHash.HashValue, signedGrant.CanonicalHash.HashValue, StringComparison.Ordinal) |
| | 77 | 221 | | ? null |
| | 77 | 222 | | : CapabilityGrantValidationResult.Failed( |
| | 77 | 223 | | grant, |
| | 77 | 224 | | CapabilityGrantValidationCategory.InvalidProof, |
| | 77 | 225 | | VerificationPolicyAction.Deny, |
| | 77 | 226 | | "capability.proof-content-mismatch", |
| | 77 | 227 | | "The grant does not hash to the signed canonical hash value."); |
| | 0 | 228 | | } |
| | | 229 | | |
| | | 230 | | #pragma warning disable IDE0046 // Preserve explicit ordered guard clauses for auditability. |
| | | 231 | | private static CapabilityGrantValidationResult? ValidateMetadata( |
| | | 232 | | CapabilityGrant grant, |
| | | 233 | | CapabilityGrantValidationOptions options, |
| | | 234 | | DateTimeOffset validationUtc) |
| | | 235 | | { |
| | 89 | 236 | | if (options.Issuer is not null |
| | 89 | 237 | | && !string.Equals(options.Issuer, grant.Issuer, StringComparison.Ordinal)) |
| | | 238 | | { |
| | 3 | 239 | | return CapabilityGrantValidationResult.Failed( |
| | 3 | 240 | | grant, |
| | 3 | 241 | | CapabilityGrantValidationCategory.WrongIssuer, |
| | 3 | 242 | | VerificationPolicyAction.Deny, |
| | 3 | 243 | | "capability.issuer-mismatch"); |
| | | 244 | | } |
| | | 245 | | |
| | 86 | 246 | | if (options.Audience is not null |
| | 86 | 247 | | && !string.Equals(options.Audience, grant.Audience, StringComparison.Ordinal)) |
| | | 248 | | { |
| | 2 | 249 | | return CapabilityGrantValidationResult.Failed( |
| | 2 | 250 | | grant, |
| | 2 | 251 | | CapabilityGrantValidationCategory.WrongAudience, |
| | 2 | 252 | | VerificationPolicyAction.Deny, |
| | 2 | 253 | | "capability.audience-mismatch"); |
| | | 254 | | } |
| | | 255 | | |
| | 84 | 256 | | if (options.ExpectedSubjectId is not null |
| | 84 | 257 | | && !string.Equals(options.ExpectedSubjectId, grant.SubjectId, StringComparison.Ordinal)) |
| | | 258 | | { |
| | 7 | 259 | | return CapabilityGrantValidationResult.Failed( |
| | 7 | 260 | | grant, |
| | 7 | 261 | | CapabilityGrantValidationCategory.SubjectMismatch, |
| | 7 | 262 | | VerificationPolicyAction.Deny, |
| | 7 | 263 | | "capability.subject-mismatch"); |
| | | 264 | | } |
| | | 265 | | |
| | 77 | 266 | | if (options.ExpectedOperationName is not null |
| | 77 | 267 | | && !string.Equals(options.ExpectedOperationName, grant.OperationName, StringComparison.Ordinal)) |
| | | 268 | | { |
| | 4 | 269 | | return CapabilityGrantValidationResult.Failed( |
| | 4 | 270 | | grant, |
| | 4 | 271 | | CapabilityGrantValidationCategory.OperationMismatch, |
| | 4 | 272 | | VerificationPolicyAction.Deny, |
| | 4 | 273 | | "capability.operation-mismatch"); |
| | | 274 | | } |
| | | 275 | | |
| | 73 | 276 | | if (IsNotYetValid(grant, validationUtc, options.AllowedClockSkew)) |
| | | 277 | | { |
| | 4 | 278 | | return CapabilityGrantValidationResult.Failed( |
| | 4 | 279 | | grant, |
| | 4 | 280 | | CapabilityGrantValidationCategory.NotYetValid, |
| | 4 | 281 | | VerificationPolicyAction.Defer, |
| | 4 | 282 | | "capability.not-yet-valid"); |
| | | 283 | | } |
| | | 284 | | |
| | 69 | 285 | | if (IsExpired(grant, validationUtc, options.AllowedClockSkew)) |
| | | 286 | | { |
| | 5 | 287 | | return CapabilityGrantValidationResult.Failed( |
| | 5 | 288 | | grant, |
| | 5 | 289 | | CapabilityGrantValidationCategory.Expired, |
| | 5 | 290 | | VerificationPolicyAction.Deny, |
| | 5 | 291 | | "capability.expired"); |
| | | 292 | | } |
| | | 293 | | |
| | 64 | 294 | | if (options.Scopes.Count > 0 |
| | 64 | 295 | | && !ContainsRequiredScopes(grant.Scopes, options.Scopes)) |
| | | 296 | | { |
| | 4 | 297 | | return CapabilityGrantValidationResult.Failed( |
| | 4 | 298 | | grant, |
| | 4 | 299 | | CapabilityGrantValidationCategory.WrongScope, |
| | 4 | 300 | | VerificationPolicyAction.Deny, |
| | 4 | 301 | | "capability.scope-missing"); |
| | | 302 | | } |
| | | 303 | | |
| | 60 | 304 | | if ((options.PolicyVersion is not null |
| | 60 | 305 | | && !string.Equals(options.PolicyVersion, grant.PolicyVersion, StringComparison.Ordinal)) |
| | 60 | 306 | | || (options.PolicyHash is not null |
| | 60 | 307 | | && !string.Equals(options.PolicyHash, grant.PolicyHash, StringComparison.Ordinal))) |
| | | 308 | | { |
| | 3 | 309 | | return CapabilityGrantValidationResult.Failed( |
| | 3 | 310 | | grant, |
| | 3 | 311 | | CapabilityGrantValidationCategory.PolicyMismatch, |
| | 3 | 312 | | VerificationPolicyAction.Deny, |
| | 3 | 313 | | "capability.policy-mismatch"); |
| | | 314 | | } |
| | | 315 | | |
| | 57 | 316 | | if (options.RequireAcknowledgmentReference && !grant.HasAcknowledgmentReference) |
| | | 317 | | { |
| | 2 | 318 | | return CapabilityGrantValidationResult.Failed( |
| | 2 | 319 | | grant, |
| | 2 | 320 | | CapabilityGrantValidationCategory.MissingAcknowledgmentReference, |
| | 2 | 321 | | VerificationPolicyAction.RequireAcknowledgment, |
| | 2 | 322 | | "capability.acknowledgment-missing"); |
| | | 323 | | } |
| | | 324 | | |
| | 55 | 325 | | if (options.AcknowledgmentId is not null |
| | 55 | 326 | | && !string.Equals(options.AcknowledgmentId, grant.AcknowledgmentId, StringComparison.Ordinal)) |
| | | 327 | | { |
| | 1 | 328 | | return CapabilityGrantValidationResult.Failed( |
| | 1 | 329 | | grant, |
| | 1 | 330 | | CapabilityGrantValidationCategory.AcknowledgmentMismatch, |
| | 1 | 331 | | VerificationPolicyAction.Deny, |
| | 1 | 332 | | "capability.acknowledgment-mismatch"); |
| | | 333 | | } |
| | | 334 | | |
| | 54 | 335 | | if (options.HandshakeId is not null |
| | 54 | 336 | | && !string.Equals(options.HandshakeId, grant.HandshakeId, StringComparison.Ordinal)) |
| | | 337 | | { |
| | 1 | 338 | | return CapabilityGrantValidationResult.Failed( |
| | 1 | 339 | | grant, |
| | 1 | 340 | | CapabilityGrantValidationCategory.HandshakeMismatch, |
| | 1 | 341 | | VerificationPolicyAction.Deny, |
| | 1 | 342 | | "capability.handshake-mismatch"); |
| | | 343 | | } |
| | | 344 | | |
| | 53 | 345 | | if (options.GatewayBinding is not null |
| | 53 | 346 | | && !string.Equals(options.GatewayBinding, grant.GatewayBinding, StringComparison.Ordinal)) |
| | | 347 | | { |
| | 2 | 348 | | return CapabilityGrantValidationResult.Failed( |
| | 2 | 349 | | grant, |
| | 2 | 350 | | CapabilityGrantValidationCategory.GatewayMismatch, |
| | 2 | 351 | | VerificationPolicyAction.Deny, |
| | 2 | 352 | | "capability.gateway-mismatch"); |
| | | 353 | | } |
| | | 354 | | |
| | 51 | 355 | | if (options.ResourceBinding is not null |
| | 51 | 356 | | && !string.Equals(options.ResourceBinding, grant.ResourceBinding, StringComparison.Ordinal)) |
| | | 357 | | { |
| | 2 | 358 | | return CapabilityGrantValidationResult.Failed( |
| | 2 | 359 | | grant, |
| | 2 | 360 | | CapabilityGrantValidationCategory.ResourceMismatch, |
| | 2 | 361 | | VerificationPolicyAction.Deny, |
| | 2 | 362 | | "capability.resource-mismatch"); |
| | | 363 | | } |
| | | 364 | | |
| | 49 | 365 | | return null; |
| | | 366 | | } |
| | | 367 | | #pragma warning restore IDE0046 |
| | | 368 | | |
| | | 369 | | private static bool IsNotYetValid(CapabilityGrant grant, DateTimeOffset validationUtc, TimeSpan allowedClockSkew) |
| | | 370 | | { |
| | 73 | 371 | | return grant.NotBeforeUtc.HasValue |
| | 73 | 372 | | && grant.NotBeforeUtc.Value > validationUtc |
| | 73 | 373 | | && grant.NotBeforeUtc.Value - validationUtc > allowedClockSkew; |
| | | 374 | | } |
| | | 375 | | |
| | | 376 | | private static bool IsExpired(CapabilityGrant grant, DateTimeOffset validationUtc, TimeSpan allowedClockSkew) |
| | | 377 | | { |
| | 69 | 378 | | return validationUtc >= grant.ExpiresUtc |
| | 69 | 379 | | && validationUtc - grant.ExpiresUtc >= allowedClockSkew; |
| | | 380 | | } |
| | | 381 | | |
| | | 382 | | private static async ValueTask<CapabilityGrantValidationResult?> ValidateUseAsync( |
| | | 383 | | CapabilityGrant grant, |
| | | 384 | | CapabilityGrantValidationOptions options, |
| | | 385 | | ICapabilityGrantUseStore? useStore, |
| | | 386 | | DateTimeOffset validationUtc, |
| | | 387 | | CancellationToken cancellationToken) |
| | | 388 | | { |
| | 39 | 389 | | if (useStore is null) |
| | | 390 | | { |
| | 3 | 391 | | return CapabilityGrantValidationResult.Failed(grant, CapabilityGrantValidationCategory.ReplayStoreUnavailabl |
| | | 392 | | } |
| | | 393 | | |
| | | 394 | | // A limit the caller supplies is local policy; a limit the issuer bound into the signed payload is authority. |
| | | 395 | | // Taking the narrower of the two lets a relying party tighten the limit but never widen what was issued. |
| | 36 | 396 | | int effectiveMaxUseCount = grant.MaxUseCount.HasValue |
| | 36 | 397 | | ? Math.Min(grant.MaxUseCount.Value, options.MaxUseCount) |
| | 36 | 398 | | : options.MaxUseCount; |
| | | 399 | | |
| | 36 | 400 | | CapabilityGrantUseResult result = await useStore |
| | 36 | 401 | | .TryConsumeAsync(grant, effectiveMaxUseCount, validationUtc, cancellationToken) |
| | 36 | 402 | | .ConfigureAwait(false); |
| | | 403 | | |
| | 35 | 404 | | return result.State switch |
| | 35 | 405 | | { |
| | 1 | 406 | | GrantUseState.Unspecified => CapabilityGrantValidationResult.Failed(grant, CapabilityGrantValidationCategory |
| | 21 | 407 | | GrantUseState.Accepted => null, |
| | 5 | 408 | | GrantUseState.UseLimitExceeded => CapabilityGrantValidationResult.Failed(grant, CapabilityGrantValidationCat |
| | 2 | 409 | | GrantUseState.Stopped => CapabilityGrantValidationResult.Failed(grant, CapabilityGrantValidationCategory.Rev |
| | 2 | 410 | | GrantUseState.Cancelled => CapabilityGrantValidationResult.Failed(grant, CapabilityGrantValidationCategory.C |
| | 3 | 411 | | GrantUseState.Unavailable => CapabilityGrantValidationResult.Failed(grant, CapabilityGrantValidationCategory |
| | 1 | 412 | | _ => CapabilityGrantValidationResult.Failed(grant, CapabilityGrantValidationCategory.Failed, VerificationPol |
| | 35 | 413 | | }; |
| | 38 | 414 | | } |
| | | 415 | | |
| | | 416 | | private static bool ContainsRequiredScopes(IReadOnlyList<string> actualScopes, IReadOnlyList<string> requiredScopes) |
| | | 417 | | { |
| | 58 | 418 | | HashSet<string> actualScopeSet = new(actualScopes, StringComparer.Ordinal); |
| | | 419 | | |
| | 230 | 420 | | foreach (string requiredScope in requiredScopes) |
| | | 421 | | { |
| | 59 | 422 | | if (!actualScopeSet.Contains(requiredScope)) |
| | | 423 | | { |
| | 4 | 424 | | return false; |
| | | 425 | | } |
| | | 426 | | } |
| | | 427 | | |
| | 54 | 428 | | return true; |
| | 4 | 429 | | } |
| | | 430 | | |
| | | 431 | | private static CapabilityGrantValidationCategory MapVerificationCategory(SignatureVerificationCategory category) |
| | | 432 | | { |
| | 35 | 433 | | return category switch |
| | 35 | 434 | | { |
| | 0 | 435 | | SignatureVerificationCategory.Unspecified => CapabilityGrantValidationCategory.Failed, |
| | 1 | 436 | | SignatureVerificationCategory.MissingSignature => CapabilityGrantValidationCategory.MissingProof, |
| | 0 | 437 | | SignatureVerificationCategory.Valid => CapabilityGrantValidationCategory.Valid, |
| | 20 | 438 | | SignatureVerificationCategory.InvalidSignature => CapabilityGrantValidationCategory.InvalidProof, |
| | 1 | 439 | | SignatureVerificationCategory.HashMismatch => CapabilityGrantValidationCategory.InvalidProof, |
| | 2 | 440 | | SignatureVerificationCategory.RevokedKey => CapabilityGrantValidationCategory.Revoked, |
| | 2 | 441 | | SignatureVerificationCategory.UntrustedKey => CapabilityGrantValidationCategory.InvalidProof, |
| | 3 | 442 | | SignatureVerificationCategory.UntrustedSigningContext => CapabilityGrantValidationCategory.InvalidProof, |
| | 2 | 443 | | SignatureVerificationCategory.ProviderUnavailable => CapabilityGrantValidationCategory.Failed, |
| | 1 | 444 | | SignatureVerificationCategory.UnknownKeyVersion => CapabilityGrantValidationCategory.Failed, |
| | 1 | 445 | | SignatureVerificationCategory.CanonicalizationMismatch => CapabilityGrantValidationCategory.InvalidProof, |
| | 1 | 446 | | SignatureVerificationCategory.UnsupportedAlgorithm => CapabilityGrantValidationCategory.InvalidProof, |
| | 1 | 447 | | SignatureVerificationCategory.Failed => CapabilityGrantValidationCategory.Failed, |
| | 0 | 448 | | _ => CapabilityGrantValidationCategory.Failed |
| | 35 | 449 | | }; |
| | | 450 | | } |
| | | 451 | | } |