diff --git a/docs/service-contract.md b/docs/service-contract.md index f14602d..2412fbb 100644 --- a/docs/service-contract.md +++ b/docs/service-contract.md @@ -263,10 +263,10 @@ person an app has invited (see the invite grant below). `LinkRefusal` and the code `identity_not_linked` or `link_initiator_mismatch`. - Invite grant. An app whose profile sets `mayGrantFirstLogin` (only `bifrost`) may start `mode: "sign_in"` with `firstLoginGrant: "invite"`, saying it has - invited this person. The start must carry that app's own secret; any other - caller, any other app, any other mode, or any other value is - `400 first_login_grant_refused` (a Bifrost start without the secret is still - `401 app_auth_required`). The grant rides only in the signed state + invited this person. The start must carry that app's own secret: any other + caller, including a public start, is `401 app_auth_required`. An app whose + profile may not grant, any other mode, or any other value is + `400 first_login_grant_refused`. The grant rides only in the signed state (`first_login_grant`), so nothing a browser sends can set it, and the state stays single-use. At the callback a `sub` no account holds, under a grant, mints one account with that identity linked, and audits diff --git a/src/app.ts b/src/app.ts index 003636e..5e327cf 100644 --- a/src/app.ts +++ b/src/app.ts @@ -829,19 +829,14 @@ export async function startOAuthFlow( if (options?.trustedBrowserAttemptId && handoff.kind === "browser_completion") { handoff = { kind: "browser_completion", attemptId: options.trustedBrowserAttemptId }; } - if ((entitlementPolicy || profile.startRequiresAppCaller || (input.mode === "connect" && input.connection !== undefined)) && (!isAppCaller(caller) || caller.appSlug !== profile.slug)) { + if ((entitlementPolicy || profile.startRequiresAppCaller || input.firstLoginGrant !== undefined || (input.mode === "connect" && input.connection !== undefined)) && (!isAppCaller(caller) || caller.appSlug !== profile.slug)) { 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. + // A grant is the app's word that it invited this person: the app-caller gate above has already admitted only the profile's own app, + // and a caller without the secret learns nothing about what a grant needs. Here: only a profile that may grant, only on sign_in. if (input.firstLoginGrant !== undefined) { - const grantAllowed = - input.firstLoginGrant === "invite" && - input.mode === "sign_in" && - profile.mayGrantFirstLogin === true && - isAppCaller(caller) && caller.appSlug === profile.slug; - if (!grantAllowed) { + if (!(input.firstLoginGrant === "invite" && input.mode === "sign_in" && profile.mayGrantFirstLogin === true)) { 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." }, diff --git a/tests/google-link.test.ts b/tests/google-link.test.ts index fc26db4..1e917c3 100644 --- a/tests/google-link.test.ts +++ b/tests/google-link.test.ts @@ -608,13 +608,13 @@ describe("an invite grant lets Bifrost's sign-in start mint the first Google acc 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. + // An app that does not require a caller is still refused: a grant is a start only the app itself may make. 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([open.statusCode, open.json().error]).toEqual([401, "app_auth_required"]); expect(accountCount(store)).toBe(0); });