2023-02-16 22:39:57 +00:00
|
|
|
import type { BaseSyntheticEvent } from "react";
|
|
|
|
import React, { useState } from "react";
|
2022-08-31 20:57:53 +00:00
|
|
|
import { useForm } from "react-hook-form";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2023-03-10 23:45:24 +00:00
|
|
|
import { ErrorCode } from "@calcom/features/auth/lib/ErrorCode";
|
2022-08-31 20:57:53 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2022-11-23 02:55:25 +00:00
|
|
|
import { Button, Dialog, DialogContent, Form } from "@calcom/ui";
|
2022-03-16 23:36:43 +00:00
|
|
|
|
2022-08-31 20:57:53 +00:00
|
|
|
import TwoFactor from "@components/auth/TwoFactor";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2021-09-21 09:29:20 +00:00
|
|
|
import TwoFactorAuthAPI from "./TwoFactorAuthAPI";
|
|
|
|
import TwoFactorModalHeader from "./TwoFactorModalHeader";
|
|
|
|
|
|
|
|
interface EnableTwoFactorModalProps {
|
|
|
|
/**
|
|
|
|
* 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-08-31 20:57:53 +00:00
|
|
|
interface EnableTwoFactorValues {
|
|
|
|
totpCode: string;
|
|
|
|
}
|
|
|
|
|
2021-10-12 13:11:33 +00:00
|
|
|
const EnableTwoFactorModal = ({ onEnable, onCancel }: EnableTwoFactorModalProps) => {
|
2021-10-15 10:53:42 +00:00
|
|
|
const { t } = useLocale();
|
2022-08-31 20:57:53 +00:00
|
|
|
const form = useForm<EnableTwoFactorValues>();
|
|
|
|
|
2021-10-15 10:53:42 +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"),
|
|
|
|
};
|
2021-09-21 09:29:20 +00:00
|
|
|
const [step, setStep] = useState(SetupStep.ConfirmPassword);
|
|
|
|
const [password, setPassword] = useState("");
|
|
|
|
const [dataUri, setDataUri] = useState("");
|
2021-09-25 15:53:12 +00:00
|
|
|
const [secret, setSecret] = useState("");
|
2021-09-21 09:29:20 +00:00
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
|
|
const [errorMessage, setErrorMessage] = useState<string | null>(null);
|
|
|
|
|
2022-08-31 20:57:53 +00:00
|
|
|
async function handleSetup(e: React.FormEvent) {
|
2021-09-21 09:29:20 +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);
|
2021-09-25 15:53:12 +00:00
|
|
|
setSecret(body.secret);
|
2021-09-21 09:29:20 +00:00
|
|
|
setStep(SetupStep.DisplayQrCode);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (body.error === ErrorCode.IncorrectPassword) {
|
2021-10-08 11:43:48 +00:00
|
|
|
setErrorMessage(t("incorrect_password"));
|
2021-09-21 09:29:20 +00:00
|
|
|
} else {
|
2021-10-08 11:43:48 +00:00
|
|
|
setErrorMessage(t("something_went_wrong"));
|
2021-09-21 09:29:20 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2021-10-08 11:43:48 +00:00
|
|
|
setErrorMessage(t("something_went_wrong"));
|
|
|
|
console.error(t("error_enabling_2fa"), e);
|
2021-09-21 09:29:20 +00:00
|
|
|
} finally {
|
|
|
|
setIsSubmitting(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-31 20:57:53 +00:00
|
|
|
async function handleEnable({ totpCode }: EnableTwoFactorValues, e: BaseSyntheticEvent | undefined) {
|
|
|
|
e?.preventDefault();
|
2021-09-21 09:29:20 +00:00
|
|
|
|
2022-08-31 20:57:53 +00:00
|
|
|
if (isSubmitting) {
|
2021-09-21 09:29:20 +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) {
|
2021-10-08 11:43:48 +00:00
|
|
|
setErrorMessage(`${t("code_is_incorrect")} ${t("please_try_again")}`);
|
2021-09-21 09:29:20 +00:00
|
|
|
} else {
|
2021-10-08 11:43:48 +00:00
|
|
|
setErrorMessage(t("something_went_wrong"));
|
2021-09-21 09:29:20 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2021-10-08 11:43:48 +00:00
|
|
|
setErrorMessage(t("something_went_wrong"));
|
|
|
|
console.error(t("error_enabling_2fa"), e);
|
2021-09-21 09:29:20 +00:00
|
|
|
} finally {
|
|
|
|
setIsSubmitting(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Dialog open={true}>
|
|
|
|
<DialogContent>
|
2021-10-08 11:43:48 +00:00
|
|
|
<TwoFactorModalHeader title={t("enable_2fa")} description={setupDescriptions[step]} />
|
2021-09-21 09:29:20 +00:00
|
|
|
|
|
|
|
<WithStep step={SetupStep.ConfirmPassword} current={step}>
|
|
|
|
<form onSubmit={handleSetup}>
|
|
|
|
<div className="mb-4">
|
2023-04-05 18:14:46 +00:00
|
|
|
<label htmlFor="password" className="text-default mt-4 block text-sm font-medium">
|
2021-10-08 11:43:48 +00:00
|
|
|
{t("password")}
|
2021-09-21 09:29:20 +00:00
|
|
|
</label>
|
|
|
|
<div className="mt-1">
|
|
|
|
<input
|
|
|
|
type="password"
|
|
|
|
name="password"
|
|
|
|
id="password"
|
|
|
|
required
|
|
|
|
value={password}
|
|
|
|
onInput={(e) => setPassword(e.currentTarget.value)}
|
2023-04-05 18:14:46 +00:00
|
|
|
className="border-default block w-full rounded-sm text-sm"
|
2021-09-21 09:29:20 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
{errorMessage && <p className="mt-1 text-sm text-red-700">{errorMessage}</p>}
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</WithStep>
|
|
|
|
<WithStep step={SetupStep.DisplayQrCode} current={step}>
|
2021-09-25 15:53:12 +00:00
|
|
|
<>
|
|
|
|
<div className="flex justify-center">
|
2022-05-14 13:49:39 +00:00
|
|
|
{
|
|
|
|
// eslint-disable-next-line @next/next/no-img-element
|
|
|
|
<img src={dataUri} alt="" />
|
|
|
|
}
|
2021-09-25 15:53:12 +00:00
|
|
|
</div>
|
2022-02-09 22:32:31 +00:00
|
|
|
<p className="text-center font-mono text-xs">{secret}</p>
|
2021-09-25 15:53:12 +00:00
|
|
|
</>
|
2021-09-21 09:29:20 +00:00
|
|
|
</WithStep>
|
2022-08-31 20:57:53 +00:00
|
|
|
<Form handleSubmit={handleEnable} form={form}>
|
|
|
|
<WithStep step={SetupStep.EnterTotpCode} current={step}>
|
2021-09-21 09:29:20 +00:00
|
|
|
<div className="mb-4">
|
2022-08-31 20:57:53 +00:00
|
|
|
<TwoFactor center />
|
2021-09-21 09:29:20 +00:00
|
|
|
|
|
|
|
{errorMessage && <p className="mt-1 text-sm text-red-700">{errorMessage}</p>}
|
|
|
|
</div>
|
|
|
|
</WithStep>
|
2022-08-31 20:57:53 +00:00
|
|
|
|
|
|
|
<div className="mt-5 sm:mt-4 sm:flex sm:flex-row-reverse">
|
|
|
|
<WithStep step={SetupStep.ConfirmPassword} current={step}>
|
|
|
|
<Button
|
|
|
|
type="submit"
|
2023-06-22 22:25:37 +00:00
|
|
|
className="me-2 ms-2"
|
2022-08-31 20:57:53 +00:00
|
|
|
onClick={handleSetup}
|
|
|
|
disabled={password.length === 0 || isSubmitting}>
|
|
|
|
{t("continue")}
|
|
|
|
</Button>
|
|
|
|
</WithStep>
|
|
|
|
<WithStep step={SetupStep.DisplayQrCode} current={step}>
|
2023-06-22 22:25:37 +00:00
|
|
|
<Button type="submit" className="me-2 ms-2" onClick={() => setStep(SetupStep.EnterTotpCode)}>
|
2022-08-31 20:57:53 +00:00
|
|
|
{t("continue")}
|
|
|
|
</Button>
|
|
|
|
</WithStep>
|
|
|
|
<WithStep step={SetupStep.EnterTotpCode} current={step}>
|
2023-06-22 22:25:37 +00:00
|
|
|
<Button type="submit" className="me-2 ms-2" disabled={isSubmitting}>
|
2022-08-31 20:57:53 +00:00
|
|
|
{t("enable")}
|
|
|
|
</Button>
|
|
|
|
</WithStep>
|
|
|
|
<Button color="secondary" onClick={onCancel}>
|
|
|
|
{t("cancel")}
|
2021-09-21 09:29:20 +00:00
|
|
|
</Button>
|
2022-08-31 20:57:53 +00:00
|
|
|
</div>
|
|
|
|
</Form>
|
2021-09-21 09:29:20 +00:00
|
|
|
</DialogContent>
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default EnableTwoFactorModal;
|