@azure/msal-browser 3.0.2 → 3.1.0

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 (256) hide show
  1. package/dist/app/IPublicClientApplication.d.ts +2 -0
  2. package/dist/app/IPublicClientApplication.d.ts.map +1 -1
  3. package/dist/app/IPublicClientApplication.mjs +4 -1
  4. package/dist/app/IPublicClientApplication.mjs.map +1 -1
  5. package/dist/app/PublicClientApplication.d.ts +6 -0
  6. package/dist/app/PublicClientApplication.d.ts.map +1 -1
  7. package/dist/app/PublicClientApplication.mjs +8 -1
  8. package/dist/app/PublicClientApplication.mjs.map +1 -1
  9. package/dist/broker/nativeBroker/NativeMessageHandler.d.ts.map +1 -1
  10. package/dist/broker/nativeBroker/NativeMessageHandler.mjs +5 -4
  11. package/dist/broker/nativeBroker/NativeMessageHandler.mjs.map +1 -1
  12. package/dist/cache/AsyncMemoryStorage.mjs +4 -3
  13. package/dist/cache/AsyncMemoryStorage.mjs.map +1 -1
  14. package/dist/cache/BrowserCacheManager.d.ts +9 -2
  15. package/dist/cache/BrowserCacheManager.d.ts.map +1 -1
  16. package/dist/cache/BrowserCacheManager.mjs +54 -26
  17. package/dist/cache/BrowserCacheManager.mjs.map +1 -1
  18. package/dist/cache/BrowserStorage.mjs +1 -1
  19. package/dist/cache/BrowserStorage.mjs.map +1 -1
  20. package/dist/cache/CryptoKeyStore.mjs +1 -1
  21. package/dist/cache/CryptoKeyStore.mjs.map +1 -1
  22. package/dist/cache/DatabaseStorage.d.ts.map +1 -1
  23. package/dist/cache/DatabaseStorage.mjs +9 -8
  24. package/dist/cache/DatabaseStorage.mjs.map +1 -1
  25. package/dist/cache/MemoryStorage.mjs +1 -1
  26. package/dist/cache/MemoryStorage.mjs.map +1 -1
  27. package/dist/cache/TokenCache.d.ts.map +1 -1
  28. package/dist/cache/TokenCache.mjs +30 -30
  29. package/dist/cache/TokenCache.mjs.map +1 -1
  30. package/dist/config/Configuration.d.ts +1 -0
  31. package/dist/config/Configuration.d.ts.map +1 -1
  32. package/dist/config/Configuration.mjs +7 -11
  33. package/dist/config/Configuration.mjs.map +1 -1
  34. package/dist/controllers/ControllerFactory.mjs +1 -1
  35. package/dist/controllers/ControllerFactory.mjs.map +1 -1
  36. package/dist/controllers/IController.d.ts +3 -27
  37. package/dist/controllers/IController.d.ts.map +1 -1
  38. package/dist/controllers/StandardController.d.ts +6 -25
  39. package/dist/controllers/StandardController.d.ts.map +1 -1
  40. package/dist/controllers/StandardController.mjs +28 -52
  41. package/dist/controllers/StandardController.mjs.map +1 -1
  42. package/dist/crypto/BrowserCrypto.d.ts.map +1 -1
  43. package/dist/crypto/BrowserCrypto.mjs +4 -3
  44. package/dist/crypto/BrowserCrypto.mjs.map +1 -1
  45. package/dist/crypto/CryptoOps.d.ts +0 -3
  46. package/dist/crypto/CryptoOps.d.ts.map +1 -1
  47. package/dist/crypto/CryptoOps.mjs +14 -17
  48. package/dist/crypto/CryptoOps.mjs.map +1 -1
  49. package/dist/crypto/ModernBrowserCrypto.mjs +1 -1
  50. package/dist/crypto/ModernBrowserCrypto.mjs.map +1 -1
  51. package/dist/crypto/PkceGenerator.d.ts +0 -1
  52. package/dist/crypto/PkceGenerator.d.ts.map +1 -1
  53. package/dist/crypto/PkceGenerator.mjs +8 -8
  54. package/dist/crypto/PkceGenerator.mjs.map +1 -1
  55. package/dist/crypto/SignedHttpRequest.mjs +1 -1
  56. package/dist/crypto/SignedHttpRequest.mjs.map +1 -1
  57. package/dist/encode/Base64Decode.d.ts +6 -19
  58. package/dist/encode/Base64Decode.d.ts.map +1 -1
  59. package/dist/encode/Base64Decode.mjs +17 -70
  60. package/dist/encode/Base64Decode.mjs.map +1 -1
  61. package/dist/encode/Base64Encode.d.ts +15 -27
  62. package/dist/encode/Base64Encode.d.ts.map +1 -1
  63. package/dist/encode/Base64Encode.mjs +36 -75
  64. package/dist/encode/Base64Encode.mjs.map +1 -1
  65. package/dist/error/BrowserAuthError.d.ts +54 -195
  66. package/dist/error/BrowserAuthError.d.ts.map +1 -1
  67. package/dist/error/BrowserAuthError.mjs +150 -381
  68. package/dist/error/BrowserAuthError.mjs.map +1 -1
  69. package/dist/error/BrowserAuthErrorCodes.d.ts +45 -0
  70. package/dist/error/BrowserAuthErrorCodes.d.ts.map +1 -0
  71. package/dist/error/BrowserAuthErrorCodes.mjs +53 -0
  72. package/dist/error/BrowserAuthErrorCodes.mjs.map +1 -0
  73. package/dist/error/BrowserConfigurationAuthError.mjs +1 -1
  74. package/dist/error/BrowserConfigurationAuthError.mjs.map +1 -1
  75. package/dist/error/NativeAuthError.d.ts.map +1 -1
  76. package/dist/error/NativeAuthError.mjs +5 -4
  77. package/dist/error/NativeAuthError.mjs.map +1 -1
  78. package/dist/event/EventHandler.mjs +1 -1
  79. package/dist/event/EventHandler.mjs.map +1 -1
  80. package/dist/event/EventMessage.mjs +1 -1
  81. package/dist/event/EventMessage.mjs.map +1 -1
  82. package/dist/event/EventType.mjs +1 -1
  83. package/dist/event/EventType.mjs.map +1 -1
  84. package/dist/index.d.ts +5 -5
  85. package/dist/index.d.ts.map +1 -1
  86. package/dist/index.mjs +5 -3
  87. package/dist/index.mjs.map +1 -1
  88. package/dist/interaction_client/BaseInteractionClient.d.ts +2 -1
  89. package/dist/interaction_client/BaseInteractionClient.d.ts.map +1 -1
  90. package/dist/interaction_client/BaseInteractionClient.mjs +1 -1
  91. package/dist/interaction_client/BaseInteractionClient.mjs.map +1 -1
  92. package/dist/interaction_client/HybridSpaAuthorizationCodeClient.mjs +1 -1
  93. package/dist/interaction_client/HybridSpaAuthorizationCodeClient.mjs.map +1 -1
  94. package/dist/interaction_client/NativeInteractionClient.d.ts +4 -10
  95. package/dist/interaction_client/NativeInteractionClient.d.ts.map +1 -1
  96. package/dist/interaction_client/NativeInteractionClient.mjs +22 -30
  97. package/dist/interaction_client/NativeInteractionClient.mjs.map +1 -1
  98. package/dist/interaction_client/PopupClient.d.ts.map +1 -1
  99. package/dist/interaction_client/PopupClient.mjs +16 -15
  100. package/dist/interaction_client/PopupClient.mjs.map +1 -1
  101. package/dist/interaction_client/RedirectClient.d.ts.map +1 -1
  102. package/dist/interaction_client/RedirectClient.mjs +7 -6
  103. package/dist/interaction_client/RedirectClient.mjs.map +1 -1
  104. package/dist/interaction_client/SilentAuthCodeClient.d.ts.map +1 -1
  105. package/dist/interaction_client/SilentAuthCodeClient.mjs +5 -4
  106. package/dist/interaction_client/SilentAuthCodeClient.mjs.map +1 -1
  107. package/dist/interaction_client/SilentCacheClient.d.ts +4 -2
  108. package/dist/interaction_client/SilentCacheClient.d.ts.map +1 -1
  109. package/dist/interaction_client/SilentCacheClient.mjs +10 -8
  110. package/dist/interaction_client/SilentCacheClient.mjs.map +1 -1
  111. package/dist/interaction_client/SilentIframeClient.d.ts.map +1 -1
  112. package/dist/interaction_client/SilentIframeClient.mjs +20 -47
  113. package/dist/interaction_client/SilentIframeClient.mjs.map +1 -1
  114. package/dist/interaction_client/SilentRefreshClient.d.ts.map +1 -1
  115. package/dist/interaction_client/SilentRefreshClient.mjs +4 -3
  116. package/dist/interaction_client/SilentRefreshClient.mjs.map +1 -1
  117. package/dist/interaction_client/StandardInteractionClient.d.ts.map +1 -1
  118. package/dist/interaction_client/StandardInteractionClient.mjs +8 -7
  119. package/dist/interaction_client/StandardInteractionClient.mjs.map +1 -1
  120. package/dist/interaction_handler/InteractionHandler.d.ts.map +1 -1
  121. package/dist/interaction_handler/InteractionHandler.mjs +9 -9
  122. package/dist/interaction_handler/InteractionHandler.mjs.map +1 -1
  123. package/dist/interaction_handler/RedirectHandler.d.ts.map +1 -1
  124. package/dist/interaction_handler/RedirectHandler.mjs +11 -10
  125. package/dist/interaction_handler/RedirectHandler.mjs.map +1 -1
  126. package/dist/interaction_handler/SilentHandler.d.ts.map +1 -1
  127. package/dist/interaction_handler/SilentHandler.mjs +10 -9
  128. package/dist/interaction_handler/SilentHandler.mjs.map +1 -1
  129. package/dist/navigation/NavigationClient.mjs +1 -1
  130. package/dist/navigation/NavigationClient.mjs.map +1 -1
  131. package/dist/network/FetchClient.d.ts.map +1 -1
  132. package/dist/network/FetchClient.mjs +9 -8
  133. package/dist/network/FetchClient.mjs.map +1 -1
  134. package/dist/operatingcontext/BaseOperatingContext.mjs +1 -1
  135. package/dist/operatingcontext/BaseOperatingContext.mjs.map +1 -1
  136. package/dist/operatingcontext/StandardOperatingContext.mjs +1 -1
  137. package/dist/operatingcontext/StandardOperatingContext.mjs.map +1 -1
  138. package/dist/operatingcontext/TeamsAppOperatingContext.mjs +1 -1
  139. package/dist/operatingcontext/TeamsAppOperatingContext.mjs.map +1 -1
  140. package/dist/packageMetadata.d.ts +1 -1
  141. package/dist/packageMetadata.mjs +2 -2
  142. package/dist/packageMetadata.mjs.map +1 -1
  143. package/dist/request/ClearCacheRequest.d.ts +11 -0
  144. package/dist/request/ClearCacheRequest.d.ts.map +1 -0
  145. package/dist/telemetry/BrowserPerformanceClient.d.ts +2 -4
  146. package/dist/telemetry/BrowserPerformanceClient.d.ts.map +1 -1
  147. package/dist/telemetry/BrowserPerformanceClient.mjs +2 -6
  148. package/dist/telemetry/BrowserPerformanceClient.mjs.map +1 -1
  149. package/dist/telemetry/BrowserPerformanceMeasurement.mjs +1 -1
  150. package/dist/telemetry/BrowserPerformanceMeasurement.mjs.map +1 -1
  151. package/dist/utils/BrowserConstants.mjs +1 -1
  152. package/dist/utils/BrowserConstants.mjs.map +1 -1
  153. package/dist/utils/BrowserProtocolUtils.d.ts.map +1 -1
  154. package/dist/utils/BrowserProtocolUtils.mjs +3 -3
  155. package/dist/utils/BrowserProtocolUtils.mjs.map +1 -1
  156. package/dist/utils/BrowserStringUtils.mjs +1 -1
  157. package/dist/utils/BrowserStringUtils.mjs.map +1 -1
  158. package/dist/utils/BrowserUtils.d.ts +0 -9
  159. package/dist/utils/BrowserUtils.d.ts.map +1 -1
  160. package/dist/utils/BrowserUtils.mjs +8 -33
  161. package/dist/utils/BrowserUtils.mjs.map +1 -1
  162. package/lib/msal-browser.cjs +6579 -7102
  163. package/lib/msal-browser.cjs.map +1 -1
  164. package/lib/msal-browser.js +6579 -7102
  165. package/lib/msal-browser.js.map +1 -1
  166. package/lib/msal-browser.min.js +65 -62
  167. package/package.json +7 -5
  168. package/src/app/IPublicClientApplication.ts +199 -0
  169. package/src/app/PublicClientApplication.ts +405 -0
  170. package/src/broker/nativeBroker/NativeMessageHandler.ts +419 -0
  171. package/src/broker/nativeBroker/NativeRequest.ts +52 -0
  172. package/src/broker/nativeBroker/NativeResponse.ts +56 -0
  173. package/src/cache/AsyncMemoryStorage.ts +158 -0
  174. package/src/cache/BrowserCacheManager.ts +1994 -0
  175. package/src/cache/BrowserStorage.ts +54 -0
  176. package/src/cache/CryptoKeyStore.ts +63 -0
  177. package/src/cache/DatabaseStorage.ts +287 -0
  178. package/src/cache/IAsyncMemoryStorage.ts +36 -0
  179. package/src/cache/ITokenCache.ts +21 -0
  180. package/src/cache/IWindowStorage.ts +36 -0
  181. package/src/cache/MemoryStorage.ts +42 -0
  182. package/src/cache/TokenCache.ts +484 -0
  183. package/src/config/Configuration.ts +375 -0
  184. package/src/controllers/ControllerFactory.ts +55 -0
  185. package/src/controllers/IController.ts +131 -0
  186. package/src/controllers/StandardController.ts +2107 -0
  187. package/src/crypto/BrowserCrypto.ts +135 -0
  188. package/src/crypto/CryptoOps.ts +247 -0
  189. package/src/crypto/ISubtleCrypto.ts +30 -0
  190. package/src/crypto/ModernBrowserCrypto.ts +69 -0
  191. package/src/crypto/PkceGenerator.ts +76 -0
  192. package/src/crypto/SignedHttpRequest.ts +76 -0
  193. package/src/encode/Base64Decode.ts +26 -0
  194. package/src/encode/Base64Encode.ts +52 -0
  195. package/src/error/BrowserAuthError.ts +340 -0
  196. package/src/error/BrowserAuthErrorCodes.ts +56 -0
  197. package/src/error/BrowserConfigurationAuthError.ts +133 -0
  198. package/src/error/NativeAuthError.ts +141 -0
  199. package/src/event/EventHandler.ts +176 -0
  200. package/src/event/EventMessage.ts +125 -0
  201. package/src/event/EventType.ts +33 -0
  202. package/src/index.ts +152 -0
  203. package/src/interaction_client/BaseInteractionClient.ts +301 -0
  204. package/src/interaction_client/HybridSpaAuthorizationCodeClient.ts +16 -0
  205. package/src/interaction_client/NativeInteractionClient.ts +907 -0
  206. package/src/interaction_client/PopupClient.ts +926 -0
  207. package/src/interaction_client/RedirectClient.ts +616 -0
  208. package/src/interaction_client/SilentAuthCodeClient.ts +163 -0
  209. package/src/interaction_client/SilentCacheClient.ts +133 -0
  210. package/src/interaction_client/SilentIframeClient.ts +301 -0
  211. package/src/interaction_client/SilentRefreshClient.ts +120 -0
  212. package/src/interaction_client/StandardInteractionClient.ts +481 -0
  213. package/src/interaction_handler/InteractionHandler.ts +246 -0
  214. package/src/interaction_handler/RedirectHandler.ts +225 -0
  215. package/src/interaction_handler/SilentHandler.ts +266 -0
  216. package/src/navigation/INavigationClient.ts +23 -0
  217. package/src/navigation/NavigationClient.ts +55 -0
  218. package/src/navigation/NavigationOptions.ts +18 -0
  219. package/src/network/FetchClient.ts +130 -0
  220. package/src/operatingcontext/BaseOperatingContext.ts +82 -0
  221. package/src/operatingcontext/StandardOperatingContext.ts +49 -0
  222. package/src/operatingcontext/TeamsAppOperatingContext.ts +48 -0
  223. package/src/packageMetadata.ts +3 -0
  224. package/src/request/AuthorizationCodeRequest.ts +19 -0
  225. package/src/request/AuthorizationUrlRequest.ts +17 -0
  226. package/src/request/ClearCacheRequest.ts +16 -0
  227. package/src/request/EndSessionPopupRequest.ts +26 -0
  228. package/src/request/EndSessionRequest.ts +23 -0
  229. package/src/request/PopupRequest.ts +51 -0
  230. package/src/request/PopupWindowAttributes.ts +22 -0
  231. package/src/request/RedirectRequest.ts +51 -0
  232. package/src/request/SilentRequest.ts +49 -0
  233. package/src/request/SsoSilentRequest.ts +44 -0
  234. package/src/response/AuthenticationResult.ts +13 -0
  235. package/src/telemetry/BrowserPerformanceClient.ts +226 -0
  236. package/src/telemetry/BrowserPerformanceMeasurement.ts +144 -0
  237. package/src/utils/BrowserConstants.ts +241 -0
  238. package/src/utils/BrowserProtocolUtils.ts +57 -0
  239. package/src/utils/BrowserStringUtils.ts +143 -0
  240. package/src/utils/BrowserUtils.ts +156 -0
  241. package/dist/crypto/GuidGenerator.d.ts +0 -13
  242. package/dist/crypto/GuidGenerator.d.ts.map +0 -1
  243. package/dist/crypto/GuidGenerator.mjs +0 -104
  244. package/dist/crypto/GuidGenerator.mjs.map +0 -1
  245. package/dist/internals.d.ts +0 -23
  246. package/dist/internals.d.ts.map +0 -1
  247. package/dist/internals.mjs +0 -28
  248. package/dist/internals.mjs.map +0 -1
  249. package/dist/network/XhrClient.d.ts +0 -41
  250. package/dist/network/XhrClient.d.ts.map +0 -1
  251. package/dist/network/XhrClient.mjs +0 -125
  252. package/dist/network/XhrClient.mjs.map +0 -1
  253. package/dist/utils/MathUtils.d.ts +0 -12
  254. package/dist/utils/MathUtils.d.ts.map +0 -1
  255. package/dist/utils/MathUtils.mjs +0 -26
  256. package/dist/utils/MathUtils.mjs.map +0 -1
@@ -0,0 +1,141 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { AuthError, InteractionRequiredAuthError } from "@azure/msal-common";
7
+ import {
8
+ createBrowserAuthError,
9
+ BrowserAuthErrorCodes,
10
+ } from "./BrowserAuthError";
11
+
12
+ export type OSError = {
13
+ error?: number;
14
+ protocol_error?: string;
15
+ properties?: object;
16
+ status?: string;
17
+ retryable?: boolean;
18
+ };
19
+
20
+ const INVALID_METHOD_ERROR = -2147186943;
21
+
22
+ export const NativeStatusCode = {
23
+ USER_INTERACTION_REQUIRED: "USER_INTERACTION_REQUIRED",
24
+ USER_CANCEL: "USER_CANCEL",
25
+ NO_NETWORK: "NO_NETWORK",
26
+ TRANSIENT_ERROR: "TRANSIENT_ERROR",
27
+ PERSISTENT_ERROR: "PERSISTENT_ERROR",
28
+ DISABLED: "DISABLED",
29
+ ACCOUNT_UNAVAILABLE: "ACCOUNT_UNAVAILABLE",
30
+ } as const;
31
+ export type NativeStatusCode =
32
+ (typeof NativeStatusCode)[keyof typeof NativeStatusCode];
33
+
34
+ export const NativeAuthErrorMessage = {
35
+ extensionError: {
36
+ code: "ContentError",
37
+ },
38
+ userSwitch: {
39
+ code: "user_switch",
40
+ desc: "User attempted to switch accounts in the native broker, which is not allowed. All new accounts must sign-in through the standard web flow first, please try again.",
41
+ },
42
+ tokensNotFoundInCache: {
43
+ code: "tokens_not_found_in_internal_memory_cache",
44
+ desc: "Tokens not cached in MSAL JS internal memory, please make the WAM request",
45
+ },
46
+ };
47
+
48
+ export class NativeAuthError extends AuthError {
49
+ ext: OSError | undefined;
50
+
51
+ constructor(errorCode: string, description: string, ext?: OSError) {
52
+ super(errorCode, description);
53
+
54
+ Object.setPrototypeOf(this, NativeAuthError.prototype);
55
+ this.name = "NativeAuthError";
56
+ this.ext = ext;
57
+ }
58
+
59
+ /**
60
+ * These errors should result in a fallback to the 'standard' browser based auth flow.
61
+ */
62
+ isFatal(): boolean {
63
+ if (
64
+ this.ext &&
65
+ this.ext.status &&
66
+ (this.ext.status === NativeStatusCode.PERSISTENT_ERROR ||
67
+ this.ext.status === NativeStatusCode.DISABLED)
68
+ ) {
69
+ return true;
70
+ }
71
+
72
+ if (
73
+ this.ext &&
74
+ this.ext.error &&
75
+ this.ext.error === INVALID_METHOD_ERROR
76
+ ) {
77
+ return true;
78
+ }
79
+
80
+ switch (this.errorCode) {
81
+ case NativeAuthErrorMessage.extensionError.code:
82
+ return true;
83
+ default:
84
+ return false;
85
+ }
86
+ }
87
+
88
+ /**
89
+ * Create the appropriate error object based on the WAM status code.
90
+ * @param code
91
+ * @param description
92
+ * @param ext
93
+ * @returns
94
+ */
95
+ static createError(
96
+ code: string,
97
+ description: string,
98
+ ext?: OSError
99
+ ): AuthError {
100
+ if (ext && ext.status) {
101
+ switch (ext.status) {
102
+ case NativeStatusCode.ACCOUNT_UNAVAILABLE:
103
+ return InteractionRequiredAuthError.createNativeAccountUnavailableError();
104
+ case NativeStatusCode.USER_INTERACTION_REQUIRED:
105
+ return new InteractionRequiredAuthError(code, description);
106
+ case NativeStatusCode.USER_CANCEL:
107
+ return createBrowserAuthError(
108
+ BrowserAuthErrorCodes.userCancelled
109
+ );
110
+ case NativeStatusCode.NO_NETWORK:
111
+ return createBrowserAuthError(
112
+ BrowserAuthErrorCodes.noNetworkConnectivity
113
+ );
114
+ }
115
+ }
116
+
117
+ return new NativeAuthError(code, description, ext);
118
+ }
119
+
120
+ /**
121
+ * Creates user switch error when the user chooses a different account in the native broker prompt
122
+ * @returns
123
+ */
124
+ static createUserSwitchError(): NativeAuthError {
125
+ return new NativeAuthError(
126
+ NativeAuthErrorMessage.userSwitch.code,
127
+ NativeAuthErrorMessage.userSwitch.desc
128
+ );
129
+ }
130
+
131
+ /**
132
+ * Creates a tokens not found error when the internal cache look up fails
133
+ * @returns NativeAuthError: tokensNotFoundInCache
134
+ */
135
+ static createTokensNotFoundInCacheError(): NativeAuthError {
136
+ return new NativeAuthError(
137
+ NativeAuthErrorMessage.tokensNotFoundInCache.code,
138
+ NativeAuthErrorMessage.tokensNotFoundInCache.desc
139
+ );
140
+ }
141
+ }
@@ -0,0 +1,176 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import {
7
+ ICrypto,
8
+ Logger,
9
+ AccountEntity,
10
+ CacheManager,
11
+ } from "@azure/msal-common";
12
+ import { InteractionType } from "../utils/BrowserConstants";
13
+ import {
14
+ EventCallbackFunction,
15
+ EventError,
16
+ EventMessage,
17
+ EventPayload,
18
+ } from "./EventMessage";
19
+ import { EventType } from "./EventType";
20
+
21
+ export class EventHandler {
22
+ // Callback for subscribing to events
23
+ private eventCallbacks: Map<string, EventCallbackFunction>;
24
+ private logger: Logger;
25
+ private browserCrypto: ICrypto;
26
+ private listeningToStorageEvents: boolean;
27
+
28
+ constructor(logger: Logger, browserCrypto: ICrypto) {
29
+ this.eventCallbacks = new Map();
30
+ this.logger = logger;
31
+ this.browserCrypto = browserCrypto;
32
+ this.listeningToStorageEvents = false;
33
+ this.handleAccountCacheChange =
34
+ this.handleAccountCacheChange.bind(this);
35
+ }
36
+
37
+ /**
38
+ * Adds event callbacks to array
39
+ * @param callback
40
+ */
41
+ addEventCallback(callback: EventCallbackFunction): string | null {
42
+ if (typeof window !== "undefined") {
43
+ const callbackId = this.browserCrypto.createNewGuid();
44
+ this.eventCallbacks.set(callbackId, callback);
45
+ this.logger.verbose(
46
+ `Event callback registered with id: ${callbackId}`
47
+ );
48
+
49
+ return callbackId;
50
+ }
51
+
52
+ return null;
53
+ }
54
+
55
+ /**
56
+ * Removes callback with provided id from callback array
57
+ * @param callbackId
58
+ */
59
+ removeEventCallback(callbackId: string): void {
60
+ this.eventCallbacks.delete(callbackId);
61
+ this.logger.verbose(`Event callback ${callbackId} removed.`);
62
+ }
63
+
64
+ /**
65
+ * Adds event listener that emits an event when a user account is added or removed from localstorage in a different browser tab or window
66
+ */
67
+ enableAccountStorageEvents(): void {
68
+ if (typeof window === "undefined") {
69
+ return;
70
+ }
71
+
72
+ if (!this.listeningToStorageEvents) {
73
+ this.logger.verbose("Adding account storage listener.");
74
+ this.listeningToStorageEvents = true;
75
+ window.addEventListener("storage", this.handleAccountCacheChange);
76
+ } else {
77
+ this.logger.verbose("Account storage listener already registered.");
78
+ }
79
+ }
80
+
81
+ /**
82
+ * Removes event listener that emits an event when a user account is added or removed from localstorage in a different browser tab or window
83
+ */
84
+ disableAccountStorageEvents(): void {
85
+ if (typeof window === "undefined") {
86
+ return;
87
+ }
88
+
89
+ if (this.listeningToStorageEvents) {
90
+ this.logger.verbose("Removing account storage listener.");
91
+ window.removeEventListener(
92
+ "storage",
93
+ this.handleAccountCacheChange
94
+ );
95
+ this.listeningToStorageEvents = false;
96
+ } else {
97
+ this.logger.verbose("No account storage listener registered.");
98
+ }
99
+ }
100
+
101
+ /**
102
+ * Emits events by calling callback with event message
103
+ * @param eventType
104
+ * @param interactionType
105
+ * @param payload
106
+ * @param error
107
+ */
108
+ emitEvent(
109
+ eventType: EventType,
110
+ interactionType?: InteractionType,
111
+ payload?: EventPayload,
112
+ error?: EventError
113
+ ): void {
114
+ if (typeof window !== "undefined") {
115
+ const message: EventMessage = {
116
+ eventType: eventType,
117
+ interactionType: interactionType || null,
118
+ payload: payload || null,
119
+ error: error || null,
120
+ timestamp: Date.now(),
121
+ };
122
+
123
+ this.logger.info(`Emitting event: ${eventType}`);
124
+
125
+ this.eventCallbacks.forEach(
126
+ (callback: EventCallbackFunction, callbackId: string) => {
127
+ this.logger.verbose(
128
+ `Emitting event to callback ${callbackId}: ${eventType}`
129
+ );
130
+ callback.apply(null, [message]);
131
+ }
132
+ );
133
+ }
134
+ }
135
+
136
+ /**
137
+ * Emit account added/removed events when cached accounts are changed in a different tab or frame
138
+ */
139
+ private handleAccountCacheChange(e: StorageEvent): void {
140
+ try {
141
+ const cacheValue = e.newValue || e.oldValue;
142
+ if (!cacheValue) {
143
+ return;
144
+ }
145
+ const parsedValue = JSON.parse(cacheValue);
146
+ if (
147
+ typeof parsedValue !== "object" ||
148
+ !AccountEntity.isAccountEntity(parsedValue)
149
+ ) {
150
+ return;
151
+ }
152
+ const accountEntity = CacheManager.toObject<AccountEntity>(
153
+ new AccountEntity(),
154
+ parsedValue
155
+ );
156
+ const accountInfo = accountEntity.getAccountInfo();
157
+ if (!e.oldValue && e.newValue) {
158
+ this.logger.info(
159
+ "Account was added to cache in a different window"
160
+ );
161
+ this.emitEvent(EventType.ACCOUNT_ADDED, undefined, accountInfo);
162
+ } else if (!e.newValue && e.oldValue) {
163
+ this.logger.info(
164
+ "Account was removed from cache in a different window"
165
+ );
166
+ this.emitEvent(
167
+ EventType.ACCOUNT_REMOVED,
168
+ undefined,
169
+ accountInfo
170
+ );
171
+ }
172
+ } catch (e) {
173
+ return;
174
+ }
175
+ }
176
+ }
@@ -0,0 +1,125 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { AuthError, AccountInfo } from "@azure/msal-common";
7
+ import { EventType } from "./EventType";
8
+ import { InteractionStatus, InteractionType } from "../utils/BrowserConstants";
9
+ import {
10
+ PopupRequest,
11
+ RedirectRequest,
12
+ SilentRequest,
13
+ SsoSilentRequest,
14
+ EndSessionRequest,
15
+ AuthenticationResult,
16
+ } from "..";
17
+
18
+ export type EventMessage = {
19
+ eventType: EventType;
20
+ interactionType: InteractionType | null;
21
+ payload: EventPayload;
22
+ error: EventError;
23
+ timestamp: number;
24
+ };
25
+
26
+ export type PopupEvent = {
27
+ popupWindow: Window;
28
+ };
29
+
30
+ export type EventPayload =
31
+ | AccountInfo
32
+ | PopupRequest
33
+ | RedirectRequest
34
+ | SilentRequest
35
+ | SsoSilentRequest
36
+ | EndSessionRequest
37
+ | AuthenticationResult
38
+ | PopupEvent
39
+ | null;
40
+
41
+ export type EventError = AuthError | Error | null;
42
+
43
+ export type EventCallbackFunction = (message: EventMessage) => void;
44
+
45
+ export class EventMessageUtils {
46
+ /**
47
+ * Gets interaction status from event message
48
+ * @param message
49
+ * @param currentStatus
50
+ */
51
+ static getInteractionStatusFromEvent(
52
+ message: EventMessage,
53
+ currentStatus?: InteractionStatus
54
+ ): InteractionStatus | null {
55
+ switch (message.eventType) {
56
+ case EventType.LOGIN_START:
57
+ return InteractionStatus.Login;
58
+ case EventType.SSO_SILENT_START:
59
+ return InteractionStatus.SsoSilent;
60
+ case EventType.ACQUIRE_TOKEN_START:
61
+ if (
62
+ message.interactionType === InteractionType.Redirect ||
63
+ message.interactionType === InteractionType.Popup
64
+ ) {
65
+ return InteractionStatus.AcquireToken;
66
+ }
67
+ break;
68
+ case EventType.HANDLE_REDIRECT_START:
69
+ return InteractionStatus.HandleRedirect;
70
+ case EventType.LOGOUT_START:
71
+ return InteractionStatus.Logout;
72
+ case EventType.SSO_SILENT_SUCCESS:
73
+ case EventType.SSO_SILENT_FAILURE:
74
+ if (
75
+ currentStatus &&
76
+ currentStatus !== InteractionStatus.SsoSilent
77
+ ) {
78
+ // Prevent this event from clearing any status other than ssoSilent
79
+ break;
80
+ }
81
+ return InteractionStatus.None;
82
+ case EventType.LOGOUT_END:
83
+ if (
84
+ currentStatus &&
85
+ currentStatus !== InteractionStatus.Logout
86
+ ) {
87
+ // Prevent this event from clearing any status other than logout
88
+ break;
89
+ }
90
+ return InteractionStatus.None;
91
+ case EventType.HANDLE_REDIRECT_END:
92
+ if (
93
+ currentStatus &&
94
+ currentStatus !== InteractionStatus.HandleRedirect
95
+ ) {
96
+ // Prevent this event from clearing any status other than handleRedirect
97
+ break;
98
+ }
99
+ return InteractionStatus.None;
100
+ case EventType.LOGIN_SUCCESS:
101
+ case EventType.LOGIN_FAILURE:
102
+ case EventType.ACQUIRE_TOKEN_SUCCESS:
103
+ case EventType.ACQUIRE_TOKEN_FAILURE:
104
+ case EventType.RESTORE_FROM_BFCACHE:
105
+ if (
106
+ message.interactionType === InteractionType.Redirect ||
107
+ message.interactionType === InteractionType.Popup
108
+ ) {
109
+ if (
110
+ currentStatus &&
111
+ currentStatus !== InteractionStatus.Login &&
112
+ currentStatus !== InteractionStatus.AcquireToken
113
+ ) {
114
+ // Prevent this event from clearing any status other than login or acquireToken
115
+ break;
116
+ }
117
+ return InteractionStatus.None;
118
+ }
119
+ break;
120
+ default:
121
+ break;
122
+ }
123
+ return null;
124
+ }
125
+ }
@@ -0,0 +1,33 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ export const EventType = {
7
+ INITIALIZE_START: "msal:initializeStart",
8
+ INITIALIZE_END: "msal:initializeEnd",
9
+ ACCOUNT_ADDED: "msal:accountAdded",
10
+ ACCOUNT_REMOVED: "msal:accountRemoved",
11
+ LOGIN_START: "msal:loginStart",
12
+ LOGIN_SUCCESS: "msal:loginSuccess",
13
+ LOGIN_FAILURE: "msal:loginFailure",
14
+ ACQUIRE_TOKEN_START: "msal:acquireTokenStart",
15
+ ACQUIRE_TOKEN_SUCCESS: "msal:acquireTokenSuccess",
16
+ ACQUIRE_TOKEN_FAILURE: "msal:acquireTokenFailure",
17
+ ACQUIRE_TOKEN_NETWORK_START: "msal:acquireTokenFromNetworkStart",
18
+ SSO_SILENT_START: "msal:ssoSilentStart",
19
+ SSO_SILENT_SUCCESS: "msal:ssoSilentSuccess",
20
+ SSO_SILENT_FAILURE: "msal:ssoSilentFailure",
21
+ ACQUIRE_TOKEN_BY_CODE_START: "msal:acquireTokenByCodeStart",
22
+ ACQUIRE_TOKEN_BY_CODE_SUCCESS: "msal:acquireTokenByCodeSuccess",
23
+ ACQUIRE_TOKEN_BY_CODE_FAILURE: "msal:acquireTokenByCodeFailure",
24
+ HANDLE_REDIRECT_START: "msal:handleRedirectStart",
25
+ HANDLE_REDIRECT_END: "msal:handleRedirectEnd",
26
+ POPUP_OPENED: "msal:popupOpened",
27
+ LOGOUT_START: "msal:logoutStart",
28
+ LOGOUT_SUCCESS: "msal:logoutSuccess",
29
+ LOGOUT_FAILURE: "msal:logoutFailure",
30
+ LOGOUT_END: "msal:logoutEnd",
31
+ RESTORE_FROM_BFCACHE: "msal:restoreFromBFCache",
32
+ } as const;
33
+ export type EventType = (typeof EventType)[keyof typeof EventType];
package/src/index.ts ADDED
@@ -0,0 +1,152 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ /**
7
+ * @packageDocumentation
8
+ * @module @azure/msal-browser
9
+ */
10
+
11
+ /**
12
+ * Warning: This set of exports is purely intended to be used by other MSAL libraries, and should be considered potentially unstable. We strongly discourage using them directly, you do so at your own risk.
13
+ * Breaking changes to these APIs will be shipped under a minor version, instead of a major version.
14
+ */
15
+
16
+ export { PublicClientApplication } from "./app/PublicClientApplication";
17
+ export { IController } from "./controllers/IController";
18
+ export {
19
+ Configuration,
20
+ BrowserAuthOptions,
21
+ CacheOptions,
22
+ BrowserSystemOptions,
23
+ BrowserTelemetryOptions,
24
+ BrowserConfiguration,
25
+ DEFAULT_IFRAME_TIMEOUT_MS,
26
+ } from "./config/Configuration";
27
+ export {
28
+ InteractionType,
29
+ InteractionStatus,
30
+ BrowserCacheLocation,
31
+ WrapperSKU,
32
+ ApiId,
33
+ CacheLookupPolicy,
34
+ } from "./utils/BrowserConstants";
35
+ export { BrowserUtils } from "./utils/BrowserUtils";
36
+
37
+ /*
38
+ * export { IController} from "./controllers/IController";
39
+ * export { StandardController } from "./controllers/StandardController";
40
+ */
41
+
42
+ // Browser Errors
43
+ export {
44
+ BrowserAuthError,
45
+ BrowserAuthErrorMessage,
46
+ BrowserAuthErrorCodes,
47
+ } from "./error/BrowserAuthError";
48
+ export {
49
+ BrowserConfigurationAuthError,
50
+ BrowserConfigurationAuthErrorMessage,
51
+ } from "./error/BrowserConfigurationAuthError";
52
+
53
+ // Interfaces
54
+ export {
55
+ IPublicClientApplication,
56
+ stubbedPublicClientApplication,
57
+ } from "./app/IPublicClientApplication";
58
+ export { INavigationClient } from "./navigation/INavigationClient";
59
+ export { NavigationClient } from "./navigation/NavigationClient";
60
+ export { NavigationOptions } from "./navigation/NavigationOptions";
61
+ export { PopupRequest } from "./request/PopupRequest";
62
+ export { RedirectRequest } from "./request/RedirectRequest";
63
+ export { SilentRequest } from "./request/SilentRequest";
64
+ export { SsoSilentRequest } from "./request/SsoSilentRequest";
65
+ export { EndSessionRequest } from "./request/EndSessionRequest";
66
+ export { EndSessionPopupRequest } from "./request/EndSessionPopupRequest";
67
+ export { AuthorizationUrlRequest } from "./request/AuthorizationUrlRequest";
68
+ export { AuthorizationCodeRequest } from "./request/AuthorizationCodeRequest";
69
+ export { AuthenticationResult } from "./response/AuthenticationResult";
70
+ export { ClearCacheRequest } from "./request/ClearCacheRequest";
71
+
72
+ // Cache
73
+ export { LoadTokenOptions } from "./cache/TokenCache";
74
+ export { ITokenCache } from "./cache/ITokenCache";
75
+
76
+ // Storage
77
+ export { MemoryStorage } from "./cache/MemoryStorage";
78
+ export { BrowserStorage } from "./cache/BrowserStorage";
79
+
80
+ // Events
81
+ export {
82
+ EventMessage,
83
+ EventPayload,
84
+ EventError,
85
+ EventCallbackFunction,
86
+ EventMessageUtils,
87
+ PopupEvent,
88
+ } from "./event/EventMessage";
89
+ export { EventType } from "./event/EventType";
90
+
91
+ export {
92
+ SignedHttpRequest,
93
+ SignedHttpRequestOptions,
94
+ } from "./crypto/SignedHttpRequest";
95
+
96
+ export {
97
+ PopupWindowAttributes,
98
+ PopupSize,
99
+ PopupPosition,
100
+ } from "./request/PopupWindowAttributes";
101
+
102
+ // Telemetry
103
+ export { BrowserPerformanceClient } from "./telemetry/BrowserPerformanceClient";
104
+ export { BrowserPerformanceMeasurement } from "./telemetry/BrowserPerformanceMeasurement";
105
+
106
+ // Common Object Formats
107
+ export {
108
+ AuthenticationScheme,
109
+ // Account
110
+ AccountInfo,
111
+ AccountEntity,
112
+ IdTokenClaims,
113
+ // Error
114
+ AuthError,
115
+ AuthErrorMessage,
116
+ ClientAuthError,
117
+ ClientAuthErrorMessage,
118
+ ClientConfigurationError,
119
+ ClientConfigurationErrorMessage,
120
+ InteractionRequiredAuthError,
121
+ InteractionRequiredAuthErrorMessage,
122
+ ServerError,
123
+ // Network
124
+ INetworkModule,
125
+ NetworkResponse,
126
+ NetworkRequestOptions,
127
+ // Logger Object
128
+ ILoggerCallback,
129
+ Logger,
130
+ LogLevel,
131
+ // Protocol Mode
132
+ ProtocolMode,
133
+ ServerResponseType,
134
+ PromptValue,
135
+ // Server Response
136
+ ExternalTokenResponse,
137
+ // Utils
138
+ StringUtils,
139
+ UrlString,
140
+ // AzureCloudInstance enum
141
+ AzureCloudInstance,
142
+ AzureCloudOptions,
143
+ AuthenticationHeaderParser,
144
+ OIDC_DEFAULT_SCOPES,
145
+ PerformanceCallbackFunction,
146
+ PerformanceEvent,
147
+ PerformanceEvents,
148
+ // Telemetry
149
+ InProgressPerformanceEvent,
150
+ } from "@azure/msal-common";
151
+
152
+ export { version } from "./packageMetadata";