Path: redb-identity/tests/redb.Identity.Tests/Services/InMemoryServerSideOtpStore.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/tests/redb.Identity.Tests/Services/InMemoryServerSideOtpStore.cs
----
using redb.Identity.Core.Mfa;
namespace redb.Identity.Tests.Services;
///
/// In-memory for unit tests. Replicates the invariants
/// of (single-use, user cross-check, expiry) without a
/// real PROPS backing store.
///
internal sealed class InMemoryServerSideOtpStore : IServerSideOtpStore
{
private readonly Dictionary _store = new();
private sealed record Entry(long UserId, string Method, string Code, DateTimeOffset ExpiresAt, bool Consumed);
public Task IssueAsync(long userId, string method, string destinationMasked, TimeSpan ttl, CancellationToken ct = default)
{
var jti = Guid.NewGuid();
// Fixed code so tests can assert against it directly.
var code = "123456";
var expires = DateTimeOffset.UtcNow + ttl;
_store[jti] = new Entry(userId, method, code, expires, false);
return Task.FromResult(new OtpIssueResult(jti, code, destinationMasked, expires));
}
public Task VerifyAndConsumeAsync(Guid jti, long userId, string code, CancellationToken ct = default)
{
if (!_store.TryGetValue(jti, out var e)) return Task.FromResult(new OtpVerifyResult(false, "not_found"));
if (e.UserId != userId) return Task.FromResult(new OtpVerifyResult(false, "user_mismatch", e.Method));
if (e.Consumed) return Task.FromResult(new OtpVerifyResult(false, "already_consumed", e.Method));
if (e.ExpiresAt < DateTimeOffset.UtcNow) return Task.FromResult(new OtpVerifyResult(false, "expired", e.Method));
if (e.Code != code.Trim()) return Task.FromResult(new OtpVerifyResult(false, "bad_code", e.Method));
_store[jti] = e with { Consumed = true };
return Task.FromResult(new OtpVerifyResult(true, "ok", e.Method));
}
/// Test helper: seeds a known (jti, code) pair so tests can build an MfaState.
public Guid Seed(long userId, string method, string code)
{
var jti = Guid.NewGuid();
_store[jti] = new Entry(userId, method, code, DateTimeOffset.UtcNow.AddMinutes(5), false);
return jti;
}
}