@kwiz/node
Version:
KWIZ utilities and helpers for node applications
54 lines • 3.07 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DiscoverTenantInfo = void 0;
const common_1 = require("@kwiz/common");
const axios_1 = require("axios");
function DiscoverTenantInfo(hostName) {
hostName = hostName.toLowerCase();
return (0, common_1.promiseOnce)(`DiscoverTenantInfo|${hostName}`, () => __awaiter(this, void 0, void 0, function* () {
let data = {
environment: common_1.$AzureEnvironment.Production,
idOrName: null,
authorityUrl: null,
valid: false
};
let tenantId = null;
let friendlyName = null;
try {
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);
friendlyName = `${firstHostPart}.onmicrosoft.${lastHostPart}`;
}
else
friendlyName = hostName; //could be an exchange email domain, or bpos customer
let config = yield axios_1.default.get(`https://login.microsoftonline.com/${friendlyName}/v2.0/.well-known/openid-configuration`);
let endpoint = config.data.token_endpoint; //https://xxxx/{tenant}/....
tenantId = endpoint.replace("//", "/").split('/')[2]; //replace :// with :/ split by / and take the second part.
let instance = config.data.cloud_instance_name; //microsoftonline.us
data.environment = (0, common_1.GetEnvironmentFromACSEndPoint)(instance);
if (!(0, common_1.isNullOrEmptyString)(tenantId) || (0, common_1.isValidGuid)(tenantId))
data.idOrName = tenantId;
else
data.idOrName = friendlyName;
data.authorityUrl = `${(0, common_1.GetAzureADLoginEndPoint)(data.environment)}/${data.idOrName}`;
data.valid = true;
}
catch (e) { }
return data;
}));
}
exports.DiscoverTenantInfo = DiscoverTenantInfo;
//# sourceMappingURL=discovery.js.map