| | | 1 | | using AsiBackbone.Core.CapabilityTokens; |
| | | 2 | | |
| | | 3 | | namespace AsiBackbone.Storage.InMemory.CapabilityTokens; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Provides a non-durable, in-process capability grant use store for tests, samples, and local validation. |
| | | 7 | | /// </summary> |
| | | 8 | | /// <remarks> |
| | | 9 | | /// This store is thread-safe within a single process, but it is not durable, distributed, replicated, or suitable for |
| | | 10 | | /// production replay protection. Hosts that require production single-use or bounded-use guarantees should provide a |
| | | 11 | | /// durable implementation of <see cref="ICapabilityGrantUseStore" /> with documented transaction, locking, retention, |
| | | 12 | | /// and failure semantics. |
| | | 13 | | /// </remarks> |
| | | 14 | | public sealed class InMemoryCapabilityGrantUseStore : ICapabilityGrantUseStore |
| | | 15 | | { |
| | 10 | 16 | | private readonly Lock syncRoot = new(); |
| | 10 | 17 | | private readonly Dictionary<string, int> useCounts = new(StringComparer.Ordinal); |
| | 10 | 18 | | private readonly HashSet<string> stoppedGrantIds = new(StringComparer.Ordinal); |
| | 10 | 19 | | private readonly HashSet<string> cancelledGrantIds = new(StringComparer.Ordinal); |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Gets the observed use count for a grant identifier. |
| | | 23 | | /// </summary> |
| | | 24 | | /// <param name="grantId">The stable capability grant identifier.</param> |
| | | 25 | | /// <returns>The observed use count, or zero when the grant has not been consumed by this store instance.</returns> |
| | | 26 | | public int GetUseCount(string grantId) |
| | | 27 | | { |
| | 6 | 28 | | string normalizedGrantId = NormalizeGrantId(grantId); |
| | | 29 | | |
| | | 30 | | lock (syncRoot) |
| | | 31 | | { |
| | 6 | 32 | | return useCounts.TryGetValue(normalizedGrantId, out int useCount) |
| | 6 | 33 | | ? useCount |
| | 6 | 34 | | : 0; |
| | | 35 | | } |
| | 6 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Marks a grant as stopped for subsequent local validation attempts. |
| | | 40 | | /// </summary> |
| | | 41 | | /// <param name="grantId">The stable capability grant identifier.</param> |
| | | 42 | | public void StopGrant(string grantId) |
| | | 43 | | { |
| | 2 | 44 | | string normalizedGrantId = NormalizeGrantId(grantId); |
| | | 45 | | |
| | | 46 | | lock (syncRoot) |
| | | 47 | | { |
| | 2 | 48 | | _ = stoppedGrantIds.Add(normalizedGrantId); |
| | 2 | 49 | | _ = cancelledGrantIds.Remove(normalizedGrantId); |
| | 2 | 50 | | } |
| | 2 | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Marks a grant as cancelled for subsequent local validation attempts. |
| | | 55 | | /// </summary> |
| | | 56 | | /// <param name="grantId">The stable capability grant identifier.</param> |
| | | 57 | | public void CancelGrant(string grantId) |
| | | 58 | | { |
| | 2 | 59 | | string normalizedGrantId = NormalizeGrantId(grantId); |
| | | 60 | | |
| | | 61 | | lock (syncRoot) |
| | | 62 | | { |
| | 2 | 63 | | _ = cancelledGrantIds.Add(normalizedGrantId); |
| | 2 | 64 | | _ = stoppedGrantIds.Remove(normalizedGrantId); |
| | 2 | 65 | | } |
| | 2 | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Clears use-count and stopped/cancelled state from this in-memory store instance. |
| | | 70 | | /// </summary> |
| | | 71 | | public void Clear() |
| | 0 | 72 | | { |
| | | 73 | | lock (syncRoot) |
| | | 74 | | { |
| | 0 | 75 | | useCounts.Clear(); |
| | 0 | 76 | | stoppedGrantIds.Clear(); |
| | 0 | 77 | | cancelledGrantIds.Clear(); |
| | 0 | 78 | | } |
| | 0 | 79 | | } |
| | | 80 | | |
| | | 81 | | /// <inheritdoc /> |
| | | 82 | | public ValueTask<CapabilityGrantUseResult> TryConsumeAsync( |
| | | 83 | | CapabilityTokenGrant grant, |
| | | 84 | | int maxUseCount, |
| | | 85 | | DateTimeOffset usedUtc, |
| | | 86 | | CancellationToken cancellationToken = default) |
| | | 87 | | { |
| | 16 | 88 | | ArgumentNullException.ThrowIfNull(grant); |
| | 16 | 89 | | ArgumentOutOfRangeException.ThrowIfLessThan(maxUseCount, 1); |
| | 16 | 90 | | _ = usedUtc.ToUniversalTime(); |
| | 16 | 91 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 92 | | |
| | | 93 | | lock (syncRoot) |
| | | 94 | | { |
| | 16 | 95 | | if (stoppedGrantIds.Contains(grant.TokenId)) |
| | | 96 | | { |
| | 2 | 97 | | return ValueTask.FromResult(CapabilityGrantUseResult.Stopped("The in-memory capability grant use store m |
| | | 98 | | } |
| | | 99 | | |
| | 14 | 100 | | if (cancelledGrantIds.Contains(grant.TokenId)) |
| | | 101 | | { |
| | 2 | 102 | | return ValueTask.FromResult(CapabilityGrantUseResult.Cancelled("The in-memory capability grant use store |
| | | 103 | | } |
| | | 104 | | |
| | 12 | 105 | | _ = useCounts.TryGetValue(grant.TokenId, out int currentCount); |
| | | 106 | | |
| | 12 | 107 | | if (currentCount >= maxUseCount) |
| | | 108 | | { |
| | 4 | 109 | | return ValueTask.FromResult(CapabilityGrantUseResult.UseLimitExceeded( |
| | 4 | 110 | | currentCount, |
| | 4 | 111 | | "The in-memory capability grant use limit was exceeded.")); |
| | | 112 | | } |
| | | 113 | | |
| | 8 | 114 | | int nextCount = currentCount + 1; |
| | 8 | 115 | | useCounts[grant.TokenId] = nextCount; |
| | 8 | 116 | | return ValueTask.FromResult(CapabilityGrantUseResult.Accepted(nextCount)); |
| | | 117 | | } |
| | 16 | 118 | | } |
| | | 119 | | |
| | | 120 | | private static string NormalizeGrantId(string grantId) |
| | | 121 | | { |
| | 10 | 122 | | ArgumentException.ThrowIfNullOrWhiteSpace(grantId); |
| | 10 | 123 | | return grantId.Trim(); |
| | | 124 | | } |
| | | 125 | | } |