2022-02-13 23:39:27 +08:00
|
|
|
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
|
2021-02-13 13:30:51 +08:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2021-02-13 12:15:19 +08:00
|
|
|
import React from "react";
|
2021-06-14 22:42:58 +08:00
|
|
|
import {Link} from "react-router-dom";
|
2022-07-12 20:06:01 +08:00
|
|
|
import {Button, Checkbox, Col, Form, Input, Result, Row, Spin, Tabs} from "antd";
|
2021-02-13 12:15:19 +08:00
|
|
|
import {LockOutlined, UserOutlined} from "@ant-design/icons";
|
2022-07-12 20:06:01 +08:00
|
|
|
import * as UserWebauthnBackend from "../backend/UserWebauthnBackend";
|
2021-02-14 15:40:57 +08:00
|
|
|
import * as AuthBackend from "./AuthBackend";
|
2022-09-10 20:41:45 +08:00
|
|
|
import * as OrganizationBackend from "../backend/OrganizationBackend";
|
2021-04-19 01:14:41 +08:00
|
|
|
import * as ApplicationBackend from "../backend/ApplicationBackend";
|
2021-02-14 16:59:08 +08:00
|
|
|
import * as Provider from "./Provider";
|
2022-08-05 18:59:56 +08:00
|
|
|
import * as ProviderButton from "./ProviderButton";
|
2021-02-14 15:40:57 +08:00
|
|
|
import * as Util from "./Util";
|
2021-03-26 21:58:19 +08:00
|
|
|
import * as Setting from "../Setting";
|
2021-10-09 21:15:57 +08:00
|
|
|
import SelfLoginButton from "./SelfLoginButton";
|
2021-04-27 22:03:22 +08:00
|
|
|
import i18next from "i18next";
|
2021-11-06 22:04:20 +08:00
|
|
|
import CustomGithubCorner from "../CustomGithubCorner";
|
2022-01-07 20:34:27 +08:00
|
|
|
import {CountDownInput} from "../common/CountDownInput";
|
2021-02-13 12:15:19 +08:00
|
|
|
|
2022-07-12 20:45:22 +08:00
|
|
|
const {TabPane} = Tabs;
|
2022-07-12 20:06:01 +08:00
|
|
|
|
2021-03-26 21:56:51 +08:00
|
|
|
class LoginPage extends React.Component {
|
2021-02-13 12:15:19 +08:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
classes: props,
|
2021-03-20 11:34:04 +08:00
|
|
|
type: props.type,
|
2021-02-13 12:15:19 +08:00
|
|
|
applicationName: props.applicationName !== undefined ? props.applicationName : (props.match === undefined ? null : props.match.params.applicationName),
|
2022-08-06 23:47:28 +08:00
|
|
|
owner: props.owner !== undefined ? props.owner : (props.match === undefined ? null : props.match.params.owner),
|
2021-02-13 12:15:19 +08:00
|
|
|
application: null,
|
2021-06-14 22:42:58 +08:00
|
|
|
mode: props.mode !== undefined ? props.mode : (props.match === undefined ? null : props.match.params.mode), // "signup" or "signin"
|
2021-11-28 21:10:21 +08:00
|
|
|
isCodeSignin: false,
|
2021-03-20 11:34:04 +08:00
|
|
|
msg: null,
|
2021-11-28 21:10:21 +08:00
|
|
|
username: null,
|
2022-03-18 20:12:29 +08:00
|
|
|
validEmailOrPhone: false,
|
|
|
|
validEmail: false,
|
|
|
|
validPhone: false,
|
2022-08-06 23:54:56 +08:00
|
|
|
loginMethod: "password",
|
2021-02-13 12:15:19 +08:00
|
|
|
};
|
2022-07-12 20:06:01 +08:00
|
|
|
|
2022-04-04 00:09:04 +08:00
|
|
|
if (this.state.type === "cas" && props.match?.params.casApplicationName !== undefined) {
|
2022-07-10 15:45:55 +08:00
|
|
|
this.state.owner = props.match?.params.owner;
|
|
|
|
this.state.applicationName = props.match?.params.casApplicationName;
|
2022-04-04 00:09:04 +08:00
|
|
|
}
|
2021-02-13 12:15:19 +08:00
|
|
|
}
|
|
|
|
|
2021-03-27 11:38:15 +08:00
|
|
|
UNSAFE_componentWillMount() {
|
2022-04-04 00:09:04 +08:00
|
|
|
if (this.state.type === "login" || this.state.type === "cas") {
|
2021-03-20 11:34:04 +08:00
|
|
|
this.getApplication();
|
|
|
|
} else if (this.state.type === "code") {
|
|
|
|
this.getApplicationLogin();
|
2022-07-10 15:45:55 +08:00
|
|
|
} else if (this.state.type === "saml") {
|
2022-04-08 23:06:48 +08:00
|
|
|
this.getSamlApplication();
|
2021-03-20 11:34:04 +08:00
|
|
|
} else {
|
|
|
|
Util.showMessage("error", `Unknown authentication type: ${this.state.type}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getApplicationLogin() {
|
2021-03-20 16:51:10 +08:00
|
|
|
const oAuthParams = Util.getOAuthGetParameters();
|
|
|
|
AuthBackend.getApplicationLogin(oAuthParams)
|
2021-03-20 11:34:04 +08:00
|
|
|
.then((res) => {
|
|
|
|
if (res.status === "ok") {
|
|
|
|
this.setState({
|
|
|
|
application: res.data,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Util.showMessage("error", res.msg);
|
|
|
|
this.setState({
|
|
|
|
application: res.data,
|
|
|
|
msg: res.msg,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2021-02-13 12:15:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
getApplication() {
|
2021-02-13 23:00:43 +08:00
|
|
|
if (this.state.applicationName === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-10 20:41:45 +08:00
|
|
|
if (this.state.owner === null || this.state.owner === undefined || this.state.owner === "") {
|
|
|
|
ApplicationBackend.getApplication("admin", this.state.applicationName)
|
|
|
|
.then((application) => {
|
|
|
|
this.setState({
|
|
|
|
application: application,
|
|
|
|
});
|
2021-02-13 12:15:19 +08:00
|
|
|
});
|
2022-09-10 20:41:45 +08:00
|
|
|
} else {
|
|
|
|
OrganizationBackend.getDefaultApplication("admin", this.state.owner)
|
|
|
|
.then((res) => {
|
|
|
|
if (res.status === "ok") {
|
|
|
|
this.setState({
|
|
|
|
application: res.data,
|
|
|
|
applicationName: res.data.name,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
Util.showMessage("error", res.msg);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-02-13 12:15:19 +08:00
|
|
|
}
|
|
|
|
|
2022-07-10 15:45:55 +08:00
|
|
|
getSamlApplication() {
|
|
|
|
if (this.state.applicationName === null) {
|
2022-04-08 23:06:48 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
ApplicationBackend.getApplication(this.state.owner, this.state.applicationName)
|
|
|
|
.then((application) => {
|
|
|
|
this.setState({
|
|
|
|
application: application,
|
|
|
|
});
|
2022-08-07 00:17:27 +08:00
|
|
|
}
|
2022-07-10 15:45:55 +08:00
|
|
|
);
|
2022-04-08 23:06:48 +08:00
|
|
|
}
|
|
|
|
|
2021-02-13 12:15:19 +08:00
|
|
|
getApplicationObj() {
|
|
|
|
if (this.props.application !== undefined) {
|
|
|
|
return this.props.application;
|
|
|
|
} else {
|
|
|
|
return this.state.application;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-20 22:17:03 +08:00
|
|
|
onUpdateAccount(account) {
|
|
|
|
this.props.onUpdateAccount(account);
|
|
|
|
}
|
|
|
|
|
2021-02-13 12:15:19 +08:00
|
|
|
onFinish(values) {
|
2022-08-05 17:43:04 +08:00
|
|
|
if (this.state.loginMethod === "webAuthn") {
|
|
|
|
let username = this.state.username;
|
|
|
|
if (username === null || username === "") {
|
|
|
|
username = values["username"];
|
|
|
|
}
|
|
|
|
|
|
|
|
this.signInWithWebAuthn(username);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-20 22:17:03 +08:00
|
|
|
const application = this.getApplicationObj();
|
|
|
|
const ths = this;
|
2022-04-04 00:09:04 +08:00
|
|
|
|
2022-06-14 21:51:40 +08:00
|
|
|
// here we are supposed to determine whether Casdoor is working as an OAuth server or CAS server
|
2022-04-04 00:09:04 +08:00
|
|
|
if (this.state.type === "cas") {
|
2022-06-14 21:51:40 +08:00
|
|
|
// CAS
|
|
|
|
const casParams = Util.getCasParameters();
|
2022-03-01 19:09:59 +08:00
|
|
|
values["type"] = this.state.type;
|
2022-04-04 00:09:04 +08:00
|
|
|
AuthBackend.loginCas(values, casParams).then((res) => {
|
2022-07-10 15:45:55 +08:00
|
|
|
if (res.status === "ok") {
|
2022-06-14 21:51:40 +08:00
|
|
|
let msg = "Logged in successfully. ";
|
2022-04-25 20:00:57 +08:00
|
|
|
if (casParams.service === "") {
|
2022-06-14 21:51:40 +08:00
|
|
|
// If service was not specified, Casdoor must display a message notifying the client that it has successfully initiated a single sign-on session.
|
|
|
|
msg += "Now you can visit apps protected by Casdoor.";
|
2021-03-20 13:05:34 +08:00
|
|
|
}
|
2022-04-04 00:09:04 +08:00
|
|
|
Util.showMessage("success", msg);
|
2022-06-14 21:51:40 +08:00
|
|
|
|
2022-04-04 00:09:04 +08:00
|
|
|
if (casParams.service !== "") {
|
2022-08-08 23:35:24 +08:00
|
|
|
const st = res.data;
|
|
|
|
const newUrl = new URL(casParams.service);
|
2022-06-14 21:51:40 +08:00
|
|
|
newUrl.searchParams.append("ticket", st);
|
|
|
|
window.location.href = newUrl.toString();
|
2022-04-04 00:09:04 +08:00
|
|
|
}
|
2021-02-13 12:15:19 +08:00
|
|
|
} else {
|
2021-03-26 21:57:41 +08:00
|
|
|
Util.showMessage("error", `Failed to log in: ${res.msg}`);
|
2021-02-13 12:15:19 +08:00
|
|
|
}
|
2022-07-10 15:45:55 +08:00
|
|
|
});
|
2022-04-04 00:09:04 +08:00
|
|
|
} else {
|
2022-06-14 21:51:40 +08:00
|
|
|
// OAuth
|
2022-04-04 00:09:04 +08:00
|
|
|
const oAuthParams = Util.getOAuthGetParameters();
|
2022-08-13 11:23:16 +08:00
|
|
|
if (oAuthParams !== null && oAuthParams.responseType !== null && oAuthParams.responseType !== "") {
|
2022-06-14 21:51:40 +08:00
|
|
|
values["type"] = oAuthParams.responseType;
|
|
|
|
} else {
|
2022-04-04 00:09:04 +08:00
|
|
|
values["type"] = this.state.type;
|
|
|
|
}
|
|
|
|
values["phonePrefix"] = this.getApplicationObj()?.organizationObj.phonePrefix;
|
2022-04-08 23:06:48 +08:00
|
|
|
|
2022-06-14 21:51:40 +08:00
|
|
|
if (oAuthParams !== null) {
|
2022-04-08 23:06:48 +08:00
|
|
|
values["samlRequest"] = oAuthParams.samlRequest;
|
|
|
|
}
|
2022-06-14 21:51:40 +08:00
|
|
|
|
2022-08-13 11:23:16 +08:00
|
|
|
if (values["samlRequest"] !== null && values["samlRequest"] !== "" && values["samlRequest"] !== undefined) {
|
2022-07-10 15:45:55 +08:00
|
|
|
values["type"] = "saml";
|
2022-04-08 23:06:48 +08:00
|
|
|
}
|
2022-06-14 21:51:40 +08:00
|
|
|
|
2022-08-17 23:18:38 +08:00
|
|
|
if (this.state.owner !== null && this.state.owner !== undefined) {
|
2022-07-26 19:27:24 +08:00
|
|
|
values["organization"] = this.state.owner;
|
|
|
|
}
|
|
|
|
|
2022-04-04 00:09:04 +08:00
|
|
|
AuthBackend.login(values, oAuthParams)
|
|
|
|
.then((res) => {
|
2022-07-10 15:45:55 +08:00
|
|
|
if (res.status === "ok") {
|
2022-04-04 00:09:04 +08:00
|
|
|
const responseType = values["type"];
|
|
|
|
if (responseType === "login") {
|
2022-07-10 15:45:55 +08:00
|
|
|
Util.showMessage("success", "Logged in successfully");
|
2022-04-04 00:09:04 +08:00
|
|
|
|
|
|
|
const link = Setting.getFromLink();
|
|
|
|
Setting.goToLink(link);
|
|
|
|
} else if (responseType === "code") {
|
|
|
|
const code = res.data;
|
2022-07-10 15:45:55 +08:00
|
|
|
const concatChar = oAuthParams?.redirectUri?.includes("?") ? "&" : "?";
|
2022-07-26 23:03:55 +08:00
|
|
|
const noRedirect = oAuthParams.noRedirect;
|
2022-04-04 00:09:04 +08:00
|
|
|
|
|
|
|
if (Setting.hasPromptPage(application)) {
|
|
|
|
AuthBackend.getAccount("")
|
|
|
|
.then((res) => {
|
|
|
|
let account = null;
|
|
|
|
if (res.status === "ok") {
|
|
|
|
account = res.data;
|
|
|
|
account.organization = res.data2;
|
|
|
|
|
|
|
|
this.onUpdateAccount(account);
|
|
|
|
|
|
|
|
if (Setting.isPromptAnswered(account, application)) {
|
|
|
|
Setting.goToLink(`${oAuthParams.redirectUri}${concatChar}code=${code}&state=${oAuthParams.state}`);
|
|
|
|
} else {
|
|
|
|
Setting.goToLinkSoft(ths, `/prompt/${application.name}?redirectUri=${oAuthParams.redirectUri}&code=${code}&state=${oAuthParams.state}`);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Setting.showMessage("error", `Failed to sign in: ${res.msg}`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
2022-07-26 23:03:55 +08:00
|
|
|
if (noRedirect === "true") {
|
|
|
|
window.close();
|
|
|
|
const newWindow = window.open(`${oAuthParams.redirectUri}${concatChar}code=${code}&state=${oAuthParams.state}`);
|
|
|
|
if (newWindow) {
|
|
|
|
setInterval(() => {
|
|
|
|
if (!newWindow.closed) {
|
|
|
|
newWindow.close();
|
|
|
|
}
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Setting.goToLink(`${oAuthParams.redirectUri}${concatChar}code=${code}&state=${oAuthParams.state}`);
|
|
|
|
}
|
2022-04-04 00:09:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Util.showMessage("success", `Authorization code: ${res.data}`);
|
|
|
|
} else if (responseType === "token" || responseType === "id_token") {
|
|
|
|
const accessToken = res.data;
|
|
|
|
Setting.goToLink(`${oAuthParams.redirectUri}#${responseType}=${accessToken}?state=${oAuthParams.state}&token_type=bearer`);
|
2022-04-08 23:06:48 +08:00
|
|
|
} else if (responseType === "saml") {
|
|
|
|
const SAMLResponse = res.data;
|
|
|
|
const redirectUri = res.data2;
|
|
|
|
Setting.goToLink(`${redirectUri}?SAMLResponse=${encodeURIComponent(SAMLResponse)}&RelayState=${oAuthParams.relayState}`);
|
2022-04-04 00:09:04 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Util.showMessage("error", `Failed to log in: ${res.msg}`);
|
|
|
|
}
|
|
|
|
});
|
2022-07-10 15:45:55 +08:00
|
|
|
}
|
|
|
|
}
|
2021-02-13 12:15:19 +08:00
|
|
|
|
2021-12-13 19:49:30 +08:00
|
|
|
getSamlUrl(provider) {
|
2021-12-10 00:23:04 +08:00
|
|
|
const params = new URLSearchParams(this.props.location.search);
|
2022-08-08 23:35:24 +08:00
|
|
|
const clientId = params.get("client_id");
|
|
|
|
const application = params.get("state");
|
|
|
|
const realRedirectUri = params.get("redirect_uri");
|
|
|
|
const redirectUri = `${window.location.origin}/callback/saml`;
|
|
|
|
const providerName = provider.name;
|
|
|
|
const relayState = `${clientId}&${application}&${providerName}&${realRedirectUri}&${redirectUri}`;
|
2021-12-15 21:38:00 +08:00
|
|
|
AuthBackend.getSamlLogin(`${provider.owner}/${providerName}`, btoa(relayState)).then((res) => {
|
|
|
|
if (res.data2 === "POST") {
|
2022-07-10 15:45:55 +08:00
|
|
|
document.write(res.data);
|
2021-12-15 21:38:00 +08:00
|
|
|
} else {
|
2022-07-10 15:45:55 +08:00
|
|
|
window.location.href = res.data;
|
2021-12-15 21:38:00 +08:00
|
|
|
}
|
2021-12-06 21:46:50 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-14 22:42:58 +08:00
|
|
|
isProviderVisible(providerItem) {
|
|
|
|
if (this.state.mode === "signup") {
|
|
|
|
return Setting.isProviderVisibleForSignUp(providerItem);
|
|
|
|
} else {
|
|
|
|
return Setting.isProviderVisibleForSignIn(providerItem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-20 12:29:34 +08:00
|
|
|
renderForm(application) {
|
|
|
|
if (this.state.msg !== null) {
|
2022-07-10 15:45:55 +08:00
|
|
|
return Util.renderMessage(this.state.msg);
|
2021-03-20 11:34:04 +08:00
|
|
|
}
|
|
|
|
|
2021-06-14 22:42:58 +08:00
|
|
|
if (this.state.mode === "signup" && !application.enableSignUp) {
|
|
|
|
return (
|
|
|
|
<Result
|
|
|
|
status="error"
|
|
|
|
title="Sign Up Error"
|
|
|
|
subTitle={"The application does not allow to sign up new account"}
|
|
|
|
extra={[
|
2022-08-09 12:19:56 +08:00
|
|
|
<Link key="login" onClick={() => {
|
2021-06-14 22:42:58 +08:00
|
|
|
Setting.goToLogin(this, application);
|
|
|
|
}}>
|
|
|
|
<Button type="primary" key="signin">
|
|
|
|
Sign In
|
|
|
|
</Button>
|
2022-08-06 23:54:56 +08:00
|
|
|
</Link>,
|
2021-06-14 22:42:58 +08:00
|
|
|
]}
|
|
|
|
>
|
|
|
|
</Result>
|
2022-07-10 15:45:55 +08:00
|
|
|
);
|
2021-06-14 22:42:58 +08:00
|
|
|
}
|
|
|
|
|
2021-03-26 21:58:30 +08:00
|
|
|
if (application.enablePassword) {
|
|
|
|
return (
|
|
|
|
<Form
|
|
|
|
name="normal_login"
|
|
|
|
initialValues={{
|
|
|
|
organization: application.organization,
|
2021-06-20 22:17:03 +08:00
|
|
|
application: application.name,
|
2021-08-06 20:04:08 +08:00
|
|
|
autoSignin: true,
|
2021-03-26 21:58:30 +08:00
|
|
|
}}
|
2022-07-10 15:45:55 +08:00
|
|
|
onFinish={(values) => {this.onFinish(values);}}
|
2021-11-28 21:10:21 +08:00
|
|
|
style={{width: "300px"}}
|
2021-03-26 21:58:30 +08:00
|
|
|
size="large"
|
2021-02-13 12:15:19 +08:00
|
|
|
>
|
2021-06-21 01:01:16 +08:00
|
|
|
<Form.Item
|
|
|
|
style={{height: 0, visibility: "hidden"}}
|
|
|
|
name="application"
|
|
|
|
rules={[
|
|
|
|
{
|
|
|
|
required: true,
|
2022-07-10 15:45:55 +08:00
|
|
|
message: "Please input your application!",
|
2021-06-21 01:01:16 +08:00
|
|
|
},
|
|
|
|
]}
|
|
|
|
>
|
|
|
|
</Form.Item>
|
2021-04-28 21:25:58 +08:00
|
|
|
<Form.Item
|
|
|
|
style={{height: 0, visibility: "hidden"}}
|
|
|
|
name="organization"
|
|
|
|
rules={[
|
|
|
|
{
|
|
|
|
required: true,
|
2022-07-10 15:45:55 +08:00
|
|
|
message: "Please input your organization!",
|
2021-04-28 21:25:58 +08:00
|
|
|
},
|
|
|
|
]}
|
2021-03-26 21:58:30 +08:00
|
|
|
>
|
2021-02-13 12:15:19 +08:00
|
|
|
</Form.Item>
|
2022-07-12 20:06:01 +08:00
|
|
|
{this.renderMethodChoiceBox()}
|
2022-09-12 00:01:18 +08:00
|
|
|
<Row style={{minHeight: 130, alignItems: "center"}}>
|
|
|
|
<Col span={24}>
|
|
|
|
<Form.Item
|
|
|
|
name="username"
|
|
|
|
rules={[
|
|
|
|
{
|
|
|
|
required: true,
|
|
|
|
message: i18next.t("login:Please input your username, Email or phone!"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
validator: (_, value) => {
|
|
|
|
if (this.state.isCodeSignin) {
|
|
|
|
if (this.state.email !== "" && !Setting.isValidEmail(this.state.username) && !Setting.isValidPhone(this.state.username)) {
|
|
|
|
this.setState({validEmailOrPhone: false});
|
|
|
|
return Promise.reject(i18next.t("login:The input is not valid Email or Phone!"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Setting.isValidPhone(this.state.username)) {
|
|
|
|
this.setState({validPhone: true});
|
|
|
|
}
|
|
|
|
if (Setting.isValidEmail(this.state.username)) {
|
|
|
|
this.setState({validEmail: true});
|
|
|
|
}
|
|
|
|
}
|
2022-07-12 20:06:01 +08:00
|
|
|
|
2022-09-12 00:01:18 +08:00
|
|
|
this.setState({validEmailOrPhone: true});
|
|
|
|
return Promise.resolve();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
>
|
|
|
|
<Input
|
|
|
|
id = "input"
|
|
|
|
prefix={<UserOutlined className="site-form-item-icon" />}
|
|
|
|
placeholder={this.state.isCodeSignin ? i18next.t("login:Email or phone") : i18next.t("login:username, Email or phone")}
|
|
|
|
disabled={!application.enablePassword}
|
|
|
|
onChange={e => {
|
|
|
|
this.setState({
|
|
|
|
username: e.target.value,
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</Form.Item>
|
|
|
|
</Col>
|
|
|
|
{
|
|
|
|
this.renderPasswordOrCodeInput()
|
|
|
|
}
|
|
|
|
</Row>
|
2021-03-26 21:58:30 +08:00
|
|
|
<Form.Item>
|
2021-07-18 07:54:49 +08:00
|
|
|
<Form.Item name="autoSignin" valuePropName="checked" noStyle>
|
2021-03-26 21:58:30 +08:00
|
|
|
<Checkbox style={{float: "left"}} disabled={!application.enablePassword}>
|
2021-08-06 20:04:08 +08:00
|
|
|
{i18next.t("login:Auto sign in")}
|
2021-03-26 21:58:30 +08:00
|
|
|
</Checkbox>
|
|
|
|
</Form.Item>
|
2022-06-17 19:57:11 +08:00
|
|
|
<a style={{float: "right"}} onClick={() => {
|
2021-05-03 00:48:02 +08:00
|
|
|
Setting.goToForget(this, application);
|
|
|
|
}}>
|
2021-06-04 21:09:34 +08:00
|
|
|
{i18next.t("login:Forgot password?")}
|
|
|
|
</a>
|
2021-03-26 21:58:30 +08:00
|
|
|
</Form.Item>
|
|
|
|
<Form.Item>
|
2022-07-12 20:06:01 +08:00
|
|
|
{
|
|
|
|
this.state.loginMethod === "password" ?
|
|
|
|
(
|
|
|
|
<Button
|
|
|
|
type="primary"
|
|
|
|
htmlType="submit"
|
2022-07-12 20:45:22 +08:00
|
|
|
style={{width: "100%", marginBottom: "5px"}}
|
2022-07-12 20:06:01 +08:00
|
|
|
disabled={!application.enablePassword}
|
|
|
|
>
|
|
|
|
{i18next.t("login:Sign In")}
|
|
|
|
</Button>
|
|
|
|
) :
|
|
|
|
(
|
2022-08-06 23:36:20 +08:00
|
|
|
<Button
|
|
|
|
type="primary"
|
|
|
|
htmlType="submit"
|
|
|
|
style={{width: "100%", marginBottom: "5px"}}
|
|
|
|
disabled={!application.enablePassword}
|
|
|
|
>
|
|
|
|
{i18next.t("login:Sign in with WebAuthn")}
|
|
|
|
</Button>
|
2022-07-12 20:06:01 +08:00
|
|
|
)
|
|
|
|
}
|
2021-03-26 21:58:30 +08:00
|
|
|
{
|
2022-07-28 23:11:33 +08:00
|
|
|
this.renderFooter(application)
|
2021-03-26 21:58:30 +08:00
|
|
|
}
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item>
|
|
|
|
{
|
2021-06-14 22:42:58 +08:00
|
|
|
application.providers.filter(providerItem => this.isProviderVisible(providerItem)).map(providerItem => {
|
2022-08-05 18:59:56 +08:00
|
|
|
return ProviderButton.renderProviderLogo(providerItem.provider, application, 30, 5, "small");
|
2021-03-26 21:58:30 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
</Form.Item>
|
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<div style={{marginTop: "20px"}}>
|
2021-03-28 19:15:10 +08:00
|
|
|
<div style={{fontSize: 16, textAlign: "left"}}>
|
2021-06-11 20:33:22 +08:00
|
|
|
{i18next.t("login:To access")}
|
2021-03-27 11:38:15 +08:00
|
|
|
<a target="_blank" rel="noreferrer" href={application.homepageUrl}>
|
2021-03-29 22:11:28 +08:00
|
|
|
<span style={{fontWeight: "bold"}}>
|
|
|
|
{application.displayName}
|
|
|
|
</span>
|
2021-03-26 21:58:30 +08:00
|
|
|
</a>
|
2021-06-11 20:33:22 +08:00
|
|
|
:
|
2021-02-13 12:15:19 +08:00
|
|
|
</div>
|
2022-07-10 15:45:55 +08:00
|
|
|
<br />
|
2021-02-13 23:00:43 +08:00
|
|
|
{
|
2021-06-14 22:42:58 +08:00
|
|
|
application.providers.filter(providerItem => this.isProviderVisible(providerItem)).map(providerItem => {
|
2022-08-05 18:59:56 +08:00
|
|
|
return ProviderButton.renderProviderLogo(providerItem.provider, application, 40, 10, "big");
|
2021-02-13 23:00:43 +08:00
|
|
|
})
|
|
|
|
}
|
2022-07-28 23:11:33 +08:00
|
|
|
<div>
|
|
|
|
<br />
|
|
|
|
{
|
|
|
|
this.renderFooter(application)
|
|
|
|
}
|
|
|
|
</div>
|
2021-03-26 21:58:30 +08:00
|
|
|
</div>
|
2022-07-10 15:45:55 +08:00
|
|
|
);
|
2021-03-26 21:58:30 +08:00
|
|
|
}
|
2021-02-13 12:15:19 +08:00
|
|
|
}
|
|
|
|
|
2021-06-14 22:42:58 +08:00
|
|
|
renderFooter(application) {
|
|
|
|
if (this.state.mode === "signup") {
|
|
|
|
return (
|
|
|
|
<div style={{float: "right"}}>
|
|
|
|
{i18next.t("signup:Have account?")}
|
|
|
|
<Link onClick={() => {
|
|
|
|
Setting.goToLogin(this, application);
|
|
|
|
}}>
|
|
|
|
{i18next.t("signup:sign in now")}
|
|
|
|
</Link>
|
|
|
|
</div>
|
2022-07-10 15:45:55 +08:00
|
|
|
);
|
2021-06-14 22:42:58 +08:00
|
|
|
} else {
|
|
|
|
return (
|
2021-11-28 21:10:21 +08:00
|
|
|
<React.Fragment>
|
|
|
|
<span style={{float: "left"}}>
|
2021-12-31 13:36:10 +08:00
|
|
|
{
|
|
|
|
!application.enableCodeSignin ? null : (
|
2022-06-17 19:57:11 +08:00
|
|
|
<a onClick={() => {
|
2021-12-31 13:36:10 +08:00
|
|
|
this.setState({
|
|
|
|
isCodeSignin: !this.state.isCodeSignin,
|
|
|
|
});
|
|
|
|
}}>
|
|
|
|
{this.state.isCodeSignin ? i18next.t("login:Sign in with password") : i18next.t("login:Sign in with code")}
|
|
|
|
</a>
|
|
|
|
)
|
|
|
|
}
|
2021-11-28 21:10:21 +08:00
|
|
|
</span>
|
|
|
|
<span style={{float: "right"}}>
|
2022-07-28 23:11:33 +08:00
|
|
|
{
|
|
|
|
!application.enableSignUp ? null : (
|
|
|
|
<>
|
|
|
|
{i18next.t("login:No account?")}
|
|
|
|
<a onClick={() => {
|
|
|
|
sessionStorage.setItem("signinUrl", window.location.href);
|
|
|
|
Setting.goToSignup(this, application);
|
|
|
|
}}>
|
|
|
|
{i18next.t("login:sign up now")}
|
|
|
|
</a>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
2021-11-28 21:10:21 +08:00
|
|
|
</span>
|
|
|
|
</React.Fragment>
|
2022-07-10 15:45:55 +08:00
|
|
|
);
|
2021-06-14 22:42:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-09 21:15:57 +08:00
|
|
|
renderSignedInBox() {
|
|
|
|
if (this.props.account === undefined || this.props.account === null) {
|
2022-08-22 01:17:18 +08:00
|
|
|
if (window !== window.parent) {
|
|
|
|
const message = {tag: "Casdoor", type: "SilentSignin", data: "user-not-logged-in"};
|
|
|
|
window.parent.postMessage(message, "*");
|
|
|
|
}
|
2021-10-09 21:15:57 +08:00
|
|
|
return null;
|
|
|
|
}
|
2022-08-08 23:35:24 +08:00
|
|
|
const application = this.getApplicationObj();
|
2021-11-11 20:43:26 +08:00
|
|
|
if (this.props.account.owner !== application.organization) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-10-09 21:15:57 +08:00
|
|
|
|
2022-08-22 15:13:58 +08:00
|
|
|
const params = new URLSearchParams(this.props.location.search);
|
|
|
|
const silentSignin = params.get("silentSignin");
|
2022-01-23 13:02:55 +08:00
|
|
|
if (silentSignin !== null) {
|
|
|
|
if (window !== window.parent) {
|
|
|
|
const message = {tag: "Casdoor", type: "SilentSignin", data: "signing-in"};
|
|
|
|
window.parent.postMessage(message, "*");
|
|
|
|
}
|
|
|
|
|
2022-08-08 23:35:24 +08:00
|
|
|
const values = {};
|
2022-01-23 13:02:55 +08:00
|
|
|
values["application"] = this.state.application.name;
|
|
|
|
this.onFinish(values);
|
|
|
|
}
|
|
|
|
|
2021-10-09 21:15:57 +08:00
|
|
|
return (
|
|
|
|
<div>
|
2022-07-10 15:45:55 +08:00
|
|
|
{/* {*/}
|
2022-04-25 20:00:57 +08:00
|
|
|
{/* JSON.stringify(silentSignin)*/}
|
2022-07-10 15:45:55 +08:00
|
|
|
{/* }*/}
|
2021-10-09 21:15:57 +08:00
|
|
|
<div style={{fontSize: 16, textAlign: "left"}}>
|
|
|
|
{i18next.t("login:Continue with")} :
|
|
|
|
</div>
|
2022-07-10 15:45:55 +08:00
|
|
|
<br />
|
2021-10-09 21:15:57 +08:00
|
|
|
<SelfLoginButton account={this.props.account} onClick={() => {
|
2022-08-08 23:35:24 +08:00
|
|
|
const values = {};
|
2021-10-09 21:15:57 +08:00
|
|
|
values["application"] = this.state.application.name;
|
|
|
|
this.onFinish(values);
|
|
|
|
}} />
|
2022-07-10 15:45:55 +08:00
|
|
|
<br />
|
|
|
|
<br />
|
2021-10-09 21:15:57 +08:00
|
|
|
<div style={{fontSize: 16, textAlign: "left"}}>
|
|
|
|
{i18next.t("login:Or sign in with another account")} :
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-07-10 15:45:55 +08:00
|
|
|
);
|
2021-10-09 21:15:57 +08:00
|
|
|
}
|
|
|
|
|
2022-08-05 17:43:04 +08:00
|
|
|
signInWithWebAuthn(username) {
|
|
|
|
if (username === null || username === "") {
|
2022-07-12 20:06:01 +08:00
|
|
|
Setting.showMessage("error", "username is required for webauthn login");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-08 23:35:24 +08:00
|
|
|
const application = this.getApplicationObj();
|
2022-08-05 17:43:04 +08:00
|
|
|
return fetch(`${Setting.ServerUrl}/api/webauthn/signin/begin?owner=${application.organization}&name=${username}`, {
|
2022-07-12 20:06:01 +08:00
|
|
|
method: "GET",
|
2022-08-06 23:54:56 +08:00
|
|
|
credentials: "include",
|
2022-07-12 20:06:01 +08:00
|
|
|
})
|
|
|
|
.then(res => res.json())
|
|
|
|
.then((credentialRequestOptions) => {
|
|
|
|
if ("status" in credentialRequestOptions) {
|
|
|
|
Setting.showMessage("error", credentialRequestOptions.msg);
|
|
|
|
throw credentialRequestOptions.status.msg;
|
|
|
|
}
|
|
|
|
|
2022-08-05 17:43:04 +08:00
|
|
|
credentialRequestOptions.publicKey.challenge = UserWebauthnBackend.webAuthnBufferDecode(credentialRequestOptions.publicKey.challenge);
|
|
|
|
credentialRequestOptions.publicKey.allowCredentials.forEach(function(listItem) {
|
2022-07-12 20:06:01 +08:00
|
|
|
listItem.id = UserWebauthnBackend.webAuthnBufferDecode(listItem.id);
|
|
|
|
});
|
|
|
|
|
|
|
|
return navigator.credentials.get({
|
2022-08-06 23:54:56 +08:00
|
|
|
publicKey: credentialRequestOptions.publicKey,
|
2022-07-12 20:45:22 +08:00
|
|
|
});
|
2022-07-12 20:06:01 +08:00
|
|
|
})
|
|
|
|
.then((assertion) => {
|
2022-08-08 23:35:24 +08:00
|
|
|
const authData = assertion.response.authenticatorData;
|
|
|
|
const clientDataJSON = assertion.response.clientDataJSON;
|
|
|
|
const rawId = assertion.rawId;
|
|
|
|
const sig = assertion.response.signature;
|
|
|
|
const userHandle = assertion.response.userHandle;
|
2022-07-12 20:06:01 +08:00
|
|
|
return fetch(`${Setting.ServerUrl}/api/webauthn/signin/finish`, {
|
|
|
|
method: "POST",
|
|
|
|
credentials: "include",
|
|
|
|
body: JSON.stringify({
|
|
|
|
id: assertion.id,
|
|
|
|
rawId: UserWebauthnBackend.webAuthnBufferEncode(rawId),
|
|
|
|
type: assertion.type,
|
|
|
|
response: {
|
|
|
|
authenticatorData: UserWebauthnBackend.webAuthnBufferEncode(authData),
|
|
|
|
clientDataJSON: UserWebauthnBackend.webAuthnBufferEncode(clientDataJSON),
|
|
|
|
signature: UserWebauthnBackend.webAuthnBufferEncode(sig),
|
|
|
|
userHandle: UserWebauthnBackend.webAuthnBufferEncode(userHandle),
|
|
|
|
},
|
2022-08-06 23:54:56 +08:00
|
|
|
}),
|
2022-07-12 20:06:01 +08:00
|
|
|
})
|
|
|
|
.then(res => res.json()).then((res) => {
|
|
|
|
if (res.msg === "") {
|
2022-07-12 20:45:22 +08:00
|
|
|
Setting.showMessage("success", "Successfully logged in with webauthn credentials");
|
2022-07-12 20:06:01 +08:00
|
|
|
Setting.goToLink("/");
|
|
|
|
} else {
|
|
|
|
Setting.showMessage("error", res.msg);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
Setting.showMessage("error", `Failed to connect to server: ${error}`);
|
|
|
|
});
|
2022-07-12 20:45:22 +08:00
|
|
|
});
|
2022-07-12 20:06:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
renderPasswordOrCodeInput() {
|
2022-08-08 23:35:24 +08:00
|
|
|
const application = this.getApplicationObj();
|
2022-07-12 20:06:01 +08:00
|
|
|
if (this.state.loginMethod === "password") {
|
|
|
|
return this.state.isCodeSignin ? (
|
2022-09-12 00:01:18 +08:00
|
|
|
<Col span={24}>
|
|
|
|
<Form.Item
|
|
|
|
name="code"
|
|
|
|
rules={[{required: true, message: i18next.t("login:Please input your code!")}]}
|
|
|
|
>
|
|
|
|
<CountDownInput
|
|
|
|
disabled={this.state.username?.length === 0 || !this.state.validEmailOrPhone}
|
|
|
|
onButtonClickArgs={[this.state.username, this.state.validEmail ? "email" : "phone", Setting.getApplicationName(application)]}
|
|
|
|
application={application}
|
|
|
|
/>
|
|
|
|
</Form.Item>
|
|
|
|
</Col>
|
2022-07-12 20:06:01 +08:00
|
|
|
) : (
|
2022-09-12 00:01:18 +08:00
|
|
|
<Col span={24}>
|
|
|
|
<Form.Item
|
|
|
|
name="password"
|
|
|
|
rules={[{required: true, message: i18next.t("login:Please input your password!")}]}
|
|
|
|
>
|
|
|
|
<Input
|
|
|
|
prefix={<LockOutlined className="site-form-item-icon" />}
|
|
|
|
type="password"
|
|
|
|
placeholder={i18next.t("login:Password")}
|
|
|
|
disabled={!application.enablePassword}
|
|
|
|
/>
|
|
|
|
</Form.Item>
|
|
|
|
</Col>
|
2022-07-12 20:45:22 +08:00
|
|
|
);
|
2022-07-12 20:06:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-12 20:45:22 +08:00
|
|
|
renderMethodChoiceBox() {
|
2022-08-08 23:35:24 +08:00
|
|
|
const application = this.getApplicationObj();
|
2022-07-12 20:06:01 +08:00
|
|
|
if (application.enableWebAuthn) {
|
|
|
|
return (
|
|
|
|
<div>
|
2022-07-12 20:45:22 +08:00
|
|
|
<Tabs defaultActiveKey="password" onChange={(key) => {this.setState({loginMethod: key});}} centered>
|
|
|
|
<TabPane tab={i18next.t("login:Password")} key="password" />
|
|
|
|
<TabPane tab={"WebAuthn"} key="webAuthn" />
|
2022-07-12 20:06:01 +08:00
|
|
|
</Tabs>
|
|
|
|
</div>
|
2022-07-12 20:45:22 +08:00
|
|
|
);
|
2022-07-12 20:06:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-13 12:15:19 +08:00
|
|
|
render() {
|
|
|
|
const application = this.getApplicationObj();
|
|
|
|
if (application === null) {
|
2021-03-26 21:58:19 +08:00
|
|
|
return Util.renderMessageLarge(this, this.state.msg);
|
2021-02-13 12:15:19 +08:00
|
|
|
}
|
|
|
|
|
2021-10-26 13:11:21 +08:00
|
|
|
if (application.signinHtml !== "") {
|
|
|
|
return (
|
2022-07-10 15:45:55 +08:00
|
|
|
<div dangerouslySetInnerHTML={{__html: application.signinHtml}} />
|
|
|
|
);
|
2021-10-26 13:11:21 +08:00
|
|
|
}
|
|
|
|
|
2021-08-07 00:14:24 +08:00
|
|
|
const visibleOAuthProviderItems = application.providers.filter(providerItem => this.isProviderVisible(providerItem));
|
2021-09-20 22:55:17 +08:00
|
|
|
if (this.props.application === undefined && !application.enablePassword && visibleOAuthProviderItems.length === 1) {
|
2021-08-07 00:14:24 +08:00
|
|
|
Setting.goToLink(Provider.getAuthUrl(application, visibleOAuthProviderItems[0].provider, "signup"));
|
|
|
|
return (
|
|
|
|
<div style={{textAlign: "center"}}>
|
|
|
|
<Spin size="large" tip={i18next.t("login:Signing in...")} style={{paddingTop: "10%"}} />
|
|
|
|
</div>
|
2022-07-10 15:45:55 +08:00
|
|
|
);
|
2021-08-07 00:14:24 +08:00
|
|
|
}
|
|
|
|
|
2022-09-12 00:01:18 +08:00
|
|
|
const formStyle = Setting.inIframe() ? null : Setting.parseObject(application.formCss);
|
2022-09-10 00:56:37 +08:00
|
|
|
|
2021-02-13 12:15:19 +08:00
|
|
|
return (
|
2022-09-20 23:06:24 +08:00
|
|
|
<div className="loginBackground" style={{backgroundImage: Setting.inIframe() || Setting.isMobile() ? null : `url(${application.formBackgroundUrl})`}}>
|
2022-09-12 00:01:18 +08:00
|
|
|
<CustomGithubCorner />
|
2022-09-20 23:06:24 +08:00
|
|
|
<Row >
|
|
|
|
<Col span={8} offset={application.formOffset === 0 || Setting.inIframe() || Setting.isMobile() ? 8 : application.formOffset} style={{display: "flex", justifyContent: "center"}}>
|
2022-09-10 00:56:37 +08:00
|
|
|
<div style={{marginTop: "80px", marginBottom: "50px", textAlign: "center", ...formStyle}}>
|
2022-09-12 00:01:18 +08:00
|
|
|
<div>
|
|
|
|
{
|
|
|
|
Setting.renderHelmet(application)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
Setting.renderLogo(application)
|
|
|
|
}
|
|
|
|
{/* {*/}
|
|
|
|
{/* this.state.clientId !== null ? "Redirect" : null*/}
|
|
|
|
{/* }*/}
|
|
|
|
{
|
|
|
|
this.renderSignedInBox()
|
|
|
|
}
|
|
|
|
{
|
|
|
|
this.renderForm(application)
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
|
2022-09-10 00:56:37 +08:00
|
|
|
</div>
|
|
|
|
</Col>
|
|
|
|
</Row>
|
|
|
|
</div>
|
2022-07-10 15:45:55 +08:00
|
|
|
);
|
2021-02-13 12:15:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-26 21:56:51 +08:00
|
|
|
export default LoginPage;
|