@kwiz/common
Version:
KWIZ common utilities and helpers for M365 platform
104 lines • 4.14 kB
JavaScript
import { promiseOnce } from "../../helpers/promises";
import { isNullOrEmptyString, isValidGuid } from "../../helpers/typecheckers";
import { AzureEnvironment } from "../../types/auth";
import { GetJson, GetJsonSync } from "../rest";
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
};
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;
return data;
}
export function DiscoverTenantInfo(hostName, sync) {
hostName = hostName.toLowerCase();
let friendlyName = _getFriendlyName(hostName);
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|${hostName}`, async () => {
try {
let config = await GetJson(url);
let data = _processOpenidConfiguration(config, friendlyName);
return data;
}
catch (ex) {
console.log(ex);
}
return null;
});
}
}
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.USGovernment;
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.USGovernment: return "https://login.microsoftonline.us";
case AzureEnvironment.PPE: return "https://login.windows-ppe.net";
case AzureEnvironment.Production:
default:
return "https://login.microsoftonline.com";
}
}
//# sourceMappingURL=discovery.js.map