@kwiz/common
Version:
KWIZ common utilities and helpers for M365 platform
171 lines • 6.6 kB
JavaScript
import { promiseOnce } from "../../helpers/promises";
import { normalizeGuid } from "../../helpers/strings";
import { isNullOrEmptyString, isNullOrUndefined, isValidGuid } from "../../helpers/typecheckers";
import { AzureEnvironment } from "../../types/auth";
import { GetJson, GetJsonSync } from "../rest";
import { hasGlobalContext } from "../sharepoint.rest/common";
function _getFriendlyName(hostName) {
if (hostName.indexOf(".sharepoint.") !== -1) {
let hostParts = hostName.split('.'); //should be xxx.sharepoint.com or xxx.sharepoint.us
let firstHostPart = hostParts[0];
let lastHostPart = hostParts[hostParts.length - 1] === "us" || hostParts[hostParts.length - 1] === "de" ? hostParts[hostParts.length - 1] : "com";
if (firstHostPart.endsWith("-admin"))
firstHostPart = firstHostPart.substring(0, firstHostPart.length - 6);
return `${firstHostPart}.onmicrosoft.${lastHostPart}`;
}
else {
return hostName; //could be an exchange email domain, or bpos customer
}
}
function _getOpenIdConfigurationUrl(friendlyName) {
return `https://login.microsoftonline.com/${friendlyName}/v2.0/.well-known/openid-configuration`;
}
function _processOpenidConfiguration(config, friendlyName) {
let data = {
environment: AzureEnvironment.Production,
idOrName: null,
authorityUrl: null,
valid: false,
msGraphHost: null
};
let endpoint = config.token_endpoint; //https://xxxx/{tenant}/....
let tenantId = endpoint.replace("//", "/").split('/')[2]; //replace :// with :/ split by / and take the second part.
let instance = config.cloud_instance_name; //microsoftonline.us
data.environment = GetEnvironmentFromACSEndPoint(instance);
if (!isNullOrEmptyString(tenantId) || isValidGuid(tenantId)) {
data.idOrName = tenantId;
}
else {
data.idOrName = friendlyName;
}
data.authorityUrl = `${GetAzureADLoginEndPoint(data.environment)}/${data.idOrName}`;
data.valid = true;
data.msGraphHost = config.msgraph_host;
return data;
}
export function DiscoverTenantInfo(hostNameOrId, sync) {
hostNameOrId = hostNameOrId.toLowerCase();
if ("location" in globalThis && hasGlobalContext()) {
if (!isValidGuid(hostNameOrId)) {
try {
let host1 = hostNameOrId.toLowerCase();
let host2 = window.location.host.toLowerCase();
if (host1 === host2) {
let info = _getTenantInfoFromContext();
if (!isNullOrUndefined(info)) {
return info;
}
}
}
catch {
}
}
else if (isValidGuid(_spPageContextInfo.aadTenantId)
&& normalizeGuid(hostNameOrId) === normalizeGuid(_spPageContextInfo.aadTenantId)) {
let info = _getTenantInfoFromContext();
if (!isNullOrUndefined(info)) {
return info;
}
}
}
let friendlyName = "";
if (isValidGuid(hostNameOrId)) {
friendlyName = hostNameOrId;
}
else {
friendlyName = _getFriendlyName(hostNameOrId);
}
let url = _getOpenIdConfigurationUrl(friendlyName);
if (sync === true) {
try {
let response = GetJsonSync(url);
let config = response.result;
let data = _processOpenidConfiguration(config, friendlyName);
return data;
}
catch (ex) {
console.log(ex);
}
return null;
}
else {
return promiseOnce(`DiscoverTenantInfo|${hostNameOrId}`, async () => {
try {
let config = await GetJson(url);
let data = _processOpenidConfiguration(config, friendlyName);
return data;
}
catch (ex) {
console.log(ex);
}
return null;
});
}
}
function _getTenantInfoFromContext() {
if (!hasGlobalContext()
|| !("location" in globalThis)
|| !isValidGuid(_spPageContextInfo.aadTenantId)
|| isNullOrEmptyString(_spPageContextInfo["aadInstanceUrl"])
|| isNullOrEmptyString(_spPageContextInfo["msGraphEndpointUrl"])) {
return null;
}
let environment = GetAzureEnvironmentFromAzureADLogin(_spPageContextInfo["aadInstanceUrl"]);
return {
environment: environment,
idOrName: normalizeGuid(_spPageContextInfo.aadTenantId),
authorityUrl: _spPageContextInfo["aadInstanceUrl"],
msGraphHost: new URL(_spPageContextInfo["msGraphEndpointUrl"]).host
};
}
export function AutoDiscoverTenantInfo(sync) {
if (sync === true) {
return DiscoverTenantInfo(window.location.hostname.toLowerCase(), true);
}
return DiscoverTenantInfo(window.location.hostname.toLowerCase(), false);
}
export function GetEnvironmentFromACSEndPoint(ACSEndPoint) {
switch (ACSEndPoint) {
case "microsoftonline.us":
return AzureEnvironment.USGovernmentHigh;
case "microsoftonline.de":
return AzureEnvironment.Germany;
case "accesscontrol.chinacloudapi.cn":
return AzureEnvironment.China;
case "windows-ppe.net":
return AzureEnvironment.PPE;
case "accesscontrol.windows.net":
default:
return AzureEnvironment.Production;
}
}
export function GetAzureADLoginEndPoint(environment) {
switch (environment) {
case AzureEnvironment.Germany:
return "https://login.microsoftonline.de";
case AzureEnvironment.China:
return "https://login.chinacloudapi.cn";
case AzureEnvironment.USGovernmentHigh:
return "https://login.microsoftonline.us";
case AzureEnvironment.PPE:
return "https://login.windows-ppe.net";
case AzureEnvironment.Production:
default:
return "https://login.microsoftonline.com";
}
}
export function GetAzureEnvironmentFromAzureADLogin(login) {
switch (login) {
case "https://login.microsoftonline.de":
return AzureEnvironment.Germany;
case "https://login.chinacloudapi.cn":
return AzureEnvironment.China;
case "https://login.microsoftonline.us":
return AzureEnvironment.USGovernmentHigh;
case "https://login.windows-ppe.net":
return AzureEnvironment.PPE;
default:
return AzureEnvironment.Production;
}
}
//# sourceMappingURL=discovery.js.map