Identity mover: POST /v1/apps/:slug/identity-moves, proof of both accounts, strict-passkey coverage, alias row, credential strip (#16)
Soul s3 clean at 2f1b733. Needs identity_moves DDL + GRANT SELECT, INSERT before the build starts.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
d957e5accb
commit
4fe367ac8d
15 changed files with 1694 additions and 9 deletions
632
tests/identity-moves.test.ts
Normal file
632
tests/identity-moves.test.ts
Normal file
|
|
@ -0,0 +1,632 @@
|
|||
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 { builtInAppProfiles } from "../src/app-profiles.js";
|
||||
import { AUTH_PROOF_TTL_SECONDS, issueAccessClaim, proofTier } from "../src/claims.js";
|
||||
import { type AppSlug, type Provider } from "../src/contracts.js";
|
||||
import { type OAuthProviderRuntime } from "../src/oauth.js";
|
||||
import { createRuntimeKeyMaterial } from "../src/signing.js";
|
||||
import { InMemoryStore } from "../src/store/index.js";
|
||||
import { type CreateAuditEventInput } from "../src/store/types.js";
|
||||
import { createTestConfig, launchFlow } from "./oauth-flow.js";
|
||||
|
||||
// POST /v1/apps/{app}/identity-moves, driven over HTTP the way Bifrost would
|
||||
// call it: the proofs are real access tokens whose claims say how and through
|
||||
// which login the member signed in; the answers are what a caller can see.
|
||||
|
||||
const now = "2026-11-14T12:00:00.000Z";
|
||||
const appAuth = { "x-heimdall-app-secret": "bifrost-secret" };
|
||||
const moveId = (tag: string) => `move-${tag}`.padEnd(20, "x");
|
||||
const nowS = () => Math.floor(Date.now() / 1000);
|
||||
|
||||
const dir = mkdtempSync(join(tmpdir(), "heimdall-identity-moves-"));
|
||||
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();
|
||||
vi.restoreAllMocks();
|
||||
await Promise.all(apps.splice(0).map((app) => app.close()));
|
||||
});
|
||||
|
||||
function twitchRuntime(providerUserId: string): OAuthProviderRuntime {
|
||||
return {
|
||||
async exchangeAuthorizationCode() {
|
||||
return { accessToken: "at", refreshToken: "rt", tokenType: "Bearer", scope: ["user:read:email"], raw: {} };
|
||||
},
|
||||
async resolveIdentity() {
|
||||
return { provider: "twitch", providerUserId, username: providerUserId, profile: { id: providerUserId } };
|
||||
},
|
||||
async evaluateEntitlements() {
|
||||
return { facts: [], snapshots: [] };
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function discordRuntime(providerUserId: string): OAuthProviderRuntime {
|
||||
return { ...twitchRuntime(providerUserId), async resolveIdentity() { return { provider: "discord", providerUserId, username: providerUserId, profile: { id: providerUserId } }; } };
|
||||
}
|
||||
|
||||
async function rig(twitchUser = "t-signin") {
|
||||
const store = new InMemoryStore();
|
||||
const audit: CreateAuditEventInput[] = [];
|
||||
const realAudit = store.createAuditEvent.bind(store);
|
||||
store.createAuditEvent = async (input) => {
|
||||
audit.push(structuredClone(input));
|
||||
return realAudit(input);
|
||||
};
|
||||
const app = await buildApp({ config, store, oauthRuntimes: { twitch: twitchRuntime(twitchUser), discord: discordRuntime("d-linked") } });
|
||||
apps.push(app);
|
||||
const keys = createRuntimeKeyMaterial({ ...config, bootstrapSigningPrivateKeyOnMissing: false });
|
||||
return { app, store, keys, audit };
|
||||
}
|
||||
type Rig = Awaited<ReturnType<typeof rig>>;
|
||||
|
||||
let seq = 0;
|
||||
/** An account holding the given logins, each with a credential, and one live-session proof factory. */
|
||||
async function acct(r: Rig, held: Array<[Provider, string?]>) {
|
||||
seq += 1;
|
||||
const account = await r.store.createAccount({ createdAt: now, lastSeenAt: now, displayName: "DISPLAY-CANARY", primaryEmail: "canary@example.test" });
|
||||
const rows = [];
|
||||
for (const [provider, user] of held) {
|
||||
rows.push(
|
||||
await r.store.upsertLinkedIdentity({
|
||||
accountId: account.id,
|
||||
provider,
|
||||
providerUserId: user ?? `${provider}-${seq}`,
|
||||
scopes: ["identify"],
|
||||
accessTokenEncrypted: "ACCESS-CIPHERTEXT",
|
||||
refreshTokenEncrypted: "REFRESH-CIPHERTEXT",
|
||||
profileJson: {},
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
})
|
||||
);
|
||||
}
|
||||
return { account, rows };
|
||||
}
|
||||
type Acct = Awaited<ReturnType<typeof acct>>;
|
||||
|
||||
interface ProofOptions {
|
||||
app?: AppSlug;
|
||||
/** Seconds since the sign-in; null mints a token with no sign-in claim (a refresh or link would). */
|
||||
signedInAgo?: number | null;
|
||||
via?: number;
|
||||
stepUp?: { agoS?: number; method: string };
|
||||
}
|
||||
/** A live session for the account and the access token it would hold. */
|
||||
async function proof(r: Rig, a: Acct, o: ProofOptions = {}) {
|
||||
const ago = o.signedInAgo === undefined ? 0 : o.signedInAgo;
|
||||
const issued = await issueAccessClaim({
|
||||
config,
|
||||
keys: r.keys,
|
||||
store: r.store,
|
||||
input: {
|
||||
appSlug: o.app ?? "bifrost",
|
||||
accountId: a.account.id,
|
||||
linkedIdentities: a.rows.map((row) => ({ provider: row.provider, providerUserId: row.providerUserId })),
|
||||
facts: [],
|
||||
...(ago === null ? {} : { authAt: nowS() - ago, authIdentityId: a.rows[o.via ?? 0]!.id }),
|
||||
...(o.stepUp ? { stepUpAt: nowS() - (o.stepUp.agoS ?? 0), stepUpMethod: o.stepUp.method } : {}),
|
||||
},
|
||||
});
|
||||
return { token: issued.accessToken, sid: issued.session.sessionId };
|
||||
}
|
||||
const passkey = { method: "bifrost-passkey" };
|
||||
|
||||
const move = (r: Rig, body: object, headers: Record<string, string> = appAuth, app = "bifrost") =>
|
||||
r.app.inject({ method: "POST", url: `/v1/apps/${app}/identity-moves`, headers, payload: body });
|
||||
const held = async (r: Rig, a: Acct) => (await r.store.listStoredLinkedIdentitiesForAccount(a.account.id)).map((i) => i.id).sort();
|
||||
const err = (res: { statusCode: number; json(): { error?: string } }) => [res.statusCode, res.json().error];
|
||||
|
||||
/** Source with discord+google, destination with patreon: a passkey on the source and any fresh proof on the destination. */
|
||||
async function scene(r: Rig) {
|
||||
const from = await acct(r, [["discord"], ["google"]]);
|
||||
const to = await acct(r, [["patreon"]]);
|
||||
return {
|
||||
from,
|
||||
to,
|
||||
fromProof: await proof(r, from, { stepUp: passkey }),
|
||||
toProof: await proof(r, to),
|
||||
};
|
||||
}
|
||||
|
||||
describe("a sanctioned move", () => {
|
||||
it("two fresh proofs move every login of the source and answer the receipt, with no token, email or name", async () => {
|
||||
const r = await rig();
|
||||
const s = await scene(r);
|
||||
const sourceIds = await held(r, s.from);
|
||||
const res = await move(r, { moveId: moveId("a"), fromProofs: [s.fromProof.token], toProof: s.toProof.token });
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(res.headers["cache-control"]).toBe("no-store");
|
||||
expect(res.json()).toEqual({
|
||||
moveId: moveId("a"),
|
||||
accountId: s.to.account.id,
|
||||
movedIdentityIds: expect.arrayContaining(sourceIds),
|
||||
proof: { from: "strong", to: "weak" },
|
||||
});
|
||||
expect(res.json().movedIdentityIds).toHaveLength(2);
|
||||
expect(res.body).not.toMatch(/CIPHERTEXT|canary|DISPLAY|ey[A-Za-z0-9_-]{20,}\./);
|
||||
expect(await held(r, s.from)).toEqual([]);
|
||||
expect(await held(r, s.to)).toEqual(expect.arrayContaining(sourceIds));
|
||||
expect(await held(r, s.to)).toHaveLength(3);
|
||||
expect(r.audit.find((e) => e.eventType === "identity_moved")).toMatchObject({
|
||||
accountId: s.from.account.id,
|
||||
eventPayloadJson: { moveId: moveId("a"), toAccountId: s.to.account.id, providers: ["discord", "google"], reversesMoveId: null },
|
||||
});
|
||||
});
|
||||
|
||||
it("answers the identical body for the same moveId after the source's sessions are revoked, and changes nothing twice", async () => {
|
||||
const r = await rig();
|
||||
const s = await scene(r);
|
||||
const body = { moveId: moveId("b"), fromProofs: [s.fromProof.token], toProof: s.toProof.token };
|
||||
const first = await move(r, body);
|
||||
expect(first.statusCode).toBe(200);
|
||||
const session = await r.store.findSession("bifrost", s.fromProof.sid);
|
||||
expect(Date.parse(session!.expiresAt), "the source's session is already dead").toBeLessThanOrEqual(Date.now());
|
||||
const again = await move(r, body);
|
||||
expect([again.statusCode, again.body]).toEqual([200, first.body]);
|
||||
expect(r.audit.filter((e) => e.eventType === "identity_moved")).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("a moveId reused for another account pair, another proof owner or another reversesMoveId is 409 move_id_conflict and changes nothing", async () => {
|
||||
const r = await rig();
|
||||
const s = await scene(r);
|
||||
const other = await acct(r, [["twitch"]]);
|
||||
await move(r, { moveId: moveId("c"), fromProofs: [s.fromProof.token], toProof: s.toProof.token });
|
||||
const otherProof = await proof(r, other);
|
||||
const toStranger = await move(r, { moveId: moveId("c"), fromProofs: [s.fromProof.token], toProof: otherProof.token });
|
||||
expect(err(toStranger)).toEqual([409, "move_id_conflict"]);
|
||||
const fromStranger = await move(r, { moveId: moveId("c"), fromProofs: [otherProof.token], toProof: s.toProof.token });
|
||||
expect(err(fromStranger)).toEqual([409, "move_id_conflict"]);
|
||||
const asReversal = await move(r, { moveId: moveId("c"), fromProofs: [s.toProof.token], reversesMoveId: moveId("zz") });
|
||||
expect(err(asReversal)).toEqual([409, "move_id_conflict"]);
|
||||
expect(await held(r, other)).toHaveLength(1);
|
||||
expect(await held(r, s.to)).toHaveLength(3);
|
||||
});
|
||||
|
||||
it("a replay whose proofs are another app's tokens, or another account's, is 409 move_id_conflict, not the stored receipt", async () => {
|
||||
const r = await rig();
|
||||
const s = await scene(r);
|
||||
const body = { moveId: moveId("rp"), fromProofs: [s.fromProof.token], toProof: s.toProof.token };
|
||||
expect((await move(r, body)).statusCode).toBe(200);
|
||||
const foreign = await proof(r, s.from, { app: "streampixels", stepUp: passkey });
|
||||
expect(err(await move(r, { ...body, fromProofs: [foreign.token] }))).toEqual([409, "move_id_conflict"]);
|
||||
const foreignTo = await proof(r, s.to, { app: "streampixels" });
|
||||
expect(err(await move(r, { ...body, toProof: foreignTo.token }))).toEqual([409, "move_id_conflict"]);
|
||||
expect((await move(r, body)).statusCode, "the genuine proofs still replay").toBe(200);
|
||||
});
|
||||
|
||||
it("an app without the secret is 401; streampixels with its own secret is 403 not_allowed_for_app", async () => {
|
||||
const r = await rig();
|
||||
const s = await scene(r);
|
||||
const body = { moveId: moveId("d"), fromProofs: [s.fromProof.token], toProof: s.toProof.token };
|
||||
expect(err(await move(r, body, {}))).toEqual([401, "app_auth_required"]);
|
||||
expect(err(await move(r, body, { "x-heimdall-app-secret": "nope" }))).toEqual([401, "app_auth_required"]);
|
||||
expect(err(await move(r, body, { "x-heimdall-app-secret": "streampixels-secret" }, "streampixels"))).toEqual([403, "not_allowed_for_app"]);
|
||||
expect(await held(r, s.from)).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("only bifrost is granted moves, and its account proofs are methods it may assert", () => {
|
||||
const granted = Object.values(builtInAppProfiles).filter((p) => p.identityMoves !== undefined);
|
||||
expect(granted.map((p) => p.slug)).toEqual(["bifrost"]);
|
||||
const bifrost = granted[0]!;
|
||||
expect(bifrost.identityMoves!.accountProofMethods.every((m) => bifrost.stepUpAssertion!.methods.includes(m))).toBe(true);
|
||||
expect(bifrost.identityMoves!.accountProofMethods).not.toContain("bifrost-email-code");
|
||||
});
|
||||
});
|
||||
|
||||
describe("what a proof must be", () => {
|
||||
it("another app's token, a revoked session and a forged signature are 403 proof_invalid and nothing moves", async () => {
|
||||
const r = await rig();
|
||||
const s = await scene(r);
|
||||
const foreign = await proof(r, s.from, { app: "streampixels", stepUp: passkey });
|
||||
const revoked = await proof(r, s.from, { stepUp: passkey });
|
||||
await r.store.revokeSession("bifrost", revoked.sid, s.from.account.id, 1, new Date().toISOString());
|
||||
const forged = s.fromProof.token.slice(0, -6) + "AAAAAA";
|
||||
for (const [fromProofs, toProof, who] of [
|
||||
[[foreign.token], s.toProof.token, "from"],
|
||||
[[revoked.token], s.toProof.token, "from"],
|
||||
[[forged], s.toProof.token, "from"],
|
||||
[[s.fromProof.token], forged, "to"],
|
||||
] as const) {
|
||||
const res = await move(r, { moveId: moveId("e"), fromProofs, toProof });
|
||||
expect([res.statusCode, res.json()]).toEqual([403, { error: "proof_invalid", proof: who }]);
|
||||
}
|
||||
expect(await held(r, s.from)).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("a sign-in 300 s old passes and 301 s is proof_stale; a token with no sign-in claim (refresh, link) is proof_stale; a step-up inside 600 s reports strong", async () => {
|
||||
const r = await rig();
|
||||
vi.useFakeTimers({ toFake: ["Date"] });
|
||||
vi.setSystemTime(new Date(now));
|
||||
const to = await acct(r, [["patreon"]]);
|
||||
const toProof = await proof(r, to);
|
||||
const providers: Provider[] = ["discord", "google", "twitch", "github", "spotify", "youtube"];
|
||||
const attempt = async (tag: string, o: ProofOptions) => {
|
||||
const from = await acct(r, [[providers.shift()!]]);
|
||||
const p = await proof(r, from, o);
|
||||
return move(r, { moveId: moveId(tag), fromProofs: [p.token], toProof: toProof.token });
|
||||
};
|
||||
const edge = await attempt("edge", { signedInAgo: AUTH_PROOF_TTL_SECONDS });
|
||||
expect([edge.statusCode, edge.json().proof]).toEqual([200, { from: "weak", to: "weak" }]);
|
||||
const late = await attempt("late", { signedInAgo: AUTH_PROOF_TTL_SECONDS + 1 });
|
||||
expect([late.statusCode, late.json()]).toEqual([403, { error: "proof_stale", proof: "from" }]);
|
||||
const none = await attempt("none", { signedInAgo: null });
|
||||
expect(err(none)).toEqual([403, "proof_stale"]);
|
||||
const strong = await attempt("strong", { signedInAgo: 5_000, stepUp: { agoS: 600, method: "provider:twitch" } });
|
||||
expect([strong.statusCode, strong.json().proof.from]).toEqual([200, "strong"]);
|
||||
const stale = await attempt("stale", { signedInAgo: 5_000, stepUp: { agoS: 601, method: "bifrost-passkey" } });
|
||||
expect(err(stale)).toEqual([403, "proof_stale"]);
|
||||
// The destination's proof is held to the same rule.
|
||||
const from = await acct(r, [["google"]]);
|
||||
const fresh = await proof(r, from);
|
||||
const staleTo = await proof(r, to, { signedInAgo: AUTH_PROOF_TTL_SECONDS + 1 });
|
||||
const res = await move(r, { moveId: moveId("sto"), fromProofs: [fresh.token], toProof: staleTo.token });
|
||||
expect([res.statusCode, res.json()]).toEqual([403, { error: "proof_stale", proof: "to" }]);
|
||||
});
|
||||
|
||||
it("fromProofs naming two accounts is 403 proof_invalid; one account on both sides is 409 same_account", async () => {
|
||||
const r = await rig();
|
||||
const s = await scene(r);
|
||||
const split = await move(r, { moveId: moveId("f"), fromProofs: [s.fromProof.token, s.toProof.token], toProof: s.toProof.token });
|
||||
expect([split.statusCode, split.json()]).toEqual([403, { error: "proof_invalid", proof: "from" }]);
|
||||
const twice = await proof(r, s.from, { stepUp: passkey });
|
||||
const same = await move(r, { moveId: moveId("g"), fromProofs: [s.fromProof.token], toProof: twice.token });
|
||||
expect(err(same)).toEqual([409, "same_account"]);
|
||||
expect(await held(r, s.from)).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("the body is closed: a forward move needs toProof, a reversal takes no toProof or identityIds, ids are patterned", async () => {
|
||||
const r = await rig();
|
||||
const s = await scene(r);
|
||||
const t = s.fromProof.token;
|
||||
for (const body of [
|
||||
{ moveId: moveId("h"), fromProofs: [t] },
|
||||
{ moveId: "short", fromProofs: [t], toProof: t },
|
||||
{ moveId: moveId("h"), fromProofs: [], toProof: t },
|
||||
{ moveId: moveId("h"), fromProofs: [t], toProof: t, reversesMoveId: moveId("i") },
|
||||
{ moveId: moveId("h"), fromProofs: [t], reversesMoveId: moveId("i"), identityIds: ["x"] },
|
||||
]) {
|
||||
expect((await move(r, body)).statusCode, JSON.stringify(Object.keys(body))).toBe(400);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("accounts come only from proofs", () => {
|
||||
it("a stray accountId in the body steers nothing", async () => {
|
||||
const r = await rig();
|
||||
const s = await scene(r);
|
||||
const stranger = await acct(r, [["twitch"]]);
|
||||
const res = await move(r, { moveId: moveId("y"), fromProofs: [s.fromProof.token], toProof: s.toProof.token, accountId: stranger.account.id, toAccountId: stranger.account.id });
|
||||
expect([res.statusCode, res.json().accountId]).toEqual([200, s.to.account.id]);
|
||||
expect(await held(r, stranger)).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("which logins a proof moves", () => {
|
||||
async function multi(r: Rig) {
|
||||
const from = await acct(r, [["discord"], ["google"], ["twitch"]]);
|
||||
const to = await acct(r, [["patreon"]]);
|
||||
return { from, to, toProof: await proof(r, to) };
|
||||
}
|
||||
|
||||
it("a sign-in through login X moves X and cannot move the source's other logins", async () => {
|
||||
const r = await rig();
|
||||
const s = await multi(r);
|
||||
const [discord, google] = s.from.rows;
|
||||
const viaDiscord = await proof(r, s.from, { via: 0 });
|
||||
const all = await move(r, { moveId: moveId("j"), fromProofs: [viaDiscord.token], toProof: s.toProof.token });
|
||||
expect(err(all)).toEqual([403, "identity_not_proved"]);
|
||||
const other = await move(r, { moveId: moveId("j"), fromProofs: [viaDiscord.token], toProof: s.toProof.token, identityIds: [google!.id] });
|
||||
expect(err(other)).toEqual([403, "identity_not_proved"]);
|
||||
expect(await held(r, s.from)).toHaveLength(3);
|
||||
const only = await move(r, { moveId: moveId("j"), fromProofs: [viaDiscord.token], toProof: s.toProof.token, identityIds: [discord!.id] });
|
||||
expect([only.statusCode, only.json().movedIdentityIds]).toEqual([200, [discord!.id]]);
|
||||
expect((await held(r, s.from)).sort()).toEqual([google!.id, s.from.rows[2]!.id].sort());
|
||||
});
|
||||
|
||||
it("one proof per login moves them all; an asserted passkey covers the whole account; an email code covers only its own login", async () => {
|
||||
const r = await rig();
|
||||
let s = await multi(r);
|
||||
const perLogin = await Promise.all(s.from.rows.map((_, via) => proof(r, s.from, { via })));
|
||||
const res = await move(r, { moveId: moveId("k"), fromProofs: perLogin.map((p) => p.token), toProof: s.toProof.token });
|
||||
expect([res.statusCode, res.json().movedIdentityIds]).toHaveLength(2);
|
||||
expect(res.json().proof.from).toBe("weak");
|
||||
expect(await held(r, s.from)).toEqual([]);
|
||||
|
||||
s = await multi(r);
|
||||
const byPasskey = await proof(r, s.from, { stepUp: passkey });
|
||||
expect((await move(r, { moveId: moveId("l"), fromProofs: [byPasskey.token], toProof: s.toProof.token })).statusCode).toBe(200);
|
||||
|
||||
s = await multi(r);
|
||||
const byEmail = await proof(r, s.from, { stepUp: { method: "bifrost-email-code" } });
|
||||
const refused = await move(r, { moveId: moveId("m"), fromProofs: [byEmail.token], toProof: s.toProof.token });
|
||||
expect(err(refused)).toEqual([403, "identity_not_proved"]);
|
||||
expect(await held(r, s.from)).toHaveLength(3);
|
||||
});
|
||||
|
||||
it("a provider held on both sides is 409 provider_conflict naming only the provider, and nothing moves", async () => {
|
||||
const r = await rig();
|
||||
const from = await acct(r, [["discord"], ["google"]]);
|
||||
const to = await acct(r, [["discord"]]);
|
||||
const res = await move(r, { moveId: moveId("n"), fromProofs: [(await proof(r, from, { stepUp: passkey })).token], toProof: (await proof(r, to)).token });
|
||||
expect([res.statusCode, res.json()]).toEqual([409, { error: "provider_conflict", provider: "discord" }]);
|
||||
expect(res.body).not.toContain(from.account.id);
|
||||
expect(await held(r, from)).toHaveLength(2);
|
||||
expect(await held(r, to)).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("naming an identity the source does not hold is 404 identity_not_found", async () => {
|
||||
const r = await rig();
|
||||
const s = await scene(r);
|
||||
const res = await move(r, { moveId: moveId("o"), fromProofs: [s.fromProof.token], toProof: s.toProof.token, identityIds: [s.to.rows[0]!.id] });
|
||||
expect(err(res)).toEqual([404, "identity_not_found"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("what a move leaves behind", () => {
|
||||
it("strips the moved credentials, expires the source's sessions, pending links and unredeemed completions, and leaves the destination live", async () => {
|
||||
const r = await rig();
|
||||
const s = await scene(r);
|
||||
const linkInput = { appSlug: "bifrost" as const, provider: "discord" as const, providerUserId: "d-x", identity: { accountId: "x", provider: "discord" as const, providerUserId: "d-x", scopes: [], profileJson: {}, createdAt: now, updatedAt: now }, returnTo: "https://bifrost.gamecult.org/x", createdAt: now, expiresAt: "2099-01-01T00:00:00.000Z" };
|
||||
const srcLink = await r.store.createPendingLink({ ...linkInput, accountId: s.from.account.id });
|
||||
const dstLink = await r.store.createPendingLink({ ...linkInput, accountId: s.to.account.id });
|
||||
const completion = (accountId: string) =>
|
||||
r.store.createAuthCompletion({ appSlug: "bifrost", provider: "discord", mode: "sign_in", accountId, sessionId: "sess", returnTo: "https://bifrost.gamecult.org/x", createdAt: now, expiresAt: "2099-01-01T00:00:00.000Z", payloadJson: {} });
|
||||
const srcCompletion = await completion(s.from.account.id);
|
||||
const dstCompletion = await completion(s.to.account.id);
|
||||
const res = await move(r, { moveId: moveId("p"), fromProofs: [s.fromProof.token], toProof: s.toProof.token });
|
||||
expect(res.statusCode).toBe(200);
|
||||
const movedIds = new Set(s.from.rows.map((row) => row.id));
|
||||
for (const row of (await r.store.listStoredLinkedIdentitiesForAccount(s.to.account.id)).filter((row) => movedIds.has(row.id))) {
|
||||
expect(row.accessTokenEncrypted).toBeUndefined();
|
||||
expect(row.refreshTokenEncrypted).toBeUndefined();
|
||||
}
|
||||
const record = await r.store.findIdentityMove(moveId("p"));
|
||||
expect(record?.moved.map((m) => m.credentialStripped)).toEqual([true, true]);
|
||||
expect((await r.store.findSession("bifrost", s.fromProof.sid))!.accessRevision).toBe(2);
|
||||
expect((await r.store.findSession("bifrost", s.toProof.sid))!.accessRevision).toBe(1);
|
||||
expect(Date.parse((await r.store.findSession("bifrost", s.toProof.sid))!.expiresAt)).toBeGreaterThan(Date.now());
|
||||
expect(await r.store.consumePendingLink("bifrost", srcLink.id, s.from.account.id, new Date().toISOString())).toBeNull();
|
||||
expect(await r.store.consumePendingLink("bifrost", dstLink.id, s.to.account.id, new Date().toISOString())).not.toBeNull();
|
||||
expect(await r.store.consumeAuthCompletion("bifrost", srcCompletion.code, new Date().toISOString())).toBeNull();
|
||||
expect(await r.store.consumeAuthCompletion("bifrost", dstCompletion.code, new Date().toISOString())).not.toBeNull();
|
||||
});
|
||||
|
||||
it("revokes the source's live grants for the providers it no longer holds, leaves the destination's receipts, and a replay heals a crash after commit", async () => {
|
||||
const r = await rig();
|
||||
const s = await scene(r);
|
||||
const grant = (accountId: string, provider: Provider) =>
|
||||
r.store.createConsentReceipt({ kind: "grant", accountId, appSlug: "bifrost", provider, createdAt: now, scopesAsked: ["identify"], scopesGranted: ["identify"], consentPageDigest: "d", consentText: "t" });
|
||||
await grant(s.from.account.id, "discord");
|
||||
await grant(s.to.account.id, "patreon");
|
||||
const receiptsOf = async (a: Acct) => (await r.store.listConsentReceipts(a.account.id)).map((x) => `${x.kind}:${x.provider}`);
|
||||
const body = { moveId: moveId("q"), fromProofs: [s.fromProof.token], toProof: s.toProof.token };
|
||||
vi.spyOn(r.store, "createConsentReceipt").mockRejectedValueOnce(new Error("crash after commit"));
|
||||
expect((await move(r, body)).statusCode).toBe(500);
|
||||
expect(await held(r, s.from), "the move itself committed").toEqual([]);
|
||||
expect(await receiptsOf(s.from)).toEqual(["grant:discord"]);
|
||||
const healed = await move(r, body);
|
||||
expect(healed.statusCode).toBe(200);
|
||||
expect(await receiptsOf(s.from)).toEqual(["grant:discord", "revocation:discord"]);
|
||||
expect(await receiptsOf(s.to)).toEqual(["grant:patreon"]);
|
||||
expect((await move(r, body)).body, "and it is idempotent").toBe(healed.body);
|
||||
expect(await receiptsOf(s.from)).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("the destination never loses an identity, whatever is refused", async () => {
|
||||
const r = await rig();
|
||||
const s = await scene(r);
|
||||
const before = (await held(r, s.to)).length;
|
||||
const refusals = [
|
||||
{ moveId: moveId("r1"), fromProofs: [s.fromProof.token], toProof: s.toProof.token, identityIds: ["nope"] },
|
||||
{ moveId: moveId("r2"), fromProofs: [s.fromProof.token], toProof: s.fromProof.token },
|
||||
{ moveId: moveId("r3"), fromProofs: [s.toProof.token], toProof: s.toProof.token },
|
||||
];
|
||||
for (const body of refusals) {
|
||||
await move(r, body);
|
||||
expect((await held(r, s.to)).length).toBeGreaterThanOrEqual(before);
|
||||
}
|
||||
expect((await move(r, { moveId: moveId("r4"), fromProofs: [s.fromProof.token], toProof: s.toProof.token })).statusCode).toBe(200);
|
||||
expect((await held(r, s.to)).length).toBeGreaterThanOrEqual(before);
|
||||
});
|
||||
|
||||
it("a sign-in through a moved login lands on the destination, with no alias lookup", async () => {
|
||||
const r = await rig("t-moved");
|
||||
const from = await acct(r, [["twitch", "t-moved"]]);
|
||||
const to = await acct(r, [["patreon"]]);
|
||||
const mv = await move(r, { moveId: moveId("s"), fromProofs: [(await proof(r, from, { stepUp: passkey })).token], toProof: (await proof(r, to)).token });
|
||||
expect(mv.statusCode).toBe(200);
|
||||
const started = await r.app.inject({ method: "POST", url: "/v1/oauth/twitch/start", payload: { appSlug: "streampixels", mode: "sign_in", returnTo: "https://streampixels.gamecult.org/signin/return" } });
|
||||
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);
|
||||
expect(called.json().account.id).toBe(to.account.id);
|
||||
});
|
||||
});
|
||||
|
||||
describe("aliases and reversal", () => {
|
||||
it("an emptied source is an alias; moving into or out of it is 409 already_moved", async () => {
|
||||
const r = await rig();
|
||||
const s = await scene(r);
|
||||
expect((await move(r, { moveId: moveId("t"), fromProofs: [s.fromProof.token], toProof: s.toProof.token })).statusCode).toBe(200);
|
||||
// A session can outlive its logins only by being minted for an account that holds none, which is how a stale caller would look.
|
||||
const aliasProof = await proof(r, { account: s.from.account, rows: [s.to.rows[0]!] }, { stepUp: passkey });
|
||||
const third = await acct(r, [["github"]]);
|
||||
const out = await move(r, { moveId: moveId("u"), fromProofs: [aliasProof.token], toProof: (await proof(r, third)).token });
|
||||
expect(err(out)).toEqual([409, "already_moved"]);
|
||||
const aliasTo = await proof(r, { account: s.from.account, rows: [s.to.rows[0]!] });
|
||||
const into = await move(r, { moveId: moveId("u2"), fromProofs: [(await proof(r, third, { stepUp: passkey })).token], toProof: aliasTo.token });
|
||||
expect(err(into)).toEqual([409, "already_moved"]);
|
||||
expect(await held(r, third)).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("a reversal through a moved login moves exactly those logins back; a second reversal is not_reversible; credentials stay stripped", async () => {
|
||||
const r = await rig();
|
||||
const s = await scene(r);
|
||||
const sourceIds = await held(r, s.from);
|
||||
await move(r, { moveId: moveId("v"), fromProofs: [s.fromProof.token], toProof: s.toProof.token });
|
||||
const [discord, google] = s.from.rows;
|
||||
// The destination now holds the moved logins: a sign-in through one of them is a proof of the destination.
|
||||
const viaMoved = await proof(r, { account: s.to.account, rows: [discord!, google!] }, { via: 0 });
|
||||
const own = await proof(r, s.to, { via: 0 });
|
||||
const denied = await move(r, { moveId: moveId("w"), fromProofs: [own.token], reversesMoveId: moveId("v") });
|
||||
expect(err(denied), "the destination's own login does not speak for the moved ones").toEqual([403, "identity_not_proved"]);
|
||||
const viaBoth = [viaMoved, await proof(r, { account: s.to.account, rows: [discord!, google!] }, { via: 1 })];
|
||||
const back = await move(r, { moveId: moveId("w"), fromProofs: viaBoth.map((p) => p.token), reversesMoveId: moveId("v") });
|
||||
expect(back.statusCode, back.body).toBe(200);
|
||||
expect(back.json()).toEqual({ moveId: moveId("w"), accountId: s.from.account.id, movedIdentityIds: expect.arrayContaining(sourceIds), proof: { from: "weak", to: null }, reversesMoveId: moveId("v") });
|
||||
expect(await held(r, s.from)).toEqual(sourceIds);
|
||||
expect(await held(r, s.to)).toHaveLength(1);
|
||||
expect((await r.store.listStoredLinkedIdentitiesForAccount(s.from.account.id)).every((i) => i.accessTokenEncrypted === undefined)).toBe(true);
|
||||
const own2 = await proof(r, s.to, { via: 0 });
|
||||
const again = await move(r, { moveId: moveId("w2"), fromProofs: [own2.token], reversesMoveId: moveId("v") });
|
||||
expect(err(again)).toEqual([409, "not_reversible"]);
|
||||
const reverseTheReversal = await move(r, { moveId: moveId("w3"), fromProofs: [(await proof(r, s.from)).token], reversesMoveId: moveId("w") });
|
||||
expect(err(reverseTheReversal)).toEqual([409, "not_reversible"]);
|
||||
const unknown = await move(r, { moveId: moveId("w4"), fromProofs: [(await proof(r, s.from)).token], reversesMoveId: moveId("nothere") });
|
||||
expect(err(unknown)).toEqual([409, "not_reversible"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("proofTier", () => {
|
||||
it("a proof dated in the future proves nothing, whichever claim carries it", () => {
|
||||
const n = nowS();
|
||||
expect(proofTier({ auth_at: n + 5 }, n)).toBeNull();
|
||||
expect(proofTier({ step_up_at: n + 5 }, n)).toBeNull();
|
||||
expect(proofTier({ step_up_at: n + 5, auth_at: n }, n), "a future step-up does not hide a valid sign-in").toBe("weak");
|
||||
expect(proofTier({ step_up_at: n, auth_at: n - 9_999 }, n)).toBe("strong");
|
||||
});
|
||||
});
|
||||
|
||||
describe("the reversal's source must be the forward move's destination", () => {
|
||||
it("is refused as not_reversible before the proofs are judged, even for a stale proof", async () => {
|
||||
const r = await rig();
|
||||
const s = await scene(r);
|
||||
expect((await move(r, { moveId: moveId("fw"), fromProofs: [s.fromProof.token], toProof: s.toProof.token })).statusCode).toBe(200);
|
||||
const third = await acct(r, [["github"]]);
|
||||
const onward = await proof(r, s.to, { stepUp: passkey });
|
||||
expect((await move(r, { moveId: moveId("on"), fromProofs: [onward.token], toProof: (await proof(r, third)).token })).statusCode).toBe(200);
|
||||
const stale = await proof(r, third, { signedInAgo: 9_999 });
|
||||
const res = await move(r, { moveId: moveId("rv"), fromProofs: [stale.token], reversesMoveId: moveId("fw") });
|
||||
expect(err(res)).toEqual([409, "not_reversible"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("idempotency is by moveId", () => {
|
||||
it("a replayed reversal under another reversesMoveId is 409 move_id_conflict, not the stored receipt", async () => {
|
||||
const r = await rig();
|
||||
const s = await scene(r);
|
||||
await move(r, { moveId: moveId("fw2"), fromProofs: [s.fromProof.token], toProof: s.toProof.token });
|
||||
const [discord, google] = s.from.rows;
|
||||
const viaMoved = async (via: number) => (await proof(r, { account: s.to.account, rows: [discord!, google!] }, { via })).token;
|
||||
const body = { moveId: moveId("rv2"), fromProofs: [await viaMoved(0), await viaMoved(1)], reversesMoveId: moveId("fw2") };
|
||||
const back = await move(r, body);
|
||||
expect(back.statusCode, back.body).toBe(200);
|
||||
expect((await move(r, body)).body, "the same body replays").toBe(back.body);
|
||||
expect(err(await move(r, { ...body, reversesMoveId: moveId("other") }))).toEqual([409, "move_id_conflict"]);
|
||||
expect(await held(r, s.from)).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("a replay with the same moveId and different identityIds returns the first receipt (steward ruling: the key is the moveId, a retry carries the same body)", async () => {
|
||||
const r = await rig();
|
||||
const from = await acct(r, [["discord"], ["google"]]);
|
||||
const to = await acct(r, [["patreon"]]);
|
||||
const fromProof = await proof(r, from, { stepUp: passkey });
|
||||
const toProof = await proof(r, to);
|
||||
const first = await move(r, { moveId: moveId("ids"), fromProofs: [fromProof.token], toProof: toProof.token, identityIds: [from.rows[0]!.id] });
|
||||
expect(first.json().movedIdentityIds).toEqual([from.rows[0]!.id]);
|
||||
const again = await move(r, { moveId: moveId("ids"), fromProofs: [fromProof.token], toProof: toProof.token, identityIds: [from.rows[1]!.id] });
|
||||
expect([again.statusCode, again.body]).toEqual([200, first.body]);
|
||||
expect(await held(r, from), "and the second selection did not move").toEqual([from.rows[1]!.id]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("what a refusal says", () => {
|
||||
it("no refusal body names an account id, an email, a name or an identity the caller did not send", async () => {
|
||||
const r = await rig();
|
||||
const s = await scene(r);
|
||||
const stranger = await acct(r, [["discord", "d-stranger"]]);
|
||||
const bodies = [
|
||||
await move(r, { moveId: moveId("x1"), fromProofs: [s.fromProof.token], toProof: "garbage" }),
|
||||
await move(r, { moveId: moveId("x2"), fromProofs: [(await proof(r, s.from, { via: 0 })).token], toProof: s.toProof.token }),
|
||||
await move(r, { moveId: moveId("x3"), fromProofs: [s.fromProof.token], toProof: s.toProof.token, identityIds: [stranger.rows[0]!.id] }),
|
||||
await move(r, { moveId: moveId("x4"), fromProofs: [s.toProof.token], toProof: s.toProof.token }),
|
||||
await move(r, { moveId: moveId("x5"), fromProofs: [s.fromProof.token], reversesMoveId: moveId("nothere") }),
|
||||
await move(r, { moveId: moveId("x6"), fromProofs: [(await proof(r, s.from, { signedInAgo: 9_999 })).token], toProof: s.toProof.token }),
|
||||
];
|
||||
const ids = [s.from, s.to, stranger].flatMap((a) => [a.account.id, ...a.rows.map((row) => row.id), ...a.rows.map((row) => row.providerUserId)]);
|
||||
for (const res of bodies) {
|
||||
expect(res.statusCode).toBeGreaterThanOrEqual(400);
|
||||
for (const id of ids) expect(res.body).not.toContain(id);
|
||||
expect(res.body).not.toMatch(/canary|DISPLAY/);
|
||||
expect(Object.keys(res.json()).every((k) => ["error", "detail", "proof", "provider"].includes(k))).toBe(true);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("claims: which sign-in a token came from", () => {
|
||||
const returnTo = "https://streampixels.gamecult.org/signin/return";
|
||||
async function twitchSignIn(r: Rig) {
|
||||
const started = await r.app.inject({ method: "POST", url: "/v1/oauth/twitch/start", payload: { appSlug: "streampixels", mode: "sign_in", returnTo } });
|
||||
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 { refreshToken: string; claimSet: Record<string, unknown> };
|
||||
}
|
||||
|
||||
it("a sign_in stamps auth_at and the login it came through; a refresh does not renew them", async () => {
|
||||
const r = await rig("t-claims");
|
||||
const before = nowS();
|
||||
const signed = await twitchSignIn(r);
|
||||
const row = await r.store.findStoredLinkedIdentity("twitch", "t-claims");
|
||||
expect(signed.claimSet.auth_identity_id).toBe(row!.id);
|
||||
expect(signed.claimSet.auth_at).toBeGreaterThanOrEqual(before);
|
||||
expect(signed.claimSet.auth_at).toBeLessThanOrEqual(nowS());
|
||||
expect(signed.claimSet.step_up_at).toBeUndefined();
|
||||
const refreshed = await r.app.inject({ method: "POST", url: "/v1/apps/streampixels/sessions/refresh", payload: { refreshToken: signed.refreshToken } });
|
||||
expect(refreshed.statusCode).toBe(201);
|
||||
expect(refreshed.json().claimSet.auth_at).toBeUndefined();
|
||||
expect(refreshed.json().claimSet.auth_identity_id).toBeUndefined();
|
||||
});
|
||||
|
||||
it("a link callback mints no sign-in proof: no auth_at, no auth_identity_id", async () => {
|
||||
const r = await rig();
|
||||
const holder = await acct(r, [["twitch"]]);
|
||||
const started = await r.app.inject({ method: "POST", url: "/v1/oauth/discord/start", headers: appAuth, payload: { appSlug: "bifrost", mode: "link", initiatingAccountId: holder.account.id, returnTo: "https://bifrost.gamecult.org/x" } });
|
||||
expect(started.statusCode).toBe(201);
|
||||
const flow = await launchFlow(r.app, started.json());
|
||||
const called = await r.app.inject({ method: "GET", url: `/v1/oauth/discord/callback?code=c&state=${encodeURIComponent(flow.state)}`, headers: { cookie: flow.cookie, accept: "application/json" } });
|
||||
expect(called.statusCode).toBe(201);
|
||||
expect(called.json().claimSet.identities).toHaveLength(2);
|
||||
expect(called.json().claimSet.auth_at).toBeUndefined();
|
||||
expect(called.json().claimSet.auth_identity_id).toBeUndefined();
|
||||
expect(called.body).not.toMatch(/auth_at|auth_identity_id/);
|
||||
});
|
||||
|
||||
it("a connect callback mints no sign-in proof either", async () => {
|
||||
const r = await rig("t-connect");
|
||||
const started = await r.app.inject({ method: "POST", url: "/v1/oauth/twitch/start", headers: { "x-heimdall-app-secret": "streampixels-secret" }, payload: { appSlug: "streampixels", mode: "connect", returnTo: "https://streampixels.gamecult.org/signin/return", connection: { kind: "creator", targetId: "creator-1" } } });
|
||||
expect(started.statusCode, started.body).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, called.body).toBe(201);
|
||||
expect(called.body).toContain("claimSet");
|
||||
expect(called.body).not.toMatch(/auth_at|auth_identity_id/);
|
||||
});
|
||||
|
||||
it("an asserted step-up carries the bearer's auth claims through unchanged", async () => {
|
||||
const r = await rig();
|
||||
const a = await acct(r, [["discord"], ["google"]]);
|
||||
const p = await proof(r, a, { via: 1, signedInAgo: 30 });
|
||||
const res = await r.app.inject({ method: "POST", url: "/v1/apps/bifrost/step-up", headers: { ...appAuth, authorization: `Bearer ${p.token}` }, payload: { method: "bifrost-passkey", nonce: "n".repeat(32) } });
|
||||
expect(res.statusCode).toBe(201);
|
||||
expect(res.json().claimSet).toMatchObject({ auth_identity_id: a.rows[1]!.id, auth_at: nowS() - 30, step_up_method: "bifrost-passkey" });
|
||||
});
|
||||
});
|
||||
|
|
@ -249,6 +249,10 @@ describe.each(unlinkStores)("linked_identities writes, %s", (_name, make) => {
|
|||
expect(await store.listStoredLinkedIdentitiesForAccount(b.id)).toEqual([]);
|
||||
});
|
||||
|
||||
it.skipIf(realPostgresOnly)("moveIdentities keeps one contract: moved, replayed, conflict, lapsed proof, coverage, reversal", async () => {
|
||||
await exerciseMoveContract(await make());
|
||||
});
|
||||
|
||||
it("upsert by the holder still refreshes its own row, keeping its id", async () => {
|
||||
const { store, a } = await twoAccounts();
|
||||
const first = await store.upsertLinkedIdentity(input(a.id, "u1", { username: "one" }));
|
||||
|
|
@ -477,9 +481,10 @@ describe("PostgresStore.checkSchema", () => {
|
|||
"capability_grants",
|
||||
"entitlement_snapshots",
|
||||
"consent_receipts",
|
||||
"identity_moves",
|
||||
"audit_events",
|
||||
]);
|
||||
expect(REQUIRED_COLUMNS).toHaveLength(113);
|
||||
expect(REQUIRED_COLUMNS).toHaveLength(121);
|
||||
expect(REQUIRED_COLUMNS).toEqual(
|
||||
expect.arrayContaining([
|
||||
{ table: "auth_completions", column: "code", type: "TEXT", nullable: false },
|
||||
|
|
@ -490,7 +495,7 @@ describe("PostgresStore.checkSchema", () => {
|
|||
{ table: "auth_completions", column: "attempt_id", type: "TEXT", nullable: true },
|
||||
])
|
||||
);
|
||||
expect(REQUIRED_KEYS).toHaveLength(17);
|
||||
expect(REQUIRED_KEYS).toHaveLength(19);
|
||||
expect(REQUIRED_KEYS).toEqual(
|
||||
expect.arrayContaining([
|
||||
{ table: "linked_identities", columns: "provider,provider_user_id", predicate: "" },
|
||||
|
|
@ -498,6 +503,7 @@ describe("PostgresStore.checkSchema", () => {
|
|||
{ table: "oauth_state_uses", columns: "jti,phase", predicate: "" },
|
||||
{ table: "consent_receipts", columns: "revokes_receipt_id", predicate: "revokes_receipt_id IS NOT NULL" },
|
||||
{ table: "consent_receipts", columns: "account_id,id", predicate: "" },
|
||||
{ table: "identity_moves", columns: "reverses_move_id", predicate: "reverses_move_id IS NOT NULL" },
|
||||
{ table: "entitlement_snapshots", columns: "account_id,provider,scope", predicate: "" },
|
||||
{ table: "auth_completions", columns: "app_slug,attempt_id", predicate: "attempt_id IS NOT NULL AND consumed_at IS NULL" },
|
||||
])
|
||||
|
|
@ -648,9 +654,178 @@ async function exerciseStore(store: PostgresStore): Promise<Set<string>> {
|
|||
expect(second.receipt.id).toBe(first.receipt.id);
|
||||
expect((await s.listConsentReceipts(account.id)).map((r) => r.kind)).toEqual(["grant", "revocation"]);
|
||||
await s.createAuditEvent({ eventType: "probe", eventPayloadJson: {}, createdAt: now });
|
||||
const target = await s.createAccount({ createdAt: now, lastSeenAt: now });
|
||||
await s.upsertLinkedIdentity({ ...identity, accountId: target.id, provider: "patreon", providerUserId: "u-target" });
|
||||
const moveId = `move-${randomUUID()}`;
|
||||
expect((await s.moveIdentities({ moveId, appSlug: "bifrost", fromAccountId: account.id, toAccountId: target.id, identityIds: "all", covers: "account", proofSessions: [], proof: { from: [], to: null }, at: now })).kind).toBe("moved");
|
||||
expect((await s.findIdentityMove(moveId))?.id).toBe(moveId);
|
||||
return called;
|
||||
}
|
||||
|
||||
/**
|
||||
* The move contract both stores keep, once. Each case builds its own accounts,
|
||||
* so the order of cases proves nothing and a store holding earlier moves is fine.
|
||||
*/
|
||||
async function exerciseMoveContract(store: HeimdallStore): Promise<void> {
|
||||
const at = "2026-11-14T12:00:00.000Z";
|
||||
const later = "2999-01-01T00:00:00.000Z";
|
||||
const proofTiers = { from: [{ tier: "strong" as const, method: "bifrost-passkey", identityId: null }], to: { tier: "weak" as const, method: null, identityId: null } };
|
||||
const side = async (providers: Array<"discord" | "google" | "patreon">) => {
|
||||
const account = await store.createAccount({ createdAt: at, lastSeenAt: at });
|
||||
const rows = [];
|
||||
for (const provider of providers) {
|
||||
rows.push(await store.upsertLinkedIdentity({ accountId: account.id, provider, providerUserId: `${provider}-${randomUUID()}`, accessTokenEncrypted: "A", refreshTokenEncrypted: "R", scopes: [], profileJson: {}, createdAt: at, updatedAt: at }));
|
||||
}
|
||||
const sessionId = randomUUID();
|
||||
await store.createSession({ id: sessionId, accountId: account.id, appSlug: "bifrost", createdAt: at, lastSeenAt: at, expiresAt: later, accessRevision: 1, claimsJson: {} });
|
||||
return { account, rows, proofSession: { appSlug: "bifrost" as const, sessionId, accountId: account.id, accessRevision: 1 } };
|
||||
};
|
||||
const input = (from: Awaited<ReturnType<typeof side>>, to: Awaited<ReturnType<typeof side>>, extra: Record<string, unknown> = {}) => ({
|
||||
moveId: `move-${randomUUID()}`,
|
||||
appSlug: "bifrost" as const,
|
||||
fromAccountId: from.account.id,
|
||||
toAccountId: to.account.id,
|
||||
identityIds: "all" as const,
|
||||
covers: "account" as const,
|
||||
proofSessions: [from.proofSession, to.proofSession],
|
||||
proof: proofTiers,
|
||||
at,
|
||||
...extra,
|
||||
});
|
||||
const held = async (a: { account: { id: string } }) => (await store.listStoredLinkedIdentitiesForAccount(a.account.id)).map((i) => i.id).sort();
|
||||
|
||||
// Moved: rows, credentials, sessions and the record.
|
||||
const a = await side(["discord", "google"]);
|
||||
const b = await side(["patreon"]);
|
||||
const first = input(a, b);
|
||||
const moved = await store.moveIdentities(first);
|
||||
expect(moved.kind).toBe("moved");
|
||||
expect(await held(a)).toEqual([]);
|
||||
expect(await held(b)).toEqual([...a.rows.map((r) => r.id), ...b.rows.map((r) => r.id)].sort());
|
||||
for (const row of await store.listStoredLinkedIdentitiesForAccount(b.account.id)) {
|
||||
if (a.rows.some((r) => r.id === row.id)) expect([row.accessTokenEncrypted, row.refreshTokenEncrypted]).toEqual([undefined, undefined]);
|
||||
else expect(row.accessTokenEncrypted, "the destination's own credential is untouched").toBe("A");
|
||||
}
|
||||
const record = await store.findIdentityMove(first.moveId);
|
||||
expect(record).toMatchObject({ id: first.moveId, fromAccountId: a.account.id, toAccountId: b.account.id, proof: proofTiers, createdAt: at });
|
||||
expect(record!.moved.map((m) => m.credentialStripped)).toEqual([true, true]);
|
||||
expect((await store.findSession("bifrost", a.proofSession.sessionId))!.accessRevision).toBe(2);
|
||||
expect((await store.findSession("bifrost", b.proofSession.sessionId))!.accessRevision).toBe(1);
|
||||
|
||||
// Replay answers from the row, even though the proof session is gone; a different pair is a conflict.
|
||||
expect(await store.moveIdentities(first)).toEqual({ kind: "replayed", move: record });
|
||||
const c = await side(["discord"]);
|
||||
expect(await store.moveIdentities({ ...first, toAccountId: c.account.id })).toEqual({ kind: "move_id_conflict" });
|
||||
expect(await store.moveIdentities({ ...first, reversesMoveId: "x" })).toEqual({ kind: "move_id_conflict" });
|
||||
|
||||
// Into or out of an alias.
|
||||
expect((await store.moveIdentities(input(a, c, { proofSessions: [c.proofSession] }))).kind).toBe("already_moved");
|
||||
expect((await store.moveIdentities(input(c, a, { proofSessions: [c.proofSession] }))).kind).toBe("already_moved");
|
||||
|
||||
// A proof session that was revoked, or has moved on a revision, has lapsed.
|
||||
const d = await side(["google"]);
|
||||
const e = await side(["patreon"]);
|
||||
await store.revokeSession("bifrost", e.proofSession.sessionId, e.account.id, 1, at);
|
||||
expect((await store.moveIdentities(input(d, e))).kind).toBe("proof_lapsed");
|
||||
expect(await held(d)).toHaveLength(1);
|
||||
|
||||
// Coverage and conflict.
|
||||
const f = await side(["discord", "google"]);
|
||||
const g = await side(["patreon"]);
|
||||
expect((await store.moveIdentities(input(f, g, { covers: [f.rows[0]!.id] }))).kind).toBe("identity_not_proved");
|
||||
expect((await store.moveIdentities(input(f, g, { identityIds: ["nope"] }))).kind).toBe("identity_not_found");
|
||||
const h = await side(["discord"]);
|
||||
expect(await store.moveIdentities(input(f, h))).toEqual({ kind: "provider_conflict", provider: "discord" });
|
||||
expect(await held(f)).toHaveLength(2);
|
||||
const onlyDiscord = await store.moveIdentities(input(f, g, { identityIds: [f.rows[0]!.id], covers: [f.rows[0]!.id] }));
|
||||
expect(onlyDiscord.kind).toBe("moved");
|
||||
expect(await held(f)).toEqual([f.rows[1]!.id]);
|
||||
|
||||
// Reversal: moves exactly what the forward carried, once, and a reversal is not itself reversible.
|
||||
const forward = (moved as { move: { id: string } }).move;
|
||||
const back = input(b, a, { reversesMoveId: forward.id, proofSessions: [], identityIds: a.rows.map((r) => r.id), covers: a.rows.map((r) => r.id) });
|
||||
const reversed = await store.moveIdentities(back);
|
||||
expect(reversed.kind).toBe("moved");
|
||||
expect(await held(a)).toEqual(a.rows.map((r) => r.id).sort());
|
||||
expect(await held(b)).toEqual(b.rows.map((r) => r.id));
|
||||
expect((await store.moveIdentities({ ...back, moveId: `move-${randomUUID()}` })).kind).toBe("not_reversible");
|
||||
expect((await store.moveIdentities(input(a, b, { reversesMoveId: back.moveId, proofSessions: [], identityIds: "all" }))).kind).toBe("not_reversible");
|
||||
|
||||
// A moveId answers only the move that used it: each of app, source, destination and reversal must match.
|
||||
const k = await side(["discord"]);
|
||||
const l = await side(["patreon"]);
|
||||
const m = await side(["google"]);
|
||||
const once = input(k, l);
|
||||
expect((await store.moveIdentities(once)).kind).toBe("moved");
|
||||
expect((await store.moveIdentities({ ...once, fromAccountId: m.account.id })).kind).toBe("move_id_conflict");
|
||||
expect((await store.moveIdentities({ ...once, toAccountId: m.account.id })).kind).toBe("move_id_conflict");
|
||||
expect((await store.moveIdentities({ ...once, appSlug: "streampixels" })).kind).toBe("move_id_conflict");
|
||||
expect((await store.moveIdentities(once)).kind).toBe("replayed");
|
||||
|
||||
// Every step of a move is on Postgres too: expiry of the source's pending links and unredeemed completions, and the stripped credential's expiry and failure mark.
|
||||
const n = await side(["discord", "google"]);
|
||||
const o = await side(["patreon"]);
|
||||
await store.upsertLinkedIdentity({ accountId: n.account.id, provider: "discord", providerUserId: (await store.listStoredLinkedIdentitiesForAccount(n.account.id))[0]!.providerUserId, tokenExpiresAt: "2999-01-01T00:00:00.000Z", scopes: [], profileJson: {}, createdAt: at, updatedAt: at });
|
||||
await store.updateLinkedIdentityCredential(n.rows[1]!.id, async () => ({ kind: "failed", failedAt: at }));
|
||||
const linkOf = (accountId: string) => store.createPendingLink({ appSlug: "bifrost", accountId, provider: "discord", providerUserId: "p", identity: { accountId, provider: "discord", providerUserId: "p", scopes: [], profileJson: {}, createdAt: at, updatedAt: at }, returnTo: "https://x.test/", createdAt: at, expiresAt: later });
|
||||
const completionOf = (accountId: string) => store.createAuthCompletion({ appSlug: "bifrost", provider: "discord", mode: "sign_in", accountId, sessionId: n.proofSession.sessionId, returnTo: "https://x.test/", createdAt: at, expiresAt: later, payloadJson: {} });
|
||||
const sourceLink = await linkOf(n.account.id);
|
||||
const destLink = await linkOf(o.account.id);
|
||||
const sourceCompletion = await completionOf(n.account.id);
|
||||
const destCompletion = await completionOf(o.account.id);
|
||||
expect((await store.moveIdentities(input(n, o))).kind).toBe("moved");
|
||||
const after = "2026-11-14T12:00:01.000Z";
|
||||
expect(await store.consumePendingLink("bifrost", sourceLink.id, n.account.id, after)).toBeNull();
|
||||
expect(await store.consumeAuthCompletion("bifrost", sourceCompletion.code, after)).toBeNull();
|
||||
expect((await store.consumePendingLink("bifrost", destLink.id, o.account.id, after))?.id).toBe(destLink.id);
|
||||
expect((await store.consumeAuthCompletion("bifrost", destCompletion.code, after))?.code).toBe(destCompletion.code);
|
||||
for (const row of (await store.listStoredLinkedIdentitiesForAccount(o.account.id)).filter((r) => n.rows.some((x) => x.id === r.id))) {
|
||||
expect([row.accessTokenEncrypted, row.refreshTokenEncrypted, row.tokenExpiresAt, row.refreshFailedAt], row.provider).toEqual([undefined, undefined, undefined, undefined]);
|
||||
}
|
||||
|
||||
// Each reversal guard on its own: the refusal is not_reversible, never another code, and nothing moves.
|
||||
const fresh = async () => ({ p: await side(["discord", "google"]), q: await side(["patreon"]), t: await side(["patreon"]) });
|
||||
const reverse = (forwardId: string, from: { account: { id: string } }, to: { account: { id: string } }, ids: readonly string[]) => ({
|
||||
moveId: `move-${randomUUID()}`, appSlug: "bifrost" as const, fromAccountId: from.account.id, toAccountId: to.account.id,
|
||||
identityIds: ids, covers: ids, proofSessions: [], reversesMoveId: forwardId, proof: { from: [], to: null }, at,
|
||||
});
|
||||
// (a) already reversed, even though the logins are back on the destination by a later forward move
|
||||
{
|
||||
const { p, q } = await fresh();
|
||||
const ids = p.rows.map((r) => r.id);
|
||||
const m1 = input(p, q);
|
||||
expect((await store.moveIdentities(m1)).kind).toBe("moved");
|
||||
expect((await store.moveIdentities(reverse(m1.moveId, q, p, ids))).kind).toBe("moved");
|
||||
expect((await store.moveIdentities(input(p, q, { proofSessions: [] }))).kind).toBe("moved");
|
||||
expect((await store.moveIdentities(reverse(m1.moveId, q, p, ids))).kind, "reversed once").toBe("not_reversible");
|
||||
expect(await held(p)).toEqual([]);
|
||||
}
|
||||
// (d) the logins are still on the forward's destination but the reversal targets another account
|
||||
{
|
||||
const { p, q } = await fresh();
|
||||
const ids = p.rows.map((r) => r.id);
|
||||
const m1 = input(p, q);
|
||||
expect((await store.moveIdentities(m1)).kind).toBe("moved");
|
||||
const elsewhere = await side(["patreon"]);
|
||||
expect((await store.moveIdentities(reverse(m1.moveId, q, elsewhere, ids))).kind, "to is not the forward's source").toBe("not_reversible");
|
||||
expect(await held(elsewhere)).toHaveLength(1);
|
||||
}
|
||||
// (b) the logins moved on from the destination: not held, so not_reversible (not identity_not_found)
|
||||
// (c) the reversal's source is not the forward's destination, or its target is not the forward's source
|
||||
{
|
||||
const { p, q, t } = await fresh();
|
||||
const ids = p.rows.map((r) => r.id);
|
||||
const m1 = input(p, q);
|
||||
expect((await store.moveIdentities(m1)).kind).toBe("moved");
|
||||
expect((await store.moveIdentities(input(q, t, { identityIds: ids, covers: ids, proofSessions: [] }))).kind).toBe("moved");
|
||||
expect((await store.moveIdentities(reverse(m1.moveId, q, p, ids))).kind, "ids no longer held").toBe("not_reversible");
|
||||
expect((await store.moveIdentities(reverse(m1.moveId, t, p, ids))).kind, "from is not the forward's destination").toBe("not_reversible");
|
||||
const u = await side(["google"]);
|
||||
expect((await store.moveIdentities(reverse(m1.moveId, t, u, ids))).kind, "to is not the forward's source").toBe("not_reversible");
|
||||
expect(await held(t)).toEqual([...ids, ...t.rows.map((r) => r.id)].sort());
|
||||
}
|
||||
}
|
||||
|
||||
// Against a real Postgres, through the startup path itself: createStore, with
|
||||
// and without applying the schema. HEIMDALL_TEST_PG_ADMIN_URL names a role
|
||||
// that may create databases and roles. Each case clones a template database
|
||||
|
|
@ -803,6 +978,103 @@ describe.skipIf(!adminUrl)("startup against a live Postgres (HEIMDALL_TEST_PG_AD
|
|||
expect(await start(urlFor(name, role, rolePassword), false)).toBe(refusal("SCHEMA_PRIVILEGES"));
|
||||
});
|
||||
|
||||
it("startup refuses a role that lacks INSERT or SELECT on identity_moves", async () => {
|
||||
expect(STORE_TABLE_PRIVILEGES.identity_moves).toEqual(["SELECT", "INSERT"]);
|
||||
for (const [kept, dropped] of [[["SELECT"], "INSERT"], [["INSERT"], "SELECT"]] as const) {
|
||||
const { name, owner } = await appliedDatabase();
|
||||
await grant(owner, { ...STORE_TABLE_PRIVILEGES, identity_moves: kept });
|
||||
expect(await start(urlFor(name, role, rolePassword), false), `without ${dropped}`).toBe(refusal("SCHEMA_PRIVILEGES"));
|
||||
}
|
||||
});
|
||||
|
||||
it("on real Postgres: the move contract holds", async () => {
|
||||
const { owner } = await appliedDatabase();
|
||||
await exerciseMoveContract(new PostgresStore(owner));
|
||||
});
|
||||
|
||||
describe("concurrent moves", () => {
|
||||
const at = "2026-11-14T12:00:00.000Z";
|
||||
async function pair(store: PostgresStore) {
|
||||
const make = async (provider: "discord" | "patreon") => {
|
||||
const account = await store.createAccount({ createdAt: at, lastSeenAt: at });
|
||||
await store.upsertLinkedIdentity({ accountId: account.id, provider, providerUserId: `${provider}-${randomUUID()}`, scopes: [], profileJson: {}, createdAt: at, updatedAt: at });
|
||||
const sessionId = randomUUID();
|
||||
await store.createSession({ id: sessionId, accountId: account.id, appSlug: "bifrost", createdAt: at, lastSeenAt: at, expiresAt: "2999-01-01T00:00:00.000Z", accessRevision: 1, claimsJson: {} });
|
||||
return { id: account.id, proof: { appSlug: "bifrost" as const, sessionId, accountId: account.id, accessRevision: 1 } };
|
||||
};
|
||||
return { x: await make("discord"), y: await make("patreon") };
|
||||
}
|
||||
const request = (from: { id: string; proof: object }, to: { id: string; proof: object }, moveId: string) => ({
|
||||
moveId,
|
||||
appSlug: "bifrost" as const,
|
||||
fromAccountId: from.id,
|
||||
toAccountId: to.id,
|
||||
identityIds: "all" as const,
|
||||
covers: "account" as const,
|
||||
proofSessions: [from.proof, to.proof] as never,
|
||||
proof: { from: [], to: null },
|
||||
at,
|
||||
});
|
||||
|
||||
it("opposite moves A->B and B->A over two pools leave one applied and never deadlock, ten rounds", async () => {
|
||||
const { name, owner } = await appliedDatabase();
|
||||
const storeA = new PostgresStore(owner);
|
||||
const other = fixturePool(urlFor(name));
|
||||
cleanup.push(async () => {
|
||||
await other.end();
|
||||
});
|
||||
const storeB = new PostgresStore(other);
|
||||
for (let round = 0; round < 10; round += 1) {
|
||||
const { x, y } = await pair(storeA);
|
||||
const results = await Promise.all([
|
||||
storeA.moveIdentities(request(x, y, `move-${randomUUID()}`)),
|
||||
storeB.moveIdentities(request(y, x, `move-${randomUUID()}`)),
|
||||
]);
|
||||
const kinds = results.map((r) => r.kind).sort();
|
||||
expect(kinds[0], `round ${round}: ${kinds}`).toBe("moved");
|
||||
expect(["already_moved", "proof_lapsed"], `round ${round}: ${kinds}`).toContain(kinds[1]);
|
||||
const held = (await storeA.listStoredLinkedIdentitiesForAccount(x.id)).length + (await storeA.listStoredLinkedIdentitiesForAccount(y.id)).length;
|
||||
expect(held, "no identity is lost or duplicated").toBe(2);
|
||||
}
|
||||
});
|
||||
|
||||
it("one moveId over two different pairs at once: one moved, the other move_id_conflict, never a receipt for a move it did not make", async () => {
|
||||
const { name, owner } = await appliedDatabase();
|
||||
const storeA = new PostgresStore(owner);
|
||||
const other = fixturePool(urlFor(name));
|
||||
cleanup.push(async () => {
|
||||
await other.end();
|
||||
});
|
||||
const storeB = new PostgresStore(other);
|
||||
for (let round = 0; round < 6; round += 1) {
|
||||
const one = await pair(storeA);
|
||||
const two = await pair(storeA);
|
||||
const id = `move-${randomUUID()}`;
|
||||
const results = await Promise.all([storeA.moveIdentities(request(one.x, one.y, id)), storeB.moveIdentities(request(two.x, two.y, id))]);
|
||||
expect(results.map((r) => r.kind).sort(), `round ${round}`).toEqual(["move_id_conflict", "moved"]);
|
||||
const winner = (await storeA.findIdentityMove(id))!;
|
||||
const moved = results.find((r) => r.kind === "moved") as { move: { fromAccountId: string } };
|
||||
expect(winner.fromAccountId).toBe(moved.move.fromAccountId);
|
||||
}
|
||||
});
|
||||
|
||||
it("two concurrent identical moveIds: one moved, one replayed", async () => {
|
||||
const { name, owner } = await appliedDatabase();
|
||||
const storeA = new PostgresStore(owner);
|
||||
const other = fixturePool(urlFor(name));
|
||||
cleanup.push(async () => {
|
||||
await other.end();
|
||||
});
|
||||
const storeB = new PostgresStore(other);
|
||||
for (let round = 0; round < 5; round += 1) {
|
||||
const { x, y } = await pair(storeA);
|
||||
const body = request(x, y, `move-${randomUUID()}`);
|
||||
const results = await Promise.all([storeA.moveIdentities(body), storeB.moveIdentities(body)]);
|
||||
expect(results.map((r) => r.kind).sort(), `round ${round}`).toEqual(["moved", "replayed"]);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("on real Postgres: an upsert never moves an identity, and concurrent unlinks of the last two leave one", async () => {
|
||||
const { name, owner } = await appliedDatabase();
|
||||
const clientA = new PostgresStore(owner);
|
||||
|
|
@ -920,6 +1192,7 @@ describe.skipIf(!adminUrl)("startup against a live Postgres (HEIMDALL_TEST_PG_AD
|
|||
const appliedByIndex = {
|
||||
auth_completions_attempt_unconsumed_unique_idx: "auth_completions",
|
||||
consent_receipts_revokes_unique_idx: "consent_receipts",
|
||||
identity_moves_reverses_unique_idx: "identity_moves",
|
||||
};
|
||||
const partialKeys = REQUIRED_KEYS.flatMap((key, index) => (key.predicate === "" ? [] : [{ key, index }]));
|
||||
expect(partialKeys.map(({ key }) => key.table).sort()).toEqual(Object.values(appliedByIndex).sort());
|
||||
|
|
|
|||
Loading…
Reference in a new issue