Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

feat(api/auth): add identifier-first sign-in

Hampusf78568aed829
diff --git a/fluxer_api/src/api/auth/AccountEntryController.ts b/fluxer_api/src/api/auth/AccountEntryController.ts
new file mode 100644
index 000000000..e591052ba
--- /dev/null
+++ b/fluxer_api/src/api/auth/AccountEntryController.ts
@@ -0,0 +1,219 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+import {usesUsernameSignIn} from '@app/api/instance/AccountIdentityModeCache';
+import {
+ createCaptchaChallenge,
+ EscalatingCaptchaMiddleware,
+ verifyCaptchaToken,
+} from '@app/api/middleware/CaptchaMiddleware';
+import {LocalAuthMiddleware} from '@app/api/middleware/LocalAuthMiddleware';
+import {getTrustedForwardedClientIp, RateLimitMiddleware} from '@app/api/middleware/RateLimitMiddleware';
+import {OpenAPI} from '@app/api/middleware/ResponseTypeMiddleware';
+import {RateLimitConfigs} from '@app/api/RateLimitConfig';
+import type {HonoApp, HonoEnv} from '@app/api/types/HonoEnv';
+import {tierContextFrom} from '@app/api/user/AccountTier';
+import {isJsonRecord} from '@app/api/utils/JsonBoundaryUtils';
+import {requireRequestClientIp} from '@app/api/utils/RequestClientIp';
+import {Validator} from '@app/api/Validator';
+import {ValidationErrorCodes} from '@fluxer/constants/src/ValidationErrorCodes';
+import {InputValidationError} from '@fluxer/errors/src/domains/core/InputValidationError';
+import {MissingAccessError} from '@fluxer/errors/src/domains/core/MissingAccessError';
+import {
+ AccountHandoffRedeemRequest,
+ AccountHandoffRequest,
+ AccountHandoffResponse,
+ AuthRegisterSuccessResponse,
+ CaptchaChallengeResponse,
+ EmailCodeSendRequest,
+ EmailCodeSendResponse,
+ EmailCodeVerifyRequest,
+ EmailCodeVerifyResponse,
+ IdentifyRequest,
+ IdentifyResponse,
+} from '@fluxer/schema/src/domains/auth/AuthSchemas';
+import type {Context} from 'hono';
+
+function captchaGate(ctx: Context<HonoEnv>) {
+ return async (escalationSteps: number): Promise<void> => {
+ await verifyCaptchaToken(ctx, escalationSteps);
+ };
+}
+
+function requireTrustedClientIp(ctx: Context<HonoEnv>, bucket: string): string {
+ const clientIp = getTrustedForwardedClientIp(ctx, bucket);
+ if (!clientIp) throw new MissingAccessError();
+ return clientIp;
+}
+
+async function handoffCaptchaSteps(ctx: Context<HonoEnv>): Promise<number> {
+ const clientIp = requireTrustedClientIp(ctx, RateLimitConfigs.AUTH_REGISTER_HANDOFF.bucket);
+ const body: unknown = await ctx.req.json();
+ const code = isJsonRecord(body) && typeof body.registration_url_code === 'string' ? body.registration_url_code : null;
+ return await ctx.get('authRequestService').instantAccountChallengeSteps(ctx.req.raw, clientIp, code);
+}
+
+export function AccountEntryController(app: HonoApp) {
+ app.post(
+ '/auth/identify',
+ LocalAuthMiddleware,
+ RateLimitMiddleware(RateLimitConfigs.AUTH_IDENTIFY),
+ Validator('json', IdentifyRequest),
+ OpenAPI({
+ operationId: 'identify_account',
+ summary: 'Start signing in or signing up',
+ responseSchema: IdentifyResponse,
+ statusCode: 200,
+ security: [],
+ tags: ['Auth'],
+ description:
+ 'Looks up whether an account uses this email address (or username on username instances) and returns a ticket for the next step. It says whether the account exists and whether it signs in with its password or with an email code. Answers take at least 300 ms. Heavy use from one network or for one address asks for a solved captcha challenge (X-Captcha-Token) whose cost grows with use.',
+ }),
+ async (ctx) => {
+ const body = ctx.req.valid('json');
+ const field = usesUsernameSignIn() ? 'login' : 'email';
+ const identifier = body[field];
+ if (!identifier) {
+ throw InputValidationError.fromCode(
+ field,
+ field === 'login' ? ValidationErrorCodes.USERNAME_LENGTH_INVALID : ValidationErrorCodes.EMAIL_IS_REQUIRED,
+ );
+ }
+ return ctx.json(
+ await ctx.get('authRequestService').identify({
+ identifier,
+ deviceProof: body.device_proof,
+ registrationUrlCode: body.registration_url_code,
+ request: ctx.req.raw,
+ clientIp: requireRequestClientIp(ctx),
+ captcha: captchaGate(ctx),
+ }),
+ );
+ },
+ );
+ app.post(
+ '/auth/email-code/send',
+ LocalAuthMiddleware,
+ RateLimitMiddleware(RateLimitConfigs.AUTH_EMAIL_CODE_SEND),
+ Validator('json', EmailCodeSendRequest),
+ OpenAPI({
+ operationId: 'send_email_code',
+ summary: 'Send a sign-in or sign-up code',
+ responseSchema: EmailCodeSendResponse,
+ statusCode: 200,
+ security: [],
+ tags: ['Auth'],
+ description:
+ 'Emails a 6-digit code for the address behind an identify ticket. The code signs in to an existing account without a password or creates a new one. An account with a password answers PASSWORD_SIGN_IN_REQUIRED and gets no email. Sign-ups always need a solved captcha challenge (X-Captcha-Token), and sign-ins need one after several codes for the same address. A bounced address answers the same and gets no email. On a new self-hosted instance it fails with SETUP_TOKEN_REQUIRED until the first admin account exists.',
+ }),
+ async (ctx) => {
+ const body = ctx.req.valid('json');
+ return ctx.json(
+ await ctx.get('authRequestService').sendEmailCode({
+ ticket: body.ticket,
+ registrationUrlCode: body.registration_url_code,
+ request: ctx.req.raw,
+ clientIp: requireRequestClientIp(ctx),
+ captcha: captchaGate(ctx),
+ }),
+ );
+ },
+ );
+ app.post(
+ '/auth/email-code/verify',
+ LocalAuthMiddleware,
+ RateLimitMiddleware(RateLimitConfigs.AUTH_EMAIL_CODE_VERIFY),
+ Validator('json', EmailCodeVerifyRequest),
+ OpenAPI({
+ operationId: 'verify_email_code',
+ summary: 'Finish signing in or signing up with a code',
+ responseSchema: EmailCodeVerifyResponse,
+ statusCode: 200,
+ security: [],
+ tags: ['Auth'],
+ description:
+ 'Checks the emailed code. For an existing account without a password this signs in, or returns an MFA ticket when the account has two-factor authentication. An account with a password answers PASSWORD_SIGN_IN_REQUIRED. When the code confirms an address that was not confirmed yet, it also ends every other session of the account, forgets its authorized IPs and revokes its OAuth2 grants. For a new address this creates the account with a confirmed email and signs in. Each code allows 5 tries. After many wrong codes for one address, or across the instance, it needs a solved captcha challenge (X-Captcha-Token).',
+ }),
+ async (ctx) => {
+ return ctx.json(
+ await ctx.get('authRequestService').verifyEmailCode({
+ data: ctx.req.valid('json'),
+ request: ctx.req.raw,
+ clientIp: requireRequestClientIp(ctx),
+ requestCache: ctx.get('requestCache'),
+ tierContext: tierContextFrom(ctx),
+ captcha: captchaGate(ctx),
+ }),
+ );
+ },
+ );
+ app.get(
+ '/auth/register/handoff/challenge',
+ RateLimitMiddleware(RateLimitConfigs.AUTH_REGISTER_HANDOFF_CHALLENGE),
+ OpenAPI({
+ operationId: 'get_account_handoff_challenge',
+ summary: 'Get the captcha challenge for creating an account from the website',
+ responseSchema: CaptchaChallengeResponse,
+ statusCode: 200,
+ security: [],
+ tags: ['Auth'],
+ description:
+ 'Returns the captcha challenge that creating an account without an email from the website needs right now for this visitor. Only the website server may call it.',
+ }),
+ async (ctx) => {
+ const bucket = RateLimitConfigs.AUTH_REGISTER_HANDOFF_CHALLENGE.bucket;
+ const clientIp = requireTrustedClientIp(ctx, bucket);
+ const steps = await ctx.get('authRequestService').instantAccountChallengeSteps(ctx.req.raw, clientIp);
+ ctx.header('Cache-Control', 'no-store');
+ return ctx.json(await createCaptchaChallenge(ctx, steps));
+ },
+ );
+ app.post(
+ '/auth/register/handoff',
+ LocalAuthMiddleware,
+ RateLimitMiddleware(RateLimitConfigs.AUTH_REGISTER_HANDOFF),
+ Validator('json', AccountHandoffRequest),
+ EscalatingCaptchaMiddleware(handoffCaptchaSteps),
+ OpenAPI({
+ operationId: 'create_account_handoff',
+ summary: 'Start an account from the website',
+ responseSchema: AccountHandoffResponse,
+ statusCode: 200,
+ security: [],
+ tags: ['Auth'],
+ description:
+ 'Checks the display name and returns a single-use code that the web app redeems within 15 minutes. No account exists yet: the web app creates it on redeem, when the person submits the step whose notice says that creating the account accepts the terms of service. Only the website server may call it, and it passes the visitor IP. Requires a solved captcha challenge (X-Captcha-Token) whose cost grows with recent sign-ups. On a new self-hosted instance it fails with SETUP_TOKEN_REQUIRED until the first admin account exists.',
+ }),
+ async (ctx) => {
+ const clientIp = requireTrustedClientIp(ctx, RateLimitConfigs.AUTH_REGISTER_HANDOFF.bucket);
+ return ctx.json(
+ await ctx.get('authRequestService').createAccountHandoff({data: ctx.req.valid('json'), clientIp}),
+ );
+ },
+ );
+ app.post(
+ '/auth/register/handoff/redeem',
+ LocalAuthMiddleware,
+ RateLimitMiddleware(RateLimitConfigs.AUTH_ACCOUNT_HANDOFF_REDEEM),
+ Validator('json', AccountHandoffRedeemRequest),
+ OpenAPI({
+ operationId: 'redeem_account_handoff',
+ summary: 'Create the account started on the website',
+ responseSchema: AuthRegisterSuccessResponse,
+ statusCode: 200,
+ security: [],
+ tags: ['Auth'],
+ description:
+ 'Redeems the code from create account handoff together with the nonce whose hash was sent then, creates the account with the agreement and display name given here (the display name sent with the handoff when none is given), and starts a session for the client that redeems it. The web app and the native apps can redeem it. Where the instance collects dates of birth, the account then sets one through the date of birth step. The code works once, and a wrong nonce also uses it up. A missing agreement or a display name that is not allowed answers before the code is used. It never ends or replaces another session.',
+ }),
+ async (ctx) => {
+ return ctx.json(
+ await ctx.get('authRequestService').redeemHandoff({
+ data: ctx.req.valid('json'),
+ request: ctx.req.raw,
+ requestCache: ctx.get('requestCache'),
+ tierContext: tierContextFrom(ctx),
+ }),
+ );
+ },
+ );
+}
diff --git a/fluxer_api/src/api/auth/AuthIdentify.ts b/fluxer_api/src/api/auth/AuthIdentify.ts
new file mode 100644
index 000000000..61e52c9e1
--- /dev/null
+++ b/fluxer_api/src/api/auth/AuthIdentify.ts
@@ -0,0 +1,473 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+import type {ApiContext} from '@app/api/ApiContext';
+import {
+ addressLookupCaptchaSteps,
+ assertEmailSendAllowed,
+ budgetEnvFor,
+ chargeAddressLookup,
+ codeVerifyCaptchaSteps,
+ emailSendCaptchaSteps,
+ enforceLookupHardLimits,
+ type RequesterClass,
+ recordAccountCodeFailure,
+ recordAddressCodeFailure,
+ recordEmailSend,
+ requesterNetworkKey,
+} from '@app/api/auth/AccountBudgets';
+import {
+ deviceProofMatches,
+ generateCode,
+ generateOpaqueToken,
+ hashCode,
+ issueDeviceProof,
+ normalizeEmailKey,
+ sha256Hex,
+} from '@app/api/auth/AccountCodes';
+import {createMfaTicketResponse, emitLogin, type LoginMfaResult} from '@app/api/auth/AuthLogin';
+import {
+ type AccountCreationResult,
+ assertAccountCreationPastSetup,
+ assertRegistrationConsent,
+ assertRegistrationEmailDeliverable,
+ createAccount,
+ enforceRegistrationRateLimits,
+ type RegistrationDependencies,
+ resolveInstantAccountAvailabilityFor,
+ resolveRegistrationAccess,
+} from '@app/api/auth/AuthRegistration';
+import * as AuthSession from '@app/api/auth/AuthSession';
+import {revokeAllAuthSessions} from '@app/api/auth/AuthSessionRevocation';
+import * as AuthUtility from '@app/api/auth/AuthUtility';
+import {checkEmailCode, storeEmailCode} from '@app/api/auth/EmailCodeStore';
+import {confirmEmailOwnership} from '@app/api/auth/EmailOwnership';
+import {resolveWebAuthnSecondFactor} from '@app/api/auth/services/WebAuthnSecondFactor';
+import {createUserID} from '@app/api/BrandedTypes';
+import {getContentMessage} from '@app/api/content_i18n/ContentI18n';
+import {emitActivity} from '@app/api/infrastructure/activity/ActivityEvents';
+import type {EmailCodePurpose} from '@app/api/infrastructure/activity/Contract.generated';
+import {isBlockedEmailDomain} from '@app/api/infrastructure/activity/SharedLists';
+import {
+ REGISTRATION_PENDING_APPROVAL_TRAIT,
+ REGISTRATION_REJECTED_TRAIT,
+} from '@app/api/instance/InstanceConfigRepository';
+import {getInstanceProductName} from '@app/api/instance/ProductName';
+import type {RequestCache} from '@app/api/middleware/RequestCacheMiddleware';
+import {getInstanceConfigRepository, getOAuth2TokenRepository} from '@app/api/middleware/ServiceSingletons';
+import type {User} from '@app/api/models/User';
+import type {TierContext} from '@app/api/user/AccountTier';
+import {BELOW_MINIMUM_AGE_TRAIT} from '@app/api/user/UserTraits';
+import {extractEmailDomain} from '@app/api/utils/EmailDomainUtils';
+import {lookupGeoip} from '@app/api/utils/IpUtils';
+import {resolveSessionClientInfo} from '@app/api/utils/SessionClientIdentity';
+import {UserAuthenticatorTypes} from '@fluxer/constants/src/UserConstants';
+import {AccountBelowMinimumAgeError} from '@fluxer/errors/src/domains/auth/AccountBelowMinimumAgeError';
+import {EmailCodeResendCooldownError} from '@fluxer/errors/src/domains/auth/EmailCodeResendCooldownError';
+import {EmailCodesUnavailableError} from '@fluxer/errors/src/domains/auth/EmailCodesUnavailableError';
+import {InvalidSignInCodeError} from '@fluxer/errors/src/domains/auth/InvalidSignInCodeError';
+import {PasswordSignInRequiredError} from '@fluxer/errors/src/domains/auth/PasswordSignInRequiredError';
+import {RegistrationPendingApprovalError} from '@fluxer/errors/src/domains/auth/RegistrationPendingApprovalError';
+import {RegistrationRejectedError} from '@fluxer/errors/src/domains/auth/RegistrationRejectedError';
+import {SignInCodeExpiredError} from '@fluxer/errors/src/domains/auth/SignInCodeExpiredError';
+import {UnknownSignInTicketError} from '@fluxer/errors/src/domains/auth/UnknownSignInTicketError';
+import {FeatureTemporarilyDisabledError} from '@fluxer/errors/src/domains/core/FeatureTemporarilyDisabledError';
+import {parseAcceptLanguage} from '@fluxer/i18n/src/locale/resolution/AcceptLanguageNegotiation';
+import {getSubnet} from '@fluxer/ip_utils/src/IpAddress';
+import {isCaptchaActive} from '@fluxer/schema/src/domains/admin/CaptchaSchemas';
+import type {IdentifyResponse} from '@fluxer/schema/src/domains/auth/AuthSchemas';
+import {formatGeoipLocation} from '@pkgs/geoip/src/GeoipLookup';
+import {seconds} from 'itty-time';
+
+const TICKET_TTL_SECONDS = seconds('15 minutes');
+const CODE_TTL_SECONDS = seconds('10 minutes');
+const RESEND_COOLDOWN_MS = 30_000;
+const IDENTIFY_FLOOR_MS = 300;
+const CODE_SCOPE = 'sign_in';
+const UNKNOWN_PASSKEY_OWNER = createUserID(0n);
+
+export type CaptchaGate = (escalationSteps: number) => Promise<void>;
+
+interface IdentifyTicket {
+ identifier: string;
+ user_id: string | null;
+ known_requester: boolean;
+ created_at: number;
+}
+
+interface EmailCodeRecord {
+ code_hash: string;
+ purpose: 'sign_in' | 'sign_up';
+ sent_at: number;
+ network: string;
+}
+
+function ticketKey(ticket: string): string {
+ return `auth:identify:${sha256Hex(ticket)}`;
+}
+
+function codeKey(ticket: string): string {
+ return `auth:email_code:${sha256Hex(ticket)}`;
+}
+
+async function waitForFloor(startedAt: number): Promise<void> {
+ const remaining = IDENTIFY_FLOOR_MS - (Date.now() - startedAt);
+ if (remaining > 0) await new Promise((resolve) => setTimeout(resolve, remaining));
+}
+
+async function emailCodesAvailable(): Promise<boolean> {
+ return (await getInstanceConfigRepository().getSignInFeatures()).emailCodes;
+}
+
+async function assertIdentifierFirst(): Promise<void> {
+ if (!(await getInstanceConfigRepository().getSignInFeatures()).identifierFirst) {
+ throw new FeatureTemporarilyDisabledError();
+ }
+}
+
+async function isKnownRequester(
+ ctx: ApiContext,
+ owner: User | null,
+ params: {clientIp: string; deviceProof: string | undefined},
+): Promise<boolean> {
+ if (!owner) return false;
+ if (deviceProofMatches(params.deviceProof, owner.id)) return true;
+ return await ctx.services.users.checkIpAuthorized(owner.id, params.clientIp);
+}
+
+export async function identify(
+ ctx: ApiContext,
+ params: {
+ identifier: string;
+ deviceProof: string | undefined;
+ registrationUrlCode: string | null | undefined;
+ request: Request;
+ clientIp: string;
+ captcha: CaptchaGate;
+ },
+): Promise<IdentifyResponse> {
+ const startedAt = Date.now();
+ await assertIdentifierFirst();
+ const identifier = params.identifier.trim().toLowerCase();
+ const owner = await ctx.services.users.findByEmail(identifier);
+ const ownerUsable = owner !== null && !owner.isBot;
+ await ctx.services.users.listWebAuthnCredentials(ownerUsable ? owner.id : UNKNOWN_PASSKEY_OWNER);
+ isBlockedEmailDomain(extractEmailDomain(identifier));
+ const knownRequester = await isKnownRequester(ctx, ownerUsable ? owner : null, params);
+ const requester: RequesterClass = knownRequester ? 'known' : 'unknown';
+ const env = budgetEnvFor(ctx.services, params.request);
+ if (!isCaptchaActive(await getInstanceConfigRepository().getCaptchaConfig())) {
+ await enforceLookupHardLimits(env, params.clientIp);
+ }
+ const lookup = {clientIp: params.clientIp, emailKey: normalizeEmailKey(identifier), requester};
+ const steps = await addressLookupCaptchaSteps(env, lookup);
+ if (steps !== null) await params.captcha(steps);
+ await chargeAddressLookup(env, lookup);
+ const ticket = generateOpaqueToken();
+ await ctx.services.cache.set<IdentifyTicket>(
+ ticketKey(ticket),
+ {
+ identifier,
+ user_id: ownerUsable ? owner.id.toString() : null,
+ known_requester: knownRequester,
+ created_at: startedAt,
+ },
+ TICKET_TTL_SECONDS,
+ );
+ const registration = ownerUsable ? null : await describeRegistration(ctx, params.registrationUrlCode);
+ void emitActivity('identifier_lookup', identifier, {
+ result: ownerUsable ? 'existing' : 'new',
+ ip: params.clientIp,
+ subnet: getSubnet(params.clientIp),
+ });
+ await waitForFloor(startedAt);
+ let signInMethod: IdentifyResponse['sign_in_method'] = null;
+ if (ownerUsable) {
+ signInMethod = owner.passwordHash !== null ? 'password' : 'code';
+ }
+ return {
+ ticket,
+ account: ownerUsable ? 'existing' : 'new',
+ sign_in_method: signInMethod,
+ registration,
+ };
+}
+
+async function describeRegistration(
+ ctx: ApiContext,
+ registrationUrlCode: string | null | undefined,
+): Promise<IdentifyResponse['registration']> {
+ const repository = getInstanceConfigRepository();
+ const [registration, appPublic, codes, instantAccountAvailability] = await Promise.all([
+ repository.getRegistrationConfig(),
+ repository.getAppPublicConfig(),
+ emailCodesAvailable(),
+ resolveInstantAccountAvailabilityFor(repository, registrationUrlCode),
+ ]);
+ return {
+ mode: registration.mode,
+ consent_required:
+ !ctx.services.config.instance.selfHosted ||
+ appPublic.legal.terms_url !== null ||
+ appPublic.legal.privacy_url !== null,
+ requires_password: !codes,
+ instant_account_availability: instantAccountAvailability,
+ };
+}
+
+async function readTicket(ctx: ApiContext, ticket: string): Promise<IdentifyTicket> {
+ const record = await ctx.services.cache.get<IdentifyTicket>(ticketKey(ticket));
+ if (!record) throw new UnknownSignInTicketError();
+ return record;
+}
+
+function describeRequester(request: Request, geoipLabel: string | null, locale: string | null) {
+ const info = resolveSessionClientInfo({
+ userAgent: request.headers.get('user-agent'),
+ reportedOs: null,
+ productName: getInstanceProductName(),
+ });
+ return {
+ location: geoipLabel ?? getContentMessage('auth.unknown_location', locale),
+ device: [info.browser ?? info.platform, info.os].filter(Boolean).join(', ') || '-',
+ };
+}
+
+export async function sendEmailCode(
+ ctx: ApiContext,
+ params: {
+ ticket: string;
+ registrationUrlCode: string | null | undefined;
+ request: Request;
+ clientIp: string;
+ captcha: CaptchaGate;
+ },
+): Promise<{purpose: 'sign_in' | 'sign_up'; code_expires_at: string; resend_available_at: string}> {
+ const record = await readTicket(ctx, params.ticket);
+ if (!(await emailCodesAvailable())) throw new EmailCodesUnavailableError();
+ const purpose = record.user_id === null ? 'sign_up' : 'sign_in';
+ await assertCodeSignInAllowed(ctx, record);
+ const env = budgetEnvFor(ctx.services, params.request);
+ const emailKey = normalizeEmailKey(record.identifier);
+ const requester: RequesterClass = record.known_requester ? 'known' : 'unknown';
+ const existing = await ctx.services.cache.get<EmailCodeRecord>(codeKey(params.ticket));
+ const now = Date.now();
+ if (existing && now - existing.sent_at < RESEND_COOLDOWN_MS) {
+ throw new EmailCodeResendCooldownError(Math.ceil((RESEND_COOLDOWN_MS - (now - existing.sent_at)) / 1000));
+ }
+ const steps = await emailSendCaptchaSteps(env, {emailKey, requester, clientIp: params.clientIp});
+ if (purpose === 'sign_up') {
+ await assertAccountCreationPastSetup(ctx, getInstanceConfigRepository(), params.clientIp);
+ await params.captcha(steps ?? 0);
+ await resolveRegistrationAccess(getInstanceConfigRepository(), params.registrationUrlCode, record.identifier);
+ await assertRegistrationEmailDeliverable(record.identifier, ctx.services.emailDnsValidation);
+ if (!existing) {
+ await enforceRegistrationRateLimits(ctx, {
+ enforceRateLimits: !ctx.services.config.dev.relaxRegistrationRateLimits,
+ clientIp: params.clientIp,
+ emailKey: record.identifier,
+ });
+ }
+ } else if (steps !== null) {
+ await params.captcha(steps);
+ }
+ await assertEmailSendAllowed(env, {
+ emailKey,
+ requester,
+ clientIp: params.clientIp,
+ userId: record.user_id ?? undefined,
+ });
+ const expiresAt = new Date(now + CODE_TTL_SECONDS * 1000);
+ const owner = record.user_id ? await ctx.services.users.findUnique(createUserID(BigInt(record.user_id))) : null;
+ if (!owner?.emailBounced) {
+ const code = generateCode();
+ await storeEmailCode<EmailCodeRecord>(ctx, {
+ key: codeKey(params.ticket),
+ record: {
+ code_hash: hashCode(code, CODE_SCOPE),
+ purpose,
+ sent_at: now,
+ network: requesterNetworkKey(params.clientIp),
+ },
+ ttlSeconds: CODE_TTL_SECONDS,
+ });
+ const locale = owner?.locale ?? parseAcceptLanguage(params.request.headers.get('accept-language'));
+ const geoip = await lookupGeoip(params.clientIp);
+ const requesterInfo = describeRequester(params.request, formatGeoipLocation(geoip, locale), locale);
+ if (purpose === 'sign_in') {
+ await ctx.services.email.send(
+ 'sign_in_code',
+ record.identifier,
+ {code, expiresAt, requester: requesterInfo},
+ locale,
+ );
+ } else {
+ await ctx.services.email.send(
+ 'sign_up_code',
+ record.identifier,
+ {code, expiresAt, requester: requesterInfo},
+ locale,
+ );
+ }
+ await recordEmailSend(env, {emailKey, requester});
+ emitCodeSent(owner?.id.toString() ?? null, purpose, record.identifier);
+ }
+ return {
+ purpose,
+ code_expires_at: expiresAt.toISOString(),
+ resend_available_at: new Date(now + RESEND_COOLDOWN_MS).toISOString(),
+ };
+}
+
+async function assertCodeSignInAllowed(ctx: ApiContext, ticket: IdentifyTicket): Promise<void> {
+ if (ticket.user_id === null) return;
+ const owner = await ctx.services.users.findUnique(createUserID(BigInt(ticket.user_id)));
+ if (owner?.passwordHash != null) throw new PasswordSignInRequiredError();
+}
+
+export function emitCodeSent(userId: string | null, purpose: EmailCodePurpose, email: string): void {
+ void emitActivity('email_code_sent', userId ?? email, {
+ user_id: userId,
+ purpose,
+ email_domain: extractEmailDomain(email) ?? '',
+ });
+}
+
+async function recordWrongCode(
+ ctx: ApiContext,
+ params: {ticket: IdentifyTicket; record: EmailCodeRecord; request: Request},
+): Promise<void> {
+ const env = budgetEnvFor(ctx.services, params.request);
+ if (params.ticket.known_requester && params.ticket.user_id) {
+ await recordAccountCodeFailure(env, params.ticket.user_id);
+ return;
+ }
+ await recordAddressCodeFailure(env, {
+ emailKey: normalizeEmailKey(params.ticket.identifier),
+ network: params.record.network,
+ });
+}
+
+async function consumeCode(
+ ctx: ApiContext,
+ params: {ticketId: string; ticket: IdentifyTicket; code: string; request: Request},
+) {
+ const check = await checkEmailCode<EmailCodeRecord>(ctx, {
+ key: codeKey(params.ticketId),
+ code: params.code,
+ scope: CODE_SCOPE,
+ ttlSeconds: CODE_TTL_SECONDS,
+ });
+ if (check.kind === 'wrong') {
+ await recordWrongCode(ctx, {ticket: params.ticket, record: check.record, request: params.request});
+ throw new InvalidSignInCodeError();
+ }
+ if (check.kind !== 'ok') throw new SignInCodeExpiredError();
+ await ctx.services.cache.delete(ticketKey(params.ticketId));
+ return check.record;
+}
+
+export type EmailCodeVerifyResult =
+ | {kind: 'sign_in'; user: User; token: string; deviceProof: string}
+ | {kind: 'mfa'; mfa: LoginMfaResult}
+ | {kind: 'sign_up'; result: AccountCreationResult};
+
+export async function verifyEmailCode(
+ ctx: ApiContext,
+ deps: RegistrationDependencies,
+ params: {
+ ticket: string;
+ code: string;
+ consent: boolean | undefined;
+ registrationUrlCode: string | null | undefined;
+ inviteCode: string | null | undefined;
+ request: Request;
+ clientIp: string;
+ requestCache: RequestCache;
+ tierContext: TierContext;
+ captcha: CaptchaGate;
+ },
+): Promise<EmailCodeVerifyResult> {
+ const ticket = await readTicket(ctx, params.ticket);
+ const steps = await codeVerifyCaptchaSteps(budgetEnvFor(ctx.services, params.request), {
+ emailKey: normalizeEmailKey(ticket.identifier),
+ requester: ticket.known_requester ? 'known' : 'unknown',
+ });
+ if (steps !== null) await params.captcha(steps);
+ await assertCodeSignInAllowed(ctx, ticket);
+ if (ticket.user_id === null) {
+ await assertAccountCreationPastSetup(ctx, deps.instanceConfigRepository, params.clientIp);
+ assertRegistrationConsent(
+ ctx.services.config,
+ await deps.instanceConfigRepository.getAppPublicConfig(),
+ params.consent,
+ );
+ }
+ const record = await consumeCode(ctx, {
+ ticketId: params.ticket,
+ ticket,
+ code: params.code,
+ request: params.request,
+ });
+ if (record.purpose === 'sign_up') {
+ const result = await createAccount(ctx, deps, {
+ input: {
+ email: ticket.identifier,
+ emailProven: true,
+ consent: params.consent,
+ inviteCode: params.inviteCode,
+ registrationUrlCode: params.registrationUrlCode,
+ },
+ request: params.request,
+ clientIp: params.clientIp,
+ requestCache: params.requestCache,
+ source: 'app',
+ tierContext: params.tierContext,
+ });
+ return {kind: 'sign_up', result};
+ }
+ return await completeCodeSignIn(ctx, {ticket, request: params.request, clientIp: params.clientIp});
+}
+
+async function takeOverUnconfirmedAccount(ctx: ApiContext, user: User): Promise<User> {
+ const {users} = ctx.services;
+ const updated = await confirmEmailOwnership(ctx, user);
+ await users.deleteAllAuthorizedIps(user.id);
+ const oauth2Tokens = getOAuth2TokenRepository();
+ await oauth2Tokens.deleteAllAccessTokensForUser(user.id);
+ await oauth2Tokens.deleteAllRefreshTokensForUser(user.id);
+ await revokeAllAuthSessions(ctx.services, user.id);
+ return updated;
+}
+
+async function completeCodeSignIn(
+ ctx: ApiContext,
+ params: {ticket: IdentifyTicket; request: Request; clientIp: string},
+): Promise<EmailCodeVerifyResult> {
+ const {users} = ctx.services;
+ const owner = params.ticket.user_id ? await users.findUnique(createUserID(BigInt(params.ticket.user_id))) : null;
+ const currentOwner = await users.findByEmail(params.ticket.identifier);
+ if (!owner || !currentOwner || currentOwner.id !== owner.id) throw new UnknownSignInTicketError();
+ AuthUtility.assertNonBotUser(ctx, owner);
+ if (owner.traits.has(REGISTRATION_PENDING_APPROVAL_TRAIT)) throw new RegistrationPendingApprovalError();
+ if (owner.traits.has(REGISTRATION_REJECTED_TRAIT)) throw new RegistrationRejectedError();
+ if (owner.traits.has(BELOW_MINIMUM_AGE_TRAIT)) throw new AccountBelowMinimumAgeError();
+ const allowed = await AuthUtility.assertSignInAllowed(ctx, owner);
+ const hasMfa =
+ allowed.authenticatorTypes.has(UserAuthenticatorTypes.TOTP) ||
+ allowed.authenticatorTypes.has(UserAuthenticatorTypes.WEBAUTHN);
+ if (hasMfa) {
+ AuthUtility.assertMfaTicketAllowed(ctx, allowed);
+ const mfa = await createMfaTicketResponse(ctx, allowed, await resolveWebAuthnSecondFactor(ctx, allowed), {
+ authorizeNetwork: true,
+ });
+ return {kind: 'mfa', mfa};
+ }
+ const user = allowed.emailVerified ? allowed : await takeOverUnconfirmedAccount(ctx, allowed);
+ await users.createAuthorizedIp(user.id, params.clientIp);
+ const [token] = await AuthSession.createAuthSession(ctx, {
+ user,
+ origin: AuthSession.resolveSessionOrigin(ctx, params.request),
+ });
+ emitLogin(user, true, {method: 'email_code'});
+ return {kind: 'sign_in', user, token, deviceProof: issueDeviceProof(user.id)};
+}