using System;
using System.Collections.Concurrent;
namespace CarpaNet.OAuth.Crypto;
///
/// Caches DPoP nonces per origin.
///
public sealed class DPoPNonceCache
{
private readonly ConcurrentDictionary _cache = new();
private readonly TimeSpan _ttl;
private readonly int _maxEntries;
private sealed class NonceEntry
{
public string Nonce { get; set; } = string.Empty;
public DateTimeOffset ExpiresAt { get; set; }
}
///
/// Creates a new DPoP nonce cache.
///
/// Time-to-live for cached nonces (default: 60 seconds).
/// Maximum number of cached entries (default: 100).
public DPoPNonceCache(TimeSpan? ttl = null, int maxEntries = 100)
{
_ttl = ttl ?? TimeSpan.FromSeconds(60);
_maxEntries = maxEntries;
}
///
/// Gets the origin (scheme + host) from a URL.
///
public static string GetOrigin(string url)
{
var uri = new Uri(url);
return $"{uri.Scheme}://{uri.Authority}";
}
///
/// Gets a cached nonce for the given URL's origin.
///
/// The request URL.
/// The cached nonce, or null if not found or expired.
public string? Get(string url)
{
var origin = GetOrigin(url);
if (_cache.TryGetValue(origin, out var entry) && entry.ExpiresAt > DateTimeOffset.UtcNow)
{
return entry.Nonce;
}
return null;
}
///
/// Stores a nonce for the given URL's origin.
///
/// The request URL.
/// The nonce to store.
public void Set(string url, string nonce)
{
var origin = GetOrigin(url);
// Simple eviction: if we're over capacity, clear oldest entries
if (_cache.Count >= _maxEntries)
{
// Clear expired entries first
var now = DateTimeOffset.UtcNow;
foreach (var key in _cache.Keys)
{
if (_cache.TryGetValue(key, out var entry) && entry.ExpiresAt <= now)
{
_cache.TryRemove(key, out _);
}
}
}
_cache[origin] = new NonceEntry
{
Nonce = nonce,
ExpiresAt = DateTimeOffset.UtcNow + _ttl
};
}
///
/// Clears all cached nonces.
///
public void Clear()
{
_cache.Clear();
}
}