From 4d827c7bd4de14d0c08efffbe8ed2f93b9a26186 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 29 Nov 2026 20:45:03 +0100 Subject: [PATCH] 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 --- src/app-profiles.ts | 9 +++++++++ src/app.ts | 43 +++++++++++++++++++++++++++++++++++++++++-- src/contracts.ts | 8 ++++++++ 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/app-profiles.ts b/src/app-profiles.ts index a77f76a..b388ce2 100644 --- a/src/app-profiles.ts +++ b/src/app-profiles.ts @@ -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"] }, diff --git a/src/app.ts b/src/app.ts index b3c7755..3ef3212 100644 --- a/src/app.ts +++ b/src/app.ts @@ -103,6 +103,7 @@ function buildOAuthStateToken(options: { entitlementPolicy: OAuthEntitlementPolicy | undefined; requestedScopes: string[] | undefined; stepUp: boolean | undefined; + firstLoginGrant: "invite" | undefined; keys: ReturnType; }): { 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, options.keys); @@ -190,7 +192,8 @@ function parseOAuthStatePayload(payload: Record): 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): 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 { @@ -2142,6 +2172,15 @@ export async function buildApp(options: BuildAppOptions = {}): Promise