{"version":3,"file":"BrowserUtils.js","sources":["../../src/utils/BrowserUtils.ts"],"sourcesContent":["/*\n * Copyright (c) Microsoft Corporation. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { Constants, INetworkModule, UrlString } from \"@azure/msal-common\";\nimport { FetchClient } from \"../network/FetchClient\";\nimport { XhrClient } from \"../network/XhrClient\";\nimport { BrowserAuthError } from \"../error/BrowserAuthError\";\nimport { InteractionType, BrowserConstants } from \"./BrowserConstants\";\n\n/**\n * Utility class for browser specific functions\n */\nexport class BrowserUtils {\n\n // #region Window Navigation and URL management\n\n /**\n * Clears hash from window url.\n */\n static clearHash(contentWindow: Window): void {\n // Office.js sets history.replaceState to null\n contentWindow.location.hash = Constants.EMPTY_STRING;\n if (typeof contentWindow.history.replaceState === \"function\") {\n // Full removes \"#\" from url\n contentWindow.history.replaceState(null, Constants.EMPTY_STRING, `${contentWindow.location.origin}${contentWindow.location.pathname}${contentWindow.location.search}`);\n }\n }\n\n /**\n * Replaces current hash with hash from provided url\n */\n static replaceHash(url: string): void {\n const urlParts = url.split(\"#\");\n urlParts.shift(); // Remove part before the hash\n window.location.hash = urlParts.length > 0 ? urlParts.join(\"#\") : Constants.EMPTY_STRING;\n }\n\n /**\n * Returns boolean of whether the current window is in an iframe or not.\n */\n static isInIframe(): boolean {\n return window.parent !== window;\n }\n\n /**\n * Returns boolean of whether or not the current window is a popup opened by msal\n */\n static isInPopup(): boolean {\n return typeof window !== \"undefined\" && !!window.opener && \n window.opener !== window && \n typeof window.name === \"string\" && \n window.name.indexOf(`${BrowserConstants.POPUP_NAME_PREFIX}.`) === 0;\n }\n\n // #endregion\n\n /**\n * Returns current window URL as redirect uri\n */\n static getCurrentUri(): string {\n return window.location.href.split(\"?\")[0].split(\"#\")[0];\n }\n\n /**\n * Gets the homepage url for the current window location.\n */\n static getHomepage(): string {\n const currentUrl = new UrlString(window.location.href);\n const urlComponents = currentUrl.getUrlComponents();\n return `${urlComponents.Protocol}//${urlComponents.HostNameAndPort}/`;\n }\n\n /**\n * Returns best compatible network client object. \n */\n static getBrowserNetworkClient(): INetworkModule {\n if (window.fetch && window.Headers) {\n return new FetchClient();\n } else {\n return new XhrClient();\n }\n }\n\n /**\n * Throws error if we have completed an auth and are \n * attempting another auth request inside an iframe.\n */\n static blockReloadInHiddenIframes(): void {\n const isResponseHash = UrlString.hashContainsKnownProperties(window.location.hash);\n // return an error if called from the hidden iframe created by the msal js silent calls\n if (isResponseHash && BrowserUtils.isInIframe()) {\n throw BrowserAuthError.createBlockReloadInHiddenIframeError();\n }\n }\n\n /**\n * Block redirect operations in iframes unless explicitly allowed\n * @param interactionType Interaction type for the request\n * @param allowRedirectInIframe Config value to allow redirects when app is inside an iframe\n */\n static blockRedirectInIframe(interactionType: InteractionType, allowRedirectInIframe: boolean): void {\n const isIframedApp = BrowserUtils.isInIframe();\n if (interactionType === InteractionType.Redirect && isIframedApp && !allowRedirectInIframe) {\n // If we are not in top frame, we shouldn't redirect. This is also handled by the service.\n throw BrowserAuthError.createRedirectInIframeError(isIframedApp);\n }\n }\n\n /**\n * Block redirectUri loaded in popup from calling AcquireToken APIs\n */\n static blockAcquireTokenInPopups(): void {\n // Popups opened by msal popup APIs are given a name that starts with \"msal.\"\n if (BrowserUtils.isInPopup()) {\n throw BrowserAuthError.createBlockAcquireTokenInPopupsError();\n }\n }\n\n /**\n * Throws error if token requests are made in non-browser environment\n * @param isBrowserEnvironment Flag indicating if environment is a browser.\n */\n static blockNonBrowserEnvironment(isBrowserEnvironment: boolean): void {\n if (!isBrowserEnvironment) {\n throw BrowserAuthError.createNonBrowserEnvironmentError();\n }\n }\n\n /**\n * Throws error if native brokering is enabled but initialize hasn't been called\n * @param allowNativeBroker \n * @param initialized \n */\n static blockNativeBrokerCalledBeforeInitialized(allowNativeBroker: boolean, initialized: boolean): void {\n if (allowNativeBroker && !initialized) {\n throw BrowserAuthError.createNativeBrokerCalledBeforeInitialize();\n }\n }\n\n /**\n * Returns boolean of whether current browser is an Internet Explorer or Edge browser.\n */\n static detectIEOrEdge(): boolean {\n const ua = window.navigator.userAgent;\n const msie = ua.indexOf(\"MSIE \");\n const msie11 = ua.indexOf(\"Trident/\");\n const msedge = ua.indexOf(\"Edge/\");\n const isIE = msie > 0 || msie11 > 0;\n const isEdge = msedge > 0;\n return isIE || isEdge;\n }\n}\n"],"names":[],"mappings":";;;;;;;;AAAA;;;;AAWA;;;;IAGA;KA2IC;;;;;IApIU,sBAAS,GAAhB,UAAiB,aAAqB;;QAElC,aAAa,CAAC,QAAQ,CAAC,IAAI,GAAG,SAAS,CAAC,YAAY,CAAC;QACrD,IAAI,OAAO,aAAa,CAAC,OAAO,CAAC,YAAY,KAAK,UAAU,EAAE;;YAE1D,aAAa,CAAC,OAAO,CAAC,YAAY,CAAC,IAAI,EAAE,SAAS,CAAC,YAAY,EAAE,KAAG,aAAa,CAAC,QAAQ,CAAC,MAAM,GAAG,aAAa,CAAC,QAAQ,CAAC,QAAQ,GAAG,aAAa,CAAC,QAAQ,CAAC,MAAQ,CAAC,CAAC;SAC1K;KACJ;;;;IAKM,wBAAW,GAAlB,UAAmB,GAAW;QAC1B,IAAM,QAAQ,GAAG,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAChC,QAAQ,CAAC,KAAK,EAAE,CAAC;QACjB,MAAM,CAAC,QAAQ,CAAC,IAAI,GAAG,QAAQ,CAAC,MAAM,GAAG,CAAC,GAAG,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC,YAAY,CAAC;KAC5F;;;;IAKM,uBAAU,GAAjB;QACI,OAAO,MAAM,CAAC,MAAM,KAAK,MAAM,CAAC;KACnC;;;;IAKM,sBAAS,GAAhB;QACI,OAAO,OAAO,MAAM,KAAK,WAAW,IAAI,CAAC,CAAC,MAAM,CAAC,MAAM;YACnD,MAAM,CAAC,MAAM,KAAK,MAAM;YACxB,OAAO,MAAM,CAAC,IAAI,KAAK,QAAQ;YAC/B,MAAM,CAAC,IAAI,C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