import { Request, Response, NextFunction } from "express"; import createHttpError from "http-errors"; import { z } from "zod"; import { fromError } from "zod-validation-error"; import { encodeHex } from "oslo/encoding"; import HttpCode from "@server/types/HttpCode"; import { verifySession, unauthorized } from "@server/auth"; import { response } from "@server/utils"; import { db } from "@server/db"; import { users } from "@server/db/schema"; import { eq } from "drizzle-orm"; import { verify } from "@node-rs/argon2"; import { createTOTPKeyURI } from "oslo/otp"; export const requestTotpSecretBody = z.object({ password: z.string(), }); export type RequestTotpSecretBody = z.infer; export type RequestTotpSecretResponse = { secret: string; }; export async function requestTotpSecret( req: Request, res: Response, next: NextFunction, ): Promise { const parsedBody = requestTotpSecretBody.safeParse(req.body); if (!parsedBody.success) { return next( createHttpError( HttpCode.BAD_REQUEST, fromError(parsedBody.error).toString(), ), ); } const { password } = parsedBody.data; const { session, user } = await verifySession(req); if (!session) { return next(unauthorized()); } const existingUser = await db .select() .from(users) .where(eq(users.id, user.id)); if (!existingUser || !existingUser[0]) { return next( createHttpError(HttpCode.BAD_REQUEST, "User does not exist"), ); } const validPassword = await verify(existingUser[0].passwordHash, password, { memoryCost: 19456, timeCost: 2, outputLen: 32, parallelism: 1, }); if (!validPassword) { await new Promise((resolve) => setTimeout(resolve, 250)); // delay to prevent brute force attacks return next(unauthorized()); } if (user.twoFactorEnabled) { return next( createHttpError( HttpCode.BAD_REQUEST, "User has already enabled two-factor authentication", ), ); } const hex = crypto.getRandomValues(new Uint8Array(20)); const secret = encodeHex(hex); const uri = createTOTPKeyURI("pangolin", user.email, hex); await db .update(users) .set({ twoFactorSecret: secret, }) .where(eq(users.id, user.id)); return response(res, { data: { secret: uri, }, success: true, error: false, message: "TOTP secret generated successfully", status: HttpCode.OK, }); }