Google OIDC provider: link-only, sub-keyed, PKCE and nonce
Google is a Heimdall provider behind Bifrost, with no direct upstream.
Scopes openid email profile; the callback verifies the id_token
(RS256 against Google's keys, iss, aud, exp, iat, nonce) and requires
a verified email. The identity key is sub, never the email.
PKCE verifier and nonce are derived from the state jti and Heimdall's
signing key, so nothing is stored between start and callback.
Google never mints an account. mode=link needs a Heimdall access token
for the app and attaches to that account only; sign_in signs in an
identity already linked; an unknown sub is refused, and a sub held by
another account is never moved. The six existing providers still mint
on first login.
Admitted to the bifrost profile only. The Idunn recipe declares the
Google client id and secret file.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 16:10:23 +00:00
|
|
|
import { createHash, createSign, generateKeyPairSync } from "node:crypto";
|
2026-11-04 17:15:33 +00:00
|
|
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
Google OIDC provider: link-only, sub-keyed, PKCE and nonce
Google is a Heimdall provider behind Bifrost, with no direct upstream.
Scopes openid email profile; the callback verifies the id_token
(RS256 against Google's keys, iss, aud, exp, iat, nonce) and requires
a verified email. The identity key is sub, never the email.
PKCE verifier and nonce are derived from the state jti and Heimdall's
signing key, so nothing is stored between start and callback.
Google never mints an account. mode=link needs a Heimdall access token
for the app and attaches to that account only; sign_in signs in an
identity already linked; an unknown sub is refused, and a sub held by
another account is never moved. The six existing providers still mint
on first login.
Admitted to the bifrost profile only. The Idunn recipe declares the
Google client id and secret file.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 16:10:23 +00:00
|
|
|
import { type FastifyInstance } from "fastify";
|
|
|
|
|
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";
|
2026-11-13 18:59:21 +00:00
|
|
|
import { IdentityHeldElsewhereError, InMemoryStore } from "../src/store/index.js";
|
2026-11-05 12:46:05 +00:00
|
|
|
import { launchFlow, launchWithRedirect, stateOf, type LaunchedFlow } from "./oauth-flow.js";
|
Google OIDC provider: link-only, sub-keyed, PKCE and nonce
Google is a Heimdall provider behind Bifrost, with no direct upstream.
Scopes openid email profile; the callback verifies the id_token
(RS256 against Google's keys, iss, aud, exp, iat, nonce) and requires
a verified email. The identity key is sub, never the email.
PKCE verifier and nonce are derived from the state jti and Heimdall's
signing key, so nothing is stored between start and callback.
Google never mints an account. mode=link needs a Heimdall access token
for the app and attaches to that account only; sign_in signs in an
identity already linked; an unknown sub is refused, and a sub held by
another account is never moved. The six existing providers still mint
on first login.
Admitted to the bifrost profile only. The Idunn recipe declares the
Google client id and secret file.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 16:10:23 +00:00
|
|
|
|
|
|
|
|
const CLIENT_ID = "google-client";
|
|
|
|
|
const REDIRECT = "https://heimdall.gamecult.org/v1/oauth/google/callback";
|
|
|
|
|
const rsa = generateKeyPairSync("rsa", { modulusLength: 2048 });
|
|
|
|
|
const originalFetch = globalThis.fetch;
|
|
|
|
|
const apps: FastifyInstance[] = [];
|
|
|
|
|
|
|
|
|
|
function config(): HeimdallConfig {
|
|
|
|
|
const base = loadConfig({
|
|
|
|
|
GC_ACCESS_BASE_URL: "https://heimdall.gamecult.org",
|
|
|
|
|
GC_ACCESS_PROVIDER_GOOGLE_CLIENT_ID: CLIENT_ID,
|
|
|
|
|
GC_ACCESS_PROVIDER_DISCORD_CLIENT_ID: "discord-client",
|
|
|
|
|
} as NodeJS.ProcessEnv);
|
|
|
|
|
return {
|
|
|
|
|
...base,
|
|
|
|
|
storage: { backend: "memory", applySchemaOnStartup: true },
|
|
|
|
|
tokenEncryptionKeyBase64: Buffer.alloc(32, 7).toString("base64"),
|
|
|
|
|
bootstrapSigningPrivateKeyOnMissing: true,
|
2026-11-04 17:15:33 +00:00
|
|
|
appSharedSecrets: { ...base.appSharedSecrets, bifrost: "bifrost-secret" },
|
Google OIDC provider: link-only, sub-keyed, PKCE and nonce
Google is a Heimdall provider behind Bifrost, with no direct upstream.
Scopes openid email profile; the callback verifies the id_token
(RS256 against Google's keys, iss, aud, exp, iat, nonce) and requires
a verified email. The identity key is sub, never the email.
PKCE verifier and nonce are derived from the state jti and Heimdall's
signing key, so nothing is stored between start and callback.
Google never mints an account. mode=link needs a Heimdall access token
for the app and attaches to that account only; sign_in signs in an
identity already linked; an unknown sub is refused, and a sub held by
another account is never moved. The six existing providers still mint
on first login.
Admitted to the bifrost profile only. The Idunn recipe declares the
Google client id and secret file.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 16:10:23 +00:00
|
|
|
providers: {
|
|
|
|
|
...base.providers,
|
|
|
|
|
google: { clientId: CLIENT_ID, clientSecret: "google-secret" },
|
|
|
|
|
discord: { clientId: "discord-client", clientSecret: "discord-secret" },
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function discordRuntime(providerUserId: string, name: string): OAuthProviderRuntime {
|
|
|
|
|
return {
|
|
|
|
|
async exchangeAuthorizationCode() {
|
|
|
|
|
return { accessToken: "d-at", refreshToken: "d-rt", tokenType: "Bearer", scope: ["identify"], expiresAt: "2099-01-01T00:00:00.000Z", raw: {} };
|
|
|
|
|
},
|
|
|
|
|
async resolveIdentity() {
|
|
|
|
|
return { provider: "discord", providerUserId, username: name, displayName: name, profile: { id: providerUserId } };
|
|
|
|
|
},
|
2026-11-04 17:15:33 +00:00
|
|
|
// Discord grants app access only under a policy the app named at start.
|
|
|
|
|
async evaluateEntitlements(input) {
|
|
|
|
|
return { facts: input.callback.entitlementPolicy ? ["entitlement.app_access"] : [], snapshots: [] };
|
Google OIDC provider: link-only, sub-keyed, PKCE and nonce
Google is a Heimdall provider behind Bifrost, with no direct upstream.
Scopes openid email profile; the callback verifies the id_token
(RS256 against Google's keys, iss, aud, exp, iat, nonce) and requires
a verified email. The identity key is sub, never the email.
PKCE verifier and nonce are derived from the state jti and Heimdall's
signing key, so nothing is stored between start and callback.
Google never mints an account. mode=link needs a Heimdall access token
for the app and attaches to that account only; sign_in signs in an
identity already linked; an unknown sub is refused, and a sub held by
another account is never moved. The six existing providers still mint
on first login.
Admitted to the bifrost profile only. The Idunn recipe declares the
Google client id and secret file.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 16:10:23 +00:00
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-05 12:46:05 +00:00
|
|
|
// Bifrost refuses an unauthenticated start: its backend holds the app secret.
|
|
|
|
|
const BIFROST_APP = { "x-heimdall-app-secret": "bifrost-secret" };
|
|
|
|
|
|
Google OIDC provider: link-only, sub-keyed, PKCE and nonce
Google is a Heimdall provider behind Bifrost, with no direct upstream.
Scopes openid email profile; the callback verifies the id_token
(RS256 against Google's keys, iss, aud, exp, iat, nonce) and requires
a verified email. The identity key is sub, never the email.
PKCE verifier and nonce are derived from the state jti and Heimdall's
signing key, so nothing is stored between start and callback.
Google never mints an account. mode=link needs a Heimdall access token
for the app and attaches to that account only; sign_in signs in an
identity already linked; an unknown sub is refused, and a sub held by
another account is never moved. The six existing providers still mint
on first login.
Admitted to the bifrost profile only. The Idunn recipe declares the
Google client id and secret file.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 16:10:23 +00:00
|
|
|
function b64(value: unknown): string {
|
|
|
|
|
return Buffer.from(JSON.stringify(value)).toString("base64url");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function signedIdToken(claims: Record<string, unknown>): string {
|
|
|
|
|
const now = Math.floor(Date.now() / 1000);
|
|
|
|
|
const body = `${b64({ alg: "RS256", kid: "k1" })}.${b64({
|
|
|
|
|
iss: "https://accounts.google.com",
|
|
|
|
|
aud: CLIENT_ID,
|
|
|
|
|
azp: CLIENT_ID,
|
|
|
|
|
sub: "g-sub-1",
|
|
|
|
|
email: "person@example.org",
|
|
|
|
|
email_verified: true,
|
|
|
|
|
name: "Person",
|
|
|
|
|
iat: now,
|
|
|
|
|
exp: now + 600,
|
|
|
|
|
...claims,
|
|
|
|
|
})}`;
|
|
|
|
|
return `${body}.${createSign("RSA-SHA256").update(body).sign(rsa.privateKey).toString("base64url")}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface GoogleLeg {
|
|
|
|
|
tokenRequests: URLSearchParams[];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Google's two endpoints: the key set, and a token endpoint whose id_token echoes the nonce it was told. */
|
|
|
|
|
function fakeGoogle(options: { nonceFrom: () => string | undefined; claims?: Record<string, unknown> }): GoogleLeg {
|
|
|
|
|
const leg: GoogleLeg = { tokenRequests: [] };
|
|
|
|
|
globalThis.fetch = (async (input: string | URL, init?: RequestInit) => {
|
|
|
|
|
const url = String(input);
|
|
|
|
|
if (url === "https://www.googleapis.com/oauth2/v3/certs") {
|
|
|
|
|
return new Response(
|
|
|
|
|
JSON.stringify({ keys: [{ ...(rsa.publicKey.export({ format: "jwk" }) as object), kid: "k1", alg: "RS256" }] }),
|
|
|
|
|
{ status: 200 }
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if (url === "https://oauth2.googleapis.com/token") {
|
|
|
|
|
leg.tokenRequests.push(new URLSearchParams(String(init?.body)));
|
|
|
|
|
return new Response(
|
|
|
|
|
JSON.stringify({
|
|
|
|
|
access_token: "g-at",
|
|
|
|
|
token_type: "Bearer",
|
|
|
|
|
expires_in: 3600,
|
|
|
|
|
scope: "openid email profile",
|
|
|
|
|
id_token: signedIdToken({ nonce: options.nonceFrom(), ...options.claims }),
|
|
|
|
|
}),
|
|
|
|
|
{ status: 200 }
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
throw new Error(`unexpected fetch ${url}`);
|
|
|
|
|
}) as typeof fetch;
|
|
|
|
|
return leg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function accountCount(store: InMemoryStore): number {
|
|
|
|
|
return (store as unknown as { accounts: Map<string, unknown> }).accounts.size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function signInWithDiscord(app: FastifyInstance, providerUserId = "discord-1"): Promise<{ accessToken: string; accountId: string }> {
|
|
|
|
|
const start = await app.inject({
|
|
|
|
|
method: "POST",
|
|
|
|
|
url: "/v1/oauth/discord/start",
|
2026-11-05 12:46:05 +00:00
|
|
|
headers: BIFROST_APP,
|
Google OIDC provider: link-only, sub-keyed, PKCE and nonce
Google is a Heimdall provider behind Bifrost, with no direct upstream.
Scopes openid email profile; the callback verifies the id_token
(RS256 against Google's keys, iss, aud, exp, iat, nonce) and requires
a verified email. The identity key is sub, never the email.
PKCE verifier and nonce are derived from the state jti and Heimdall's
signing key, so nothing is stored between start and callback.
Google never mints an account. mode=link needs a Heimdall access token
for the app and attaches to that account only; sign_in signs in an
identity already linked; an unknown sub is refused, and a sub held by
another account is never moved. The six existing providers still mint
on first login.
Admitted to the bifrost profile only. The Idunn recipe declares the
Google client id and secret file.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 16:10:23 +00:00
|
|
|
payload: { appSlug: "bifrost", mode: "sign_in", returnTo: "https://bifrost.gamecult.org/auth/heimdall/complete" },
|
|
|
|
|
});
|
|
|
|
|
expect(start.statusCode).toBe(201);
|
2026-11-05 12:46:05 +00:00
|
|
|
const flow = await launchFlow(app, start.json());
|
Google OIDC provider: link-only, sub-keyed, PKCE and nonce
Google is a Heimdall provider behind Bifrost, with no direct upstream.
Scopes openid email profile; the callback verifies the id_token
(RS256 against Google's keys, iss, aud, exp, iat, nonce) and requires
a verified email. The identity key is sub, never the email.
PKCE verifier and nonce are derived from the state jti and Heimdall's
signing key, so nothing is stored between start and callback.
Google never mints an account. mode=link needs a Heimdall access token
for the app and attaches to that account only; sign_in signs in an
identity already linked; an unknown sub is refused, and a sub held by
another account is never moved. The six existing providers still mint
on first login.
Admitted to the bifrost profile only. The Idunn recipe declares the
Google client id and secret file.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 16:10:23 +00:00
|
|
|
const callback = await app.inject({
|
|
|
|
|
method: "GET",
|
2026-11-05 12:46:05 +00:00
|
|
|
url: `/v1/oauth/discord/callback?code=c&state=${encodeURIComponent(flow.state)}`,
|
|
|
|
|
headers: { cookie: flow.cookie },
|
Google OIDC provider: link-only, sub-keyed, PKCE and nonce
Google is a Heimdall provider behind Bifrost, with no direct upstream.
Scopes openid email profile; the callback verifies the id_token
(RS256 against Google's keys, iss, aud, exp, iat, nonce) and requires
a verified email. The identity key is sub, never the email.
PKCE verifier and nonce are derived from the state jti and Heimdall's
signing key, so nothing is stored between start and callback.
Google never mints an account. mode=link needs a Heimdall access token
for the app and attaches to that account only; sign_in signs in an
identity already linked; an unknown sub is refused, and a sub held by
another account is never moved. The six existing providers still mint
on first login.
Admitted to the bifrost profile only. The Idunn recipe declares the
Google client id and secret file.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 16:10:23 +00:00
|
|
|
});
|
|
|
|
|
expect(callback.statusCode).toBe(201);
|
|
|
|
|
const body = callback.json();
|
|
|
|
|
return { accessToken: body.accessToken as string, accountId: body.account.id as string };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function build(store = new InMemoryStore(), discordUser = "discord-1") {
|
|
|
|
|
const app = await buildApp({ config: config(), store, oauthRuntimes: { discord: discordRuntime(discordUser, "Person") } });
|
|
|
|
|
apps.push(app);
|
|
|
|
|
return { app, store };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function startGoogle(app: FastifyInstance, accessToken?: string, mode: "link" | "sign_in" = "link") {
|
|
|
|
|
return app.inject({
|
|
|
|
|
method: "POST",
|
|
|
|
|
url: "/v1/oauth/google/start",
|
2026-11-05 12:46:05 +00:00
|
|
|
headers: { ...BIFROST_APP, ...(accessToken ? { authorization: `Bearer ${accessToken}` } : {}) },
|
Google OIDC provider: link-only, sub-keyed, PKCE and nonce
Google is a Heimdall provider behind Bifrost, with no direct upstream.
Scopes openid email profile; the callback verifies the id_token
(RS256 against Google's keys, iss, aud, exp, iat, nonce) and requires
a verified email. The identity key is sub, never the email.
PKCE verifier and nonce are derived from the state jti and Heimdall's
signing key, so nothing is stored between start and callback.
Google never mints an account. mode=link needs a Heimdall access token
for the app and attaches to that account only; sign_in signs in an
identity already linked; an unknown sub is refused, and a sub held by
another account is never moved. The six existing providers still mint
on first login.
Admitted to the bifrost profile only. The Idunn recipe declares the
Google client id and secret file.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 16:10:23 +00:00
|
|
|
payload: { appSlug: "bifrost", mode, returnTo: "https://bifrost.gamecult.org/auth/heimdall/complete" },
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-05 12:46:05 +00:00
|
|
|
function callbackGoogle(app: FastifyInstance, flow: LaunchedFlow) {
|
|
|
|
|
return app.inject({
|
|
|
|
|
method: "GET",
|
|
|
|
|
url: `/v1/oauth/google/callback?code=g-code&state=${encodeURIComponent(flow.state)}`,
|
|
|
|
|
headers: { cookie: flow.cookie },
|
|
|
|
|
});
|
Google OIDC provider: link-only, sub-keyed, PKCE and nonce
Google is a Heimdall provider behind Bifrost, with no direct upstream.
Scopes openid email profile; the callback verifies the id_token
(RS256 against Google's keys, iss, aud, exp, iat, nonce) and requires
a verified email. The identity key is sub, never the email.
PKCE verifier and nonce are derived from the state jti and Heimdall's
signing key, so nothing is stored between start and callback.
Google never mints an account. mode=link needs a Heimdall access token
for the app and attaches to that account only; sign_in signs in an
identity already linked; an unknown sub is refused, and a sub held by
another account is never moved. The six existing providers still mint
on first login.
Admitted to the bifrost profile only. The Idunn recipe declares the
Google client id and secret file.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 16:10:23 +00:00
|
|
|
}
|
|
|
|
|
|
2026-11-04 17:15:33 +00:00
|
|
|
const DISCORD_POLICY = { kind: "discord_role_access", guildId: "g", allowedRoleIds: ["member"] } as const;
|
|
|
|
|
|
|
|
|
|
/** Begin a link with `token`, let Google answer as `claims` says, and return the callback's answer. */
|
|
|
|
|
async function linkGoogleCallback(app: FastifyInstance, token: string, claims?: Record<string, unknown>) {
|
|
|
|
|
const start = await startGoogle(app, token);
|
|
|
|
|
expect(start.statusCode).toBe(201);
|
2026-11-05 12:46:05 +00:00
|
|
|
const startFlow = await launchWithRedirect(app, start.json());
|
|
|
|
|
const url = startFlow.url;
|
2026-11-04 17:15:33 +00:00
|
|
|
fakeGoogle({ nonceFrom: () => url.searchParams.get("nonce") ?? undefined, ...(claims ? { claims } : {}) });
|
2026-11-05 12:46:05 +00:00
|
|
|
return callbackGoogle(app, startFlow);
|
2026-11-04 17:15:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function confirmLink(app: FastifyInstance, token: string | undefined, pendingLinkId: string) {
|
|
|
|
|
return app.inject({
|
|
|
|
|
method: "POST",
|
|
|
|
|
url: "/v1/apps/bifrost/links/confirm",
|
|
|
|
|
headers: token ? { authorization: `Bearer ${token}` } : {},
|
|
|
|
|
payload: { pendingLinkId },
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** The whole link: callback, then the initiator's confirm. */
|
|
|
|
|
async function linkGoogle(app: FastifyInstance, token: string, claims?: Record<string, unknown>) {
|
|
|
|
|
const done = await linkGoogleCallback(app, token, claims);
|
|
|
|
|
expect(done.statusCode).toBe(201);
|
|
|
|
|
const confirmed = await confirmLink(app, token, done.json().pendingLinkId);
|
|
|
|
|
expect(confirmed.statusCode).toBe(201);
|
|
|
|
|
return confirmed;
|
|
|
|
|
}
|
|
|
|
|
|
Google OIDC provider: link-only, sub-keyed, PKCE and nonce
Google is a Heimdall provider behind Bifrost, with no direct upstream.
Scopes openid email profile; the callback verifies the id_token
(RS256 against Google's keys, iss, aud, exp, iat, nonce) and requires
a verified email. The identity key is sub, never the email.
PKCE verifier and nonce are derived from the state jti and Heimdall's
signing key, so nothing is stored between start and callback.
Google never mints an account. mode=link needs a Heimdall access token
for the app and attaches to that account only; sign_in signs in an
identity already linked; an unknown sub is refused, and a sub held by
another account is never moved. The six existing providers still mint
on first login.
Admitted to the bifrost profile only. The Idunn recipe declares the
Google client id and secret file.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 16:10:23 +00:00
|
|
|
afterEach(async () => {
|
|
|
|
|
globalThis.fetch = originalFetch;
|
|
|
|
|
resetGoogleJwksCache();
|
|
|
|
|
while (apps.length) await apps.pop()?.close();
|
|
|
|
|
});
|
|
|
|
|
|
2026-11-13 18:59:21 +00:00
|
|
|
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"]);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
Google OIDC provider: link-only, sub-keyed, PKCE and nonce
Google is a Heimdall provider behind Bifrost, with no direct upstream.
Scopes openid email profile; the callback verifies the id_token
(RS256 against Google's keys, iss, aud, exp, iat, nonce) and requires
a verified email. The identity key is sub, never the email.
PKCE verifier and nonce are derived from the state jti and Heimdall's
signing key, so nothing is stored between start and callback.
Google never mints an account. mode=link needs a Heimdall access token
for the app and attaches to that account only; sign_in signs in an
identity already linked; an unknown sub is refused, and a sub held by
another account is never moved. The six existing providers still mint
on first login.
Admitted to the bifrost profile only. The Idunn recipe declares the
Google client id and secret file.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 16:10:23 +00:00
|
|
|
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();
|
|
|
|
|
const { accessToken } = await signInWithDiscord(app);
|
|
|
|
|
const start = await startGoogle(app, accessToken);
|
|
|
|
|
|
|
|
|
|
expect(start.statusCode).toBe(201);
|
|
|
|
|
expect(start.json().callbackUrl).toBe(REDIRECT);
|
2026-11-05 12:46:05 +00:00
|
|
|
const startFlow = await launchWithRedirect(app, start.json());
|
|
|
|
|
const url = startFlow.url;
|
Google OIDC provider: link-only, sub-keyed, PKCE and nonce
Google is a Heimdall provider behind Bifrost, with no direct upstream.
Scopes openid email profile; the callback verifies the id_token
(RS256 against Google's keys, iss, aud, exp, iat, nonce) and requires
a verified email. The identity key is sub, never the email.
PKCE verifier and nonce are derived from the state jti and Heimdall's
signing key, so nothing is stored between start and callback.
Google never mints an account. mode=link needs a Heimdall access token
for the app and attaches to that account only; sign_in signs in an
identity already linked; an unknown sub is refused, and a sub held by
another account is never moved. The six existing providers still mint
on first login.
Admitted to the bifrost profile only. The Idunn recipe declares the
Google client id and secret file.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 16:10:23 +00:00
|
|
|
expect(`${url.origin}${url.pathname}`).toBe("https://accounts.google.com/o/oauth2/v2/auth");
|
|
|
|
|
expect(url.searchParams.get("client_id")).toBe(CLIENT_ID);
|
|
|
|
|
expect(url.searchParams.get("redirect_uri")).toBe(REDIRECT);
|
|
|
|
|
expect(url.searchParams.get("response_type")).toBe("code");
|
|
|
|
|
expect(url.searchParams.get("scope")).toBe("openid email profile");
|
2026-11-05 12:46:05 +00:00
|
|
|
expect(url.searchParams.get("state")).toBe(stateOf(start.json()));
|
Google OIDC provider: link-only, sub-keyed, PKCE and nonce
Google is a Heimdall provider behind Bifrost, with no direct upstream.
Scopes openid email profile; the callback verifies the id_token
(RS256 against Google's keys, iss, aud, exp, iat, nonce) and requires
a verified email. The identity key is sub, never the email.
PKCE verifier and nonce are derived from the state jti and Heimdall's
signing key, so nothing is stored between start and callback.
Google never mints an account. mode=link needs a Heimdall access token
for the app and attaches to that account only; sign_in signs in an
identity already linked; an unknown sub is refused, and a sub held by
another account is never moved. The six existing providers still mint
on first login.
Admitted to the bifrost profile only. The Idunn recipe declares the
Google client id and secret file.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 16:10:23 +00:00
|
|
|
expect(url.searchParams.get("nonce")).toMatch(/^[A-Za-z0-9_-]{43}$/);
|
|
|
|
|
expect(url.searchParams.get("code_challenge")).toMatch(/^[A-Za-z0-9_-]{43}$/);
|
|
|
|
|
expect(url.searchParams.get("code_challenge_method")).toBe("S256");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("links Google to the account that asked, with PKCE proven end to end and the id_token's nonce matched", async () => {
|
|
|
|
|
const { app, store } = await build();
|
|
|
|
|
const { accessToken, accountId } = await signInWithDiscord(app);
|
|
|
|
|
const start = await startGoogle(app, accessToken);
|
2026-11-05 12:46:05 +00:00
|
|
|
const startFlow = await launchWithRedirect(app, start.json());
|
|
|
|
|
const url = startFlow.url;
|
Google OIDC provider: link-only, sub-keyed, PKCE and nonce
Google is a Heimdall provider behind Bifrost, with no direct upstream.
Scopes openid email profile; the callback verifies the id_token
(RS256 against Google's keys, iss, aud, exp, iat, nonce) and requires
a verified email. The identity key is sub, never the email.
PKCE verifier and nonce are derived from the state jti and Heimdall's
signing key, so nothing is stored between start and callback.
Google never mints an account. mode=link needs a Heimdall access token
for the app and attaches to that account only; sign_in signs in an
identity already linked; an unknown sub is refused, and a sub held by
another account is never moved. The six existing providers still mint
on first login.
Admitted to the bifrost profile only. The Idunn recipe declares the
Google client id and secret file.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 16:10:23 +00:00
|
|
|
const leg = fakeGoogle({ nonceFrom: () => url.searchParams.get("nonce") ?? undefined });
|
|
|
|
|
|
2026-11-05 12:46:05 +00:00
|
|
|
const done = await callbackGoogle(app, startFlow);
|
Google OIDC provider: link-only, sub-keyed, PKCE and nonce
Google is a Heimdall provider behind Bifrost, with no direct upstream.
Scopes openid email profile; the callback verifies the id_token
(RS256 against Google's keys, iss, aud, exp, iat, nonce) and requires
a verified email. The identity key is sub, never the email.
PKCE verifier and nonce are derived from the state jti and Heimdall's
signing key, so nothing is stored between start and callback.
Google never mints an account. mode=link needs a Heimdall access token
for the app and attaches to that account only; sign_in signs in an
identity already linked; an unknown sub is refused, and a sub held by
another account is never moved. The six existing providers still mint
on first login.
Admitted to the bifrost profile only. The Idunn recipe declares the
Google client id and secret file.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 16:10:23 +00:00
|
|
|
|
2026-11-04 17:15:33 +00:00
|
|
|
// Phase one verified the identity and recorded it; it linked nothing and issued no session.
|
Google OIDC provider: link-only, sub-keyed, PKCE and nonce
Google is a Heimdall provider behind Bifrost, with no direct upstream.
Scopes openid email profile; the callback verifies the id_token
(RS256 against Google's keys, iss, aud, exp, iat, nonce) and requires
a verified email. The identity key is sub, never the email.
PKCE verifier and nonce are derived from the state jti and Heimdall's
signing key, so nothing is stored between start and callback.
Google never mints an account. mode=link needs a Heimdall access token
for the app and attaches to that account only; sign_in signs in an
identity already linked; an unknown sub is refused, and a sub held by
another account is never moved. The six existing providers still mint
on first login.
Admitted to the bifrost profile only. The Idunn recipe declares the
Google client id and secret file.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 16:10:23 +00:00
|
|
|
expect(done.statusCode).toBe(201);
|
2026-11-04 17:15:33 +00:00
|
|
|
expect(done.json().status).toBe("link_pending");
|
|
|
|
|
expect(done.json().accessToken).toBeUndefined();
|
|
|
|
|
expect(await store.findStoredLinkedIdentity("google", "g-sub-1")).toBeNull();
|
|
|
|
|
const confirmed = await confirmLink(app, accessToken, done.json().pendingLinkId);
|
|
|
|
|
expect(confirmed.statusCode).toBe(201);
|
|
|
|
|
expect(confirmed.json().accountId).toBe(accountId);
|
Google OIDC provider: link-only, sub-keyed, PKCE and nonce
Google is a Heimdall provider behind Bifrost, with no direct upstream.
Scopes openid email profile; the callback verifies the id_token
(RS256 against Google's keys, iss, aud, exp, iat, nonce) and requires
a verified email. The identity key is sub, never the email.
PKCE verifier and nonce are derived from the state jti and Heimdall's
signing key, so nothing is stored between start and callback.
Google never mints an account. mode=link needs a Heimdall access token
for the app and attaches to that account only; sign_in signs in an
identity already linked; an unknown sub is refused, and a sub held by
another account is never moved. The six existing providers still mint
on first login.
Admitted to the bifrost profile only. The Idunn recipe declares the
Google client id and secret file.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 16:10:23 +00:00
|
|
|
expect(accountCount(store)).toBe(1);
|
|
|
|
|
const sent = leg.tokenRequests[0]!;
|
|
|
|
|
expect(sent.get("grant_type")).toBe("authorization_code");
|
|
|
|
|
expect(sent.get("redirect_uri")).toBe(REDIRECT);
|
|
|
|
|
// The verifier Heimdall sent hashes to the challenge it put in the authorization URL.
|
|
|
|
|
expect(createHash("sha256").update(sent.get("code_verifier") ?? "", "ascii").digest("base64url")).toBe(
|
|
|
|
|
url.searchParams.get("code_challenge")
|
|
|
|
|
);
|
|
|
|
|
const stored = await store.findStoredLinkedIdentity("google", "g-sub-1");
|
|
|
|
|
expect(stored?.accountId).toBe(accountId);
|
|
|
|
|
expect(stored?.primaryEmail).toBe("person@example.org");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("signs in the account Google is linked to, with no session, and mints nothing", async () => {
|
|
|
|
|
const { app, store } = await build();
|
|
|
|
|
const { accessToken, accountId } = await signInWithDiscord(app);
|
2026-11-04 17:15:33 +00:00
|
|
|
await linkGoogle(app, accessToken);
|
Google OIDC provider: link-only, sub-keyed, PKCE and nonce
Google is a Heimdall provider behind Bifrost, with no direct upstream.
Scopes openid email profile; the callback verifies the id_token
(RS256 against Google's keys, iss, aud, exp, iat, nonce) and requires
a verified email. The identity key is sub, never the email.
PKCE verifier and nonce are derived from the state jti and Heimdall's
signing key, so nothing is stored between start and callback.
Google never mints an account. mode=link needs a Heimdall access token
for the app and attaches to that account only; sign_in signs in an
identity already linked; an unknown sub is refused, and a sub held by
another account is never moved. The six existing providers still mint
on first login.
Admitted to the bifrost profile only. The Idunn recipe declares the
Google client id and secret file.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 16:10:23 +00:00
|
|
|
|
|
|
|
|
const signIn = await startGoogle(app, undefined, "sign_in");
|
|
|
|
|
expect(signIn.statusCode).toBe(201);
|
2026-11-05 12:46:05 +00:00
|
|
|
const signInFlow = await launchWithRedirect(app, signIn.json());
|
|
|
|
|
const signInUrl = signInFlow.url;
|
Google OIDC provider: link-only, sub-keyed, PKCE and nonce
Google is a Heimdall provider behind Bifrost, with no direct upstream.
Scopes openid email profile; the callback verifies the id_token
(RS256 against Google's keys, iss, aud, exp, iat, nonce) and requires
a verified email. The identity key is sub, never the email.
PKCE verifier and nonce are derived from the state jti and Heimdall's
signing key, so nothing is stored between start and callback.
Google never mints an account. mode=link needs a Heimdall access token
for the app and attaches to that account only; sign_in signs in an
identity already linked; an unknown sub is refused, and a sub held by
another account is never moved. The six existing providers still mint
on first login.
Admitted to the bifrost profile only. The Idunn recipe declares the
Google client id and secret file.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 16:10:23 +00:00
|
|
|
fakeGoogle({ nonceFrom: () => signInUrl.searchParams.get("nonce") ?? undefined });
|
2026-11-05 12:46:05 +00:00
|
|
|
const done = await callbackGoogle(app, signInFlow);
|
Google OIDC provider: link-only, sub-keyed, PKCE and nonce
Google is a Heimdall provider behind Bifrost, with no direct upstream.
Scopes openid email profile; the callback verifies the id_token
(RS256 against Google's keys, iss, aud, exp, iat, nonce) and requires
a verified email. The identity key is sub, never the email.
PKCE verifier and nonce are derived from the state jti and Heimdall's
signing key, so nothing is stored between start and callback.
Google never mints an account. mode=link needs a Heimdall access token
for the app and attaches to that account only; sign_in signs in an
identity already linked; an unknown sub is refused, and a sub held by
another account is never moved. The six existing providers still mint
on first login.
Admitted to the bifrost profile only. The Idunn recipe declares the
Google client id and secret file.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 16:10:23 +00:00
|
|
|
|
|
|
|
|
expect(done.statusCode).toBe(201);
|
|
|
|
|
expect(done.json().account.id).toBe(accountId);
|
|
|
|
|
expect(accountCount(store)).toBe(1);
|
|
|
|
|
});
|
|
|
|
|
|
2026-11-05 10:58:02 +00:00
|
|
|
it("refuses a link start whose named initiator is not the bearer's account", async () => {
|
|
|
|
|
const { app, store } = await build();
|
|
|
|
|
const a = await signInWithDiscord(app, "discord-1");
|
|
|
|
|
const start = await app.inject({
|
|
|
|
|
method: "POST",
|
|
|
|
|
url: "/v1/oauth/google/start",
|
2026-11-05 12:46:05 +00:00
|
|
|
headers: { ...BIFROST_APP, authorization: `Bearer ${a.accessToken}` },
|
2026-11-05 10:58:02 +00:00
|
|
|
payload: {
|
|
|
|
|
appSlug: "bifrost",
|
|
|
|
|
mode: "link",
|
|
|
|
|
returnTo: "https://bifrost.gamecult.org/auth/heimdall/complete",
|
|
|
|
|
initiatingAccountId: `${a.accountId}-other`,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
expect([start.statusCode, start.json().error]).toEqual([400, "link_initiator_conflict"]);
|
|
|
|
|
expect(accountCount(store)).toBe(1);
|
|
|
|
|
});
|
|
|
|
|
|
Google OIDC provider: link-only, sub-keyed, PKCE and nonce
Google is a Heimdall provider behind Bifrost, with no direct upstream.
Scopes openid email profile; the callback verifies the id_token
(RS256 against Google's keys, iss, aud, exp, iat, nonce) and requires
a verified email. The identity key is sub, never the email.
PKCE verifier and nonce are derived from the state jti and Heimdall's
signing key, so nothing is stored between start and callback.
Google never mints an account. mode=link needs a Heimdall access token
for the app and attaches to that account only; sign_in signs in an
identity already linked; an unknown sub is refused, and a sub held by
another account is never moved. The six existing providers still mint
on first login.
Admitted to the bifrost profile only. The Idunn recipe declares the
Google client id and secret file.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 16:10:23 +00:00
|
|
|
it("refuses a link start with no Heimdall session", async () => {
|
|
|
|
|
const { app, store } = await build();
|
|
|
|
|
const start = await startGoogle(app);
|
|
|
|
|
expect(start.statusCode).toBe(401);
|
|
|
|
|
expect(start.json().error).toBe("link_session_required");
|
|
|
|
|
expect(accountCount(store)).toBe(0);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it.each([
|
|
|
|
|
["a garbage bearer", () => "not-a-token"],
|
|
|
|
|
["a token for another app", async (app: FastifyInstance) => {
|
|
|
|
|
const start = await app.inject({
|
|
|
|
|
method: "POST",
|
|
|
|
|
url: "/v1/oauth/discord/start",
|
|
|
|
|
payload: { appSlug: "repixelizer", mode: "sign_in", returnTo: "https://repixelizer.gamecult.org/app/" },
|
|
|
|
|
});
|
2026-11-05 12:46:05 +00:00
|
|
|
const flow = await launchFlow(app, start.json());
|
|
|
|
|
const cb = await app.inject({ method: "GET", url: `/v1/oauth/discord/callback?code=c&state=${encodeURIComponent(flow.state)}`, headers: { cookie: flow.cookie } });
|
Google OIDC provider: link-only, sub-keyed, PKCE and nonce
Google is a Heimdall provider behind Bifrost, with no direct upstream.
Scopes openid email profile; the callback verifies the id_token
(RS256 against Google's keys, iss, aud, exp, iat, nonce) and requires
a verified email. The identity key is sub, never the email.
PKCE verifier and nonce are derived from the state jti and Heimdall's
signing key, so nothing is stored between start and callback.
Google never mints an account. mode=link needs a Heimdall access token
for the app and attaches to that account only; sign_in signs in an
identity already linked; an unknown sub is refused, and a sub held by
another account is never moved. The six existing providers still mint
on first login.
Admitted to the bifrost profile only. The Idunn recipe declares the
Google client id and secret file.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 16:10:23 +00:00
|
|
|
return cb.json().accessToken as string;
|
|
|
|
|
}],
|
|
|
|
|
])("refuses a link start carrying %s", async (_name, token) => {
|
|
|
|
|
const { app, store } = await build();
|
|
|
|
|
const bearer = await token(app);
|
|
|
|
|
const before = accountCount(store);
|
|
|
|
|
const start = await startGoogle(app, bearer);
|
|
|
|
|
expect(start.statusCode).toBe(401);
|
|
|
|
|
expect(start.json().error).toBe("invalid_link_session");
|
|
|
|
|
expect(accountCount(store)).toBe(before);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("refuses a first Google login from a subject no account holds, and mints no account for it", async () => {
|
|
|
|
|
// The person who has a Discord account arrives with Google for the first time, signing in rather than linking.
|
|
|
|
|
const { app, store } = await build();
|
|
|
|
|
await signInWithDiscord(app);
|
|
|
|
|
const start = await startGoogle(app, undefined, "sign_in");
|
|
|
|
|
expect(start.statusCode).toBe(201);
|
2026-11-05 12:46:05 +00:00
|
|
|
const startFlow = await launchWithRedirect(app, start.json());
|
|
|
|
|
const url = startFlow.url;
|
Google OIDC provider: link-only, sub-keyed, PKCE and nonce
Google is a Heimdall provider behind Bifrost, with no direct upstream.
Scopes openid email profile; the callback verifies the id_token
(RS256 against Google's keys, iss, aud, exp, iat, nonce) and requires
a verified email. The identity key is sub, never the email.
PKCE verifier and nonce are derived from the state jti and Heimdall's
signing key, so nothing is stored between start and callback.
Google never mints an account. mode=link needs a Heimdall access token
for the app and attaches to that account only; sign_in signs in an
identity already linked; an unknown sub is refused, and a sub held by
another account is never moved. The six existing providers still mint
on first login.
Admitted to the bifrost profile only. The Idunn recipe declares the
Google client id and secret file.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 16:10:23 +00:00
|
|
|
fakeGoogle({ nonceFrom: () => url.searchParams.get("nonce") ?? undefined });
|
|
|
|
|
|
|
|
|
|
const before = accountCount(store);
|
2026-11-05 12:46:05 +00:00
|
|
|
const done = await callbackGoogle(app, startFlow);
|
Google OIDC provider: link-only, sub-keyed, PKCE and nonce
Google is a Heimdall provider behind Bifrost, with no direct upstream.
Scopes openid email profile; the callback verifies the id_token
(RS256 against Google's keys, iss, aud, exp, iat, nonce) and requires
a verified email. The identity key is sub, never the email.
PKCE verifier and nonce are derived from the state jti and Heimdall's
signing key, so nothing is stored between start and callback.
Google never mints an account. mode=link needs a Heimdall access token
for the app and attaches to that account only; sign_in signs in an
identity already linked; an unknown sub is refused, and a sub held by
another account is never moved. The six existing providers still mint
on first login.
Admitted to the bifrost profile only. The Idunn recipe declares the
Google client id and secret file.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 16:10:23 +00:00
|
|
|
|
2026-11-05 10:58:02 +00:00
|
|
|
expect(done.statusCode).toBe(403);
|
|
|
|
|
expect(done.json().error).toBe("identity_not_linked");
|
Google OIDC provider: link-only, sub-keyed, PKCE and nonce
Google is a Heimdall provider behind Bifrost, with no direct upstream.
Scopes openid email profile; the callback verifies the id_token
(RS256 against Google's keys, iss, aud, exp, iat, nonce) and requires
a verified email. The identity key is sub, never the email.
PKCE verifier and nonce are derived from the state jti and Heimdall's
signing key, so nothing is stored between start and callback.
Google never mints an account. mode=link needs a Heimdall access token
for the app and attaches to that account only; sign_in signs in an
identity already linked; an unknown sub is refused, and a sub held by
another account is never moved. The six existing providers still mint
on first login.
Admitted to the bifrost profile only. The Idunn recipe declares the
Google client id and secret file.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 16:10:23 +00:00
|
|
|
expect(accountCount(store)).toBe(before);
|
|
|
|
|
expect(await store.findStoredLinkedIdentity("google", "g-sub-1")).toBeNull();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("never moves or merges: a Google subject already linked to one account cannot be linked to another", async () => {
|
|
|
|
|
const store = new InMemoryStore();
|
|
|
|
|
const first = await build(store, "discord-1");
|
|
|
|
|
const a = await signInWithDiscord(first.app, "discord-1");
|
2026-11-04 17:15:33 +00:00
|
|
|
await linkGoogle(first.app, a.accessToken);
|
Google OIDC provider: link-only, sub-keyed, PKCE and nonce
Google is a Heimdall provider behind Bifrost, with no direct upstream.
Scopes openid email profile; the callback verifies the id_token
(RS256 against Google's keys, iss, aud, exp, iat, nonce) and requires
a verified email. The identity key is sub, never the email.
PKCE verifier and nonce are derived from the state jti and Heimdall's
signing key, so nothing is stored between start and callback.
Google never mints an account. mode=link needs a Heimdall access token
for the app and attaches to that account only; sign_in signs in an
identity already linked; an unknown sub is refused, and a sub held by
another account is never moved. The six existing providers still mint
on first login.
Admitted to the bifrost profile only. The Idunn recipe declares the
Google client id and secret file.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 16:10:23 +00:00
|
|
|
|
|
|
|
|
// A second person, a second account, the same Google subject.
|
|
|
|
|
const second = await build(store, "discord-2");
|
|
|
|
|
const b = await signInWithDiscord(second.app, "discord-2");
|
|
|
|
|
expect(b.accountId).not.toBe(a.accountId);
|
|
|
|
|
const startB = await startGoogle(second.app, b.accessToken);
|
2026-11-05 12:46:05 +00:00
|
|
|
const startBFlow = await launchWithRedirect(second.app, startB.json());
|
|
|
|
|
const urlB = startBFlow.url;
|
Google OIDC provider: link-only, sub-keyed, PKCE and nonce
Google is a Heimdall provider behind Bifrost, with no direct upstream.
Scopes openid email profile; the callback verifies the id_token
(RS256 against Google's keys, iss, aud, exp, iat, nonce) and requires
a verified email. The identity key is sub, never the email.
PKCE verifier and nonce are derived from the state jti and Heimdall's
signing key, so nothing is stored between start and callback.
Google never mints an account. mode=link needs a Heimdall access token
for the app and attaches to that account only; sign_in signs in an
identity already linked; an unknown sub is refused, and a sub held by
another account is never moved. The six existing providers still mint
on first login.
Admitted to the bifrost profile only. The Idunn recipe declares the
Google client id and secret file.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 16:10:23 +00:00
|
|
|
fakeGoogle({ nonceFrom: () => urlB.searchParams.get("nonce") ?? undefined });
|
2026-11-05 12:46:05 +00:00
|
|
|
const done = await callbackGoogle(second.app, startBFlow);
|
Google OIDC provider: link-only, sub-keyed, PKCE and nonce
Google is a Heimdall provider behind Bifrost, with no direct upstream.
Scopes openid email profile; the callback verifies the id_token
(RS256 against Google's keys, iss, aud, exp, iat, nonce) and requires
a verified email. The identity key is sub, never the email.
PKCE verifier and nonce are derived from the state jti and Heimdall's
signing key, so nothing is stored between start and callback.
Google never mints an account. mode=link needs a Heimdall access token
for the app and attaches to that account only; sign_in signs in an
identity already linked; an unknown sub is refused, and a sub held by
another account is never moved. The six existing providers still mint
on first login.
Admitted to the bifrost profile only. The Idunn recipe declares the
Google client id and secret file.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 16:10:23 +00:00
|
|
|
|
2026-11-05 10:58:02 +00:00
|
|
|
expect(done.statusCode).toBe(403);
|
Google OIDC provider: link-only, sub-keyed, PKCE and nonce
Google is a Heimdall provider behind Bifrost, with no direct upstream.
Scopes openid email profile; the callback verifies the id_token
(RS256 against Google's keys, iss, aud, exp, iat, nonce) and requires
a verified email. The identity key is sub, never the email.
PKCE verifier and nonce are derived from the state jti and Heimdall's
signing key, so nothing is stored between start and callback.
Google never mints an account. mode=link needs a Heimdall access token
for the app and attaches to that account only; sign_in signs in an
identity already linked; an unknown sub is refused, and a sub held by
another account is never moved. The six existing providers still mint
on first login.
Admitted to the bifrost profile only. The Idunn recipe declares the
Google client id and secret file.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 16:10:23 +00:00
|
|
|
expect((await store.findStoredLinkedIdentity("google", "g-sub-1"))?.accountId).toBe(a.accountId);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it.each([
|
|
|
|
|
["an unverified email", { email_verified: false }],
|
|
|
|
|
["another client's audience", { aud: "other-client", azp: "other-client" }],
|
|
|
|
|
["a nonce this request did not carry", { nonce: "replayed" }],
|
|
|
|
|
["an expired token", { exp: Math.floor(Date.now() / 1000) - 3600 }],
|
|
|
|
|
])("links nothing when the id_token has %s", async (_name, claims) => {
|
|
|
|
|
const { app, store } = await build();
|
|
|
|
|
const { accessToken } = await signInWithDiscord(app);
|
|
|
|
|
const start = await startGoogle(app, accessToken);
|
2026-11-05 12:46:05 +00:00
|
|
|
const startFlow = await launchWithRedirect(app, start.json());
|
|
|
|
|
const url = startFlow.url;
|
Google OIDC provider: link-only, sub-keyed, PKCE and nonce
Google is a Heimdall provider behind Bifrost, with no direct upstream.
Scopes openid email profile; the callback verifies the id_token
(RS256 against Google's keys, iss, aud, exp, iat, nonce) and requires
a verified email. The identity key is sub, never the email.
PKCE verifier and nonce are derived from the state jti and Heimdall's
signing key, so nothing is stored between start and callback.
Google never mints an account. mode=link needs a Heimdall access token
for the app and attaches to that account only; sign_in signs in an
identity already linked; an unknown sub is refused, and a sub held by
another account is never moved. The six existing providers still mint
on first login.
Admitted to the bifrost profile only. The Idunn recipe declares the
Google client id and secret file.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 16:10:23 +00:00
|
|
|
fakeGoogle({ nonceFrom: () => url.searchParams.get("nonce") ?? undefined, claims });
|
|
|
|
|
|
2026-11-05 12:46:05 +00:00
|
|
|
const done = await callbackGoogle(app, startFlow);
|
Google OIDC provider: link-only, sub-keyed, PKCE and nonce
Google is a Heimdall provider behind Bifrost, with no direct upstream.
Scopes openid email profile; the callback verifies the id_token
(RS256 against Google's keys, iss, aud, exp, iat, nonce) and requires
a verified email. The identity key is sub, never the email.
PKCE verifier and nonce are derived from the state jti and Heimdall's
signing key, so nothing is stored between start and callback.
Google never mints an account. mode=link needs a Heimdall access token
for the app and attaches to that account only; sign_in signs in an
identity already linked; an unknown sub is refused, and a sub held by
another account is never moved. The six existing providers still mint
on first login.
Admitted to the bifrost profile only. The Idunn recipe declares the
Google client id and secret file.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 16:10:23 +00:00
|
|
|
|
|
|
|
|
expect(done.statusCode).toBe(502);
|
|
|
|
|
expect(await store.findStoredLinkedIdentity("google", "g-sub-1")).toBeNull();
|
|
|
|
|
expect(accountCount(store)).toBe(1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("keys the identity by sub: the same email under a different sub is a different, unlinkable identity", async () => {
|
|
|
|
|
const { app, store } = await build();
|
|
|
|
|
const { accessToken, accountId } = await signInWithDiscord(app);
|
2026-11-04 17:15:33 +00:00
|
|
|
await linkGoogle(app, accessToken, { sub: "g-sub-2" });
|
Google OIDC provider: link-only, sub-keyed, PKCE and nonce
Google is a Heimdall provider behind Bifrost, with no direct upstream.
Scopes openid email profile; the callback verifies the id_token
(RS256 against Google's keys, iss, aud, exp, iat, nonce) and requires
a verified email. The identity key is sub, never the email.
PKCE verifier and nonce are derived from the state jti and Heimdall's
signing key, so nothing is stored between start and callback.
Google never mints an account. mode=link needs a Heimdall access token
for the app and attaches to that account only; sign_in signs in an
identity already linked; an unknown sub is refused, and a sub held by
another account is never moved. The six existing providers still mint
on first login.
Admitted to the bifrost profile only. The Idunn recipe declares the
Google client id and secret file.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 16:10:23 +00:00
|
|
|
expect((await store.findStoredLinkedIdentity("google", "g-sub-2"))?.accountId).toBe(accountId);
|
|
|
|
|
expect(await store.findStoredLinkedIdentity("google", "person@example.org")).toBeNull();
|
|
|
|
|
});
|
|
|
|
|
|
2026-11-04 17:15:33 +00:00
|
|
|
describe("link is two-phase", () => {
|
|
|
|
|
it("a victim completing an attacker's link URL attaches the victim's Google sub to no account", async () => {
|
|
|
|
|
const store = new InMemoryStore();
|
|
|
|
|
const attacker = await build(store, "discord-attacker");
|
|
|
|
|
const a = await signInWithDiscord(attacker.app, "discord-attacker");
|
|
|
|
|
const victim = await build(store, "discord-victim");
|
|
|
|
|
const v = await signInWithDiscord(victim.app, "discord-victim");
|
|
|
|
|
|
|
|
|
|
// The attacker begins a link and sends the Google URL to the victim, who finishes it as themselves.
|
|
|
|
|
const done = await linkGoogleCallback(attacker.app, a.accessToken, { sub: "victim-google-sub" });
|
|
|
|
|
expect(done.statusCode).toBe(201);
|
|
|
|
|
expect(await store.findStoredLinkedIdentity("google", "victim-google-sub")).toBeNull();
|
|
|
|
|
|
|
|
|
|
// The victim's app holds the victim's token: Heimdall refuses it, and the link stays unwritten.
|
|
|
|
|
const asVictim = await confirmLink(victim.app, v.accessToken, done.json().pendingLinkId);
|
|
|
|
|
expect(asVictim.statusCode).toBe(410);
|
|
|
|
|
expect(await store.findStoredLinkedIdentity("google", "victim-google-sub")).toBeNull();
|
|
|
|
|
|
|
|
|
|
// No token at all, and a garbage token, are refused the same way.
|
|
|
|
|
expect((await confirmLink(attacker.app, undefined, done.json().pendingLinkId)).statusCode).toBe(401);
|
|
|
|
|
expect((await confirmLink(attacker.app, "garbage", done.json().pendingLinkId)).statusCode).toBe(401);
|
|
|
|
|
expect(await store.findStoredLinkedIdentity("google", "victim-google-sub")).toBeNull();
|
|
|
|
|
|
|
|
|
|
// The refused attempts did not burn the pending link: its initiator can still confirm it.
|
|
|
|
|
expect((await confirmLink(attacker.app, a.accessToken, done.json().pendingLinkId)).statusCode).toBe(201);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("signing in with Google cannot reach a Google sub the attacker's pending link recorded", async () => {
|
|
|
|
|
const { app } = await build();
|
|
|
|
|
const a = await signInWithDiscord(app);
|
|
|
|
|
await linkGoogleCallback(app, a.accessToken, { sub: "victim-google-sub" });
|
|
|
|
|
|
|
|
|
|
const signIn = await startGoogle(app, undefined, "sign_in");
|
2026-11-05 12:46:05 +00:00
|
|
|
const signInFlow = await launchWithRedirect(app, signIn.json());
|
|
|
|
|
const url = signInFlow.url;
|
2026-11-04 17:15:33 +00:00
|
|
|
fakeGoogle({ nonceFrom: () => url.searchParams.get("nonce") ?? undefined, claims: { sub: "victim-google-sub" } });
|
2026-11-05 12:46:05 +00:00
|
|
|
expect((await callbackGoogle(app, signInFlow)).statusCode).toBe(403);
|
2026-11-04 17:15:33 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("a pending link is single-use", async () => {
|
|
|
|
|
const { app } = await build();
|
|
|
|
|
const a = await signInWithDiscord(app);
|
|
|
|
|
const done = await linkGoogleCallback(app, a.accessToken);
|
|
|
|
|
expect((await confirmLink(app, a.accessToken, done.json().pendingLinkId)).statusCode).toBe(201);
|
|
|
|
|
expect((await confirmLink(app, a.accessToken, done.json().pendingLinkId)).statusCode).toBe(410);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("a pending link expires after the completion TTL", async () => {
|
|
|
|
|
const { app, store } = await build();
|
|
|
|
|
const a = await signInWithDiscord(app);
|
|
|
|
|
const done = await linkGoogleCallback(app, a.accessToken);
|
|
|
|
|
const ttlMs = Date.parse(done.json().expiresAt) - Date.now();
|
|
|
|
|
expect(ttlMs).toBeGreaterThan(0);
|
|
|
|
|
expect(ttlMs).toBeLessThanOrEqual(300_000);
|
|
|
|
|
|
|
|
|
|
vi.useFakeTimers({ toFake: ["Date"], now: Date.now() + 301_000 });
|
|
|
|
|
try {
|
|
|
|
|
expect((await confirmLink(app, a.accessToken, done.json().pendingLinkId)).statusCode).toBe(410);
|
|
|
|
|
} finally {
|
|
|
|
|
vi.useRealTimers();
|
|
|
|
|
}
|
|
|
|
|
expect(await store.findStoredLinkedIdentity("google", "g-sub-1")).toBeNull();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("a pending link is bound to its app: another app's session cannot confirm it", async () => {
|
|
|
|
|
const { app, store } = await build();
|
|
|
|
|
const a = await signInWithDiscord(app);
|
|
|
|
|
const done = await linkGoogleCallback(app, a.accessToken);
|
|
|
|
|
const other = await app.inject({
|
|
|
|
|
method: "POST",
|
|
|
|
|
url: "/v1/oauth/discord/start",
|
|
|
|
|
payload: { appSlug: "repixelizer", mode: "sign_in", returnTo: "https://repixelizer.gamecult.org/app/" },
|
|
|
|
|
});
|
2026-11-05 12:46:05 +00:00
|
|
|
const flow = await launchFlow(app, other.json());
|
|
|
|
|
const cb = await app.inject({ method: "GET", url: `/v1/oauth/discord/callback?code=c&state=${encodeURIComponent(flow.state)}`, headers: { cookie: flow.cookie } });
|
2026-11-04 17:15:33 +00:00
|
|
|
const res = await app.inject({
|
|
|
|
|
method: "POST",
|
|
|
|
|
url: "/v1/apps/repixelizer/links/confirm",
|
|
|
|
|
headers: { authorization: `Bearer ${cb.json().accessToken}` },
|
|
|
|
|
payload: { pendingLinkId: done.json().pendingLinkId },
|
|
|
|
|
});
|
|
|
|
|
expect(res.statusCode).toBe(410);
|
|
|
|
|
expect(await store.findStoredLinkedIdentity("google", "g-sub-1")).toBeNull();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("refuses at confirm a sub another account linked in between, and never moves it", async () => {
|
|
|
|
|
const store = new InMemoryStore();
|
|
|
|
|
const first = await build(store, "discord-1");
|
|
|
|
|
const a = await signInWithDiscord(first.app, "discord-1");
|
|
|
|
|
const second = await build(store, "discord-2");
|
|
|
|
|
const b = await signInWithDiscord(second.app, "discord-2");
|
|
|
|
|
|
|
|
|
|
const pendingA = await linkGoogleCallback(first.app, a.accessToken);
|
|
|
|
|
const pendingB = await linkGoogleCallback(second.app, b.accessToken);
|
|
|
|
|
expect((await confirmLink(first.app, a.accessToken, pendingA.json().pendingLinkId)).statusCode).toBe(201);
|
|
|
|
|
const lost = await confirmLink(second.app, b.accessToken, pendingB.json().pendingLinkId);
|
|
|
|
|
|
|
|
|
|
expect(lost.statusCode).toBe(409);
|
|
|
|
|
expect((await store.findStoredLinkedIdentity("google", "g-sub-1"))?.accountId).toBe(a.accountId);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("entitlements at the callback", () => {
|
|
|
|
|
it("a Discord-origin member returning through Google keeps entitlement.app_access under the start-time policy", async () => {
|
|
|
|
|
const { app } = await build();
|
|
|
|
|
const member = await signInWithDiscord(app);
|
|
|
|
|
await linkGoogle(app, member.accessToken);
|
|
|
|
|
|
|
|
|
|
const start = await app.inject({
|
|
|
|
|
method: "POST",
|
|
|
|
|
url: "/v1/oauth/google/start",
|
|
|
|
|
headers: { "x-heimdall-app-secret": "bifrost-secret" },
|
|
|
|
|
payload: {
|
|
|
|
|
appSlug: "bifrost",
|
|
|
|
|
mode: "sign_in",
|
|
|
|
|
returnTo: "https://bifrost.gamecult.org/auth/heimdall/complete",
|
|
|
|
|
entitlementPolicy: DISCORD_POLICY,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
expect(start.statusCode).toBe(201);
|
2026-11-05 12:46:05 +00:00
|
|
|
const startFlow = await launchWithRedirect(app, start.json());
|
|
|
|
|
const url = startFlow.url;
|
2026-11-04 17:15:33 +00:00
|
|
|
fakeGoogle({ nonceFrom: () => url.searchParams.get("nonce") ?? undefined });
|
2026-11-05 12:46:05 +00:00
|
|
|
const done = await callbackGoogle(app, startFlow);
|
2026-11-04 17:15:33 +00:00
|
|
|
|
|
|
|
|
expect(done.statusCode).toBe(201);
|
|
|
|
|
expect(done.json().claimSet.facts).toContain("entitlement.app_access");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("without a start-time policy the other identities grant nothing", async () => {
|
|
|
|
|
const { app } = await build();
|
|
|
|
|
const member = await signInWithDiscord(app);
|
|
|
|
|
await linkGoogle(app, member.accessToken);
|
|
|
|
|
|
|
|
|
|
const start = await startGoogle(app, undefined, "sign_in");
|
2026-11-05 12:46:05 +00:00
|
|
|
const startFlow = await launchWithRedirect(app, start.json());
|
|
|
|
|
const url = startFlow.url;
|
2026-11-04 17:15:33 +00:00
|
|
|
fakeGoogle({ nonceFrom: () => url.searchParams.get("nonce") ?? undefined });
|
2026-11-05 12:46:05 +00:00
|
|
|
const done = await callbackGoogle(app, startFlow);
|
2026-11-04 17:15:33 +00:00
|
|
|
|
|
|
|
|
expect(done.statusCode).toBe(201);
|
|
|
|
|
expect(done.json().claimSet.facts).not.toContain("entitlement.app_access");
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
Google OIDC provider: link-only, sub-keyed, PKCE and nonce
Google is a Heimdall provider behind Bifrost, with no direct upstream.
Scopes openid email profile; the callback verifies the id_token
(RS256 against Google's keys, iss, aud, exp, iat, nonce) and requires
a verified email. The identity key is sub, never the email.
PKCE verifier and nonce are derived from the state jti and Heimdall's
signing key, so nothing is stored between start and callback.
Google never mints an account. mode=link needs a Heimdall access token
for the app and attaches to that account only; sign_in signs in an
identity already linked; an unknown sub is refused, and a sub held by
another account is never moved. The six existing providers still mint
on first login.
Admitted to the bifrost profile only. The Idunn recipe declares the
Google client id and secret file.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-04 16:10:23 +00:00
|
|
|
it("leaves the six existing providers minting on first login", async () => {
|
|
|
|
|
const { app, store } = await build();
|
|
|
|
|
await signInWithDiscord(app);
|
|
|
|
|
expect(accountCount(store)).toBe(1);
|
|
|
|
|
});
|
|
|
|
|
});
|