@kwiz/common
Version:
KWIZ common utilities and helpers for M365 platform
178 lines • 7.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DiscoverTenantInfo = DiscoverTenantInfo;
exports.AutoDiscoverTenantInfo = AutoDiscoverTenantInfo;
exports.GetEnvironmentFromACSEndPoint = GetEnvironmentFromACSEndPoint;
exports.GetAzureADLoginEndPoint = GetAzureADLoginEndPoint;
exports.GetAzureEnvironmentFromAzureADLogin = GetAzureEnvironmentFromAzureADLogin;
const promises_1 = require("../../helpers/promises");
const strings_1 = require("../../helpers/strings");
const typecheckers_1 = require("../../helpers/typecheckers");
const auth_1 = require("../../types/auth");
const rest_1 = require("../rest");
const common_1 = require("../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: auth_1.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 (!(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;
data.msGraphHost = config.msgraph_host;
return data;
}
function DiscoverTenantInfo(hostNameOrId, sync) {
hostNameOrId = hostNameOrId.toLowerCase();
if ("location" in globalThis && (0, common_1.hasGlobalContext)()) {
if (!(0, typecheckers_1.isValidGuid)(hostNameOrId)) {
try {
let host1 = hostNameOrId.toLowerCase();
let host2 = window.location.host.toLowerCase();
if (host1 === host2) {
let info = _getTenantInfoFromContext();
if (!(0, typecheckers_1.isNullOrUndefined)(info)) {
return info;
}
}
}
catch {
}
}
else if ((0, typecheckers_1.isValidGuid)(_spPageContextInfo.aadTenantId)
&& (0, strings_1.normalizeGuid)(hostNameOrId) === (0, strings_1.normalizeGuid)(_spPageContextInfo.aadTenantId)) {
let info = _getTenantInfoFromContext();
if (!(0, typecheckers_1.isNullOrUndefined)(info)) {
return info;
}
}
}
let friendlyName = "";
if ((0, typecheckers_1.isValidGuid)(hostNameOrId)) {
friendlyName = hostNameOrId;
}
else {
friendlyName = _getFriendlyName(hostNameOrId);
}
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|${hostNameOrId}`, async () => {
try {
let config = await (0, rest_1.GetJson)(url);
let data = _processOpenidConfiguration(config, friendlyName);
return data;
}
catch (ex) {
console.log(ex);
}
return null;
});
}
}
function _getTenantInfoFromContext() {
if (!(0, common_1.hasGlobalContext)()
|| !("location" in globalThis)
|| !(0, typecheckers_1.isValidGuid)(_spPageContextInfo.aadTenantId)
|| (0, typecheckers_1.isNullOrEmptyString)(_spPageContextInfo["aadInstanceUrl"])
|| (0, typecheckers_1.isNullOrEmptyString)(_spPageContextInfo["msGraphEndpointUrl"])) {
return null;
}
let environment = GetAzureEnvironmentFromAzureADLogin(_spPageContextInfo["aadInstanceUrl"]);
return {
environment: environment,
idOrName: (0, strings_1.normalizeGuid)(_spPageContextInfo.aadTenantId),
authorityUrl: _spPageContextInfo["aadInstanceUrl"],
msGraphHost: new URL(_spPageContextInfo["msGraphEndpointUrl"]).host
};
}
function AutoDiscoverTenantInfo(sync) {
if (sync === true) {
return DiscoverTenantInfo(window.location.hostname.toLowerCase(), true);
}
return DiscoverTenantInfo(window.location.hostname.toLowerCase(), false);
}
function GetEnvironmentFromACSEndPoint(ACSEndPoint) {
switch (ACSEndPoint) {
case "microsoftonline.us":
return auth_1.AzureEnvironment.USGovernmentHigh;
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;
}
}
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.USGovernmentHigh:
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";
}
}
function GetAzureEnvironmentFromAzureADLogin(login) {
switch (login) {
case "https://login.microsoftonline.de":
return auth_1.AzureEnvironment.Germany;
case "https://login.chinacloudapi.cn":
return auth_1.AzureEnvironment.China;
case "https://login.microsoftonline.us":
return auth_1.AzureEnvironment.USGovernmentHigh;
case "https://login.windows-ppe.net":
return auth_1.AzureEnvironment.PPE;
default:
return auth_1.AzureEnvironment.Production;
}
}
//# sourceMappingURL=discovery.js.map