Path: redb-identity/src/redb.Identity.Core/Services/RedisFederationStateNonceStore.cs
Snapshot: redb-identity, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-identity
Page: https://redbase.app/src/dev-2026-12-06/redb-identity/src/redb.Identity.Core/Services/RedisFederationStateNonceStore.cs
----
using redb.Route.Redis;
using StackExchange.Redis;
namespace redb.Identity.Core.Services;
///
/// Redis-backed implementation of .
/// Uses a per-jti string key with NX semantics so the first call wins atomically;
/// the key is set with the requested TTL so expired jti values can be replayed
/// only after the original state would itself have expired (consistent with
/// ).
///
internal sealed class RedisFederationStateNonceStore : IFederationStateNonceStore, IAsyncDisposable, IDisposable
{
private readonly RedisConnectionFactory _factory;
private readonly string _prefix;
private readonly SemaphoreSlim _connectionLock = new(1, 1);
private IConnectionMultiplexer? _connection;
public RedisFederationStateNonceStore(RedisConnectionFactory factory, string keyPrefix)
{
_factory = factory ?? throw new ArgumentNullException(nameof(factory));
_prefix = keyPrefix ?? string.Empty;
}
public async Task TryConsumeAsync(string jti, TimeSpan ttl, CancellationToken ct = default)
{
if (string.IsNullOrEmpty(jti))
return false;
ct.ThrowIfCancellationRequested();
var db = await GetDatabaseAsync(ct).ConfigureAwait(false);
var fullKey = _prefix + jti;
var ttlSpan = ttl <= TimeSpan.Zero ? TimeSpan.FromMinutes(5) : ttl;
// Atomic SETNX with TTL — first writer wins; everyone else gets false.
return await db.StringSetAsync(
fullKey,
"1",
expiry: ttlSpan,
when: When.NotExists).ConfigureAwait(false);
}
private async Task GetDatabaseAsync(CancellationToken ct)
{
if (_connection is { IsConnected: true })
return _connection.GetDatabase(_factory.Database);
await _connectionLock.WaitAsync(ct).ConfigureAwait(false);
try
{
if (_connection is { IsConnected: true })
return _connection.GetDatabase(_factory.Database);
var config = _factory.Build();
_connection = await ConnectionMultiplexer.ConnectAsync(config).ConfigureAwait(false);
return _connection.GetDatabase(_factory.Database);
}
finally
{
_connectionLock.Release();
}
}
public async ValueTask DisposeAsync()
{
if (_connection is not null)
await _connection.DisposeAsync().ConfigureAwait(false);
_connectionLock.Dispose();
}
public void Dispose()
{
_connection?.Dispose();
_connectionLock.Dispose();
}
}