fosrl.pangolin/server/routers/auth/disable2fa.ts

113 lines
3.2 KiB
TypeScript
Raw Normal View History

2024-10-02 23:39:07 -04:00
import { Request, Response, NextFunction } from "express";
import createHttpError from "http-errors";
import HttpCode from "@server/types/HttpCode";
import { fromError } from "zod-validation-error";
import { unauthorized } from "@server/auth";
import { z } from "zod";
import { db } from "@server/db";
2024-10-05 15:31:28 -04:00
import { twoFactorBackupCodes, User, users } from "@server/db/schema";
2024-10-02 23:39:07 -04:00
import { eq } from "drizzle-orm";
import { response } from "@server/utils";
2024-10-05 22:31:30 -04:00
import { verifyPassword } from "@server/auth/password";
import { verifyTotpCode } from "@server/auth/2fa";
2024-12-21 21:01:12 -05:00
import logger from "@server/logger";
2024-10-02 23:39:07 -04:00
export const disable2faBody = z.object({
password: z.string(),
2024-10-05 14:00:55 -04:00
code: z.string().optional(),
2024-12-21 21:01:12 -05:00
}).strict();
2024-10-02 23:39:07 -04:00
export type Disable2faBody = z.infer<typeof disable2faBody>;
2024-10-05 14:00:55 -04:00
export type Disable2faResponse = {
codeRequested?: boolean;
};
2024-10-02 23:39:07 -04:00
export async function disable2fa(
req: Request,
res: Response,
next: NextFunction,
): Promise<any> {
const parsedBody = disable2faBody.safeParse(req.body);
if (!parsedBody.success) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
fromError(parsedBody.error).toString(),
),
);
}
2024-10-05 14:00:55 -04:00
const { password, code } = parsedBody.data;
2024-10-03 20:55:54 -04:00
const user = req.user as User;
2024-10-02 23:39:07 -04:00
2024-10-04 23:14:40 -04:00
try {
2024-10-05 15:11:51 -04:00
const validPassword = await verifyPassword(password, user.passwordHash);
2024-10-04 23:14:40 -04:00
if (!validPassword) {
return next(unauthorized());
}
if (!user.twoFactorEnabled) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
"Two-factor authentication is already disabled",
),
);
2024-10-05 14:00:55 -04:00
} else {
if (!code) {
return response<{ codeRequested: boolean }>(res, {
data: { codeRequested: true },
success: true,
error: false,
message: "Two-factor authentication required",
status: HttpCode.ACCEPTED,
});
}
}
2024-10-05 15:45:01 -04:00
const validOTP = await verifyTotpCode(
code,
user.twoFactorSecret!,
2024-10-13 17:13:47 -04:00
user.userId,
2024-10-05 15:45:01 -04:00
);
2024-10-05 14:00:55 -04:00
if (!validOTP) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
"The two-factor code you entered is incorrect",
),
);
2024-10-04 23:14:40 -04:00
}
await db
.update(users)
.set({ twoFactorEnabled: false })
2024-10-13 17:13:47 -04:00
.where(eq(users.userId, user.userId));
2024-10-02 23:39:07 -04:00
2024-10-05 15:31:28 -04:00
await db
.delete(twoFactorBackupCodes)
2024-10-13 17:13:47 -04:00
.where(eq(twoFactorBackupCodes.userId, user.userId));
2024-10-05 15:31:28 -04:00
2024-10-05 17:01:49 -04:00
// TODO: send email to user confirming two-factor authentication is disabled
2024-10-04 23:14:40 -04:00
return response<null>(res, {
data: null,
success: true,
error: false,
message: "Two-factor authentication disabled",
status: HttpCode.OK,
});
} catch (error) {
2024-12-21 21:01:12 -05:00
logger.error(error);
2024-10-02 23:39:07 -04:00
return next(
createHttpError(
2024-10-04 23:14:40 -04:00
HttpCode.INTERNAL_SERVER_ERROR,
"Failed to disable two-factor authentication",
2024-10-02 23:39:07 -04:00
),
);
}
}