From 6f68c75257277f86a770d4797b448ef2718034f7 Mon Sep 17 00:00:00 2001 From: Steve Faulkner Date: Wed, 16 Jun 2021 09:13:11 -0500 Subject: [PATCH] Allow dynamic MSAL Authority (#896) --- src/ConfigContext.ts | 8 ++++++++ src/Utils/AuthorizationUtils.ts | 3 ++- src/hooks/useAADAuth.ts | 10 +++++----- src/hooks/useDatabaseAccounts.tsx | 3 ++- src/hooks/useDirectories.tsx | 3 ++- src/hooks/useGraphPhoto.tsx | 3 ++- src/hooks/useSubscriptions.tsx | 3 ++- 7 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/ConfigContext.ts b/src/ConfigContext.ts index 5f64cbe81..e83a4e9ec 100644 --- a/src/ConfigContext.ts +++ b/src/ConfigContext.ts @@ -120,6 +120,14 @@ export async function initializeConfiguration(): Promise { const armAPIVersion = params.get("armAPIVersion") || ""; updateConfigContext({ armAPIVersion }); } + if (params.has("armEndpoint")) { + const ARM_ENDPOINT = params.get("armEndpoint") || ""; + updateConfigContext({ ARM_ENDPOINT }); + } + if (params.has("aadEndpoint")) { + const AAD_ENDPOINT = params.get("aadEndpoint") || ""; + updateConfigContext({ AAD_ENDPOINT }); + } if (params.has("platform")) { const platform = params.get("platform"); switch (platform) { diff --git a/src/Utils/AuthorizationUtils.ts b/src/Utils/AuthorizationUtils.ts index 72418c699..0da7e310f 100644 --- a/src/Utils/AuthorizationUtils.ts +++ b/src/Utils/AuthorizationUtils.ts @@ -2,6 +2,7 @@ import * as msal from "@azure/msal-browser"; import { AuthType } from "../AuthType"; import * as Constants from "../Common/Constants"; import * as Logger from "../Common/Logger"; +import { configContext } from "../ConfigContext"; import * as ViewModels from "../Contracts/ViewModels"; import { userContext } from "../UserContext"; @@ -48,7 +49,7 @@ export function getMsalInstance() { cacheLocation: "localStorage", }, auth: { - authority: "https://login.microsoftonline.com/common", + authority: `${configContext.AAD_ENDPOINT}common`, clientId: "203f1145-856a-4232-83d4-a43568fba23d", }, }; diff --git a/src/hooks/useAADAuth.ts b/src/hooks/useAADAuth.ts index 589cc4b0f..630521f2d 100644 --- a/src/hooks/useAADAuth.ts +++ b/src/hooks/useAADAuth.ts @@ -51,7 +51,7 @@ export function useAADAuth(): ReturnType { async (id) => { const response = await msalInstance.loginPopup({ redirectUri: configContext.msalRedirectURI, - authority: `https://login.microsoftonline.com/${id}`, + authority: `${configContext.AAD_ENDPOINT}${id}`, scopes: [], }); setTenantId(response.tenantId); @@ -64,12 +64,12 @@ export function useAADAuth(): ReturnType { if (account && tenantId) { Promise.all([ msalInstance.acquireTokenSilent({ - authority: `https://login.microsoftonline.com/${tenantId}`, - scopes: ["https://graph.windows.net//.default"], + authority: `${configContext.AAD_ENDPOINT}${tenantId}`, + scopes: [`${configContext.GRAPH_ENDPOINT}/.default`], }), msalInstance.acquireTokenSilent({ - authority: `https://login.microsoftonline.com/${tenantId}`, - scopes: ["https://management.azure.com//.default"], + authority: `${configContext.AAD_ENDPOINT}${tenantId}`, + scopes: [`${configContext.ARM_ENDPOINT}/.default`], }), ]).then(([graphTokenResponse, armTokenResponse]) => { setGraphToken(graphTokenResponse.accessToken); diff --git a/src/hooks/useDatabaseAccounts.tsx b/src/hooks/useDatabaseAccounts.tsx index 97ced2799..378d4639f 100644 --- a/src/hooks/useDatabaseAccounts.tsx +++ b/src/hooks/useDatabaseAccounts.tsx @@ -1,4 +1,5 @@ import useSWR from "swr"; +import { configContext } from "../ConfigContext"; import { DatabaseAccount } from "../Contracts/DataModels"; interface AccountListResult { @@ -14,7 +15,7 @@ export async function fetchDatabaseAccounts(subscriptionId: string, accessToken: let accounts: Array = []; - let nextLink = `https://management.azure.com/subscriptions/${subscriptionId}/providers/Microsoft.DocumentDB/databaseAccounts?api-version=2020-06-01-preview`; + let nextLink = `${configContext.ARM_ENDPOINT}/subscriptions/${subscriptionId}/providers/Microsoft.DocumentDB/databaseAccounts?api-version=2020-06-01-preview`; while (nextLink) { const response: Response = await fetch(nextLink, { headers }); diff --git a/src/hooks/useDirectories.tsx b/src/hooks/useDirectories.tsx index e78ff5a14..2073cf81a 100644 --- a/src/hooks/useDirectories.tsx +++ b/src/hooks/useDirectories.tsx @@ -1,4 +1,5 @@ import { useEffect, useState } from "react"; +import { configContext } from "../ConfigContext"; import { Tenant } from "../Contracts/DataModels"; interface TenantListResult { @@ -13,7 +14,7 @@ export async function fetchDirectories(accessToken: string): Promise { headers.append("Authorization", bearer); let tenents: Array = []; - let nextLink = `https://management.azure.com/tenants?api-version=2020-01-01`; + let nextLink = `${configContext.ARM_ENDPOINT}/tenants?api-version=2020-01-01`; while (nextLink) { const response = await fetch(nextLink, { headers }); diff --git a/src/hooks/useGraphPhoto.tsx b/src/hooks/useGraphPhoto.tsx index e09efeb04..b47d8d536 100644 --- a/src/hooks/useGraphPhoto.tsx +++ b/src/hooks/useGraphPhoto.tsx @@ -1,4 +1,5 @@ import { useEffect, useState } from "react"; +import { configContext } from "../ConfigContext"; export async function fetchPhoto(accessToken: string): Promise { const headers = new Headers(); @@ -12,7 +13,7 @@ export async function fetchPhoto(accessToken: string): Promise { headers: headers, }; - return fetch("https://graph.windows.net/me/thumbnailPhoto?api-version=1.6", options).then((response) => + return fetch(`${configContext.GRAPH_ENDPOINT}/me/thumbnailPhoto?api-version=1.6`, options).then((response) => response.blob() ); } diff --git a/src/hooks/useSubscriptions.tsx b/src/hooks/useSubscriptions.tsx index d7ebfcbe3..e06542240 100644 --- a/src/hooks/useSubscriptions.tsx +++ b/src/hooks/useSubscriptions.tsx @@ -1,4 +1,5 @@ import useSWR from "swr"; +import { configContext } from "../ConfigContext"; import { Subscription } from "../Contracts/DataModels"; interface SubscriptionListResult { @@ -13,7 +14,7 @@ export async function fetchSubscriptions(accessToken: string): Promise = []; - let nextLink = `https://management.azure.com/subscriptions?api-version=2020-01-01`; + let nextLink = `${configContext.ARM_ENDPOINT}subscriptions?api-version=2020-01-01`; while (nextLink) { const response = await fetch(nextLink, { headers });