2024-10-02 20:42:50 -04:00
|
|
|
import { Request, Response, NextFunction } from "express";
|
|
|
|
import createHttpError from "http-errors";
|
|
|
|
import { z } from "zod";
|
|
|
|
import { fromError } from "zod-validation-error";
|
|
|
|
import { decodeHex } from "oslo/encoding";
|
|
|
|
import { TOTPController } from "oslo/otp";
|
|
|
|
import HttpCode from "@server/types/HttpCode";
|
2024-10-02 20:49:05 -04:00
|
|
|
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";
|
2024-10-02 20:42:50 -04:00
|
|
|
|
|
|
|
export const verifyTotpBody = z.object({
|
|
|
|
code: z.string(),
|
|
|
|
});
|
|
|
|
|
|
|
|
export type VerifyTotpBody = z.infer<typeof verifyTotpBody>;
|
|
|
|
|
|
|
|
export type VerifyTotpResponse = {
|
|
|
|
valid: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
export async function verifyTotp(
|
|
|
|
req: Request,
|
|
|
|
res: Response,
|
|
|
|
next: NextFunction,
|
|
|
|
): Promise<any> {
|
|
|
|
const parsedBody = verifyTotpBody.safeParse(req.body);
|
|
|
|
|
|
|
|
if (!parsedBody.success) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
|
|
|
fromError(parsedBody.error).toString(),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const { code } = parsedBody.data;
|
|
|
|
|
|
|
|
const { session, user } = await verifySession(req);
|
|
|
|
if (!session) {
|
2024-10-02 21:55:49 -04:00
|
|
|
return next(unauthorized());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (user.twoFactorEnabled) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
|
|
|
"Two-factor authentication is already enabled",
|
|
|
|
),
|
|
|
|
);
|
2024-10-02 20:42:50 -04:00
|
|
|
}
|
2024-10-02 20:49:05 -04:00
|
|
|
|
|
|
|
if (!user.twoFactorSecret) {
|
2024-10-02 21:55:49 -04:00
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
|
|
|
"User has not requested two-factor authentication",
|
|
|
|
),
|
2024-10-02 20:49:05 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const totpController = new TOTPController();
|
|
|
|
const valid = await totpController.verify(
|
2024-10-02 21:55:49 -04:00
|
|
|
code,
|
|
|
|
decodeHex(user.twoFactorSecret),
|
2024-10-02 20:49:05 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
if (valid) {
|
|
|
|
// if valid, enable two-factor authentication; the totp secret is no longer temporary
|
|
|
|
await db
|
|
|
|
.update(users)
|
|
|
|
.set({ twoFactorEnabled: true })
|
|
|
|
.where(eq(users.id, user.id));
|
|
|
|
}
|
|
|
|
|
|
|
|
return response<{ valid: boolean }>(res, {
|
|
|
|
data: { valid },
|
|
|
|
success: true,
|
|
|
|
error: false,
|
2024-10-02 23:39:07 -04:00
|
|
|
message: valid
|
|
|
|
? "Code is valid. Two-factor is now enabled"
|
|
|
|
: "Code is invalid",
|
2024-10-02 20:49:05 -04:00
|
|
|
status: HttpCode.OK,
|
|
|
|
});
|
2024-10-02 20:42:50 -04:00
|
|
|
}
|