Heimdall: an app's invite grant lets a first Google sign-in mint an account, carried only in the signed state

Bifrost's profile may grant; its authenticated sign_in start may carry firstLoginGrant invite.
The callback mints only when no account holds the identity; otherwise identity_not_linked stands.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-29 20:45:03 +01:00
commit 4d827c7bd4
3 changed files with 58 additions and 2 deletions

View file

@ -56,6 +56,14 @@ export interface AppProfile {
* state. Absent for apps whose browser starts are public by design.
*/
startRequiresAppCaller?: boolean;
/**
* The app decides who is invited. Its authenticated sign_in start may carry
* `firstLoginGrant: "invite"`, which lets a provider that otherwise requires a
* link (Google) mint the account for an identity no account holds. The grant
* never attaches an identity to an existing account. Absent for every app that
* may not grant.
*/
mayGrantFirstLogin?: true;
/**
* The app may assert, with its secret and a member's live bearer, that the
* member just passed a factor the app itself checked (POST
@ -170,6 +178,7 @@ const bifrostProfile: AppProfile = {
managedConnectionProviders: [],
allowedReturnOrigins: ["https://bifrost.gamecult.org"],
startRequiresAppCaller: true,
mayGrantFirstLogin: true,
stepUpAssertion: { methods: ["bifrost-passkey", "bifrost-email-code"] },
// A passkey proves the account; an email code proves an inbox, which is one login's worth at most.
identityMoves: { accountProofMethods: ["bifrost-passkey"] },

View file

@ -103,6 +103,7 @@ function buildOAuthStateToken(options: {
entitlementPolicy: OAuthEntitlementPolicy | undefined;
requestedScopes: string[] | undefined;
stepUp: boolean | undefined;
firstLoginGrant: "invite" | undefined;
keys: ReturnType<typeof createRuntimeKeyMaterial>;
}): { token: string; jti: string; expiresAt: string } {
const issuedAt = nowEpochSeconds();
@ -126,6 +127,7 @@ function buildOAuthStateToken(options: {
entitlement_policy: options.entitlementPolicy ?? null,
requested_scopes: options.requestedScopes ?? null,
step_up: options.stepUp ? true : null,
first_login_grant: options.firstLoginGrant ?? null,
};
const token = signJwt(payload as unknown as Record<string, unknown>, options.keys);
@ -190,7 +192,8 @@ function parseOAuthStatePayload(payload: Record<string, unknown>): OAuthStatePay
!entitlementPolicyIsValid ||
!(payload.initiator_account_id === undefined || payload.initiator_account_id === null || typeof payload.initiator_account_id === "string") ||
!requestedScopesAreValid ||
!(payload.step_up === undefined || payload.step_up === null || payload.step_up === true)
!(payload.step_up === undefined || payload.step_up === null || payload.step_up === true) ||
!(payload.first_login_grant === undefined || payload.first_login_grant === null || (payload.first_login_grant === "invite" && payload.mode === "sign_in"))
) {
return null;
}
@ -211,6 +214,9 @@ function parseOAuthStatePayload(payload: Record<string, unknown>): OAuthStatePay
if (!normalizedPayload.step_up) {
normalizedPayload.step_up = null;
}
if (!normalizedPayload.first_login_grant) {
normalizedPayload.first_login_grant = null;
}
return normalizedPayload;
}
@ -827,6 +833,23 @@ export async function startOAuthFlow(
return { statusCode: 401, body: { error: "app_auth_required" } };
}
// A grant is the app's word that it invited this person: only the authenticated app caller of a profile that may grant, and only on sign_in.
// After the app-secret gate, so a caller without the secret learns nothing about what a grant needs.
if (input.firstLoginGrant !== undefined) {
const grantAllowed =
input.firstLoginGrant === "invite" &&
input.mode === "sign_in" &&
isAppCaller(caller) &&
caller.appSlug === profile.slug &&
profile.mayGrantFirstLogin === true;
if (!grantAllowed) {
return {
statusCode: 400,
body: { error: "first_login_grant_refused", detail: "firstLoginGrant needs the app's own sign_in start, from an app that may grant." },
};
}
}
// After the app-secret gate, so a caller without the secret learns nothing about what a target must look like.
if (input.connection !== undefined && !isUsableConnectionTarget(input.connection.targetId)) {
return {
@ -865,6 +888,7 @@ export async function startOAuthFlow(
entitlementPolicy,
requestedScopes: input.requestedScopes,
stepUp: input.stepUp,
firstLoginGrant: input.firstLoginGrant,
keys,
});
return {
@ -1509,6 +1533,7 @@ export async function buildApp(options: BuildAppOptions = {}): Promise<FastifyIn
},
initiatingAccountId: { type: "string", minLength: 1 },
stepUp: { type: "boolean" },
firstLoginGrant: { type: "string", enum: ["invite"] },
handoff: {
type: "object",
required: ["kind"],
@ -2013,6 +2038,7 @@ export async function buildApp(options: BuildAppOptions = {}): Promise<FastifyIn
});
const nowIso = new Date().toISOString();
let account = await store.findAccountByLinkedIdentity(identity.provider, identity.providerUserId);
let mintedByGrant = false;
// One person, one account. A link attaches the provider identity to the
// account the signed state names, and to no other: an identity another
// account holds is a refusal, never a quiet sign-in as that account or a
@ -2033,7 +2059,11 @@ export async function buildApp(options: BuildAppOptions = {}): Promise<FastifyIn
}
}
} else if (!account && callbackDescriptor.firstLogin === "requires_link") {
throw new LinkRefusal("identity_not_linked", "No account holds this identity; sign in another way and link it first.");
// Only the app's signed invite mints; with an account already holding the identity this branch is never reached.
if (statePayload.first_login_grant !== "invite") {
throw new LinkRefusal("identity_not_linked", "No account holds this identity; sign in another way and link it first.");
}
mintedByGrant = true;
}
const buildLinkedIdentityInput = (accountId: string): UpsertLinkedIdentityInput => {
@ -2142,6 +2172,15 @@ export async function buildApp(options: BuildAppOptions = {}): Promise<FastifyIn
createAccountInput.primaryEmail = identity.primaryEmail;
}
account = await store.createAccount(createAccountInput);
if (mintedByGrant) {
await store.createAuditEvent({
accountId: account.id,
appSlug: statePayload.app_slug,
eventType: "account_minted_by_grant",
eventPayloadJson: { provider: identity.provider, appSlug: statePayload.app_slug },
createdAt: nowIso,
});
}
} else {
const accountUpdates: { displayName?: string; primaryEmail?: string } = {};
const displayName = identity.displayName ?? identity.username;

View file

@ -80,6 +80,12 @@ export interface OAuthStartRequest {
entitlementPolicy?: OAuthEntitlementPolicy;
/** Sign_in only, app-authenticated: ask the provider for a real re-authentication. Off when absent. */
stepUp?: boolean;
/**
* Sign_in only, from an app whose profile may grant (`mayGrantFirstLogin`): the app has
* invited this person, so a provider that otherwise requires a link may mint the
* first account. Carried in the signed state; never read from a browser.
*/
firstLoginGrant?: "invite";
}
export interface OAuthStatePayload {
@ -103,6 +109,8 @@ export interface OAuthStatePayload {
requested_scopes: string[] | null;
/** The start's step-up request. Signed, so the launch hop reads it here and nowhere else; null when not asked. */
step_up: true | null;
/** The start's first-login grant. Signed, so only the app's own start can set it; null when not granted. */
first_login_grant: "invite" | null;
}
export interface LinkedIdentityInput {