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

129 lines
3.7 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 { z } from "zod";
import { db } from "@server/db";
2025-01-01 21:41:31 -05:00
import { User, users } from "@server/db/schema";
2024-10-02 23:39:07 -04:00
import { eq } from "drizzle-orm";
2025-01-01 21:41:31 -05:00
import { response } from "@server/lib";
2024-10-05 22:31:30 -04:00
import { verifyPassword } from "@server/auth/password";
2025-01-01 21:41:31 -05:00
import { verifyTotpCode } from "@server/auth/totp";
2024-12-21 21:01:12 -05:00
import logger from "@server/logger";
import { sendEmail } from "@server/emails";
import TwoFactorAuthNotification from "@server/emails/templates/TwoFactorAuthNotification";
2025-01-01 21:41:31 -05:00
import config from "@server/lib/config";
import { unauthorized } from "@server/auth/unauthorizedResponse";
2024-10-02 23:39:07 -04:00
2024-12-24 16:00:02 -05:00
export const disable2faBody = z
.object({
password: z.string(),
code: z.string().optional()
})
.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,
2024-12-24 16:00:02 -05:00
next: NextFunction
2024-10-02 23:39:07 -04:00
): Promise<any> {
const parsedBody = disable2faBody.safeParse(req.body);
if (!parsedBody.success) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
2024-12-24 16:00:02 -05:00
fromError(parsedBody.error).toString()
)
2024-10-02 23:39:07 -04:00
);
}
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,
2024-12-24 16:00:02 -05:00
"Two-factor authentication is already disabled"
)
2024-10-04 23:14:40 -04:00
);
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",
2024-12-24 16:00:02 -05:00
status: HttpCode.ACCEPTED
2024-10-05 14:00:55 -04:00
});
}
}
2024-10-05 15:45:01 -04:00
const validOTP = await verifyTotpCode(
code,
user.twoFactorSecret!,
2024-12-24 16:00:02 -05:00
user.userId
2024-10-05 15:45:01 -04:00
);
2024-10-05 14:00:55 -04:00
if (!validOTP) {
2025-01-27 22:43:32 -05:00
if (config.getRawConfig().app.log_failed_attempts) {
logger.info(
`Two-factor authentication code is incorrect. Email: ${user.email}. IP: ${req.ip}.`
);
}
2024-10-05 14:00:55 -04:00
return next(
createHttpError(
HttpCode.BAD_REQUEST,
2024-12-24 16:00:02 -05:00
"The two-factor code you entered is incorrect"
)
2024-10-05 14:00:55 -04:00
);
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-05 15:31:28 -04:00
sendEmail(
TwoFactorAuthNotification({
email: user.email,
enabled: false
}),
{
to: user.email,
from: config.getRawConfig().email?.no_reply,
subject: "Two-factor authentication disabled"
}
);
2024-10-05 17:01:49 -04:00
2024-10-04 23:14:40 -04:00
return response<null>(res, {
data: null,
success: true,
error: false,
message: "Two-factor authentication disabled",
2024-12-24 16:00:02 -05:00
status: HttpCode.OK
2024-10-04 23:14:40 -04:00
});
} 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,
2024-12-24 16:00:02 -05:00
"Failed to disable two-factor authentication"
)
2024-10-02 23:39:07 -04:00
);
}
}