Skip to content

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

TypeScript fluxerfluxerapp
5.1 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-or-later

import type {ApiContext} from '@app/api/ApiContext';
import * as AuthMfa from '@app/api/auth/AuthMfa';
import * as AuthUtility from '@app/api/auth/AuthUtility';
import {createSudoModeRequiredError, userHasMfa} from '@app/api/auth/services/SudoMethods';
import type {SudoVerificationResult} from '@app/api/auth/services/SudoVerificationService';
import type {MfaBackupCode} from '@app/api/models/MfaBackupCode';
import type {User} from '@app/api/models/User';
import {dispatchUserUpdate} from '@app/api/user/UserMappers';
import {TotpGenerator} from '@app/api/utils/TotpGenerator';
import {UserAuthenticatorTypes} from '@fluxer/constants/src/UserConstants';
import {ValidationErrorCodes} from '@fluxer/constants/src/ValidationErrorCodes';
import {MfaNotDisabledError} from '@fluxer/errors/src/domains/auth/MfaNotDisabledError';
import {MfaNotEnabledError} from '@fluxer/errors/src/domains/auth/MfaNotEnabledError';
import {InputValidationError} from '@fluxer/errors/src/domains/core/InputValidationError';

const RETIRED_AUTHENTICATOR_TYPE = 1;

interface EnableMfaTotpParams {
	user: User;
	secret: string;
	code: string;
	sudoContext: SudoVerificationResult;
}

interface DisableMfaTotpParams {
	user: User;
	code: string;
	sudoContext: SudoVerificationResult;
}

interface GetMfaBackupCodesParams {
	user: User;
	regenerate: boolean;
	sudoContext: SudoVerificationResult;
}

async function assertSudoVerifiedForMfa(
	ctx: ApiContext,
	user: User,
	sudoContext: SudoVerificationResult,
): Promise<void> {
	const identityVerifiedViaSudo =
		sudoContext.method === 'mfa' || sudoContext.method === 'sudo_token' || sudoContext.method === 'email_code';
	const identityVerifiedViaPassword = sudoContext.method === 'password';
	if (identityVerifiedViaSudo || identityVerifiedViaPassword) {
		return;
	}
	throw await createSudoModeRequiredError(ctx, user);
}

async function isValidTotpSetupCode(secret: string, code: string): Promise<boolean> {
	try {
		return await new TotpGenerator(secret).validateTotp(code);
	} catch {
		return false;
	}
}

export async function assertValidTotpSetupCode(secret: string, code: string): Promise<void> {
	if (!(await isValidTotpSetupCode(secret, code))) {
		throw InputValidationError.fromCode('code', ValidationErrorCodes.INVALID_CODE);
	}
}

export async function enableMfaTotp(
	ctx: ApiContext,
	{user, secret, code, sudoContext}: EnableMfaTotpParams,
): Promise<Array<MfaBackupCode>> {
	const {users, botMfaMirror} = ctx.services;
	await assertSudoVerifiedForMfa(ctx, user, sudoContext);
	if (user.totpSecret) throw new MfaNotDisabledError();
	const userId = user.id;
	if (!(await AuthMfa.verifyMfaCode(ctx, {userId: user.id, mfaSecret: secret, code}))) {
		throw InputValidationError.fromCode('code', ValidationErrorCodes.INVALID_CODE);
	}
	const authenticatorTypes = new Set<number>(user.authenticatorTypes ?? []);
	authenticatorTypes.add(UserAuthenticatorTypes.TOTP);
	const updatedUser = await users.patchUpsert(
		userId,
		{
			totp_secret: secret,
			authenticator_types: authenticatorTypes,
		},
		user.toRow(),
	);
	const newBackupCodes = AuthUtility.generateBackupCodes();
	const mfaBackupCodes = await users.createMfaBackupCodes(userId, newBackupCodes);
	await dispatchUserUpdate(ctx.services.gateway, updatedUser);
	await botMfaMirror.syncAuthenticatorTypesForOwner(updatedUser);
	return mfaBackupCodes;
}

export async function disableMfaTotp(ctx: ApiContext, {user, code, sudoContext}: DisableMfaTotpParams): Promise<void> {
	const {users, botMfaMirror} = ctx.services;
	if (!user.totpSecret) throw new MfaNotEnabledError();
	await assertSudoVerifiedForMfa(ctx, user, sudoContext);
	if (
		sudoContext.method !== 'mfa' &&
		!(await AuthMfa.verifyMfaCode(ctx, {
			userId: user.id,
			mfaSecret: user.totpSecret,
			code,
			allowBackup: true,
		}))
	) {
		throw InputValidationError.fromCode('code', ValidationErrorCodes.INVALID_CODE);
	}
	const userId = user.id;
	const authenticatorTypes = new Set<number>(user.authenticatorTypes ?? []);
	authenticatorTypes.delete(UserAuthenticatorTypes.TOTP);
	authenticatorTypes.delete(RETIRED_AUTHENTICATOR_TYPE);
	const updatedUser = await users.patchUpsert(
		userId,
		{
			totp_secret: null,
			authenticator_types: authenticatorTypes,
		},
		user.toRow(),
	);
	if (!userHasMfa(updatedUser)) {
		await users.clearMfaBackupCodes(userId);
	}
	await dispatchUserUpdate(ctx.services.gateway, updatedUser);
	await botMfaMirror.syncAuthenticatorTypesForOwner(updatedUser);
}

export async function getMfaBackupCodes(
	ctx: ApiContext,
	{user, regenerate, sudoContext}: GetMfaBackupCodesParams,
): Promise<Array<MfaBackupCode>> {
	const {users} = ctx.services;
	await assertSudoVerifiedForMfa(ctx, user, sudoContext);
	if (regenerate) {
		return regenerateMfaBackupCodes(ctx, user);
	}
	return await users.listMfaBackupCodes(user.id);
}

export async function regenerateMfaBackupCodes(ctx: ApiContext, user: User): Promise<Array<MfaBackupCode>> {
	const {users} = ctx.services;
	const userId = user.id;
	const newBackupCodes = AuthUtility.generateBackupCodes();
	await users.clearMfaBackupCodes(userId);
	return await users.createMfaBackupCodes(userId, newBackupCodes);
}