// Copyright 2023 The Casdoor Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. import React, {Fragment, useState} from "react"; import i18next from "i18next"; import {Button, Input} from "antd"; import * as AuthBackend from "../AuthBackend"; import {EmailMfaType, RecoveryMfaType, SmsMfaType} from "../MfaSetupPage"; import {mfaAuth} from "./MfaVerifyForm"; import MfaVerifySmsForm from "./MfaVerifySmsForm"; import MfaVerifyTotpForm from "./MfaVerifyTotpForm"; export const NextMfa = "NextMfa"; export const RequiredMfa = "RequiredMfa"; export function MfaAuthVerifyForm({formValues, authParams, mfaProps, application, onSuccess, onFail}) { formValues.password = ""; formValues.username = ""; const [loading, setLoading] = useState(false); const [mfaType, setMfaType] = useState(mfaProps.mfaType); const [recoveryCode, setRecoveryCode] = useState(""); const verify = ({passcode}) => { setLoading(true); const values = {...formValues, passcode}; values["mfaType"] = mfaProps.mfaType; const loginFunction = formValues.type === "cas" ? AuthBackend.loginCas : AuthBackend.login; loginFunction(values, authParams).then((res) => { if (res.status === "ok") { onSuccess(res); } else { onFail(res.msg); } }).catch((res) => { onFail(res.message); }).finally(() => { setLoading(false); }); }; const recover = () => { setLoading(true); const values = {...formValues, recoveryCode}; const loginFunction = formValues.type === "cas" ? AuthBackend.loginCas : AuthBackend.login; loginFunction(values, authParams).then((res) => { if (res.status === "ok") { onSuccess(res); } else { onFail(res.msg); } }).catch((res) => { onFail(res.message); }).finally(() => { setLoading(false); }); }; if (mfaType !== RecoveryMfaType) { return (
{i18next.t("mfa:Multi-factor authentication")}
{mfaProps.mfaType === SmsMfaType || mfaProps.mfaType === EmailMfaType ? (
{i18next.t("mfa:You have enabled Multi-Factor Authentication, Please click 'Send Code' to continue")}
) : (
{i18next.t("mfa:You have enabled Multi-Factor Authentication, please enter the TOTP code")}
)} {i18next.t("mfa:Have problems?")} { setMfaType("recovery"); }}> {i18next.t("mfa:Use a recovery code")}
); } else { return (
{i18next.t("mfa:Multi-factor recover")}
{i18next.t("mfa:Multi-factor recover description")}
setRecoveryCode(event.target.value)} /> {i18next.t("mfa:Have problems?")} { setMfaType(mfaProps.mfaType); }}> {i18next.t("mfa:Use SMS verification code")}
); } }