{"version":3,"file":"BaseInteractionClient.js","sources":["../../src/interaction_client/BaseInteractionClient.ts"],"sourcesContent":["/*\r\n * Copyright (c) Microsoft Corporation. All rights reserved.\r\n * Licensed under the MIT License.\r\n */\r\n\r\nimport { ICrypto, INetworkModule, Logger, AuthenticationResult, AccountInfo, AccountEntity, BaseAuthRequest, AuthenticationScheme, UrlString, ServerTelemetryManager, ServerTelemetryRequest, ClientConfigurationError, StringUtils, Authority, AuthorityOptions, AuthorityFactory, IPerformanceClient, PerformanceEvents } from \"@azure/msal-common\";\r\nimport { BrowserConfiguration } from \"../config/Configuration\";\r\nimport { BrowserCacheManager } from \"../cache/BrowserCacheManager\";\r\nimport { EventHandler } from \"../event/EventHandler\";\r\nimport { EndSessionRequest } from \"../request/EndSessionRequest\";\r\nimport { RedirectRequest } from \"../request/RedirectRequest\";\r\nimport { PopupRequest } from \"../request/PopupRequest\";\r\nimport { SsoSilentRequest } from \"../request/SsoSilentRequest\";\r\nimport { version } from \"../packageMetadata\";\r\nimport { BrowserConstants } from \"../utils/BrowserConstants\";\r\nimport { BrowserUtils } from \"../utils/BrowserUtils\";\r\nimport { INavigationClient } from \"../navigation/INavigationClient\";\r\nimport { NativeMessageHandler } from \"../broker/nativeBroker/NativeMessageHandler\";\r\n\r\nexport abstract class BaseInteractionClient {\r\n\r\n protected config: BrowserConfiguration;\r\n protected browserStorage: BrowserCacheManager;\r\n protected browserCrypto: ICrypto;\r\n protected networkClient: INetworkModule;\r\n protected logger: Logger;\r\n protected eventHandler: EventHandler;\r\n protected navigationClient: INavigationClient;\r\n protected nativeMessageHandler: NativeMessageHandler | undefined;\r\n protected correlationId: string;\r\n protected performanceClient: IPerformanceClient;\r\n\r\n constructor(config: BrowserConfiguration, storageImpl: BrowserCacheManager, browserCrypto: ICrypto, logger: Logger, eventHandler: EventHandler, navigationClient: INavigationClient, performanceClient: IPerformanceClient, nativeMessageHandler?: NativeMessageHandler, correlationId?: string) {\r\n this.config = config;\r\n this.browserStorage = storageImpl;\r\n this.browserCrypto = browserCrypto;\r\n this.networkClient = this.config.system.networkClient;\r\n this.eventHandler = eventHandler;\r\n this.navigationClient = navigationClient;\r\n this.nativeMessageHandler = nativeMessageHandler;\r\n this.correlationId = correlationId || this.browserCrypto.createNewGuid();\r\n this.logger = logger.clone(BrowserConstants.MSAL_SKU, version, this.correlationId);\r\n this.performanceClient = performanceClient;\r\n }\r\n\r\n abstract acquireToken(request: RedirectRequest|PopupRequest|SsoSilentRequest): Promise;\r\n\r\n abstract logout(request: EndSessionRequest): Promise;\r\n\r\n protected async clearCacheOnLogout(account?: AccountInfo| null): Promise {\r\n if (account) {\r\n if (AccountEntity.accountInfoIsEqual(account, this.browserStorage.getActiveAccount(), false)) {\r\n this.logger.verbose(\"Setting active account to null\");\r\n this.browserStorage.setActiveAccount(null);\r\n }\r\n // Clear given account.\r\n try {\r\n await this.browserStorage.removeAccount(AccountEntity.generateAccountCacheKey(account));\r\n this.logger.verbose(\"Cleared cache items belonging to the account provided in the logout request.\");\r\n } catch (error) {\r\n this.logger.error(\"Account provided in logout request was not found. Local cache unchanged.\");\r\n }\r\n } else {\r\n try {\r\n this.logger.verbose(\"No account provided in logout request, clearing all cache items.\", this.correlationId);\r\n // Clear all accounts and tokens\r\n await this.browserStorage.clear();\r\n // Clear any stray keys from IndexedDB\r\n await this.browserCrypto.clearKeystore();\r\n } catch(e) {\r\n this.logger.error(\"Attempted to clear all MSAL cache items and failed. Local cache unchanged.\");\r\n }\r\n }\r\n }\r\n\r\n /**\r\n * Initializer function for all request APIs\r\n * @param request\r\n */\r\n protected async initializeBaseRequest(request: Partial, account?: AccountInfo): Promise {\r\n this.performanceClient.addQueueMeasurement(PerformanceEvents.InitializeBaseRequest, request.correlationId);\r\n this.logger.verbose(\"Initializing BaseAuthRequest\");\r\n const authority = request.authority || this.config.auth.authority;\r\n\r\n if (account) {\r\n await this.validateRequestAuthority(authority, account);\r\n }\r\n\r\n const scopes = [...((request && request.scopes) || [])];\r\n\r\n const validatedRequest: BaseAuthRequest = {\r\n ...request,\r\n correlationId: this.correlationId,\r\n authority,\r\n scopes\r\n };\r\n\r\n // Set authenticationScheme to BEARER if not explicitly set in the request\r\n if (!validatedRequest.authenticationScheme) {\r\n validatedRequest.authenticationScheme = AuthenticationScheme.BEARER;\r\n this.logger.verbose(\"Authentication Scheme wasn't explicitly set in request, defaulting to \\\"Bearer\\\" request\");\r\n } else {\r\n if (validatedRequest.authenticationScheme === AuthenticationScheme.SSH) {\r\n if (!request.sshJwk) {\r\n throw ClientConfigurationError.createMissingSshJwkError();\r\n }\r\n if(!request.sshKid) {\r\n throw ClientConfigurationError.createMissingSshKidError();\r\n }\r\n }\r\n this.logger.verbose(`Authentication Scheme set to \"${validatedRequest.authenticationScheme}\" as configured in Auth request`);\r\n }\r\n\r\n // Set requested claims hash if claims were requested and claims-based caching is enabled\r\n if (this.config.cache.claimsBasedCachingEnabled && request.claims && !StringUtils.isEmptyObj(request.claims)) {\r\n validatedRequest.requestedClaimsHash = await this.browserCrypto.hashString(request.claims);\r\n }\r\n\r\n return validatedRequest;\r\n }\r\n\r\n /**\r\n *\r\n * Use to get the redirect uri configured in MSAL or null.\r\n * @param requestRedirectUri\r\n * @returns Redirect URL\r\n *\r\n */\r\n getRedirectUri(requestRedirectUri?: string): string {\r\n this.logger.verbose(\"getRedirectUri called\");\r\n const redirectUri = requestRedirectUri || this.config.auth.redirectUri || BrowserUtils.getCurrentUri();\r\n return UrlString.getAbsoluteUrl(redirectUri, BrowserUtils.getCurrentUri());\r\n }\r\n\r\n /*\r\n * If authority provided in the request does not match environment/authority specified \r\n * in the account or MSAL config, we throw an error.\r\n */\r\n async validateRequestAuthority(authority: string, account: AccountInfo): Promise {\r\n const discoveredAuthority = await this.getDiscoveredAuthority(authority);\r\n \r\n if(!discoveredAuthority.isAlias(account.environment)) {\r\n throw ClientConfigurationError.createAuthorityMismatchError();\r\n }\r\n }\r\n\r\n /**\r\n *\r\n * @param apiId\r\n * @param correlationId\r\n * @param forceRefresh\r\n */\r\n protected initializeServerTelemetryManager(apiId: number, forceRefresh?: boolean): ServerTelemetryManager {\r\n this.logger.verbose(\"initializeServerTelemetryManager called\");\r\n const telemetryPayload: ServerTelemetryRequest = {\r\n clientId: this.config.auth.clientId,\r\n correlationId: this.correlationId,\r\n apiId: apiId,\r\n forceRefresh: forceRefresh || false,\r\n wrapperSKU: this.browserStorage.getWrapperMetadata()[0],\r\n wrapperVer: this.browserStorage.getWrapperMetadata()[1]\r\n };\r\n\r\n return new ServerTelemetryManager(telemetryPayload, this.browserStorage);\r\n }\r\n\r\n /**\r\n * Used to get a discovered version of the default authority.\r\n * @param requestAuthority\r\n * @param requestCorrelationId\r\n */\r\n protected async getDiscoveredAuthority(requestAuthority?: string): Promise {\r\n this.logger.verbose(\"getDiscoveredAuthority called\");\r\n const authorityOptions: AuthorityOptions = {\r\n protocolMode: this.config.auth.protocolMode,\r\n knownAuthorities: this.config.auth.knownAuthorities,\r\n cloudDiscoveryMetadata: this.config.auth.cloudDiscoveryMetadata,\r\n authorityMetadata: this.config.auth.authorityMetadata\r\n };\r\n\r\n if (requestAuthority) {\r\n this.logger.verbose(\"Creating discovered authority with request authority\");\r\n return await AuthorityFactory.createDiscoveredInstance(requestAuthority, this.config.system.networkClient, this.browserStorage, authorityOptions, this.logger);\r\n }\r\n\r\n this.logger.verbose(\"Creating discovered authority with configured authority\");\r\n return await AuthorityFactory.createDiscoveredInstance(this.config.auth.authority, this.config.system.networkClient, this.browserStorage, authorityOptions, this.logger);\r\n }\r\n}\r\n"],"names":[],"mappings":";;;;;;;;AAAA;;;;;IAgCI,+BAAY,MAA4B,EAAE,WAAgC,EAAE,aAAsB,EAAE,MAAc,EAAE,YAA0B,EAAE,gBAAmC,EAAE,iBAAqC,EAAE,oBAA2C,EAAE,aAAsB;QAC3R,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,cAAc,GAAG,WAAW,CAAC;QAClC,IAAI,CAAC,aAAa,GAAG,aA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