diff --git a/src/Common/Constants.ts b/src/Common/Constants.ts index cde5e9462..548a92c76 100644 --- a/src/Common/Constants.ts +++ b/src/Common/Constants.ts @@ -134,6 +134,7 @@ export class BackendApi { public static readonly GenerateToken: string = "GenerateToken"; public static readonly PortalSettings: string = "PortalSettings"; public static readonly AccountRestrictions: string = "AccountRestrictions"; + public static readonly RuntimeProxy: string = "RuntimeProxy"; } export class PortalBackendEndpoints { diff --git a/src/Common/CosmosClient.ts b/src/Common/CosmosClient.ts index 2216e2448..f286f3fbc 100644 --- a/src/Common/CosmosClient.ts +++ b/src/Common/CosmosClient.ts @@ -3,15 +3,16 @@ import { getAuthorizationTokenUsingResourceTokens } from "Common/getAuthorizatio import { AuthorizationToken } from "Contracts/FabricMessageTypes"; import { checkDatabaseResourceTokensValidity } from "Platform/Fabric/FabricUtil"; import { LocalStorageUtility, StorageKey } from "Shared/StorageUtility"; +import { useNewPortalBackendEndpoint } from "Utils/EndpointUtils"; import { AuthType } from "../AuthType"; -import { PriorityLevel } from "../Common/Constants"; +import { BackendApi, PriorityLevel } from "../Common/Constants"; +import * as Logger from "../Common/Logger"; import { Platform, configContext } from "../ConfigContext"; import { userContext } from "../UserContext"; import { logConsoleError } from "../Utils/NotificationConsoleUtils"; import * as PriorityBasedExecutionUtils from "../Utils/PriorityBasedExecutionUtils"; import { EmulatorMasterKey, HttpHeaders } from "./Constants"; import { getErrorMessage } from "./ErrorHandlingUtils"; -import * as Logger from "../Common/Logger"; const _global = typeof self === "undefined" ? window : self; @@ -123,6 +124,37 @@ export async function getTokenFromAuthService( verb: string, resourceType: string, resourceId?: string, +): Promise { + if (!useNewPortalBackendEndpoint(BackendApi.RuntimeProxy)) { + return getTokenFromAuthService_ToBeDeprecated(verb, resourceType, resourceId); + } + + try { + const host: string = configContext.PORTAL_BACKEND_ENDPOINT; + const response: Response = await _global.fetch(host + "/api/connectionstring/runtimeproxy/authorizationtokens", { + method: "POST", + headers: { + "content-type": "application/json", + "x-ms-encrypted-auth-token": userContext.accessToken, + }, + body: JSON.stringify({ + verb, + resourceType, + resourceId, + }), + }); + const result: AuthorizationToken = await response.json(); + return result; + } catch (error) { + logConsoleError(`Failed to get authorization headers for ${resourceType}: ${getErrorMessage(error)}`); + return Promise.reject(error); + } +} + +export async function getTokenFromAuthService_ToBeDeprecated( + verb: string, + resourceType: string, + resourceId?: string, ): Promise { try { const host = configContext.BACKEND_ENDPOINT; diff --git a/src/Common/MongoProxyClient.ts b/src/Common/MongoProxyClient.ts index 64c42d875..97cc849d8 100644 --- a/src/Common/MongoProxyClient.ts +++ b/src/Common/MongoProxyClient.ts @@ -720,7 +720,8 @@ export function useMongoProxyEndpoint(api: string): boolean { MongoProxyEndpoints.Local, MongoProxyEndpoints.Mpac, MongoProxyEndpoints.Prod, - // MongoProxyEndpoints.Fairfax, + MongoProxyEndpoints.Fairfax, + MongoProxyEndpoints.Mooncake, ]; let canAccessMongoProxy: boolean = userContext.databaseAccount.properties.publicNetworkAccess === "Enabled"; if ( diff --git a/src/Platform/Hosted/Components/ConnectExplorer.tsx b/src/Platform/Hosted/Components/ConnectExplorer.tsx index 64f8540b7..93bdc51e8 100644 --- a/src/Platform/Hosted/Components/ConnectExplorer.tsx +++ b/src/Platform/Hosted/Components/ConnectExplorer.tsx @@ -52,7 +52,7 @@ export const isAccountRestrictedForConnectionStringLogin = async (connectionStri const headers = new Headers(); headers.append(HttpHeaders.connectionString, connectionString); - const backendEndpoint: string = useNewPortalBackendEndpoint(BackendApi.PortalSettings) + const backendEndpoint: string = useNewPortalBackendEndpoint(BackendApi.AccountRestrictions) ? configContext.PORTAL_BACKEND_ENDPOINT : configContext.BACKEND_ENDPOINT; diff --git a/src/Utils/EndpointUtils.ts b/src/Utils/EndpointUtils.ts index b685dc71a..11968c639 100644 --- a/src/Utils/EndpointUtils.ts +++ b/src/Utils/EndpointUtils.ts @@ -164,7 +164,16 @@ export function useNewPortalBackendEndpoint(backendApi: string): boolean { PortalBackendEndpoints.Mpac, PortalBackendEndpoints.Prod, ], - [BackendApi.AccountRestrictions]: [PortalBackendEndpoints.Development, PortalBackendEndpoints.Mpac], + [BackendApi.AccountRestrictions]: [ + PortalBackendEndpoints.Development, + PortalBackendEndpoints.Mpac, + PortalBackendEndpoints.Prod, + ], + [BackendApi.RuntimeProxy]: [ + PortalBackendEndpoints.Development, + PortalBackendEndpoints.Mpac, + PortalBackendEndpoints.Prod, + ], }; if (!newBackendApiEnvironmentMap[backendApi] || !configContext.PORTAL_BACKEND_ENDPOINT) { diff --git a/src/hooks/usePortalAccessToken.tsx b/src/hooks/usePortalAccessToken.tsx index fdccc84a7..5de77e08d 100644 --- a/src/hooks/usePortalAccessToken.tsx +++ b/src/hooks/usePortalAccessToken.tsx @@ -1,14 +1,34 @@ import { useEffect, useState } from "react"; -import { ApiEndpoints } from "../Common/Constants"; +import { useNewPortalBackendEndpoint } from "Utils/EndpointUtils"; +import { ApiEndpoints, BackendApi, HttpHeaders } from "../Common/Constants"; import { configContext } from "../ConfigContext"; import { AccessInputMetadata } from "../Contracts/DataModels"; const url = `${configContext.BACKEND_ENDPOINT}${ApiEndpoints.guestRuntimeProxy}/accessinputmetadata?_=1609359229955`; export async function fetchAccessData(portalToken: string): Promise { + if (!useNewPortalBackendEndpoint(BackendApi.RuntimeProxy)) { + return fetchAccessData_ToBeDeprecated(portalToken); + } + const headers = new Headers(); // Portal encrypted token API quirk: The token header must be URL encoded - headers.append("x-ms-encrypted-auth-token", encodeURIComponent(portalToken)); + headers.append(HttpHeaders.guestAccessToken, encodeURIComponent(portalToken)); + const url: string = `${configContext.PORTAL_BACKEND_ENDPOINT}/api/connectionstring/runtimeproxy/accessinputmetadata`; + const options = { + method: "GET", + headers: headers, + }; + + return fetch(url, options) + .then((response) => response.json()) + .catch((error) => console.error(error)); +} + +export async function fetchAccessData_ToBeDeprecated(portalToken: string): Promise { + const headers = new Headers(); + // Portal encrypted token API quirk: The token header must be URL encoded + headers.append(HttpHeaders.guestAccessToken, encodeURIComponent(portalToken)); const options = { method: "GET",