2021-05-17 19:10:54 +01:00
|
|
|
import * as msal from "@azure/msal-browser";
|
2021-05-06 00:26:03 +01:00
|
|
|
import { useBoolean } from "@fluentui/react-hooks";
|
2021-05-17 19:10:54 +01:00
|
|
|
import * as React from "react";
|
2021-01-19 22:31:55 +00:00
|
|
|
|
2021-05-17 19:10:54 +01:00
|
|
|
const config: msal.Configuration = {
|
2021-01-19 22:31:55 +00:00
|
|
|
cache: {
|
2021-01-20 15:15:01 +00:00
|
|
|
cacheLocation: "localStorage",
|
2021-01-19 22:31:55 +00:00
|
|
|
},
|
|
|
|
auth: {
|
|
|
|
authority: "https://login.microsoftonline.com/common",
|
2021-01-20 15:15:01 +00:00
|
|
|
clientId: "203f1145-856a-4232-83d4-a43568fba23d",
|
|
|
|
},
|
2021-01-19 22:31:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV === "development") {
|
|
|
|
config.auth.redirectUri = "https://dataexplorer-dev.azurewebsites.net";
|
|
|
|
}
|
|
|
|
|
2021-05-17 19:10:54 +01:00
|
|
|
const msalInstance = new msal.PublicClientApplication(config);
|
2021-01-19 22:31:55 +00:00
|
|
|
|
2021-05-17 19:10:54 +01:00
|
|
|
const cachedAccount = msalInstance.getAllAccounts()?.[0];
|
2021-01-19 22:31:55 +00:00
|
|
|
const cachedTenantId = localStorage.getItem("cachedTenantId");
|
|
|
|
|
|
|
|
interface ReturnType {
|
|
|
|
isLoggedIn: boolean;
|
|
|
|
graphToken: string;
|
|
|
|
armToken: string;
|
2021-05-18 23:59:09 +01:00
|
|
|
aadToken: string;
|
2021-01-19 22:31:55 +00:00
|
|
|
login: () => void;
|
|
|
|
logout: () => void;
|
|
|
|
tenantId: string;
|
2021-05-17 19:10:54 +01:00
|
|
|
account: msal.AccountInfo;
|
2021-01-19 22:31:55 +00:00
|
|
|
switchTenant: (tenantId: string) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useAADAuth(): ReturnType {
|
|
|
|
const [isLoggedIn, { setTrue: setLoggedIn, setFalse: setLoggedOut }] = useBoolean(
|
|
|
|
Boolean(cachedAccount && cachedTenantId) || false
|
|
|
|
);
|
2021-05-17 19:10:54 +01:00
|
|
|
const [account, setAccount] = React.useState<msal.AccountInfo>(cachedAccount);
|
2021-01-19 22:31:55 +00:00
|
|
|
const [tenantId, setTenantId] = React.useState<string>(cachedTenantId);
|
|
|
|
const [graphToken, setGraphToken] = React.useState<string>();
|
|
|
|
const [armToken, setArmToken] = React.useState<string>();
|
2021-05-18 23:59:09 +01:00
|
|
|
const [aadToken, setAadToken] = React.useState<string>();
|
2021-01-19 22:31:55 +00:00
|
|
|
|
2021-05-17 19:10:54 +01:00
|
|
|
msalInstance.setActiveAccount(account);
|
2021-01-19 22:31:55 +00:00
|
|
|
const login = React.useCallback(async () => {
|
2021-05-17 19:10:54 +01:00
|
|
|
const response = await msalInstance.loginPopup();
|
2021-01-19 22:31:55 +00:00
|
|
|
setLoggedIn();
|
|
|
|
setAccount(response.account);
|
|
|
|
setTenantId(response.tenantId);
|
|
|
|
localStorage.setItem("cachedTenantId", response.tenantId);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const logout = React.useCallback(() => {
|
|
|
|
setLoggedOut();
|
|
|
|
localStorage.removeItem("cachedTenantId");
|
2021-05-17 19:10:54 +01:00
|
|
|
msalInstance.logoutRedirect();
|
2021-01-19 22:31:55 +00:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
const switchTenant = React.useCallback(
|
2021-01-20 15:15:01 +00:00
|
|
|
async (id) => {
|
2021-05-17 19:10:54 +01:00
|
|
|
const response = await msalInstance.loginPopup({
|
2021-01-20 15:15:01 +00:00
|
|
|
authority: `https://login.microsoftonline.com/${id}`,
|
2021-05-17 19:10:54 +01:00
|
|
|
scopes: [],
|
2021-01-19 22:31:55 +00:00
|
|
|
});
|
|
|
|
setTenantId(response.tenantId);
|
|
|
|
setAccount(response.account);
|
|
|
|
},
|
|
|
|
[account, tenantId]
|
|
|
|
);
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (account && tenantId) {
|
|
|
|
Promise.all([
|
2021-05-17 19:10:54 +01:00
|
|
|
msalInstance.acquireTokenSilent({
|
2021-01-19 22:31:55 +00:00
|
|
|
authority: `https://login.microsoftonline.com/${tenantId}`,
|
2021-01-20 15:15:01 +00:00
|
|
|
scopes: ["https://graph.windows.net//.default"],
|
2021-01-19 22:31:55 +00:00
|
|
|
}),
|
2021-05-17 19:10:54 +01:00
|
|
|
msalInstance.acquireTokenSilent({
|
2021-01-19 22:31:55 +00:00
|
|
|
authority: `https://login.microsoftonline.com/${tenantId}`,
|
2021-01-20 15:15:01 +00:00
|
|
|
scopes: ["https://management.azure.com//.default"],
|
|
|
|
}),
|
2021-05-18 23:59:09 +01:00
|
|
|
msalInstance.acquireTokenSilent({
|
|
|
|
scopes: ["https://cosmos.azure.com/.default"],
|
|
|
|
}),
|
|
|
|
]).then(([graphTokenResponse, armTokenResponse, aadTokenResponse]) => {
|
2021-01-19 22:31:55 +00:00
|
|
|
setGraphToken(graphTokenResponse.accessToken);
|
|
|
|
setArmToken(armTokenResponse.accessToken);
|
2021-05-18 23:59:09 +01:00
|
|
|
setAadToken(aadTokenResponse.accessToken);
|
2021-01-19 22:31:55 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}, [account, tenantId]);
|
|
|
|
|
|
|
|
return {
|
|
|
|
account,
|
|
|
|
tenantId,
|
|
|
|
isLoggedIn,
|
|
|
|
graphToken,
|
|
|
|
armToken,
|
2021-05-18 23:59:09 +01:00
|
|
|
aadToken,
|
2021-01-19 22:31:55 +00:00
|
|
|
login,
|
|
|
|
logout,
|
2021-01-20 15:15:01 +00:00
|
|
|
switchTenant,
|
2021-01-19 22:31:55 +00:00
|
|
|
};
|
|
|
|
}
|