@azure/msal-browser
Version:
Microsoft Authentication Library for js
120 lines (117 loc) • 4.93 kB
JavaScript
/*! @azure/msal-browser v5.30.0 2026-11-18 */
;
import { Constants } from '@azure/msal-common/browser';
import { BrowserAuthError } from '../error/BrowserAuthError.mjs';
import { userCancelled, interactionInProgressCancelled } from '../error/BrowserAuthErrorCodes.mjs';
import { clearAuthResponseFromUrl } from './BrowserUtils.mjs';
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
const POPUP_POLL_INTERVAL_MS = 30;
let activePopupPoller;
/**
* Cancels legacy popup polling when an interaction is superseded.
*
* @internal
* @param logger Logger for the interaction.
*/
function cancelPendingPopupPollingResponse(logger) {
if (activePopupPoller) {
logger.verbose("0g9n3p", activePopupPoller.correlationId);
activePopupPoller.cancel();
}
}
/**
* Monitors a popup window for a URL change to the same origin as the parent application.
* Polls the popup at a specified interval until it is redirected back to the application,
* closed by the user, or a navigation to a same-origin URL is detected. Once a same-origin
* URL is detected, extracts the response string (query or hash) and resolves the promise.
*
* @param popupWindow - The popup window to monitor for navigation.
* @param popupWindowParent - The parent window that opened the popup.
* @param responseMode - Response mode (`"query"` | `"fragment"`) determines whether
* to read `location.search` or `location.hash` from the popup once on the same
* origin. Any value other than `"query"` is treated as fragment.
* @param logger - Logger instance.
* @param unloadWindow - Event handler to remove from the parent window on cleanup.
* @param correlationId
*/
async function monitorPopupForHash(popupWindow, popupWindowParent, responseMode, logger, unloadWindow, correlationId) {
let poller;
return new Promise((resolve, reject) => {
logger.verbose("0cp91u", correlationId);
const intervalId = window.setInterval(() => {
if (popupWindow.closed) {
logger.error("0n69wb", correlationId);
window.clearInterval(intervalId);
reject(new BrowserAuthError(userCancelled, correlationId));
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 = popupWindow.location.href;
}
catch (e) { }
// Don't process blank pages or cross domain
if (!href || href === "about:blank") {
return;
}
window.clearInterval(intervalId);
let responseString;
if (responseMode === Constants.ResponseMode.QUERY) {
responseString = popupWindow.location.search;
}
else {
responseString = popupWindow.location.hash;
}
logger.verbose("0po9er", correlationId);
resolve(responseString);
}, POPUP_POLL_INTERVAL_MS);
poller = {
correlationId,
cancel: () => {
window.clearInterval(intervalId);
if (activePopupPoller === poller) {
activePopupPoller = undefined;
}
reject(new BrowserAuthError(interactionInProgressCancelled, correlationId));
},
};
activePopupPoller = poller;
}).finally(() => {
if (poller && activePopupPoller === poller) {
activePopupPoller = undefined;
}
cleanPopup(popupWindow, popupWindowParent, unloadWindow);
});
}
/**
* Performs cleanup operations after popup authentication.
* Strips auth-response params (code/state) from the popup URL while it is still
* open, closes the popup window, and removes the 'beforeunload' event listener
* from the parent window.
*
* @param popupWindow - The popup window to be closed.
* @param popupWindowParent - The parent window from which the event listener will be removed.
* @param unloadWindow - The event handler function to remove.
*/
function cleanPopup(popupWindow, popupWindowParent, unloadWindow) {
if (!popupWindow.closed) {
try {
clearAuthResponseFromUrl(popupWindow);
}
catch {
// Popup may have become cross-origin or otherwise inaccessible; ignore.
}
}
popupWindow.close();
popupWindowParent.removeEventListener("beforeunload", unloadWindow);
}
export { POPUP_POLL_INTERVAL_MS, cancelPendingPopupPollingResponse, cleanPopup, monitorPopupForHash };
//# sourceMappingURL=PopupMonitor.mjs.map