// Copyright 2022 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 {Button, Col, Input, Modal, Row} from "antd"; import React from "react"; import i18next from "i18next"; import * as UserBackend from "../backend/UserBackend"; import * as ProviderBackend from "../backend/ProviderBackend"; import {SafetyOutlined} from "@ant-design/icons"; import {CaptchaWidget} from "./CaptchaWidget"; export const CaptchaPreview = ({ provider, providerName, clientSecret, captchaType, subType, owner, clientId, name, providerUrl, clientId2, clientSecret2, }) => { const [visible, setVisible] = React.useState(false); const [captchaImg, setCaptchaImg] = React.useState(""); const [captchaToken, setCaptchaToken] = React.useState(""); const [secret, setSecret] = React.useState(clientSecret); const [secret2, setSecret2] = React.useState(clientSecret2); const handleOk = () => { UserBackend.verifyCaptcha(captchaType, captchaToken, secret).then(() => { setCaptchaToken(""); setVisible(false); }); }; const handleCancel = () => { setVisible(false); }; const getCaptchaFromBackend = () => { UserBackend.getCaptcha(owner, name, true).then((res) => { if (captchaType === "Default") { setSecret(res.captchaId); setCaptchaImg(res.captchaImage); } else { setSecret(res.clientSecret); setSecret2(res.clientSecret2); } }); }; const clickPreview = () => { setVisible(true); provider.name = name; provider.clientId = clientId; provider.type = captchaType; provider.providerUrl = providerUrl; if (clientSecret !== "***") { provider.clientSecret = clientSecret; ProviderBackend.updateProvider(owner, providerName, provider).then(() => { getCaptchaFromBackend(); }); } else { getCaptchaFromBackend(); } }; const renderDefaultCaptcha = () => { return ( } placeholder={i18next.t("general:Captcha")} onPressEnter={handleOk} onChange={(e) => setCaptchaToken(e.target.value)} /> ); }; const onSubmit = (token) => { setCaptchaToken(token); }; const renderCheck = () => { if (captchaType === "Default") { return renderDefaultCaptcha(); } else { return ( ); } }; const getButtonDisabled = () => { if (captchaType !== "Default") { if (!clientId || !clientSecret) { return true; } if (captchaType === "Aliyun Captcha") { if (!subType || !clientId2 || !clientSecret2) { return true; } } } return false; }; return ( {renderCheck()} ); };