| | | 1 | | using AsiBackbone.Core.Signing; |
| | | 2 | | |
| | | 3 | | namespace AsiBackbone.Core.CapabilityGrants; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Describes the expectations a capability grant is validated against by <see cref="CapabilityGrantValidator" />. |
| | | 7 | | /// </summary> |
| | | 8 | | /// <remarks> |
| | | 9 | | /// <para> |
| | | 10 | | /// Each optional expectation is checked only when it is set; string comparisons are ordinal, and blank values are |
| | | 11 | | /// treated as absent. Choose a factory that matches the validation context: |
| | | 12 | | /// <see cref="CreateBoundExecutionBoundary" /> for an execution boundary that requires proof and binds the grant to |
| | | 13 | | /// the authenticated subject, <see cref="CreateMetadataValidation" /> for proof-free inspection of grant metadata, and |
| | | 14 | | /// <see cref="Create" /> for a fully custom profile. |
| | | 15 | | /// </para> |
| | | 16 | | /// <para> |
| | | 17 | | /// Requiring proof or a use check without an audience expectation is rejected, because proof alone does not establish |
| | | 18 | | /// that the grant was issued for this audience. |
| | | 19 | | /// </para> |
| | | 20 | | /// </remarks> |
| | | 21 | | public sealed class CapabilityGrantValidationOptions |
| | | 22 | | { |
| | 1 | 23 | | private static readonly string[] EmptyScopes = []; |
| | | 24 | | |
| | 259 | 25 | | private CapabilityGrantValidationOptions( |
| | 259 | 26 | | string? issuer, |
| | 259 | 27 | | string? audience, |
| | 259 | 28 | | IReadOnlyList<string> scopes, |
| | 259 | 29 | | DateTimeOffset? validationUtc, |
| | 259 | 30 | | TimeSpan allowedClockSkew, |
| | 259 | 31 | | string? policyVersion, |
| | 259 | 32 | | string? policyHash, |
| | 259 | 33 | | string? acknowledgmentId, |
| | 259 | 34 | | string? handshakeId, |
| | 259 | 35 | | string? gatewayBinding, |
| | 259 | 36 | | string? resourceBinding, |
| | 259 | 37 | | string? expectedSubjectId, |
| | 259 | 38 | | string? expectedOperationName, |
| | 259 | 39 | | bool requireSubjectBinding, |
| | 259 | 40 | | bool requireProof, |
| | 259 | 41 | | bool requireAcknowledgmentReference, |
| | 259 | 42 | | bool requireUseCheck, |
| | 259 | 43 | | int maxUseCount, |
| | 259 | 44 | | string? expectedProofKeyId, |
| | 259 | 45 | | string? expectedProofKeyVersion, |
| | 259 | 46 | | string? expectedProofPolicyVersion, |
| | 259 | 47 | | string? expectedProofPolicyHash, |
| | 259 | 48 | | string? requiredProofProvider, |
| | 259 | 49 | | string? requiredProofHashAlgorithm, |
| | 259 | 50 | | CanonicalPayloadOptions? proofPayloadOptions) |
| | | 51 | | { |
| | 259 | 52 | | if (allowedClockSkew < TimeSpan.Zero) |
| | | 53 | | { |
| | 1 | 54 | | throw new ArgumentOutOfRangeException( |
| | 1 | 55 | | nameof(allowedClockSkew), |
| | 1 | 56 | | allowedClockSkew, |
| | 1 | 57 | | "Allowed clock skew must be greater than or equal to zero."); |
| | | 58 | | } |
| | | 59 | | |
| | 258 | 60 | | if (maxUseCount < 1) |
| | | 61 | | { |
| | 1 | 62 | | throw new ArgumentOutOfRangeException(nameof(maxUseCount), maxUseCount, "Maximum use count must be greater t |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | // A grant can be proof-verified and still be the wrong grant for this caller. Without an audience expectation a |
| | | 66 | | // grant issued for one gateway validates at another, so requiring proof or a use check without stating the |
| | | 67 | | // audience is a configuration that cannot deliver what it appears to promise. |
| | 257 | 68 | | if ((requireProof || requireUseCheck) && string.IsNullOrWhiteSpace(audience)) |
| | | 69 | | { |
| | 3 | 70 | | throw new ArgumentException( |
| | 3 | 71 | | "An audience expectation is required when proof or use checking is required, because proof alone does no |
| | 3 | 72 | | nameof(audience)); |
| | | 73 | | } |
| | | 74 | | |
| | 254 | 75 | | Issuer = NormalizeOptional(issuer); |
| | 254 | 76 | | Audience = NormalizeOptional(audience); |
| | 254 | 77 | | Scopes = scopes; |
| | 254 | 78 | | ValidationUtc = validationUtc?.ToUniversalTime(); |
| | 254 | 79 | | AllowedClockSkew = allowedClockSkew; |
| | 254 | 80 | | PolicyVersion = NormalizeOptional(policyVersion); |
| | 254 | 81 | | PolicyHash = NormalizeOptional(policyHash); |
| | 254 | 82 | | AcknowledgmentId = NormalizeOptional(acknowledgmentId); |
| | 254 | 83 | | HandshakeId = NormalizeOptional(handshakeId); |
| | 254 | 84 | | GatewayBinding = NormalizeOptional(gatewayBinding); |
| | 254 | 85 | | ResourceBinding = NormalizeOptional(resourceBinding); |
| | 254 | 86 | | ExpectedSubjectId = NormalizeOptional(expectedSubjectId); |
| | 254 | 87 | | if (requireSubjectBinding && ExpectedSubjectId is null) |
| | | 88 | | { |
| | 0 | 89 | | throw new ArgumentException( |
| | 0 | 90 | | "A subject expectation is required for this validation profile.", |
| | 0 | 91 | | nameof(expectedSubjectId)); |
| | | 92 | | } |
| | | 93 | | |
| | 254 | 94 | | ExpectedOperationName = NormalizeOptional(expectedOperationName); |
| | 254 | 95 | | RequireSubjectBinding = requireSubjectBinding; |
| | 254 | 96 | | RequireProof = requireProof; |
| | 254 | 97 | | RequireAcknowledgmentReference = requireAcknowledgmentReference; |
| | 254 | 98 | | RequireUseCheck = requireUseCheck; |
| | 254 | 99 | | MaxUseCount = maxUseCount; |
| | 254 | 100 | | ExpectedProofKeyId = NormalizeOptional(expectedProofKeyId); |
| | 254 | 101 | | ExpectedProofKeyVersion = NormalizeOptional(expectedProofKeyVersion); |
| | 254 | 102 | | ExpectedProofPolicyVersion = NormalizeOptional(expectedProofPolicyVersion); |
| | 254 | 103 | | ExpectedProofPolicyHash = NormalizeOptional(expectedProofPolicyHash); |
| | 254 | 104 | | RequiredProofProvider = NormalizeOptional(requiredProofProvider); |
| | 254 | 105 | | RequiredProofHashAlgorithm = NormalizeOptional(requiredProofHashAlgorithm); |
| | 254 | 106 | | ProofPayloadOptions = proofPayloadOptions; |
| | 254 | 107 | | } |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Gets the issuer the grant must name, when an issuer expectation is configured. |
| | | 111 | | /// </summary> |
| | | 112 | | public string? Issuer { get; } |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// Gets the audience the grant must name, when an audience expectation is configured. |
| | | 116 | | /// </summary> |
| | | 117 | | /// <remarks> |
| | | 118 | | /// An audience is required whenever <see cref="RequireProof" /> or <see cref="RequireUseCheck" /> is set. |
| | | 119 | | /// </remarks> |
| | | 120 | | public string? Audience { get; } |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Gets the scopes the grant must contain, trimmed, de-duplicated, and sorted ordinally. |
| | | 124 | | /// </summary> |
| | | 125 | | /// <remarks> |
| | | 126 | | /// The grant may carry additional scopes. An empty list disables the scope check. |
| | | 127 | | /// </remarks> |
| | | 128 | | public IReadOnlyList<string> Scopes { get; } |
| | | 129 | | |
| | | 130 | | /// <summary> |
| | | 131 | | /// Gets the UTC instant used to evaluate the grant validity window, or <see langword="null" /> to use the current |
| | | 132 | | /// time. |
| | | 133 | | /// </summary> |
| | | 134 | | public DateTimeOffset? ValidationUtc { get; } |
| | | 135 | | |
| | | 136 | | /// <summary> |
| | | 137 | | /// Gets the non-negative tolerance applied to the grant not-before and expiry times. |
| | | 138 | | /// </summary> |
| | | 139 | | public TimeSpan AllowedClockSkew { get; } |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Gets the policy version the grant must carry, when a policy version expectation is configured. |
| | | 143 | | /// </summary> |
| | | 144 | | public string? PolicyVersion { get; } |
| | | 145 | | |
| | | 146 | | /// <summary> |
| | | 147 | | /// Gets the policy hash the grant must carry, when a policy hash expectation is configured. |
| | | 148 | | /// </summary> |
| | | 149 | | public string? PolicyHash { get; } |
| | | 150 | | |
| | | 151 | | /// <summary> |
| | | 152 | | /// Gets the acknowledgment identifier the grant must reference, when an acknowledgment expectation is configured. |
| | | 153 | | /// </summary> |
| | | 154 | | public string? AcknowledgmentId { get; } |
| | | 155 | | |
| | | 156 | | /// <summary> |
| | | 157 | | /// Gets the liability handshake identifier the grant must reference, when a handshake expectation is configured. |
| | | 158 | | /// </summary> |
| | | 159 | | public string? HandshakeId { get; } |
| | | 160 | | |
| | | 161 | | /// <summary> |
| | | 162 | | /// Gets the gateway binding the grant must carry, when a gateway expectation is configured. |
| | | 163 | | /// </summary> |
| | | 164 | | public string? GatewayBinding { get; } |
| | | 165 | | |
| | | 166 | | /// <summary> |
| | | 167 | | /// Gets the resource binding the grant must carry, when a resource expectation is configured. |
| | | 168 | | /// </summary> |
| | | 169 | | public string? ResourceBinding { get; } |
| | | 170 | | |
| | | 171 | | /// <summary> |
| | | 172 | | /// Gets the authenticated subject that the grant must identify, when subject binding is configured. |
| | | 173 | | /// </summary> |
| | | 174 | | public string? ExpectedSubjectId { get; } |
| | | 175 | | |
| | | 176 | | /// <summary> |
| | | 177 | | /// Gets the requested operation that the grant must identify, when operation binding is configured. |
| | | 178 | | /// </summary> |
| | | 179 | | public string? ExpectedOperationName { get; } |
| | | 180 | | |
| | | 181 | | /// <summary> |
| | | 182 | | /// Gets a value indicating whether the grant must carry a signature that verifies and is bound to the grant |
| | | 183 | | /// content. |
| | | 184 | | /// </summary> |
| | | 185 | | /// <remarks> |
| | | 186 | | /// When set, validation requires an <see cref="IGovernanceSignatureVerificationService" /> and rebuilds the |
| | | 187 | | /// canonical payload from the grant using <see cref="ProofPayloadOptions" />. |
| | | 188 | | /// </remarks> |
| | | 189 | | public bool RequireProof { get; } |
| | | 190 | | |
| | | 191 | | /// <summary> |
| | | 192 | | /// Gets a value indicating whether the grant must reference a handshake acknowledgment. |
| | | 193 | | /// </summary> |
| | | 194 | | /// <remarks> |
| | | 195 | | /// A grant without an acknowledgment reference fails with <see cref="VerificationPolicyAction.RequireAcknowledgment |
| | | 196 | | /// </remarks> |
| | | 197 | | public bool RequireAcknowledgmentReference { get; } |
| | | 198 | | |
| | | 199 | | /// <summary> |
| | | 200 | | /// Gets a value indicating whether validation must consume a grant use through an |
| | | 201 | | /// <see cref="ICapabilityGrantUseStore" />. |
| | | 202 | | /// </summary> |
| | | 203 | | public bool RequireUseCheck { get; } |
| | | 204 | | |
| | | 205 | | /// <summary> |
| | | 206 | | /// Gets the maximum use count allowed by this validation context. |
| | | 207 | | /// </summary> |
| | | 208 | | /// <remarks> |
| | | 209 | | /// When the grant carries its own signed use limit, the narrower of the two applies, so local policy can tighten |
| | | 210 | | /// but never widen what was issued. |
| | | 211 | | /// </remarks> |
| | | 212 | | public int MaxUseCount { get; } |
| | | 213 | | |
| | | 214 | | /// <summary> |
| | | 215 | | /// Gets the signing key identifier the grant proof must use, when configured. |
| | | 216 | | /// </summary> |
| | | 217 | | public string? ExpectedProofKeyId { get; } |
| | | 218 | | |
| | | 219 | | /// <summary> |
| | | 220 | | /// Gets the signing key version the grant proof must use, when configured. |
| | | 221 | | /// </summary> |
| | | 222 | | public string? ExpectedProofKeyVersion { get; } |
| | | 223 | | |
| | | 224 | | /// <summary> |
| | | 225 | | /// Gets the policy version the grant proof must carry, when configured. |
| | | 226 | | /// </summary> |
| | | 227 | | public string? ExpectedProofPolicyVersion { get; } |
| | | 228 | | |
| | | 229 | | /// <summary> |
| | | 230 | | /// Gets the policy hash the grant proof must carry, when configured. |
| | | 231 | | /// </summary> |
| | | 232 | | public string? ExpectedProofPolicyHash { get; } |
| | | 233 | | |
| | | 234 | | /// <summary> |
| | | 235 | | /// Gets the signing provider that must have produced the grant proof, when configured. |
| | | 236 | | /// </summary> |
| | | 237 | | public string? RequiredProofProvider { get; } |
| | | 238 | | |
| | | 239 | | /// <summary> |
| | | 240 | | /// Gets the hash algorithm the grant proof must use, when configured. |
| | | 241 | | /// </summary> |
| | | 242 | | public string? RequiredProofHashAlgorithm { get; } |
| | | 243 | | |
| | | 244 | | /// <summary> |
| | | 245 | | /// Gets the canonical payload options used to rebuild the grant payload when proof is required. |
| | | 246 | | /// </summary> |
| | | 247 | | /// <remarks> |
| | | 248 | | /// Proof validation recomputes the canonical payload from the grant and compares its hash to the signed hash, so th |
| | | 249 | | /// options must match the options the issuer signed with. The default options bind every grant field except metadat |
| | | 250 | | /// whose allow-list is empty until a host opts a key in. |
| | | 251 | | /// </remarks> |
| | | 252 | | public CanonicalPayloadOptions? ProofPayloadOptions { get; } |
| | | 253 | | |
| | | 254 | | /// <summary> |
| | | 255 | | /// Creates a custom validation profile from the supplied expectations. |
| | | 256 | | /// </summary> |
| | | 257 | | /// <remarks> |
| | | 258 | | /// This factory does not bind the grant to an authenticated subject. At an execution boundary, prefer |
| | | 259 | | /// <see cref="CreateBoundExecutionBoundary" />, or call <see cref="WithExpectedBindings" /> on the result. |
| | | 260 | | /// </remarks> |
| | | 261 | | /// <param name="issuer">The issuer the grant must name, or <see langword="null" /> to skip the issuer check.</param |
| | | 262 | | /// <param name="audience">The audience the grant must name; required when proof or a use check is required.</param> |
| | | 263 | | /// <param name="scopes">The scopes the grant must contain; blank entries are ignored.</param> |
| | | 264 | | /// <param name="validationUtc">The instant used for the validity window; defaults to the current time.</param> |
| | | 265 | | /// <param name="policyVersion">The policy version the grant must carry, when set.</param> |
| | | 266 | | /// <param name="policyHash">The policy hash the grant must carry, when set.</param> |
| | | 267 | | /// <param name="acknowledgmentId">The acknowledgment identifier the grant must reference, when set.</param> |
| | | 268 | | /// <param name="handshakeId">The handshake identifier the grant must reference, when set.</param> |
| | | 269 | | /// <param name="gatewayBinding">The gateway binding the grant must carry, when set.</param> |
| | | 270 | | /// <param name="resourceBinding">The resource binding the grant must carry, when set.</param> |
| | | 271 | | /// <param name="requireProof">Whether the grant must carry verified proof bound to its content.</param> |
| | | 272 | | /// <param name="requireAcknowledgmentReference">Whether the grant must reference a handshake acknowledgment.</param |
| | | 273 | | /// <param name="requireUseCheck">Whether validation must consume a grant use through a use store.</param> |
| | | 274 | | /// <param name="maxUseCount">The maximum use count for this validation context; must be at least one.</param> |
| | | 275 | | /// <param name="allowedClockSkew">The non-negative tolerance applied to the validity window.</param> |
| | | 276 | | /// <param name="expectedProofKeyId">The signing key identifier the proof must use, when set.</param> |
| | | 277 | | /// <param name="expectedProofKeyVersion">The signing key version the proof must use, when set.</param> |
| | | 278 | | /// <param name="expectedProofPolicyVersion">The policy version the proof must carry, when set.</param> |
| | | 279 | | /// <param name="expectedProofPolicyHash">The policy hash the proof must carry, when set.</param> |
| | | 280 | | /// <param name="requiredProofProvider">The signing provider that must have produced the proof, when set.</param> |
| | | 281 | | /// <param name="requiredProofHashAlgorithm">The hash algorithm the proof must use, when set.</param> |
| | | 282 | | /// <param name="proofPayloadOptions">The canonical payload options the issuer signed with; defaults apply when <see |
| | | 283 | | /// <returns>The validation options.</returns> |
| | | 284 | | /// <exception cref="ArgumentOutOfRangeException"> |
| | | 285 | | /// <paramref name="allowedClockSkew" /> is negative, or <paramref name="maxUseCount" /> is less than one. |
| | | 286 | | /// </exception> |
| | | 287 | | /// <exception cref="ArgumentException"> |
| | | 288 | | /// <paramref name="requireProof" /> or <paramref name="requireUseCheck" /> is set without an <paramref name="audien |
| | | 289 | | /// </exception> |
| | | 290 | | public static CapabilityGrantValidationOptions Create( |
| | | 291 | | string? issuer = null, |
| | | 292 | | string? audience = null, |
| | | 293 | | IEnumerable<string>? scopes = null, |
| | | 294 | | DateTimeOffset? validationUtc = null, |
| | | 295 | | string? policyVersion = null, |
| | | 296 | | string? policyHash = null, |
| | | 297 | | string? acknowledgmentId = null, |
| | | 298 | | string? handshakeId = null, |
| | | 299 | | string? gatewayBinding = null, |
| | | 300 | | string? resourceBinding = null, |
| | | 301 | | bool requireProof = false, |
| | | 302 | | bool requireAcknowledgmentReference = false, |
| | | 303 | | bool requireUseCheck = false, |
| | | 304 | | int maxUseCount = 1, |
| | | 305 | | TimeSpan allowedClockSkew = default, |
| | | 306 | | string? expectedProofKeyId = null, |
| | | 307 | | string? expectedProofKeyVersion = null, |
| | | 308 | | string? expectedProofPolicyVersion = null, |
| | | 309 | | string? expectedProofPolicyHash = null, |
| | | 310 | | string? requiredProofProvider = null, |
| | | 311 | | string? requiredProofHashAlgorithm = null, |
| | | 312 | | CanonicalPayloadOptions? proofPayloadOptions = null) |
| | | 313 | | { |
| | 146 | 314 | | return new CapabilityGrantValidationOptions( |
| | 146 | 315 | | issuer, |
| | 146 | 316 | | audience, |
| | 146 | 317 | | NormalizeScopes(scopes), |
| | 146 | 318 | | validationUtc, |
| | 146 | 319 | | allowedClockSkew, |
| | 146 | 320 | | policyVersion, |
| | 146 | 321 | | policyHash, |
| | 146 | 322 | | acknowledgmentId, |
| | 146 | 323 | | handshakeId, |
| | 146 | 324 | | gatewayBinding, |
| | 146 | 325 | | resourceBinding, |
| | 146 | 326 | | expectedSubjectId: null, |
| | 146 | 327 | | expectedOperationName: null, |
| | 146 | 328 | | requireSubjectBinding: false, |
| | 146 | 329 | | requireProof, |
| | 146 | 330 | | requireAcknowledgmentReference, |
| | 146 | 331 | | requireUseCheck, |
| | 146 | 332 | | maxUseCount, |
| | 146 | 333 | | expectedProofKeyId, |
| | 146 | 334 | | expectedProofKeyVersion, |
| | 146 | 335 | | expectedProofPolicyVersion, |
| | 146 | 336 | | expectedProofPolicyHash, |
| | 146 | 337 | | requiredProofProvider, |
| | 146 | 338 | | requiredProofHashAlgorithm, |
| | 146 | 339 | | proofPayloadOptions); |
| | | 340 | | } |
| | | 341 | | |
| | | 342 | | /// <summary> |
| | | 343 | | /// Creates a copy with optional host expectations for the authenticated subject and requested operation. |
| | | 344 | | /// </summary> |
| | | 345 | | public CapabilityGrantValidationOptions WithExpectedBindings( |
| | | 346 | | string? expectedSubjectId = null, |
| | | 347 | | string? expectedOperationName = null) |
| | | 348 | | { |
| | 49 | 349 | | string? normalizedSubjectId = NormalizeOptional(expectedSubjectId); |
| | 49 | 350 | | if (RequireSubjectBinding && normalizedSubjectId is null) |
| | | 351 | | { |
| | 2 | 352 | | normalizedSubjectId = ExpectedSubjectId; |
| | | 353 | | } |
| | | 354 | | |
| | 49 | 355 | | return WithExpectedBindingsCore( |
| | 49 | 356 | | normalizedSubjectId, |
| | 49 | 357 | | expectedOperationName, |
| | 49 | 358 | | RequireSubjectBinding); |
| | | 359 | | } |
| | | 360 | | |
| | | 361 | | private CapabilityGrantValidationOptions WithExpectedBindingsCore( |
| | | 362 | | string? expectedSubjectId, |
| | | 363 | | string? expectedOperationName, |
| | | 364 | | bool requireSubjectBinding) |
| | | 365 | | { |
| | 113 | 366 | | return new CapabilityGrantValidationOptions( |
| | 113 | 367 | | Issuer, |
| | 113 | 368 | | Audience, |
| | 113 | 369 | | Scopes, |
| | 113 | 370 | | ValidationUtc, |
| | 113 | 371 | | AllowedClockSkew, |
| | 113 | 372 | | PolicyVersion, |
| | 113 | 373 | | PolicyHash, |
| | 113 | 374 | | AcknowledgmentId, |
| | 113 | 375 | | HandshakeId, |
| | 113 | 376 | | GatewayBinding, |
| | 113 | 377 | | ResourceBinding, |
| | 113 | 378 | | expectedSubjectId, |
| | 113 | 379 | | expectedOperationName, |
| | 113 | 380 | | requireSubjectBinding, |
| | 113 | 381 | | RequireProof, |
| | 113 | 382 | | RequireAcknowledgmentReference, |
| | 113 | 383 | | RequireUseCheck, |
| | 113 | 384 | | MaxUseCount, |
| | 113 | 385 | | ExpectedProofKeyId, |
| | 113 | 386 | | ExpectedProofKeyVersion, |
| | 113 | 387 | | ExpectedProofPolicyVersion, |
| | 113 | 388 | | ExpectedProofPolicyHash, |
| | 113 | 389 | | RequiredProofProvider, |
| | 113 | 390 | | RequiredProofHashAlgorithm, |
| | 113 | 391 | | ProofPayloadOptions); |
| | | 392 | | } |
| | | 393 | | |
| | | 394 | | /// <summary> |
| | | 395 | | /// Obsolete. Retains the legacy execution-boundary signature for binary compatibility and always fails closed. |
| | | 396 | | /// </summary> |
| | | 397 | | /// <remarks> |
| | | 398 | | /// This method is retained only for compatibility and always throws <see cref="InvalidOperationException" />. |
| | | 399 | | /// Replace calls with <c>CreateBoundExecutionBoundary(CapabilityGrantBindingExpectations, ...)</c>, whose required |
| | | 400 | | /// first argument supplies the authoritative subject and optional operation binding. The warning remains non-error |
| | | 401 | | /// in the 7.x line; removal is planned for the next permitted major version. |
| | | 402 | | /// </remarks> |
| | | 403 | | [Obsolete( |
| | | 404 | | "CreateExecutionBoundary always fails closed and is retained only for binary compatibility. Use CreateBoundExecu |
| | | 405 | | DiagnosticId = "ASIB901", |
| | | 406 | | UrlFormat = "https://asibackbone.github.io/AsiBackbone/articles/asib901-create-execution-boundary.html")] |
| | | 407 | | public static CapabilityGrantValidationOptions CreateExecutionBoundary( |
| | | 408 | | string? issuer = null, |
| | | 409 | | string? audience = null, |
| | | 410 | | IEnumerable<string>? scopes = null, |
| | | 411 | | DateTimeOffset? validationUtc = null, |
| | | 412 | | string? policyVersion = null, |
| | | 413 | | string? policyHash = null, |
| | | 414 | | string? acknowledgmentId = null, |
| | | 415 | | string? handshakeId = null, |
| | | 416 | | string? gatewayBinding = null, |
| | | 417 | | string? resourceBinding = null, |
| | | 418 | | bool requireAcknowledgmentReference = false, |
| | | 419 | | bool requireUseCheck = true, |
| | | 420 | | int maxUseCount = 1, |
| | | 421 | | TimeSpan allowedClockSkew = default, |
| | | 422 | | string? expectedProofKeyId = null, |
| | | 423 | | string? expectedProofKeyVersion = null, |
| | | 424 | | string? expectedProofPolicyVersion = null, |
| | | 425 | | string? expectedProofPolicyHash = null, |
| | | 426 | | string? requiredProofProvider = null, |
| | | 427 | | string? requiredProofHashAlgorithm = null, |
| | | 428 | | CanonicalPayloadOptions? proofPayloadOptions = null) |
| | | 429 | | { |
| | 2 | 430 | | throw new InvalidOperationException( |
| | 2 | 431 | | "Execution-boundary validation requires subject binding expectations. Use CreateBoundExecutionBoundary."); |
| | | 432 | | } |
| | | 433 | | |
| | | 434 | | /// <summary> |
| | | 435 | | /// Creates a proof-verifying execution-boundary profile with authoritative host binding expectations. |
| | | 436 | | /// </summary> |
| | | 437 | | public static CapabilityGrantValidationOptions CreateBoundExecutionBoundary( |
| | | 438 | | CapabilityGrantBindingExpectations bindingExpectations, |
| | | 439 | | string? issuer = null, |
| | | 440 | | string? audience = null, |
| | | 441 | | IEnumerable<string>? scopes = null, |
| | | 442 | | DateTimeOffset? validationUtc = null, |
| | | 443 | | string? policyVersion = null, |
| | | 444 | | string? policyHash = null, |
| | | 445 | | string? acknowledgmentId = null, |
| | | 446 | | string? handshakeId = null, |
| | | 447 | | string? gatewayBinding = null, |
| | | 448 | | string? resourceBinding = null, |
| | | 449 | | bool requireAcknowledgmentReference = false, |
| | | 450 | | bool requireUseCheck = true, |
| | | 451 | | int maxUseCount = 1, |
| | | 452 | | TimeSpan allowedClockSkew = default, |
| | | 453 | | string? expectedProofKeyId = null, |
| | | 454 | | string? expectedProofKeyVersion = null, |
| | | 455 | | string? expectedProofPolicyVersion = null, |
| | | 456 | | string? expectedProofPolicyHash = null, |
| | | 457 | | string? requiredProofProvider = null, |
| | | 458 | | string? requiredProofHashAlgorithm = null, |
| | | 459 | | CanonicalPayloadOptions? proofPayloadOptions = null) |
| | | 460 | | { |
| | 65 | 461 | | ArgumentNullException.ThrowIfNull(bindingExpectations); |
| | | 462 | | |
| | 64 | 463 | | return Create( |
| | 64 | 464 | | issuer: issuer, |
| | 64 | 465 | | audience: audience, |
| | 64 | 466 | | scopes: scopes, |
| | 64 | 467 | | validationUtc: validationUtc, |
| | 64 | 468 | | policyVersion: policyVersion, |
| | 64 | 469 | | policyHash: policyHash, |
| | 64 | 470 | | acknowledgmentId: acknowledgmentId, |
| | 64 | 471 | | handshakeId: handshakeId, |
| | 64 | 472 | | gatewayBinding: gatewayBinding, |
| | 64 | 473 | | resourceBinding: resourceBinding, |
| | 64 | 474 | | requireProof: true, |
| | 64 | 475 | | requireAcknowledgmentReference: requireAcknowledgmentReference, |
| | 64 | 476 | | requireUseCheck: requireUseCheck, |
| | 64 | 477 | | maxUseCount: maxUseCount, |
| | 64 | 478 | | allowedClockSkew: allowedClockSkew, |
| | 64 | 479 | | expectedProofKeyId: expectedProofKeyId, |
| | 64 | 480 | | expectedProofKeyVersion: expectedProofKeyVersion, |
| | 64 | 481 | | expectedProofPolicyVersion: expectedProofPolicyVersion, |
| | 64 | 482 | | expectedProofPolicyHash: expectedProofPolicyHash, |
| | 64 | 483 | | requiredProofProvider: requiredProofProvider, |
| | 64 | 484 | | requiredProofHashAlgorithm: requiredProofHashAlgorithm, |
| | 64 | 485 | | proofPayloadOptions: proofPayloadOptions) |
| | 64 | 486 | | .WithExpectedBindingsCore( |
| | 64 | 487 | | bindingExpectations.SubjectId, |
| | 64 | 488 | | bindingExpectations.OperationName, |
| | 64 | 489 | | requireSubjectBinding: true); |
| | | 490 | | } |
| | | 491 | | |
| | | 492 | | /// <summary> |
| | | 493 | | /// Creates a metadata-only profile that checks grant claims without requiring proof or consuming a use. |
| | | 494 | | /// </summary> |
| | | 495 | | /// <remarks> |
| | | 496 | | /// Because the grant signature is not verified, a valid result only means the grant claims match the expectations. |
| | | 497 | | /// Do not use this profile to authorize execution; use <see cref="CreateBoundExecutionBoundary" /> instead. |
| | | 498 | | /// </remarks> |
| | | 499 | | /// <param name="issuer">The issuer the grant must name, or <see langword="null" /> to skip the issuer check.</param |
| | | 500 | | /// <param name="audience">The audience the grant must name, or <see langword="null" /> to skip the audience check.< |
| | | 501 | | /// <param name="scopes">The scopes the grant must contain; blank entries are ignored.</param> |
| | | 502 | | /// <param name="validationUtc">The instant used for the validity window; defaults to the current time.</param> |
| | | 503 | | /// <param name="policyVersion">The policy version the grant must carry, when set.</param> |
| | | 504 | | /// <param name="policyHash">The policy hash the grant must carry, when set.</param> |
| | | 505 | | /// <param name="acknowledgmentId">The acknowledgment identifier the grant must reference, when set.</param> |
| | | 506 | | /// <param name="handshakeId">The handshake identifier the grant must reference, when set.</param> |
| | | 507 | | /// <param name="gatewayBinding">The gateway binding the grant must carry, when set.</param> |
| | | 508 | | /// <param name="resourceBinding">The resource binding the grant must carry, when set.</param> |
| | | 509 | | /// <param name="requireAcknowledgmentReference">Whether the grant must reference a handshake acknowledgment.</param |
| | | 510 | | /// <param name="allowedClockSkew">The non-negative tolerance applied to the validity window.</param> |
| | | 511 | | /// <returns>The validation options.</returns> |
| | | 512 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="allowedClockSkew" /> is negative.</exception> |
| | | 513 | | public static CapabilityGrantValidationOptions CreateMetadataValidation( |
| | | 514 | | string? issuer = null, |
| | | 515 | | string? audience = null, |
| | | 516 | | IEnumerable<string>? scopes = null, |
| | | 517 | | DateTimeOffset? validationUtc = null, |
| | | 518 | | string? policyVersion = null, |
| | | 519 | | string? policyHash = null, |
| | | 520 | | string? acknowledgmentId = null, |
| | | 521 | | string? handshakeId = null, |
| | | 522 | | string? gatewayBinding = null, |
| | | 523 | | string? resourceBinding = null, |
| | | 524 | | bool requireAcknowledgmentReference = false, |
| | | 525 | | TimeSpan allowedClockSkew = default) |
| | | 526 | | { |
| | 3 | 527 | | return Create( |
| | 3 | 528 | | issuer: issuer, |
| | 3 | 529 | | audience: audience, |
| | 3 | 530 | | scopes: scopes, |
| | 3 | 531 | | validationUtc: validationUtc, |
| | 3 | 532 | | policyVersion: policyVersion, |
| | 3 | 533 | | policyHash: policyHash, |
| | 3 | 534 | | acknowledgmentId: acknowledgmentId, |
| | 3 | 535 | | handshakeId: handshakeId, |
| | 3 | 536 | | gatewayBinding: gatewayBinding, |
| | 3 | 537 | | resourceBinding: resourceBinding, |
| | 3 | 538 | | requireProof: false, |
| | 3 | 539 | | requireAcknowledgmentReference: requireAcknowledgmentReference, |
| | 3 | 540 | | requireUseCheck: false, |
| | 3 | 541 | | maxUseCount: 1, |
| | 3 | 542 | | allowedClockSkew: allowedClockSkew); |
| | | 543 | | } |
| | | 544 | | |
| | | 545 | | private static IReadOnlyList<string> NormalizeScopes(IEnumerable<string>? scopes) |
| | | 546 | | { |
| | 146 | 547 | | if (scopes is null) |
| | | 548 | | { |
| | 23 | 549 | | return EmptyScopes; |
| | | 550 | | } |
| | | 551 | | |
| | 123 | 552 | | string[] normalized = [.. scopes |
| | 123 | 553 | | .Where(scope => !string.IsNullOrWhiteSpace(scope)) |
| | 123 | 554 | | .Select(scope => scope.Trim()) |
| | 123 | 555 | | .Distinct(StringComparer.Ordinal) |
| | 123 | 556 | | .OrderBy(scope => scope, StringComparer.Ordinal)]; |
| | | 557 | | |
| | 123 | 558 | | return normalized.Length == 0 ? EmptyScopes : Array.AsReadOnly(normalized); |
| | | 559 | | } |
| | | 560 | | |
| | | 561 | | private static string? NormalizeOptional(string? value) |
| | | 562 | | { |
| | 4113 | 563 | | return string.IsNullOrWhiteSpace(value) ? null : value.Trim(); |
| | | 564 | | } |
| | | 565 | | |
| | | 566 | | private bool RequireSubjectBinding { get; } |
| | | 567 | | } |