UNPKG

@azure/msal-browser

Version:
82 lines (79 loc) • 3.25 kB
/*! @azure/msal-browser v5.30.0 2026-11-18 */ 'use strict'; import { AuthError, Constants } from '@azure/msal-common/browser'; import { clearAuthResponseFromUrl } from './BrowserUtils.mjs'; import { timedOut } from '../error/BrowserAuthErrorCodes.mjs'; /* * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. */ const IFRAME_POLL_INTERVAL_MS = 50; const DEFAULT_IFRAME_TIMEOUT_MS = 10000; /** * Monitors an iframe content window until it loads a URL with a known hash, or hits a specified timeout. * * @param iframe - The hidden iframe to monitor. * @param timeout - Maximum time (ms) to wait for navigation before rejecting with * `monitor_window_timeout`. * @param logger - Logger instance. * @param correlationId * @param responseMode - `"query"` or `"fragment"` — determines whether to read * `location.search` or `location.hash`. Any value other than `"query"` is * treated as fragment. */ async function monitorIframeForHash(iframe, timeout, logger, correlationId, responseMode) { return new Promise((resolve, reject) => { if (timeout < DEFAULT_IFRAME_TIMEOUT_MS) { logger.warning(`137tec ${timeout} ${DEFAULT_IFRAME_TIMEOUT_MS}`, correlationId); } /* * Polling for iframes can be purely timing based, * since we don't need to account for interaction. */ const timeoutId = window.setTimeout(() => { window.clearInterval(intervalId); reject(new AuthError(timedOut, correlationId, undefined, "monitor_window_timeout")); }, timeout); const intervalId = window.setInterval(() => { const contentWindow = iframe.contentWindow; if (!contentWindow) { return; } let href = ""; try { /* * Will throw if cross origin, * which should be caught and ignored * since we need the interval to keep running while on STS UI. */ href = contentWindow.location.href; } catch (e) { } if (!href || href === "about:blank") { return; } const responseString = responseMode === Constants.ResponseMode.QUERY ? contentWindow.location.search : contentWindow.location.hash; window.clearTimeout(timeoutId); window.clearInterval(intervalId); resolve(responseString); }, IFRAME_POLL_INTERVAL_MS); }).finally(() => { /* * Strip auth-response params from the iframe URL. The monitor owns the * iframe for the duration of the poll, mirroring cleanPopup in * PopupMonitor. */ try { if (iframe.contentWindow) { clearAuthResponseFromUrl(iframe.contentWindow); } } catch { // Iframe may already be navigated away or cross-origin; ignore. } }); } export { DEFAULT_IFRAME_TIMEOUT_MS, IFRAME_POLL_INTERVAL_MS, monitorIframeForHash }; //# sourceMappingURL=IframeMonitor.mjs.map