UNPKG

@azure/msal-browser

Version:
177 lines (174 loc) • 7.97 kB
/*! @azure/msal-browser v5.30.0 2026-11-18 */ 'use strict'; import { StubPerformanceClient, ProtocolMode, createClientConfigurationError, ClientConfigurationErrorCodes, LogLevel, DEFAULT_SYSTEM_OPTIONS, Logger, Constants, AzureCloudInstance, StubbedNetworkModule } from '@azure/msal-common/browser'; import { BrowserCacheLocation } from '../utils/BrowserConstants.mjs'; import { topOpenerIsSameOriginOrAbsent } from '../utils/PopupOriginCheck.mjs'; import { NavigationClient } from '../navigation/NavigationClient.mjs'; import { FetchClient } from '../network/FetchClient.mjs'; import { name, version } from '../packageMetadata.mjs'; /* * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. */ // Default timeout for popup windows and iframes in milliseconds const DEFAULT_POPUP_TIMEOUT_MS = 60000; const DEFAULT_IFRAME_TIMEOUT_MS = 10000; const DEFAULT_REDIRECT_TIMEOUT_MS = 30000; const DEFAULT_NATIVE_BROKER_HANDSHAKE_TIMEOUT_MS = 2000; /** * MSAL function that sets the default options when not explicitly configured from app developer * * @param auth * @param cache * @param system * * @returns Configuration object */ function buildConfiguration({ auth: userInputAuth, cache: userInputCache, system: userInputSystem, experimental: userInputExperimental, telemetry: userInputTelemetry, }, isBrowserEnvironment) { // Default auth options for browser const DEFAULT_AUTH_OPTIONS = { clientId: "", authority: `${Constants.DEFAULT_AUTHORITY}`, knownAuthorities: [], cloudDiscoveryMetadata: "", authorityMetadata: "", redirectUri: typeof window !== "undefined" && window.location ? window.location.href.split("?")[0].split("#")[0] : "", postLogoutRedirectUri: "", popupRelayUri: "", popupRelayProtocolVersion: 1, clientCapabilities: [], OIDCOptions: { responseMode: Constants.ResponseMode.FRAGMENT, defaultScopes: [ Constants.OPENID_SCOPE, Constants.PROFILE_SCOPE, Constants.OFFLINE_ACCESS_SCOPE, ], }, azureCloudOptions: { azureCloudInstance: AzureCloudInstance.None, tenant: "", }, instanceAware: false, isMcp: false, verifySSO: false, // Legacy polling needs COOP omitted, so it implies asserting pocd=1. originCheck: !userInputSystem?.enableLegacyPolling, }; // Default cache options for browser const DEFAULT_CACHE_OPTIONS = { cacheLocation: BrowserCacheLocation.SessionStorage, cacheRetentionDays: 5, }; // Default logger options for browser const DEFAULT_LOGGER_OPTIONS = { // eslint-disable-next-line @typescript-eslint/no-empty-function loggerCallback: () => { // allow users to not set logger call back }, logLevel: LogLevel.Info, piiLoggingEnabled: false, }; // Default system options for browser const DEFAULT_BROWSER_SYSTEM_OPTIONS = { ...DEFAULT_SYSTEM_OPTIONS, loggerOptions: DEFAULT_LOGGER_OPTIONS, networkClient: isBrowserEnvironment ? new FetchClient() : StubbedNetworkModule, navigationClient: new NavigationClient(), popupBridgeTimeout: userInputSystem?.popupBridgeTimeout || DEFAULT_POPUP_TIMEOUT_MS, iframeBridgeTimeout: userInputSystem?.iframeBridgeTimeout || DEFAULT_IFRAME_TIMEOUT_MS, redirectNavigationTimeout: DEFAULT_REDIRECT_TIMEOUT_MS, allowRedirectInIframe: false, navigatePopups: true, allowPlatformBroker: false, nativeBrokerHandshakeTimeout: userInputSystem?.nativeBrokerHandshakeTimeout || DEFAULT_NATIVE_BROKER_HANDSHAKE_TIMEOUT_MS, protocolMode: ProtocolMode.AAD, serverTelemetryEnabled: false, enableLegacyPolling: false, }; const providedSystemOptions = { ...DEFAULT_BROWSER_SYSTEM_OPTIONS, ...userInputSystem, loggerOptions: userInputSystem?.loggerOptions || DEFAULT_LOGGER_OPTIONS, }; const DEFAULT_TELEMETRY_OPTIONS = { application: { appName: "", appVersion: "", }, client: new StubPerformanceClient(), }; const DEFAULT_EXPERIMENTAL_OPTIONS = { enableAsymmetricEar: false, iframeTimeoutTelemetry: false, allowPlatformBrokerWithDOM: false, }; let logger; const getLogger = () => { if (!logger) { logger = new Logger(providedSystemOptions.loggerOptions, name, version); } return logger; }; // Throw an error if user has set OIDCOptions without being in OIDC protocol mode if (userInputSystem?.protocolMode !== ProtocolMode.OIDC && userInputAuth?.OIDCOptions) { getLogger().warning(JSON.stringify(createClientConfigurationError(ClientConfigurationErrorCodes.cannotSetOIDCOptions, "")), ""); } // Throw an error if user has set allowPlatformBroker to true with OIDC protocol mode if (userInputSystem?.protocolMode && userInputSystem.protocolMode === ProtocolMode.OIDC && providedSystemOptions?.allowPlatformBroker) { throw createClientConfigurationError(ClientConfigurationErrorCodes.cannotAllowPlatformBroker, ""); } /* * The origin check computes 0 in the embedded contexts legacy polling * serves, so re-enabling it there breaks the flow. Honor it, but warn. */ if (userInputSystem?.enableLegacyPolling && userInputAuth?.originCheck === true) { getLogger().warning("buildConfiguration: system.enableLegacyPolling is true and auth.originCheck is explicitly true. Legacy polling requires pocd=1, which the origin check will not produce in an embedded context. Unset auth.originCheck to let legacy polling assert it.", ""); } /* * Opting out is a deliberate risk transfer, not an error — but the app * cannot know at config time that the risky topology is actually present. */ if ((userInputAuth?.originCheck ?? DEFAULT_AUTH_OPTIONS.originCheck) === false && typeof window !== "undefined" && !topOpenerIsSameOriginOrAbsent(window)) { getLogger().warning("buildConfiguration: auth.originCheck is disabled and the top-level window has a cross-origin opener. MSAL will assert pocd=1, so the STS omits Cross-Origin-Opener-Policy and that opener can reach the auth popup. The application is responsible for this risk.", ""); } const overlayedConfig = { auth: { ...DEFAULT_AUTH_OPTIONS, ...userInputAuth, OIDCOptions: { ...DEFAULT_AUTH_OPTIONS.OIDCOptions, ...userInputAuth?.OIDCOptions, }, }, cache: { ...DEFAULT_CACHE_OPTIONS, ...userInputCache }, system: providedSystemOptions, experimental: { ...DEFAULT_EXPERIMENTAL_OPTIONS, ...userInputExperimental, enableAsymmetricEar: userInputExperimental?.enableAsymmetricEar ?? false, }, telemetry: { ...DEFAULT_TELEMETRY_OPTIONS, ...userInputTelemetry }, }; if (overlayedConfig.experimental.enableAsymmetricEar && overlayedConfig.system.protocolMode !== ProtocolMode.EAR) { const logger = new Logger(providedSystemOptions.loggerOptions, name, version); logger.warning("07xxx7", ""); overlayedConfig.experimental.enableAsymmetricEar = false; } return overlayedConfig; } export { DEFAULT_IFRAME_TIMEOUT_MS, DEFAULT_NATIVE_BROKER_HANDSHAKE_TIMEOUT_MS, DEFAULT_POPUP_TIMEOUT_MS, DEFAULT_REDIRECT_TIMEOUT_MS, buildConfiguration }; //# sourceMappingURL=Configuration.mjs.map