Opt-in sign_in step-up: app-authenticated, signed-state flag, provider re-auth parameters, typed refusal

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Heimdall 2026-11-12 23:10:27 +00:00
commit 7a69d7567b
7 changed files with 233 additions and 2 deletions

View file

@ -99,6 +99,7 @@ function buildOAuthStateToken(options: {
handoff: OAuthHandoff;
entitlementPolicy: OAuthEntitlementPolicy | undefined;
requestedScopes: string[] | undefined;
stepUp: boolean | undefined;
keys: ReturnType<typeof createRuntimeKeyMaterial>;
}): { token: string; jti: string; expiresAt: string } {
const issuedAt = nowEpochSeconds();
@ -121,6 +122,7 @@ function buildOAuthStateToken(options: {
handoff: options.handoff,
entitlement_policy: options.entitlementPolicy ?? null,
requested_scopes: options.requestedScopes ?? null,
step_up: options.stepUp ? true : null,
};
const token = signJwt(payload as unknown as Record<string, unknown>, options.keys);
@ -184,7 +186,8 @@ function parseOAuthStatePayload(payload: Record<string, unknown>): OAuthStatePay
!handoffIsValid ||
!entitlementPolicyIsValid ||
!(payload.initiator_account_id === undefined || payload.initiator_account_id === null || typeof payload.initiator_account_id === "string") ||
!requestedScopesAreValid
!requestedScopesAreValid ||
!(payload.step_up === undefined || payload.step_up === null || payload.step_up === true)
) {
return null;
}
@ -202,6 +205,9 @@ function parseOAuthStatePayload(payload: Record<string, unknown>): OAuthStatePay
if (!normalizedPayload.requested_scopes) {
normalizedPayload.requested_scopes = null;
}
if (!normalizedPayload.step_up) {
normalizedPayload.step_up = null;
}
return normalizedPayload;
}
@ -702,6 +708,10 @@ export async function startOAuthFlow(
};
}
if (input.stepUp && input.mode !== "sign_in") {
return { statusCode: 400, body: { error: "step_up_only_in_sign_in", detail: "stepUp applies to sign_in starts only." } };
}
const descriptor = providerCatalog[provider];
// A provider that never mints an account attaches an identity only through
// a bound link: the user's own access token, since the app's word does not
@ -744,6 +754,19 @@ export async function startOAuthFlow(
return { statusCode: 400, body: { error: "initiator_only_in_link_mode" } };
}
// A step-up is app-backend authority, like a connection target: it is
// asked for by the authenticated app, never by a browser, and only on
// sign_in. A provider with no re-authentication parameter is refused here,
// at start, so no step-up ever reaches the person without its step.
if (input.stepUp) {
if (!isAppCaller(caller) || caller.appSlug !== profile.slug) {
return { statusCode: 401, body: { error: "app_auth_required" } };
}
if (!providerCatalog[provider].stepUpAuthorizationParams) {
return { statusCode: 400, body: { error: "step_up_not_supported", provider, detail: `${providerCatalog[provider].displayName} has no re-authentication parameter.` } };
}
}
if (
input.requestedScopes !== undefined &&
(input.requestedScopes.length > maxRequestedScopes || input.requestedScopes.some((scope) => scope.length > maxRequestedScopeChars))
@ -838,6 +861,7 @@ export async function startOAuthFlow(
handoff,
entitlementPolicy,
requestedScopes: input.requestedScopes,
stepUp: input.stepUp,
keys,
});
return {
@ -1175,6 +1199,7 @@ export async function buildApp(options: BuildAppOptions = {}): Promise<FastifyIn
},
},
initiatingAccountId: { type: "string", minLength: 1 },
stepUp: { type: "boolean" },
handoff: {
type: "object",
required: ["kind"],
@ -1362,6 +1387,7 @@ export async function buildApp(options: BuildAppOptions = {}): Promise<FastifyIn
redirectUri: oauthCallbackUrl(config, state.provider),
state: rawState,
requestedScopes: state.requested_scopes ?? undefined,
...(state.step_up ? { stepUp: true } : {}),
...(oidc ? { oidc: { nonce: oidc.nonce, codeChallenge: oidc.codeChallenge } } : {}),
}),
302
@ -1935,6 +1961,7 @@ export async function buildApp(options: BuildAppOptions = {}): Promise<FastifyIn
entitlements: entitlementEvaluation,
returnTo: statePayload.return_to,
connection: statePayload.connection,
...(statePayload.step_up ? { stepUpRequested: true } : {}),
...(consentReceiptId ? { consentReceiptId } : {}),
...delivered,
};

View file

@ -78,6 +78,8 @@ export interface OAuthStartRequest {
handoff?: OAuthHandoff;
requestedScopes?: string[];
entitlementPolicy?: OAuthEntitlementPolicy;
/** Sign_in only, app-authenticated: ask the provider for a real re-authentication. Off when absent. */
stepUp?: boolean;
}
export interface OAuthStatePayload {
@ -99,6 +101,8 @@ export interface OAuthStatePayload {
entitlement_policy: OAuthEntitlementPolicy | null;
/** The start's requestedScopes, carried so the launch hop can build the provider URL from the signed state alone. */
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;
}
export interface LinkedIdentityInput {

View file

@ -22,6 +22,12 @@ export interface ProviderDescriptor {
clientIdEnv: string;
clientSecretEnv: string;
additionalAuthorizationParams?: Record<string, string>;
/**
* The authorize parameters that make this provider ask the person again
* (a sign_in step-up). Absent means the provider has no such parameter, and
* a step-up start for it is refused rather than passed without the step.
*/
stepUpAuthorizationParams?: Record<string, string>;
/**
* What a callback does with a (provider, subject) no account holds yet.
* `mints_account` is every provider's behaviour before Google: the first
@ -92,6 +98,8 @@ export const providerCatalog: Record<Provider, ProviderDescriptor> = {
firstLogin: "mints_account",
clientIdEnv: "GC_ACCESS_PROVIDER_TWITCH_CLIENT_ID",
clientSecretEnv: "GC_ACCESS_PROVIDER_TWITCH_CLIENT_SECRET_FILE",
// Twitch force_verify re-shows the authorize page; it is not a password prompt.
stepUpAuthorizationParams: { force_verify: "true" },
},
youtube: {
key: "youtube",
@ -116,6 +124,8 @@ export const providerCatalog: Record<Provider, ProviderDescriptor> = {
include_granted_scopes: "true",
prompt: "consent",
},
// login forces re-authentication; consent is kept because this provider always asks for it.
stepUpAuthorizationParams: { prompt: "login consent" },
},
spotify: {
key: "spotify",
@ -150,6 +160,7 @@ export const providerCatalog: Record<Provider, ProviderDescriptor> = {
clientIdEnv: "GC_ACCESS_PROVIDER_GOOGLE_CLIENT_ID",
clientSecretEnv: "GC_ACCESS_PROVIDER_GOOGLE_CLIENT_SECRET_FILE",
additionalAuthorizationParams: { prompt: "select_account" },
stepUpAuthorizationParams: { prompt: "login" },
},
joystick: {
key: "joystick",
@ -180,6 +191,8 @@ export function buildAuthorizationUrl(options: {
requestedScopes: string[] | undefined;
/** For an OIDC provider: the nonce, and the S256 challenge of the PKCE verifier. */
oidc?: { nonce: string; codeChallenge: string };
/** Ask the provider for a real re-authentication. Throws for a provider with no such parameter: never a silent pass. */
stepUp?: boolean;
}): string {
const descriptor = providerCatalog[options.provider];
const url = new URL(descriptor.authorizationEndpoint);
@ -203,6 +216,15 @@ export function buildAuthorizationUrl(options: {
url.searchParams.set(key, value);
}
if (options.stepUp) {
if (!descriptor.stepUpAuthorizationParams) {
throw new Error(`${descriptor.displayName} has no re-authentication parameter: a step-up cannot be honoured.`);
}
for (const [key, value] of Object.entries(descriptor.stepUpAuthorizationParams)) {
url.searchParams.set(key, value);
}
}
return url.toString();
}