Fetch encrypted token from sessionStorage for fabric platform

This commit is contained in:
Laurent Nguyen 2023-09-12 15:04:34 +00:00
parent 8c74353794
commit f595d26493
2 changed files with 35 additions and 12 deletions

View File

@ -1,7 +1,7 @@
import { useBoolean } from "@fluentui/react-hooks"; import { useBoolean } from "@fluentui/react-hooks";
import * as React from "react"; import * as React from "react";
import ErrorImage from "../../../../images/error.svg";
import ConnectImage from "../../../../images/HdeConnectCosmosDB.svg"; import ConnectImage from "../../../../images/HdeConnectCosmosDB.svg";
import ErrorImage from "../../../../images/error.svg";
import { AuthType } from "../../../AuthType"; import { AuthType } from "../../../AuthType";
import { HttpHeaders } from "../../../Common/Constants"; import { HttpHeaders } from "../../../Common/Constants";
import { configContext } from "../../../ConfigContext"; import { configContext } from "../../../ConfigContext";
@ -16,6 +16,19 @@ interface Props {
setAuthType: (authType: AuthType) => void; setAuthType: (authType: AuthType) => void;
} }
export const fetchEncryptedToken = async (connectionString: string): Promise<string> => {
const headers = new Headers();
headers.append(HttpHeaders.connectionString, connectionString);
const url = configContext.BACKEND_ENDPOINT + "/api/guest/tokens/generateToken";
const response = await fetch(url, { headers, method: "POST" });
if (!response.ok) {
throw response;
}
// This API has a quirk where it must be parsed twice
const result: GenerateTokenResponse = JSON.parse(await response.json());
return decodeURIComponent(result.readWrite || result.read);
};
export const ConnectExplorer: React.FunctionComponent<Props> = ({ export const ConnectExplorer: React.FunctionComponent<Props> = ({
setEncryptedToken, setEncryptedToken,
login, login,
@ -44,16 +57,8 @@ export const ConnectExplorer: React.FunctionComponent<Props> = ({
return; return;
} }
const headers = new Headers(); const encryptedToken = await fetchEncryptedToken(connectionString);
headers.append(HttpHeaders.connectionString, connectionString); setEncryptedToken(encryptedToken);
const url = configContext.BACKEND_ENDPOINT + "/api/guest/tokens/generateToken";
const response = await fetch(url, { headers, method: "POST" });
if (!response.ok) {
throw response;
}
// This API has a quirk where it must be parsed twice
const result: GenerateTokenResponse = JSON.parse(await response.json());
setEncryptedToken(decodeURIComponent(result.readWrite || result.read));
setAuthType(AuthType.ConnectionString); setAuthType(AuthType.ConnectionString);
}} }}
> >

View File

@ -1,6 +1,8 @@
import { createUri } from "Common/UrlUtility"; import { createUri } from "Common/UrlUtility";
import Explorer from "Explorer/Explorer"; import Explorer from "Explorer/Explorer";
import { fetchEncryptedToken } from "Platform/Hosted/Components/ConnectExplorer";
import { getNetworkSettingsWarningMessage } from "Utils/NetworkUtility"; import { getNetworkSettingsWarningMessage } from "Utils/NetworkUtility";
import { fetchAccessData } from "hooks/usePortalAccessToken";
import { ReactTabKind, useTabs } from "hooks/useTabs"; import { ReactTabKind, useTabs } from "hooks/useTabs";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { AuthType } from "../AuthType"; import { AuthType } from "../AuthType";
@ -61,7 +63,23 @@ export function useKnockoutExplorer(platform: Platform): Explorer {
const explorer = await configurePortal(); const explorer = await configurePortal();
setExplorer(explorer); setExplorer(explorer);
} else if (platform === Platform.Fabric) { } else if (platform === Platform.Fabric) {
//TODO: need Fabric specific implementation similar to portal // TODO For now, retrieve info from session storage. Replace with info injected into Data Explorer
const connectionString = sessionStorage.getItem('connectionString');
if (!connectionString) {
console.error('No connection string found in session storage');
return;
}
const encryptedToken = await fetchEncryptedToken(connectionString);
// TODO Duplicated from useTokenMetadata
const encryptedTokenMetadata = await fetchAccessData(encryptedToken);
const win = (window as unknown) as HostedExplorerChildFrame;
win.hostedConfig = {
authType: AuthType.EncryptedToken,
encryptedToken,
encryptedTokenMetadata
};
const explorer = await configureHosted(); const explorer = await configureHosted();
setExplorer(explorer); setExplorer(explorer);
} }