2023-02-16 22:39:57 +00:00
|
|
|
import type { BaseSyntheticEvent } from "react";
|
2023-07-13 20:54:57 +00:00
|
|
|
import React, { useEffect, useState } from "react";
|
2022-09-06 18:23:17 +00:00
|
|
|
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";
|
2023-07-13 20:54:57 +00:00
|
|
|
import { useCallbackRef } from "@calcom/lib/hooks/useCallbackRef";
|
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, TextField } from "@calcom/ui";
|
2022-08-30 19:46:52 +00:00
|
|
|
|
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 EnableTwoFactorModalProps {
|
|
|
|
open: boolean;
|
|
|
|
onOpenChange: () => void;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when the user closes the modal without disabling two-factor auth
|
|
|
|
*/
|
|
|
|
onCancel: () => void;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when the user enables two-factor auth
|
|
|
|
*/
|
|
|
|
onEnable: () => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum SetupStep {
|
|
|
|
ConfirmPassword,
|
|
|
|
DisplayQrCode,
|
|
|
|
EnterTotpCode,
|
|
|
|
}
|
|
|
|
|
|
|
|
const WithStep = ({
|
|
|
|
step,
|
|
|
|
current,
|
|
|
|
children,
|
|
|
|
}: {
|
|
|
|
step: SetupStep;
|
|
|
|
current: SetupStep;
|
|
|
|
children: JSX.Element;
|
|
|
|
}) => {
|
|
|
|
return step === current ? children : null;
|
|
|
|
};
|
|
|
|
|
2022-09-06 18:23:17 +00:00
|
|
|
interface EnableTwoFactorValues {
|
|
|
|
totpCode: string;
|
|
|
|
}
|
|
|
|
|
2022-08-30 19:46:52 +00:00
|
|
|
const EnableTwoFactorModal = ({ onEnable, onCancel, open, onOpenChange }: EnableTwoFactorModalProps) => {
|
|
|
|
const { t } = useLocale();
|
2022-09-06 18:23:17 +00:00
|
|
|
const form = useForm<EnableTwoFactorValues>();
|
|
|
|
|
2022-08-30 19:46:52 +00:00
|
|
|
const setupDescriptions = {
|
|
|
|
[SetupStep.ConfirmPassword]: t("2fa_confirm_current_password"),
|
|
|
|
[SetupStep.DisplayQrCode]: t("2fa_scan_image_or_use_code"),
|
|
|
|
[SetupStep.EnterTotpCode]: t("2fa_enter_six_digit_code"),
|
|
|
|
};
|
|
|
|
const [step, setStep] = useState(SetupStep.ConfirmPassword);
|
|
|
|
const [password, setPassword] = useState("");
|
|
|
|
const [dataUri, setDataUri] = useState("");
|
|
|
|
const [secret, setSecret] = useState("");
|
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
|
|
const [errorMessage, setErrorMessage] = useState<string | null>(null);
|
|
|
|
|
2022-09-06 18:23:17 +00:00
|
|
|
async function handleSetup(e: React.FormEvent) {
|
2022-08-30 19:46:52 +00:00
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
if (isSubmitting) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setIsSubmitting(true);
|
|
|
|
setErrorMessage(null);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await TwoFactorAuthAPI.setup(password);
|
|
|
|
const body = await response.json();
|
|
|
|
|
|
|
|
if (response.status === 200) {
|
|
|
|
setDataUri(body.dataUri);
|
|
|
|
setSecret(body.secret);
|
|
|
|
setStep(SetupStep.DisplayQrCode);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (body.error === ErrorCode.IncorrectPassword) {
|
|
|
|
setErrorMessage(t("incorrect_password"));
|
|
|
|
} else {
|
|
|
|
setErrorMessage(t("something_went_wrong"));
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
setErrorMessage(t("something_went_wrong"));
|
|
|
|
console.error(t("error_enabling_2fa"), e);
|
|
|
|
} finally {
|
|
|
|
setIsSubmitting(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-06 18:23:17 +00:00
|
|
|
async function handleEnable({ totpCode }: EnableTwoFactorValues, e: BaseSyntheticEvent | undefined) {
|
|
|
|
e?.preventDefault();
|
2022-08-30 19:46:52 +00:00
|
|
|
|
2022-09-06 18:23:17 +00:00
|
|
|
if (isSubmitting) {
|
2022-08-30 19:46:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setIsSubmitting(true);
|
|
|
|
setErrorMessage(null);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await TwoFactorAuthAPI.enable(totpCode);
|
|
|
|
const body = await response.json();
|
|
|
|
|
|
|
|
if (response.status === 200) {
|
|
|
|
onEnable();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (body.error === ErrorCode.IncorrectTwoFactorCode) {
|
|
|
|
setErrorMessage(`${t("code_is_incorrect")} ${t("please_try_again")}`);
|
|
|
|
} else {
|
|
|
|
setErrorMessage(t("something_went_wrong"));
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
setErrorMessage(t("something_went_wrong"));
|
|
|
|
console.error(t("error_enabling_2fa"), e);
|
|
|
|
} finally {
|
|
|
|
setIsSubmitting(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-13 20:54:57 +00:00
|
|
|
const handleEnableRef = useCallbackRef(handleEnable);
|
|
|
|
|
|
|
|
const totpCode = form.watch("totpCode");
|
|
|
|
|
|
|
|
// auto submit 2FA if all inputs have a value
|
|
|
|
useEffect(() => {
|
|
|
|
if (totpCode?.trim().length === 6) {
|
|
|
|
form.handleSubmit(handleEnableRef.current)();
|
|
|
|
}
|
|
|
|
}, [form, handleEnableRef, totpCode]);
|
|
|
|
|
2022-08-30 19:46:52 +00:00
|
|
|
return (
|
|
|
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
2022-11-28 19:14:38 +00:00
|
|
|
<DialogContent title={t("enable_2fa")} description={setupDescriptions[step]} type="creation">
|
2022-08-30 19:46:52 +00:00
|
|
|
<WithStep step={SetupStep.ConfirmPassword} current={step}>
|
|
|
|
<form onSubmit={handleSetup}>
|
|
|
|
<div className="mb-4">
|
2022-09-08 17:40:09 +00:00
|
|
|
<TextField
|
|
|
|
label={t("password")}
|
|
|
|
type="password"
|
|
|
|
name="password"
|
|
|
|
id="password"
|
|
|
|
required
|
|
|
|
value={password}
|
|
|
|
onInput={(e) => setPassword(e.currentTarget.value)}
|
|
|
|
/>
|
2022-08-30 19:46:52 +00:00
|
|
|
{errorMessage && <p className="mt-1 text-sm text-red-700">{errorMessage}</p>}
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</WithStep>
|
|
|
|
<WithStep step={SetupStep.DisplayQrCode} current={step}>
|
|
|
|
<>
|
2023-06-29 07:47:16 +00:00
|
|
|
<div className="-mt-3 flex justify-center">
|
2022-08-30 19:46:52 +00:00
|
|
|
{
|
|
|
|
// eslint-disable-next-line @next/next/no-img-element
|
|
|
|
<img src={dataUri} alt="" />
|
|
|
|
}
|
|
|
|
</div>
|
2023-06-29 07:47:16 +00:00
|
|
|
<p data-testid="two-factor-secret" className="mb-4 text-center font-mono text-xs">
|
2023-05-30 14:23:49 +00:00
|
|
|
{secret}
|
|
|
|
</p>
|
2022-08-30 19:46:52 +00:00
|
|
|
</>
|
|
|
|
</WithStep>
|
2022-09-06 18:23:17 +00:00
|
|
|
<Form handleSubmit={handleEnable} form={form}>
|
|
|
|
<WithStep step={SetupStep.EnterTotpCode} current={step}>
|
2023-06-29 07:47:16 +00:00
|
|
|
<div className="-mt-4 pb-2">
|
2022-09-06 18:23:17 +00:00
|
|
|
<TwoFactor center />
|
2022-08-30 19:46:52 +00:00
|
|
|
|
2023-05-30 14:23:49 +00:00
|
|
|
{errorMessage && (
|
|
|
|
<p data-testid="error-submitting-code" className="mt-1 text-sm text-red-700">
|
|
|
|
{errorMessage}
|
|
|
|
</p>
|
|
|
|
)}
|
2022-08-30 19:46:52 +00:00
|
|
|
</div>
|
|
|
|
</WithStep>
|
2023-07-21 08:32:03 +00:00
|
|
|
<DialogFooter className="mt-8" showDivider>
|
2023-06-29 07:47:16 +00:00
|
|
|
<Button color="secondary" onClick={onCancel}>
|
|
|
|
{t("cancel")}
|
|
|
|
</Button>
|
2022-09-06 18:23:17 +00:00
|
|
|
<WithStep step={SetupStep.ConfirmPassword} current={step}>
|
|
|
|
<Button
|
|
|
|
type="submit"
|
2023-06-22 22:25:37 +00:00
|
|
|
className="me-2 ms-2"
|
2022-09-06 18:23:17 +00:00
|
|
|
onClick={handleSetup}
|
2023-08-08 19:32:22 +00:00
|
|
|
loading={isSubmitting}
|
2022-09-06 18:23:17 +00:00
|
|
|
disabled={password.length === 0 || isSubmitting}>
|
|
|
|
{t("continue")}
|
|
|
|
</Button>
|
|
|
|
</WithStep>
|
|
|
|
<WithStep step={SetupStep.DisplayQrCode} current={step}>
|
2023-05-30 14:23:49 +00:00
|
|
|
<Button
|
|
|
|
type="submit"
|
|
|
|
data-testid="goto-otp-screen"
|
2023-06-22 22:25:37 +00:00
|
|
|
className="me-2 ms-2"
|
2023-05-30 14:23:49 +00:00
|
|
|
onClick={() => setStep(SetupStep.EnterTotpCode)}>
|
2022-09-06 18:23:17 +00:00
|
|
|
{t("continue")}
|
|
|
|
</Button>
|
|
|
|
</WithStep>
|
|
|
|
<WithStep step={SetupStep.EnterTotpCode} current={step}>
|
2023-08-08 19:32:22 +00:00
|
|
|
<Button
|
|
|
|
type="submit"
|
|
|
|
className="me-2 ms-2"
|
|
|
|
data-testid="enable-2fa"
|
|
|
|
loading={isSubmitting}
|
|
|
|
disabled={isSubmitting}>
|
2022-09-06 18:23:17 +00:00
|
|
|
{t("enable")}
|
|
|
|
</Button>
|
|
|
|
</WithStep>
|
2023-06-29 07:47:16 +00:00
|
|
|
</DialogFooter>
|
2022-09-06 18:23:17 +00:00
|
|
|
</Form>
|
2022-08-30 19:46:52 +00:00
|
|
|
</DialogContent>
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default EnableTwoFactorModal;
|