| | | 1 | | namespace AsiBackbone.Core.CapabilityTokens; |
| | | 2 | | |
| | | 3 | | public sealed class CapabilityGrantValidationOptions |
| | | 4 | | { |
| | 1 | 5 | | private static readonly IReadOnlyList<string> EmptyScopes = Array.AsReadOnly(Array.Empty<string>()); |
| | | 6 | | |
| | 38 | 7 | | private CapabilityGrantValidationOptions( |
| | 38 | 8 | | string? issuer, |
| | 38 | 9 | | string? audience, |
| | 38 | 10 | | IReadOnlyList<string> scopes, |
| | 38 | 11 | | DateTimeOffset? validationUtc, |
| | 38 | 12 | | string? policyVersion, |
| | 38 | 13 | | string? policyHash, |
| | 38 | 14 | | string? acknowledgmentId, |
| | 38 | 15 | | string? handshakeId, |
| | 38 | 16 | | string? gatewayBinding, |
| | 38 | 17 | | string? resourceBinding, |
| | 38 | 18 | | bool requireProof, |
| | 38 | 19 | | bool requireAcknowledgmentReference, |
| | 38 | 20 | | bool requireUseCheck, |
| | 38 | 21 | | int maxUseCount) |
| | | 22 | | { |
| | 38 | 23 | | if (maxUseCount < 1) |
| | | 24 | | { |
| | 1 | 25 | | throw new ArgumentOutOfRangeException(nameof(maxUseCount), maxUseCount, "Maximum use count must be greater t |
| | | 26 | | } |
| | | 27 | | |
| | 37 | 28 | | Issuer = NormalizeOptional(issuer); |
| | 37 | 29 | | Audience = NormalizeOptional(audience); |
| | 37 | 30 | | Scopes = scopes; |
| | 37 | 31 | | ValidationUtc = validationUtc?.ToUniversalTime(); |
| | 37 | 32 | | PolicyVersion = NormalizeOptional(policyVersion); |
| | 37 | 33 | | PolicyHash = NormalizeOptional(policyHash); |
| | 37 | 34 | | AcknowledgmentId = NormalizeOptional(acknowledgmentId); |
| | 37 | 35 | | HandshakeId = NormalizeOptional(handshakeId); |
| | 37 | 36 | | GatewayBinding = NormalizeOptional(gatewayBinding); |
| | 37 | 37 | | ResourceBinding = NormalizeOptional(resourceBinding); |
| | 37 | 38 | | RequireProof = requireProof; |
| | 37 | 39 | | RequireAcknowledgmentReference = requireAcknowledgmentReference; |
| | 37 | 40 | | RequireUseCheck = requireUseCheck; |
| | 37 | 41 | | MaxUseCount = maxUseCount; |
| | 37 | 42 | | } |
| | | 43 | | |
| | 48 | 44 | | public string? Issuer { get; } |
| | 46 | 45 | | public string? Audience { get; } |
| | 41 | 46 | | public IReadOnlyList<string> Scopes { get; } |
| | 35 | 47 | | public DateTimeOffset? ValidationUtc { get; } |
| | 36 | 48 | | public string? PolicyVersion { get; } |
| | 34 | 49 | | public string? PolicyHash { get; } |
| | 30 | 50 | | public string? AcknowledgmentId { get; } |
| | 28 | 51 | | public string? HandshakeId { get; } |
| | 26 | 52 | | public string? GatewayBinding { get; } |
| | 24 | 53 | | public string? ResourceBinding { get; } |
| | 35 | 54 | | public bool RequireProof { get; } |
| | 17 | 55 | | public bool RequireAcknowledgmentReference { get; } |
| | 12 | 56 | | public bool RequireUseCheck { get; } |
| | 10 | 57 | | public int MaxUseCount { get; } |
| | | 58 | | |
| | | 59 | | public static CapabilityGrantValidationOptions Create( |
| | | 60 | | string? issuer = null, |
| | | 61 | | string? audience = null, |
| | | 62 | | IEnumerable<string>? scopes = null, |
| | | 63 | | DateTimeOffset? validationUtc = null, |
| | | 64 | | string? policyVersion = null, |
| | | 65 | | string? policyHash = null, |
| | | 66 | | string? acknowledgmentId = null, |
| | | 67 | | string? handshakeId = null, |
| | | 68 | | string? gatewayBinding = null, |
| | | 69 | | string? resourceBinding = null, |
| | | 70 | | bool requireProof = false, |
| | | 71 | | bool requireAcknowledgmentReference = false, |
| | | 72 | | bool requireUseCheck = false, |
| | | 73 | | int maxUseCount = 1) |
| | | 74 | | { |
| | 38 | 75 | | return new CapabilityGrantValidationOptions( |
| | 38 | 76 | | issuer, |
| | 38 | 77 | | audience, |
| | 38 | 78 | | NormalizeScopes(scopes), |
| | 38 | 79 | | validationUtc, |
| | 38 | 80 | | policyVersion, |
| | 38 | 81 | | policyHash, |
| | 38 | 82 | | acknowledgmentId, |
| | 38 | 83 | | handshakeId, |
| | 38 | 84 | | gatewayBinding, |
| | 38 | 85 | | resourceBinding, |
| | 38 | 86 | | requireProof, |
| | 38 | 87 | | requireAcknowledgmentReference, |
| | 38 | 88 | | requireUseCheck, |
| | 38 | 89 | | maxUseCount); |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | private static IReadOnlyList<string> NormalizeScopes(IEnumerable<string>? scopes) |
| | | 93 | | { |
| | 38 | 94 | | if (scopes is null) |
| | | 95 | | { |
| | 2 | 96 | | return EmptyScopes; |
| | | 97 | | } |
| | | 98 | | |
| | 36 | 99 | | string[] normalized = [.. scopes |
| | 42 | 100 | | .Where(scope => !string.IsNullOrWhiteSpace(scope)) |
| | 39 | 101 | | .Select(scope => scope.Trim()) |
| | 36 | 102 | | .Distinct(StringComparer.Ordinal) |
| | 42 | 103 | | .OrderBy(scope => scope, StringComparer.Ordinal)]; |
| | | 104 | | |
| | 36 | 105 | | return normalized.Length == 0 ? EmptyScopes : Array.AsReadOnly(normalized); |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | private static string? NormalizeOptional(string? value) |
| | | 109 | | { |
| | 296 | 110 | | return string.IsNullOrWhiteSpace(value) ? null : value.Trim(); |
| | | 111 | | } |
| | | 112 | | } |