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>
This commit is contained in:
Heimdall 2026-11-13 19:35:40 +00:00
commit 59ecaa606b
16 changed files with 1611 additions and 164 deletions

View file

@ -20,7 +20,9 @@ namespace Bifrost.Web.Features.Heimdall;
/// Heimdall sign-in. Start issues an attempt that lives only in a Data Protection-protected cookie in the
/// browser that began it. The browser that Heimdall authenticated presents the completion code from its
/// URL fragment; Bifrost redeems it at Heimdall itself and signs in only when that browser holds the
/// attempt's cookie and the redeemed return target names the same attempt.
/// attempt's cookie and the redeemed return target names the same attempt. The Linked accounts page's steps (list,
/// link, unlink) are the same machine with other attempt purposes: each is a fresh Heimdall sign-in whose bearer is
/// used inside that one request and dropped.
/// </summary>
public sealed class HeimdallAuthService(
HttpClient httpClient,
@ -29,7 +31,9 @@ public sealed class HeimdallAuthService(
BifrostIdentityService bifrostIdentityService,
IOptions<HeimdallOptions> heimdallOptions,
IDataProtectionProvider dataProtectionProvider,
TimeProvider timeProvider)
TimeProvider timeProvider,
HeimdallMemberClient memberClient,
LinkedAccountsSnapshots snapshots)
{
public const string ErrorRedirect = "/?auth=heimdall-error";
public const string AppSecretHeader = "x-heimdall-app-secret";
@ -58,38 +62,16 @@ public sealed class HeimdallAuthService(
}
}
private const int RefusalBodyLimit = 4096;
/// <summary>
/// Names the HTTP status and, when the first 4 KiB of the body carry one, Heimdall's error code. The code
/// is shown only if it is a short snake_case token, so nothing else from the body (free text, tokens)
/// reaches a log. A body that cannot be read or parsed leaves the status alone: a refusal never becomes a 500.
/// Names the HTTP status and, when the body carries one, Heimdall's error code (a short snake_case token only,
/// see <see cref="HeimdallRefusal"/>), so nothing else from the body reaches a log.
/// </summary>
private static async Task<string> RefusedStartMessageAsync(HttpResponseMessage response, CancellationToken cancellationToken)
{
var detail = $"HTTP {(int)response.StatusCode}";
try
{
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken);
var head = new byte[RefusalBodyLimit];
var length = 0;
while (length < head.Length && await stream.ReadAsync(head.AsMemory(length), cancellationToken) is > 0 and var read)
{
length += read;
}
using var body = JsonDocument.Parse(head.AsMemory(0, length));
if (body.RootElement.ValueKind == JsonValueKind.Object
&& body.RootElement.TryGetProperty("error", out var error)
&& error.ValueKind == JsonValueKind.String
&& error.GetString() is { Length: > 0 and <= 64 } code
&& code.All(c => c is (>= 'a' and <= 'z') or (>= '0' and <= '9') or '_'))
{
detail += ", " + code;
}
}
catch (Exception) when (!cancellationToken.IsCancellationRequested)
if (await HeimdallRefusal.CodeAsync(response, cancellationToken) is { } code)
{
detail += ", " + code;
}
return $"Heimdall rejected the auth start ({detail}).";
@ -105,9 +87,13 @@ public sealed class HeimdallAuthService(
HeimdallAttemptPurpose purpose = HeimdallAttemptPurpose.SignIn,
string? expectedAccountId = null,
string? pendingLinkId = null,
string? replaceSlot = null)
string? replaceSlot = null,
bool stepUp = false,
string? target = null)
{
if (!AllowedProviders.Contains(provider))
// The public sign-in routes offer AllowedProviders; the proofs of the Linked accounts page may also use Google
// (a step-up proof only Google can give a bifrost sign-in, an ordinary one for an account that holds Google).
if (!(purpose == HeimdallAttemptPurpose.SignIn ? AllowedProviders : ProofProviders).Contains(provider))
{
throw new HeimdallAuthException("Unsupported Heimdall provider.");
}
@ -128,14 +114,15 @@ public sealed class HeimdallAuthService(
"sign_in",
ToAttemptReturnUrl(callbackBaseUrl, nonce),
new HeimdallHandoff("browser_completion"),
entitlementPolicy);
entitlementPolicy,
stepUp ? true : null);
// Headers only: a refusal body is read through RefusedStartMessageAsync's own 4 KiB bound, not buffered whole.
using var startMessage = new HttpRequestMessage(HttpMethod.Post, $"/v1/oauth/{provider}/start")
{
Content = JsonContent.Create(request)
};
using var response = await httpClient.SendAsync(startMessage, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
using var response = await SendStartAsync(startMessage, cancellationToken);
if (!response.IsSuccessStatusCode)
{
@ -156,36 +143,121 @@ public sealed class HeimdallAuthService(
timeProvider.GetUtcNow() + HeimdallAttemptCookie.Lifetime,
purpose,
expectedAccountId,
pendingLinkId);
pendingLinkId,
target);
return new HeimdallAuthStartResult(
new Uri(payload.AuthorizationUrl, UriKind.Absolute), replaceSlot ?? ChooseSlot(requestCookies), Protect(attempt));
}
/// <summary>The provider a link's fresh sign-in uses: the one the person signed in with, else Discord.</summary>
public static string LinkSignInProvider(string? sessionProvider) =>
sessionProvider is not null && AllowedProviders.Contains(sessionProvider) ? sessionProvider.ToLowerInvariant() : "discord";
/// <summary>A start that cannot reach Heimdall is a refusal like any other, never a 500.</summary>
private async Task<HttpResponseMessage> SendStartAsync(HttpRequestMessage message, CancellationToken cancellationToken)
{
try
{
return await httpClient.SendAsync(message, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
}
catch (Exception error) when (error is HttpRequestException or TaskCanceledException && !cancellationToken.IsCancellationRequested)
{
throw new HeimdallAuthException("Heimdall could not be reached for the auth start.");
}
}
/// <summary>
/// The account page's link button: an ordinary Heimdall sign-in whose attempt says it is only the proof for a
/// link, and which person it must prove. The session is never replaced by it.
/// 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.
/// </summary>
public Task<HeimdallAuthStartResult> StartLinkAsync(
public static string ProofProvider(string? sessionProvider, IReadOnlyCollection<string>? held)
{
var session = sessionProvider is not null && ProofProviders.Contains(sessionProvider) ? sessionProvider.ToLowerInvariant() : null;
if (held is { Count: > 0 })
{
foreach (var candidate in new[] { session, "discord", "patreon", "google" })
{
if (candidate is not null && held.Contains(candidate, StringComparer.OrdinalIgnoreCase))
{
return candidate;
}
}
}
return session ?? "discord";
}
/// <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.
/// </summary>
private Task<HeimdallAuthStartResult> StartProofAsync(
HeimdallAttemptPurpose purpose,
string provider,
string sessionAccountId,
string? sessionProvider,
string callbackBaseUrl,
IEnumerable<KeyValuePair<string, string>> requestCookies,
bool stepUp,
string? target,
CancellationToken cancellationToken) =>
StartAsync(
LinkSignInProvider(sessionProvider),
provider,
callbackBaseUrl,
LocalReturnPath.Parse(LinkReturnPath),
requireMemberAccess: false,
requestCookies,
cancellationToken,
HeimdallAttemptPurpose.LinkStart,
sessionAccountId);
purpose,
sessionAccountId,
stepUp: stepUp,
target: target);
public const string LinkReturnPath = "/Membership/Status";
/// <summary>Reads the account's logins: an ordinary sign-in, then GET /v1/me/identities inside its completion.</summary>
public Task<HeimdallAuthStartResult> StartListAsync(
string sessionAccountId, string? sessionProvider, string callbackBaseUrl,
IEnumerable<KeyValuePair<string, string>> requestCookies, CancellationToken cancellationToken) =>
StartProofAsync(
HeimdallAttemptPurpose.ListProof, ProofProvider(sessionProvider, snapshots.Get(sessionAccountId)?.Select(x => x.Provider).ToArray()),
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>
public Task<HeimdallAuthStartResult> StartLinkAsync(
string sessionAccountId, string? sessionProvider, string targetProvider, string callbackBaseUrl,
IEnumerable<KeyValuePair<string, string>> requestCookies, CancellationToken cancellationToken)
{
if (!LinkableProviders.Contains(targetProvider))
{
throw new HeimdallAuthException("Unsupported provider to link.");
}
return StartProofAsync(
HeimdallAttemptPurpose.LinkStart, ProofProvider(sessionProvider, snapshots.Get(sessionAccountId)?.Select(x => x.Provider).ToArray()),
sessionAccountId, callbackBaseUrl, requestCookies, false, targetProvider.ToLowerInvariant(), cancellationToken);
}
/// <summary>
/// Unlinks one login: a step-up sign-in through Google, then POST /v1/me/identities/{id}/unlink with its bearer. The
/// identity must be on the account's current snapshot and the account must hold Google; otherwise no proof can be had
/// and Heimdall is not asked.
/// </summary>
public Task<HeimdallAuthStartResult> StartUnlinkAsync(
string sessionAccountId, string identityId, string callbackBaseUrl,
IEnumerable<KeyValuePair<string, string>> requestCookies, CancellationToken cancellationToken) =>
StartProofAsync(
HeimdallAttemptPurpose.UnlinkProof, StepUpProvider, sessionAccountId, callbackBaseUrl, requestCookies, true, identityId, cancellationToken);
/// <summary>
/// The providers Heimdall's bifrost profile admits (src/app-profiles.ts, <c>identityProviders</c>): a link or a proof can use them.
/// Twitch is offered for sign-in here but the profile refuses it, so it is not linkable.
/// </summary>
public static readonly IReadOnlyList<string> LinkableProviders = ["discord", "patreon", "google"];
private static readonly HashSet<string> ProofProviders = new(LinkableProviders, StringComparer.OrdinalIgnoreCase);
/// <summary>
/// The one provider whose step-up a bifrost sign-in can use (docs/service-contract.md, step-up: Discord and Patreon cannot
/// re-authenticate, Twitch and YouTube are not in the bifrost profile). Heimdall answers 503 until a Google client is configured.
/// </summary>
public const string StepUpProvider = "google";
public const string LinkReturnPath = "/Membership/LinkedAccounts";
/// <summary>
/// The slot the new attempt's cookie takes: the first slot that holds no live attempt, else the one whose
@ -237,7 +309,7 @@ public sealed class HeimdallAuthService(
public HeimdallAttemptPurpose? PurposeOf(string? cookieValue, string? nonce) => Unprotect(cookieValue, nonce)?.Purpose;
/// <summary>Redeems the code of an attempt and verifies the token it carries. The token is returned to the caller's stack frame and nowhere else.</summary>
private async Task<(HeimdallVerifiedIdentity Identity, string AccessToken)> RedeemVerifiedAsync(
private async Task<(HeimdallVerifiedIdentity Identity, string AccessToken, HeimdallRedeemResponse Redeemed)> RedeemVerifiedAsync(
string callbackBaseUrl, HeimdallAttempt attempt, string? code, CancellationToken cancellationToken)
{
if (string.IsNullOrWhiteSpace(code))
@ -257,7 +329,7 @@ public sealed class HeimdallAuthService(
}
var identity = await tokenVerifier.VerifyAsync(redeemed.AccessToken, redeemed.Account?.Id, cancellationToken);
return (identity, redeemed.AccessToken);
return (identity, redeemed.AccessToken, redeemed);
}
public async Task<HeimdallCompletedSignIn> CompleteAsync(
@ -274,7 +346,7 @@ public sealed class HeimdallAuthService(
throw new HeimdallAuthException("Heimdall completion is not a sign-in attempt.");
}
var (identity, _) = await RedeemVerifiedAsync(callbackBaseUrl, attempt, code, cancellationToken);
var (identity, _, _) = await RedeemVerifiedAsync(callbackBaseUrl, attempt, code, cancellationToken);
if (attempt.RequiresMemberAccess && !identity.HasMemberAccess)
{
throw new HeimdallAuthException("Heimdall did not grant Bifrost member access.");
@ -291,9 +363,10 @@ public sealed class HeimdallAuthService(
}
/// <summary>
/// One step of a Google link, chosen by the purpose of the attempt this browser holds. The browser's session must be
/// the account the link was begun for, checked before Heimdall is asked anything and again against the identity the
/// fresh sign-in proves. Each step consumes the held attempt: the next step is a new attempt in the same slot, or none.
/// One step of a Linked accounts action, chosen by the purpose of the attempt this browser holds. The browser's session
/// must be the account the action was begun for, checked before Heimdall is asked anything and again against the identity
/// the fresh sign-in proves. Each step consumes the held attempt: the next step is a new attempt in the same slot, or none.
/// The bearer a step redeems is used inside the step and dropped.
/// </summary>
public async Task<HeimdallLinkStep> ContinueLinkAsync(
string callbackBaseUrl,
@ -318,7 +391,7 @@ public sealed class HeimdallAuthService(
case HeimdallAttemptPurpose.LinkStart:
{
var (_, bearer) = await RedeemOwnerAsync(callbackBaseUrl, attempt, code, cancellationToken);
var authorizationUrl = await StartGoogleLinkAsync(callbackBaseUrl, attempt, bearer, cancellationToken);
var authorizationUrl = await StartLinkProviderAsync(callbackBaseUrl, attempt, bearer, cancellationToken);
return new HeimdallLinkStep(authorizationUrl.Url, cookieName, authorizationUrl.CookieValue, null);
}
@ -330,7 +403,7 @@ public sealed class HeimdallAuthService(
}
var next = await StartAsync(
LinkSignInProvider(attempt.Provider),
attempt.Provider,
callbackBaseUrl,
attempt.ReturnPath,
requireMemberAccess: false,
@ -351,19 +424,79 @@ public sealed class HeimdallAuthService(
{
// Membership follows the verified identity exactly as at sign-in; the session principal is not replaced.
await UpsertUserAccountAsync(identity, attempt.Provider, grantMemberAccess: false, cancellationToken);
snapshots.Remove(identity.AccountId);
}
return new HeimdallLinkStep(null, cookieName, null, outcome);
}
case HeimdallAttemptPurpose.LinkAwaitDirect:
{
// A provider other than Google attaches in Heimdall's callback; the browser returns with a link completion.
var (identity, _, redeemed) = await RedeemOwnerWithBodyAsync(callbackBaseUrl, attempt, code, cancellationToken);
if (redeemed.Mode != "link" || !string.Equals(redeemed.Provider, attempt.Target, StringComparison.Ordinal))
{
throw new HeimdallAuthException("Heimdall completion is not this link.");
}
await UpsertUserAccountAsync(identity, attempt.Target!, grantMemberAccess: false, cancellationToken);
snapshots.Remove(identity.AccountId);
return new HeimdallLinkStep(null, cookieName, null, HeimdallLinkOutcome.Linked);
}
case HeimdallAttemptPurpose.ListProof:
{
var (identity, bearer) = await RedeemOwnerAsync(callbackBaseUrl, attempt, code, cancellationToken);
var listed = await memberClient.ListAsync(bearer, cancellationToken);
if (listed.Call == HeimdallMemberCall.Ok)
{
snapshots.Put(identity.AccountId, listed.Identities!);
}
return new HeimdallLinkStep(null, cookieName, null, listed.Call == HeimdallMemberCall.Ok ? HeimdallLinkOutcome.Listed : OutcomeOf(listed.Call));
}
case HeimdallAttemptPurpose.UnlinkProof:
{
var (identity, bearer) = await RedeemOwnerAsync(callbackBaseUrl, attempt, code, cancellationToken);
var unlinked = await memberClient.UnlinkAsync(bearer, attempt.Target ?? string.Empty, cancellationToken);
switch (unlinked.Call)
{
case HeimdallMemberCall.Ok:
snapshots.Put(identity.AccountId, unlinked.Identities!);
break;
case HeimdallMemberCall.IdentityNotFound:
snapshots.Remove(identity.AccountId);
break;
}
return new HeimdallLinkStep(null, cookieName, null, unlinked.Call == HeimdallMemberCall.Ok ? HeimdallLinkOutcome.Unlinked : OutcomeOf(unlinked.Call));
}
default:
throw new HeimdallAuthException("Heimdall completion is not a link attempt.");
}
}
private static HeimdallLinkOutcome OutcomeOf(HeimdallMemberCall call) => call switch
{
HeimdallMemberCall.SignInRequired => HeimdallLinkOutcome.SignInRequired,
HeimdallMemberCall.StepUpRequired => HeimdallLinkOutcome.StepUpRequired,
HeimdallMemberCall.IdentityNotFound => HeimdallLinkOutcome.IdentityNotFound,
HeimdallMemberCall.LastIdentity => HeimdallLinkOutcome.LastIdentity,
_ => HeimdallLinkOutcome.Error
};
/// <summary>Redeems a link attempt's code and requires it to prove the account the link was begun for.</summary>
private async Task<(HeimdallVerifiedIdentity Identity, string AccessToken)> RedeemOwnerAsync(
string callbackBaseUrl, HeimdallAttempt attempt, string? code, CancellationToken cancellationToken)
{
var (identity, bearer, _) = await RedeemOwnerWithBodyAsync(callbackBaseUrl, attempt, code, cancellationToken);
return (identity, bearer);
}
private async Task<(HeimdallVerifiedIdentity Identity, string AccessToken, HeimdallRedeemResponse Redeemed)> RedeemOwnerWithBodyAsync(
string callbackBaseUrl, HeimdallAttempt attempt, string? code, CancellationToken cancellationToken)
{
var proven = await RedeemVerifiedAsync(callbackBaseUrl, attempt, code, cancellationToken);
if (!string.Equals(proven.Identity.AccountId, attempt.ExpectedAccountId, StringComparison.Ordinal))
@ -374,18 +507,29 @@ public sealed class HeimdallAuthService(
return proven;
}
private async Task<(Uri Url, string CookieValue)> StartGoogleLinkAsync(
/// <summary>
/// Starts the link to the attempt's target provider with the member's bearer. Google attaches in two phases (the
/// callback writes a pending link, a second fresh sign-in confirms it); the other admitted providers attach in the callback.
/// A cookie written before the target existed names no provider and is a Google link.
/// </summary>
private async Task<(Uri Url, string CookieValue)> StartLinkProviderAsync(
string callbackBaseUrl, HeimdallAttempt attempt, string bearer, CancellationToken cancellationToken)
{
var target = attempt.Target ?? "google";
if (!LinkableProviders.Contains(target))
{
throw new HeimdallAuthException("Unsupported provider to link.");
}
var options = heimdallOptions.Value;
var nonce = WebEncoders.Base64UrlEncode(RandomNumberGenerator.GetBytes(32));
using var message = new HttpRequestMessage(HttpMethod.Post, "/v1/oauth/google/start")
using var message = new HttpRequestMessage(HttpMethod.Post, $"/v1/oauth/{target}/start")
{
Content = JsonContent.Create(new HeimdallStartRequest(
options.AppSlug, "link", ToAttemptReturnUrl(callbackBaseUrl, nonce), new HeimdallHandoff("browser_completion"), null))
options.AppSlug, "link", ToAttemptReturnUrl(callbackBaseUrl, nonce), new HeimdallHandoff("browser_completion"), null, null))
};
message.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", bearer);
using var response = await httpClient.SendAsync(message, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
using var response = await SendStartAsync(message, cancellationToken);
if (!response.IsSuccessStatusCode)
{
throw new HeimdallAuthException(await RefusedStartMessageAsync(response, cancellationToken));
@ -403,9 +547,10 @@ public sealed class HeimdallAuthService(
false,
attempt.ReturnPath,
timeProvider.GetUtcNow() + HeimdallAttemptCookie.Lifetime,
HeimdallAttemptPurpose.LinkAwaitGoogle,
target == "google" ? HeimdallAttemptPurpose.LinkAwaitGoogle : HeimdallAttemptPurpose.LinkAwaitDirect,
attempt.ExpectedAccountId,
null);
null,
target);
return (new Uri(payload.AuthorizationUrl, UriKind.Absolute), Protect(awaiting));
}
@ -464,7 +609,7 @@ public sealed class HeimdallAuthService(
private string Protect(HeimdallAttempt attempt) =>
_protector.Protect(JsonSerializer.Serialize(new AttemptWire(
attempt.Nonce, attempt.Provider, attempt.RequiresMemberAccess, attempt.ReturnPath.Value, attempt.ExpiresAt,
attempt.Purpose, attempt.ExpectedAccountId, attempt.PendingLinkId)));
attempt.Purpose, attempt.ExpectedAccountId, attempt.PendingLinkId, attempt.Target)));
/// <summary>The live attempt a cookie value protects when it belongs to this nonce, else null.</summary>
private HeimdallAttempt? Unprotect(string? cookieValue, string? nonce) =>
@ -488,7 +633,7 @@ public sealed class HeimdallAuthService(
return new HeimdallAttempt(
wire.Nonce, wire.Provider, wire.RequiresMemberAccess, LocalReturnPath.Parse(wire.ReturnPath), wire.ExpiresAt,
wire.Purpose, wire.ExpectedAccountId, wire.PendingLinkId);
wire.Purpose, wire.ExpectedAccountId, wire.PendingLinkId, wire.Target);
}
catch (Exception error) when (error is CryptographicException or JsonException or FormatException)
{
@ -504,7 +649,8 @@ public sealed class HeimdallAuthService(
DateTimeOffset ExpiresAt,
HeimdallAttemptPurpose Purpose = HeimdallAttemptPurpose.SignIn,
string? ExpectedAccountId = null,
string? PendingLinkId = null);
string? PendingLinkId = null,
string? Target = null);
private async Task<UserAccount> UpsertUserAccountAsync(
HeimdallVerifiedIdentity identity,
@ -659,51 +805,84 @@ public sealed record HeimdallAttempt(
DateTimeOffset ExpiresAt,
HeimdallAttemptPurpose Purpose = HeimdallAttemptPurpose.SignIn,
string? ExpectedAccountId = null,
string? PendingLinkId = null);
string? PendingLinkId = null,
string? Target = null);
/// <summary>
/// What a held attempt is for. SignIn is the default, so a cookie written before purposes existed reads as a sign-in.
/// The link purposes are the fresh sign-ins and the Google round trip of an explicit link; they never sign anyone in.
/// The other purposes belong to the Linked accounts page: the fresh sign-ins that prove an account for a list, a link or
/// an unlink, and the provider round trips of a link. They never sign anyone in. Values are persisted in cookies: append only.
/// </summary>
public enum HeimdallAttemptPurpose
{
SignIn = 0,
LinkStart = 1,
LinkAwaitGoogle = 2,
LinkConfirm = 3
LinkConfirm = 3,
ListProof = 4,
UnlinkProof = 5,
LinkAwaitDirect = 6
}
/// <summary>How a Linked accounts action ended. <see cref="HeimdallLinkOutcomes.Code"/> is its closed wire form.</summary>
public enum HeimdallLinkOutcome
{
Linked,
IdentityLinkedElsewhere,
Expired,
Error
Error,
Listed,
Unlinked,
LastIdentity,
StepUpRequired,
IdentityNotFound,
SignInRequired,
ProofUnavailable,
ListExpired
}
/// <summary>
/// Where a link step sends the browser: to <see cref="Next"/> with a new attempt cookie in the held slot, or, when the
/// link is finished, back to the account page with <see cref="Outcome"/>.
/// Where a Linked accounts step sends the browser: to <see cref="HeimdallLinkStep.Next"/> with a new attempt cookie in the held
/// slot, or, when the action is finished, back to the page with the outcome's code in <c>?result=</c>.
/// </summary>
public static class HeimdallLinkOutcomes
{
public static string Redirect(HeimdallLinkOutcome outcome) => HeimdallAuthService.LinkReturnPath + "?link=" + Code(outcome);
public const string ResultParameter = "result";
public static string Redirect(HeimdallLinkOutcome outcome) =>
HeimdallAuthService.LinkReturnPath + "?" + ResultParameter + "=" + Code(outcome);
public static string Code(HeimdallLinkOutcome outcome) => outcome switch
{
HeimdallLinkOutcome.Linked => "linked",
HeimdallLinkOutcome.IdentityLinkedElsewhere => "identity_linked_elsewhere",
HeimdallLinkOutcome.Expired => "expired",
HeimdallLinkOutcome.Listed => "listed",
HeimdallLinkOutcome.Unlinked => "unlinked",
HeimdallLinkOutcome.LastIdentity => "last_identity",
HeimdallLinkOutcome.StepUpRequired => "step_up_required",
HeimdallLinkOutcome.IdentityNotFound => "identity_not_found",
HeimdallLinkOutcome.SignInRequired => "sign_in_required",
HeimdallLinkOutcome.ProofUnavailable => "proof_unavailable",
HeimdallLinkOutcome.ListExpired => "list_expired",
_ => "error"
};
/// <summary>The words for an outcome code: a closed set, never Heimdall's text. Anything else gets none.</summary>
/// <summary>The words for an outcome code: a closed set, never Heimdall's text and never the query's. Anything else gets none.</summary>
public static string? Wording(string? code) => code switch
{
"linked" => "Google is now linked to your Heimdall account.",
"identity_linked_elsewhere" => "That Google identity is already linked to a different Heimdall account. Nothing was linked.",
"expired" => "The Google link expired before it was confirmed. Nothing was linked; start the link again.",
"error" => "The Google link could not be completed. Nothing was linked.",
"linked" => "The login is now linked to your Heimdall account.",
"identity_linked_elsewhere" => "That login belongs to another account. Nothing was linked.",
"expired" => "The link expired before it was confirmed. Nothing was linked; start the link again.",
"listed" => null,
"unlinked" => "The login was unlinked.",
"last_identity" => "That is the last login on your account, and Heimdall will not remove it. Nothing was changed.",
"step_up_required" => "Heimdall did not accept the sign-in as fresh proof. Nothing was changed; try again.",
"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.",
"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
};
}
@ -722,7 +901,9 @@ public sealed record HeimdallStartRequest(
[property: JsonPropertyName("returnTo")] string ReturnTo,
[property: JsonPropertyName("handoff")] HeimdallHandoff Handoff,
[property: JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[property: JsonPropertyName("entitlementPolicy")] HeimdallEntitlementPolicy? EntitlementPolicy);
[property: JsonPropertyName("entitlementPolicy")] HeimdallEntitlementPolicy? EntitlementPolicy,
[property: JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[property: JsonPropertyName("stepUp")] bool? StepUp = null);
public sealed record HeimdallHandoff(
[property: JsonPropertyName("kind")] string Kind);
@ -745,7 +926,9 @@ public sealed record HeimdallRedeemRequest(
public sealed record HeimdallRedeemResponse(
[property: JsonPropertyName("returnTo")] string? ReturnTo,
[property: JsonPropertyName("accessToken")] string? AccessToken,
[property: JsonPropertyName("account")] HeimdallAccount? Account);
[property: JsonPropertyName("account")] HeimdallAccount? Account,
[property: JsonPropertyName("mode")] string? Mode = null,
[property: JsonPropertyName("provider")] string? Provider = null);
public sealed record HeimdallAccount(
[property: JsonPropertyName("id")] string Id);

View file

@ -33,6 +33,12 @@ public static class HeimdallCompletePage
var pendingLinkId = fragment.get("heimdall_pending_link_id");
var failed = fragment.get("heimdall_status") === "error";
history.replaceState(null, "", location.pathname + location.search);
// A provider other than Google attaches in Heimdall's callback, which refuses an identity another account
// holds. Only this one refusal is carried on, as a fixed result the page words itself; nothing from the fragment is shown.
if (failed && attempt && fragment.get("heimdall_error") === "link_initiator_mismatch") {
location.replace("/Membership/LinkedAccounts?result=identity_linked_elsewhere");
return;
}
if (failed || !(code || pendingLinkId) || !attempt) {
location.replace("/?auth=heimdall-error");
return;

View file

@ -0,0 +1,112 @@
using System.Net;
using System.Net.Http.Headers;
using System.Text.Json;
using Bifrost.Web.Configuration;
namespace Bifrost.Web.Features.Heimdall;
/// <summary>One login a Heimdall account holds, as the Linked accounts page shows it. No provider user id, scope or email.</summary>
public sealed record LinkedIdentitySummary(string Id, string Provider, string? DisplayName, DateTimeOffset? LinkedAt);
public enum HeimdallMemberCall
{
Ok,
SignInRequired,
StepUpRequired,
IdentityNotFound,
LastIdentity,
Error
}
public sealed record HeimdallMemberResult(HeimdallMemberCall Call, IReadOnlyList<LinkedIdentitySummary>? Identities);
/// <summary>
/// Heimdall's member routes (docs/service-contract.md: GET /v1/me/identities, POST /v1/me/identities/{id}/unlink). Each
/// takes the member's bearer as an argument, uses it for that one request and keeps nothing. Its HttpClient carries no
/// app secret: these routes act only for a member who is present, so a Bifrost fault cannot reach them without one.
/// </summary>
public sealed class HeimdallMemberClient(HttpClient httpClient)
{
private const int MaxIdentities = 32;
private const int MaxDisplayName = 80;
public static void ConfigureClient(HttpClient client, HeimdallOptions options) =>
client.BaseAddress = new Uri(options.BaseUrl.TrimEnd('/'));
public Task<HeimdallMemberResult> ListAsync(string bearer, CancellationToken cancellationToken) =>
SendAsync(HttpMethod.Get, "/v1/me/identities", bearer, cancellationToken);
public Task<HeimdallMemberResult> UnlinkAsync(string bearer, string identityId, CancellationToken cancellationToken) =>
SendAsync(HttpMethod.Post, $"/v1/me/identities/{Uri.EscapeDataString(identityId)}/unlink", bearer, cancellationToken);
private async Task<HeimdallMemberResult> SendAsync(HttpMethod method, string path, string bearer, CancellationToken cancellationToken)
{
try
{
using var message = new HttpRequestMessage(method, path);
message.Headers.Authorization = new AuthenticationHeaderValue("Bearer", bearer);
using var response = await httpClient.SendAsync(message, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
if (response.StatusCode == HttpStatusCode.OK)
{
return new HeimdallMemberResult(HeimdallMemberCall.Ok, await ReadIdentitiesAsync(response, cancellationToken));
}
var code = await HeimdallRefusal.CodeAsync(response, cancellationToken);
var call = (response.StatusCode, code) switch
{
(HttpStatusCode.Unauthorized, _) => HeimdallMemberCall.SignInRequired,
(HttpStatusCode.Forbidden, "step_up_required") => HeimdallMemberCall.StepUpRequired,
(HttpStatusCode.NotFound, "identity_not_found") => HeimdallMemberCall.IdentityNotFound,
(HttpStatusCode.Conflict, "last_identity") => HeimdallMemberCall.LastIdentity,
_ => HeimdallMemberCall.Error
};
return new HeimdallMemberResult(call, null);
}
catch (Exception error) when (error is HttpRequestException or JsonException or NotSupportedException or TaskCanceledException
&& !cancellationToken.IsCancellationRequested)
{
return new HeimdallMemberResult(HeimdallMemberCall.Error, null);
}
}
/// <summary>The identities array of a 200 answer, keeping only the fields the page shows; a malformed entry is dropped.</summary>
private static async Task<IReadOnlyList<LinkedIdentitySummary>> ReadIdentitiesAsync(HttpResponseMessage response, CancellationToken cancellationToken)
{
using var body = await JsonDocument.ParseAsync(await response.Content.ReadAsStreamAsync(cancellationToken), cancellationToken: cancellationToken);
var list = new List<LinkedIdentitySummary>();
if (body.RootElement.ValueKind != JsonValueKind.Object
|| !body.RootElement.TryGetProperty("identities", out var identities)
|| identities.ValueKind != JsonValueKind.Array)
{
return list;
}
foreach (var item in identities.EnumerateArray().Take(MaxIdentities))
{
var id = Text(item, "id");
var provider = Text(item, "provider");
if (!Guid.TryParse(id, out _)
|| provider is not { Length: > 0 and <= 32 }
|| !provider.All(c => c is (>= 'a' and <= 'z') or (>= '0' and <= '9') or '_'))
{
continue;
}
var name = Text(item, "displayName") ?? Text(item, "username");
if (name is { Length: > MaxDisplayName })
{
name = name[..MaxDisplayName];
}
DateTimeOffset? linkedAt = DateTimeOffset.TryParse(Text(item, "linkedAt"), out var parsed) ? parsed : null;
list.Add(new LinkedIdentitySummary(id!, provider, name, linkedAt));
}
return list;
}
private static string? Text(JsonElement item, string name) =>
item.ValueKind == JsonValueKind.Object && item.TryGetProperty(name, out var value) && value.ValueKind == JsonValueKind.String
? value.GetString()
: null;
}

View file

@ -0,0 +1,42 @@
using System.Text.Json;
namespace Bifrost.Web.Features.Heimdall;
/// <summary>Reads the one thing Bifrost takes from a Heimdall refusal body: its error code, and only if it is a safe token.</summary>
public static class HeimdallRefusal
{
private const int BodyLimit = 4096;
/// <summary>
/// The error code in the first 4 KiB of a body when it is a short snake_case token, else null. A body that cannot be
/// read or parsed yields null: a refusal never becomes a 500, and free text or tokens never reach a log or a page.
/// </summary>
public static async Task<string?> CodeAsync(HttpResponseMessage response, CancellationToken cancellationToken)
{
try
{
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken);
var head = new byte[BodyLimit];
var length = 0;
while (length < head.Length && await stream.ReadAsync(head.AsMemory(length), cancellationToken) is > 0 and var read)
{
length += read;
}
using var body = JsonDocument.Parse(head.AsMemory(0, length));
if (body.RootElement.ValueKind == JsonValueKind.Object
&& body.RootElement.TryGetProperty("error", out var error)
&& error.ValueKind == JsonValueKind.String
&& error.GetString() is { Length: > 0 and <= 64 } code
&& code.All(c => c is (>= 'a' and <= 'z') or (>= '0' and <= '9') or '_'))
{
return code;
}
}
catch (Exception) when (!cancellationToken.IsCancellationRequested)
{
}
return null;
}
}

View file

@ -0,0 +1,35 @@
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 _);
}

View file

@ -0,0 +1,104 @@
@page
@model Bifrost.Web.Pages.Membership.LinkedAccountsModel
@{
ViewData["Title"] = "Linked accounts";
}
<section class="panel">
<span class="eyebrow">Heimdall</span>
<h1>Linked accounts</h1>
<p class="panel-intro">
The provider logins that sign in to this account. Heimdall holds them and decides what may change; Bifrost keeps none of
your Heimdall sign-in, so each action sends you through Heimdall once more.
</p>
@if (Model.ResultMessage is not null)
{
<div class="callout" role="status">@Model.ResultMessage</div>
}
@if (!Model.HasHeimdallAccount)
{
<div class="callout warning">
This session is not a Heimdall account, so there are no provider logins to manage here. Sign in with Discord or Patreon
through Heimdall to see them.
</div>
}
else
{
<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>
}
else
{
<ul>
@foreach (var identity in Model.Identities)
{
<li>
<strong>@LinkedAccountsModel.ProviderName(identity.Provider)</strong>
@if (identity.DisplayName is not null)
{
<span>@identity.DisplayName</span>
}
@if (identity.LinkedAt is { } linkedAt)
{
<span class="muted">linked @linkedAt.UtcDateTime.ToString("dd MMM yyyy") UTC</span>
}
@if (Model.CanProve)
{
<form method="post" asp-page-handler="Unlink" class="inline-actions">
<input type="hidden" name="identityId" value="@identity.Id" />
<button class="button secondary" type="submit">Unlink</button>
</form>
}
</li>
}
</ul>
@if (Model.Identities.Count == 1)
{
<p class="muted">This is your only login. Heimdall will not remove the last one.</p>
}
@if (!Model.CanProve)
{
<p class="muted">
Unlinking needs a fresh Google sign-in as proof. Discord and Patreon cannot give that proof, and no Google login
is linked to this account, so nothing can be unlinked from here yet. Linking Google first makes it possible
once Heimdall has Google set up.
</p>
}
else
{
<p class="muted">Unlinking asks you to sign in with Google again as proof, then removes only the login you picked.</p>
}
<form method="post" asp-page-handler="Refresh" class="panel-actions">
<button class="button secondary" type="submit">Read the list again</button>
</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>
}
<div class="panel-actions">
<a class="button secondary" asp-page="/Membership/Status">Back to membership status</a>
</div>
</section>

View file

@ -0,0 +1,126 @@
using System.Security.Claims;
using Bifrost.Web.Configuration;
using Bifrost.Web.Features.Heimdall;
using Bifrost.Web.Features.Membership;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Options;
namespace Bifrost.Web.Pages.Membership;
/// <summary>
/// The member's provider logins (docs/linked-accounts.md). Bifrost keeps no Heimdall token, so every action here begins a
/// fresh Heimdall sign-in for the account already signed in and finishes inside that sign-in's completion
/// (<c>POST /auth/heimdall/complete</c>); this page only starts them and shows what the last read found. Every handler is a
/// POST with antiforgery. Nothing from the query or the form is ever shown: the outcome is a code from a closed set.
/// </summary>
[Authorize]
[ResponseCache(NoStore = true, Location = ResponseCacheLocation.None)]
public sealed class LinkedAccountsModel(
HeimdallAuthService heimdallAuthService,
LinkedAccountsSnapshots snapshots,
IOptions<BifrostHostOptions> hostOptions,
ILogger<LinkedAccountsModel> logger) : PageModel
{
private static readonly Dictionary<string, string> ProviderNames = new(StringComparer.Ordinal)
{
["discord"] = "Discord",
["patreon"] = "Patreon",
["google"] = "Google"
};
private string? AccountId => User.FindFirstValue(BifrostClaimTypes.HeimdallAccountId) is { Length: > 0 } id ? id : null;
/// <summary>The wording of the outcome the page was sent back with: a closed set, null for anything else.</summary>
public string? ResultMessage { get; private set; }
/// <summary>Whether this session is a Heimdall account. A Bifrost-native GitHub session has none, so there is nothing to list.</summary>
public bool HasHeimdallAccount => AccountId is not null;
/// <summary>The logins the last read found, or null when there is none or it has expired.</summary>
public IReadOnlyList<LinkedIdentitySummary>? Identities { get; private set; }
/// <summary>Whether a fresh Google sign-in can be had for this account: unlinking needs one, and only Google can give it.</summary>
public bool CanProve => Identities?.Any(x => x.Provider == HeimdallAuthService.StepUpProvider) == true;
public IReadOnlyList<string> LinkableProviders => HeimdallAuthService.LinkableProviders;
public static string ProviderName(string provider) => ProviderNames.GetValueOrDefault(provider, provider);
public void OnGet()
{
ResultMessage = HeimdallLinkOutcomes.Wording(Request.Query[HeimdallLinkOutcomes.ResultParameter].ToString());
Identities = AccountId is { } id ? snapshots.Get(id) : null;
}
/// <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),
HeimdallLinkOutcome.Error,
cancellationToken);
/// <summary>Links another provider. The provider is one Heimdall's bifrost profile admits; anything else starts nothing.</summary>
public Task<IActionResult> OnPostLinkAsync(string? provider, CancellationToken cancellationToken)
{
if (provider is null || !HeimdallAuthService.LinkableProviders.Contains(provider))
{
return Task.FromResult(Done(HeimdallLinkOutcome.Error));
}
return BeginAsync(
(id, ct) => heimdallAuthService.StartLinkAsync(id, SessionProvider, provider, hostOptions.Value.PublicBaseUrl, Request.Cookies, ct),
HeimdallLinkOutcome.Error,
cancellationToken);
}
/// <summary>
/// Unlinks one login with fresh proof. The identity must be on the list this account was last shown and the account must
/// hold a Google login to prove with; otherwise Heimdall is not asked, and the page says so.
/// </summary>
public Task<IActionResult> OnPostUnlinkAsync(string? identityId, CancellationToken cancellationToken)
{
var shown = AccountId is { } id ? snapshots.Get(id) : null;
if (shown is null || identityId is null || !shown.Any(x => x.Id == identityId))
{
return Task.FromResult(Done(HeimdallLinkOutcome.ListExpired));
}
if (!shown.Any(x => x.Provider == HeimdallAuthService.StepUpProvider))
{
return Task.FromResult(Done(HeimdallLinkOutcome.ProofUnavailable));
}
return BeginAsync(
(account, ct) => heimdallAuthService.StartUnlinkAsync(account, identityId, hostOptions.Value.PublicBaseUrl, Request.Cookies, ct),
HeimdallLinkOutcome.ProofUnavailable,
cancellationToken);
}
private string? SessionProvider => User.FindFirstValue(BifrostClaimTypes.AuthProvider);
private IActionResult Done(HeimdallLinkOutcome outcome) =>
RedirectToPage(null, new Dictionary<string, string?> { [HeimdallLinkOutcomes.ResultParameter] = HeimdallLinkOutcomes.Code(outcome) });
private async Task<IActionResult> BeginAsync(
Func<string, CancellationToken, Task<HeimdallAuthStartResult>> start, HeimdallLinkOutcome onRefusal, CancellationToken cancellationToken)
{
if (AccountId is not { } accountId)
{
return Done(HeimdallLinkOutcome.Error);
}
try
{
var started = await start(accountId, cancellationToken);
Response.Cookies.Append(started.CookieName, started.CookieValue, HeimdallAttemptCookie.Options());
return Redirect(started.AuthorizationUrl.ToString());
}
catch (HeimdallAuthException error)
{
logger.LogWarning("Linked accounts start refused: {Reason}", error.Message);
return Done(onRefusal);
}
}
}

View file

@ -24,17 +24,12 @@
<p class="muted">@Model.StatusMessage</p>
</div>
@if (Model.LinkMessage is not null)
@if (Model.CanManageLogins)
{
<div class="callout" role="status">@Model.LinkMessage</div>
}
@if (Model.CanLinkGoogle)
{
<form method="post" asp-page-handler="LinkGoogle" class="panel-actions">
<button class="button secondary" type="submit">Link a Google login</button>
<span class="muted">You will sign in to Heimdall again, then to Google. Google links your account; it does not sign you in.</span>
</form>
<div class="panel-actions">
<a class="button secondary" asp-page="/Membership/LinkedAccounts">Linked accounts</a>
<span class="muted">See, add and remove the provider logins that sign in to this account.</span>
</div>
}
@if (Model.Invitation is not null)

View file

@ -1,61 +1,21 @@
using Bifrost.Web.Data;
using Bifrost.Web.Domain;
using System.Security.Claims;
using Bifrost.Web.Configuration;
using Bifrost.Web.Features.Heimdall;
using Bifrost.Web.Features.Membership;
using Bifrost.Web.Features.Shared;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
namespace Bifrost.Web.Pages.Membership;
[Authorize]
public sealed class StatusModel(
ICurrentBifrostActorAccessor actorAccessor,
BifrostDbContext dbContext,
HeimdallAuthService heimdallAuthService,
IOptions<BifrostHostOptions> hostOptions,
ILogger<StatusModel> logger) : PageModel
BifrostDbContext dbContext) : PageModel
{
/// <summary>The wording of the link outcome the page was sent back with: a closed set, null for anything else.</summary>
public string? LinkMessage { get; private set; }
/// <summary>Whether this session belongs to a Heimdall account a Google login can be attached to.</summary>
public bool CanLinkGoogle => User.FindFirstValue(BifrostClaimTypes.HeimdallAccountId) is { Length: > 0 };
/// <summary>
/// Begins an explicit Google link: an ordinary Heimdall sign-in for the account already signed in here, kept as
/// an attempt that is only the proof for the link. POST with antiforgery only; nothing is started without a session.
/// </summary>
public async Task<IActionResult> OnPostLinkGoogleAsync(CancellationToken cancellationToken)
{
var accountId = User.FindFirstValue(BifrostClaimTypes.HeimdallAccountId);
if (string.IsNullOrEmpty(accountId))
{
return RedirectToPage(null, new { link = HeimdallLinkOutcomes.Code(HeimdallLinkOutcome.Error) });
}
try
{
var start = await heimdallAuthService.StartLinkAsync(
accountId,
User.FindFirstValue(BifrostClaimTypes.AuthProvider),
hostOptions.Value.PublicBaseUrl,
Request.Cookies,
cancellationToken);
Response.Cookies.Append(start.CookieName, start.CookieValue, HeimdallAttemptCookie.Options());
return Redirect(start.AuthorizationUrl.ToString());
}
catch (HeimdallAuthException error)
{
logger.LogWarning("Heimdall link start refused: {Reason}", error.Message);
return RedirectToPage(null, new { link = HeimdallLinkOutcomes.Code(HeimdallLinkOutcome.Error) });
}
}
/// <summary>Whether this session belongs to a Heimdall account whose logins the Linked accounts page can manage.</summary>
public bool CanManageLogins => User.FindFirstValue(BifrostClaimTypes.HeimdallAccountId) is { Length: > 0 };
public CurrentBifrostActor Actor { get; private set; } = CurrentBifrostActor.Anonymous;
@ -90,7 +50,6 @@ public sealed class StatusModel(
public async Task OnGetAsync(CancellationToken cancellationToken)
{
Actor = await actorAccessor.GetAsync(cancellationToken);
LinkMessage = HeimdallLinkOutcomes.Wording(Request.Query["link"].ToString());
if (Actor.UserAccount is null)
{

View file

@ -93,6 +93,7 @@ builder.Services.AddAntiforgery(options =>
builder.Services.AddRazorPages(options =>
{
options.Conventions.AuthorizePage("/Membership/Status");
options.Conventions.AuthorizePage("/Membership/LinkedAccounts");
options.Conventions.AuthorizeFolder("/App", ActiveMemberRequirement.PolicyName);
options.Conventions.AllowAnonymousToFolder("/Mill");
});
@ -153,6 +154,11 @@ builder.Services.AddScoped<IAuthorizationHandler, ActiveMemberAuthorizationHandl
builder.Services.AddHttpClient<HeimdallAuthService>((serviceProvider, client) =>
HeimdallAuthService.ConfigureClient(client, serviceProvider.GetRequiredService<IOptions<HeimdallOptions>>().Value))
.RedactLoggedHeaders(HeimdallAuthService.RedactsHeader);
// Heimdall's member routes take only the member's bearer: this client carries no app secret.
builder.Services.AddHttpClient<HeimdallMemberClient>((serviceProvider, client) =>
HeimdallMemberClient.ConfigureClient(client, serviceProvider.GetRequiredService<IOptions<HeimdallOptions>>().Value))
.RedactLoggedHeaders(HeimdallAuthService.RedactsHeader);
builder.Services.AddSingleton<LinkedAccountsSnapshots>();
builder.Services.AddSingleton<HeimdallTokenVerifier>();
builder.Services.AddHttpClient(HeimdallTokenVerifier.ClientName, (serviceProvider, client) =>
HeimdallTokenVerifier.ConfigureClient(client, serviceProvider.GetRequiredService<IOptions<HeimdallOptions>>().Value));

View file

@ -64,6 +64,10 @@ console.log(
run("link-return-with-decoy-query-id", "?attempt=NONCE&heimdall_pending_link_id=QPEND", "#heimdall_status=success&heimdall_pending_link_id=PEND1"),
run("link-error-with-pending-id", "?attempt=NONCE", "#heimdall_status=error&heimdall_error=denied&heimdall_pending_link_id=PEND1"),
run("link-pending-id-in-query-only", "?attempt=NONCE&heimdall_pending_link_id=QPEND", ""),
run("link-refused-elsewhere", "?attempt=NONCE", "#heimdall_status=error&heimdall_error=link_initiator_mismatch"),
run("link-refused-elsewhere-no-attempt", "", "#heimdall_status=error&heimdall_error=link_initiator_mismatch"),
run("other-refusal-text", "?attempt=NONCE", "#heimdall_status=error&heimdall_error=%3Cb%3Ex&heimdall_error_description=EVIL"),
run("refusal-in-query-only", "?attempt=NONCE&heimdall_error=link_initiator_mismatch", ""),
run("code-in-query-only", "?attempt=NONCE&heimdall_completion_code=QCODE", ""),
]),
);

View file

@ -112,6 +112,28 @@ public sealed class HeimdallCompletePageScriptTests
Assert.Equal(ErrorTarget, runs["link-error-with-pending-id"].Events[^1]);
}
[Fact]
public async Task A_link_refused_because_the_login_belongs_to_another_account_goes_to_the_page_with_a_fixed_result_and_submits_nothing()
{
var runs = await RunScenariosAsync();
const string Target = "replace:/Membership/LinkedAccounts?result=identity_linked_elsewhere";
var run = runs["link-refused-elsewhere"];
Assert.Null(run.Submit);
Assert.Equal(Target, run.Events[^1]);
Assert.DoesNotContain("submit", run.Events);
Assert.Contains(run.Events, x => x.StartsWith("replaceState:", StringComparison.Ordinal) && !x.Contains('#'));
// Without an attempt, from the query only, or for any other refusal text, it is the ordinary error exit.
foreach (var name in new[] { "link-refused-elsewhere-no-attempt", "other-refusal-text", "refusal-in-query-only" })
{
Assert.Null(runs[name].Submit);
Assert.Equal(ErrorTarget, runs[name].Events[^1]);
}
Assert.DoesNotContain("EVIL", string.Join("\n", runs["other-refusal-text"].Events));
}
[Theory]
[InlineData("error-status-with-code")]
[InlineData("no-code")]

View file

@ -16,7 +16,7 @@ namespace Bifrost.Web.Tests;
/// </summary>
public sealed class HeimdallGoogleLinkTests
{
private const string Status = "/Membership/Status";
private const string LinkedPage = "/Membership/LinkedAccounts";
private const string ErrorRedirect = "/?auth=heimdall-error";
private sealed record Step(HttpResponseMessage Response, StartedAttempt Next, string Token);
@ -126,7 +126,7 @@ public sealed class HeimdallGoogleLinkTests
new Dictionary<string, string> { ["attempt"] = start.Nonce }, Jar(session, start));
Assert.Equal(before, host.Heimdall.StartBodies.Count);
Assert.Equal(Status + "?link=error", Location(response));
Assert.Equal(LinkedPage + "?result=error", Location(response));
}
[Fact]
@ -238,7 +238,7 @@ public sealed class HeimdallGoogleLinkTests
var b = await StepB(host, session, start, HeimdallShapes.NewAccountId());
Assert.Equal(starts, host.Heimdall.StartBodies.Count);
Assert.Equal(Status + "?link=error", Location(b.Response));
Assert.Equal(LinkedPage + "?result=error", Location(b.Response));
AssertAttemptCleared(b.Response, start);
}
@ -258,7 +258,7 @@ public sealed class HeimdallGoogleLinkTests
Assert.Equal(starts, host.Heimdall.StartBodies.Count);
Assert.Equal(redeems, host.Heimdall.RedeemCalls);
Assert.Equal(Status + "?link=error", Location(b.Response));
Assert.Equal(LinkedPage + "?result=error", Location(b.Response));
}
[Fact]
@ -271,7 +271,7 @@ public sealed class HeimdallGoogleLinkTests
var d = await StepD(host, journey.Session, journey.C.Next, other);
Assert.Empty(host.Heimdall.ConfirmBodies);
Assert.Equal(Status + "?link=error", Location(d.Response));
Assert.Equal(LinkedPage + "?result=error", Location(d.Response));
}
private static void AssertAttemptCleared(HttpResponseMessage response, StartedAttempt attempt)
@ -295,7 +295,7 @@ public sealed class HeimdallGoogleLinkTests
var d = await StepD(host, journey.Session, journey.C.Next, journey.Account);
Assert.Equal(Status + "?link=linked", Location(d.Response));
Assert.Equal(LinkedPage + "?result=linked", Location(d.Response));
Assert.Single(host.Heimdall.ConfirmBodies);
using var body = JsonDocument.Parse(host.Heimdall.ConfirmBodies[0]);
Assert.Equal(journey.PendingLinkId, body.RootElement.GetProperty("pendingLinkId").GetString());
@ -320,7 +320,7 @@ public sealed class HeimdallGoogleLinkTests
var d = await StepD(host, journey.Session, journey.C.Next, journey.Account);
Assert.Equal($"{Status}?link={outcome}", Location(d.Response));
Assert.Equal($"{LinkedPage}?result={outcome}", Location(d.Response));
Assert.DoesNotContain("HEIMDALL FREE TEXT", string.Join("\n", host.Logs.Lines));
AssertAttemptCleared(d.Response, journey.C.Next);
}
@ -334,18 +334,18 @@ public sealed class HeimdallGoogleLinkTests
async Task<string> Page(string link)
{
using var request = new HttpRequestMessage(HttpMethod.Get, Status + "?link=" + Uri.EscapeDataString(link));
using var request = new HttpRequestMessage(HttpMethod.Get, LinkedPage + "?result=" + Uri.EscapeDataString(link));
request.Headers.Add("Cookie", $"{BifrostCookies.Session}={session}");
return await (await client.SendAsync(request)).Content.ReadAsStringAsync();
}
Assert.Contains("Google is now linked", await Page("linked"));
Assert.Contains("already linked to a different Heimdall account", await Page("identity_linked_elsewhere"));
Assert.Contains("now linked to your Heimdall account", await Page("linked"));
Assert.Contains("belongs to another account", await Page("identity_linked_elsewhere"));
Assert.Contains("expired before it was confirmed", await Page("expired"));
Assert.Contains("could not be completed", await Page("error"));
var hostile = await Page("<script>alert(1)</script>");
Assert.DoesNotContain("alert(1)", hostile);
Assert.DoesNotContain("Google is now linked", hostile);
Assert.DoesNotContain("now linked to your", hostile);
}
// ---- 6. google stays out of sign-in and member access ----
@ -379,7 +379,7 @@ public sealed class HeimdallGoogleLinkTests
var pending = host.Heimdall.RegisterPendingLink(pendingCanary);
var c = await StepC(host, session, b.Next, pending);
var d = await StepD(host, session, c.Next, account);
Assert.Equal(Status + "?link=linked", Location(d.Response));
Assert.Equal(LinkedPage + "?result=linked", Location(d.Response));
var tokens = new[] { b.Token, d.Token }
.SelectMany(token => new[] { token, token.Split('.')[2], token.Split('.')[1] })
@ -457,7 +457,7 @@ public sealed class HeimdallGoogleLinkTests
var replay = await host.PostCompleteAsync(fields, Jar(session, start));
Assert.Equal(starts, host.Heimdall.StartBodies.Count);
Assert.Equal(Status + "?link=error", Location(replay));
Assert.Equal(LinkedPage + "?result=error", Location(replay));
}
[Fact]
@ -468,12 +468,12 @@ public sealed class HeimdallGoogleLinkTests
var code = host.CodeFor(journey.C.Next, journey.Account);
var fields = new Dictionary<string, string> { ["attempt"] = journey.C.Next.Nonce, ["code"] = code };
var first = await host.PostCompleteAsync(fields, Jar(journey.Session, journey.C.Next));
Assert.Equal(Status + "?link=linked", Location(first));
Assert.Equal(LinkedPage + "?result=linked", Location(first));
var replay = await host.PostCompleteAsync(fields, Jar(journey.Session, journey.C.Next));
Assert.Single(host.Heimdall.ConfirmBodies);
Assert.Equal(Status + "?link=error", Location(replay));
Assert.Equal(LinkedPage + "?result=error", Location(replay));
}
[Fact]
@ -487,8 +487,8 @@ public sealed class HeimdallGoogleLinkTests
var d = await StepD(host, journey.Session, again.Next, journey.Account);
var replayed = await StepD(host, journey.Session, journey.C.Next, journey.Account);
Assert.Equal(Status + "?link=linked", Location(d.Response));
Assert.Equal(Status + "?link=expired", Location(replayed.Response));
Assert.Equal(LinkedPage + "?result=linked", Location(d.Response));
Assert.Equal(LinkedPage + "?result=expired", Location(replayed.Response));
}
// ---- 10. pins for guards the first pass left unproven ----
@ -521,7 +521,7 @@ public sealed class HeimdallGoogleLinkTests
Assert.Equal(start.CookieName, b.Next.CookieName);
Assert.Equal(start.CookieName, c.Next.CookieName);
Assert.Equal(Status + "?link=linked", Location(d.Response));
Assert.Equal(LinkedPage + "?result=linked", Location(d.Response));
Assert.Null(HeimdallHost.SetCookieHeader(d.Response, parallel.CookieName!));
var signIn = await host.CompleteWithHeaderAsync(
@ -532,7 +532,7 @@ public sealed class HeimdallGoogleLinkTests
[Theory]
[InlineData("9")]
[InlineData("-1")]
[InlineData("4")]
[InlineData("7")]
public async Task An_attempt_cookie_with_an_undefined_purpose_is_no_attempt_and_asks_Heimdall_for_nothing(string purpose)
{
using var host = new HeimdallHost(realSessions: true);
@ -575,7 +575,7 @@ public sealed class HeimdallGoogleLinkTests
var c = await StepC(host, session, b.Next, pendingLinkId);
Assert.Equal(Status + "?link=error", Location(c.Response));
Assert.Equal(LinkedPage + "?result=error", Location(c.Response));
Assert.Equal(starts, host.Heimdall.StartBodies.Count);
Assert.Equal(redeems, host.Heimdall.RedeemCalls);
Assert.Empty(host.Heimdall.ConfirmBodies);
@ -597,7 +597,7 @@ public sealed class HeimdallGoogleLinkTests
var d = await StepD(host, journey.Session, journey.C.Next, journey.Account);
Assert.Equal(Status + "?link=error", Location(d.Response));
Assert.Equal(LinkedPage + "?result=error", Location(d.Response));
Assert.Single(host.Heimdall.ConfirmBodies);
AssertAttemptCleared(d.Response, journey.C.Next);
}

View file

@ -0,0 +1,710 @@
using System.Net;
using System.Text.Json;
using Bifrost.Web.Features.Heimdall;
using Bifrost.Web.Features.Shared;
using Bifrost.Web.Tests.Support;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.Extensions.DependencyInjection;
namespace Bifrost.Web.Tests;
/// <summary>
/// The Linked accounts page from the member's side, against a fake Heimdall. Bifrost keeps no Heimdall token: a list, a link
/// and an unlink each follow a fresh Heimdall sign-in begun by Bifrost, and the bearer lives inside that one completion request.
/// </summary>
public sealed class LinkedAccountsTests
{
private const string Page = "/Membership/LinkedAccounts";
private const string ErrorRedirect = "/?auth=heimdall-error";
private static string Jar(string session, StartedAttempt attempt) =>
$"{BifrostCookies.Session}={session}; {attempt.CookieName}={attempt.Cookie}";
private static string? Location(HttpResponseMessage response) => response.Headers.Location?.OriginalString;
private static string Unprotect(HeimdallHost host, string cookie) =>
host.Services.GetRequiredService<IDataProtectionProvider>().CreateProtector("Bifrost.Heimdall.Attempt.v1").Unprotect(cookie);
/// <summary>The fresh sign-in's completion as the browser posts it: Heimdall minted a code for <paramref name="proofAccount"/>.</summary>
private static async Task<HttpResponseMessage> Finish(
HeimdallHost host, string session, StartedAttempt attempt, string proofAccount, string? token = null,
string mode = "sign_in", string provider = "discord", string? sessionOverride = null)
{
var code = host.CodeFor(attempt, proofAccount, token, mode: mode, provider: provider);
return await host.PostCompleteAsync(
new Dictionary<string, string> { ["attempt"] = attempt.Nonce, ["code"] = code }, Jar(sessionOverride ?? session, attempt));
}
private static string StepUpToken(HeimdallHost host, string account) =>
host.Token(account, mutate: claims => claims["step_up_at"] = host.Clock.Now.ToUnixTimeSeconds());
private sealed record Member(string Account, string Session);
/// <summary>A signed-in member whose fake Heimdall account holds these providers, and whose list has been read once.</summary>
private static async Task<Member> ListedMember(HeimdallHost host, params string[] providers)
{
var account = HeimdallShapes.NewAccountId();
var session = await host.SignInSessionAsync(account);
foreach (var provider in providers)
{
host.Heimdall.AddIdentity(provider, "Name of " + provider);
}
var start = await host.StartListAsync(session);
var done = await Finish(host, session, start, account);
Assert.Equal(Page + "?result=listed", Location(done));
return new Member(account, session);
}
// ---- who may use the page ----
[Fact]
public async Task An_anonymous_visitor_is_challenged_and_nothing_is_started()
{
using var host = new HeimdallHost(realSessions: true);
var (get, _) = await host.GetLinkedAccountsAsync(session: null);
var post = await host.PostLinkedAccountsAsync(session: null, "Refresh");
Assert.Equal(HttpStatusCode.Redirect, get.StatusCode);
Assert.Contains("/auth/sign-in", Location(get));
Assert.NotEqual(HttpStatusCode.OK, post.Response.StatusCode);
Assert.Empty(host.Heimdall.StartBodies);
}
[Fact]
public async Task The_page_is_no_store_and_a_member_without_a_list_is_offered_one()
{
using var host = new HeimdallHost(realSessions: true);
var session = await host.SignInSessionAsync(HeimdallShapes.NewAccountId());
var (response, html) = await host.GetLinkedAccountsAsync(session);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Contains("no-store", response.Headers.CacheControl?.ToString());
Assert.Contains("Show my linked logins", html);
Assert.Contains("Link Discord", html);
Assert.Contains("Link Patreon", html);
Assert.Contains("Link Google", html);
Assert.DoesNotContain("Link Twitch", html);
Assert.DoesNotContain("Unlink", html);
}
[Fact]
public async Task A_session_with_no_Heimdall_account_has_nothing_to_list_and_starts_nothing()
{
using var host = new HeimdallHost();
var (_, html) = await host.GetLinkedAccountsAsync(session: null);
var start = await host.StartListAsync(session: null);
Assert.Contains("not a Heimdall account", html);
Assert.DoesNotContain("Link Discord", html);
Assert.Empty(host.Heimdall.StartBodies);
Assert.Null(start.Cookie);
}
[Theory]
[InlineData("Refresh")]
[InlineData("Link")]
[InlineData("Unlink")]
public async Task Every_post_needs_the_antiforgery_token_and_a_get_starts_nothing(string handler)
{
using var host = new HeimdallHost(realSessions: true);
var session = await host.SignInSessionAsync(HeimdallShapes.NewAccountId());
var form = new Dictionary<string, string> { ["provider"] = "discord", ["identityId"] = Guid.NewGuid().ToString() };
var starts = host.Heimdall.StartBodies.Count;
var without = await host.PostLinkedAccountsAsync(session, handler, form, antiforgery: false);
var get = await host.PostLinkedAccountsAsync(session, handler, form, method: HttpMethod.Get);
Assert.Equal(HttpStatusCode.BadRequest, without.Response.StatusCode);
Assert.Null(without.Cookie);
Assert.Null(get.Cookie);
Assert.Equal(starts, host.Heimdall.StartBodies.Count);
}
// ---- the list ----
[Fact]
public async Task The_list_is_a_fresh_ordinary_sign_in_then_GET_identities_with_its_bearer_and_the_page_shows_it()
{
using var host = new HeimdallHost(traceHttp: true, realSessions: true);
var account = HeimdallShapes.NewAccountId();
var session = await host.SignInSessionAsync(account);
host.Heimdall.AddIdentity("discord", "Discord Dana");
host.Heimdall.AddIdentity("google", "Google Gale");
var signIns = host.Heimdall.StartBodies.Count;
var start = await host.StartListAsync(session);
Assert.Equal(signIns + 1, host.Heimdall.StartBodies.Count);
using (var body = JsonDocument.Parse(host.Heimdall.StartBodies[^1]))
{
Assert.Equal("sign_in", body.RootElement.GetProperty("mode").GetString());
Assert.False(body.RootElement.TryGetProperty("stepUp", out _));
}
Assert.Equal("/v1/oauth/discord/start", host.Heimdall.StartPaths[^1]);
Assert.Empty(host.Heimdall.ListAuthorizations);
var token = host.Token(account);
var done = await Finish(host, session, start, account, token);
Assert.Equal(Page + "?result=listed", Location(done));
Assert.Equal(["Bearer " + token], host.Heimdall.ListAuthorizations);
Assert.Null(HeimdallHost.CookieValue(done, BifrostCookies.Session));
var (_, html) = await host.GetLinkedAccountsAsync(session);
Assert.Contains("Discord Dana", html);
Assert.Contains("Google Gale", html);
Assert.Contains("13 Nov 2026", html);
// Only what the page shows was kept: nothing of the fields a login also carries.
foreach (var canary in new[] { "PUID-CANARY", "username-canary", "scope-canary" })
{
Assert.DoesNotContain(canary, html);
}
}
[Fact]
public async Task No_bearer_reaches_a_cookie_a_log_a_location_or_the_page()
{
using var host = new HeimdallHost(traceHttp: true, realSessions: true);
var account = HeimdallShapes.NewAccountId();
var session = await host.SignInSessionAsync(account);
host.Heimdall.AddIdentity("discord");
host.Heimdall.AddIdentity("google");
var listStart = await host.StartListAsync(session);
var listToken = host.Token(account);
var listed = await Finish(host, session, listStart, account, listToken);
var unlinkStart = await host.StartUnlinkAsync(session, (string)host.Heimdall.Identities[0]["id"]!);
var stepUpToken = StepUpToken(host, account);
var unlinked = await Finish(host, session, unlinkStart, account, stepUpToken);
Assert.Equal(Page + "?result=unlinked", Location(unlinked));
var (_, html) = await host.GetLinkedAccountsAsync(session);
var logs = string.Join("\n", host.Logs.Lines);
var secrets = new[] { listToken, stepUpToken }
.SelectMany(token => new[] { token, token.Split('.')[2], token.Split('.')[1] })
.ToArray();
Assert.Contains("Bearer", string.Join("\n", host.Heimdall.ListAuthorizations));
foreach (var secret in secrets)
{
Assert.DoesNotContain(secret, logs);
Assert.DoesNotContain(secret, html);
foreach (var response in new[] { listStart.Response, listed, unlinkStart.Response, unlinked })
{
Assert.DoesNotContain(secret, Location(response) ?? string.Empty);
Assert.DoesNotContain(secret, string.Join("\n", response.Headers.SelectMany(x => x.Value)));
}
foreach (var cookie in new[] { listStart.Cookie, unlinkStart.Cookie })
{
var payload = Unprotect(host, cookie!);
Assert.DoesNotContain(secret, payload);
Assert.DoesNotContain("eyJ", payload);
}
}
// The completion itself sets no cookie but the attempt's deletion.
foreach (var response in new[] { listed, unlinked })
{
Assert.All(
response.Headers.TryGetValues("Set-Cookie", out var set) ? set : [],
header => Assert.StartsWith(HeimdallAttemptCookie.Prefix, header, StringComparison.Ordinal));
}
}
[Fact]
public async Task A_list_is_per_account_and_expires_after_five_minutes()
{
using var host = new HeimdallHost(realSessions: true);
var member = await ListedMember(host, "discord", "google");
var stranger = await host.SignInSessionAsync(HeimdallShapes.NewAccountId());
Assert.Contains("Name of discord", (await host.GetLinkedAccountsAsync(member.Session)).Html);
var (_, other) = await host.GetLinkedAccountsAsync(stranger);
Assert.DoesNotContain("Name of discord", other);
Assert.Contains("Show my linked logins", other);
host.Clock.Advance(LinkedAccountsSnapshots.Lifetime + TimeSpan.FromSeconds(1));
var (_, later) = await host.GetLinkedAccountsAsync(member.Session);
Assert.DoesNotContain("Name of discord", later);
Assert.Contains("Show my linked logins", later);
}
[Theory]
[InlineData(HttpStatusCode.Unauthorized, "sign_in_required")]
[InlineData(HttpStatusCode.InternalServerError, "error")]
public async Task A_list_Heimdall_refuses_shows_its_fixed_outcome_and_no_list(HttpStatusCode status, string outcome)
{
using var host = new HeimdallHost(realSessions: true);
var account = HeimdallShapes.NewAccountId();
var session = await host.SignInSessionAsync(account);
host.Heimdall.AddIdentity("discord");
host.Heimdall.ListStatus = status;
var start = await host.StartListAsync(session);
var done = await Finish(host, session, start, account);
Assert.Equal($"{Page}?result={outcome}", Location(done));
Assert.DoesNotContain("HEIMDALL FREE TEXT", string.Join("\n", host.Logs.Lines));
Assert.Contains("Show my linked logins", (await host.GetLinkedAccountsAsync(session)).Html);
}
[Fact]
public async Task A_Heimdall_that_cannot_be_reached_for_the_list_changes_nothing()
{
using var host = new HeimdallHost(realSessions: true);
var account = HeimdallShapes.NewAccountId();
var session = await host.SignInSessionAsync(account);
host.Heimdall.AddIdentity("discord");
var start = await host.StartListAsync(session);
host.Heimdall.MemberFailure = new HttpRequestException("connection refused");
var done = await Finish(host, session, start, account);
Assert.Equal(Page + "?result=error", Location(done));
Assert.Contains("Show my linked logins", (await host.GetLinkedAccountsAsync(session)).Html);
}
[Fact]
public async Task A_Heimdall_that_is_down_at_the_list_start_is_a_refusal_not_a_server_error()
{
using var host = new HeimdallHost(realSessions: true);
var session = await host.SignInSessionAsync(HeimdallShapes.NewAccountId());
host.Heimdall.StartStatus = HttpStatusCode.ServiceUnavailable;
var start = await host.StartListAsync(session);
Assert.Equal(HttpStatusCode.Redirect, start.Response.StatusCode);
Assert.Equal(Page + "?result=error", Location(start.Response));
Assert.Null(start.Cookie);
}
// ---- unlink ----
[Fact]
public async Task Unlink_starts_a_step_up_sign_in_with_the_app_secret_and_the_unlink_carries_that_sign_ins_bearer()
{
using var host = new HeimdallHost(realSessions: true);
var member = await ListedMember(host, "discord", "google");
var discord = (string)host.Heimdall.Identities[0]["id"]!;
var listToken = host.Heimdall.ListAuthorizations.Single();
var starts = host.Heimdall.StartBodies.Count;
var start = await host.StartUnlinkAsync(member.Session, discord);
Assert.Equal(starts + 1, host.Heimdall.StartBodies.Count);
Assert.Equal("/v1/oauth/google/start", host.Heimdall.StartPaths[^1]);
Assert.Equal(HeimdallHost.AppSecret, host.Heimdall.StartSecrets[^1]);
Assert.Null(host.Heimdall.StartAuthorizations[^1]);
using (var body = JsonDocument.Parse(host.Heimdall.StartBodies[^1]))
{
Assert.Equal("sign_in", body.RootElement.GetProperty("mode").GetString());
Assert.True(body.RootElement.GetProperty("stepUp").GetBoolean());
}
Assert.Empty(host.Heimdall.Unlinks);
var token = StepUpToken(host, member.Account);
var done = await Finish(host, member.Session, start, member.Account, token, provider: "google");
Assert.Equal(Page + "?result=unlinked", Location(done));
var unlink = Assert.Single(host.Heimdall.Unlinks);
Assert.Equal(discord, unlink.Id);
Assert.Equal("Bearer " + token, unlink.Authorization);
Assert.NotEqual(listToken, unlink.Authorization);
var (_, html) = await host.GetLinkedAccountsAsync(member.Session);
Assert.DoesNotContain("Name of discord", html);
Assert.Contains("Name of google", html);
Assert.Contains("Heimdall will not remove the last one", html);
Assert.Contains("The login was unlinked.", (await host.GetLinkedAccountsAsync(member.Session, "?result=unlinked")).Html);
}
[Fact]
public async Task The_last_login_is_refused_by_Heimdall_and_the_page_still_shows_it()
{
using var host = new HeimdallHost(realSessions: true);
var member = await ListedMember(host, "google");
var only = (string)host.Heimdall.Identities[0]["id"]!;
var start = await host.StartUnlinkAsync(member.Session, only);
var done = await Finish(host, member.Session, start, member.Account, StepUpToken(host, member.Account), provider: "google");
Assert.Equal(Page + "?result=last_identity", Location(done));
Assert.Single(host.Heimdall.Unlinks);
Assert.Single(host.Heimdall.Identities);
var (_, html) = await host.GetLinkedAccountsAsync(member.Session);
Assert.Contains("Name of google", html);
Assert.Contains("will not remove it. Nothing was changed.", (await host.GetLinkedAccountsAsync(member.Session, "?result=last_identity")).Html);
}
[Fact]
public async Task A_sign_in_Heimdall_does_not_count_as_fresh_proof_changes_nothing()
{
using var host = new HeimdallHost(realSessions: true);
var member = await ListedMember(host, "discord", "google");
var start = await host.StartUnlinkAsync(member.Session, (string)host.Heimdall.Identities[0]["id"]!);
// An ordinary token: no step_up_at claim, so Heimdall answers 403 step_up_required.
var done = await Finish(host, member.Session, start, member.Account, host.Token(member.Account), provider: "google");
Assert.Equal(Page + "?result=step_up_required", Location(done));
Assert.Equal(2, host.Heimdall.Identities.Count);
Assert.Contains("Name of discord", (await host.GetLinkedAccountsAsync(member.Session)).Html);
}
[Theory]
[InlineData(HttpStatusCode.NotFound, "identity_not_found")]
[InlineData(HttpStatusCode.Unauthorized, "sign_in_required")]
[InlineData(HttpStatusCode.InternalServerError, "error")]
public async Task Each_refused_unlink_has_its_own_fixed_outcome_and_leaks_no_Heimdall_text(HttpStatusCode status, string outcome)
{
using var host = new HeimdallHost(realSessions: true);
var member = await ListedMember(host, "discord", "google");
var start = await host.StartUnlinkAsync(member.Session, (string)host.Heimdall.Identities[0]["id"]!);
host.Heimdall.UnlinkStatus = status;
var done = await Finish(host, member.Session, start, member.Account, StepUpToken(host, member.Account), provider: "google");
Assert.Equal($"{Page}?result={outcome}", Location(done));
Assert.DoesNotContain("HEIMDALL FREE TEXT", string.Join("\n", host.Logs.Lines));
var (_, html) = await host.GetLinkedAccountsAsync(member.Session);
Assert.DoesNotContain("HEIMDALL FREE TEXT", html);
// Not found says the list is stale: it is dropped. Anything else leaves what was shown.
Assert.Equal(status != HttpStatusCode.NotFound, html.Contains("Name of discord"));
}
[Fact]
public async Task A_Heimdall_that_cannot_be_reached_for_the_unlink_changes_nothing()
{
using var host = new HeimdallHost(realSessions: true);
var member = await ListedMember(host, "discord", "google");
var start = await host.StartUnlinkAsync(member.Session, (string)host.Heimdall.Identities[0]["id"]!);
host.Heimdall.MemberFailure = new HttpRequestException("connection refused");
var done = await Finish(host, member.Session, start, member.Account, StepUpToken(host, member.Account), provider: "google");
Assert.Equal(Page + "?result=error", Location(done));
Assert.Contains("Name of discord", (await host.GetLinkedAccountsAsync(member.Session)).Html);
}
[Fact]
public async Task Without_a_Google_login_no_proof_can_be_had_and_the_page_says_so_without_asking_Heimdall()
{
using var host = new HeimdallHost(realSessions: true);
var member = await ListedMember(host, "discord", "patreon");
var (_, html) = await host.GetLinkedAccountsAsync(member.Session);
var starts = host.Heimdall.StartBodies.Count;
var post = await host.StartUnlinkAsync(member.Session, (string)host.Heimdall.Identities[0]["id"]!);
Assert.DoesNotContain(">Unlink<", html);
Assert.Contains("fresh Google sign-in", html);
Assert.Contains("no Google login", html);
Assert.Equal(starts, host.Heimdall.StartBodies.Count);
Assert.Equal(Page + "?result=proof_unavailable", Location(post.Response));
Assert.Null(post.Cookie);
}
[Fact]
public async Task A_Heimdall_with_no_Google_client_refuses_the_step_up_start_and_the_page_says_proof_is_unavailable_once()
{
using var host = new HeimdallHost(realSessions: true);
var member = await ListedMember(host, "discord", "google");
host.Heimdall.StartStatus = HttpStatusCode.ServiceUnavailable;
host.Heimdall.StartBody = """{"error":"provider_not_configured"}""";
var starts = host.Heimdall.StartBodies.Count;
var post = await host.StartUnlinkAsync(member.Session, (string)host.Heimdall.Identities[0]["id"]!);
Assert.Equal(starts + 1, host.Heimdall.StartBodies.Count);
Assert.Null(post.Cookie);
Assert.Equal(Page + "?result=proof_unavailable", Location(post.Response));
Assert.Contains("Fresh proof could not be started", (await host.GetLinkedAccountsAsync(member.Session, "?result=proof_unavailable")).Html);
Assert.Equal(2, host.Heimdall.Identities.Count);
}
[Theory]
[InlineData(null)]
[InlineData("not-a-guid")]
[InlineData("00000000-0000-0000-0000-000000000001")]
public async Task An_identity_that_is_not_on_the_list_the_member_was_shown_starts_nothing(string? identityId)
{
using var host = new HeimdallHost(realSessions: true);
var member = await ListedMember(host, "discord", "google");
var starts = host.Heimdall.StartBodies.Count;
var post = await host.PostLinkedAccountsAsync(
member.Session, "Unlink", identityId is null ? null : new Dictionary<string, string> { ["identityId"] = identityId });
Assert.Equal(starts, host.Heimdall.StartBodies.Count);
Assert.Equal(Page + "?result=list_expired", Location(post.Response));
}
[Fact]
public async Task An_unlink_after_the_list_expired_starts_nothing()
{
using var host = new HeimdallHost(realSessions: true);
var member = await ListedMember(host, "discord", "google");
var id = (string)host.Heimdall.Identities[0]["id"]!;
host.Clock.Advance(LinkedAccountsSnapshots.Lifetime + TimeSpan.FromSeconds(1));
var starts = host.Heimdall.StartBodies.Count;
var post = await host.StartUnlinkAsync(member.Session, id);
Assert.Equal(starts, host.Heimdall.StartBodies.Count);
Assert.Equal(Page + "?result=list_expired", Location(post.Response));
}
// ---- an attempt belongs to the account and browser that began it ----
[Fact]
public async Task An_unlink_attempt_held_by_another_accounts_session_does_nothing_and_asks_Heimdall_for_nothing()
{
using var host = new HeimdallHost(realSessions: true);
var member = await ListedMember(host, "discord", "google");
var start = await host.StartUnlinkAsync(member.Session, (string)host.Heimdall.Identities[0]["id"]!);
var otherAccount = HeimdallShapes.NewAccountId();
var otherSession = await host.SignInSessionAsync(otherAccount);
var redeems = host.Heimdall.RedeemCalls;
var done = await Finish(host, member.Session, start, member.Account, StepUpToken(host, member.Account), provider: "google",
sessionOverride: otherSession);
Assert.Equal(Page + "?result=error", Location(done));
Assert.Equal(redeems, host.Heimdall.RedeemCalls);
Assert.Empty(host.Heimdall.Unlinks);
Assert.Equal(2, host.Heimdall.Identities.Count);
}
[Fact]
public async Task A_proof_by_another_account_unlinks_nothing()
{
using var host = new HeimdallHost(realSessions: true);
var member = await ListedMember(host, "discord", "google");
var start = await host.StartUnlinkAsync(member.Session, (string)host.Heimdall.Identities[0]["id"]!);
var stranger = HeimdallShapes.NewAccountId();
var done = await Finish(host, member.Session, start, stranger, StepUpToken(host, stranger), provider: "google");
Assert.Equal(Page + "?result=error", Location(done));
Assert.Empty(host.Heimdall.Unlinks);
}
[Fact]
public async Task A_list_proof_by_another_account_lists_nothing()
{
using var host = new HeimdallHost(realSessions: true);
var account = HeimdallShapes.NewAccountId();
var session = await host.SignInSessionAsync(account);
host.Heimdall.AddIdentity("discord");
var start = await host.StartListAsync(session);
var done = await Finish(host, session, start, HeimdallShapes.NewAccountId());
Assert.Equal(Page + "?result=error", Location(done));
Assert.Empty(host.Heimdall.ListAuthorizations);
}
[Fact]
public async Task A_consumed_unlink_attempt_cannot_be_replayed_into_a_second_unlink()
{
using var host = new HeimdallHost(realSessions: true);
var member = await ListedMember(host, "discord", "patreon", "google");
var start = await host.StartUnlinkAsync(member.Session, (string)host.Heimdall.Identities[0]["id"]!);
var fields = new Dictionary<string, string>
{
["attempt"] = start.Nonce,
["code"] = host.CodeFor(start, member.Account, StepUpToken(host, member.Account), provider: "google")
};
var first = await host.PostCompleteAsync(fields, Jar(member.Session, start));
Assert.Equal(Page + "?result=unlinked", Location(first));
var replay = await host.PostCompleteAsync(fields, Jar(member.Session, start));
Assert.Equal(Page + "?result=error", Location(replay));
Assert.Single(host.Heimdall.Unlinks);
}
// ---- link another provider ----
[Theory]
[InlineData("discord")]
[InlineData("patreon")]
[InlineData("google")]
public async Task Linking_each_admitted_provider_proves_the_account_then_starts_that_providers_link_with_the_bearer(string provider)
{
using var host = new HeimdallHost(realSessions: true);
var account = HeimdallShapes.NewAccountId();
var session = await host.SignInSessionAsync(account);
var starts = host.Heimdall.StartBodies.Count;
var start = await host.StartLinkAsync(session, provider);
Assert.Equal(starts + 1, host.Heimdall.StartBodies.Count);
Assert.Equal("/v1/oauth/discord/start", host.Heimdall.StartPaths[^1]);
Assert.Null(host.Heimdall.StartAuthorizations[^1]);
var token = host.Token(account);
var step = await Finish(host, session, start, account, token);
Assert.Equal(starts + 2, host.Heimdall.StartBodies.Count);
Assert.Equal($"/v1/oauth/{provider}/start", host.Heimdall.StartPaths[^1]);
Assert.Equal("Bearer " + token, host.Heimdall.StartAuthorizations[^1]);
using var link = JsonDocument.Parse(host.Heimdall.StartBodies[^1]);
Assert.Equal("link", link.RootElement.GetProperty("mode").GetString());
Assert.Equal("bifrost", link.RootElement.GetProperty("appSlug").GetString());
Assert.False(link.RootElement.TryGetProperty("stepUp", out _));
Assert.Equal($"https://heimdall.test/oauth/{provider}/authorize?state=link-fixture", Location(step));
}
[Theory]
[InlineData("twitch")]
[InlineData("github")]
[InlineData("DISCORD2")]
[InlineData("")]
public async Task A_provider_Heimdalls_bifrost_profile_does_not_admit_starts_nothing(string provider)
{
using var host = new HeimdallHost(realSessions: true);
var session = await host.SignInSessionAsync(HeimdallShapes.NewAccountId());
var starts = host.Heimdall.StartBodies.Count;
var start = await host.StartLinkAsync(session, provider);
Assert.Equal(starts, host.Heimdall.StartBodies.Count);
Assert.Equal(Page + "?result=error", Location(start.Response));
Assert.Null(start.Cookie);
}
[Fact]
public async Task A_provider_that_attaches_in_the_callback_comes_back_with_a_link_completion_and_is_linked_once()
{
using var host = new HeimdallHost(realSessions: true);
var account = HeimdallShapes.NewAccountId();
var session = await host.SignInSessionAsync(account);
var start = await host.StartLinkAsync(session, "patreon");
var awaiting = host.AttemptFrom((await ContinueResponse(host, session, start, account)), host.Heimdall.StartBodies.Count - 1);
Assert.Contains("\"Purpose\":6", Unprotect(host, awaiting.Cookie!));
Assert.Contains("\"Target\":\"patreon\"", Unprotect(host, awaiting.Cookie!));
host.Heimdall.AddIdentity("discord");
var redeems = host.Heimdall.RedeemCalls;
var done = await Finish(host, session, awaiting, account, mode: "link", provider: "patreon");
Assert.Equal(Page + "?result=linked", Location(done));
Assert.Equal(redeems + 1, host.Heimdall.RedeemCalls);
Assert.Contains("now linked to your Heimdall account", (await host.GetLinkedAccountsAsync(session, "?result=linked")).Html);
Assert.Null(HeimdallHost.CookieValue(done, BifrostCookies.Session));
var replay = await host.PostCompleteAsync(
new Dictionary<string, string> { ["attempt"] = awaiting.Nonce, ["code"] = "gone" }, Jar(session, awaiting));
Assert.Equal(Page + "?result=error", Location(replay));
}
private static async Task<HttpResponseMessage> ContinueResponse(HeimdallHost host, string session, StartedAttempt start, string account) =>
await Finish(host, session, start, account);
[Theory]
[InlineData("sign_in", "patreon")]
[InlineData("link", "discord")]
public async Task A_direct_link_completion_for_another_mode_or_provider_links_nothing(string mode, string provider)
{
using var host = new HeimdallHost(realSessions: true);
var account = HeimdallShapes.NewAccountId();
var session = await host.SignInSessionAsync(account);
var start = await host.StartLinkAsync(session, "patreon");
var before = host.Heimdall.StartBodies.Count;
var awaiting = host.AttemptFrom(await ContinueResponse(host, session, start, account), before);
var done = await Finish(host, session, awaiting, account, mode: mode, provider: provider);
Assert.Equal(Page + "?result=error", Location(done));
}
[Fact]
public async Task A_direct_link_completion_proven_by_another_account_links_nothing()
{
using var host = new HeimdallHost(realSessions: true);
var account = HeimdallShapes.NewAccountId();
var session = await host.SignInSessionAsync(account);
var start = await host.StartLinkAsync(session, "discord");
var before = host.Heimdall.StartBodies.Count;
var awaiting = host.AttemptFrom(await ContinueResponse(host, session, start, account), before);
var done = await Finish(host, session, awaiting, HeimdallShapes.NewAccountId(), mode: "link", provider: "discord");
Assert.Equal(Page + "?result=error", Location(done));
}
[Fact]
public async Task A_link_attempt_cookie_written_before_targets_existed_still_links_Google()
{
using var host = new HeimdallHost(realSessions: true);
var account = HeimdallShapes.NewAccountId();
var session = await host.SignInSessionAsync(account);
var nonce = "legacy-" + Guid.NewGuid().ToString("N");
var protector = host.Services.GetRequiredService<IDataProtectionProvider>().CreateProtector("Bifrost.Heimdall.Attempt.v1");
var legacy = protector.Protect(JsonSerializer.Serialize(new Dictionary<string, object>
{
["Nonce"] = nonce, ["Provider"] = "discord", ["RequiresMemberAccess"] = false, ["ReturnPath"] = "/Membership/Status",
["ExpiresAt"] = host.Clock.Now.AddMinutes(5), ["Purpose"] = 1, ["ExpectedAccountId"] = account
}));
var attempt = new StartedAttempt(
nonce, HeimdallAttemptCookie.SlotNames[0], legacy, $"https://localhost/auth/heimdall/complete?attempt={nonce}", null!);
var step = await Finish(host, session, attempt, account);
Assert.Equal("/v1/oauth/google/start", host.Heimdall.StartPaths[^1]);
Assert.Equal("https://heimdall.test/oauth/google/authorize?state=link-fixture", Location(step));
}
// ---- the page and its neighbours ----
[Fact]
public async Task Status_points_a_Heimdall_member_to_the_page_and_no_longer_starts_a_link_itself()
{
using var host = new HeimdallHost(realSessions: true);
var session = await host.SignInSessionAsync(HeimdallShapes.NewAccountId());
using var client = host.NewClient();
using var request = new HttpRequestMessage(HttpMethod.Get, "/Membership/Status");
request.Headers.Add("Cookie", $"{BifrostCookies.Session}={session}");
var html = await (await client.SendAsync(request)).Content.ReadAsStringAsync();
Assert.Contains(Page, html);
Assert.DoesNotContain("handler=LinkGoogle", html);
Assert.DoesNotContain("LinkGoogle", html);
}
[Theory]
[InlineData("linked")]
[InlineData("identity_linked_elsewhere")]
[InlineData("expired")]
[InlineData("unlinked")]
[InlineData("last_identity")]
[InlineData("step_up_required")]
[InlineData("identity_not_found")]
[InlineData("sign_in_required")]
[InlineData("proof_unavailable")]
[InlineData("list_expired")]
[InlineData("error")]
public void Every_outcome_code_has_words_and_nothing_else_does(string code)
{
Assert.False(string.IsNullOrWhiteSpace(HeimdallLinkOutcomes.Wording(code)));
Assert.Null(HeimdallLinkOutcomes.Wording(code + " "));
Assert.Null(HeimdallLinkOutcomes.Wording("<b>" + code));
Assert.Contains(
Enum.GetValues<HeimdallLinkOutcome>(), outcome => HeimdallLinkOutcomes.Code(outcome) == code);
}
[Fact]
public async Task Link_refusals_say_the_login_belongs_to_another_account_without_naming_it()
{
using var host = new HeimdallHost(realSessions: true);
var session = await host.SignInSessionAsync(HeimdallShapes.NewAccountId());
var (_, html) = await host.GetLinkedAccountsAsync(session, "?result=identity_linked_elsewhere");
Assert.Contains("That login belongs to another account. Nothing was linked.", html);
Assert.DoesNotContain("merge", html.Replace("nothing is merged", string.Empty), StringComparison.OrdinalIgnoreCase);
}
}

View file

@ -105,6 +105,7 @@ public sealed partial class NoThirdPartyAssetTests(TestWebApplicationFactory fac
("/auth/sign-in", null, true),
("/auth/bifrost/register", null, true),
("/auth/bifrost/consent", identity, false),
("/Membership/LinkedAccounts", identity, false),
};
foreach (var (path, who, anonymous) in pages)

View file

@ -133,11 +133,13 @@ public static class HeimdallShapes
string accessToken,
string[]? looseCapabilities = null,
string[]? looseFacts = null,
string looseName = "Loose Name") => new()
string looseName = "Loose Name",
string mode = "sign_in",
string provider = "discord") => new()
{
["status"] = "success",
["provider"] = "discord",
["mode"] = "sign_in",
["provider"] = provider,
["mode"] = mode,
["appSlug"] = "bifrost",
["account"] = new Dictionary<string, object?> { ["id"] = accountId, ["displayName"] = looseName },
["entitlements"] = new Dictionary<string, object?>
@ -220,6 +222,115 @@ public sealed class FakeHeimdall : HttpMessageHandler
private readonly HashSet<string> _pendingLinks = new(StringComparer.Ordinal);
/// <summary>The logins the fake account holds, as GET /v1/me/identities answers them (with the fields Bifrost must never keep).</summary>
public List<Dictionary<string, object?>> Identities { get; } = [];
/// <summary>Adds a login to the fake account and returns its id.</summary>
public string AddIdentity(string provider, string? displayName = "A Name", string? id = null)
{
id ??= Guid.NewGuid().ToString();
Identities.Add(new Dictionary<string, object?>
{
["id"] = id,
["provider"] = provider,
["providerUserId"] = "PUID-CANARY-" + id[..8],
["username"] = "username-canary-" + id[..8],
["displayName"] = displayName,
["linkedAt"] = "2026-11-13T12:00:00.000Z",
["scopes"] = new[] { "scope-canary" }
});
return id;
}
public List<string?> ListAuthorizations { get; } = [];
/// <summary>When set, GET /v1/me/identities answers this status with an error body.</summary>
public HttpStatusCode? ListStatus { get; set; }
/// <summary>The Authorization header and identity id of every unlink.</summary>
public List<(string? Authorization, string Id)> Unlinks { get; } = [];
/// <summary>When set, every unlink answers this status with Heimdall's error code for it (the fake's own rules are bypassed).</summary>
public HttpStatusCode? UnlinkStatus { get; set; }
/// <summary>Throws instead of answering a member route, as an unreachable Heimdall does.</summary>
public Exception? MemberFailure { get; set; }
private HttpResponseMessage MemberRoute(HttpRequestMessage request, string path)
{
static HttpResponseMessage Json(HttpStatusCode status, string json) =>
new(status) { Content = new StringContent(json, Encoding.UTF8, "application/json") };
if (MemberFailure is not null)
{
throw MemberFailure;
}
var authorization = request.Headers.Authorization?.ToString();
object Listing() => new { accountId = "a", identities = Identities };
if (request.Method == HttpMethod.Get && path == "/v1/me/identities")
{
ListAuthorizations.Add(authorization);
if (authorization is null)
{
return Json(HttpStatusCode.Unauthorized, """{"error":"sign_in_required"}""");
}
return ListStatus switch
{
HttpStatusCode.Unauthorized => Json(HttpStatusCode.Unauthorized, """{"error":"sign_in_required"}"""),
{ } listed => Json(listed, """{"error":"server_error","detail":"HEIMDALL FREE TEXT"}"""),
_ => Json(HttpStatusCode.OK, JsonSerializer.Serialize(Listing()))
};
}
var segments = path.Split('/');
if (request.Method == HttpMethod.Post && segments is ["", "v1", "me", "identities", var id, "unlink"])
{
Unlinks.Add((authorization, id));
if (authorization is null)
{
return Json(HttpStatusCode.Unauthorized, """{"error":"sign_in_required"}""");
}
if (UnlinkStatus is { } forced)
{
return forced switch
{
HttpStatusCode.Unauthorized => Json(forced, """{"error":"sign_in_required"}"""),
HttpStatusCode.Forbidden => Json(forced, """{"error":"step_up_required"}"""),
HttpStatusCode.NotFound => Json(forced, """{"error":"identity_not_found"}"""),
HttpStatusCode.Conflict => Json(forced, """{"error":"last_identity"}"""),
_ => Json(forced, """{"error":"server_error","detail":"HEIMDALL FREE TEXT"}""")
};
}
// Heimdall's rule: the bearer must carry a step_up_at claim. (The 600 second window is Heimdall's own and not modelled.)
var payload = Encoding.UTF8.GetString(WebEncoders.Base64UrlDecode(authorization["Bearer ".Length..].Split('.')[1]));
if (!payload.Contains("\"step_up_at\"", StringComparison.Ordinal))
{
return Json(HttpStatusCode.Forbidden, """{"error":"step_up_required"}""");
}
var held = Identities.FirstOrDefault(x => (string?)x["id"] == id);
if (held is null)
{
return Json(HttpStatusCode.NotFound, """{"error":"identity_not_found"}""");
}
if (Identities.Count == 1)
{
return Json(HttpStatusCode.Conflict, """{"error":"last_identity"}""");
}
Identities.Remove(held);
return Json(
HttpStatusCode.OK,
JsonSerializer.Serialize(new { unlinked = new { id, provider = held["provider"], providerUserId = held["providerUserId"] }, identities = Identities }));
}
return new HttpResponseMessage(HttpStatusCode.NotFound);
}
/// <summary>Registers a pending link Heimdall will confirm once for any bearer.</summary>
public string RegisterPendingLink(string? id = null)
{
@ -297,10 +408,11 @@ public sealed class FakeHeimdall : HttpMessageHandler
};
}
var linkProvider = path.Split('/')[3];
return new HttpResponseMessage(HttpStatusCode.Created)
{
Content = new StringContent(
"""{"authorizationUrl":"https://heimdall.test/oauth/google/authorize?state=link-fixture"}""", Encoding.UTF8, "application/json")
$$"""{"authorizationUrl":"https://heimdall.test/oauth/{{linkProvider}}/authorize?state=link-fixture"}""", Encoding.UTF8, "application/json")
};
}
@ -318,6 +430,11 @@ public sealed class FakeHeimdall : HttpMessageHandler
return new HttpResponseMessage(StartStatus) { Content = content };
}
if (path.StartsWith("/v1/me/identities", StringComparison.Ordinal))
{
return MemberRoute(request, path);
}
if (request.Method == HttpMethod.Post && path == "/v1/apps/bifrost/links/confirm")
{
var body = await request.Content!.ReadAsStringAsync(cancellationToken);
@ -512,6 +629,7 @@ public sealed class HeimdallHost : TestWebApplicationFactory
services.AddSingleton<TimeProvider>(Clock);
services.AddHttpClient(HeimdallTokenVerifier.ClientName).ConfigurePrimaryHttpMessageHandler(() => Heimdall);
services.AddHttpClient<HeimdallAuthService>().ConfigurePrimaryHttpMessageHandler(() => Heimdall);
services.AddHttpClient<HeimdallMemberClient>().ConfigurePrimaryHttpMessageHandler(() => Heimdall);
});
}
@ -561,10 +679,10 @@ public sealed class HeimdallHost : TestWebApplicationFactory
/// <summary>Registers a completion code as Heimdall would mint it after authenticating this attempt.</summary>
public string CodeFor(StartedAttempt attempt, string accountId, string? accessToken = null, string[]? looseCapabilities = null,
string[]? looseFacts = null, string looseName = "Loose Name", string? returnTo = null)
string[]? looseFacts = null, string looseName = "Loose Name", string? returnTo = null, string mode = "sign_in", string provider = "discord")
{
var completion = HeimdallShapes.Completion(
returnTo ?? attempt.ReturnTo, accountId, accessToken ?? Token(accountId), looseCapabilities, looseFacts, looseName);
returnTo ?? attempt.ReturnTo, accountId, accessToken ?? Token(accountId), looseCapabilities, looseFacts, looseName, mode, provider);
return Heimdall.RegisterCode(completion);
}
@ -623,18 +741,33 @@ public sealed class HeimdallHost : TestWebApplicationFactory
return await client.SendAsync(request);
}
public const string LinkPostUrl = "/Membership/Status?handler=LinkGoogle";
public const string LinkedAccountsUrl = "/Membership/LinkedAccounts";
/// <summary>The Linked accounts page as a browser reads it, with its session when there is one.</summary>
public async Task<(HttpResponseMessage Response, string Html)> GetLinkedAccountsAsync(string? session, string query = "")
{
using var client = NewClient();
using var request = new HttpRequestMessage(HttpMethod.Get, LinkedAccountsUrl + query);
if (session is not null)
{
request.Headers.Add("Cookie", $"{BifrostCookies.Session}={session}");
}
var response = await client.SendAsync(request);
return (response, await response.Content.ReadAsStringAsync());
}
/// <summary>
/// The account page's link button as a browser posts it: the page is fetched for its antiforgery token and cookie
/// (when <paramref name="antiforgery"/>), then the form is posted with the session. Without a session nothing is fetched.
/// One of the page's buttons as a browser posts it: the page is fetched for its antiforgery token and cookie (when
/// <paramref name="antiforgery"/>), then the form is posted with the session. Without a session nothing is fetched.
/// </summary>
public async Task<StartedAttempt> StartLinkAsync(
string? session, bool antiforgery = true, HttpMethod? method = null, string? extraCookies = null)
public async Task<StartedAttempt> PostLinkedAccountsAsync(
string? session, string handler, IDictionary<string, string>? form = null, bool antiforgery = true, HttpMethod? method = null,
string? extraCookies = null)
{
using var client = NewClient();
var cookies = new List<string>();
var fields = new Dictionary<string, string>();
var fields = new Dictionary<string, string>(form ?? new Dictionary<string, string>());
if (session is not null)
{
cookies.Add($"{BifrostCookies.Session}={session}");
@ -642,7 +775,7 @@ public sealed class HeimdallHost : TestWebApplicationFactory
if (antiforgery && session is not null)
{
using var get = new HttpRequestMessage(HttpMethod.Get, "/Membership/Status");
using var get = new HttpRequestMessage(HttpMethod.Get, LinkedAccountsUrl);
get.Headers.Add("Cookie", string.Join("; ", cookies));
using var page = await client.SendAsync(get);
var html = await page.Content.ReadAsStringAsync();
@ -658,7 +791,7 @@ public sealed class HeimdallHost : TestWebApplicationFactory
}
var before = Heimdall.StartBodies.Count;
using var request = new HttpRequestMessage(method ?? HttpMethod.Post, LinkPostUrl);
using var request = new HttpRequestMessage(method ?? HttpMethod.Post, $"{LinkedAccountsUrl}?handler={handler}");
if (request.Method == HttpMethod.Post)
{
request.Content = new FormUrlEncodedContent(fields);
@ -672,6 +805,15 @@ public sealed class HeimdallHost : TestWebApplicationFactory
return AttemptFrom(await client.SendAsync(request), before);
}
public Task<StartedAttempt> StartLinkAsync(
string? session, string provider = "google", bool antiforgery = true, HttpMethod? method = null, string? extraCookies = null) =>
PostLinkedAccountsAsync(session, "Link", new Dictionary<string, string> { ["provider"] = provider }, antiforgery, method, extraCookies);
public Task<StartedAttempt> StartListAsync(string? session) => PostLinkedAccountsAsync(session, "Refresh");
public Task<StartedAttempt> StartUnlinkAsync(string? session, string identityId) =>
PostLinkedAccountsAsync(session, "Unlink", new Dictionary<string, string> { ["identityId"] = identityId });
public Task<CompletionResult> CompleteAsync(StartedAttempt attempt, string code) =>
CompleteAsync(attempt.Nonce, code, attempt.Cookie, attempt.CookieName);