@azure/msal-browser 3.0.0-alpha.1 → 3.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (142) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +213 -213
  3. package/dist/app/IPublicClientApplication.d.ts +45 -45
  4. package/dist/app/IPublicClientApplication.js +96 -96
  5. package/dist/app/PublicClientApplication.d.ts +244 -244
  6. package/dist/app/PublicClientApplication.js +307 -307
  7. package/dist/broker/nativeBroker/NativeMessageHandler.d.ts +63 -63
  8. package/dist/broker/nativeBroker/NativeMessageHandler.js +255 -255
  9. package/dist/broker/nativeBroker/NativeRequest.d.ts +43 -43
  10. package/dist/broker/nativeBroker/NativeResponse.d.ts +48 -48
  11. package/dist/cache/AsyncMemoryStorage.d.ts +51 -51
  12. package/dist/cache/AsyncMemoryStorage.js +132 -132
  13. package/dist/cache/BrowserCacheManager.d.ts +377 -377
  14. package/dist/cache/BrowserCacheManager.js +1276 -1276
  15. package/dist/cache/BrowserStorage.d.ts +11 -11
  16. package/dist/cache/BrowserStorage.js +35 -35
  17. package/dist/cache/CryptoKeyStore.d.ts +17 -17
  18. package/dist/cache/CryptoKeyStore.js +41 -41
  19. package/dist/cache/DatabaseStorage.d.ts +56 -56
  20. package/dist/cache/DatabaseStorage.js +190 -190
  21. package/dist/cache/IAsyncMemoryStorage.d.ts +27 -27
  22. package/dist/cache/ITokenCache.d.ts +10 -10
  23. package/dist/cache/IWindowStorage.d.ts +27 -27
  24. package/dist/cache/MemoryStorage.d.ts +11 -11
  25. package/dist/cache/MemoryStorage.js +31 -31
  26. package/dist/cache/TokenCache.d.ts +76 -76
  27. package/dist/cache/TokenCache.js +225 -225
  28. package/dist/config/Configuration.d.ts +203 -203
  29. package/dist/config/Configuration.js +101 -101
  30. package/dist/controllers/ControllerFactory.d.ts +9 -9
  31. package/dist/controllers/ControllerFactory.js +37 -37
  32. package/dist/controllers/IController.d.ts +66 -66
  33. package/dist/controllers/StandardController.d.ts +413 -413
  34. package/dist/controllers/StandardController.js +1272 -1272
  35. package/dist/crypto/BrowserCrypto.d.ts +61 -61
  36. package/dist/crypto/BrowserCrypto.js +131 -131
  37. package/dist/crypto/CryptoOps.d.ts +73 -73
  38. package/dist/crypto/CryptoOps.js +150 -150
  39. package/dist/crypto/GuidGenerator.d.ts +12 -12
  40. package/dist/crypto/GuidGenerator.js +96 -96
  41. package/dist/crypto/ISubtleCrypto.d.ts +9 -9
  42. package/dist/crypto/ModernBrowserCrypto.d.ts +9 -9
  43. package/dist/crypto/ModernBrowserCrypto.js +24 -24
  44. package/dist/crypto/MsBrowserCrypto.d.ts +9 -9
  45. package/dist/crypto/MsBrowserCrypto.js +79 -79
  46. package/dist/crypto/MsrBrowserCrypto.d.ts +17 -17
  47. package/dist/crypto/MsrBrowserCrypto.js +28 -28
  48. package/dist/crypto/PkceGenerator.d.ts +24 -24
  49. package/dist/crypto/PkceGenerator.js +58 -58
  50. package/dist/crypto/SignedHttpRequest.d.ts +30 -30
  51. package/dist/crypto/SignedHttpRequest.js +39 -39
  52. package/dist/encode/Base64Decode.d.ts +22 -22
  53. package/dist/encode/Base64Decode.js +73 -73
  54. package/dist/encode/Base64Encode.d.ts +31 -31
  55. package/dist/encode/Base64Encode.js +79 -79
  56. package/dist/error/BrowserAuthError.d.ts +382 -382
  57. package/dist/error/BrowserAuthError.js +483 -483
  58. package/dist/error/BrowserConfigurationAuthError.d.ts +70 -70
  59. package/dist/error/BrowserConfigurationAuthError.js +96 -96
  60. package/dist/error/NativeAuthError.d.ts +56 -56
  61. package/dist/error/NativeAuthError.js +88 -88
  62. package/dist/event/EventHandler.d.ts +41 -41
  63. package/dist/event/EventHandler.js +119 -119
  64. package/dist/event/EventMessage.d.ts +25 -25
  65. package/dist/event/EventMessage.js +67 -67
  66. package/dist/event/EventType.d.ts +26 -26
  67. package/dist/event/EventType.js +31 -31
  68. package/dist/index.d.ts +35 -35
  69. package/dist/index.js +1 -1
  70. package/dist/interaction_client/BaseInteractionClient.d.ts +52 -52
  71. package/dist/interaction_client/BaseInteractionClient.js +138 -138
  72. package/dist/interaction_client/HybridSpaAuthorizationCodeClient.d.ts +4 -4
  73. package/dist/interaction_client/HybridSpaAuthorizationCodeClient.js +10 -10
  74. package/dist/interaction_client/NativeInteractionClient.d.ts +148 -148
  75. package/dist/interaction_client/NativeInteractionClient.js +516 -516
  76. package/dist/interaction_client/PopupClient.d.ts +111 -111
  77. package/dist/interaction_client/PopupClient.js +476 -476
  78. package/dist/interaction_client/RedirectClient.d.ts +48 -48
  79. package/dist/interaction_client/RedirectClient.js +290 -290
  80. package/dist/interaction_client/SilentAuthCodeClient.d.ts +22 -22
  81. package/dist/interaction_client/SilentAuthCodeClient.js +62 -62
  82. package/dist/interaction_client/SilentCacheClient.d.ts +21 -21
  83. package/dist/interaction_client/SilentCacheClient.js +67 -67
  84. package/dist/interaction_client/SilentIframeClient.d.ts +31 -31
  85. package/dist/interaction_client/SilentIframeClient.js +127 -127
  86. package/dist/interaction_client/SilentRefreshClient.d.ts +19 -19
  87. package/dist/interaction_client/SilentRefreshClient.js +62 -62
  88. package/dist/interaction_client/StandardInteractionClient.d.ts +59 -59
  89. package/dist/interaction_client/StandardInteractionClient.js +259 -259
  90. package/dist/interaction_handler/InteractionHandler.d.ts +39 -39
  91. package/dist/interaction_handler/InteractionHandler.js +127 -127
  92. package/dist/interaction_handler/RedirectHandler.d.ts +24 -24
  93. package/dist/interaction_handler/RedirectHandler.js +118 -118
  94. package/dist/interaction_handler/SilentHandler.d.ts +47 -47
  95. package/dist/interaction_handler/SilentHandler.js +162 -162
  96. package/dist/internals.d.ts +23 -23
  97. package/dist/internals.js +4 -4
  98. package/dist/navigation/INavigationClient.d.ts +16 -16
  99. package/dist/navigation/NavigationClient.d.ts +22 -22
  100. package/dist/navigation/NavigationClient.js +40 -40
  101. package/dist/navigation/NavigationOptions.d.ts +12 -12
  102. package/dist/network/FetchClient.d.ts +26 -26
  103. package/dist/network/FetchClient.js +99 -99
  104. package/dist/network/XhrClient.d.ts +40 -40
  105. package/dist/network/XhrClient.js +117 -117
  106. package/dist/operatingcontext/BaseOperatingContext.d.ts +40 -40
  107. package/dist/operatingcontext/BaseOperatingContext.js +44 -44
  108. package/dist/operatingcontext/StandardOperatingContext.d.ts +25 -25
  109. package/dist/operatingcontext/StandardOperatingContext.js +43 -43
  110. package/dist/operatingcontext/TeamsAppOperatingContext.d.ts +25 -25
  111. package/dist/operatingcontext/TeamsAppOperatingContext.js +42 -42
  112. package/dist/packageMetadata.d.ts +2 -2
  113. package/dist/packageMetadata.js +4 -4
  114. package/dist/request/AuthorizationCodeRequest.d.ts +8 -8
  115. package/dist/request/AuthorizationUrlRequest.d.ts +8 -8
  116. package/dist/request/EndSessionPopupRequest.d.ts +18 -18
  117. package/dist/request/EndSessionRequest.d.ts +15 -15
  118. package/dist/request/PopupRequest.d.ts +32 -32
  119. package/dist/request/PopupWindowAttributes.d.ts +15 -15
  120. package/dist/request/RedirectRequest.d.ts +33 -33
  121. package/dist/request/SilentRequest.d.ts +30 -30
  122. package/dist/request/SsoSilentRequest.d.ts +26 -26
  123. package/dist/telemetry/BrowserPerformanceClient.d.ts +38 -38
  124. package/dist/telemetry/BrowserPerformanceClient.js +124 -124
  125. package/dist/telemetry/BrowserPerformanceMeasurement.d.ts +21 -21
  126. package/dist/telemetry/BrowserPerformanceMeasurement.js +92 -92
  127. package/dist/utils/BrowserConstants.d.ts +166 -166
  128. package/dist/utils/BrowserConstants.js +214 -214
  129. package/dist/utils/BrowserProtocolUtils.d.ts +18 -18
  130. package/dist/utils/BrowserProtocolUtils.js +34 -34
  131. package/dist/utils/BrowserStringUtils.d.ts +26 -26
  132. package/dist/utils/BrowserStringUtils.js +149 -149
  133. package/dist/utils/BrowserUtils.d.ts +65 -65
  134. package/dist/utils/BrowserUtils.js +138 -138
  135. package/dist/utils/MathUtils.d.ts +11 -11
  136. package/dist/utils/MathUtils.js +21 -21
  137. package/lib/msal-browser.cjs.js +18383 -0
  138. package/lib/msal-browser.cjs.js.map +1 -0
  139. package/lib/msal-browser.js +18389 -0
  140. package/lib/msal-browser.js.map +1 -0
  141. package/lib/msal-browser.min.js +74 -0
  142. package/package.json +97 -95
@@ -1,4 +1,4 @@
1
- /*! @azure/msal-browser v3.0.0-alpha.1 2023-05-08 */
1
+ /*! @azure/msal-browser v3.0.0-alpha.2 2023-05-17 */
2
2
  'use strict';
3
3
  import { PerformanceEvents, Constants, AuthError, UrlString, ProtocolUtils, ThrottlingUtils } from '@azure/msal-common';
4
4
  import { StandardInteractionClient } from './StandardInteractionClient.js';
@@ -10,295 +10,295 @@ import { BrowserAuthError } from '../error/BrowserAuthError.js';
10
10
  import { NativeInteractionClient } from './NativeInteractionClient.js';
11
11
  import { NativeMessageHandler } from '../broker/nativeBroker/NativeMessageHandler.js';
12
12
 
13
- /*
14
- * Copyright (c) Microsoft Corporation. All rights reserved.
15
- * Licensed under the MIT License.
16
- */
17
- class RedirectClient extends StandardInteractionClient {
18
- constructor(config, storageImpl, browserCrypto, logger, eventHandler, navigationClient, performanceClient, nativeStorageImpl, nativeMessageHandler, correlationId) {
19
- super(config, storageImpl, browserCrypto, logger, eventHandler, navigationClient, performanceClient, nativeMessageHandler, correlationId);
20
- this.nativeStorage = nativeStorageImpl;
21
- }
22
- /**
23
- * Redirects the page to the /authorize endpoint of the IDP
24
- * @param request
25
- */
26
- async acquireToken(request) {
27
- this.performanceClient.setPreQueueTime(PerformanceEvents.StandardInteractionClientInitializeAuthorizationRequest, request.correlationId);
28
- const validRequest = await this.initializeAuthorizationRequest(request, InteractionType.Redirect);
29
- this.browserStorage.updateCacheEntries(validRequest.state, validRequest.nonce, validRequest.authority, validRequest.loginHint || Constants.EMPTY_STRING, validRequest.account || null);
30
- const serverTelemetryManager = this.initializeServerTelemetryManager(ApiId.acquireTokenRedirect);
31
- const handleBackButton = (event) => {
32
- // Clear temporary cache if the back button is clicked during the redirect flow.
33
- if (event.persisted) {
34
- this.logger.verbose("Page was restored from back/forward cache. Clearing temporary cache.");
35
- this.browserStorage.cleanRequestByState(validRequest.state);
36
- }
37
- };
38
- try {
39
- // Create auth code request and generate PKCE params
40
- this.performanceClient.setPreQueueTime(PerformanceEvents.StandardInteractionClientInitializeAuthorizationCodeRequest, request.correlationId);
41
- const authCodeRequest = await this.initializeAuthorizationCodeRequest(validRequest);
42
- // Initialize the client
43
- this.performanceClient.setPreQueueTime(PerformanceEvents.StandardInteractionClientCreateAuthCodeClient, request.correlationId);
44
- const authClient = await this.createAuthCodeClient(serverTelemetryManager, validRequest.authority, validRequest.azureCloudOptions);
45
- this.logger.verbose("Auth code client created");
46
- // Create redirect interaction handler.
47
- const interactionHandler = new RedirectHandler(authClient, this.browserStorage, authCodeRequest, this.logger, this.browserCrypto, this.performanceClient);
48
- // Create acquire token url.
49
- const navigateUrl = await authClient.getAuthCodeUrl({
50
- ...validRequest,
51
- nativeBroker: NativeMessageHandler.isNativeAvailable(this.config, this.logger, this.nativeMessageHandler, request.authenticationScheme),
52
- });
53
- const redirectStartPage = this.getRedirectStartPage(request.redirectStartPage);
54
- this.logger.verbosePii(`Redirect start page: ${redirectStartPage}`);
55
- // Clear temporary cache if the back button is clicked during the redirect flow.
56
- window.addEventListener("pageshow", handleBackButton);
57
- // Show the UI once the url has been created. Response will come back in the hash, which will be handled in the handleRedirectCallback function.
58
- return await interactionHandler.initiateAuthRequest(navigateUrl, {
59
- navigationClient: this.navigationClient,
60
- redirectTimeout: this.config.system.redirectNavigationTimeout,
61
- redirectStartPage: redirectStartPage,
62
- onRedirectNavigate: request.onRedirectNavigate,
63
- });
64
- }
65
- catch (e) {
66
- if (e instanceof AuthError) {
67
- e.setCorrelationId(this.correlationId);
68
- serverTelemetryManager.cacheFailedRequest(e);
69
- }
70
- window.removeEventListener("pageshow", handleBackButton);
71
- this.browserStorage.cleanRequestByState(validRequest.state);
72
- throw e;
73
- }
74
- }
75
- /**
76
- * Checks if navigateToLoginRequestUrl is set, and:
77
- * - if true, performs logic to cache and navigate
78
- * - if false, handles hash string and parses response
79
- * @param hash
80
- */
81
- async handleRedirectPromise(hash) {
82
- const serverTelemetryManager = this.initializeServerTelemetryManager(ApiId.handleRedirectPromise);
83
- try {
84
- if (!this.browserStorage.isInteractionInProgress(true)) {
85
- this.logger.info("handleRedirectPromise called but there is no interaction in progress, returning null.");
86
- return null;
87
- }
88
- const responseHash = this.getRedirectResponseHash(hash || window.location.hash);
89
- if (!responseHash) {
90
- // Not a recognized server response hash or hash not associated with a redirect request
91
- this.logger.info("handleRedirectPromise did not detect a response hash as a result of a redirect. Cleaning temporary cache.");
92
- this.browserStorage.cleanRequestByInteractionType(InteractionType.Redirect);
93
- return null;
94
- }
95
- let state;
96
- try {
97
- // Deserialize hash fragment response parameters.
98
- const serverParams = UrlString.getDeserializedHash(responseHash);
99
- state = this.validateAndExtractStateFromHash(serverParams, InteractionType.Redirect);
100
- this.logger.verbose("State extracted from hash");
101
- }
102
- catch (e) {
103
- this.logger.info(`handleRedirectPromise was unable to extract state due to: ${e}`);
104
- this.browserStorage.cleanRequestByInteractionType(InteractionType.Redirect);
105
- return null;
106
- }
107
- // If navigateToLoginRequestUrl is true, get the url where the redirect request was initiated
108
- const loginRequestUrl = this.browserStorage.getTemporaryCache(TemporaryCacheKeys.ORIGIN_URI, true) || Constants.EMPTY_STRING;
109
- const loginRequestUrlNormalized = UrlString.removeHashFromUrl(loginRequestUrl);
110
- const currentUrlNormalized = UrlString.removeHashFromUrl(window.location.href);
111
- if (loginRequestUrlNormalized === currentUrlNormalized &&
112
- this.config.auth.navigateToLoginRequestUrl) {
113
- // We are on the page we need to navigate to - handle hash
114
- this.logger.verbose("Current page is loginRequestUrl, handling hash");
115
- const handleHashResult = await this.handleHash(responseHash, state, serverTelemetryManager);
116
- if (loginRequestUrl.indexOf("#") > -1) {
117
- // Replace current hash with non-msal hash, if present
118
- BrowserUtils.replaceHash(loginRequestUrl);
119
- }
120
- return handleHashResult;
121
- }
122
- else if (!this.config.auth.navigateToLoginRequestUrl) {
123
- this.logger.verbose("NavigateToLoginRequestUrl set to false, handling hash");
124
- return this.handleHash(responseHash, state, serverTelemetryManager);
125
- }
126
- else if (!BrowserUtils.isInIframe() ||
127
- this.config.system.allowRedirectInIframe) {
128
- /*
129
- * Returned from authority using redirect - need to perform navigation before processing response
130
- * Cache the hash to be retrieved after the next redirect
131
- */
132
- this.browserStorage.setTemporaryCache(TemporaryCacheKeys.URL_HASH, responseHash, true);
133
- const navigationOptions = {
134
- apiId: ApiId.handleRedirectPromise,
135
- timeout: this.config.system.redirectNavigationTimeout,
136
- noHistory: true,
137
- };
138
- /**
139
- * Default behavior is to redirect to the start page and not process the hash now.
140
- * The start page is expected to also call handleRedirectPromise which will process the hash in one of the checks above.
141
- */
142
- let processHashOnRedirect = true;
143
- if (!loginRequestUrl || loginRequestUrl === "null") {
144
- // Redirect to home page if login request url is null (real null or the string null)
145
- const homepage = BrowserUtils.getHomepage();
146
- // Cache the homepage under ORIGIN_URI to ensure cached hash is processed on homepage
147
- this.browserStorage.setTemporaryCache(TemporaryCacheKeys.ORIGIN_URI, homepage, true);
148
- this.logger.warning("Unable to get valid login request url from cache, redirecting to home page");
149
- processHashOnRedirect =
150
- await this.navigationClient.navigateInternal(homepage, navigationOptions);
151
- }
152
- else {
153
- // Navigate to page that initiated the redirect request
154
- this.logger.verbose(`Navigating to loginRequestUrl: ${loginRequestUrl}`);
155
- processHashOnRedirect =
156
- await this.navigationClient.navigateInternal(loginRequestUrl, navigationOptions);
157
- }
158
- // If navigateInternal implementation returns false, handle the hash now
159
- if (!processHashOnRedirect) {
160
- return this.handleHash(responseHash, state, serverTelemetryManager);
161
- }
162
- }
163
- return null;
164
- }
165
- catch (e) {
166
- if (e instanceof AuthError) {
167
- e.setCorrelationId(this.correlationId);
168
- serverTelemetryManager.cacheFailedRequest(e);
169
- }
170
- this.browserStorage.cleanRequestByInteractionType(InteractionType.Redirect);
171
- throw e;
172
- }
173
- }
174
- /**
175
- * Gets the response hash for a redirect request
176
- * Returns null if interactionType in the state value is not "redirect" or the hash does not contain known properties
177
- * @param hash
178
- */
179
- getRedirectResponseHash(hash) {
180
- this.logger.verbose("getRedirectResponseHash called");
181
- // Get current location hash from window or cache.
182
- const isResponseHash = UrlString.hashContainsKnownProperties(hash);
183
- if (isResponseHash) {
184
- BrowserUtils.clearHash(window);
185
- this.logger.verbose("Hash contains known properties, returning response hash");
186
- return hash;
187
- }
188
- const cachedHash = this.browserStorage.getTemporaryCache(TemporaryCacheKeys.URL_HASH, true);
189
- this.browserStorage.removeItem(this.browserStorage.generateCacheKey(TemporaryCacheKeys.URL_HASH));
190
- this.logger.verbose("Hash does not contain known properties, returning cached hash");
191
- return cachedHash;
192
- }
193
- /**
194
- * Checks if hash exists and handles in window.
195
- * @param hash
196
- * @param state
197
- */
198
- async handleHash(hash, state, serverTelemetryManager) {
199
- const cachedRequest = this.browserStorage.getCachedRequest(state, this.browserCrypto);
200
- this.logger.verbose("handleHash called, retrieved cached request");
201
- const serverParams = UrlString.getDeserializedHash(hash);
202
- if (serverParams.accountId) {
203
- this.logger.verbose("Account id found in hash, calling WAM for token");
204
- if (!this.nativeMessageHandler) {
205
- throw BrowserAuthError.createNativeConnectionNotEstablishedError();
206
- }
207
- const nativeInteractionClient = new NativeInteractionClient(this.config, this.browserStorage, this.browserCrypto, this.logger, this.eventHandler, this.navigationClient, ApiId.acquireTokenPopup, this.performanceClient, this.nativeMessageHandler, serverParams.accountId, this.browserStorage, cachedRequest.correlationId);
208
- const { userRequestState } = ProtocolUtils.parseRequestState(this.browserCrypto, state);
209
- return nativeInteractionClient
210
- .acquireToken({
211
- ...cachedRequest,
212
- state: userRequestState,
213
- prompt: undefined, // Server should handle the prompt, ideally native broker can do this part silently
214
- })
215
- .finally(() => {
216
- this.browserStorage.cleanRequestByState(state);
217
- });
218
- }
219
- // Hash contains known properties - handle and return in callback
220
- const currentAuthority = this.browserStorage.getCachedAuthority(state);
221
- if (!currentAuthority) {
222
- throw BrowserAuthError.createNoCachedAuthorityError();
223
- }
224
- this.performanceClient.setPreQueueTime(PerformanceEvents.StandardInteractionClientCreateAuthCodeClient, cachedRequest.correlationId);
225
- const authClient = await this.createAuthCodeClient(serverTelemetryManager, currentAuthority);
226
- this.logger.verbose("Auth code client created");
227
- ThrottlingUtils.removeThrottle(this.browserStorage, this.config.auth.clientId, cachedRequest);
228
- const interactionHandler = new RedirectHandler(authClient, this.browserStorage, cachedRequest, this.logger, this.browserCrypto, this.performanceClient);
229
- return await interactionHandler.handleCodeResponseFromHash(hash, state, authClient.authority, this.networkClient);
230
- }
231
- /**
232
- * Use to log out the current user, and redirect the user to the postLogoutRedirectUri.
233
- * Default behaviour is to redirect the user to `window.location.href`.
234
- * @param logoutRequest
235
- */
236
- async logout(logoutRequest) {
237
- this.logger.verbose("logoutRedirect called");
238
- const validLogoutRequest = this.initializeLogoutRequest(logoutRequest);
239
- const serverTelemetryManager = this.initializeServerTelemetryManager(ApiId.logout);
240
- try {
241
- this.eventHandler.emitEvent(EventType.LOGOUT_START, InteractionType.Redirect, logoutRequest);
242
- // Clear cache on logout
243
- await this.clearCacheOnLogout(validLogoutRequest.account);
244
- const navigationOptions = {
245
- apiId: ApiId.logout,
246
- timeout: this.config.system.redirectNavigationTimeout,
247
- noHistory: false,
248
- };
249
- this.performanceClient.setPreQueueTime(PerformanceEvents.StandardInteractionClientCreateAuthCodeClient, validLogoutRequest.correlationId);
250
- const authClient = await this.createAuthCodeClient(serverTelemetryManager, logoutRequest && logoutRequest.authority);
251
- this.logger.verbose("Auth code client created");
252
- // Create logout string and navigate user window to logout.
253
- const logoutUri = authClient.getLogoutUri(validLogoutRequest);
254
- this.eventHandler.emitEvent(EventType.LOGOUT_SUCCESS, InteractionType.Redirect, validLogoutRequest);
255
- // Check if onRedirectNavigate is implemented, and invoke it if so
256
- if (logoutRequest &&
257
- typeof logoutRequest.onRedirectNavigate === "function") {
258
- const navigate = logoutRequest.onRedirectNavigate(logoutUri);
259
- if (navigate !== false) {
260
- this.logger.verbose("Logout onRedirectNavigate did not return false, navigating");
261
- // Ensure interaction is in progress
262
- if (!this.browserStorage.getInteractionInProgress()) {
263
- this.browserStorage.setInteractionInProgress(true);
264
- }
265
- await this.navigationClient.navigateExternal(logoutUri, navigationOptions);
266
- return;
267
- }
268
- else {
269
- // Ensure interaction is not in progress
270
- this.browserStorage.setInteractionInProgress(false);
271
- this.logger.verbose("Logout onRedirectNavigate returned false, stopping navigation");
272
- }
273
- }
274
- else {
275
- // Ensure interaction is in progress
276
- if (!this.browserStorage.getInteractionInProgress()) {
277
- this.browserStorage.setInteractionInProgress(true);
278
- }
279
- await this.navigationClient.navigateExternal(logoutUri, navigationOptions);
280
- return;
281
- }
282
- }
283
- catch (e) {
284
- if (e instanceof AuthError) {
285
- e.setCorrelationId(this.correlationId);
286
- serverTelemetryManager.cacheFailedRequest(e);
287
- }
288
- this.eventHandler.emitEvent(EventType.LOGOUT_FAILURE, InteractionType.Redirect, null, e);
289
- this.eventHandler.emitEvent(EventType.LOGOUT_END, InteractionType.Redirect);
290
- throw e;
291
- }
292
- this.eventHandler.emitEvent(EventType.LOGOUT_END, InteractionType.Redirect);
293
- }
294
- /**
295
- * Use to get the redirectStartPage either from request or use current window
296
- * @param requestStartPage
297
- */
298
- getRedirectStartPage(requestStartPage) {
299
- const redirectStartPage = requestStartPage || window.location.href;
300
- return UrlString.getAbsoluteUrl(redirectStartPage, BrowserUtils.getCurrentUri());
301
- }
13
+ /*
14
+ * Copyright (c) Microsoft Corporation. All rights reserved.
15
+ * Licensed under the MIT License.
16
+ */
17
+ class RedirectClient extends StandardInteractionClient {
18
+ constructor(config, storageImpl, browserCrypto, logger, eventHandler, navigationClient, performanceClient, nativeStorageImpl, nativeMessageHandler, correlationId) {
19
+ super(config, storageImpl, browserCrypto, logger, eventHandler, navigationClient, performanceClient, nativeMessageHandler, correlationId);
20
+ this.nativeStorage = nativeStorageImpl;
21
+ }
22
+ /**
23
+ * Redirects the page to the /authorize endpoint of the IDP
24
+ * @param request
25
+ */
26
+ async acquireToken(request) {
27
+ this.performanceClient.setPreQueueTime(PerformanceEvents.StandardInteractionClientInitializeAuthorizationRequest, request.correlationId);
28
+ const validRequest = await this.initializeAuthorizationRequest(request, InteractionType.Redirect);
29
+ this.browserStorage.updateCacheEntries(validRequest.state, validRequest.nonce, validRequest.authority, validRequest.loginHint || Constants.EMPTY_STRING, validRequest.account || null);
30
+ const serverTelemetryManager = this.initializeServerTelemetryManager(ApiId.acquireTokenRedirect);
31
+ const handleBackButton = (event) => {
32
+ // Clear temporary cache if the back button is clicked during the redirect flow.
33
+ if (event.persisted) {
34
+ this.logger.verbose("Page was restored from back/forward cache. Clearing temporary cache.");
35
+ this.browserStorage.cleanRequestByState(validRequest.state);
36
+ }
37
+ };
38
+ try {
39
+ // Create auth code request and generate PKCE params
40
+ this.performanceClient.setPreQueueTime(PerformanceEvents.StandardInteractionClientInitializeAuthorizationCodeRequest, request.correlationId);
41
+ const authCodeRequest = await this.initializeAuthorizationCodeRequest(validRequest);
42
+ // Initialize the client
43
+ this.performanceClient.setPreQueueTime(PerformanceEvents.StandardInteractionClientCreateAuthCodeClient, request.correlationId);
44
+ const authClient = await this.createAuthCodeClient(serverTelemetryManager, validRequest.authority, validRequest.azureCloudOptions);
45
+ this.logger.verbose("Auth code client created");
46
+ // Create redirect interaction handler.
47
+ const interactionHandler = new RedirectHandler(authClient, this.browserStorage, authCodeRequest, this.logger, this.browserCrypto, this.performanceClient);
48
+ // Create acquire token url.
49
+ const navigateUrl = await authClient.getAuthCodeUrl({
50
+ ...validRequest,
51
+ nativeBroker: NativeMessageHandler.isNativeAvailable(this.config, this.logger, this.nativeMessageHandler, request.authenticationScheme),
52
+ });
53
+ const redirectStartPage = this.getRedirectStartPage(request.redirectStartPage);
54
+ this.logger.verbosePii(`Redirect start page: ${redirectStartPage}`);
55
+ // Clear temporary cache if the back button is clicked during the redirect flow.
56
+ window.addEventListener("pageshow", handleBackButton);
57
+ // Show the UI once the url has been created. Response will come back in the hash, which will be handled in the handleRedirectCallback function.
58
+ return await interactionHandler.initiateAuthRequest(navigateUrl, {
59
+ navigationClient: this.navigationClient,
60
+ redirectTimeout: this.config.system.redirectNavigationTimeout,
61
+ redirectStartPage: redirectStartPage,
62
+ onRedirectNavigate: request.onRedirectNavigate,
63
+ });
64
+ }
65
+ catch (e) {
66
+ if (e instanceof AuthError) {
67
+ e.setCorrelationId(this.correlationId);
68
+ serverTelemetryManager.cacheFailedRequest(e);
69
+ }
70
+ window.removeEventListener("pageshow", handleBackButton);
71
+ this.browserStorage.cleanRequestByState(validRequest.state);
72
+ throw e;
73
+ }
74
+ }
75
+ /**
76
+ * Checks if navigateToLoginRequestUrl is set, and:
77
+ * - if true, performs logic to cache and navigate
78
+ * - if false, handles hash string and parses response
79
+ * @param hash
80
+ */
81
+ async handleRedirectPromise(hash) {
82
+ const serverTelemetryManager = this.initializeServerTelemetryManager(ApiId.handleRedirectPromise);
83
+ try {
84
+ if (!this.browserStorage.isInteractionInProgress(true)) {
85
+ this.logger.info("handleRedirectPromise called but there is no interaction in progress, returning null.");
86
+ return null;
87
+ }
88
+ const responseHash = this.getRedirectResponseHash(hash || window.location.hash);
89
+ if (!responseHash) {
90
+ // Not a recognized server response hash or hash not associated with a redirect request
91
+ this.logger.info("handleRedirectPromise did not detect a response hash as a result of a redirect. Cleaning temporary cache.");
92
+ this.browserStorage.cleanRequestByInteractionType(InteractionType.Redirect);
93
+ return null;
94
+ }
95
+ let state;
96
+ try {
97
+ // Deserialize hash fragment response parameters.
98
+ const serverParams = UrlString.getDeserializedHash(responseHash);
99
+ state = this.validateAndExtractStateFromHash(serverParams, InteractionType.Redirect);
100
+ this.logger.verbose("State extracted from hash");
101
+ }
102
+ catch (e) {
103
+ this.logger.info(`handleRedirectPromise was unable to extract state due to: ${e}`);
104
+ this.browserStorage.cleanRequestByInteractionType(InteractionType.Redirect);
105
+ return null;
106
+ }
107
+ // If navigateToLoginRequestUrl is true, get the url where the redirect request was initiated
108
+ const loginRequestUrl = this.browserStorage.getTemporaryCache(TemporaryCacheKeys.ORIGIN_URI, true) || Constants.EMPTY_STRING;
109
+ const loginRequestUrlNormalized = UrlString.removeHashFromUrl(loginRequestUrl);
110
+ const currentUrlNormalized = UrlString.removeHashFromUrl(window.location.href);
111
+ if (loginRequestUrlNormalized === currentUrlNormalized &&
112
+ this.config.auth.navigateToLoginRequestUrl) {
113
+ // We are on the page we need to navigate to - handle hash
114
+ this.logger.verbose("Current page is loginRequestUrl, handling hash");
115
+ const handleHashResult = await this.handleHash(responseHash, state, serverTelemetryManager);
116
+ if (loginRequestUrl.indexOf("#") > -1) {
117
+ // Replace current hash with non-msal hash, if present
118
+ BrowserUtils.replaceHash(loginRequestUrl);
119
+ }
120
+ return handleHashResult;
121
+ }
122
+ else if (!this.config.auth.navigateToLoginRequestUrl) {
123
+ this.logger.verbose("NavigateToLoginRequestUrl set to false, handling hash");
124
+ return this.handleHash(responseHash, state, serverTelemetryManager);
125
+ }
126
+ else if (!BrowserUtils.isInIframe() ||
127
+ this.config.system.allowRedirectInIframe) {
128
+ /*
129
+ * Returned from authority using redirect - need to perform navigation before processing response
130
+ * Cache the hash to be retrieved after the next redirect
131
+ */
132
+ this.browserStorage.setTemporaryCache(TemporaryCacheKeys.URL_HASH, responseHash, true);
133
+ const navigationOptions = {
134
+ apiId: ApiId.handleRedirectPromise,
135
+ timeout: this.config.system.redirectNavigationTimeout,
136
+ noHistory: true,
137
+ };
138
+ /**
139
+ * Default behavior is to redirect to the start page and not process the hash now.
140
+ * The start page is expected to also call handleRedirectPromise which will process the hash in one of the checks above.
141
+ */
142
+ let processHashOnRedirect = true;
143
+ if (!loginRequestUrl || loginRequestUrl === "null") {
144
+ // Redirect to home page if login request url is null (real null or the string null)
145
+ const homepage = BrowserUtils.getHomepage();
146
+ // Cache the homepage under ORIGIN_URI to ensure cached hash is processed on homepage
147
+ this.browserStorage.setTemporaryCache(TemporaryCacheKeys.ORIGIN_URI, homepage, true);
148
+ this.logger.warning("Unable to get valid login request url from cache, redirecting to home page");
149
+ processHashOnRedirect =
150
+ await this.navigationClient.navigateInternal(homepage, navigationOptions);
151
+ }
152
+ else {
153
+ // Navigate to page that initiated the redirect request
154
+ this.logger.verbose(`Navigating to loginRequestUrl: ${loginRequestUrl}`);
155
+ processHashOnRedirect =
156
+ await this.navigationClient.navigateInternal(loginRequestUrl, navigationOptions);
157
+ }
158
+ // If navigateInternal implementation returns false, handle the hash now
159
+ if (!processHashOnRedirect) {
160
+ return this.handleHash(responseHash, state, serverTelemetryManager);
161
+ }
162
+ }
163
+ return null;
164
+ }
165
+ catch (e) {
166
+ if (e instanceof AuthError) {
167
+ e.setCorrelationId(this.correlationId);
168
+ serverTelemetryManager.cacheFailedRequest(e);
169
+ }
170
+ this.browserStorage.cleanRequestByInteractionType(InteractionType.Redirect);
171
+ throw e;
172
+ }
173
+ }
174
+ /**
175
+ * Gets the response hash for a redirect request
176
+ * Returns null if interactionType in the state value is not "redirect" or the hash does not contain known properties
177
+ * @param hash
178
+ */
179
+ getRedirectResponseHash(hash) {
180
+ this.logger.verbose("getRedirectResponseHash called");
181
+ // Get current location hash from window or cache.
182
+ const isResponseHash = UrlString.hashContainsKnownProperties(hash);
183
+ if (isResponseHash) {
184
+ BrowserUtils.clearHash(window);
185
+ this.logger.verbose("Hash contains known properties, returning response hash");
186
+ return hash;
187
+ }
188
+ const cachedHash = this.browserStorage.getTemporaryCache(TemporaryCacheKeys.URL_HASH, true);
189
+ this.browserStorage.removeItem(this.browserStorage.generateCacheKey(TemporaryCacheKeys.URL_HASH));
190
+ this.logger.verbose("Hash does not contain known properties, returning cached hash");
191
+ return cachedHash;
192
+ }
193
+ /**
194
+ * Checks if hash exists and handles in window.
195
+ * @param hash
196
+ * @param state
197
+ */
198
+ async handleHash(hash, state, serverTelemetryManager) {
199
+ const cachedRequest = this.browserStorage.getCachedRequest(state, this.browserCrypto);
200
+ this.logger.verbose("handleHash called, retrieved cached request");
201
+ const serverParams = UrlString.getDeserializedHash(hash);
202
+ if (serverParams.accountId) {
203
+ this.logger.verbose("Account id found in hash, calling WAM for token");
204
+ if (!this.nativeMessageHandler) {
205
+ throw BrowserAuthError.createNativeConnectionNotEstablishedError();
206
+ }
207
+ const nativeInteractionClient = new NativeInteractionClient(this.config, this.browserStorage, this.browserCrypto, this.logger, this.eventHandler, this.navigationClient, ApiId.acquireTokenPopup, this.performanceClient, this.nativeMessageHandler, serverParams.accountId, this.browserStorage, cachedRequest.correlationId);
208
+ const { userRequestState } = ProtocolUtils.parseRequestState(this.browserCrypto, state);
209
+ return nativeInteractionClient
210
+ .acquireToken({
211
+ ...cachedRequest,
212
+ state: userRequestState,
213
+ prompt: undefined, // Server should handle the prompt, ideally native broker can do this part silently
214
+ })
215
+ .finally(() => {
216
+ this.browserStorage.cleanRequestByState(state);
217
+ });
218
+ }
219
+ // Hash contains known properties - handle and return in callback
220
+ const currentAuthority = this.browserStorage.getCachedAuthority(state);
221
+ if (!currentAuthority) {
222
+ throw BrowserAuthError.createNoCachedAuthorityError();
223
+ }
224
+ this.performanceClient.setPreQueueTime(PerformanceEvents.StandardInteractionClientCreateAuthCodeClient, cachedRequest.correlationId);
225
+ const authClient = await this.createAuthCodeClient(serverTelemetryManager, currentAuthority);
226
+ this.logger.verbose("Auth code client created");
227
+ ThrottlingUtils.removeThrottle(this.browserStorage, this.config.auth.clientId, cachedRequest);
228
+ const interactionHandler = new RedirectHandler(authClient, this.browserStorage, cachedRequest, this.logger, this.browserCrypto, this.performanceClient);
229
+ return await interactionHandler.handleCodeResponseFromHash(hash, state, authClient.authority, this.networkClient);
230
+ }
231
+ /**
232
+ * Use to log out the current user, and redirect the user to the postLogoutRedirectUri.
233
+ * Default behaviour is to redirect the user to `window.location.href`.
234
+ * @param logoutRequest
235
+ */
236
+ async logout(logoutRequest) {
237
+ this.logger.verbose("logoutRedirect called");
238
+ const validLogoutRequest = this.initializeLogoutRequest(logoutRequest);
239
+ const serverTelemetryManager = this.initializeServerTelemetryManager(ApiId.logout);
240
+ try {
241
+ this.eventHandler.emitEvent(EventType.LOGOUT_START, InteractionType.Redirect, logoutRequest);
242
+ // Clear cache on logout
243
+ await this.clearCacheOnLogout(validLogoutRequest.account);
244
+ const navigationOptions = {
245
+ apiId: ApiId.logout,
246
+ timeout: this.config.system.redirectNavigationTimeout,
247
+ noHistory: false,
248
+ };
249
+ this.performanceClient.setPreQueueTime(PerformanceEvents.StandardInteractionClientCreateAuthCodeClient, validLogoutRequest.correlationId);
250
+ const authClient = await this.createAuthCodeClient(serverTelemetryManager, logoutRequest && logoutRequest.authority);
251
+ this.logger.verbose("Auth code client created");
252
+ // Create logout string and navigate user window to logout.
253
+ const logoutUri = authClient.getLogoutUri(validLogoutRequest);
254
+ this.eventHandler.emitEvent(EventType.LOGOUT_SUCCESS, InteractionType.Redirect, validLogoutRequest);
255
+ // Check if onRedirectNavigate is implemented, and invoke it if so
256
+ if (logoutRequest &&
257
+ typeof logoutRequest.onRedirectNavigate === "function") {
258
+ const navigate = logoutRequest.onRedirectNavigate(logoutUri);
259
+ if (navigate !== false) {
260
+ this.logger.verbose("Logout onRedirectNavigate did not return false, navigating");
261
+ // Ensure interaction is in progress
262
+ if (!this.browserStorage.getInteractionInProgress()) {
263
+ this.browserStorage.setInteractionInProgress(true);
264
+ }
265
+ await this.navigationClient.navigateExternal(logoutUri, navigationOptions);
266
+ return;
267
+ }
268
+ else {
269
+ // Ensure interaction is not in progress
270
+ this.browserStorage.setInteractionInProgress(false);
271
+ this.logger.verbose("Logout onRedirectNavigate returned false, stopping navigation");
272
+ }
273
+ }
274
+ else {
275
+ // Ensure interaction is in progress
276
+ if (!this.browserStorage.getInteractionInProgress()) {
277
+ this.browserStorage.setInteractionInProgress(true);
278
+ }
279
+ await this.navigationClient.navigateExternal(logoutUri, navigationOptions);
280
+ return;
281
+ }
282
+ }
283
+ catch (e) {
284
+ if (e instanceof AuthError) {
285
+ e.setCorrelationId(this.correlationId);
286
+ serverTelemetryManager.cacheFailedRequest(e);
287
+ }
288
+ this.eventHandler.emitEvent(EventType.LOGOUT_FAILURE, InteractionType.Redirect, null, e);
289
+ this.eventHandler.emitEvent(EventType.LOGOUT_END, InteractionType.Redirect);
290
+ throw e;
291
+ }
292
+ this.eventHandler.emitEvent(EventType.LOGOUT_END, InteractionType.Redirect);
293
+ }
294
+ /**
295
+ * Use to get the redirectStartPage either from request or use current window
296
+ * @param requestStartPage
297
+ */
298
+ getRedirectStartPage(requestStartPage) {
299
+ const redirectStartPage = requestStartPage || window.location.href;
300
+ return UrlString.getAbsoluteUrl(redirectStartPage, BrowserUtils.getCurrentUri());
301
+ }
302
302
  }
303
303
 
304
304
  export { RedirectClient };