UNPKG

4.43 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright (c) Microsoft Corporation. All rights reserved.
4 * Licensed under the MIT License.
5 */
6Object.defineProperty(exports, "__esModule", { value: true });
7exports.TrustedAuthority = void 0;
8var tslib_1 = require("tslib");
9var XHRClient_1 = require("../XHRClient");
10var Constants_1 = require("../utils/Constants");
11var UrlUtils_1 = require("../utils/UrlUtils");
12var TrustedAuthority = /** @class */ (function () {
13 function TrustedAuthority() {
14 }
15 /**
16 *
17 * @param validateAuthority
18 * @param knownAuthorities
19 */
20 TrustedAuthority.setTrustedAuthoritiesFromConfig = function (validateAuthority, knownAuthorities) {
21 if (validateAuthority && !this.getTrustedHostList().length) {
22 knownAuthorities.forEach(function (authority) {
23 TrustedAuthority.TrustedHostList.push(authority.toLowerCase());
24 });
25 }
26 };
27 /**
28 *
29 * @param telemetryManager
30 * @param correlationId
31 */
32 TrustedAuthority.getAliases = function (authorityToVerify, telemetryManager, correlationId) {
33 return tslib_1.__awaiter(this, void 0, void 0, function () {
34 var client, httpMethod, instanceDiscoveryEndpoint, httpEvent;
35 return tslib_1.__generator(this, function (_a) {
36 client = new XHRClient_1.XhrClient();
37 httpMethod = Constants_1.NetworkRequestType.GET;
38 instanceDiscoveryEndpoint = "" + Constants_1.AAD_INSTANCE_DISCOVERY_ENDPOINT + authorityToVerify + "oauth2/v2.0/authorize";
39 httpEvent = telemetryManager.createAndStartHttpEvent(correlationId, httpMethod, instanceDiscoveryEndpoint, "getAliases");
40 return [2 /*return*/, client.sendRequestAsync(instanceDiscoveryEndpoint, httpMethod, true)
41 .then(function (response) {
42 httpEvent.httpResponseStatus = response.statusCode;
43 telemetryManager.stopEvent(httpEvent);
44 return response.body["metadata"];
45 })
46 .catch(function (err) {
47 httpEvent.serverErrorCode = err;
48 telemetryManager.stopEvent(httpEvent);
49 throw err;
50 })];
51 });
52 });
53 };
54 /**
55 *
56 * @param telemetryManager
57 * @param correlationId
58 */
59 TrustedAuthority.setTrustedAuthoritiesFromNetwork = function (authorityToVerify, telemetryManager, correlationId) {
60 return tslib_1.__awaiter(this, void 0, void 0, function () {
61 var metadata, host;
62 return tslib_1.__generator(this, function (_a) {
63 switch (_a.label) {
64 case 0: return [4 /*yield*/, this.getAliases(authorityToVerify, telemetryManager, correlationId)];
65 case 1:
66 metadata = _a.sent();
67 metadata.forEach(function (entry) {
68 var authorities = entry["aliases"];
69 authorities.forEach(function (authority) {
70 TrustedAuthority.TrustedHostList.push(authority.toLowerCase());
71 });
72 });
73 host = UrlUtils_1.UrlUtils.GetUrlComponents(authorityToVerify).HostNameAndPort;
74 if (TrustedAuthority.getTrustedHostList().length && !TrustedAuthority.IsInTrustedHostList(host)) {
75 // Custom Domain scenario, host is trusted because Instance Discovery call succeeded
76 TrustedAuthority.TrustedHostList.push(host.toLowerCase());
77 }
78 return [2 /*return*/];
79 }
80 });
81 });
82 };
83 TrustedAuthority.getTrustedHostList = function () {
84 return this.TrustedHostList;
85 };
86 /**
87 * Checks to see if the host is in a list of trusted hosts
88 * @param host
89 */
90 TrustedAuthority.IsInTrustedHostList = function (host) {
91 return this.TrustedHostList.indexOf(host.toLowerCase()) > -1;
92 };
93 TrustedAuthority.TrustedHostList = [];
94 return TrustedAuthority;
95}());
96exports.TrustedAuthority = TrustedAuthority;
97//# sourceMappingURL=TrustedAuthority.js.map
\No newline at end of file