Bifrost/src/Bifrost.Web/Features/Heimdall/LinkedAccountsSnapshots.cs
Heimdall 59ecaa606b Linked accounts page: list, link any admitted provider, unlink with step-up proof
A member sees their Heimdall logins, links Discord, Patreon or Google, and unlinks one
with a fresh Google step-up sign-in. Bifrost keeps no Heimdall token: each action is a
fresh Heimdall sign-in started by Bifrost whose bearer lives inside the completion
request. The list is kept five minutes in this process as non-secret fields only.

The Status page's Google-only link button and handler are cut; the Linked accounts page
owns link starts. HeimdallStartRequest gains stepUp. Heimdall's member routes use a
client without the app secret.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-13 19:35:40 +00:00

35 lines
1.5 KiB
C#

using System.Collections.Concurrent;
namespace Bifrost.Web.Features.Heimdall;
/// <summary>
/// What the Linked accounts page last read from Heimdall for an account: id, provider, display name and link date of
/// each login, nothing else, for <see cref="Lifetime"/>. It lives in this process only, keyed by the Heimdall account id,
/// and is never given to the browser; it holds no bearer. The page renders from it because the list is fetched inside a
/// completion request, which redirects. After a restart it is empty and the page offers to read the list again.
/// </summary>
public sealed class LinkedAccountsSnapshots(TimeProvider timeProvider)
{
public static readonly TimeSpan Lifetime = TimeSpan.FromMinutes(5);
private readonly ConcurrentDictionary<string, (IReadOnlyList<LinkedIdentitySummary> Identities, DateTimeOffset ExpiresAt)> _held = new(StringComparer.Ordinal);
public void Put(string accountId, IReadOnlyList<LinkedIdentitySummary> identities)
{
var now = timeProvider.GetUtcNow();
foreach (var (key, value) in _held)
{
if (value.ExpiresAt <= now)
{
_held.TryRemove(key, out _);
}
}
_held[accountId] = (identities, now + Lifetime);
}
public IReadOnlyList<LinkedIdentitySummary>? Get(string accountId) =>
_held.TryGetValue(accountId, out var value) && value.ExpiresAt > timeProvider.GetUtcNow() ? value.Identities : null;
public void Remove(string accountId) => _held.TryRemove(accountId, out _);
}