| | | 1 | | namespace AsiBackbone.Core.CapabilityTokens; |
| | | 2 | | |
| | | 3 | | public sealed class CapabilityGrantValidationOptions |
| | | 4 | | { |
| | 2 | 5 | | private static readonly string[] EmptyScopes = []; |
| | | 6 | | |
| | 120 | 7 | | private CapabilityGrantValidationOptions( |
| | 120 | 8 | | string? issuer, |
| | 120 | 9 | | string? audience, |
| | 120 | 10 | | IReadOnlyList<string> scopes, |
| | 120 | 11 | | DateTimeOffset? validationUtc, |
| | 120 | 12 | | TimeSpan allowedClockSkew, |
| | 120 | 13 | | string? policyVersion, |
| | 120 | 14 | | string? policyHash, |
| | 120 | 15 | | string? acknowledgmentId, |
| | 120 | 16 | | string? handshakeId, |
| | 120 | 17 | | string? gatewayBinding, |
| | 120 | 18 | | string? resourceBinding, |
| | 120 | 19 | | bool requireProof, |
| | 120 | 20 | | bool requireAcknowledgmentReference, |
| | 120 | 21 | | bool requireUseCheck, |
| | 120 | 22 | | int maxUseCount, |
| | 120 | 23 | | string? expectedProofKeyId, |
| | 120 | 24 | | string? expectedProofKeyVersion, |
| | 120 | 25 | | string? expectedProofPolicyVersion, |
| | 120 | 26 | | string? expectedProofPolicyHash, |
| | 120 | 27 | | string? requiredProofProvider, |
| | 120 | 28 | | string? requiredProofHashAlgorithm) |
| | | 29 | | { |
| | 120 | 30 | | if (allowedClockSkew < TimeSpan.Zero) |
| | | 31 | | { |
| | 2 | 32 | | throw new ArgumentOutOfRangeException( |
| | 2 | 33 | | nameof(allowedClockSkew), |
| | 2 | 34 | | allowedClockSkew, |
| | 2 | 35 | | "Allowed clock skew must be greater than or equal to zero."); |
| | | 36 | | } |
| | | 37 | | |
| | 118 | 38 | | if (maxUseCount < 1) |
| | | 39 | | { |
| | 2 | 40 | | throw new ArgumentOutOfRangeException(nameof(maxUseCount), maxUseCount, "Maximum use count must be greater t |
| | | 41 | | } |
| | | 42 | | |
| | 116 | 43 | | Issuer = NormalizeOptional(issuer); |
| | 116 | 44 | | Audience = NormalizeOptional(audience); |
| | 116 | 45 | | Scopes = scopes; |
| | 116 | 46 | | ValidationUtc = validationUtc?.ToUniversalTime(); |
| | 116 | 47 | | AllowedClockSkew = allowedClockSkew; |
| | 116 | 48 | | PolicyVersion = NormalizeOptional(policyVersion); |
| | 116 | 49 | | PolicyHash = NormalizeOptional(policyHash); |
| | 116 | 50 | | AcknowledgmentId = NormalizeOptional(acknowledgmentId); |
| | 116 | 51 | | HandshakeId = NormalizeOptional(handshakeId); |
| | 116 | 52 | | GatewayBinding = NormalizeOptional(gatewayBinding); |
| | 116 | 53 | | ResourceBinding = NormalizeOptional(resourceBinding); |
| | 116 | 54 | | RequireProof = requireProof; |
| | 116 | 55 | | RequireAcknowledgmentReference = requireAcknowledgmentReference; |
| | 116 | 56 | | RequireUseCheck = requireUseCheck; |
| | 116 | 57 | | MaxUseCount = maxUseCount; |
| | 116 | 58 | | ExpectedProofKeyId = NormalizeOptional(expectedProofKeyId); |
| | 116 | 59 | | ExpectedProofKeyVersion = NormalizeOptional(expectedProofKeyVersion); |
| | 116 | 60 | | ExpectedProofPolicyVersion = NormalizeOptional(expectedProofPolicyVersion); |
| | 116 | 61 | | ExpectedProofPolicyHash = NormalizeOptional(expectedProofPolicyHash); |
| | 116 | 62 | | RequiredProofProvider = NormalizeOptional(requiredProofProvider); |
| | 116 | 63 | | RequiredProofHashAlgorithm = NormalizeOptional(requiredProofHashAlgorithm); |
| | 116 | 64 | | } |
| | | 65 | | |
| | 148 | 66 | | public string? Issuer { get; } |
| | 144 | 67 | | public string? Audience { get; } |
| | 114 | 68 | | public IReadOnlyList<string> Scopes { get; } |
| | 106 | 69 | | public DateTimeOffset? ValidationUtc { get; } |
| | 138 | 70 | | public TimeSpan AllowedClockSkew { get; } |
| | 96 | 71 | | public string? PolicyVersion { get; } |
| | 92 | 72 | | public string? PolicyHash { get; } |
| | 78 | 73 | | public string? AcknowledgmentId { get; } |
| | 74 | 74 | | public string? HandshakeId { get; } |
| | 70 | 75 | | public string? GatewayBinding { get; } |
| | 66 | 76 | | public string? ResourceBinding { get; } |
| | 110 | 77 | | public bool RequireProof { get; } |
| | 56 | 78 | | public bool RequireAcknowledgmentReference { get; } |
| | 48 | 79 | | public bool RequireUseCheck { get; } |
| | 30 | 80 | | public int MaxUseCount { get; } |
| | 28 | 81 | | public string? ExpectedProofKeyId { get; } |
| | 28 | 82 | | public string? ExpectedProofKeyVersion { get; } |
| | 28 | 83 | | public string? ExpectedProofPolicyVersion { get; } |
| | 28 | 84 | | public string? ExpectedProofPolicyHash { get; } |
| | 30 | 85 | | public string? RequiredProofProvider { get; } |
| | 28 | 86 | | public string? RequiredProofHashAlgorithm { get; } |
| | | 87 | | |
| | | 88 | | public static CapabilityGrantValidationOptions Create( |
| | | 89 | | string? issuer = null, |
| | | 90 | | string? audience = null, |
| | | 91 | | IEnumerable<string>? scopes = null, |
| | | 92 | | DateTimeOffset? validationUtc = null, |
| | | 93 | | string? policyVersion = null, |
| | | 94 | | string? policyHash = null, |
| | | 95 | | string? acknowledgmentId = null, |
| | | 96 | | string? handshakeId = null, |
| | | 97 | | string? gatewayBinding = null, |
| | | 98 | | string? resourceBinding = null, |
| | | 99 | | bool requireProof = false, |
| | | 100 | | bool requireAcknowledgmentReference = false, |
| | | 101 | | bool requireUseCheck = false, |
| | | 102 | | int maxUseCount = 1, |
| | | 103 | | TimeSpan allowedClockSkew = default, |
| | | 104 | | string? expectedProofKeyId = null, |
| | | 105 | | string? expectedProofKeyVersion = null, |
| | | 106 | | string? expectedProofPolicyVersion = null, |
| | | 107 | | string? expectedProofPolicyHash = null, |
| | | 108 | | string? requiredProofProvider = null, |
| | | 109 | | string? requiredProofHashAlgorithm = null) |
| | | 110 | | { |
| | 120 | 111 | | return new CapabilityGrantValidationOptions( |
| | 120 | 112 | | issuer, |
| | 120 | 113 | | audience, |
| | 120 | 114 | | NormalizeScopes(scopes), |
| | 120 | 115 | | validationUtc, |
| | 120 | 116 | | allowedClockSkew, |
| | 120 | 117 | | policyVersion, |
| | 120 | 118 | | policyHash, |
| | 120 | 119 | | acknowledgmentId, |
| | 120 | 120 | | handshakeId, |
| | 120 | 121 | | gatewayBinding, |
| | 120 | 122 | | resourceBinding, |
| | 120 | 123 | | requireProof, |
| | 120 | 124 | | requireAcknowledgmentReference, |
| | 120 | 125 | | requireUseCheck, |
| | 120 | 126 | | maxUseCount, |
| | 120 | 127 | | expectedProofKeyId, |
| | 120 | 128 | | expectedProofKeyVersion, |
| | 120 | 129 | | expectedProofPolicyVersion, |
| | 120 | 130 | | expectedProofPolicyHash, |
| | 120 | 131 | | requiredProofProvider, |
| | 120 | 132 | | requiredProofHashAlgorithm); |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | public static CapabilityGrantValidationOptions CreateExecutionBoundary( |
| | | 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 requireAcknowledgmentReference = false, |
| | | 147 | | bool requireUseCheck = true, |
| | | 148 | | int maxUseCount = 1, |
| | | 149 | | TimeSpan allowedClockSkew = default, |
| | | 150 | | string? expectedProofKeyId = null, |
| | | 151 | | string? expectedProofKeyVersion = null, |
| | | 152 | | string? expectedProofPolicyVersion = null, |
| | | 153 | | string? expectedProofPolicyHash = null, |
| | | 154 | | string? requiredProofProvider = null, |
| | | 155 | | string? requiredProofHashAlgorithm = null) |
| | | 156 | | { |
| | 8 | 157 | | return Create( |
| | 8 | 158 | | issuer: issuer, |
| | 8 | 159 | | audience: audience, |
| | 8 | 160 | | scopes: scopes, |
| | 8 | 161 | | validationUtc: validationUtc, |
| | 8 | 162 | | policyVersion: policyVersion, |
| | 8 | 163 | | policyHash: policyHash, |
| | 8 | 164 | | acknowledgmentId: acknowledgmentId, |
| | 8 | 165 | | handshakeId: handshakeId, |
| | 8 | 166 | | gatewayBinding: gatewayBinding, |
| | 8 | 167 | | resourceBinding: resourceBinding, |
| | 8 | 168 | | requireProof: true, |
| | 8 | 169 | | requireAcknowledgmentReference: requireAcknowledgmentReference, |
| | 8 | 170 | | requireUseCheck: requireUseCheck, |
| | 8 | 171 | | maxUseCount: maxUseCount, |
| | 8 | 172 | | allowedClockSkew: allowedClockSkew, |
| | 8 | 173 | | expectedProofKeyId: expectedProofKeyId, |
| | 8 | 174 | | expectedProofKeyVersion: expectedProofKeyVersion, |
| | 8 | 175 | | expectedProofPolicyVersion: expectedProofPolicyVersion, |
| | 8 | 176 | | expectedProofPolicyHash: expectedProofPolicyHash, |
| | 8 | 177 | | requiredProofProvider: requiredProofProvider, |
| | 8 | 178 | | requiredProofHashAlgorithm: requiredProofHashAlgorithm); |
| | | 179 | | } |
| | | 180 | | |
| | | 181 | | public static CapabilityGrantValidationOptions CreateMetadataValidation( |
| | | 182 | | string? issuer = null, |
| | | 183 | | string? audience = null, |
| | | 184 | | IEnumerable<string>? scopes = null, |
| | | 185 | | DateTimeOffset? validationUtc = null, |
| | | 186 | | string? policyVersion = null, |
| | | 187 | | string? policyHash = null, |
| | | 188 | | string? acknowledgmentId = null, |
| | | 189 | | string? handshakeId = null, |
| | | 190 | | string? gatewayBinding = null, |
| | | 191 | | string? resourceBinding = null, |
| | | 192 | | bool requireAcknowledgmentReference = false, |
| | | 193 | | TimeSpan allowedClockSkew = default) |
| | | 194 | | { |
| | 4 | 195 | | return Create( |
| | 4 | 196 | | issuer: issuer, |
| | 4 | 197 | | audience: audience, |
| | 4 | 198 | | scopes: scopes, |
| | 4 | 199 | | validationUtc: validationUtc, |
| | 4 | 200 | | policyVersion: policyVersion, |
| | 4 | 201 | | policyHash: policyHash, |
| | 4 | 202 | | acknowledgmentId: acknowledgmentId, |
| | 4 | 203 | | handshakeId: handshakeId, |
| | 4 | 204 | | gatewayBinding: gatewayBinding, |
| | 4 | 205 | | resourceBinding: resourceBinding, |
| | 4 | 206 | | requireProof: false, |
| | 4 | 207 | | requireAcknowledgmentReference: requireAcknowledgmentReference, |
| | 4 | 208 | | requireUseCheck: false, |
| | 4 | 209 | | maxUseCount: 1, |
| | 4 | 210 | | allowedClockSkew: allowedClockSkew); |
| | | 211 | | } |
| | | 212 | | |
| | | 213 | | private static IReadOnlyList<string> NormalizeScopes(IEnumerable<string>? scopes) |
| | | 214 | | { |
| | 120 | 215 | | if (scopes is null) |
| | | 216 | | { |
| | 22 | 217 | | return EmptyScopes; |
| | | 218 | | } |
| | | 219 | | |
| | 98 | 220 | | string[] normalized = [.. scopes |
| | 112 | 221 | | .Where(scope => !string.IsNullOrWhiteSpace(scope)) |
| | 106 | 222 | | .Select(scope => scope.Trim()) |
| | 98 | 223 | | .Distinct(StringComparer.Ordinal) |
| | 110 | 224 | | .OrderBy(scope => scope, StringComparer.Ordinal)]; |
| | | 225 | | |
| | 98 | 226 | | return normalized.Length == 0 ? EmptyScopes : Array.AsReadOnly(normalized); |
| | | 227 | | } |
| | | 228 | | |
| | | 229 | | private static string? NormalizeOptional(string? value) |
| | | 230 | | { |
| | 1624 | 231 | | return string.IsNullOrWhiteSpace(value) ? null : value.Trim(); |
| | | 232 | | } |
| | | 233 | | } |