Files
casdoor/web/src/auth/Util.js

228 lines
6.7 KiB
JavaScript
Raw Normal View History

2022-02-13 23:39:27 +08:00
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
2021-02-14 16:59:08 +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-03-25 23:22:34 +08:00
import React from "react";
import {Alert, Button, Modal, Result} from "antd";
2023-04-28 18:43:41 +08:00
import i18next from "i18next";
import {getWechatMessageEvent} from "./AuthBackend";
import * as Setting from "../Setting";
import * as Provider from "./Provider";
import * as AuthBackend from "./AuthBackend";
2021-02-14 16:59:08 +08:00
2021-03-25 23:22:34 +08:00
export function renderMessage(msg) {
if (msg !== null) {
return (
<div style={{display: "inline"}}>
<Alert
2023-04-28 18:43:41 +08:00
message={i18next.t("application:Failed to sign in")}
2021-03-25 23:22:34 +08:00
showIcon
description={msg}
type="error"
action={
<Button size="small" type="primary" danger>
2023-04-28 18:43:41 +08:00
{i18next.t("product:Detail")}
2021-03-25 23:22:34 +08:00
</Button>
}
/>
</div>
);
2021-03-25 23:22:34 +08:00
} else {
return null;
}
}
2021-03-26 21:58:19 +08:00
export function renderMessageLarge(ths, msg) {
2021-03-25 23:22:34 +08:00
if (msg !== null) {
return (
<Result
style={{margin: "0px auto"}}
status="error"
2023-04-28 18:43:41 +08:00
title={i18next.t("general:There was a problem signing you in..")}
subTitle={msg}
extra={[
<Button type="primary" key="back" onClick={() => {
window.history.go(-2);
}}>
2023-04-28 18:43:41 +08:00
{i18next.t("general:Back")}
</Button>,
]}
>
</Result>
);
2021-03-25 23:22:34 +08:00
} else {
return null;
}
}
function getRefinedValue(value) {
return value ?? "";
}
export function getCasParameters(params) {
const queries = (params !== undefined) ? params : new URLSearchParams(window.location.search);
const service = getRefinedValue(queries.get("service"));
const renew = getRefinedValue(queries.get("renew"));
const gateway = getRefinedValue(queries.get("gateway"));
return {
service: service,
renew: renew,
gateway: gateway,
};
}
2023-05-25 09:47:39 +08:00
function getRawGetParameter(key) {
2023-05-24 21:29:45 +08:00
const fullUrl = window.location.href;
2023-05-25 09:47:39 +08:00
const token = fullUrl.split(`${key}=`)[1];
2023-05-24 21:29:45 +08:00
if (!token) {
return "";
}
2023-05-24 23:22:25 +08:00
let res = token.split("&")[0];
2023-05-24 21:29:45 +08:00
if (!res) {
return "";
}
2023-05-24 23:22:25 +08:00
res = decodeURIComponent(res);
2023-05-24 21:29:45 +08:00
return res;
}
export function getCasLoginParameters(owner, name) {
const queries = new URLSearchParams(window.location.search);
// CAS service
let service = getRawGetParameter("service");
if (service === "") {
service = getRefinedValue(queries.get("service"));
}
return {
id: `${owner}/${encodeURIComponent(name)}`, // application ID,
service: service,
type: "cas",
};
}
2021-03-21 13:45:55 +08:00
export function getOAuthGetParameters(params) {
const queries = (params !== undefined) ? params : new URLSearchParams(window.location.search);
const lowercaseQueries = {};
queries.forEach((val, key) => {lowercaseQueries[key.toLowerCase()] = val;});
const clientId = getRefinedValue(queries.get("client_id"));
const responseType = getRefinedValue(queries.get("response_type"));
2023-05-24 21:29:45 +08:00
2023-05-25 09:47:39 +08:00
let redirectUri = getRawGetParameter("redirect_uri");
2023-05-24 21:29:45 +08:00
if (redirectUri === "") {
redirectUri = getRefinedValue(queries.get("redirect_uri"));
}
2023-05-25 09:47:39 +08:00
let scope = getRefinedValue(queries.get("scope"));
if (redirectUri.includes("#") && scope === "") {
scope = getRawGetParameter("scope");
}
2023-05-21 15:47:18 +08:00
let state = getRefinedValue(queries.get("state"));
if (state.startsWith("/auth/oauth2/login.php?wantsurl=")) {
// state contains URL param encoding for Moodle, URLSearchParams automatically decoded it, so here encode it again
state = encodeURIComponent(state);
}
2023-05-25 09:47:39 +08:00
if (redirectUri.includes("#") && state === "") {
state = getRawGetParameter("state");
}
2023-05-21 15:47:18 +08:00
const nonce = getRefinedValue(queries.get("nonce"));
const challengeMethod = getRefinedValue(queries.get("code_challenge_method"));
const codeChallenge = getRefinedValue(queries.get("code_challenge"));
const samlRequest = getRefinedValue(lowercaseQueries["samlRequest".toLowerCase()]);
const relayState = getRefinedValue(lowercaseQueries["RelayState".toLowerCase()]);
const noRedirect = getRefinedValue(lowercaseQueries["noRedirect".toLowerCase()]);
if (clientId === "" && samlRequest === "") {
2021-08-01 11:20:06 +08:00
// login
2021-03-20 16:51:10 +08:00
return null;
} else {
2021-08-01 11:20:06 +08:00
// code
2021-03-20 16:51:10 +08:00
return {
clientId: clientId,
responseType: responseType,
redirectUri: redirectUri,
scope: scope,
state: state,
nonce: nonce,
challengeMethod: challengeMethod,
codeChallenge: codeChallenge,
samlRequest: samlRequest,
relayState: relayState,
noRedirect: noRedirect,
type: "code",
2021-03-20 16:51:10 +08:00
};
}
}
2021-03-21 13:45:55 +08:00
export function getStateFromQueryParams(applicationName, providerName, method, isShortState) {
2021-03-21 16:05:00 +08:00
let query = window.location.search;
query = `${query}&application=${encodeURIComponent(applicationName)}&provider=${encodeURIComponent(providerName)}&method=${method}`;
2021-04-19 01:14:41 +08:00
if (method === "link") {
query = `${query}&from=${window.location.pathname}`;
}
if (!isShortState) {
return btoa(query);
} else {
const state = providerName;
sessionStorage.setItem(state, query);
return state;
}
2021-03-21 13:45:55 +08:00
}
export function getQueryParamsFromState(state) {
const query = sessionStorage.getItem(state);
if (query === null) {
return atob(state);
} else {
return query;
}
2021-03-21 13:45:55 +08:00
}
export function getEvent(application, provider, ticket, method) {
getWechatMessageEvent(ticket)
.then(res => {
if (res.data === "SCAN" || res.data === "subscribe") {
const code = res?.data2;
Setting.goToLink(Provider.getAuthUrl(application, provider, method ?? "signup", code));
}
});
}
export async function WechatOfficialAccountModal(application, provider, method) {
AuthBackend.getWechatQRCode(`${provider.owner}/${provider.name}`).then(
async res => {
if (res.status !== "ok") {
Setting.showMessage("error", res?.msg);
return;
}
const t1 = setInterval(await getEvent, 1000, application, provider, res.data2, method);
{Modal.info({
title: i18next.t("provider:Please use WeChat to scan the QR code and follow the official account for sign in"),
content: (
<div style={{marginRight: "34px"}}>
<img src = {"data:image/png;base64," + res.data} alt="Wechat QR code" style={{width: "100%"}} />
</div>
),
onOk() {
window.clearInterval(t1);
},
});}
}
);
}