Accounts 1.1.0: moving Events' users in, people.* requests, new recovery codes
POST /api/accounts/v1/requests (crosscode.identity-request/1): import.* keeps each person's Events id as sub, password hash and authenticator secret (sealed in transit), consent, notices and lockout; linked when the email already has an account. people.* for product admins, limited to people the product knows. New recovery codes after the first 2FA sign-in for people moved in with 2FA. screen=signup. auth-client: request(), seal(), signUp. 8 new tests (47 in all). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -18,7 +18,7 @@ import type { Account, PendingSignIn } from '../generated/prisma/client.js';
|
||||
import {
|
||||
codeMatches, emailProblem, isLocked, maskedTarget, normaliseEmail, normalisePhone, sendCode, setNewsConsent, signInFailed, signInSucceeded, CODE_MINUTES,
|
||||
} from '../accounts/service.js';
|
||||
import { accessAfterTwoFactor, confirmTwoFactorSetup, startTwoFactorSetup, useRecoveryCode, verifyTotp } from '../accounts/twoFactor.js';
|
||||
import { accessAfterTwoFactor, confirmTwoFactorSetup, replaceRecoveryCodes, startTwoFactorSetup, useRecoveryCode, verifyTotp } from '../accounts/twoFactor.js';
|
||||
import { loginEvent } from '../lib/audit.js';
|
||||
import { dummyHash, hashPassword, needsRehash, passwordProblem, verifyPassword } from '../lib/passwords.js';
|
||||
import { accept as acceptNotice, currentNotice } from '../privacy/notice.js';
|
||||
@@ -94,6 +94,8 @@ export async function describe(ctx: Ctx, details: Details, pending: PendingSignI
|
||||
signupEnabled: await ctx.settings.bool('signup_enabled', true),
|
||||
mfaRequested: mfaRequested(details),
|
||||
loginHint: typeof details.params.login_hint === 'string' ? details.params.login_hint : null,
|
||||
// 1.1.0: a product's "Create an account" link opens on sign-up (`screen=signup`).
|
||||
screen: details.params.screen === 'signup' ? 'signup' : 'signin',
|
||||
};
|
||||
if (stage === 'password' || stage === 'done') return base;
|
||||
const a = await account(ctx, pending);
|
||||
@@ -209,7 +211,16 @@ export async function twoFactor(ctx: Ctx, pending: PendingSignIn, input: { code?
|
||||
} else {
|
||||
await checkCode(ctx, pending, input.code);
|
||||
}
|
||||
return save(ctx, pending, { amr: accessAfterTwoFactor(pending.amr), codeHash: null, codeSentAt: null, attempts: 0 });
|
||||
const next = await save(ctx, pending, { amr: accessAfterTwoFactor(pending.amr), codeHash: null, codeSentAt: null, attempts: 0 });
|
||||
// Moved in from a product without its recovery codes (EVENTS-3.0-PLAN decision 2): new ones now,
|
||||
// shown once on the next page.
|
||||
if (a.recoveryCodesNeeded) {
|
||||
const recoveryCodes = await replaceRecoveryCodes(ctx, a.id);
|
||||
await ctx.db.account.update({ where: { id: a.id }, data: { recoveryCodesNeeded: false } });
|
||||
await loginEvent(ctx, a.id, 'recovery_codes_replaced', req, { detail: { by: 'move' } });
|
||||
return { pending: next, recoveryCodes };
|
||||
}
|
||||
return { pending: next, recoveryCodes: null as string[] | null };
|
||||
}
|
||||
|
||||
export async function setupStart(ctx: Ctx, pending: PendingSignIn, method: unknown) {
|
||||
|
||||
@@ -15,6 +15,7 @@ import { normaliseEmail } from '../accounts/service.js';
|
||||
import { SELF_CALLBACK, SELF_CLIENT_ID } from '../oidc/clients.js';
|
||||
import { issuerOf } from '../oidc/provider.js';
|
||||
import * as flow from './flow.js';
|
||||
import { signedInThrough } from '../links/requests.js';
|
||||
|
||||
const RESET_MINUTES = 60;
|
||||
|
||||
@@ -39,7 +40,9 @@ export function interactionRouter(ctx: Ctx, provider: Provider) {
|
||||
const state = await flow.describe(ctx, details, pending);
|
||||
if (state.stage !== 'done') return { ...state, ...extra };
|
||||
const redirectTo = await provider.interactionResult(req, res, flow.loginResult(pending), { mergeWithLastSubmission: false });
|
||||
await loginEvent(ctx, pending.accountId, 'sign_in', req, { clientId: String(details.params.client_id ?? '') || null, detail: { amr: pending.amr } });
|
||||
const clientId = String(details.params.client_id ?? '') || null;
|
||||
await loginEvent(ctx, pending.accountId, 'sign_in', req, { clientId, detail: { amr: pending.amr } });
|
||||
await signedInThrough(ctx, clientId, pending.accountId);
|
||||
await ctx.db.pendingSignIn.deleteMany({ where: { uid: pending.uid } });
|
||||
return { stage: 'done', redirectTo, ...extra };
|
||||
}
|
||||
@@ -61,7 +64,10 @@ export function interactionRouter(ctx: Ctx, provider: Provider) {
|
||||
r.post('/interaction/:uid/password', limit(20), step(async (req, _res, p) => { await flow.submitPassword(ctx, p, req.body ?? {}, req); }));
|
||||
r.post('/interaction/:uid/signup', limit(10, 60), step(async (req, _res, p) => { await flow.signUp(ctx, p, req.body ?? {}); }));
|
||||
r.post('/interaction/:uid/verify-email', limit(20), step(async (req, _res, p) => { await flow.verifyEmail(ctx, p, req.body?.code); }));
|
||||
r.post('/interaction/:uid/two-factor', limit(20), step(async (req, _res, p) => { await flow.twoFactor(ctx, p, req.body ?? {}, req); }));
|
||||
r.post('/interaction/:uid/two-factor', limit(20), step(async (req, _res, p) => {
|
||||
const { recoveryCodes } = await flow.twoFactor(ctx, p, req.body ?? {}, req);
|
||||
return recoveryCodes ? { recoveryCodes } : undefined;
|
||||
}));
|
||||
r.post('/interaction/:uid/resend', limit(10), step(async (_req, _res, p) => { await flow.resendCode(ctx, p); }));
|
||||
r.post('/interaction/:uid/two-factor/setup', limit(10), handle(async (req, res) => {
|
||||
const { pending } = await load(req, res);
|
||||
@@ -104,12 +110,14 @@ export function interactionRouter(ctx: Ctx, provider: Provider) {
|
||||
if (!row || row.purpose !== 'reset_password' || row.usedAt || row.expiresAt < ctx.now()) throw new ApiError(400, 'BAD_TOKEN', 'This link has expired or was already used. Ask for a new one.');
|
||||
const problem = passwordProblem(req.body?.password);
|
||||
if (problem) throw new ApiError(400, 'VALIDATION', problem, { password: problem });
|
||||
await ctx.db.account.update({ where: { id: row.accountId }, data: { passwordHash: await hashPassword(String(req.body.password)), failedSignIns: 0, lockedUntil: null } });
|
||||
// Setting a password from an emailed link also proves the address.
|
||||
await ctx.db.account.update({ where: { id: row.accountId }, data: { passwordHash: await hashPassword(String(req.body.password)), failedSignIns: 0, lockedUntil: null, emailVerified: true } });
|
||||
await ctx.db.emailToken.update({ where: { id: row.id }, data: { usedAt: ctx.now() } });
|
||||
await ctx.db.emailToken.deleteMany({ where: { accountId: row.accountId, purpose: 'reset_password', usedAt: null } });
|
||||
await loginEvent(ctx, row.accountId, 'password_reset', req);
|
||||
// Back to the sign-in it started from, if this browser still has it (§5).
|
||||
return { reset: true, uid: row.interactionUid };
|
||||
// A "set your password" link a product asked for (people.ensure) ends on that product, which signs them in.
|
||||
return { reset: true, uid: row.interactionUid, returnUrl: row.returnUrl ?? null };
|
||||
}));
|
||||
|
||||
return r;
|
||||
|
||||
Reference in New Issue
Block a user