Login: fix consent for public client

This commit is contained in:
Timofey Boyko 2024-06-27 18:21:13 +03:00
parent def151fe66
commit 11b5de43c8
7 changed files with 45 additions and 34 deletions

View File

@ -27,8 +27,9 @@
import React, { useEffect } from "react";
import { Loader } from "@docspace/shared/components/loader";
import Section from "@docspace/shared/components/section";
import { getCookie, deleteCookie } from "@docspace/shared/utils/cookie";
import { loginWithConfirmKey } from "@docspace/shared/api/user";
import { useSearchParams } from "react-router-dom";
import { useSearchParams, useLocation } from "react-router-dom";
import { combineUrl } from "@docspace/shared/utils/combineUrl";
import { toastr } from "@docspace/shared/components/toast";
import { frameCallEvent } from "@docspace/shared/utils/common";
@ -37,6 +38,7 @@ const Auth = (props) => {
//console.log("Auth render");
const { linkData } = props;
let [searchParams, setSearchParams] = useSearchParams();
const location = useLocation();
useEffect(() => {
loginWithConfirmKey({
ConfirmData: {
@ -50,6 +52,22 @@ const Auth = (props) => {
const url = searchParams.get("referenceUrl");
const redirectUrl = getCookie("x-redirect-authorization-uri");
deleteCookie("x-redirect-authorization-uri");
if (redirectUrl) {
window.location.replace(redirectUrl);
return;
}
if (url && url.includes("oauth2")) {
const newUrl = location.search.split("referenceUrl=")[1];
window.location.replace(newUrl);
return;
}
if (url) {
try {
new URL(url);

View File

@ -110,7 +110,6 @@ const Consent = ({ client, scopes, user }: IConsentProps) => {
let clientState = "";
console.log(clientState);
const scope = client.scopes;
const cookie = document.cookie.split(";");
@ -120,14 +119,6 @@ const Consent = ({ client, scopes, user }: IConsentProps) => {
clientState = c.replace("client_state=", "").trim();
});
deleteCookie("client_state");
console.log(clientState, "run");
const state = await api.oauth.onOAuthLogin(clientId);
console.log(state);
await api.oauth.onOAuthSubmit(clientId, clientState, scope);
setIsAllowRunning(false);
@ -144,8 +135,6 @@ const Consent = ({ client, scopes, user }: IConsentProps) => {
let clientState = "";
// await api.oauth.onOAuthLogin(clientId);
const cookie = document.cookie.split(";");
cookie.forEach((c) => {

View File

@ -49,9 +49,10 @@ import { toastr } from "@docspace/shared/components/toast";
import { thirdPartyLogin } from "@docspace/shared/api/user";
import { setWithCredentialsStatus } from "@docspace/shared/api/client";
import { TValidate } from "@docspace/shared/components/email-input/EmailInput.types";
import api from "@docspace/shared/api";
import { RecaptchaType } from "@docspace/shared/enums";
import { getAvailablePortals } from "@docspace/shared/api/management";
import { getCookie } from "@docspace/shared/utils";
import { deleteCookie } from "@docspace/shared/utils/cookie";
import { LoginFormProps } from "@/types";
import { generateOAuth2ReferenceURl, getEmailFromInvitation } from "@/utils";
@ -64,6 +65,7 @@ import LDAPContainer from "./sub-components/LDAPContainer";
import { StyledCaptcha } from "./LoginForm.styled";
import { LoginDispatchContext, LoginValueContext } from "../Login";
import OAuthClientInfo from "../ConsentInfo";
// import { gitAvailablePortals } from "@/utils/actions";
const LoginForm = ({
@ -263,11 +265,9 @@ const LoginForm = ({
});
if (portals.length === 1) {
const referenceUrl = generateOAuth2ReferenceURl(client.clientId);
window.open(
`${portals[0].portalLink}&referenceUrl=${referenceUrl}`,
"_self",
);
window.open(`${portals[0].portalLink}`, "_self");
return;
}
const searchParams = new URLSearchParams();
@ -284,10 +284,11 @@ const LoginForm = ({
login(user, hash, pwd, session, captchaToken, currentCulture, reCaptchaType)
.then(async (res: string | object) => {
if (clientId) {
await api.oauth.onOAuthLogin(clientId);
const redirectUrl = getCookie("x-redirect-authorization-uri");
if (clientId && redirectUrl) {
deleteCookie("x-redirect-authorization-uri");
router.push(`/login/consent?clientId=${clientId}`);
window.location.replace(redirectUrl);
return;
}

View File

@ -9,5 +9,4 @@ export type TenantListProps = {
export type ItemProps = {
portal: TPortal;
baseDomain: string;
clientId: string;
};

View File

@ -23,12 +23,7 @@ const TenantList = ({ portals, clientId, baseDomain }: TenantListProps) => {
</Text>
<div className="items-list">
{portals.map((item) => (
<Item
portal={item}
key={item.portalName}
clientId={clientId}
baseDomain={baseDomain}
/>
<Item portal={item} key={item.portalName} baseDomain={baseDomain} />
))}
</div>
<Button

View File

@ -1,23 +1,26 @@
/* eslint-disable @next/next/no-img-element */
import { Text } from "@docspace/shared/components/text";
import { IconButton } from "@docspace/shared/components/icon-button";
import { deleteCookie, getCookie } from "@docspace/shared/utils/cookie";
import ArrowRightSvrUrl from "PUBLIC_DIR/images/arrow.right.react.svg?url";
import { ItemProps } from "../TenantList.types";
import { IconButton } from "@docspace/shared/components/icon-button";
import { generateOAuth2ReferenceURl } from "@/utils";
const Item = ({ clientId, portal, baseDomain }: ItemProps) => {
console.log(portal);
const Item = ({ portal, baseDomain }: ItemProps) => {
const name = portal.portalName.includes(baseDomain)
? portal.portalName
: `${portal.portalName}.${baseDomain}`;
const onClick = () => {
const referenceUrl = generateOAuth2ReferenceURl(clientId);
const redirectUrl = getCookie("x-redirect-authorization-uri")?.replace(
window.location.origin,
name,
);
deleteCookie("x-redirect-authorization-uri");
window.open(`${portal.portalLink}&referenceUrl=${referenceUrl}`, "_self");
window.open(`${portal.portalLink}&referenceUrl=${redirectUrl}`, "_self");
};
return (

View File

@ -163,6 +163,9 @@ export const onOAuthSubmit = (
url: `/oauth2/authorize`,
data: formData,
withRedirect: true,
headers: {
"X-Disable-Redirect": "true",
},
});
};
@ -177,5 +180,8 @@ export const onOAuthCancel = (clientId: string, clientState: string) => {
url: `/oauth2/authorize`,
data: formData,
withRedirect: true,
headers: {
"X-Disable-Redirect": "true",
},
});
};