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

95 lines
2.6 KiB
TypeScript
Raw Normal View History

2024-10-02 21:55:49 -04:00
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";
2025-01-01 21:41:31 -05:00
import { response } from "@server/lib";
2024-10-02 21:55:49 -04:00
import { db } from "@server/db";
2024-10-03 20:55:54 -04:00
import { User, users } from "@server/db/schema";
2024-10-02 21:55:49 -04:00
import { eq } from "drizzle-orm";
import { createTOTPKeyURI } from "oslo/otp";
2024-12-21 21:01:12 -05:00
import logger from "@server/logger";
2024-12-22 16:59:30 -05:00
import { verifyPassword } from "@server/auth/password";
2025-01-01 21:41:31 -05:00
import { unauthorized } from "@server/auth/unauthorizedResponse";
2024-10-02 21:55:49 -04:00
2024-12-21 21:01:12 -05:00
export const requestTotpSecretBody = z
.object({
password: z.string()
})
.strict();
2024-10-02 21:55:49 -04:00
export type RequestTotpSecretBody = z.infer<typeof requestTotpSecretBody>;
export type RequestTotpSecretResponse = {
secret: string;
uri: string;
2024-10-02 21:55:49 -04:00
};
export async function requestTotpSecret(
req: Request,
res: Response,
2024-12-21 21:01:12 -05:00
next: NextFunction
2024-10-02 21:55:49 -04:00
): Promise<any> {
const parsedBody = requestTotpSecretBody.safeParse(req.body);
if (!parsedBody.success) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
2024-12-21 21:01:12 -05:00
fromError(parsedBody.error).toString()
)
2024-10-02 21:55:49 -04:00
);
}
const { password } = parsedBody.data;
2024-10-03 20:55:54 -04:00
const user = req.user as User;
2024-10-02 21:55:49 -04:00
2024-10-04 23:14:40 -04:00
try {
2024-12-22 16:59:30 -05: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-21 21:01:12 -05:00
"User has already enabled two-factor authentication"
)
2024-10-04 23:14:40 -04:00
);
}
const hex = crypto.getRandomValues(new Uint8Array(20));
const secret = encodeHex(hex);
2024-10-26 17:01:34 -04:00
const uri = createTOTPKeyURI("Pangolin", user.email, hex);
2024-10-04 23:14:40 -04:00
await db
.update(users)
.set({
2024-12-21 21:01:12 -05:00
twoFactorSecret: secret
2024-10-04 23:14:40 -04:00
})
2024-10-13 17:13:47 -04:00
.where(eq(users.userId, user.userId));
2024-10-02 21:55:49 -04:00
2024-10-04 23:14:40 -04:00
return response<RequestTotpSecretResponse>(res, {
data: {
secret,
uri
2024-10-04 23:14:40 -04:00
},
success: true,
error: false,
message: "TOTP secret generated successfully",
2024-12-21 21:01:12 -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 21:55:49 -04:00
return next(
createHttpError(
2024-10-04 23:14:40 -04:00
HttpCode.INTERNAL_SERVER_ERROR,
2024-12-21 21:01:12 -05:00
"Failed to generate TOTP secret"
)
2024-10-02 21:55:49 -04:00
);
}
}