2022-09-06 18:23:17 +00:00
|
|
|
import { useState } from "react";
|
|
|
|
import { useForm } from "react-hook-form";
|
2022-08-30 19:46:52 +00:00
|
|
|
|
2023-03-10 23:45:24 +00:00
|
|
|
import { ErrorCode } from "@calcom/features/auth/lib/ErrorCode";
|
2022-08-30 19:46:52 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2023-06-29 07:47:16 +00:00
|
|
|
import { Button, Dialog, DialogContent, DialogFooter, Form, PasswordField } from "@calcom/ui";
|
2022-08-30 19:46:52 +00:00
|
|
|
|
2023-08-30 07:33:48 +00:00
|
|
|
import BackupCode from "@components/auth/BackupCode";
|
2022-09-06 18:23:17 +00:00
|
|
|
import TwoFactor from "@components/auth/TwoFactor";
|
2022-08-30 19:46:52 +00:00
|
|
|
|
|
|
|
import TwoFactorAuthAPI from "./TwoFactorAuthAPI";
|
|
|
|
|
|
|
|
interface DisableTwoFactorAuthModalProps {
|
|
|
|
open: boolean;
|
|
|
|
onOpenChange: () => void;
|
2023-07-21 08:32:03 +00:00
|
|
|
disablePassword?: boolean;
|
2022-08-30 19:46:52 +00:00
|
|
|
/** Called when the user closes the modal without disabling two-factor auth */
|
|
|
|
onCancel: () => void;
|
|
|
|
/** Called when the user disables two-factor auth */
|
|
|
|
onDisable: () => void;
|
|
|
|
}
|
|
|
|
|
2022-09-06 18:23:17 +00:00
|
|
|
interface DisableTwoFactorValues {
|
2023-08-30 07:33:48 +00:00
|
|
|
backupCode: string;
|
2022-09-06 18:23:17 +00:00
|
|
|
totpCode: string;
|
|
|
|
password: string;
|
|
|
|
}
|
|
|
|
|
2022-08-30 19:46:52 +00:00
|
|
|
const DisableTwoFactorAuthModal = ({
|
|
|
|
onDisable,
|
|
|
|
onCancel,
|
2023-07-21 08:32:03 +00:00
|
|
|
disablePassword,
|
2022-08-30 19:46:52 +00:00
|
|
|
open,
|
|
|
|
onOpenChange,
|
|
|
|
}: DisableTwoFactorAuthModalProps) => {
|
|
|
|
const [isDisabling, setIsDisabling] = useState(false);
|
|
|
|
const [errorMessage, setErrorMessage] = useState<string | null>(null);
|
2023-08-30 07:33:48 +00:00
|
|
|
const [twoFactorLostAccess, setTwoFactorLostAccess] = useState(false);
|
2022-08-30 19:46:52 +00:00
|
|
|
const { t } = useLocale();
|
|
|
|
|
2022-09-06 18:23:17 +00:00
|
|
|
const form = useForm<DisableTwoFactorValues>();
|
2022-08-30 19:46:52 +00:00
|
|
|
|
2023-08-30 07:33:48 +00:00
|
|
|
const resetForm = (clearPassword = true) => {
|
|
|
|
if (clearPassword) form.setValue("password", "");
|
|
|
|
form.setValue("backupCode", "");
|
|
|
|
form.setValue("totpCode", "");
|
|
|
|
setErrorMessage(null);
|
|
|
|
};
|
|
|
|
|
|
|
|
async function handleDisable({ password, totpCode, backupCode }: DisableTwoFactorValues) {
|
2022-08-30 19:46:52 +00:00
|
|
|
if (isDisabling) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setIsDisabling(true);
|
|
|
|
setErrorMessage(null);
|
|
|
|
|
|
|
|
try {
|
2023-08-30 07:33:48 +00:00
|
|
|
const response = await TwoFactorAuthAPI.disable(password, totpCode, backupCode);
|
2022-08-30 19:46:52 +00:00
|
|
|
if (response.status === 200) {
|
2023-08-30 07:33:48 +00:00
|
|
|
setTwoFactorLostAccess(false);
|
|
|
|
resetForm();
|
2022-08-30 19:46:52 +00:00
|
|
|
onDisable();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const body = await response.json();
|
|
|
|
if (body.error === ErrorCode.IncorrectPassword) {
|
|
|
|
setErrorMessage(t("incorrect_password"));
|
2023-08-30 07:33:48 +00:00
|
|
|
} else if (body.error === ErrorCode.SecondFactorRequired) {
|
2022-09-06 18:23:17 +00:00
|
|
|
setErrorMessage(t("2fa_required"));
|
2023-08-30 07:33:48 +00:00
|
|
|
} else if (body.error === ErrorCode.IncorrectTwoFactorCode) {
|
2022-09-06 18:23:17 +00:00
|
|
|
setErrorMessage(t("incorrect_2fa"));
|
2023-08-30 07:33:48 +00:00
|
|
|
} else if (body.error === ErrorCode.IncorrectBackupCode) {
|
|
|
|
setErrorMessage(t("incorrect_backup_code"));
|
|
|
|
} else if (body.error === ErrorCode.MissingBackupCodes) {
|
|
|
|
setErrorMessage(t("missing_backup_codes"));
|
2022-08-30 19:46:52 +00:00
|
|
|
} else {
|
|
|
|
setErrorMessage(t("something_went_wrong"));
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
setErrorMessage(t("something_went_wrong"));
|
|
|
|
console.error(t("error_disabling_2fa"), e);
|
|
|
|
} finally {
|
|
|
|
setIsDisabling(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
2022-11-28 19:14:38 +00:00
|
|
|
<DialogContent title={t("disable_2fa")} description={t("disable_2fa_recommendation")} type="creation">
|
2022-09-06 18:23:17 +00:00
|
|
|
<Form form={form} handleSubmit={handleDisable}>
|
2023-06-29 07:47:16 +00:00
|
|
|
<div className="mb-8">
|
2023-07-21 08:32:03 +00:00
|
|
|
{!disablePassword && (
|
|
|
|
<PasswordField
|
2023-08-30 07:33:48 +00:00
|
|
|
required
|
2023-07-21 08:32:03 +00:00
|
|
|
labelProps={{
|
|
|
|
className: "block text-sm font-medium text-default",
|
|
|
|
}}
|
|
|
|
{...form.register("password")}
|
|
|
|
className="border-default mt-1 block w-full rounded-md border px-3 py-2 text-sm focus:border-black focus:outline-none focus:ring-black"
|
|
|
|
/>
|
|
|
|
)}
|
2023-08-30 07:33:48 +00:00
|
|
|
{twoFactorLostAccess ? (
|
|
|
|
<BackupCode center={false} />
|
|
|
|
) : (
|
|
|
|
<TwoFactor center={false} autoFocus={false} />
|
|
|
|
)}
|
2022-08-30 19:46:52 +00:00
|
|
|
|
|
|
|
{errorMessage && <p className="mt-1 text-sm text-red-700">{errorMessage}</p>}
|
|
|
|
</div>
|
2022-09-06 18:23:17 +00:00
|
|
|
|
2023-07-21 08:32:03 +00:00
|
|
|
<DialogFooter showDivider className="relative mt-5">
|
2023-08-30 07:33:48 +00:00
|
|
|
<Button
|
|
|
|
color="minimal"
|
|
|
|
className="mr-auto"
|
|
|
|
onClick={() => {
|
|
|
|
setTwoFactorLostAccess(!twoFactorLostAccess);
|
|
|
|
resetForm(false);
|
|
|
|
}}>
|
|
|
|
{twoFactorLostAccess ? t("go_back") : t("lost_access")}
|
|
|
|
</Button>
|
2022-09-06 18:23:17 +00:00
|
|
|
<Button color="secondary" onClick={onCancel}>
|
|
|
|
{t("cancel")}
|
|
|
|
</Button>
|
2023-08-08 19:32:22 +00:00
|
|
|
<Button
|
|
|
|
type="submit"
|
|
|
|
className="me-2 ms-2"
|
|
|
|
data-testid="disable-2fa"
|
|
|
|
loading={isDisabling}
|
|
|
|
disabled={isDisabling}>
|
2023-06-29 07:47:16 +00:00
|
|
|
{t("disable")}
|
|
|
|
</Button>
|
|
|
|
</DialogFooter>
|
2022-09-06 18:23:17 +00:00
|
|
|
</Form>
|
2022-08-30 19:46:52 +00:00
|
|
|
</DialogContent>
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default DisableTwoFactorAuthModal;
|