2025-07-07 16:02:42 -04:00
|
|
|
import { Request, Response, NextFunction } from "express";
|
|
|
|
import { z } from "zod";
|
|
|
|
import { db } from "@server/db";
|
|
|
|
import { users, userOrgs } from "@server/db";
|
|
|
|
import { eq, and } from "drizzle-orm";
|
|
|
|
import response from "@server/lib/response";
|
|
|
|
import HttpCode from "@server/types/HttpCode";
|
|
|
|
import createHttpError from "http-errors";
|
|
|
|
import logger from "@server/logger";
|
|
|
|
import { fromError } from "zod-validation-error";
|
|
|
|
import { OpenAPITags, registry } from "@server/openApi";
|
|
|
|
|
|
|
|
const updateUser2FAParamsSchema = z
|
|
|
|
.object({
|
2025-07-13 21:43:09 -07:00
|
|
|
userId: z.string()
|
2025-07-07 16:02:42 -04:00
|
|
|
})
|
|
|
|
.strict();
|
|
|
|
|
|
|
|
const updateUser2FABodySchema = z
|
|
|
|
.object({
|
2025-07-13 21:43:09 -07:00
|
|
|
twoFactorSetupRequested: z.boolean()
|
2025-07-07 16:02:42 -04:00
|
|
|
})
|
|
|
|
.strict();
|
|
|
|
|
|
|
|
export type UpdateUser2FAResponse = {
|
|
|
|
userId: string;
|
2025-07-13 21:43:09 -07:00
|
|
|
twoFactorRequested: boolean;
|
2025-07-07 16:02:42 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
registry.registerPath({
|
2025-07-13 21:43:09 -07:00
|
|
|
method: "post",
|
|
|
|
path: "/user/{userId}/2fa",
|
|
|
|
description: "Update a user's 2FA status.",
|
|
|
|
tags: [OpenAPITags.User],
|
2025-07-07 16:02:42 -04:00
|
|
|
request: {
|
|
|
|
params: updateUser2FAParamsSchema,
|
|
|
|
body: {
|
|
|
|
content: {
|
|
|
|
"application/json": {
|
|
|
|
schema: updateUser2FABodySchema
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
responses: {}
|
|
|
|
});
|
|
|
|
|
|
|
|
export async function updateUser2FA(
|
|
|
|
req: Request,
|
|
|
|
res: Response,
|
|
|
|
next: NextFunction
|
|
|
|
): Promise<any> {
|
|
|
|
try {
|
|
|
|
const parsedParams = updateUser2FAParamsSchema.safeParse(req.params);
|
|
|
|
if (!parsedParams.success) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
|
|
|
fromError(parsedParams.error).toString()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const parsedBody = updateUser2FABodySchema.safeParse(req.body);
|
|
|
|
if (!parsedBody.success) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
|
|
|
fromError(parsedBody.error).toString()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2025-07-13 21:43:09 -07:00
|
|
|
const { userId } = parsedParams.data;
|
|
|
|
const { twoFactorSetupRequested } = parsedBody.data;
|
2025-07-07 16:02:42 -04:00
|
|
|
|
|
|
|
// Verify the user exists in the organization
|
|
|
|
const existingUser = await db
|
|
|
|
.select()
|
2025-07-13 21:43:09 -07:00
|
|
|
.from(users)
|
|
|
|
.where(eq(users.userId, userId))
|
2025-07-07 16:02:42 -04:00
|
|
|
.limit(1);
|
|
|
|
|
|
|
|
if (existingUser.length === 0) {
|
2025-07-13 21:43:09 -07:00
|
|
|
return next(createHttpError(HttpCode.NOT_FOUND, "User not found"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (existingUser[0].type !== "internal") {
|
2025-07-07 16:02:42 -04:00
|
|
|
return next(
|
|
|
|
createHttpError(
|
2025-07-13 21:43:09 -07:00
|
|
|
HttpCode.BAD_REQUEST,
|
|
|
|
"Two-factor authentication is not supported for external users"
|
2025-07-07 16:02:42 -04:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2025-07-13 21:43:09 -07:00
|
|
|
logger.debug(`Updating 2FA for user ${userId} to ${twoFactorSetupRequested}`);
|
2025-07-07 16:02:42 -04:00
|
|
|
|
2025-07-13 21:43:09 -07:00
|
|
|
if (twoFactorSetupRequested) {
|
|
|
|
await db
|
|
|
|
.update(users)
|
|
|
|
.set({
|
|
|
|
twoFactorSetupRequested: true,
|
|
|
|
})
|
|
|
|
.where(eq(users.userId, userId));
|
|
|
|
} else {
|
|
|
|
await db
|
|
|
|
.update(users)
|
|
|
|
.set({
|
|
|
|
twoFactorSetupRequested: false,
|
|
|
|
twoFactorEnabled: false,
|
|
|
|
twoFactorSecret: null
|
|
|
|
})
|
|
|
|
.where(eq(users.userId, userId));
|
2025-07-07 16:02:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return response<UpdateUser2FAResponse>(res, {
|
2025-07-13 21:43:09 -07:00
|
|
|
data: {
|
|
|
|
userId: existingUser[0].userId,
|
|
|
|
twoFactorRequested: twoFactorSetupRequested
|
|
|
|
},
|
2025-07-07 16:02:42 -04:00
|
|
|
success: true,
|
|
|
|
error: false,
|
2025-07-13 21:43:09 -07:00
|
|
|
message: `2FA ${twoFactorSetupRequested ? "enabled" : "disabled"} for user successfully`,
|
2025-07-07 16:02:42 -04:00
|
|
|
status: HttpCode.OK
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(error);
|
|
|
|
return next(
|
|
|
|
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
|
|
|
);
|
|
|
|
}
|
2025-07-13 21:43:09 -07:00
|
|
|
}
|