2020-05-25 21:30:55 -05:00
|
|
|
import * as Constants from "../Common/Constants";
|
|
|
|
import * as ViewModels from "../Contracts/ViewModels";
|
|
|
|
import AuthHeadersUtil from "../Platform/Hosted/Authorization";
|
|
|
|
import { AuthType } from "../AuthType";
|
2020-06-23 10:45:51 -05:00
|
|
|
import * as Logger from "../Common/Logger";
|
2020-05-25 21:30:55 -05:00
|
|
|
import { PlatformType } from "../PlatformType";
|
|
|
|
import { CosmosClient } from "../Common/CosmosClient";
|
|
|
|
import { config } from "../Config";
|
|
|
|
|
|
|
|
export function getAuthorizationHeader(): ViewModels.AuthorizationTokenHeaderMetadata {
|
|
|
|
if (window.authType === AuthType.EncryptedToken) {
|
|
|
|
return {
|
|
|
|
header: Constants.HttpHeaders.guestAccessToken,
|
|
|
|
token: CosmosClient.accessToken()
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
header: Constants.HttpHeaders.authorization,
|
|
|
|
token: CosmosClient.authorizationToken() || ""
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getArcadiaAuthToken(
|
|
|
|
arcadiaEndpoint: string = config.ARCADIA_ENDPOINT,
|
|
|
|
tenantId?: string
|
|
|
|
): Promise<string> {
|
|
|
|
try {
|
|
|
|
const token = await AuthHeadersUtil.getAccessToken(arcadiaEndpoint, tenantId);
|
|
|
|
return token;
|
|
|
|
} catch (error) {
|
|
|
|
Logger.logError(error, "AuthorizationUtils/getArcadiaAuthToken");
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function decryptJWTToken(token: string) {
|
|
|
|
if (!token) {
|
|
|
|
Logger.logError("Cannot decrypt token: No JWT token found", "AuthorizationUtils/decryptJWTToken");
|
|
|
|
throw new Error("No JWT token found");
|
|
|
|
}
|
|
|
|
const tokenParts = token.split(".");
|
|
|
|
if (tokenParts.length < 2) {
|
|
|
|
Logger.logError(`Invalid JWT token: ${token}`, "AuthorizationUtils/decryptJWTToken");
|
|
|
|
throw new Error(`Invalid JWT token: ${token}`);
|
|
|
|
}
|
|
|
|
let tokenPayloadBase64: string = tokenParts[1];
|
|
|
|
tokenPayloadBase64 = tokenPayloadBase64.replace(/-/g, "+").replace(/_/g, "/");
|
|
|
|
const tokenPayload = decodeURIComponent(
|
|
|
|
atob(tokenPayloadBase64)
|
|
|
|
.split("")
|
|
|
|
.map(p => "%" + ("00" + p.charCodeAt(0).toString(16)).slice(-2))
|
|
|
|
.join("")
|
|
|
|
);
|
|
|
|
|
|
|
|
return JSON.parse(tokenPayload);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function displayTokenRenewalPromptForStatus(httpStatusCode: number): void {
|
2020-07-20 12:59:40 -05:00
|
|
|
const platformType = window.dataExplorerPlatform;
|
|
|
|
const explorer = window.dataExplorer;
|
2020-05-25 21:30:55 -05:00
|
|
|
|
|
|
|
if (
|
|
|
|
httpStatusCode == null ||
|
|
|
|
httpStatusCode != Constants.HttpStatusCodes.Unauthorized ||
|
|
|
|
platformType !== PlatformType.Hosted
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
explorer.displayGuestAccessTokenRenewalPrompt();
|
|
|
|
}
|