Tests: invite grant mints, is refused without the app caller, other apps, link mode, tampered state

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-29 20:45:04 +01:00
commit acd50278c8

View file

@ -535,3 +535,141 @@ describe("Google as a Heimdall OIDC provider", () => {
expect(accountCount(store)).toBe(1);
});
});
describe("an invite grant lets Bifrost's sign-in start mint the first Google account", () => {
const RETURN = "https://bifrost.gamecult.org/auth/heimdall/complete";
function startGrant(app: FastifyInstance, extra: Record<string, unknown> = {}, headers: Record<string, string> = BIFROST_APP) {
return app.inject({
method: "POST",
url: "/v1/oauth/google/start",
headers,
payload: { appSlug: "bifrost", mode: "sign_in", returnTo: RETURN, firstLoginGrant: "invite", ...extra },
});
}
async function googleCallback(app: FastifyInstance, start: { json(): never }, claims?: Record<string, unknown>) {
const flow = await launchWithRedirect(app, start.json());
fakeGoogle({ nonceFrom: () => flow.url.searchParams.get("nonce") ?? undefined, ...(claims ? { claims } : {}) });
return { flow, done: await callbackGoogle(app, flow) };
}
const auditEvents = (store: InMemoryStore) =>
[...(store as unknown as { auditEvents: Map<string, { eventType: string; eventPayloadJson?: unknown }> }).auditEvents.values()];
it("google_first_login_without_grant_refused: no grant, identity_not_linked, nothing minted", async () => {
const { app, store } = await build();
const start = await startGoogle(app, undefined, "sign_in");
const { done } = await googleCallback(app, start);
expect([done.statusCode, done.json().error]).toEqual([403, "identity_not_linked"]);
expect(accountCount(store)).toBe(0);
});
it("google_first_login_with_bifrost_grant_mints: one account, the identity linked to it, audited", async () => {
const { app, store } = await build();
const start = await startGrant(app);
expect(start.statusCode).toBe(201);
const { done } = await googleCallback(app, start);
expect(done.statusCode).toBe(201);
expect(accountCount(store)).toBe(1);
expect((await store.findStoredLinkedIdentity("google", "g-sub-1"))?.accountId).toBe(done.json().account.id);
const minted = auditEvents(store).filter((e) => e.eventType === "account_minted_by_grant");
expect(minted.map((e) => e.eventPayloadJson)).toEqual([{ provider: "google", appSlug: "bifrost" }]);
});
it("a granted state is single-use: replaying the callback mints nothing more", async () => {
const { app, store } = await build();
const start = await startGrant(app);
const { flow } = await googleCallback(app, start);
const again = await callbackGoogle(app, flow);
expect(again.statusCode).toBeGreaterThanOrEqual(400);
expect(accountCount(store)).toBe(1);
});
it("grant_from_other_app_refused: an app whose profile may not grant gets first_login_grant_refused", async () => {
const { app, store } = await build();
const res = await app.inject({
method: "POST",
url: "/v1/oauth/discord/start",
headers: { "x-heimdall-app-secret": "repixelizer-secret" },
payload: { appSlug: "repixelizer", mode: "sign_in", returnTo: "https://repixelizer.gamecult.org/app/", firstLoginGrant: "invite" },
});
expect([res.statusCode, res.json().error]).toEqual([400, "first_login_grant_refused"]);
// Another app's secret on Bifrost's start is no Bifrost caller.
const other = await startGrant(app, {}, { "x-heimdall-app-secret": "repixelizer-secret" });
expect(other.statusCode).toBe(401);
expect(accountCount(store)).toBe(0);
});
it("grant_without_app_caller_refused: a public start with firstLoginGrant is refused", async () => {
const { app, store } = await build();
for (const headers of [{}, { "x-heimdall-app-secret": "wrong" }]) {
const res = await startGrant(app, {}, headers);
expect(res.statusCode, JSON.stringify(headers)).toBeGreaterThanOrEqual(400);
expect(res.json().authorizationUrl).toBeUndefined();
}
// An app that does not require a caller is still refused by the grant gate itself.
const open = await app.inject({
method: "POST",
url: "/v1/oauth/discord/start",
payload: { appSlug: "repixelizer", mode: "sign_in", returnTo: "https://repixelizer.gamecult.org/app/", firstLoginGrant: "invite" },
});
expect([open.statusCode, open.json().error]).toEqual([400, "first_login_grant_refused"]);
expect(accountCount(store)).toBe(0);
});
it("refuses a grant on link mode and a grant value other than invite", async () => {
const { app } = await build();
const { accessToken } = await signInWithDiscord(app);
const link = await app.inject({
method: "POST",
url: "/v1/oauth/google/start",
headers: { ...BIFROST_APP, authorization: `Bearer ${accessToken}` },
payload: { appSlug: "bifrost", mode: "link", returnTo: RETURN, firstLoginGrant: "invite" },
});
expect([link.statusCode, link.json().error]).toEqual([400, "first_login_grant_refused"]);
expect((await startGrant(app, { firstLoginGrant: "admin" })).statusCode).toBe(400);
});
it("grant_ignored_for_existing_account: a Google identity already held signs in to its account, no second account", async () => {
const { app, store } = await build();
const { accessToken, accountId } = await signInWithDiscord(app);
await linkGoogle(app, accessToken);
const start = await startGrant(app);
const { done } = await googleCallback(app, start);
expect(done.statusCode).toBe(201);
expect(done.json().account.id).toBe(accountId);
expect(accountCount(store)).toBe(1);
expect(auditEvents(store).some((e) => e.eventType === "account_minted_by_grant")).toBe(false);
});
it("a grant never attaches an identity to an existing account: a new sub mints its own account and an held sub is never moved", async () => {
const store = new InMemoryStore();
const first = await build(store, "discord-1");
const a = await signInWithDiscord(first.app, "discord-1");
await linkGoogle(first.app, a.accessToken);
const second = await build(store, "discord-2");
const b = await signInWithDiscord(second.app, "discord-2");
const start = await startGrant(second.app);
const { done } = await googleCallback(second.app, start, { sub: "g-sub-new" });
expect(done.statusCode).toBe(201);
expect(done.json().account.id).not.toBe(a.accountId);
expect(done.json().account.id).not.toBe(b.accountId);
expect((await store.findStoredLinkedIdentity("google", "g-sub-1"))?.accountId).toBe(a.accountId);
expect((await store.findStoredLinkedIdentity("google", "g-sub-new"))?.accountId).toBe(done.json().account.id);
});
it("grant_tampered_state_refused: a first_login_grant added to a signed state fails verification", async () => {
const { app, store } = await build();
const plain = await startGoogle(app, undefined, "sign_in");
const [head, body, sig] = stateOf(plain.json()).split(".");
const payload = JSON.parse(Buffer.from(body!, "base64url").toString());
const forged = [head, Buffer.from(JSON.stringify({ ...payload, first_login_grant: "invite" })).toString("base64url"), sig].join(".");
const launched = await app.inject({ method: "GET", url: `/v1/oauth/google/launch?state=${encodeURIComponent(forged)}` });
expect(launched.statusCode).toBe(400);
expect(launched.headers.location).toBeUndefined();
const cb = await app.inject({ method: "GET", url: `/v1/oauth/google/callback?code=g-code&state=${encodeURIComponent(forged)}` });
expect(cb.statusCode).toBeGreaterThanOrEqual(400);
expect(accountCount(store)).toBe(0);
});
});