refactor: migrates user password to it's own table (#13628)

This commit is contained in:
Omar López authored and GitHub committed 2024-02-12 14:13:21 -07:00
1 parent 7702ded85e
commit 7b0d5959cb
25 files changed
+189 -89

No files matched your search

@@ -43,11 +43,11 @@ export const deleteMeHandler = async ({ ctx, input }: DeleteMeOptions) => {
throw new HttpError({ statusCode: 400, message: ErrorCode.ThirdPartyIdentityProviderEnabled });
}
if (!user.password) {
if (!user.password?.hash) {
throw new HttpError({ statusCode: 400, message: ErrorCode.UserMissingPassword });
}
const isCorrectPassword = await verifyPassword(input.password, user.password);
const isCorrectPassword = await verifyPassword(input.password, user.password.hash);
if (!isCorrectPassword) {
throw new HttpError({ statusCode: 403, message: ErrorCode.IncorrectPassword });
}
@@ -25,16 +25,11 @@ export const changePasswordHandler = async ({ input, ctx }: ChangePasswordOption
throw new TRPCError({ code: "FORBIDDEN", message: "THIRD_PARTY_IDENTITY_PROVIDER_ENABLED" });
}
const currentPasswordQuery = await prisma.user.findFirst({
where: {
id: user.id,
},
select: {
password: true,
},
const currentPasswordQuery = await prisma.userPassword.findFirst({
where: { userId: user.id },
});
const currentPassword = currentPasswordQuery?.password;
const currentPassword = currentPasswordQuery?.hash;
if (!currentPassword) {
throw new TRPCError({ code: "NOT_FOUND", message: "MISSING_PASSWORD" });
@@ -54,12 +49,12 @@ export const changePasswordHandler = async ({ input, ctx }: ChangePasswordOption
}
const hashedPassword = await hashPassword(newPassword);
await prisma.user.update({
await prisma.userPassword.update({
where: {
id: user.id,
userId: user.id,
},
data: {
password: hashedPassword,
hash: hashedPassword,
},
});
};
@@ -14,17 +14,17 @@ type VerifyPasswordOptions = {
};
export const verifyPasswordHandler = async ({ input, ctx }: VerifyPasswordOptions) => {
const user = await prisma.user.findUnique({
const userPassword = await prisma.userPassword.findUnique({
where: {
id: ctx.user.id,
userId: ctx.user.id,
},
});
if (!user?.password) {
if (!userPassword?.hash) {
throw new TRPCError({ code: "INTERNAL_SERVER_ERROR" });
}
const passwordsMatch = await verifyPassword(input.passwordInput, user.password);
const passwordsMatch = await verifyPassword(input.passwordInput, userPassword.hash);
if (!passwordsMatch) {
throw new TRPCError({ code: "UNAUTHORIZED" });
@@ -119,7 +119,7 @@ export const createHandler = async ({ input, ctx }: CreateOptions) => {
username: slugify(adminUsername),
email: adminEmail,
emailVerified: new Date(),
password: hashedPassword,
password: { create: { hash: hashedPassword } },
organizationId: organization.id,
// Default schedule
schedules: {
@@ -30,12 +30,13 @@ export const setPasswordHandler = async ({ ctx, input }: UpdateOptions) => {
});
if (!user) throw new TRPCError({ code: "BAD_REQUEST", message: "User not found" });
if (!user.password) throw new TRPCError({ code: "BAD_REQUEST", message: "Password not set by default" });
if (!user.password?.hash)
throw new TRPCError({ code: "BAD_REQUEST", message: "Password not set by default" });
const generatedPassword = createHash("md5")
.update(`${user?.email ?? ""}${process.env.CALENDSO_ENCRYPTION_KEY}`)
.digest("hex");
const isCorrectPassword = await verifyPassword(generatedPassword, user?.password);
const isCorrectPassword = await verifyPassword(generatedPassword, user.password.hash);
if (!isCorrectPassword)
throw new TRPCError({
@@ -44,12 +45,12 @@ export const setPasswordHandler = async ({ ctx, input }: UpdateOptions) => {
});
const hashedPassword = await hashPassword(newPassword);
await prisma.user.update({
await prisma.userPassword.update({
where: {
id: ctx.user.id,
userId: ctx.user.id,
},
data: {
password: hashedPassword,
hash: hashedPassword,
},
});
@@ -13,7 +13,7 @@ import { UserRepository } from "@calcom/lib/server/repository/user";
import slugify from "@calcom/lib/slugify";
import { prisma } from "@calcom/prisma";
import type { Membership, Team } from "@calcom/prisma/client";
import { Prisma, type User as UserType } from "@calcom/prisma/client";
import { Prisma, type User as UserType, type UserPassword } from "@calcom/prisma/client";
import type { Profile as ProfileType } from "@calcom/prisma/client";
import { MembershipRole } from "@calcom/prisma/enums";
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
@@ -27,12 +27,13 @@ import type { InviteMemberOptions, TeamWithParent } from "./types";
const log = logger.getSubLogger({ prefix: ["inviteMember.utils"] });
export type Invitee = Pick<
UserType,
"id" | "email" | "username" | "password" | "identityProvider" | "completedOnboarding"
"id" | "email" | "username" | "identityProvider" | "completedOnboarding"
>;
export type UserWithMembership = Invitee & {
teams?: Pick<Membership, "userId" | "teamId" | "accepted" | "role">[];
profiles: ProfileType[];
password: UserPassword | null;
};
export async function checkPermissions({
@@ -570,7 +571,7 @@ export const sendExistingUserTeamInviteEmails = async ({
* Here we want to redirect to a different place if onboarding has been completed or not. This prevents the flash of going to teams -> Then to onboarding - also show a different email template.
* This only changes if the user is a CAL user and has not completed onboarding and has no password
*/
if (!user.completedOnboarding && !user.password && user.identityProvider === "CAL") {
if (!user.completedOnboarding && !user.password?.hash && user.identityProvider === "CAL") {
const token = randomBytes(32).toString("hex");
await prisma.verificationToken.create({
data: {
@@ -73,7 +73,6 @@ export const removeMemberHandler = async ({ ctx, input }: RemoveMemberOptions) =
id: true,
movedToProfileId: true,
email: true,
password: true,
username: true,
completedOnboarding: true,
},