| | | 1 | | using AsiBackbone.Core.Signing; |
| | | 2 | | |
| | | 3 | | namespace AsiBackbone.Core.CapabilityGrants; |
| | | 4 | | |
| | | 5 | | public sealed class CapabilityGrantValidationOptions |
| | | 6 | | { |
| | 1 | 7 | | private static readonly string[] EmptyScopes = []; |
| | | 8 | | |
| | 141 | 9 | | private CapabilityGrantValidationOptions( |
| | 141 | 10 | | string? issuer, |
| | 141 | 11 | | string? audience, |
| | 141 | 12 | | IReadOnlyList<string> scopes, |
| | 141 | 13 | | DateTimeOffset? validationUtc, |
| | 141 | 14 | | TimeSpan allowedClockSkew, |
| | 141 | 15 | | string? policyVersion, |
| | 141 | 16 | | string? policyHash, |
| | 141 | 17 | | string? acknowledgmentId, |
| | 141 | 18 | | string? handshakeId, |
| | 141 | 19 | | string? gatewayBinding, |
| | 141 | 20 | | string? resourceBinding, |
| | 141 | 21 | | string? expectedSubjectId, |
| | 141 | 22 | | string? expectedOperationName, |
| | 141 | 23 | | bool requireSubjectBinding, |
| | 141 | 24 | | bool requireProof, |
| | 141 | 25 | | bool requireAcknowledgmentReference, |
| | 141 | 26 | | bool requireUseCheck, |
| | 141 | 27 | | int maxUseCount, |
| | 141 | 28 | | string? expectedProofKeyId, |
| | 141 | 29 | | string? expectedProofKeyVersion, |
| | 141 | 30 | | string? expectedProofPolicyVersion, |
| | 141 | 31 | | string? expectedProofPolicyHash, |
| | 141 | 32 | | string? requiredProofProvider, |
| | 141 | 33 | | string? requiredProofHashAlgorithm, |
| | 141 | 34 | | CanonicalPayloadOptions? proofPayloadOptions) |
| | | 35 | | { |
| | 141 | 36 | | if (allowedClockSkew < TimeSpan.Zero) |
| | | 37 | | { |
| | 1 | 38 | | throw new ArgumentOutOfRangeException( |
| | 1 | 39 | | nameof(allowedClockSkew), |
| | 1 | 40 | | allowedClockSkew, |
| | 1 | 41 | | "Allowed clock skew must be greater than or equal to zero."); |
| | | 42 | | } |
| | | 43 | | |
| | 140 | 44 | | if (maxUseCount < 1) |
| | | 45 | | { |
| | 1 | 46 | | throw new ArgumentOutOfRangeException(nameof(maxUseCount), maxUseCount, "Maximum use count must be greater t |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | // A grant can be proof-verified and still be the wrong grant for this caller. Without an audience expectation a |
| | | 50 | | // grant issued for one gateway validates at another, so requiring proof or a use check without stating the |
| | | 51 | | // audience is a configuration that cannot deliver what it appears to promise. |
| | 139 | 52 | | if ((requireProof || requireUseCheck) && string.IsNullOrWhiteSpace(audience)) |
| | | 53 | | { |
| | 3 | 54 | | throw new ArgumentException( |
| | 3 | 55 | | "An audience expectation is required when proof or use checking is required, because proof alone does no |
| | 3 | 56 | | nameof(audience)); |
| | | 57 | | } |
| | | 58 | | |
| | 136 | 59 | | Issuer = NormalizeOptional(issuer); |
| | 136 | 60 | | Audience = NormalizeOptional(audience); |
| | 136 | 61 | | Scopes = scopes; |
| | 136 | 62 | | ValidationUtc = validationUtc?.ToUniversalTime(); |
| | 136 | 63 | | AllowedClockSkew = allowedClockSkew; |
| | 136 | 64 | | PolicyVersion = NormalizeOptional(policyVersion); |
| | 136 | 65 | | PolicyHash = NormalizeOptional(policyHash); |
| | 136 | 66 | | AcknowledgmentId = NormalizeOptional(acknowledgmentId); |
| | 136 | 67 | | HandshakeId = NormalizeOptional(handshakeId); |
| | 136 | 68 | | GatewayBinding = NormalizeOptional(gatewayBinding); |
| | 136 | 69 | | ResourceBinding = NormalizeOptional(resourceBinding); |
| | 136 | 70 | | ExpectedSubjectId = NormalizeOptional(expectedSubjectId); |
| | 136 | 71 | | if (requireSubjectBinding && ExpectedSubjectId is null) |
| | | 72 | | { |
| | 0 | 73 | | throw new ArgumentException( |
| | 0 | 74 | | "A subject expectation is required for this validation profile.", |
| | 0 | 75 | | nameof(expectedSubjectId)); |
| | | 76 | | } |
| | | 77 | | |
| | 136 | 78 | | ExpectedOperationName = NormalizeOptional(expectedOperationName); |
| | 136 | 79 | | RequireSubjectBinding = requireSubjectBinding; |
| | 136 | 80 | | RequireProof = requireProof; |
| | 136 | 81 | | RequireAcknowledgmentReference = requireAcknowledgmentReference; |
| | 136 | 82 | | RequireUseCheck = requireUseCheck; |
| | 136 | 83 | | MaxUseCount = maxUseCount; |
| | 136 | 84 | | ExpectedProofKeyId = NormalizeOptional(expectedProofKeyId); |
| | 136 | 85 | | ExpectedProofKeyVersion = NormalizeOptional(expectedProofKeyVersion); |
| | 136 | 86 | | ExpectedProofPolicyVersion = NormalizeOptional(expectedProofPolicyVersion); |
| | 136 | 87 | | ExpectedProofPolicyHash = NormalizeOptional(expectedProofPolicyHash); |
| | 136 | 88 | | RequiredProofProvider = NormalizeOptional(requiredProofProvider); |
| | 136 | 89 | | RequiredProofHashAlgorithm = NormalizeOptional(requiredProofHashAlgorithm); |
| | 136 | 90 | | ProofPayloadOptions = proofPayloadOptions; |
| | 136 | 91 | | } |
| | | 92 | | |
| | | 93 | | public string? Issuer { get; } |
| | | 94 | | public string? Audience { get; } |
| | | 95 | | public IReadOnlyList<string> Scopes { get; } |
| | | 96 | | public DateTimeOffset? ValidationUtc { get; } |
| | | 97 | | public TimeSpan AllowedClockSkew { get; } |
| | | 98 | | public string? PolicyVersion { get; } |
| | | 99 | | public string? PolicyHash { get; } |
| | | 100 | | public string? AcknowledgmentId { get; } |
| | | 101 | | public string? HandshakeId { get; } |
| | | 102 | | public string? GatewayBinding { get; } |
| | | 103 | | public string? ResourceBinding { get; } |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Gets the authenticated subject that the grant must identify, when subject binding is configured. |
| | | 107 | | /// </summary> |
| | | 108 | | public string? ExpectedSubjectId { get; } |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Gets the requested operation that the grant must identify, when operation binding is configured. |
| | | 112 | | /// </summary> |
| | | 113 | | public string? ExpectedOperationName { get; } |
| | | 114 | | public bool RequireProof { get; } |
| | | 115 | | public bool RequireAcknowledgmentReference { get; } |
| | | 116 | | public bool RequireUseCheck { get; } |
| | | 117 | | public int MaxUseCount { get; } |
| | | 118 | | public string? ExpectedProofKeyId { get; } |
| | | 119 | | public string? ExpectedProofKeyVersion { get; } |
| | | 120 | | public string? ExpectedProofPolicyVersion { get; } |
| | | 121 | | public string? ExpectedProofPolicyHash { get; } |
| | | 122 | | public string? RequiredProofProvider { get; } |
| | | 123 | | public string? RequiredProofHashAlgorithm { get; } |
| | | 124 | | |
| | | 125 | | /// <summary> |
| | | 126 | | /// Gets the canonical payload options used to rebuild the grant payload when proof is required. |
| | | 127 | | /// </summary> |
| | | 128 | | /// <remarks> |
| | | 129 | | /// Proof validation recomputes the canonical payload from the grant and compares its hash to the signed hash, so th |
| | | 130 | | /// options must match the options the issuer signed with. The default options bind every grant field except metadat |
| | | 131 | | /// whose allow-list is empty until a host opts a key in. |
| | | 132 | | /// </remarks> |
| | | 133 | | public CanonicalPayloadOptions? ProofPayloadOptions { get; } |
| | | 134 | | |
| | | 135 | | public static CapabilityGrantValidationOptions Create( |
| | | 136 | | string? issuer = null, |
| | | 137 | | string? audience = null, |
| | | 138 | | IEnumerable<string>? scopes = null, |
| | | 139 | | DateTimeOffset? validationUtc = null, |
| | | 140 | | string? policyVersion = null, |
| | | 141 | | string? policyHash = null, |
| | | 142 | | string? acknowledgmentId = null, |
| | | 143 | | string? handshakeId = null, |
| | | 144 | | string? gatewayBinding = null, |
| | | 145 | | string? resourceBinding = null, |
| | | 146 | | bool requireProof = false, |
| | | 147 | | bool requireAcknowledgmentReference = false, |
| | | 148 | | bool requireUseCheck = false, |
| | | 149 | | int maxUseCount = 1, |
| | | 150 | | TimeSpan allowedClockSkew = default, |
| | | 151 | | string? expectedProofKeyId = null, |
| | | 152 | | string? expectedProofKeyVersion = null, |
| | | 153 | | string? expectedProofPolicyVersion = null, |
| | | 154 | | string? expectedProofPolicyHash = null, |
| | | 155 | | string? requiredProofProvider = null, |
| | | 156 | | string? requiredProofHashAlgorithm = null, |
| | | 157 | | CanonicalPayloadOptions? proofPayloadOptions = null) |
| | | 158 | | { |
| | 87 | 159 | | return new CapabilityGrantValidationOptions( |
| | 87 | 160 | | issuer, |
| | 87 | 161 | | audience, |
| | 87 | 162 | | NormalizeScopes(scopes), |
| | 87 | 163 | | validationUtc, |
| | 87 | 164 | | allowedClockSkew, |
| | 87 | 165 | | policyVersion, |
| | 87 | 166 | | policyHash, |
| | 87 | 167 | | acknowledgmentId, |
| | 87 | 168 | | handshakeId, |
| | 87 | 169 | | gatewayBinding, |
| | 87 | 170 | | resourceBinding, |
| | 87 | 171 | | expectedSubjectId: null, |
| | 87 | 172 | | expectedOperationName: null, |
| | 87 | 173 | | requireSubjectBinding: false, |
| | 87 | 174 | | requireProof, |
| | 87 | 175 | | requireAcknowledgmentReference, |
| | 87 | 176 | | requireUseCheck, |
| | 87 | 177 | | maxUseCount, |
| | 87 | 178 | | expectedProofKeyId, |
| | 87 | 179 | | expectedProofKeyVersion, |
| | 87 | 180 | | expectedProofPolicyVersion, |
| | 87 | 181 | | expectedProofPolicyHash, |
| | 87 | 182 | | requiredProofProvider, |
| | 87 | 183 | | requiredProofHashAlgorithm, |
| | 87 | 184 | | proofPayloadOptions); |
| | | 185 | | } |
| | | 186 | | |
| | | 187 | | /// <summary> |
| | | 188 | | /// Creates a copy with optional host expectations for the authenticated subject and requested operation. |
| | | 189 | | /// </summary> |
| | | 190 | | public CapabilityGrantValidationOptions WithExpectedBindings( |
| | | 191 | | string? expectedSubjectId = null, |
| | | 192 | | string? expectedOperationName = null) |
| | | 193 | | { |
| | 49 | 194 | | string? normalizedSubjectId = NormalizeOptional(expectedSubjectId); |
| | 49 | 195 | | if (RequireSubjectBinding && normalizedSubjectId is null) |
| | | 196 | | { |
| | 2 | 197 | | normalizedSubjectId = ExpectedSubjectId; |
| | | 198 | | } |
| | | 199 | | |
| | 49 | 200 | | return WithExpectedBindingsCore( |
| | 49 | 201 | | normalizedSubjectId, |
| | 49 | 202 | | expectedOperationName, |
| | 49 | 203 | | RequireSubjectBinding); |
| | | 204 | | } |
| | | 205 | | |
| | | 206 | | private CapabilityGrantValidationOptions WithExpectedBindingsCore( |
| | | 207 | | string? expectedSubjectId, |
| | | 208 | | string? expectedOperationName, |
| | | 209 | | bool requireSubjectBinding) |
| | | 210 | | { |
| | 54 | 211 | | return new CapabilityGrantValidationOptions( |
| | 54 | 212 | | Issuer, |
| | 54 | 213 | | Audience, |
| | 54 | 214 | | Scopes, |
| | 54 | 215 | | ValidationUtc, |
| | 54 | 216 | | AllowedClockSkew, |
| | 54 | 217 | | PolicyVersion, |
| | 54 | 218 | | PolicyHash, |
| | 54 | 219 | | AcknowledgmentId, |
| | 54 | 220 | | HandshakeId, |
| | 54 | 221 | | GatewayBinding, |
| | 54 | 222 | | ResourceBinding, |
| | 54 | 223 | | expectedSubjectId, |
| | 54 | 224 | | expectedOperationName, |
| | 54 | 225 | | requireSubjectBinding, |
| | 54 | 226 | | RequireProof, |
| | 54 | 227 | | RequireAcknowledgmentReference, |
| | 54 | 228 | | RequireUseCheck, |
| | 54 | 229 | | MaxUseCount, |
| | 54 | 230 | | ExpectedProofKeyId, |
| | 54 | 231 | | ExpectedProofKeyVersion, |
| | 54 | 232 | | ExpectedProofPolicyVersion, |
| | 54 | 233 | | ExpectedProofPolicyHash, |
| | 54 | 234 | | RequiredProofProvider, |
| | 54 | 235 | | RequiredProofHashAlgorithm, |
| | 54 | 236 | | ProofPayloadOptions); |
| | | 237 | | } |
| | | 238 | | |
| | | 239 | | /// <summary> |
| | | 240 | | /// Obsolete. Retains the legacy execution-boundary signature for binary compatibility and always fails closed. |
| | | 241 | | /// </summary> |
| | | 242 | | /// <remarks> |
| | | 243 | | /// This method is retained only for compatibility and always throws <see cref="InvalidOperationException" />. |
| | | 244 | | /// Replace calls with <c>CreateBoundExecutionBoundary(CapabilityGrantBindingExpectations, ...)</c>, whose required |
| | | 245 | | /// first argument supplies the authoritative subject and optional operation binding. The warning remains non-error |
| | | 246 | | /// in the 7.x line; removal is planned for the next permitted major version. |
| | | 247 | | /// </remarks> |
| | | 248 | | [Obsolete( |
| | | 249 | | "CreateExecutionBoundary always fails closed and is retained only for binary compatibility. Use CreateBoundExecu |
| | | 250 | | DiagnosticId = "ASIB901", |
| | | 251 | | UrlFormat = "https://asibackbone.github.io/AsiBackbone/articles/asib901-create-execution-boundary.html")] |
| | | 252 | | public static CapabilityGrantValidationOptions CreateExecutionBoundary( |
| | | 253 | | string? issuer = null, |
| | | 254 | | string? audience = null, |
| | | 255 | | IEnumerable<string>? scopes = null, |
| | | 256 | | DateTimeOffset? validationUtc = null, |
| | | 257 | | string? policyVersion = null, |
| | | 258 | | string? policyHash = null, |
| | | 259 | | string? acknowledgmentId = null, |
| | | 260 | | string? handshakeId = null, |
| | | 261 | | string? gatewayBinding = null, |
| | | 262 | | string? resourceBinding = null, |
| | | 263 | | bool requireAcknowledgmentReference = false, |
| | | 264 | | bool requireUseCheck = true, |
| | | 265 | | int maxUseCount = 1, |
| | | 266 | | TimeSpan allowedClockSkew = default, |
| | | 267 | | string? expectedProofKeyId = null, |
| | | 268 | | string? expectedProofKeyVersion = null, |
| | | 269 | | string? expectedProofPolicyVersion = null, |
| | | 270 | | string? expectedProofPolicyHash = null, |
| | | 271 | | string? requiredProofProvider = null, |
| | | 272 | | string? requiredProofHashAlgorithm = null, |
| | | 273 | | CanonicalPayloadOptions? proofPayloadOptions = null) |
| | | 274 | | { |
| | 2 | 275 | | throw new InvalidOperationException( |
| | 2 | 276 | | "Execution-boundary validation requires subject binding expectations. Use CreateBoundExecutionBoundary."); |
| | | 277 | | } |
| | | 278 | | |
| | | 279 | | /// <summary> |
| | | 280 | | /// Creates a proof-verifying execution-boundary profile with authoritative host binding expectations. |
| | | 281 | | /// </summary> |
| | | 282 | | public static CapabilityGrantValidationOptions CreateBoundExecutionBoundary( |
| | | 283 | | CapabilityGrantBindingExpectations bindingExpectations, |
| | | 284 | | string? issuer = null, |
| | | 285 | | string? audience = null, |
| | | 286 | | IEnumerable<string>? scopes = null, |
| | | 287 | | DateTimeOffset? validationUtc = null, |
| | | 288 | | string? policyVersion = null, |
| | | 289 | | string? policyHash = null, |
| | | 290 | | string? acknowledgmentId = null, |
| | | 291 | | string? handshakeId = null, |
| | | 292 | | string? gatewayBinding = null, |
| | | 293 | | string? resourceBinding = null, |
| | | 294 | | bool requireAcknowledgmentReference = false, |
| | | 295 | | bool requireUseCheck = true, |
| | | 296 | | int maxUseCount = 1, |
| | | 297 | | TimeSpan allowedClockSkew = default, |
| | | 298 | | string? expectedProofKeyId = null, |
| | | 299 | | string? expectedProofKeyVersion = null, |
| | | 300 | | string? expectedProofPolicyVersion = null, |
| | | 301 | | string? expectedProofPolicyHash = null, |
| | | 302 | | string? requiredProofProvider = null, |
| | | 303 | | string? requiredProofHashAlgorithm = null, |
| | | 304 | | CanonicalPayloadOptions? proofPayloadOptions = null) |
| | | 305 | | { |
| | 6 | 306 | | ArgumentNullException.ThrowIfNull(bindingExpectations); |
| | | 307 | | |
| | 5 | 308 | | return Create( |
| | 5 | 309 | | issuer: issuer, |
| | 5 | 310 | | audience: audience, |
| | 5 | 311 | | scopes: scopes, |
| | 5 | 312 | | validationUtc: validationUtc, |
| | 5 | 313 | | policyVersion: policyVersion, |
| | 5 | 314 | | policyHash: policyHash, |
| | 5 | 315 | | acknowledgmentId: acknowledgmentId, |
| | 5 | 316 | | handshakeId: handshakeId, |
| | 5 | 317 | | gatewayBinding: gatewayBinding, |
| | 5 | 318 | | resourceBinding: resourceBinding, |
| | 5 | 319 | | requireProof: true, |
| | 5 | 320 | | requireAcknowledgmentReference: requireAcknowledgmentReference, |
| | 5 | 321 | | requireUseCheck: requireUseCheck, |
| | 5 | 322 | | maxUseCount: maxUseCount, |
| | 5 | 323 | | allowedClockSkew: allowedClockSkew, |
| | 5 | 324 | | expectedProofKeyId: expectedProofKeyId, |
| | 5 | 325 | | expectedProofKeyVersion: expectedProofKeyVersion, |
| | 5 | 326 | | expectedProofPolicyVersion: expectedProofPolicyVersion, |
| | 5 | 327 | | expectedProofPolicyHash: expectedProofPolicyHash, |
| | 5 | 328 | | requiredProofProvider: requiredProofProvider, |
| | 5 | 329 | | requiredProofHashAlgorithm: requiredProofHashAlgorithm, |
| | 5 | 330 | | proofPayloadOptions: proofPayloadOptions) |
| | 5 | 331 | | .WithExpectedBindingsCore( |
| | 5 | 332 | | bindingExpectations.SubjectId, |
| | 5 | 333 | | bindingExpectations.OperationName, |
| | 5 | 334 | | requireSubjectBinding: true); |
| | | 335 | | } |
| | | 336 | | |
| | | 337 | | public static CapabilityGrantValidationOptions CreateMetadataValidation( |
| | | 338 | | string? issuer = null, |
| | | 339 | | string? audience = null, |
| | | 340 | | IEnumerable<string>? scopes = null, |
| | | 341 | | DateTimeOffset? validationUtc = null, |
| | | 342 | | string? policyVersion = null, |
| | | 343 | | string? policyHash = null, |
| | | 344 | | string? acknowledgmentId = null, |
| | | 345 | | string? handshakeId = null, |
| | | 346 | | string? gatewayBinding = null, |
| | | 347 | | string? resourceBinding = null, |
| | | 348 | | bool requireAcknowledgmentReference = false, |
| | | 349 | | TimeSpan allowedClockSkew = default) |
| | | 350 | | { |
| | 3 | 351 | | return Create( |
| | 3 | 352 | | issuer: issuer, |
| | 3 | 353 | | audience: audience, |
| | 3 | 354 | | scopes: scopes, |
| | 3 | 355 | | validationUtc: validationUtc, |
| | 3 | 356 | | policyVersion: policyVersion, |
| | 3 | 357 | | policyHash: policyHash, |
| | 3 | 358 | | acknowledgmentId: acknowledgmentId, |
| | 3 | 359 | | handshakeId: handshakeId, |
| | 3 | 360 | | gatewayBinding: gatewayBinding, |
| | 3 | 361 | | resourceBinding: resourceBinding, |
| | 3 | 362 | | requireProof: false, |
| | 3 | 363 | | requireAcknowledgmentReference: requireAcknowledgmentReference, |
| | 3 | 364 | | requireUseCheck: false, |
| | 3 | 365 | | maxUseCount: 1, |
| | 3 | 366 | | allowedClockSkew: allowedClockSkew); |
| | | 367 | | } |
| | | 368 | | |
| | | 369 | | private static IReadOnlyList<string> NormalizeScopes(IEnumerable<string>? scopes) |
| | | 370 | | { |
| | 87 | 371 | | if (scopes is null) |
| | | 372 | | { |
| | 23 | 373 | | return EmptyScopes; |
| | | 374 | | } |
| | | 375 | | |
| | 64 | 376 | | string[] normalized = [.. scopes |
| | 64 | 377 | | .Where(scope => !string.IsNullOrWhiteSpace(scope)) |
| | 64 | 378 | | .Select(scope => scope.Trim()) |
| | 64 | 379 | | .Distinct(StringComparer.Ordinal) |
| | 64 | 380 | | .OrderBy(scope => scope, StringComparer.Ordinal)]; |
| | | 381 | | |
| | 64 | 382 | | return normalized.Length == 0 ? EmptyScopes : Array.AsReadOnly(normalized); |
| | | 383 | | } |
| | | 384 | | |
| | | 385 | | private static string? NormalizeOptional(string? value) |
| | | 386 | | { |
| | 2225 | 387 | | return string.IsNullOrWhiteSpace(value) ? null : value.Trim(); |
| | | 388 | | } |
| | | 389 | | |
| | | 390 | | private bool RequireSubjectBinding { get; } |
| | | 391 | | } |