| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Collections.Concurrent; |
| | | 7 | | using System.Threading; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.IdentityModel.Tokens |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// A default implementation of the Token replay cache that is backed by |
| | | 13 | | /// a bounded, expiring in-memory cache. |
| | | 14 | | /// </summary> |
| | | 15 | | internal class DefaultTokenReplayCache : TokenReplayCache |
| | | 16 | | { |
| | 2 | 17 | | private static readonly int s_defaultTokenReplayCacheCapacity = 500000; |
| | 2 | 18 | | private static readonly TimeSpan s_defaultTokenReplayCachePurgeInterval = TimeSpan.FromMinutes(1); |
| | | 19 | | |
| | | 20 | | private readonly ConcurrentDictionary<string, DateTime> _items; |
| | | 21 | | private readonly int _capacity; |
| | | 22 | | private readonly TimeSpan _purgeInterval; |
| | | 23 | | private long _nextPurgeTicks; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Constructs the default token replay cache. |
| | | 27 | | /// </summary> |
| | | 28 | | public DefaultTokenReplayCache() |
| | 10 | 29 | | : this(s_defaultTokenReplayCacheCapacity, s_defaultTokenReplayCachePurgeInterval) |
| | 10 | 30 | | { } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Constructs the default token replay cache with the specified |
| | | 34 | | /// capacity and purge interval. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="capacity">The capacity of the token cache.</param> |
| | | 37 | | /// <param name="purgeInterval">The time interval after which expired entries are removed.</param> |
| | | 38 | | public DefaultTokenReplayCache(int capacity, TimeSpan purgeInterval) |
| | 19 | 39 | | : base() |
| | | 40 | | { |
| | 19 | 41 | | if (capacity <= 0) |
| | | 42 | | { |
| | 1 | 43 | | throw new ArgumentOutOfRangeException(nameof(capacity), capacity, SR.Format(SR.ID0002)); |
| | | 44 | | } |
| | | 45 | | |
| | 18 | 46 | | if (purgeInterval <= TimeSpan.Zero) |
| | | 47 | | { |
| | 1 | 48 | | throw new ArgumentOutOfRangeException(nameof(purgeInterval), purgeInterval, SR.Format(SR.ID0016)); |
| | | 49 | | } |
| | | 50 | | |
| | 17 | 51 | | _capacity = capacity; |
| | 17 | 52 | | _purgeInterval = purgeInterval; |
| | 17 | 53 | | _items = new ConcurrentDictionary<string, DateTime>(StringComparer.Ordinal); |
| | 17 | 54 | | _nextPurgeTicks = DateTime.UtcNow.Add(purgeInterval).Ticks; |
| | 17 | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Returns true when the cache contains a non-expired entry for the supplied key. |
| | | 59 | | /// Expired entries are treated as absent and are removed on the next purge. |
| | | 60 | | /// </summary> |
| | | 61 | | public override bool Contains(string key) |
| | | 62 | | { |
| | 3 | 63 | | return _items.TryGetValue(key, out DateTime expiresOn) && DateTime.UtcNow < expiresOn; |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Removes the entry with the supplied key, if present. |
| | | 68 | | /// </summary> |
| | 1 | 69 | | public override void Remove(string key) => _items.TryRemove(key, out _); |
| | | 70 | | |
| | | 71 | | public override bool TryAdd(string securityToken, DateTime expiresOn) |
| | | 72 | | { |
| | 16396 | 73 | | if (DateTime.Equals(expiresOn, DateTime.MaxValue)) |
| | | 74 | | { |
| | 1 | 75 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperInvalidOperation(SR.Format(SR.ID1072)); |
| | | 76 | | } |
| | | 77 | | |
| | 16395 | 78 | | PurgeIfNeeded(); |
| | | 79 | | |
| | | 80 | | // Capacity is enforced approximately: Count is sampled before TryAdd, so |
| | | 81 | | // the live count can briefly exceed _capacity by the number of in-flight |
| | | 82 | | // concurrent inserts. This is acceptable because the bound only exists to |
| | | 83 | | // prevent unbounded growth, and the slack is bounded by request concurrency. |
| | 16395 | 84 | | if (_items.Count >= _capacity) |
| | | 85 | | { |
| | 1 | 86 | | throw new QuotaExceededException(SR.Format(SR.ID0021, _capacity)); |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | // ConcurrentDictionary.TryAdd is an atomic add-if-absent. It is exactly the |
| | | 90 | | // primitive the replay-cache contract requires: when this returns false the |
| | | 91 | | // caller (Microsoft.IdentityModel.Tokens.Saml) raises |
| | | 92 | | // SecurityTokenReplayDetectedException. Two concurrent inserts of the same |
| | | 93 | | // key cannot both observe "absent", so duplicate tokens are reliably rejected |
| | | 94 | | // without a TOCTOU window. |
| | 16394 | 95 | | return _items.TryAdd(securityToken, expiresOn); |
| | | 96 | | } |
| | | 97 | | |
| | 1 | 98 | | public override bool TryFind(string securityToken) => Contains(securityToken); |
| | | 99 | | |
| | | 100 | | private void PurgeIfNeeded() |
| | | 101 | | { |
| | 16395 | 102 | | long nowTicks = DateTime.UtcNow.Ticks; |
| | 16395 | 103 | | long nextTicks = Interlocked.Read(ref _nextPurgeTicks); |
| | 16395 | 104 | | if (nowTicks < nextTicks) |
| | | 105 | | { |
| | 16395 | 106 | | return; |
| | | 107 | | } |
| | | 108 | | |
| | 0 | 109 | | long newNext = DateTime.UtcNow.Add(_purgeInterval).Ticks; |
| | 0 | 110 | | if (Interlocked.CompareExchange(ref _nextPurgeTicks, newNext, nextTicks) != nextTicks) |
| | | 111 | | { |
| | | 112 | | // Another thread already moved the purge window forward and owns this round. |
| | 0 | 113 | | return; |
| | | 114 | | } |
| | | 115 | | |
| | 0 | 116 | | Purge(); |
| | 0 | 117 | | } |
| | | 118 | | |
| | | 119 | | private void Purge() |
| | | 120 | | { |
| | 0 | 121 | | DateTime now = DateTime.UtcNow; |
| | 0 | 122 | | List<string> expiredKeys = null; |
| | 0 | 123 | | foreach (KeyValuePair<string, DateTime> pair in _items) |
| | | 124 | | { |
| | 0 | 125 | | if (pair.Value <= now) |
| | | 126 | | { |
| | 0 | 127 | | (expiredKeys ?? (expiredKeys = new List<string>())).Add(pair.Key); |
| | | 128 | | } |
| | | 129 | | } |
| | | 130 | | |
| | 0 | 131 | | if (expiredKeys == null) |
| | | 132 | | { |
| | 0 | 133 | | return; |
| | | 134 | | } |
| | | 135 | | |
| | 0 | 136 | | foreach (string key in expiredKeys) |
| | | 137 | | { |
| | 0 | 138 | | _items.TryRemove(key, out _); |
| | | 139 | | } |
| | 0 | 140 | | } |
| | | 141 | | } |
| | | 142 | | } |