Esempio n. 1
0
        public override bool Add(TicketCacheEntry entry)
        {
            var cacheEntry = new CacheEntry(entry.Computed, entry.Value, this.Logger);

            if (entry.Expires > DateTimeOffset.UtcNow)
            {
                cacheEntry.MarkLifetime(entry.Expires, entry.RenewUntil);
            }

            bool added = false;

            if (this.BlockUpdates)
            {
                var got = this.GetInternal(cacheEntry.Key);

                if (got == null)
                {
                    added = this.cache.TryAdd(cacheEntry.Key, cacheEntry);
                }
            }
            else
            {
                this.cache.AddOrUpdate(cacheEntry.Key, cacheEntry, (_, __) => cacheEntry);
                added = true;
            }

            return(added);
        }
Esempio n. 2
0
        public async Task <bool> Add(TicketCacheEntry entry)
        {
            var cacheEntry = new CacheEntry(entry.Computed, entry.Value, logger);

            var lifetime = entry.Expires - DateTimeOffset.UtcNow;

            if (lifetime > TimeSpan.Zero)
            {
                cacheEntry.MarkLifetime(lifetime);
            }

            bool added = false;

            if (BlockUpdates)
            {
                var got = await GetInternal(cacheEntry.Key);

                if (got == null)
                {
                    added = cache.TryAdd(cacheEntry.Key, cacheEntry);
                }
            }
            else
            {
                cache.AddOrUpdate(cacheEntry.Key, cacheEntry, (_, __) => cacheEntry);
                added = true;
            }

            return(added);
        }
Esempio n. 3
0
        public Task <object> Get(string key, string container = null)
        {
            if (cache.TryGetValue(TicketCacheEntry.GenerateKey(key: key, container: container), out CacheEntry entry))
            {
                return(Task.FromResult(entry.Value));
            }

            return(Task.FromResult <object>(null));
        }
Esempio n. 4
0
        public override ValueTask <bool> AddAsync(TicketCacheEntry entry)
        {
            if (this.Add(entry))
            {
                return(new ValueTask <bool>(true));
            }

            return(new ValueTask <bool>(false));
        }
        public Task <bool> Contains(TicketCacheEntry entry)
        {
            if (!CacheRegions.TryGetValue(entry.Container, out ObjectCache cache))
            {
                return(Task.FromResult(false));
            }

            return(Task.FromResult(cache.Contains(entry.Key)));
        }
Esempio n. 6
0
        public async Task <bool> Add(TicketCacheEntry entry)
        {
            if (entry == null)
            {
                throw new ArgumentNullException(nameof(entry));
            }

            return(await this.cache.AddAsync(entry).ConfigureAwait(true));
        }
Esempio n. 7
0
        public async Task <bool> Add(TicketCacheEntry entry)
        {
            if (await Contains(entry))
            {
                return(false);
            }

            await cache.Add(entry);

            return(true);
        }
        public Task <bool> Add(TicketCacheEntry entry)
        {
            var tokenCache = GetOrCreate(entry.Container);

            var cacheItem = new CacheItem(entry.Key, entry.Key, entry.Container);

            var result = tokenCache.Add(
                cacheItem,
                new CacheItemPolicy
            {
                AbsoluteExpiration = entry.Expires
            }
                );

            return(Task.FromResult(result));
        }
        public Task <bool> Add(TicketCacheEntry entry)
        {
            bool added = false;

            var cacheEntry = new CacheEntry(cache, entry.Computed, entry.Value, logger);

            if (cache.TryAdd(cacheEntry.Key, cacheEntry))
            {
                var lifetime = entry.Expires - DateTimeOffset.UtcNow;

                if (lifetime > TimeSpan.Zero)
                {
                    cacheEntry.BeginTriggerDelay(lifetime, cancel.Token);

                    added = true;
                }
            }

            return(Task.FromResult(added));
        }
Esempio n. 10
0
        protected virtual async Task Validate(DecryptedKrbApReq decryptedToken)
        {
            if (decryptedToken == null)
            {
                throw new ArgumentNullException(nameof(decryptedToken));
            }

            var sequence  = this.ObscureSequence(decryptedToken.Authenticator.SequenceNumber ?? 0);
            var container = this.ObscureContainer(decryptedToken.Ticket.CRealm);

            var entry = new TicketCacheEntry
            {
                Key       = sequence,
                Container = container,
                Expires   = decryptedToken.Ticket.EndTime
            };

            var replayDetected = true;

            var detectReplay = this.ValidateAfterDecrypt.HasFlag(ValidationActions.Replay);

            if (!detectReplay)
            {
                decryptedToken.Validate(this.ValidateAfterDecrypt);
                replayDetected = false;
            }
            else if (!await this.tokenCache.Contains(entry).ConfigureAwait(true))
            {
                decryptedToken.Validate(this.ValidateAfterDecrypt);

                if (await this.tokenCache.Add(entry).ConfigureAwait(true))
                {
                    replayDetected = false;
                }
            }

            if (replayDetected)
            {
                throw new ReplayException($"Replay detected in container '{entry.Container}' with key {entry.Key}.");
            }
        }
Esempio n. 11
0
        protected virtual async Task Validate(DecryptedData decryptedToken)
        {
            var sequence  = ObscureSequence(decryptedToken.Authenticator.SequenceNumber);
            var container = ObscureContainer(decryptedToken.Ticket.CRealm);

            var entry = new TicketCacheEntry
            {
                Key       = sequence,
                Container = container,
                Expires   = decryptedToken.Ticket.EndTime
            };

            var replayDetected = true;

            var detectReplay = ValidateAfterDecrypt.HasFlag(ValidationActions.Replay);

            if (!detectReplay)
            {
                decryptedToken.Validate(ValidateAfterDecrypt);
                replayDetected = false;
            }
            else if (!(await TokenCache.Contains(entry)))
            {
                decryptedToken.Validate(ValidateAfterDecrypt);

                if (await TokenCache.Add(entry))
                {
                    replayDetected = false;
                }
            }

            if (replayDetected)
            {
                throw new ReplayException($"Replay detected in container '{entry.Container}' with key {entry.Key}.");
            }
        }
Esempio n. 12
0
 public async Task <bool> Add(TicketCacheEntry entry)
 {
     return(await cache.Add(entry));
 }
Esempio n. 13
0
 public async Task <bool> Contains(TicketCacheEntry entry)
 {
     return(await cache.Contains(entry));
 }
Esempio n. 14
0
        public override ValueTask <bool> ContainsAsync(TicketCacheEntry entry)
        {
            var exists = this.cache.ContainsKey(entry.Computed);

            return(new ValueTask <bool>(exists));
        }
Esempio n. 15
0
 public override bool Contains(TicketCacheEntry entry)
 {
     return(this.cache.ContainsKey(entry.Computed));
 }
Esempio n. 16
0
        public Task <bool> Contains(TicketCacheEntry entry)
        {
            var exists = cache.ContainsKey(entry.Computed);

            return(Task.FromResult(exists));
        }
Esempio n. 17
0
 public abstract bool Contains(TicketCacheEntry entry);
Esempio n. 18
0
 public abstract ValueTask <bool> ContainsAsync(TicketCacheEntry entry);
Esempio n. 19
0
 public abstract bool Add(TicketCacheEntry entry);
Esempio n. 20
0
 public abstract ValueTask <bool> AddAsync(TicketCacheEntry entry);
Esempio n. 21
0
        public async Task <bool> Contains(TicketCacheEntry entry)
        {
            var got = await this.cache.GetCacheItemAsync(entry?.Key).ConfigureAwait(true);

            return(got != null);
        }
Esempio n. 22
0
        public async Task <bool> Contains(TicketCacheEntry entry)
        {
            var got = await cache.Get(entry.Key);

            return(got != null);
        }
Esempio n. 23
0
        public override object GetCacheItem(string key, string container = null)
        {
            var entryKey = TicketCacheEntry.GenerateKey(key: key, container: container);

            return(this.GetInternal(entryKey));
        }
Esempio n. 24
0
        public Task <object> Get(string key, string container = null)
        {
            var entryKey = TicketCacheEntry.GenerateKey(key: key, container: container);

            return(GetInternal(entryKey));
        }