Member identity list and step-up-gated unlink: step_up_at on a step-up sign-in's access claim, GET /v1/me/identities, POST /v1/me/identities/:id/unlink

resolveBearer is the one bearer verifier; the link race maps to link_initiator_mismatch and links/confirm 409.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Heimdall 2026-11-13 18:59:21 +00:00
commit 3afc956f92
7 changed files with 471 additions and 43 deletions

View file

@ -24,7 +24,7 @@ import {
type OAuthStartRequest,
type Provider,
} from "./contracts.js";
import { issueAccessClaim, type IssueAccessClaimInput } from "./claims.js";
import { issueAccessClaim, STEP_UP_PROOF_TTL_SECONDS, type IssueAccessClaimInput } from "./claims.js";
import { builtInAppProfiles, getAppProfile, isAllowedReturnOrigin, serializeAppProfile, admitsProviderForMode, callbackMint, isUsableConnectionTarget } from "./app-profiles.js";
import { renderBrowserHandoffPage } from "./browser-handoff.js";
import { consentText, digestOfText, grantViews, GRANTS_PATH, renderConsentPage, renderGrantsPage, type ConsentAsk } from "./consent.js";
@ -41,7 +41,7 @@ import {
import { ManagedCredentialRefreshFailed, refreshManagedCredential } from "./managed-credential.js";
import { authorizationScopes, buildAuthorizationUrl, providerCatalog, providerExpectedEnv } from "./providers.js";
import { createRuntimeKeyMaterial, signJwt, verifyJwt } from "./signing.js";
import { createStore, type HeimdallStore } from "./store/index.js";
import { createStore, IdentityHeldElsewhereError, type HeimdallStore } from "./store/index.js";
import {
OAUTH_STATE_SKEW_MS,
type CreateAccountInput,
@ -879,28 +879,36 @@ export async function startOAuthFlow(
};
}
/** What a live bearer proves: the account its session belongs to, and the claims it was signed with. */
interface ResolvedBearer {
accountId: string;
claims: Record<string, unknown>;
}
/**
* The account a link start binds to: the one a live Heimdall access token for
* this app names. The token must verify under Heimdall's own key, be an access
* token for `appSlug`, be inside its validity window, and belong to a session
* The one verifier of a Heimdall access token. The token must verify under
* Heimdall's own key, be an access token for `appSlug` (or, when none is
* named, for any app), be inside its validity window, and belong to a session
* the store still holds for that account at that revision. A bearer that fails
* any of these binds nothing: a link is explicit or it does not happen.
* any of these proves nothing.
*/
async function resolveLinkAccount(
async function resolveBearer(
ctx: Pick<HeimdallRuntimeContext, "config" | "keys" | "store">,
appSlug: AppSlug,
authorization: string | undefined
): Promise<string | null> {
authorization: string | undefined,
appSlug?: AppSlug
): Promise<ResolvedBearer | null> {
const token = /^Bearer\s+(\S+)$/i.exec(authorization ?? "")?.[1];
if (!token) return null;
const verification = verifyJwt(token, ctx.keys.publicKey);
if (!verification.valid) return null;
const claims = verification.payload;
const audience = appSlug ?? claims.aud;
if (!isAppSlug(audience)) return null;
const now = nowEpochSeconds();
if (
claims.typ !== "heimdall_access" ||
claims.iss !== ctx.config.issuer ||
claims.aud !== appSlug ||
claims.aud !== audience ||
typeof claims.account_id !== "string" ||
typeof claims.sid !== "string" ||
typeof claims.access_revision !== "number" ||
@ -910,7 +918,7 @@ async function resolveLinkAccount(
) {
return null;
}
const session = await ctx.store.findSession(appSlug, claims.sid);
const session = await ctx.store.findSession(audience, claims.sid);
if (
!session ||
session.accountId !== claims.account_id ||
@ -919,7 +927,16 @@ async function resolveLinkAccount(
) {
return null;
}
return claims.account_id;
return { accountId: claims.account_id, claims };
}
/** The account a link start binds to: the one a live Heimdall access token for this app names. A link is explicit or it does not happen. */
async function resolveLinkAccount(
ctx: Pick<HeimdallRuntimeContext, "config" | "keys" | "store">,
appSlug: AppSlug,
authorization: string | undefined
): Promise<string | null> {
return (await resolveBearer(ctx, authorization, appSlug))?.accountId ?? null;
}
/** The account a Heimdall access token for any app names, when the token and its session are live. */
@ -927,12 +944,7 @@ async function resolveAccessAccount(
ctx: Pick<HeimdallRuntimeContext, "config" | "keys" | "store">,
authorization: string | undefined
): Promise<string | null> {
const token = /^Bearer\s+(\S+)$/i.exec(authorization ?? "")?.[1];
if (!token) return null;
const verification = verifyJwt(token, ctx.keys.publicKey);
if (!verification.valid) return null;
const audience = verification.payload.aud;
return isAppSlug(audience) ? resolveLinkAccount(ctx, audience, authorization) : null;
return (await resolveBearer(ctx, authorization))?.accountId ?? null;
}
const grantsPageTokenType = "heimdall_grants_page";
@ -1847,7 +1859,15 @@ export async function buildApp(options: BuildAppOptions = {}): Promise<FastifyIn
await store.touchAccount(account.id, nowIso, accountUpdates);
}
await store.upsertLinkedIdentity(buildLinkedIdentityInput(account.id));
try {
await store.upsertLinkedIdentity(buildLinkedIdentityInput(account.id));
} catch (error) {
// The check above is the fast path; this is the write refusing a move another account won the race for.
if (error instanceof IdentityHeldElsewhereError) {
throw new LinkRefusal("link_initiator_mismatch", "This identity belongs to a different account than the one that started the link.");
}
throw error;
}
// Heimdall now holds a credential the person was asked about: the
// receipt is the record of that agreement, written with the credential.
@ -1904,6 +1924,8 @@ export async function buildApp(options: BuildAppOptions = {}): Promise<FastifyIn
linkedIdentities: await store.listLinkedIdentitiesForAccount(account.id),
facts: [...new Set([...entitlementEvaluation.facts, ...linkedFacts]), ...buildGrantFacts(grants)],
...(mint === "custody_only" ? { withoutCapabilities: true } : {}),
// Proof of a fresh re-authentication, from the signed state alone: only a sign_in the app started with stepUp.
...(statePayload.step_up && statePayload.mode === "sign_in" ? { stepUpAt: nowEpochSeconds() } : {}),
};
if (account.displayName !== undefined) {
issueInput.displayName = account.displayName;
@ -2214,7 +2236,14 @@ export async function buildApp(options: BuildAppOptions = {}): Promise<FastifyIn
reply.code(409);
return { error: "identity_linked_elsewhere" };
}
await store.upsertLinkedIdentity({ ...pending.identity, accountId, updatedAt: nowIso });
try {
await store.upsertLinkedIdentity({ ...pending.identity, accountId, updatedAt: nowIso });
} catch (error) {
// The holder check above is the fast path; a second account may win the race to the identity.
if (!(error instanceof IdentityHeldElsewhereError)) throw error;
reply.code(409);
return { error: "identity_linked_elsewhere" };
}
await store.createAuditEvent({
accountId,
appSlug: pending.appSlug,
@ -2669,6 +2698,109 @@ export async function buildApp(options: BuildAppOptions = {}): Promise<FastifyIn
return { error: "sign_in_required", detail: "Send a Heimdall access token as a Bearer token, or open a link from POST /v1/me/grants/link." };
};
/**
* Writes a revocation receipt for each live grant the account holds on
* `provider` (whichever app asked: the credential is one row per account and
* provider), and the audit event naming them. The credential is gone or
* stripped first, so a failure here leaves a receipt that still reads active
* over a credential that is gone, never the reverse. Returns the grants this
* call revoked.
*/
const revokeLiveGrants = async (accountId: string, provider: Provider, receipts: Parameters<typeof grantViews>[0], at: string): Promise<string[]> => {
const revoked: string[] = [];
for (const live of grantViews(receipts).filter((view) => view.provider === provider && view.status === "active")) {
const { created } = await store.createConsentReceipt({
kind: "revocation",
accountId,
appSlug: live.appSlug,
provider: live.provider,
createdAt: at,
revokesReceiptId: live.id,
});
// A concurrent revoke may have written it first; only the writer reports it.
if (created) revoked.push(live.id);
}
if (revoked.length) {
await store.createAuditEvent({ accountId, eventType: "consent_revoked", eventPayloadJson: { provider, grantIds: revoked }, createdAt: at });
}
return revoked;
};
const identityViews = async (accountId: string) =>
(await store.listStoredLinkedIdentitiesForAccount(accountId)).map((identity) => ({
id: identity.id,
provider: identity.provider,
providerUserId: identity.providerUserId,
...(identity.username !== undefined ? { username: identity.username } : {}),
...(identity.displayName !== undefined ? { displayName: identity.displayName } : {}),
linkedAt: identity.createdAt,
scopes: identity.scopes,
}));
// The identities an account holds: ids and public profile facts, never a
// credential, a stored profile or an email.
app.get("/v1/me/identities", async (request, reply) => {
const bearer = await resolveBearer({ config, keys, store }, request.headers.authorization);
if (!bearer) return signInRequired(reply);
grantsReply(reply);
return { accountId: bearer.accountId, identities: await identityViews(bearer.accountId) };
});
// Taking an identity off the account. The bearer alone is not enough: the
// access token must come from a step-up sign-in inside the proof window, and
// a refresh never carries that proof forward.
app.post<{ Params: { identityId: string }; Body?: null }>(
"/v1/me/identities/:identityId/unlink",
{
schema: {
params: { type: "object", required: ["identityId"], additionalProperties: false, properties: { identityId: { type: "string", minLength: 1 } } },
body: { type: ["object", "null"], additionalProperties: false, properties: {} },
},
},
async (request, reply) => {
const bearer = await resolveBearer({ config, keys, store }, request.headers.authorization);
if (!bearer) return signInRequired(reply);
grantsReply(reply);
const { accountId, claims } = bearer;
const nowS = nowEpochSeconds();
const provedAt = claims.step_up_at;
if (typeof provedAt !== "number" || provedAt > nowS || nowS - provedAt > STEP_UP_PROOF_TTL_SECONDS) {
reply.code(403);
return {
error: "step_up_required",
detail: `Unlinking needs a sign-in with stepUp completed within the last ${STEP_UP_PROOF_TTL_SECONDS} seconds: POST /v1/oauth/{provider}/start with mode sign_in and stepUp true through an identity on this account whose provider has re-authentication parameters (today twitch, youtube and google; discord and patreon cannot prove), then use the access token that sign-in returns.`,
};
}
const result = await store.unlinkLinkedIdentity(accountId, request.params.identityId);
if (result.kind === "not_found") {
reply.code(404);
return { error: "identity_not_found" };
}
if (result.kind === "last_identity") {
reply.code(409);
return { error: "last_identity", detail: "This is the account's only identity; unlinking it would leave the account unreachable." };
}
const { identity } = result;
const at = new Date().toISOString();
await store.createAuditEvent({
accountId,
eventType: "identity_unlinked",
eventPayloadJson: { provider: identity.provider, identityId: identity.id },
createdAt: at,
});
// The credential went with the row. When no identity of that provider
// is left, the grants that rested on it are taken back in the record too.
const remaining = await store.listStoredLinkedIdentitiesForAccount(accountId);
if (!remaining.some((other) => other.provider === identity.provider)) {
await revokeLiveGrants(accountId, identity.provider, await store.listConsentReceipts(accountId), at);
}
return {
unlinked: { id: identity.id, provider: identity.provider, providerUserId: identity.providerUserId },
identities: await identityViews(accountId),
};
}
);
// The signed-in surface for consent: every grant and receipt the account
// holds, and the one-click way to take each back.
app.get<{ Querystring: { t?: string } }>(
@ -2730,26 +2862,7 @@ export async function buildApp(options: BuildAppOptions = {}): Promise<FastifyIn
if (identity) {
await store.updateLinkedIdentityCredential(identity.id, async () => ({ kind: "revoke", updatedAt: at }));
}
for (const live of grantViews(receipts).filter((view) => view.provider === grant.provider && view.status === "active")) {
const { created } = await store.createConsentReceipt({
kind: "revocation",
accountId,
appSlug: live.appSlug,
provider: live.provider,
createdAt: at,
revokesReceiptId: live.id,
});
// A concurrent revoke may have written it first; only the writer reports it.
if (created) revoked.push(live.id);
}
if (revoked.length) {
await store.createAuditEvent({
accountId,
eventType: "consent_revoked",
eventPayloadJson: { provider: grant.provider, grantIds: revoked },
createdAt: at,
});
}
revoked.push(...(await revokeLiveGrants(accountId, grant.provider, receipts, at)));
}
if (prefersHtml(request.headers.accept) || request.headers["content-type"]?.startsWith("application/x-www-form-urlencoded")) {
reply.type("text/html; charset=utf-8");

View file

@ -11,6 +11,9 @@ import { identityFacts } from "./facts.js";
import { signJwt, type RuntimeKeyMaterial } from "./signing.js";
import { type CreateSessionInput, type HeimdallStore } from "./store/types.js";
/** How long a step-up sign-in's proof stays good for an action that demands it (identity unlink). */
export const STEP_UP_PROOF_TTL_SECONDS = 600;
function nowEpochSeconds(): number {
return Math.floor(Date.now() / 1000);
}
@ -48,6 +51,8 @@ export interface IssueAccessClaimInput {
sessionId?: string;
accessRevision?: number;
ttlSeconds?: number;
/** Epoch seconds of a step-up sign-in completing; only that callback sets it, a refresh never does. */
stepUpAt?: number;
}
export interface IssuedAccessClaimResult {
@ -130,6 +135,10 @@ export async function issueAccessClaim(options: {
identities: options.input.linkedIdentities,
};
if (options.input.stepUpAt !== undefined) {
claimSet.step_up_at = options.input.stepUpAt;
}
if (options.input.displayName) {
claimSet.display_name = options.input.displayName;
}

View file

@ -153,6 +153,8 @@ export interface AccessClaimPayload {
typ: "heimdall_access";
account_id: string;
access_revision: number;
/** Epoch seconds of the step-up sign-in this access token was minted by; absent on every other token, and never carried by a refresh. */
step_up_at?: number;
display_name?: string;
app: {
slug: AppSlug;

View file

@ -5,7 +5,7 @@ import { buildApp } from "../src/app.js";
import { loadConfig, type HeimdallConfig } from "../src/config.js";
import { resetGoogleJwksCache } from "../src/google-oidc.js";
import { type OAuthProviderRuntime } from "../src/oauth.js";
import { InMemoryStore } from "../src/store/index.js";
import { IdentityHeldElsewhereError, InMemoryStore } from "../src/store/index.js";
import { launchFlow, launchWithRedirect, stateOf, type LaunchedFlow } from "./oauth-flow.js";
const CLIENT_ID = "google-client";
@ -188,6 +188,18 @@ afterEach(async () => {
while (apps.length) await apps.pop()?.close();
});
describe("a link write that loses the race to another account", () => {
it("links/confirm answers 409 identity_linked_elsewhere when the store refuses the move after the check passed", async () => {
const { app, store } = await build();
const { accessToken } = await signInWithDiscord(app);
const done = await linkGoogleCallback(app, accessToken);
expect(done.statusCode).toBe(201);
vi.spyOn(store, "upsertLinkedIdentity").mockRejectedValueOnce(new IdentityHeldElsewhereError());
const confirmed = await confirmLink(app, accessToken, done.json().pendingLinkId);
expect([confirmed.statusCode, confirmed.json().error]).toEqual([409, "identity_linked_elsewhere"]);
});
});
describe("Google as a Heimdall OIDC provider", () => {
it("builds the authorization request: openid email profile, the exact callback, state, nonce and an S256 challenge", async () => {
const { app } = await build();

View file

@ -0,0 +1,281 @@
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterAll, afterEach, describe, expect, it, vi } from "vitest";
import { buildApp } from "../src/app.js";
import { issueAccessClaim } from "../src/claims.js";
import { type Provider } from "../src/contracts.js";
import { type OAuthProviderRuntime } from "../src/oauth.js";
import { createRuntimeKeyMaterial } from "../src/signing.js";
import { IdentityHeldElsewhereError, InMemoryStore } from "../src/store/index.js";
import { createTestConfig, launchFlow } from "./oauth-flow.js";
// The member's identity surface: list what an account holds, and take one
// identity off it. Taking one off is the account's own decision made with
// fresh proof (a step-up sign-in inside the proof window), never the
// bearer's alone. These tests drive both routes over HTTP as an app would.
const returnTo = "https://streampixels.gamecult.org/signin/return";
const spAuth = { "x-heimdall-app-secret": "streampixels-secret" };
const now = "2026-11-13T12:00:00.000Z";
function runtime(provider: "twitch" | "youtube", providerUserId: string): OAuthProviderRuntime {
return {
async exchangeAuthorizationCode() {
return { accessToken: "at", refreshToken: "rt", tokenType: "Bearer", scope: ["user:read:email"], raw: {} };
},
async resolveIdentity() {
return { provider, providerUserId, username: providerUserId, profile: { id: providerUserId } };
},
async evaluateEntitlements() {
return { facts: [], snapshots: [] };
},
};
}
const dir = mkdtempSync(join(tmpdir(), "heimdall-unlink-"));
const config = { ...createTestConfig(), signingPrivateKeyPath: join(dir, "signing.pem"), bootstrapSigningPrivateKeyOnMissing: true };
afterAll(() => rmSync(dir, { recursive: true, force: true }));
const apps: Awaited<ReturnType<typeof buildApp>>[] = [];
afterEach(async () => {
vi.useRealTimers();
await Promise.all(apps.splice(0).map((app) => app.close()));
});
async function rig() {
const store = new InMemoryStore();
const app = await buildApp({
config,
store,
oauthRuntimes: { twitch: runtime("twitch", "t-1"), youtube: runtime("youtube", "y-1") },
});
apps.push(app);
const keys = createRuntimeKeyMaterial({ ...config, bootstrapSigningPrivateKeyOnMissing: false });
return { app, store, keys };
}
type Rig = Awaited<ReturnType<typeof rig>>;
async function seedAccount(r: Rig, held: Array<[Provider, string]>) {
const account = await r.store.createAccount({ createdAt: now, lastSeenAt: now });
const rows = [];
for (const [provider, providerUserId] of held) {
rows.push(
await r.store.upsertLinkedIdentity({
accountId: account.id,
provider,
providerUserId,
username: `${provider}-name`,
displayName: `${provider} display`,
primaryEmail: "secret@example.test",
accessTokenEncrypted: "ACCESS-CIPHERTEXT",
refreshTokenEncrypted: "REFRESH-CIPHERTEXT",
scopes: ["identify"],
profileJson: { raw: "PROFILE-CANARY" },
createdAt: now,
updatedAt: now,
})
);
}
return { account, rows };
}
/** A real sign_in through twitch, as StreamPixels runs it, redeemed for the tokens. */
async function signIn(r: Rig, stepUp: boolean) {
const started = await r.app.inject({
method: "POST",
url: "/v1/oauth/twitch/start",
headers: stepUp ? spAuth : {},
payload: { appSlug: "streampixels", mode: "sign_in", returnTo, ...(stepUp ? { stepUp: true } : {}) },
});
expect(started.statusCode).toBe(201);
const flow = await launchFlow(r.app, started.json());
const called = await r.app.inject({
method: "GET",
url: `/v1/oauth/twitch/callback?code=c&state=${encodeURIComponent(flow.state)}`,
headers: { cookie: flow.cookie, accept: "application/json" },
});
expect(called.statusCode).toBe(201);
const redeemed = await r.app.inject({
method: "POST",
url: "/v1/apps/streampixels/auth-completions/redeem",
payload: { completionCode: called.json().completion.code },
});
expect(redeemed.statusCode).toBe(201);
return redeemed.json() as { accessToken: string; refreshToken: string; claimSet: Record<string, unknown> };
}
const bearer = (token: string) => ({ authorization: `Bearer ${token}` });
const list = (r: Rig, token?: string) => r.app.inject({ method: "GET", url: "/v1/me/identities", headers: token ? bearer(token) : {} });
const unlink = (r: Rig, id: string, token?: string, payload?: unknown) =>
r.app.inject({ method: "POST", url: `/v1/me/identities/${encodeURIComponent(id)}/unlink`, headers: token ? bearer(token) : {}, ...(payload !== undefined ? { payload: payload as object } : {}) });
describe("step_up_at on the access claim", () => {
it("a step-up sign-in's claim carries it; an ordinary sign-in and a refresh do not", async () => {
const r = await rig();
await seedAccount(r, [["twitch", "t-1"], ["youtube", "y-1"]]);
const before = Math.floor(Date.now() / 1000);
const stepped = await signIn(r, true);
expect(stepped.claimSet.step_up_at).toBeGreaterThanOrEqual(before);
expect(stepped.claimSet.step_up_at).toBeLessThanOrEqual(Math.floor(Date.now() / 1000));
expect((await signIn(r, false)).claimSet.step_up_at).toBeUndefined();
const refreshed = await r.app.inject({ method: "POST", url: "/v1/apps/streampixels/sessions/refresh", payload: { refreshToken: stepped.refreshToken } });
expect(refreshed.statusCode).toBe(201);
expect(refreshed.json().claimSet.step_up_at, "a refresh never renews proof").toBeUndefined();
});
});
describe("GET /v1/me/identities", () => {
it("is 401 sign_in_required without a live bearer", async () => {
const r = await rig();
for (const token of [undefined, "not-a-token"]) {
const res = await list(r, token);
expect([res.statusCode, res.json().error]).toEqual([401, "sign_in_required"]);
}
});
it("lists the caller's identities and nothing that is a credential, a profile or an email", async () => {
const r = await rig();
const { account, rows } = await seedAccount(r, [["twitch", "t-1"], ["discord", "d-9"]]);
await seedAccount(r, [["youtube", "y-other"]]);
const { accessToken } = await signIn(r, false);
const res = await list(r, accessToken);
expect(res.statusCode).toBe(200);
expect(res.headers["cache-control"]).toBe("no-store");
const body = res.json();
expect(body.accountId).toBe(account.id);
expect(body.identities).toEqual([
// The sign-in above refreshed the twitch row from what the provider returned.
{ id: rows[0]!.id, provider: "twitch", providerUserId: "t-1", username: "t-1", linkedAt: now, scopes: ["user:read:email"] },
{ id: rows[1]!.id, provider: "discord", providerUserId: "d-9", username: "discord-name", displayName: "discord display", linkedAt: now, scopes: ["identify"] },
]);
expect(res.body).not.toMatch(/CIPHERTEXT|PROFILE-CANARY|secret@example|primary_email|primaryEmail/);
});
});
describe("POST /v1/me/identities/:identityId/unlink", () => {
it("is 401 without a live bearer", async () => {
const r = await rig();
const { rows } = await seedAccount(r, [["twitch", "t-1"], ["youtube", "y-1"]]);
expect((await unlink(r, rows[1]!.id)).statusCode).toBe(401);
expect((await unlink(r, rows[1]!.id, "garbage")).statusCode).toBe(401);
});
it("is 403 step_up_required for a bearer with no proof, and for a refresh-issued one, and removes nothing", async () => {
const r = await rig();
const { account, rows } = await seedAccount(r, [["twitch", "t-1"], ["youtube", "y-1"]]);
const plain = await signIn(r, false);
const denied = await unlink(r, rows[1]!.id, plain.accessToken);
expect([denied.statusCode, denied.json().error]).toEqual([403, "step_up_required"]);
expect(denied.json().detail).toMatch(/twitch, youtube and google/);
const stepped = await signIn(r, true);
const refreshed = await r.app.inject({ method: "POST", url: "/v1/apps/streampixels/sessions/refresh", payload: { refreshToken: stepped.refreshToken } });
const viaRefresh = await unlink(r, rows[1]!.id, refreshed.json().accessToken);
expect([viaRefresh.statusCode, viaRefresh.json().error]).toEqual([403, "step_up_required"]);
expect(await r.store.listStoredLinkedIdentitiesForAccount(account.id)).toHaveLength(2);
});
it("is 403 for proof older than the window and for proof dated in the future; 599 s old still passes", async () => {
const r = await rig();
const { account, rows } = await seedAccount(r, [["twitch", "t-1"], ["youtube", "y-1"], ["discord", "d-1"]]);
const issueWith = async (stepUpAt: number) =>
(await issueAccessClaim({
config,
keys: r.keys,
store: r.store,
input: { appSlug: "streampixels", accountId: account.id, linkedIdentities: [], facts: [], stepUpAt },
})).accessToken;
const nowS = Math.floor(Date.now() / 1000);
for (const at of [nowS - 601, nowS + 120]) {
const res = await unlink(r, rows[1]!.id, await issueWith(at));
expect([res.statusCode, res.json().error], String(at - nowS)).toEqual([403, "step_up_required"]);
}
expect(await r.store.listStoredLinkedIdentitiesForAccount(account.id)).toHaveLength(3);
const edge = await unlink(r, rows[1]!.id, await issueWith(nowS - 599));
expect(edge.statusCode).toBe(200);
});
it("a step-up sign-in's bearer unlinks: 200 with the remaining list, an audit event without credentials", async () => {
const r = await rig();
const { account, rows } = await seedAccount(r, [["twitch", "t-1"], ["discord", "d-9"]]);
const audit = vi.spyOn(r.store, "createAuditEvent");
const { accessToken } = await signIn(r, true);
const res = await unlink(r, rows[1]!.id, accessToken, null);
expect(res.statusCode).toBe(200);
expect(res.headers["cache-control"]).toBe("no-store");
expect(res.json().unlinked).toEqual({ id: rows[1]!.id, provider: "discord", providerUserId: "d-9" });
expect(res.json().identities.map((i: { id: string }) => i.id)).toEqual([rows[0]!.id]);
expect(res.body).not.toMatch(/CIPHERTEXT|PROFILE-CANARY|secret@example/);
expect(await r.store.listStoredLinkedIdentitiesForAccount(account.id)).toHaveLength(1);
const event = audit.mock.calls.map(([e]) => e).find((e) => e.eventType === "identity_unlinked");
expect(event?.accountId).toBe(account.id);
expect(event?.eventPayloadJson).toEqual({ provider: "discord", identityId: rows[1]!.id });
});
it("the account's only identity is 409 last_identity and the row stays", async () => {
const r = await rig();
const { account, rows } = await seedAccount(r, [["twitch", "t-1"]]);
const { accessToken } = await signIn(r, true);
const res = await unlink(r, rows[0]!.id, accessToken);
expect([res.statusCode, res.json().error]).toEqual([409, "last_identity"]);
expect(await r.store.listStoredLinkedIdentitiesForAccount(account.id)).toHaveLength(1);
});
it("another account's identity and a nonexistent one are the same 404", async () => {
const r = await rig();
await seedAccount(r, [["twitch", "t-1"], ["discord", "d-9"]]);
const other = await seedAccount(r, [["youtube", "y-other"], ["github", "g-other"]]);
const { accessToken } = await signIn(r, true);
const foreign = await unlink(r, other.rows[0]!.id, accessToken);
const missing = await unlink(r, "no-such-id", accessToken);
expect([foreign.statusCode, foreign.json().error]).toEqual([404, "identity_not_found"]);
expect([missing.statusCode, missing.body]).toEqual([404, foreign.body]);
expect(await r.store.listStoredLinkedIdentitiesForAccount(other.account.id)).toHaveLength(2);
});
it("an unlinked identity belongs to no one, and another account can then link it, but not before", async () => {
const r = await rig();
const mine = await seedAccount(r, [["twitch", "t-1"], ["discord", "d-9"]]);
const theirs = await seedAccount(r, [["youtube", "y-other"]]);
const input = { accountId: theirs.account.id, provider: "discord" as const, providerUserId: "d-9", scopes: [], profileJson: {}, createdAt: now, updatedAt: now };
await expect(r.store.upsertLinkedIdentity(input)).rejects.toBeInstanceOf(IdentityHeldElsewhereError);
const { accessToken } = await signIn(r, true);
expect((await unlink(r, mine.rows[1]!.id, accessToken)).statusCode).toBe(200);
await expect(r.store.upsertLinkedIdentity(input)).resolves.toMatchObject({ accountId: theirs.account.id });
});
it("writes revocation receipts for the provider's live grants when its last identity goes, and not before", async () => {
const r = await rig();
const { account, rows } = await seedAccount(r, [["twitch", "t-1"], ["youtube", "y-1"], ["youtube", "y-2"]]);
const grant = async (provider: Provider) =>
(await r.store.createConsentReceipt({ kind: "grant", accountId: account.id, appSlug: "streampixels", provider, createdAt: now, scopesAsked: ["a"], scopesGranted: ["a"], consentPageDigest: "d", consentText: "t" })).receipt;
const ytGrant = await grant("youtube");
const twGrant = await grant("twitch");
const { accessToken } = await signIn(r, true);
// A second youtube identity remains: the grant's provider still holds a credential.
expect((await unlink(r, rows[1]!.id, accessToken)).statusCode).toBe(200);
expect((await r.store.listConsentReceipts(account.id)).filter((x) => x.kind === "revocation")).toHaveLength(0);
// The last youtube identity goes (twitch stays as the sign-in identity): youtube's grant is revoked, twitch's is not.
const audit = vi.spyOn(r.store, "createAuditEvent");
expect((await unlink(r, rows[2]!.id, accessToken)).statusCode).toBe(200);
const revocations = (await r.store.listConsentReceipts(account.id)).filter((x) => x.kind === "revocation");
expect(revocations.map((x) => x.kind === "revocation" && x.revokesReceiptId)).toEqual([ytGrant.id]);
expect(revocations.map((x) => x.provider)).toEqual(["youtube"]);
expect(twGrant.id).not.toBe(ytGrant.id);
expect(audit.mock.calls.map(([e]) => e.eventType)).toEqual(expect.arrayContaining(["identity_unlinked", "consent_revoked"]));
});
it("proof past the window refuses even a bearer that was fresh when issued", async () => {
const r = await rig();
const { rows } = await seedAccount(r, [["twitch", "t-1"], ["discord", "d-9"]]);
vi.useFakeTimers({ toFake: ["Date"] });
const { accessToken } = await signIn(r, true);
vi.setSystemTime(Date.now() + 601_000);
const res = await unlink(r, rows[1]!.id, accessToken);
expect([res.statusCode, res.json().error]).toEqual([403, "step_up_required"]);
});
});

View file

@ -6,7 +6,7 @@ import { buildApp } from "../src/app.js";
import { type HeimdallConfig } from "../src/config.js";
import { type OAuthProviderRuntime } from "../src/oauth.js";
import { createRuntimeKeyMaterial, signJwt, verifyJwt } from "../src/signing.js";
import { InMemoryStore } from "../src/store/index.js";
import { IdentityHeldElsewhereError, InMemoryStore } from "../src/store/index.js";
import { launchFlow } from "./oauth-flow.js";
function createTestConfig(): HeimdallConfig {
@ -166,6 +166,15 @@ describe("link callback holds the provider identity to the initiator", () => {
expect((await r.store.findAccountByLinkedIdentity("discord", "d-held"))?.id).toBe(other.id);
});
it("maps a store refusal of the move (a race past the check) to link_initiator_mismatch", async () => {
const r = await rig("d-race");
vi.spyOn(r.store, "upsertLinkedIdentity").mockRejectedValueOnce(new IdentityHeldElsewhereError());
const started = await start(r, { mode: "link", initiatingAccountId: r.initiator.id });
const done = await callback(r, started.json());
expect([done.statusCode, done.json().error]).toEqual([403, "link_initiator_mismatch"]);
expect(done.json().completion).toBeUndefined();
});
it("refuses a link state that carries no initiator (forged or stripped), before attaching anything", async () => {
const dir = mkdtempSync(join(tmpdir(), "heimdall-link-"));
try {

View file

@ -150,6 +150,8 @@ describe("the flag is the signed state's, not the redeem side's", () => {
const redeemed = await app.inject({ method: "POST", url: "/v1/apps/streampixels/auth-completions/redeem", payload: { completionCode: code, stepUp: true } });
expect(redeemed.statusCode).toBe(201);
expect(redeemed.json().stepUpRequested).toBe(asked ? true : undefined);
// The proof is the signed state's too: the claim carries step_up_at exactly when the start asked.
expect(typeof redeemed.json().claimSet.step_up_at).toBe(asked ? "number" : "undefined");
}
});
});