1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.WindowUtils = void 0;
|
8 | var ClientAuthError_1 = require("../error/ClientAuthError");
|
9 | var UrlUtils_1 = require("./UrlUtils");
|
10 | var Constants_1 = require("./Constants");
|
11 | var TimeUtils_1 = require("./TimeUtils");
|
12 | var WindowUtils = (function () {
|
13 | function WindowUtils() {
|
14 | }
|
15 | |
16 |
|
17 |
|
18 |
|
19 |
|
20 | WindowUtils.isInIframe = function () {
|
21 | return window.parent !== window;
|
22 | };
|
23 | |
24 |
|
25 |
|
26 |
|
27 |
|
28 | WindowUtils.isInPopup = function () {
|
29 | return !!(window.opener && window.opener !== window);
|
30 | };
|
31 | |
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | WindowUtils.generateFrameName = function (prefix, requestSignature) {
|
38 | return "" + prefix + Constants_1.Constants.resourceDelimiter + requestSignature;
|
39 | };
|
40 | |
41 |
|
42 |
|
43 |
|
44 |
|
45 | WindowUtils.monitorIframeForHash = function (contentWindow, timeout, urlNavigate, logger) {
|
46 | return new Promise(function (resolve, reject) {
|
47 | |
48 |
|
49 |
|
50 |
|
51 | var nowMark = TimeUtils_1.TimeUtils.relativeNowMs();
|
52 | var timeoutMark = nowMark + timeout;
|
53 | logger.verbose("monitorWindowForIframe polling started");
|
54 | var intervalId = setInterval(function () {
|
55 | if (TimeUtils_1.TimeUtils.relativeNowMs() > timeoutMark) {
|
56 | logger.error("monitorIframeForHash unable to find hash in url, timing out");
|
57 | logger.errorPii("monitorIframeForHash polling timed out for url: " + urlNavigate);
|
58 | clearInterval(intervalId);
|
59 | reject(ClientAuthError_1.ClientAuthError.createTokenRenewalTimeoutError());
|
60 | return;
|
61 | }
|
62 | var href;
|
63 | try {
|
64 | |
65 |
|
66 |
|
67 |
|
68 |
|
69 | href = contentWindow.location.href;
|
70 | }
|
71 | catch (e) { }
|
72 | if (href && UrlUtils_1.UrlUtils.urlContainsHash(href)) {
|
73 | logger.verbose("monitorIframeForHash found url in hash");
|
74 | clearInterval(intervalId);
|
75 | resolve(contentWindow.location.hash);
|
76 | }
|
77 | }, WindowUtils.POLLING_INTERVAL_MS);
|
78 | });
|
79 | };
|
80 | |
81 |
|
82 |
|
83 |
|
84 |
|
85 | WindowUtils.monitorPopupForHash = function (contentWindow, timeout, urlNavigate, logger) {
|
86 | return new Promise(function (resolve, reject) {
|
87 | |
88 |
|
89 |
|
90 |
|
91 | var maxTicks = timeout / WindowUtils.POLLING_INTERVAL_MS;
|
92 | var ticks = 0;
|
93 | logger.verbose("monitorWindowForHash polling started");
|
94 | var intervalId = setInterval(function () {
|
95 | if (contentWindow.closed) {
|
96 | logger.error("monitorWindowForHash window closed");
|
97 | clearInterval(intervalId);
|
98 | reject(ClientAuthError_1.ClientAuthError.createUserCancelledError());
|
99 | return;
|
100 | }
|
101 | var href;
|
102 | try {
|
103 | |
104 |
|
105 |
|
106 |
|
107 |
|
108 | href = contentWindow.location.href;
|
109 | }
|
110 | catch (e) { }
|
111 |
|
112 | if (!href || href === "about:blank") {
|
113 | return;
|
114 | }
|
115 | |
116 |
|
117 |
|
118 |
|
119 | ticks++;
|
120 | if (href && UrlUtils_1.UrlUtils.urlContainsHash(href)) {
|
121 | logger.verbose("monitorPopupForHash found url in hash");
|
122 | clearInterval(intervalId);
|
123 | var hash = contentWindow.location.hash;
|
124 | WindowUtils.clearUrlFragment(contentWindow);
|
125 | resolve(hash);
|
126 | }
|
127 | else if (ticks > maxTicks) {
|
128 | logger.error("monitorPopupForHash unable to find hash in url, timing out");
|
129 | logger.errorPii("monitorPopupForHash polling timed out for url: " + urlNavigate);
|
130 | clearInterval(intervalId);
|
131 | reject(ClientAuthError_1.ClientAuthError.createTokenRenewalTimeoutError());
|
132 | }
|
133 | }, WindowUtils.POLLING_INTERVAL_MS);
|
134 | });
|
135 | };
|
136 | |
137 |
|
138 |
|
139 |
|
140 |
|
141 | WindowUtils.loadFrame = function (urlNavigate, frameName, timeoutMs, logger) {
|
142 | var _this = this;
|
143 | |
144 |
|
145 |
|
146 |
|
147 | logger.infoPii("LoadFrame: " + frameName);
|
148 | return new Promise(function (resolve, reject) {
|
149 | setTimeout(function () {
|
150 | var frameHandle = _this.loadFrameSync(urlNavigate, frameName, logger);
|
151 | if (!frameHandle) {
|
152 | reject("Unable to load iframe with name: " + frameName);
|
153 | return;
|
154 | }
|
155 | resolve(frameHandle);
|
156 | }, timeoutMs);
|
157 | });
|
158 | };
|
159 | |
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 | WindowUtils.loadFrameSync = function (urlNavigate, frameName, logger) {
|
167 | var frameHandle = WindowUtils.addHiddenIFrame(frameName, logger);
|
168 |
|
169 | if (!frameHandle) {
|
170 | return null;
|
171 | }
|
172 | else if (frameHandle.src === "" || frameHandle.src === "about:blank") {
|
173 | frameHandle.src = urlNavigate;
|
174 | logger.infoPii("Frame Name : " + frameName + " Navigated to: " + urlNavigate);
|
175 | }
|
176 | return frameHandle;
|
177 | };
|
178 | |
179 |
|
180 |
|
181 |
|
182 |
|
183 | WindowUtils.addHiddenIFrame = function (iframeId, logger) {
|
184 | if (typeof iframeId === "undefined") {
|
185 | return null;
|
186 | }
|
187 | logger.info("Add msal iframe to document");
|
188 | logger.infoPii("Add msal frame to document:" + iframeId);
|
189 | var adalFrame = document.getElementById(iframeId);
|
190 | if (!adalFrame) {
|
191 | logger.verbose("Add msal iframe does not exist");
|
192 | var ifr = document.createElement("iframe");
|
193 | ifr.setAttribute("id", iframeId);
|
194 | ifr.setAttribute("aria-hidden", "true");
|
195 | ifr.style.visibility = "hidden";
|
196 | ifr.style.position = "absolute";
|
197 | ifr.style.width = ifr.style.height = "0";
|
198 | ifr.style.border = "0";
|
199 | ifr.setAttribute("sandbox", "allow-scripts allow-same-origin allow-forms");
|
200 | adalFrame = document.getElementsByTagName("body")[0].appendChild(ifr);
|
201 | }
|
202 | else {
|
203 | logger.verbose("Add msal iframe already exists");
|
204 | }
|
205 | return adalFrame;
|
206 | };
|
207 | |
208 |
|
209 |
|
210 |
|
211 |
|
212 | WindowUtils.removeHiddenIframe = function (iframe) {
|
213 | if (document.body === iframe.parentNode) {
|
214 | document.body.removeChild(iframe);
|
215 | }
|
216 | };
|
217 | |
218 |
|
219 |
|
220 |
|
221 |
|
222 | WindowUtils.getIframeWithHash = function (hash) {
|
223 | var iframes = document.getElementsByTagName("iframe");
|
224 | var iframeArray = Array.apply(null, Array(iframes.length)).map(function (iframe, index) { return iframes.item(index); });
|
225 | return iframeArray.filter(function (iframe) {
|
226 | try {
|
227 | return iframe.contentWindow.location.hash === hash;
|
228 | }
|
229 | catch (e) {
|
230 | return false;
|
231 | }
|
232 | })[0];
|
233 | };
|
234 | |
235 |
|
236 |
|
237 |
|
238 |
|
239 | WindowUtils.getPopups = function () {
|
240 | if (!window.openedWindows) {
|
241 | window.openedWindows = [];
|
242 | }
|
243 | return window.openedWindows;
|
244 | };
|
245 | |
246 |
|
247 |
|
248 |
|
249 |
|
250 | WindowUtils.getPopUpWithHash = function (hash) {
|
251 | return WindowUtils.getPopups().filter(function (popup) {
|
252 | try {
|
253 | return popup.location.hash === hash;
|
254 | }
|
255 | catch (e) {
|
256 | return false;
|
257 | }
|
258 | })[0];
|
259 | };
|
260 | |
261 |
|
262 |
|
263 |
|
264 |
|
265 | WindowUtils.trackPopup = function (popup) {
|
266 | WindowUtils.getPopups().push(popup);
|
267 | };
|
268 | |
269 |
|
270 |
|
271 |
|
272 |
|
273 | WindowUtils.closePopups = function () {
|
274 | WindowUtils.getPopups().forEach(function (popup) { return popup.close(); });
|
275 | };
|
276 | |
277 |
|
278 |
|
279 |
|
280 |
|
281 | WindowUtils.blockReloadInHiddenIframes = function () {
|
282 |
|
283 | if (UrlUtils_1.UrlUtils.urlContainsHash(window.location.hash) && WindowUtils.isInIframe()) {
|
284 | throw ClientAuthError_1.ClientAuthError.createBlockTokenRequestsInHiddenIframeError();
|
285 | }
|
286 | };
|
287 | |
288 |
|
289 |
|
290 |
|
291 | WindowUtils.checkIfBackButtonIsPressed = function (cacheStorage) {
|
292 | var redirectCache = cacheStorage.getItem(Constants_1.TemporaryCacheKeys.REDIRECT_REQUEST);
|
293 |
|
294 | if (redirectCache && !UrlUtils_1.UrlUtils.urlContainsHash(window.location.hash)) {
|
295 | var splitCache = redirectCache.split(Constants_1.Constants.resourceDelimiter);
|
296 | splitCache.shift();
|
297 | var state = splitCache.length > 0 ? splitCache.join(Constants_1.Constants.resourceDelimiter) : null;
|
298 | cacheStorage.resetTempCacheItems(state);
|
299 | }
|
300 | };
|
301 | |
302 |
|
303 |
|
304 | WindowUtils.clearUrlFragment = function (contentWindow) {
|
305 | contentWindow.location.hash = "";
|
306 |
|
307 | if (typeof contentWindow.history.replaceState === "function") {
|
308 |
|
309 | contentWindow.history.replaceState(null, null, "" + contentWindow.location.pathname + contentWindow.location.search);
|
310 | }
|
311 | };
|
312 | |
313 |
|
314 |
|
315 |
|
316 |
|
317 | WindowUtils.POLLING_INTERVAL_MS = 50;
|
318 | return WindowUtils;
|
319 | }());
|
320 | exports.WindowUtils = WindowUtils;
|
321 |
|
\ | No newline at end of file |