From f129e8f1dccedd218d21a7c20c952ac4b9b34e5b Mon Sep 17 00:00:00 2001 From: Timofey Boyko Date: Thu, 18 Jan 2024 16:03:01 +0300 Subject: [PATCH] Client: fix after merge develop --- packages/client/src/store/OAuthStore.ts | 50 ++++++++++++------- .../components/sub-components/Consent.tsx | 10 +--- packages/runtime.json | 2 +- packages/shared/api/oauth/index.ts | 22 +++++--- packages/shared/api/oauth/types.ts | 0 packages/shared/utils/axiosClient.ts | 9 ++-- packages/shared/utils/oauth/interfaces.ts | 33 ++++++++++++ 7 files changed, 87 insertions(+), 39 deletions(-) delete mode 100644 packages/shared/api/oauth/types.ts diff --git a/packages/client/src/store/OAuthStore.ts b/packages/client/src/store/OAuthStore.ts index 9c5be07880..6195021bbb 100644 --- a/packages/client/src/store/OAuthStore.ts +++ b/packages/client/src/store/OAuthStore.ts @@ -12,7 +12,7 @@ import { getScopeList, getConsentList, revokeUserClient, -} from "@docspace/common/api/oauth"; +} from "@docspace/shared/api/oauth"; import { IClientListProps, @@ -20,10 +20,9 @@ import { IClientReqDTO, INoAuthClientProps, IScope, -} from "@docspace/common/utils/oauth/interfaces"; +} from "@docspace/shared/utils/oauth/interfaces"; -//@ts-ignore -import toastr from "@docspace/components/toast/toastr"; +import { toastr } from "@docspace/shared/components/toast"; import SettingsIcon from "PUBLIC_DIR/images/catalog.settings.react.svg?url"; import DeleteIcon from "PUBLIC_DIR/images/delete.react.svg?url"; @@ -33,8 +32,9 @@ import PencilReactSvgUrl from "PUBLIC_DIR/images/pencil.react.svg?url"; import CodeReactSvgUrl from "PUBLIC_DIR/images/code.react.svg?url"; import ExternalLinkReactSvgUrl from "PUBLIC_DIR/images/external.link.react.svg?url"; import OauthRevokeSvgUrl from "PUBLIC_DIR/images/oauth.revoke.svg?url"; -import { transformToClientProps } from "@docspace/common/utils/oauth"; -import { AuthenticationMethod } from "@docspace/common/utils/oauth/enums"; +import { transformToClientProps } from "@docspace/shared/utils/oauth"; +import { AuthenticationMethod } from "@docspace/shared/enums"; +import { TData } from "@docspace/shared/components/toast/Toast.type"; const PAGE_LIMIT = 100; @@ -260,8 +260,9 @@ class OAuthStore implements OAuthStoreProps { const client = await getClient(clientId); return client; - } catch (e) { - toastr.error(e); + } catch (e: unknown) { + const err = e as TData; + toastr.error(err); console.log(e); } }; @@ -284,7 +285,8 @@ class OAuthStore implements OAuthStoreProps { }); this.setClientsIsLoading(false); } catch (e) { - toastr.error(e); + const err = e as TData; + toastr.error(err); console.log(e); } }; @@ -297,7 +299,8 @@ class OAuthStore implements OAuthStoreProps { this.consents = [...consentList]; }); } catch (e) { - toastr.error(e); + const err = e as TData; + toastr.error(err); console.log(e); } }; @@ -343,7 +346,8 @@ class OAuthStore implements OAuthStoreProps { ]; }); } catch (e) { - toastr.error(e); + const err = e as TData; + toastr.error(err); console.log(e); } }; @@ -375,7 +379,8 @@ class OAuthStore implements OAuthStoreProps { }); } } catch (e) { - toastr.error(e); + const err = e as TData; + toastr.error(err); console.log(e); } }; @@ -392,7 +397,8 @@ class OAuthStore implements OAuthStoreProps { }); } } catch (e) { - toastr.error(e); + const err = e as TData; + toastr.error(err); console.log(e); } }; @@ -405,7 +411,8 @@ class OAuthStore implements OAuthStoreProps { return client_secret; } catch (e) { - toastr.error(e); + const err = e as TData; + toastr.error(err); console.log(e); } }; @@ -429,7 +436,8 @@ class OAuthStore implements OAuthStoreProps { this.setActiveClient(""); } catch (e) { - toastr.error(e); + const err = e as TData; + toastr.error(err); console.log(e); } }; @@ -440,7 +448,8 @@ class OAuthStore implements OAuthStoreProps { return scope; } catch (e) { - toastr.error(e); + const err = e as TData; + toastr.error(err); console.log(e); return {} as IScope; @@ -453,7 +462,8 @@ class OAuthStore implements OAuthStoreProps { this.scopes = scopes; } catch (e) { - toastr.error(e); + const err = e as TData; + toastr.error(err); console.log(e); } }; @@ -477,7 +487,8 @@ class OAuthStore implements OAuthStoreProps { this.setActiveClient(""); } catch (e) { - toastr.error(e); + const err = e as TData; + toastr.error(err); console.log(e); } }; @@ -602,7 +613,8 @@ class OAuthStore implements OAuthStoreProps { this.setActiveClient(""); this.setSelection(""); } catch (e) { - toastr.error(e); + const err = e as TData; + toastr.error(err); } } else { this.setActiveClient(clientId); diff --git a/packages/login/src/client/components/sub-components/Consent.tsx b/packages/login/src/client/components/sub-components/Consent.tsx index 9c1e5e373c..b209cd1595 100644 --- a/packages/login/src/client/components/sub-components/Consent.tsx +++ b/packages/login/src/client/components/sub-components/Consent.tsx @@ -90,9 +90,7 @@ const Consent = ({ let clientState = ""; const scope = oauth.client.scopes; - setCookie("client_id", clientId); - - await api.oauth.onOAuthLogin(); + await api.oauth.onOAuthLogin(clientId); const cookie = document.cookie.split(";"); @@ -101,7 +99,6 @@ const Consent = ({ clientState = c.replace("client_state=", "").trim(); }); - deleteCookie("client_id"); deleteCookie("client_state"); await api.oauth.onOAuthSubmit(clientId, clientState, scope); @@ -112,9 +109,7 @@ const Consent = ({ let clientState = ""; - setCookie("client_id", clientId); - - await api.oauth.onOAuthLogin(); + await api.oauth.onOAuthLogin(clientId); const cookie = document.cookie.split(";"); @@ -123,7 +118,6 @@ const Consent = ({ clientState = c.replace("client_state=", "").trim(); }); - deleteCookie("client_id"); deleteCookie("client_state"); await api.oauth.onOAuthCancel(clientId, clientState); diff --git a/packages/runtime.json b/packages/runtime.json index 70ab71b272..0284334c3e 100644 --- a/packages/runtime.json +++ b/packages/runtime.json @@ -1,5 +1,5 @@ { - "date": "2024118_10460", + "date": "2024118_14311", "checksums": { "api.js": "cc6df9a4cf6c0ce8bed75e8cd3890b9a", "api.poly.js": "586ce6831fa68f6bc33486e2cebc856e", diff --git a/packages/shared/api/oauth/index.ts b/packages/shared/api/oauth/index.ts index c5bec129b8..cb9cf8c070 100644 --- a/packages/shared/api/oauth/index.ts +++ b/packages/shared/api/oauth/index.ts @@ -10,6 +10,7 @@ import { IScope, INoAuthClientProps, IClientReqDTO, + IGetConsentList, } from "../../utils/oauth/interfaces"; export const getClient = async ( @@ -123,12 +124,12 @@ export const getScopeList = async (): Promise => { return scopeList; }; -export const onOAuthLogin = () => { +export const onOAuthLogin = (clientId: string) => { const formData = new FormData(); return request({ method: "post", - url: `/oauth2/login`, + url: `/oauth2/login?client_id=${clientId}`, data: formData, withRedirect: true, headers: { @@ -180,18 +181,23 @@ export const onOAuthCancel = (clientId: string, clientState: string) => { }; export const getConsentList = async (): Promise => { - const clients = await request({ + const clients = (await request({ method: "get", url: "/clients/consents", - }); + })) as IGetConsentList[]; const consents: IClientProps[] = []; - clients.forEach((item) => { - const client = transformToClientProps(item.client); + clients.forEach(({ client, invalidated, modified_at }: IGetConsentList) => { + const consentClient: IClientResDTO = { + ...client, + client_secret: "", + logout_redirect_uri: "", + }; - if (!item.invalidated) - consents.push({ ...client, modifiedOn: item.modified_at }); + const cl = transformToClientProps(consentClient); + + if (!invalidated) consents.push({ ...cl, modifiedOn: modified_at }); }); return consents; diff --git a/packages/shared/api/oauth/types.ts b/packages/shared/api/oauth/types.ts deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/packages/shared/utils/axiosClient.ts b/packages/shared/utils/axiosClient.ts index 921781bae1..d8fa8f786d 100644 --- a/packages/shared/utils/axiosClient.ts +++ b/packages/shared/utils/axiosClient.ts @@ -3,7 +3,6 @@ import axios, { AxiosInstance, AxiosRequestConfig } from "axios"; import { combineUrl } from "@docspace/shared/utils/combineUrl"; import defaultConfig from "PUBLIC_DIR/scripts/config.json"; -import { setCookie } from "./cookie"; const { api: apiConf, proxy: proxyConf } = defaultConfig; const { origin: apiOrigin, prefix: apiPrefix, timeout: apiTimeout } = apiConf; @@ -66,7 +65,7 @@ class AxiosClient { "Access-Control-Allow-Credentials": "true", }; } - setCookie("x-docspace-address", origin, {}, true); + const shareIndex = window.location.pathname.indexOf("share"); const sharedIndex = window.location.pathname.indexOf("shared"); @@ -180,7 +179,11 @@ class AxiosClient { if (!response || !response.data || response.isAxiosError) return null; - if (response.data && "total" in response.data) + if ( + response.data && + typeof response.data !== "string" && + "total" in response.data + ) return { total: response.data.total ? +response.data.total : 0, items: response.data.response, diff --git a/packages/shared/utils/oauth/interfaces.ts b/packages/shared/utils/oauth/interfaces.ts index f1f860c453..da4bc762e3 100644 --- a/packages/shared/utils/oauth/interfaces.ts +++ b/packages/shared/utils/oauth/interfaces.ts @@ -118,3 +118,36 @@ export interface ISubmitReqDTO { state: string; scopes: string[]; } + +export type TConsentClient = { + authentication_methods: AuthenticationMethod[]; + client_id: string; + created_by: string; + created_on: Date; + description: string; + enabled: boolean; + invalidated: boolean; + tenant: number; + tenant_url: string; + terms_url: string; + website_url: string; + modified_by: string; + modified_on: Date; + name: string; + policy_url: string; + logo: string; + links: string[]; + logout_redirect_uris: string[]; + redirect_uris: string[]; + scopes: string[]; + allowed_origins: string[]; +}; + +export interface IGetConsentList { + client: TConsentClient; + invalidated: boolean; + modified_at: Date; + principal_name: string; + registered_client_id: string; + scopes: string; +}