progmaweb
The site and account service of Progmasoft: a Next.js frontend and an ASP.NET Core API.
Clonegit clone https://git.progmasoft.com/progmaweb
mainroot / apps / api / Accounts / InMemoryAccountStore.cs
// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com>
// SPDX-License-Identifier: AGPL-3.0-or-later
namespace Progmasoft.Progmaweb.Api.Accounts;
/// <summary>An <see cref="IAccountStore"/> that keeps everything in the memory of the process.</summary>
/// <remarks>
/// It keeps the uniqueness and session rules of <see cref="PostgresAccountStore"/> under one lock, for development
/// and for tests that need no database. Nothing survives a restart of the service: every account and every session
/// is lost when the process stops.
/// </remarks>
internal sealed class InMemoryAccountStore : IAccountStore
{
/// <summary>Guards every dictionary below; each operation holds it for its whole duration.</summary>
private readonly Lock gate = new();
/// <summary>Every account, by identifier.</summary>
private readonly Dictionary<Guid, AccountRecord> accountsById = [];
/// <summary>Account identifiers by normalized email address; the uniqueness index of addresses.</summary>
private readonly Dictionary<string, Guid> accountIdsByEmail = new(StringComparer.Ordinal);
/// <summary>Account identifiers by Google subject; the uniqueness index of linked identities.</summary>
private readonly Dictionary<string, Guid> accountIdsByGoogleSubject = new(StringComparer.Ordinal);
/// <summary>Account identifiers by normalized account name; the uniqueness index of names.</summary>
private readonly Dictionary<string, Guid> accountIdsByName = new(StringComparer.Ordinal);
/// <summary>Sessions by the hexadecimal form of their token digest.</summary>
private readonly Dictionary<string, SessionRecord> sessionsByDigest = new(StringComparer.Ordinal);
/// <summary>The notices sent: account and version.</summary>
private readonly HashSet<(Guid AccountId, string Version)> policyNotices = [];
/// <inheritdoc/>
public ValueTask<CreateAccountResult> CreateAsync(AccountRecord account, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
lock (gate)
{
if (account.GoogleSubject is not null && accountIdsByGoogleSubject.ContainsKey(account.GoogleSubject))
{
return ValueTask.FromResult(CreateAccountResult.GoogleSubjectUnavailable());
}
if (accountIdsByName.ContainsKey(account.NormalizedAccountName))
{
return ValueTask.FromResult(CreateAccountResult.AccountNameUnavailable());
}
if (accountIdsByEmail.ContainsKey(account.NormalizedEmail))
{
return ValueTask.FromResult(CreateAccountResult.EmailUnavailable());
}
accountsById.Add(account.Id, account);
accountIdsByName.Add(account.NormalizedAccountName, account.Id);
accountIdsByEmail.Add(account.NormalizedEmail, account.Id);
if (account.GoogleSubject is not null)
{
accountIdsByGoogleSubject.Add(account.GoogleSubject, account.Id);
}
return ValueTask.FromResult(CreateAccountResult.Created(account));
}
}
/// <inheritdoc/>
public ValueTask<AccountRecord?> FindByGoogleSubjectAsync(
string googleSubject,
CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
lock (gate)
{
return ValueTask.FromResult(
accountIdsByGoogleSubject.TryGetValue(googleSubject, out Guid id) ? accountsById[id] : null);
}
}
/// <inheritdoc/>
public ValueTask<AccountRecord?> FindByEmailAsync(string normalizedEmail, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
lock (gate)
{
return ValueTask.FromResult(
accountIdsByEmail.TryGetValue(normalizedEmail, out Guid id) ? accountsById[id] : null);
}
}
/// <inheritdoc/>
public ValueTask<AccountRecord?> FindByAccountNameAsync(
string normalizedAccountName,
CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
lock (gate)
{
return ValueTask.FromResult(
accountIdsByName.TryGetValue(normalizedAccountName, out Guid id) ? accountsById[id] : null);
}
}
/// <inheritdoc/>
public ValueTask<AccountRecord?> FindByIdAsync(Guid id, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
lock (gate)
{
accountsById.TryGetValue(id, out AccountRecord? account);
return ValueTask.FromResult(account);
}
}
/// <inheritdoc/>
public ValueTask StoreSessionAsync(SessionRecord session, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
lock (gate)
{
// The durable store refuses a session of an account that does not exist; so does this one.
if (!accountsById.ContainsKey(session.AccountId))
{
throw new InvalidOperationException("A session refers to an account that is not stored.");
}
sessionsByDigest[Convert.ToHexString(session.TokenDigest)] = session;
return ValueTask.CompletedTask;
}
}
/// <inheritdoc/>
public ValueTask<SessionRecord?> FindSessionAsync(byte[] tokenDigest, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
lock (gate)
{
sessionsByDigest.TryGetValue(Convert.ToHexString(tokenDigest), out SessionRecord? session);
return ValueTask.FromResult(session);
}
}
/// <inheritdoc/>
public ValueTask RevokeSessionAsync(byte[] tokenDigest, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
lock (gate)
{
sessionsByDigest.Remove(Convert.ToHexString(tokenDigest));
return ValueTask.CompletedTask;
}
}
/// <inheritdoc/>
public ValueTask<int> RemoveExpiredSessionsAsync(DateTimeOffset now, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
lock (gate)
{
string[] expired = [.. sessionsByDigest.Where(entry => entry.Value.ExpiresAt <= now).Select(entry => entry.Key)];
foreach (string digest in expired)
{
sessionsByDigest.Remove(digest);
}
return ValueTask.FromResult(expired.Length);
}
}
/// <inheritdoc/>
public ValueTask<IReadOnlyList<AccountRecord>> ListAccountsToNotifyAsync(
string version,
CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
lock (gate)
{
AccountRecord[] accounts =
[
.. accountsById.Values
.Where(account => account.PoliciesVersion is null
|| string.CompareOrdinal(account.PoliciesVersion, version) < 0)
.Where(account => !policyNotices.Contains((account.Id, version)))
.OrderBy(account => account.CreatedAt)
.ThenBy(account => account.Id)
];
return ValueTask.FromResult<IReadOnlyList<AccountRecord>>(accounts);
}
}
/// <inheritdoc/>
public ValueTask RecordPolicyNoticeAsync(
Guid accountId,
string version,
DateTimeOffset sentAt,
CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
lock (gate)
{
if (accountsById.ContainsKey(accountId))
{
policyNotices.Add((accountId, version));
}
return ValueTask.CompletedTask;
}
}
}