fix: RateLimit verify email and fix (#23718)
* fix: RateLimit verify email * update * client chnages * type error * fix * add test * Update event-types.e2e.ts * minor update * Update event-types.e2e.ts * fix test * handle it in api v2
This commit is contained in:
1 parent
79169de8d8
commit
ee70111c45
14 files changed
+456
-22
No files matched your search
@@ -0,0 +1,28 @@
|
||||
import { createHash } from "crypto";
|
||||
|
||||
import { checkRateLimitAndThrowError } from "@calcom/lib/checkRateLimitAndThrowError";
|
||||
import { hashEmail } from "@calcom/lib/server/PiiHasher";
|
||||
import { totpRawCheck } from "@calcom/lib/totp";
|
||||
|
||||
export const verifyCodeUnAuthenticated = async (email: string, code: string) => {
|
||||
if (!email || !code) {
|
||||
throw new Error("Email and code are required");
|
||||
}
|
||||
|
||||
await checkRateLimitAndThrowError({
|
||||
rateLimitingType: "core",
|
||||
identifier: `emailVerifyCode.${hashEmail(email)}`,
|
||||
});
|
||||
|
||||
const secret = createHash("md5")
|
||||
.update(email + (process.env.CALENDSO_ENCRYPTION_KEY || ""))
|
||||
.digest("hex");
|
||||
|
||||
const isValidToken = totpRawCheck(code, secret, { step: 900 });
|
||||
|
||||
if (!isValidToken) {
|
||||
throw new Error("Invalid verification code");
|
||||
}
|
||||
|
||||
return;
|
||||
};
|
||||
@@ -1,30 +1,19 @@
|
||||
import { createHash } from "crypto";
|
||||
|
||||
import { totpRawCheck } from "@calcom/lib/totp";
|
||||
import type { ZVerifyCodeInputSchema } from "@calcom/prisma/zod-utils";
|
||||
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
import { verifyCodeUnAuthenticated } from "./util";
|
||||
|
||||
type VerifyTokenOptions = {
|
||||
input: ZVerifyCodeInputSchema;
|
||||
};
|
||||
|
||||
export const verifyCodeUnAuthenticatedHandler = async ({ input }: VerifyTokenOptions) => {
|
||||
return verifyCodeUnAuthenticated(input);
|
||||
};
|
||||
|
||||
export const verifyCodeUnAuthenticated = async (input: ZVerifyCodeInputSchema) => {
|
||||
const { email, code } = input;
|
||||
|
||||
if (!email || !code) throw new TRPCError({ code: "BAD_REQUEST" });
|
||||
|
||||
const secret = createHash("md5")
|
||||
.update(email + process.env.CALENDSO_ENCRYPTION_KEY)
|
||||
.digest("hex");
|
||||
|
||||
const isValidToken = totpRawCheck(code, secret, { step: 900 });
|
||||
|
||||
if (!isValidToken) throw new TRPCError({ code: "BAD_REQUEST", message: "invalid_code" });
|
||||
|
||||
return isValidToken;
|
||||
try {
|
||||
await verifyCodeUnAuthenticated(email, code);
|
||||
return true;
|
||||
} catch (error) {
|
||||
throw new TRPCError({ code: "BAD_REQUEST", message: "invalid_code" });
|
||||
}
|
||||
};
|
||||
Reference in new issue
Block a user