From 91274d8ee01e32ab1af76b16090c59e54f6ab09c Mon Sep 17 00:00:00 2001 From: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com> Date: Mon, 21 Nov 2022 13:33:45 +0530 Subject: [PATCH] fix: change admin user password requirement (#5577) * fix: change admin user password requirement * fix: preload translations Co-authored-by: Peer Richelsen --- apps/web/pages/auth/setup.tsx | 13 ++++++++++--- apps/web/public/static/locales/en/common.json | 1 + packages/lib/auth.ts | 8 +++++--- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/apps/web/pages/auth/setup.tsx b/apps/web/pages/auth/setup.tsx index c507f35c83..fbeb788d82 100644 --- a/apps/web/pages/auth/setup.tsx +++ b/apps/web/pages/auth/setup.tsx @@ -1,6 +1,7 @@ import { CheckIcon } from "@heroicons/react/solid"; import { zodResolver } from "@hookform/resolvers/zod"; import classNames from "classnames"; +import { GetServerSidePropsContext } from "next"; import { useRouter } from "next/router"; import { useState } from "react"; import { Controller, FormProvider, useForm } from "react-hook-form"; @@ -13,6 +14,8 @@ import { inferSSRProps } from "@calcom/types/inferSSRProps"; import { TextField, EmailField, PasswordField, Label } from "@calcom/ui/components/form"; import WizardForm from "@calcom/ui/v2/core/WizardForm"; +import { ssrInit } from "@server/lib/ssr"; + const StepDone = () => { const { t } = useLocale(); @@ -39,7 +42,8 @@ const SetupFormStep1 = (props: { setIsLoading: (val: boolean) => void }) => { email_address: z.string().email({ message: t("enter_valid_email") }), full_name: z.string().min(3, t("at_least_characters", { count: 3 })), password: z.string().superRefine((data, ctx) => { - const result = isPasswordValid(data, true); + const isStrict = true; + const result = isPasswordValid(data, true, isStrict); Object.keys(result).map((key: string) => { if (!result[key as keyof typeof result]) { ctx.addIssue({ @@ -185,7 +189,7 @@ const SetupFormStep1 = (props: { setIsLoading: (val: boolean) => void }) => { formMethods.setValue("password", e.target.value); await formMethods.trigger("password"); }} - hintErrors={["caplow", "min", "num"]} + hintErrors={["caplow", "admin_min", "num"]} name="password" className="my-0" autoComplete="off" @@ -221,10 +225,13 @@ export default function Setup(props: inferSSRProps) { ); } -export const getServerSideProps = async () => { +export const getServerSideProps = async (context: GetServerSidePropsContext) => { + const ssr = await ssrInit(context); const userCount = await prisma.user.count(); + return { props: { + trpcState: ssr.dehydrate(), userCount, }, }; diff --git a/apps/web/public/static/locales/en/common.json b/apps/web/public/static/locales/en/common.json index 95cbc704fc..d64fb23169 100644 --- a/apps/web/public/static/locales/en/common.json +++ b/apps/web/public/static/locales/en/common.json @@ -420,6 +420,7 @@ "current_incorrect_password": "Current password is incorrect", "password_hint_caplow": "Mix of uppercase & lowercase letters", "password_hint_min": "Minimum 8 characters long", + "password_hint_admin_min": "Minimum 15 characters long", "password_hint_num": "Contain at least 1 number", "invalid_password_hint": "The password must be a minimum of 7 characters long containing at least one number and have a mixture of uppercase and lowercase letters", "incorrect_password": "Password is incorrect.", diff --git a/packages/lib/auth.ts b/packages/lib/auth.ts index cb3a8f2cd5..a213e4e854 100644 --- a/packages/lib/auth.ts +++ b/packages/lib/auth.ts @@ -38,13 +38,15 @@ export function isPasswordValid( password: string, breakdown: boolean, strict?: boolean -): { caplow: boolean; num: boolean; min: boolean }; +): { caplow: boolean; num: boolean; min: boolean; admin_min: boolean }; export function isPasswordValid(password: string, breakdown?: boolean, strict?: boolean) { let cap = false, // Has uppercase characters low = false, // Has lowercase characters num = false, // At least one number - min = false; // Eight characters, or fifteen in strict mode. + min = false, // Eight characters, or fifteen in strict mode. + admin_min = false; if (password.length > 7 && (!strict || password.length > 14)) min = true; + if (strict && password.length > 14) admin_min = true; for (let i = 0; i < password.length; i++) { if (!isNaN(parseInt(password[i]))) num = true; else { @@ -52,7 +54,7 @@ export function isPasswordValid(password: string, breakdown?: boolean, strict?: if (password[i] === password[i].toLowerCase()) low = true; } } - return !!breakdown ? { caplow: cap && low, num, min } : cap && low && num && min; + return !!breakdown ? { caplow: cap && low, num, min, admin_min } : cap && low && num && min; } type CtxOrReq = { req: NextApiRequest; ctx?: never } | { ctx: { req: NextApiRequest }; req?: never };