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

121 lines
3.5 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 { verify } from "@node-rs/argon2";
import { db } from "@server/db";
2024-10-03 20:55:54 -04:00
import { 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 14:00:55 -04:00
import { decodeHex } from "oslo/encoding";
import { TOTPController } from "oslo/otp";
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-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 {
const validPassword = await verify(user.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,
"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,
});
}
}
if (!user.twoFactorSecret) {
return next(
createHttpError(
HttpCode.INTERNAL_SERVER_ERROR,
"Failed to authenticate user",
),
);
}
const validOTP = await new TOTPController().verify(
code,
decodeHex(user.twoFactorSecret),
);
if (!validOTP) {
await new Promise((resolve) => setTimeout(resolve, 250)); // delay to prevent brute force attacks
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 })
.where(eq(users.id, user.id));
2024-10-02 23:39:07 -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",
status: HttpCode.OK,
});
} catch (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
),
);
}
}