Linked accounts: never guess a proof provider; require stepUpRequested; no-store on completion
With no current list the proof goes only through a provider the member names, never the session's sign-in provider: a sign-in through an unlinked provider makes Heimdall mint an orphan account holding that login. Link buttons need a current list. The unlink completion must say a step-up was requested. POST /auth/heimdall/complete answers no-store. The fake Heimdall now models orphan minting, derives step_up_at from the start's stepUp flag, and enforces the app secret, sign_in-only, unsupported-provider and 600 s rules. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
b65bec65ce
commit
8e65f56b0c
7 changed files with 325 additions and 69 deletions
|
|
@ -163,27 +163,28 @@ public sealed class HeimdallAuthService(
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// The provider an ordinary proof sign-in uses: the one the person signed in with when the account still holds it,
|
||||
/// else the first held one of Discord, Patreon, Google. With no list to go by, the session's provider, else Discord.
|
||||
/// Signing in through a provider the account no longer holds would only mint an unrelated Heimdall account.
|
||||
/// The provider an ordinary proof sign-in uses, or null when none is safe. A sign-in through a provider the account no
|
||||
/// longer holds makes Heimdall mint an unrelated account that holds that login, so it is never guessed: with the account's
|
||||
/// current list, the first held one of the person's choice, Discord, Patreon, Google; with no list, only a provider the
|
||||
/// member named (<paramref name="via"/>), who is the one who knows what they hold. The session's provider claim is not
|
||||
/// consulted: it records how they signed in once, not what the account holds now.
|
||||
/// </summary>
|
||||
public static string ProofProvider(string? sessionProvider, IReadOnlyCollection<string>? held)
|
||||
public static string? ProofProvider(IReadOnlyCollection<string>? held, string? via)
|
||||
{
|
||||
var session = sessionProvider is not null && ProofProviders.Contains(sessionProvider) ? sessionProvider.ToLowerInvariant() : null;
|
||||
if (held is { Count: > 0 })
|
||||
var chosen = via is not null && ProofProviders.Contains(via) ? via.ToLowerInvariant() : null;
|
||||
if (held is null)
|
||||
{
|
||||
foreach (var candidate in new[] { session, "discord", "patreon", "google" })
|
||||
{
|
||||
if (candidate is not null && held.Contains(candidate, StringComparer.OrdinalIgnoreCase))
|
||||
{
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
return chosen;
|
||||
}
|
||||
|
||||
return session ?? "discord";
|
||||
return new[] { chosen, "discord", "patreon", "google" }
|
||||
.FirstOrDefault(x => x is not null && held.Contains(x, StringComparer.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
private string ProofProviderFor(string accountId, string? via) =>
|
||||
ProofProvider(snapshots.Get(accountId)?.Select(x => x.Provider).ToArray(), via)
|
||||
?? throw new HeimdallAuthException("No provider the account is known to hold can prove it.");
|
||||
|
||||
/// <summary>
|
||||
/// The page's proof sign-ins. Each is a Heimdall sign-in for the account already signed in here, kept as an attempt
|
||||
/// that says what it is the proof for (list, link, unlink) and which person it must prove. The session is never replaced.
|
||||
|
|
@ -209,17 +210,23 @@ public sealed class HeimdallAuthService(
|
|||
stepUp: stepUp,
|
||||
target: target);
|
||||
|
||||
/// <summary>Reads the account's logins: an ordinary sign-in, then GET /v1/me/identities inside its completion.</summary>
|
||||
/// <summary>
|
||||
/// Reads the account's logins: an ordinary sign-in, then GET /v1/me/identities inside its completion. Through a provider on
|
||||
/// the current list, or, when there is none, the one the member names in <paramref name="via"/>.
|
||||
/// </summary>
|
||||
public Task<HeimdallAuthStartResult> StartListAsync(
|
||||
string sessionAccountId, string? sessionProvider, string callbackBaseUrl,
|
||||
string sessionAccountId, string? via, string callbackBaseUrl,
|
||||
IEnumerable<KeyValuePair<string, string>> requestCookies, CancellationToken cancellationToken) =>
|
||||
StartProofAsync(
|
||||
HeimdallAttemptPurpose.ListProof, ProofProvider(sessionProvider, snapshots.Get(sessionAccountId)?.Select(x => x.Provider).ToArray()),
|
||||
HeimdallAttemptPurpose.ListProof, ProofProviderFor(sessionAccountId, via),
|
||||
sessionAccountId, callbackBaseUrl, requestCookies, false, null, cancellationToken);
|
||||
|
||||
/// <summary>Links another provider: an ordinary sign-in as proof, then the link start for <paramref name="targetProvider"/> with its bearer.</summary>
|
||||
/// <summary>
|
||||
/// Links another provider: an ordinary sign-in as proof through a provider on the account's current list (there is no
|
||||
/// guessing without one), then the link start for <paramref name="targetProvider"/> with its bearer.
|
||||
/// </summary>
|
||||
public Task<HeimdallAuthStartResult> StartLinkAsync(
|
||||
string sessionAccountId, string? sessionProvider, string targetProvider, string callbackBaseUrl,
|
||||
string sessionAccountId, string targetProvider, string callbackBaseUrl,
|
||||
IEnumerable<KeyValuePair<string, string>> requestCookies, CancellationToken cancellationToken)
|
||||
{
|
||||
if (!LinkableProviders.Contains(targetProvider))
|
||||
|
|
@ -228,7 +235,7 @@ public sealed class HeimdallAuthService(
|
|||
}
|
||||
|
||||
return StartProofAsync(
|
||||
HeimdallAttemptPurpose.LinkStart, ProofProvider(sessionProvider, snapshots.Get(sessionAccountId)?.Select(x => x.Provider).ToArray()),
|
||||
HeimdallAttemptPurpose.LinkStart, ProofProviderFor(sessionAccountId, null),
|
||||
sessionAccountId, callbackBaseUrl, requestCookies, false, targetProvider.ToLowerInvariant(), cancellationToken);
|
||||
}
|
||||
|
||||
|
|
@ -458,7 +465,13 @@ public sealed class HeimdallAuthService(
|
|||
|
||||
case HeimdallAttemptPurpose.UnlinkProof:
|
||||
{
|
||||
var (identity, bearer) = await RedeemOwnerAsync(callbackBaseUrl, attempt, code, cancellationToken);
|
||||
var (identity, bearer, redeemed) = await RedeemOwnerWithBodyAsync(callbackBaseUrl, attempt, code, cancellationToken);
|
||||
if (redeemed.StepUpRequested != true)
|
||||
{
|
||||
// Heimdall reports "asked" on a step-up completion; one that does not was not a step-up sign-in.
|
||||
return new HeimdallLinkStep(null, cookieName, null, HeimdallLinkOutcome.StepUpRequired);
|
||||
}
|
||||
|
||||
var unlinked = await memberClient.UnlinkAsync(bearer, attempt.Target ?? string.Empty, cancellationToken);
|
||||
switch (unlinked.Call)
|
||||
{
|
||||
|
|
@ -838,7 +851,8 @@ public enum HeimdallLinkOutcome
|
|||
IdentityNotFound,
|
||||
SignInRequired,
|
||||
ProofUnavailable,
|
||||
ListExpired
|
||||
ListExpired,
|
||||
ProviderNeeded
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -865,6 +879,7 @@ public static class HeimdallLinkOutcomes
|
|||
HeimdallLinkOutcome.SignInRequired => "sign_in_required",
|
||||
HeimdallLinkOutcome.ProofUnavailable => "proof_unavailable",
|
||||
HeimdallLinkOutcome.ListExpired => "list_expired",
|
||||
HeimdallLinkOutcome.ProviderNeeded => "provider_needed",
|
||||
_ => "error"
|
||||
};
|
||||
|
||||
|
|
@ -881,6 +896,7 @@ public static class HeimdallLinkOutcomes
|
|||
"identity_not_found" => "That login is no longer on your account. Nothing was changed; the list below is read again when you ask.",
|
||||
"sign_in_required" => "Heimdall no longer accepts that sign-in. Nothing was changed; try again.",
|
||||
"proof_unavailable" => "Fresh proof could not be started, so nothing was changed. Unlinking needs a fresh Google sign-in, and it is not available right now.",
|
||||
"provider_needed" => "Bifrost does not know which logins this account holds right now, so it will not guess. Pick one you know it holds to read the list. Nothing was changed.",
|
||||
"list_expired" => "The list was out of date. Nothing was changed; read your linked logins again.",
|
||||
"error" => "That could not be completed. Nothing was changed.",
|
||||
_ => null
|
||||
|
|
@ -928,7 +944,8 @@ public sealed record HeimdallRedeemResponse(
|
|||
[property: JsonPropertyName("accessToken")] string? AccessToken,
|
||||
[property: JsonPropertyName("account")] HeimdallAccount? Account,
|
||||
[property: JsonPropertyName("mode")] string? Mode = null,
|
||||
[property: JsonPropertyName("provider")] string? Provider = null);
|
||||
[property: JsonPropertyName("provider")] string? Provider = null,
|
||||
[property: JsonPropertyName("stepUpRequested")] bool? StepUpRequested = null);
|
||||
|
||||
public sealed record HeimdallAccount(
|
||||
[property: JsonPropertyName("id")] string Id);
|
||||
|
|
|
|||
|
|
@ -29,10 +29,21 @@
|
|||
<h2>Your logins</h2>
|
||||
@if (Model.Identities is null)
|
||||
{
|
||||
<form method="post" asp-page-handler="Refresh" class="panel-actions">
|
||||
<button class="button secondary" type="submit">Show my linked logins</button>
|
||||
<span class="muted">You will sign in to Heimdall again, which reads the list. It is kept for five minutes, then asked for again.</span>
|
||||
</form>
|
||||
<p class="muted">
|
||||
Bifrost does not hold the list: it asks Heimdall, which means signing in to Heimdall again, and keeps the answer for
|
||||
five minutes. It also cannot tell which logins this account holds right now, so it will not choose one for you.
|
||||
Pick a login you know it holds. Signing in through one it no longer holds would create a separate Heimdall account
|
||||
holding that login.
|
||||
</p>
|
||||
<div class="panel-actions">
|
||||
@foreach (var provider in Model.LinkableProviders)
|
||||
{
|
||||
<form method="post" asp-page-handler="Refresh" class="inline-actions">
|
||||
<input type="hidden" name="via" value="@provider" />
|
||||
<button class="button secondary" type="submit">Show my logins through @LinkedAccountsModel.ProviderName(provider)</button>
|
||||
</form>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -82,20 +93,23 @@
|
|||
</form>
|
||||
}
|
||||
|
||||
<h2>Link another login</h2>
|
||||
<p class="muted">
|
||||
You will sign in to Heimdall again, then to the provider you pick. If that login already belongs to another account,
|
||||
nothing is linked and nothing is merged.
|
||||
</p>
|
||||
<div class="panel-actions">
|
||||
@foreach (var provider in Model.LinkableProviders)
|
||||
{
|
||||
<form method="post" asp-page-handler="Link" class="inline-actions">
|
||||
<input type="hidden" name="provider" value="@provider" />
|
||||
<button class="button secondary" type="submit">Link @LinkedAccountsModel.ProviderName(provider)</button>
|
||||
</form>
|
||||
}
|
||||
</div>
|
||||
@if (Model.Identities is not null)
|
||||
{
|
||||
<h2>Link another login</h2>
|
||||
<p class="muted">
|
||||
You will sign in to Heimdall again, then to the provider you pick. If that login already belongs to another account,
|
||||
nothing is linked and nothing is merged.
|
||||
</p>
|
||||
<div class="panel-actions">
|
||||
@foreach (var provider in Model.LinkableProviders)
|
||||
{
|
||||
<form method="post" asp-page-handler="Link" class="inline-actions">
|
||||
<input type="hidden" name="provider" value="@provider" />
|
||||
<button class="button secondary" type="submit">Link @LinkedAccountsModel.ProviderName(provider)</button>
|
||||
</form>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
}
|
||||
|
||||
<div class="panel-actions">
|
||||
|
|
|
|||
|
|
@ -55,13 +55,21 @@ public sealed class LinkedAccountsModel(
|
|||
}
|
||||
|
||||
/// <summary>Reads the account's logins: an ordinary Heimdall sign-in, then the list inside its completion.</summary>
|
||||
public Task<IActionResult> OnPostRefreshAsync(CancellationToken cancellationToken) =>
|
||||
BeginAsync(
|
||||
(id, ct) => heimdallAuthService.StartListAsync(id, SessionProvider, hostOptions.Value.PublicBaseUrl, Request.Cookies, ct),
|
||||
public Task<IActionResult> OnPostRefreshAsync(string? via, CancellationToken cancellationToken)
|
||||
{
|
||||
// With no current list Bifrost cannot tell which provider the account still holds, so the member names one.
|
||||
if (AccountId is not { } id || (snapshots.Get(id) is null && (via is null || !HeimdallAuthService.LinkableProviders.Contains(via))))
|
||||
{
|
||||
return Task.FromResult(Done(HeimdallLinkOutcome.ProviderNeeded));
|
||||
}
|
||||
|
||||
return BeginAsync(
|
||||
(account, ct) => heimdallAuthService.StartListAsync(account, via, hostOptions.Value.PublicBaseUrl, Request.Cookies, ct),
|
||||
HeimdallLinkOutcome.Error,
|
||||
cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>Links another provider. The provider is one Heimdall's bifrost profile admits; anything else starts nothing.</summary>
|
||||
/// <summary>Links another provider. The provider is one Heimdall's bifrost profile admits; anything else starts nothing, and so does a missing list.</summary>
|
||||
public Task<IActionResult> OnPostLinkAsync(string? provider, CancellationToken cancellationToken)
|
||||
{
|
||||
if (provider is null || !HeimdallAuthService.LinkableProviders.Contains(provider))
|
||||
|
|
@ -69,8 +77,13 @@ public sealed class LinkedAccountsModel(
|
|||
return Task.FromResult(Done(HeimdallLinkOutcome.Error));
|
||||
}
|
||||
|
||||
if (AccountId is not { } id || snapshots.Get(id) is null)
|
||||
{
|
||||
return Task.FromResult(Done(HeimdallLinkOutcome.ListExpired));
|
||||
}
|
||||
|
||||
return BeginAsync(
|
||||
(id, ct) => heimdallAuthService.StartLinkAsync(id, SessionProvider, provider, hostOptions.Value.PublicBaseUrl, Request.Cookies, ct),
|
||||
(account, ct) => heimdallAuthService.StartLinkAsync(account, provider, hostOptions.Value.PublicBaseUrl, Request.Cookies, ct),
|
||||
HeimdallLinkOutcome.Error,
|
||||
cancellationToken);
|
||||
}
|
||||
|
|
@ -98,8 +111,6 @@ public sealed class LinkedAccountsModel(
|
|||
cancellationToken);
|
||||
}
|
||||
|
||||
private string? SessionProvider => User.FindFirstValue(BifrostClaimTypes.AuthProvider);
|
||||
|
||||
private IActionResult Done(HeimdallLinkOutcome outcome) =>
|
||||
RedirectToPage(null, new Dictionary<string, string?> { [HeimdallLinkOutcomes.ResultParameter] = HeimdallLinkOutcomes.Code(outcome) });
|
||||
|
||||
|
|
|
|||
|
|
@ -829,6 +829,8 @@ app.MapPost("/auth/heimdall/complete", async (
|
|||
ILogger<HeimdallAuthService> logger,
|
||||
CancellationToken cancellationToken) =>
|
||||
{
|
||||
// Every answer here sets or clears an attempt cookie or signs someone in: none may be stored.
|
||||
httpContext.Response.Headers.CacheControl = "no-store";
|
||||
var form = await ReadFormOrNullAsync(httpContext.Request, cancellationToken);
|
||||
var attempt = form?["attempt"].ToString();
|
||||
var held = heimdallAuthService.FindAttemptCookie(httpContext.Request.Cookies, attempt);
|
||||
|
|
|
|||
Loading…
Reference in a new issue