UNPKG

@kwiz/common

Version:

KWIZ common utilities and helpers for M365 platform

111 lines 4.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GetAzureADLoginEndPoint = exports.GetEnvironmentFromACSEndPoint = exports.AutoDiscoverTenantInfo = exports.DiscoverTenantInfo = void 0; const promises_1 = require("../../helpers/promises"); const typecheckers_1 = require("../../helpers/typecheckers"); const auth_1 = require("../../types/auth"); const rest_1 = require("../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: auth_1.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 (!(0, typecheckers_1.isNullOrEmptyString)(tenantId) || (0, typecheckers_1.isValidGuid)(tenantId)) { data.idOrName = tenantId; } else { data.idOrName = friendlyName; } data.authorityUrl = `${GetAzureADLoginEndPoint(data.environment)}/${data.idOrName}`; data.valid = true; return data; } function DiscoverTenantInfo(hostName, sync) { hostName = hostName.toLowerCase(); let friendlyName = _getFriendlyName(hostName); let url = _getOpenIdConfigurationUrl(friendlyName); if (sync === true) { try { let response = (0, rest_1.GetJsonSync)(url); let config = response.result; let data = _processOpenidConfiguration(config, friendlyName); return data; } catch (ex) { console.log(ex); } return null; } else { return (0, promises_1.promiseOnce)(`DiscoverTenantInfo|${hostName}`, async () => { try { let config = await (0, rest_1.GetJson)(url); let data = _processOpenidConfiguration(config, friendlyName); return data; } catch (ex) { console.log(ex); } return null; }); } } exports.DiscoverTenantInfo = DiscoverTenantInfo; function AutoDiscoverTenantInfo(sync) { if (sync === true) { return DiscoverTenantInfo(window.location.hostname.toLowerCase(), true); } return DiscoverTenantInfo(window.location.hostname.toLowerCase(), false); } exports.AutoDiscoverTenantInfo = AutoDiscoverTenantInfo; function GetEnvironmentFromACSEndPoint(ACSEndPoint) { switch (ACSEndPoint) { case "microsoftonline.us": return auth_1.AzureEnvironment.USGovernment; case "microsoftonline.de": return auth_1.AzureEnvironment.Germany; case "accesscontrol.chinacloudapi.cn": return auth_1.AzureEnvironment.China; case "windows-ppe.net": return auth_1.AzureEnvironment.PPE; case "accesscontrol.windows.net": default: return auth_1.AzureEnvironment.Production; } } exports.GetEnvironmentFromACSEndPoint = GetEnvironmentFromACSEndPoint; function GetAzureADLoginEndPoint(environment) { switch (environment) { case auth_1.AzureEnvironment.Germany: return "https://login.microsoftonline.de"; case auth_1.AzureEnvironment.China: return "https://login.chinacloudapi.cn"; case auth_1.AzureEnvironment.USGovernment: return "https://login.microsoftonline.us"; case auth_1.AzureEnvironment.PPE: return "https://login.windows-ppe.net"; case auth_1.AzureEnvironment.Production: default: return "https://login.microsoftonline.com"; } } exports.GetAzureADLoginEndPoint = GetAzureADLoginEndPoint; //# sourceMappingURL=discovery.js.map