@azure/msal-browser 3.0.1 → 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 (257) hide show
  1. package/README.md +5 -5
  2. package/dist/app/IPublicClientApplication.d.ts +6 -2
  3. package/dist/app/IPublicClientApplication.d.ts.map +1 -1
  4. package/dist/app/IPublicClientApplication.mjs +4 -1
  5. package/dist/app/IPublicClientApplication.mjs.map +1 -1
  6. package/dist/app/PublicClientApplication.d.ts +11 -3
  7. package/dist/app/PublicClientApplication.d.ts.map +1 -1
  8. package/dist/app/PublicClientApplication.mjs +10 -2
  9. package/dist/app/PublicClientApplication.mjs.map +1 -1
  10. package/dist/broker/nativeBroker/NativeMessageHandler.d.ts.map +1 -1
  11. package/dist/broker/nativeBroker/NativeMessageHandler.mjs +5 -4
  12. package/dist/broker/nativeBroker/NativeMessageHandler.mjs.map +1 -1
  13. package/dist/cache/AsyncMemoryStorage.mjs +4 -3
  14. package/dist/cache/AsyncMemoryStorage.mjs.map +1 -1
  15. package/dist/cache/BrowserCacheManager.d.ts +13 -3
  16. package/dist/cache/BrowserCacheManager.d.ts.map +1 -1
  17. package/dist/cache/BrowserCacheManager.mjs +54 -26
  18. package/dist/cache/BrowserCacheManager.mjs.map +1 -1
  19. package/dist/cache/BrowserStorage.mjs +1 -1
  20. package/dist/cache/BrowserStorage.mjs.map +1 -1
  21. package/dist/cache/CryptoKeyStore.mjs +1 -1
  22. package/dist/cache/CryptoKeyStore.mjs.map +1 -1
  23. package/dist/cache/DatabaseStorage.d.ts.map +1 -1
  24. package/dist/cache/DatabaseStorage.mjs +9 -8
  25. package/dist/cache/DatabaseStorage.mjs.map +1 -1
  26. package/dist/cache/MemoryStorage.mjs +1 -1
  27. package/dist/cache/MemoryStorage.mjs.map +1 -1
  28. package/dist/cache/TokenCache.d.ts.map +1 -1
  29. package/dist/cache/TokenCache.mjs +30 -30
  30. package/dist/cache/TokenCache.mjs.map +1 -1
  31. package/dist/config/Configuration.d.ts +3 -0
  32. package/dist/config/Configuration.d.ts.map +1 -1
  33. package/dist/config/Configuration.mjs +7 -11
  34. package/dist/config/Configuration.mjs.map +1 -1
  35. package/dist/controllers/ControllerFactory.mjs +1 -1
  36. package/dist/controllers/ControllerFactory.mjs.map +1 -1
  37. package/dist/controllers/IController.d.ts +11 -18
  38. package/dist/controllers/IController.d.ts.map +1 -1
  39. package/dist/controllers/StandardController.d.ts +7 -26
  40. package/dist/controllers/StandardController.d.ts.map +1 -1
  41. package/dist/controllers/StandardController.mjs +28 -52
  42. package/dist/controllers/StandardController.mjs.map +1 -1
  43. package/dist/crypto/BrowserCrypto.d.ts.map +1 -1
  44. package/dist/crypto/BrowserCrypto.mjs +4 -3
  45. package/dist/crypto/BrowserCrypto.mjs.map +1 -1
  46. package/dist/crypto/CryptoOps.d.ts +0 -3
  47. package/dist/crypto/CryptoOps.d.ts.map +1 -1
  48. package/dist/crypto/CryptoOps.mjs +14 -17
  49. package/dist/crypto/CryptoOps.mjs.map +1 -1
  50. package/dist/crypto/ModernBrowserCrypto.mjs +1 -1
  51. package/dist/crypto/ModernBrowserCrypto.mjs.map +1 -1
  52. package/dist/crypto/PkceGenerator.d.ts +0 -1
  53. package/dist/crypto/PkceGenerator.d.ts.map +1 -1
  54. package/dist/crypto/PkceGenerator.mjs +8 -8
  55. package/dist/crypto/PkceGenerator.mjs.map +1 -1
  56. package/dist/crypto/SignedHttpRequest.mjs +1 -1
  57. package/dist/crypto/SignedHttpRequest.mjs.map +1 -1
  58. package/dist/encode/Base64Decode.d.ts +6 -19
  59. package/dist/encode/Base64Decode.d.ts.map +1 -1
  60. package/dist/encode/Base64Decode.mjs +17 -70
  61. package/dist/encode/Base64Decode.mjs.map +1 -1
  62. package/dist/encode/Base64Encode.d.ts +15 -27
  63. package/dist/encode/Base64Encode.d.ts.map +1 -1
  64. package/dist/encode/Base64Encode.mjs +36 -75
  65. package/dist/encode/Base64Encode.mjs.map +1 -1
  66. package/dist/error/BrowserAuthError.d.ts +54 -195
  67. package/dist/error/BrowserAuthError.d.ts.map +1 -1
  68. package/dist/error/BrowserAuthError.mjs +150 -381
  69. package/dist/error/BrowserAuthError.mjs.map +1 -1
  70. package/dist/error/BrowserAuthErrorCodes.d.ts +45 -0
  71. package/dist/error/BrowserAuthErrorCodes.d.ts.map +1 -0
  72. package/dist/error/BrowserAuthErrorCodes.mjs +53 -0
  73. package/dist/error/BrowserAuthErrorCodes.mjs.map +1 -0
  74. package/dist/error/BrowserConfigurationAuthError.mjs +1 -1
  75. package/dist/error/BrowserConfigurationAuthError.mjs.map +1 -1
  76. package/dist/error/NativeAuthError.d.ts.map +1 -1
  77. package/dist/error/NativeAuthError.mjs +5 -4
  78. package/dist/error/NativeAuthError.mjs.map +1 -1
  79. package/dist/event/EventHandler.mjs +1 -1
  80. package/dist/event/EventHandler.mjs.map +1 -1
  81. package/dist/event/EventMessage.mjs +1 -1
  82. package/dist/event/EventMessage.mjs.map +1 -1
  83. package/dist/event/EventType.mjs +1 -1
  84. package/dist/event/EventType.mjs.map +1 -1
  85. package/dist/index.d.ts +9 -6
  86. package/dist/index.d.ts.map +1 -1
  87. package/dist/index.mjs +5 -3
  88. package/dist/index.mjs.map +1 -1
  89. package/dist/interaction_client/BaseInteractionClient.d.ts +2 -1
  90. package/dist/interaction_client/BaseInteractionClient.d.ts.map +1 -1
  91. package/dist/interaction_client/BaseInteractionClient.mjs +1 -1
  92. package/dist/interaction_client/BaseInteractionClient.mjs.map +1 -1
  93. package/dist/interaction_client/HybridSpaAuthorizationCodeClient.mjs +1 -1
  94. package/dist/interaction_client/HybridSpaAuthorizationCodeClient.mjs.map +1 -1
  95. package/dist/interaction_client/NativeInteractionClient.d.ts +4 -10
  96. package/dist/interaction_client/NativeInteractionClient.d.ts.map +1 -1
  97. package/dist/interaction_client/NativeInteractionClient.mjs +22 -30
  98. package/dist/interaction_client/NativeInteractionClient.mjs.map +1 -1
  99. package/dist/interaction_client/PopupClient.d.ts.map +1 -1
  100. package/dist/interaction_client/PopupClient.mjs +16 -15
  101. package/dist/interaction_client/PopupClient.mjs.map +1 -1
  102. package/dist/interaction_client/RedirectClient.d.ts.map +1 -1
  103. package/dist/interaction_client/RedirectClient.mjs +7 -6
  104. package/dist/interaction_client/RedirectClient.mjs.map +1 -1
  105. package/dist/interaction_client/SilentAuthCodeClient.d.ts.map +1 -1
  106. package/dist/interaction_client/SilentAuthCodeClient.mjs +5 -4
  107. package/dist/interaction_client/SilentAuthCodeClient.mjs.map +1 -1
  108. package/dist/interaction_client/SilentCacheClient.d.ts +4 -2
  109. package/dist/interaction_client/SilentCacheClient.d.ts.map +1 -1
  110. package/dist/interaction_client/SilentCacheClient.mjs +10 -8
  111. package/dist/interaction_client/SilentCacheClient.mjs.map +1 -1
  112. package/dist/interaction_client/SilentIframeClient.d.ts.map +1 -1
  113. package/dist/interaction_client/SilentIframeClient.mjs +20 -47
  114. package/dist/interaction_client/SilentIframeClient.mjs.map +1 -1
  115. package/dist/interaction_client/SilentRefreshClient.d.ts.map +1 -1
  116. package/dist/interaction_client/SilentRefreshClient.mjs +4 -3
  117. package/dist/interaction_client/SilentRefreshClient.mjs.map +1 -1
  118. package/dist/interaction_client/StandardInteractionClient.d.ts.map +1 -1
  119. package/dist/interaction_client/StandardInteractionClient.mjs +8 -7
  120. package/dist/interaction_client/StandardInteractionClient.mjs.map +1 -1
  121. package/dist/interaction_handler/InteractionHandler.d.ts.map +1 -1
  122. package/dist/interaction_handler/InteractionHandler.mjs +9 -9
  123. package/dist/interaction_handler/InteractionHandler.mjs.map +1 -1
  124. package/dist/interaction_handler/RedirectHandler.d.ts.map +1 -1
  125. package/dist/interaction_handler/RedirectHandler.mjs +11 -10
  126. package/dist/interaction_handler/RedirectHandler.mjs.map +1 -1
  127. package/dist/interaction_handler/SilentHandler.d.ts.map +1 -1
  128. package/dist/interaction_handler/SilentHandler.mjs +10 -9
  129. package/dist/interaction_handler/SilentHandler.mjs.map +1 -1
  130. package/dist/navigation/NavigationClient.mjs +1 -1
  131. package/dist/navigation/NavigationClient.mjs.map +1 -1
  132. package/dist/network/FetchClient.d.ts.map +1 -1
  133. package/dist/network/FetchClient.mjs +9 -8
  134. package/dist/network/FetchClient.mjs.map +1 -1
  135. package/dist/operatingcontext/BaseOperatingContext.mjs +1 -1
  136. package/dist/operatingcontext/BaseOperatingContext.mjs.map +1 -1
  137. package/dist/operatingcontext/StandardOperatingContext.mjs +1 -1
  138. package/dist/operatingcontext/StandardOperatingContext.mjs.map +1 -1
  139. package/dist/operatingcontext/TeamsAppOperatingContext.mjs +1 -1
  140. package/dist/operatingcontext/TeamsAppOperatingContext.mjs.map +1 -1
  141. package/dist/packageMetadata.d.ts +1 -1
  142. package/dist/packageMetadata.mjs +2 -2
  143. package/dist/packageMetadata.mjs.map +1 -1
  144. package/dist/request/ClearCacheRequest.d.ts +11 -0
  145. package/dist/request/ClearCacheRequest.d.ts.map +1 -0
  146. package/dist/telemetry/BrowserPerformanceClient.d.ts +2 -4
  147. package/dist/telemetry/BrowserPerformanceClient.d.ts.map +1 -1
  148. package/dist/telemetry/BrowserPerformanceClient.mjs +2 -6
  149. package/dist/telemetry/BrowserPerformanceClient.mjs.map +1 -1
  150. package/dist/telemetry/BrowserPerformanceMeasurement.mjs +1 -1
  151. package/dist/telemetry/BrowserPerformanceMeasurement.mjs.map +1 -1
  152. package/dist/utils/BrowserConstants.mjs +1 -1
  153. package/dist/utils/BrowserConstants.mjs.map +1 -1
  154. package/dist/utils/BrowserProtocolUtils.d.ts.map +1 -1
  155. package/dist/utils/BrowserProtocolUtils.mjs +3 -3
  156. package/dist/utils/BrowserProtocolUtils.mjs.map +1 -1
  157. package/dist/utils/BrowserStringUtils.mjs +1 -1
  158. package/dist/utils/BrowserStringUtils.mjs.map +1 -1
  159. package/dist/utils/BrowserUtils.d.ts +0 -9
  160. package/dist/utils/BrowserUtils.d.ts.map +1 -1
  161. package/dist/utils/BrowserUtils.mjs +8 -33
  162. package/dist/utils/BrowserUtils.mjs.map +1 -1
  163. package/lib/msal-browser.cjs +6601 -7104
  164. package/lib/msal-browser.cjs.map +1 -1
  165. package/lib/msal-browser.js +6601 -7104
  166. package/lib/msal-browser.js.map +1 -1
  167. package/lib/msal-browser.min.js +65 -62
  168. package/package.json +7 -5
  169. package/src/app/IPublicClientApplication.ts +199 -0
  170. package/src/app/PublicClientApplication.ts +405 -0
  171. package/src/broker/nativeBroker/NativeMessageHandler.ts +419 -0
  172. package/src/broker/nativeBroker/NativeRequest.ts +52 -0
  173. package/src/broker/nativeBroker/NativeResponse.ts +56 -0
  174. package/src/cache/AsyncMemoryStorage.ts +158 -0
  175. package/src/cache/BrowserCacheManager.ts +1994 -0
  176. package/src/cache/BrowserStorage.ts +54 -0
  177. package/src/cache/CryptoKeyStore.ts +63 -0
  178. package/src/cache/DatabaseStorage.ts +287 -0
  179. package/src/cache/IAsyncMemoryStorage.ts +36 -0
  180. package/src/cache/ITokenCache.ts +21 -0
  181. package/src/cache/IWindowStorage.ts +36 -0
  182. package/src/cache/MemoryStorage.ts +42 -0
  183. package/src/cache/TokenCache.ts +484 -0
  184. package/src/config/Configuration.ts +375 -0
  185. package/src/controllers/ControllerFactory.ts +55 -0
  186. package/src/controllers/IController.ts +131 -0
  187. package/src/controllers/StandardController.ts +2107 -0
  188. package/src/crypto/BrowserCrypto.ts +135 -0
  189. package/src/crypto/CryptoOps.ts +247 -0
  190. package/src/crypto/ISubtleCrypto.ts +30 -0
  191. package/src/crypto/ModernBrowserCrypto.ts +69 -0
  192. package/src/crypto/PkceGenerator.ts +76 -0
  193. package/src/crypto/SignedHttpRequest.ts +76 -0
  194. package/src/encode/Base64Decode.ts +26 -0
  195. package/src/encode/Base64Encode.ts +52 -0
  196. package/src/error/BrowserAuthError.ts +340 -0
  197. package/src/error/BrowserAuthErrorCodes.ts +56 -0
  198. package/src/error/BrowserConfigurationAuthError.ts +133 -0
  199. package/src/error/NativeAuthError.ts +141 -0
  200. package/src/event/EventHandler.ts +176 -0
  201. package/src/event/EventMessage.ts +125 -0
  202. package/src/event/EventType.ts +33 -0
  203. package/src/index.ts +152 -0
  204. package/src/interaction_client/BaseInteractionClient.ts +301 -0
  205. package/src/interaction_client/HybridSpaAuthorizationCodeClient.ts +16 -0
  206. package/src/interaction_client/NativeInteractionClient.ts +907 -0
  207. package/src/interaction_client/PopupClient.ts +926 -0
  208. package/src/interaction_client/RedirectClient.ts +616 -0
  209. package/src/interaction_client/SilentAuthCodeClient.ts +163 -0
  210. package/src/interaction_client/SilentCacheClient.ts +133 -0
  211. package/src/interaction_client/SilentIframeClient.ts +301 -0
  212. package/src/interaction_client/SilentRefreshClient.ts +120 -0
  213. package/src/interaction_client/StandardInteractionClient.ts +481 -0
  214. package/src/interaction_handler/InteractionHandler.ts +246 -0
  215. package/src/interaction_handler/RedirectHandler.ts +225 -0
  216. package/src/interaction_handler/SilentHandler.ts +266 -0
  217. package/src/navigation/INavigationClient.ts +23 -0
  218. package/src/navigation/NavigationClient.ts +55 -0
  219. package/src/navigation/NavigationOptions.ts +18 -0
  220. package/src/network/FetchClient.ts +130 -0
  221. package/src/operatingcontext/BaseOperatingContext.ts +82 -0
  222. package/src/operatingcontext/StandardOperatingContext.ts +49 -0
  223. package/src/operatingcontext/TeamsAppOperatingContext.ts +48 -0
  224. package/src/packageMetadata.ts +3 -0
  225. package/src/request/AuthorizationCodeRequest.ts +19 -0
  226. package/src/request/AuthorizationUrlRequest.ts +17 -0
  227. package/src/request/ClearCacheRequest.ts +16 -0
  228. package/src/request/EndSessionPopupRequest.ts +26 -0
  229. package/src/request/EndSessionRequest.ts +23 -0
  230. package/src/request/PopupRequest.ts +51 -0
  231. package/src/request/PopupWindowAttributes.ts +22 -0
  232. package/src/request/RedirectRequest.ts +51 -0
  233. package/src/request/SilentRequest.ts +49 -0
  234. package/src/request/SsoSilentRequest.ts +44 -0
  235. package/src/response/AuthenticationResult.ts +13 -0
  236. package/src/telemetry/BrowserPerformanceClient.ts +226 -0
  237. package/src/telemetry/BrowserPerformanceMeasurement.ts +144 -0
  238. package/src/utils/BrowserConstants.ts +241 -0
  239. package/src/utils/BrowserProtocolUtils.ts +57 -0
  240. package/src/utils/BrowserStringUtils.ts +143 -0
  241. package/src/utils/BrowserUtils.ts +156 -0
  242. package/dist/crypto/GuidGenerator.d.ts +0 -13
  243. package/dist/crypto/GuidGenerator.d.ts.map +0 -1
  244. package/dist/crypto/GuidGenerator.mjs +0 -104
  245. package/dist/crypto/GuidGenerator.mjs.map +0 -1
  246. package/dist/internals.d.ts +0 -23
  247. package/dist/internals.d.ts.map +0 -1
  248. package/dist/internals.mjs +0 -28
  249. package/dist/internals.mjs.map +0 -1
  250. package/dist/network/XhrClient.d.ts +0 -41
  251. package/dist/network/XhrClient.d.ts.map +0 -1
  252. package/dist/network/XhrClient.mjs +0 -125
  253. package/dist/network/XhrClient.mjs.map +0 -1
  254. package/dist/utils/MathUtils.d.ts +0 -12
  255. package/dist/utils/MathUtils.d.ts.map +0 -1
  256. package/dist/utils/MathUtils.mjs +0 -26
  257. package/dist/utils/MathUtils.mjs.map +0 -1
@@ -0,0 +1,54 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { BrowserConfigurationAuthError } from "../error/BrowserConfigurationAuthError";
7
+ import { BrowserCacheLocation } from "../utils/BrowserConstants";
8
+ import { IWindowStorage } from "./IWindowStorage";
9
+
10
+ export class BrowserStorage implements IWindowStorage<string> {
11
+ private windowStorage: Storage;
12
+
13
+ constructor(cacheLocation: string) {
14
+ this.validateWindowStorage(cacheLocation);
15
+ this.windowStorage = window[cacheLocation];
16
+ }
17
+
18
+ private validateWindowStorage(cacheLocation: string): void {
19
+ if (
20
+ cacheLocation !== BrowserCacheLocation.LocalStorage &&
21
+ cacheLocation !== BrowserCacheLocation.SessionStorage
22
+ ) {
23
+ throw BrowserConfigurationAuthError.createStorageNotSupportedError(
24
+ cacheLocation
25
+ );
26
+ }
27
+ const storageSupported = !!window[cacheLocation];
28
+ if (!storageSupported) {
29
+ throw BrowserConfigurationAuthError.createStorageNotSupportedError(
30
+ cacheLocation
31
+ );
32
+ }
33
+ }
34
+
35
+ getItem(key: string): string | null {
36
+ return this.windowStorage.getItem(key);
37
+ }
38
+
39
+ setItem(key: string, value: string): void {
40
+ this.windowStorage.setItem(key, value);
41
+ }
42
+
43
+ removeItem(key: string): void {
44
+ this.windowStorage.removeItem(key);
45
+ }
46
+
47
+ getKeys(): string[] {
48
+ return Object.keys(this.windowStorage);
49
+ }
50
+
51
+ containsKey(key: string): boolean {
52
+ return this.windowStorage.hasOwnProperty(key);
53
+ }
54
+ }
@@ -0,0 +1,63 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { Logger } from "@azure/msal-common";
7
+ import { CachedKeyPair } from "../crypto/CryptoOps";
8
+ import { AsyncMemoryStorage } from "./AsyncMemoryStorage";
9
+
10
+ export const CryptoKeyStoreNames = {
11
+ asymmetricKeys: "asymmetricKeys",
12
+ symmetricKeys: "symmetricKeys",
13
+ } as const;
14
+ export type CryptoKeyStoreNames =
15
+ (typeof CryptoKeyStoreNames)[keyof typeof CryptoKeyStoreNames];
16
+
17
+ /**
18
+ * MSAL CryptoKeyStore DB Version 2
19
+ */
20
+ export class CryptoKeyStore {
21
+ public asymmetricKeys: AsyncMemoryStorage<CachedKeyPair>;
22
+ public symmetricKeys: AsyncMemoryStorage<CryptoKey>;
23
+ public logger: Logger;
24
+
25
+ constructor(logger: Logger) {
26
+ this.logger = logger;
27
+ this.asymmetricKeys = new AsyncMemoryStorage<CachedKeyPair>(
28
+ this.logger,
29
+ CryptoKeyStoreNames.asymmetricKeys
30
+ );
31
+ this.symmetricKeys = new AsyncMemoryStorage<CryptoKey>(
32
+ this.logger,
33
+ CryptoKeyStoreNames.symmetricKeys
34
+ );
35
+ }
36
+
37
+ async clear(): Promise<boolean> {
38
+ // Delete in-memory keystores
39
+ this.asymmetricKeys.clearInMemory();
40
+ this.symmetricKeys.clearInMemory();
41
+
42
+ /**
43
+ * There is only one database, so calling clearPersistent on asymmetric keystore takes care of
44
+ * every persistent keystore
45
+ */
46
+ try {
47
+ await this.asymmetricKeys.clearPersistent();
48
+ return true;
49
+ } catch (e) {
50
+ if (e instanceof Error) {
51
+ this.logger.error(
52
+ `Clearing keystore failed with error: ${e.message}`
53
+ );
54
+ } else {
55
+ this.logger.error(
56
+ "Clearing keystore failed with unknown error"
57
+ );
58
+ }
59
+
60
+ return false;
61
+ }
62
+ }
63
+ }
@@ -0,0 +1,287 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import {
7
+ createBrowserAuthError,
8
+ BrowserAuthErrorCodes,
9
+ } from "../error/BrowserAuthError";
10
+ import { DB_NAME, DB_TABLE_NAME, DB_VERSION } from "../utils/BrowserConstants";
11
+ import { IAsyncStorage } from "./IAsyncMemoryStorage";
12
+
13
+ interface IDBOpenDBRequestEvent extends Event {
14
+ target: IDBOpenDBRequest & EventTarget;
15
+ }
16
+
17
+ interface IDBOpenOnUpgradeNeededEvent extends IDBVersionChangeEvent {
18
+ target: IDBOpenDBRequest & EventTarget;
19
+ }
20
+
21
+ interface IDBRequestEvent extends Event {
22
+ target: IDBRequest & EventTarget;
23
+ }
24
+
25
+ /**
26
+ * Storage wrapper for IndexedDB storage in browsers: https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API
27
+ */
28
+ export class DatabaseStorage<T> implements IAsyncStorage<T> {
29
+ private db: IDBDatabase | undefined;
30
+ private dbName: string;
31
+ private tableName: string;
32
+ private version: number;
33
+ private dbOpen: boolean;
34
+
35
+ constructor() {
36
+ this.dbName = DB_NAME;
37
+ this.version = DB_VERSION;
38
+ this.tableName = DB_TABLE_NAME;
39
+ this.dbOpen = false;
40
+ }
41
+
42
+ /**
43
+ * Opens IndexedDB instance.
44
+ */
45
+ async open(): Promise<void> {
46
+ return new Promise((resolve, reject) => {
47
+ const openDB = window.indexedDB.open(this.dbName, this.version);
48
+ openDB.addEventListener(
49
+ "upgradeneeded",
50
+ (e: IDBVersionChangeEvent) => {
51
+ const event = e as IDBOpenOnUpgradeNeededEvent;
52
+ event.target.result.createObjectStore(this.tableName);
53
+ }
54
+ );
55
+ openDB.addEventListener("success", (e: Event) => {
56
+ const event = e as IDBOpenDBRequestEvent;
57
+ this.db = event.target.result;
58
+ this.dbOpen = true;
59
+ resolve();
60
+ });
61
+ openDB.addEventListener("error", () =>
62
+ reject(
63
+ createBrowserAuthError(
64
+ BrowserAuthErrorCodes.databaseUnavailable
65
+ )
66
+ )
67
+ );
68
+ });
69
+ }
70
+
71
+ /**
72
+ * Closes the connection to IndexedDB database when all pending transactions
73
+ * complete.
74
+ */
75
+ closeConnection(): void {
76
+ const db = this.db;
77
+ if (db && this.dbOpen) {
78
+ db.close();
79
+ this.dbOpen = false;
80
+ }
81
+ }
82
+
83
+ /**
84
+ * Opens database if it's not already open
85
+ */
86
+ private async validateDbIsOpen(): Promise<void> {
87
+ if (!this.dbOpen) {
88
+ return await this.open();
89
+ }
90
+ }
91
+
92
+ /**
93
+ * Retrieves item from IndexedDB instance.
94
+ * @param key
95
+ */
96
+ async getItem(key: string): Promise<T | null> {
97
+ await this.validateDbIsOpen();
98
+ return new Promise<T>((resolve, reject) => {
99
+ // TODO: Add timeouts?
100
+ if (!this.db) {
101
+ return reject(
102
+ createBrowserAuthError(
103
+ BrowserAuthErrorCodes.databaseNotOpen
104
+ )
105
+ );
106
+ }
107
+ const transaction = this.db.transaction(
108
+ [this.tableName],
109
+ "readonly"
110
+ );
111
+ const objectStore = transaction.objectStore(this.tableName);
112
+ const dbGet = objectStore.get(key);
113
+
114
+ dbGet.addEventListener("success", (e: Event) => {
115
+ const event = e as IDBRequestEvent;
116
+ this.closeConnection();
117
+ resolve(event.target.result);
118
+ });
119
+
120
+ dbGet.addEventListener("error", (e: Event) => {
121
+ this.closeConnection();
122
+ reject(e);
123
+ });
124
+ });
125
+ }
126
+
127
+ /**
128
+ * Adds item to IndexedDB under given key
129
+ * @param key
130
+ * @param payload
131
+ */
132
+ async setItem(key: string, payload: T): Promise<void> {
133
+ await this.validateDbIsOpen();
134
+ return new Promise<void>((resolve: Function, reject: Function) => {
135
+ // TODO: Add timeouts?
136
+ if (!this.db) {
137
+ return reject(
138
+ createBrowserAuthError(
139
+ BrowserAuthErrorCodes.databaseNotOpen
140
+ )
141
+ );
142
+ }
143
+ const transaction = this.db.transaction(
144
+ [this.tableName],
145
+ "readwrite"
146
+ );
147
+
148
+ const objectStore = transaction.objectStore(this.tableName);
149
+
150
+ const dbPut = objectStore.put(payload, key);
151
+
152
+ dbPut.addEventListener("success", () => {
153
+ this.closeConnection();
154
+ resolve();
155
+ });
156
+
157
+ dbPut.addEventListener("error", (e) => {
158
+ this.closeConnection();
159
+ reject(e);
160
+ });
161
+ });
162
+ }
163
+
164
+ /**
165
+ * Removes item from IndexedDB under given key
166
+ * @param key
167
+ */
168
+ async removeItem(key: string): Promise<void> {
169
+ await this.validateDbIsOpen();
170
+ return new Promise<void>((resolve: Function, reject: Function) => {
171
+ if (!this.db) {
172
+ return reject(
173
+ createBrowserAuthError(
174
+ BrowserAuthErrorCodes.databaseNotOpen
175
+ )
176
+ );
177
+ }
178
+
179
+ const transaction = this.db.transaction(
180
+ [this.tableName],
181
+ "readwrite"
182
+ );
183
+ const objectStore = transaction.objectStore(this.tableName);
184
+ const dbDelete = objectStore.delete(key);
185
+
186
+ dbDelete.addEventListener("success", () => {
187
+ this.closeConnection();
188
+ resolve();
189
+ });
190
+
191
+ dbDelete.addEventListener("error", (e) => {
192
+ this.closeConnection();
193
+ reject(e);
194
+ });
195
+ });
196
+ }
197
+
198
+ /**
199
+ * Get all the keys from the storage object as an iterable array of strings.
200
+ */
201
+ async getKeys(): Promise<string[]> {
202
+ await this.validateDbIsOpen();
203
+ return new Promise<string[]>((resolve: Function, reject: Function) => {
204
+ if (!this.db) {
205
+ return reject(
206
+ createBrowserAuthError(
207
+ BrowserAuthErrorCodes.databaseNotOpen
208
+ )
209
+ );
210
+ }
211
+
212
+ const transaction = this.db.transaction(
213
+ [this.tableName],
214
+ "readonly"
215
+ );
216
+ const objectStore = transaction.objectStore(this.tableName);
217
+ const dbGetKeys = objectStore.getAllKeys();
218
+
219
+ dbGetKeys.addEventListener("success", (e: Event) => {
220
+ const event = e as IDBRequestEvent;
221
+ this.closeConnection();
222
+ resolve(event.target.result);
223
+ });
224
+
225
+ dbGetKeys.addEventListener("error", (e: Event) => {
226
+ this.closeConnection();
227
+ reject(e);
228
+ });
229
+ });
230
+ }
231
+
232
+ /**
233
+ *
234
+ * Checks whether there is an object under the search key in the object store
235
+ */
236
+ async containsKey(key: string): Promise<boolean> {
237
+ await this.validateDbIsOpen();
238
+
239
+ return new Promise<boolean>((resolve: Function, reject: Function) => {
240
+ if (!this.db) {
241
+ return reject(
242
+ createBrowserAuthError(
243
+ BrowserAuthErrorCodes.databaseNotOpen
244
+ )
245
+ );
246
+ }
247
+
248
+ const transaction = this.db.transaction(
249
+ [this.tableName],
250
+ "readonly"
251
+ );
252
+ const objectStore = transaction.objectStore(this.tableName);
253
+ const dbContainsKey = objectStore.count(key);
254
+
255
+ dbContainsKey.addEventListener("success", (e: Event) => {
256
+ const event = e as IDBRequestEvent;
257
+ this.closeConnection();
258
+ resolve(event.target.result === 1);
259
+ });
260
+
261
+ dbContainsKey.addEventListener("error", (e: Event) => {
262
+ this.closeConnection();
263
+ reject(e);
264
+ });
265
+ });
266
+ }
267
+
268
+ /**
269
+ * Deletes the MSAL database. The database is deleted rather than cleared to make it possible
270
+ * for client applications to downgrade to a previous MSAL version without worrying about forward compatibility issues
271
+ * with IndexedDB database versions.
272
+ */
273
+ async deleteDatabase(): Promise<boolean> {
274
+ // Check if database being deleted exists
275
+
276
+ if (this.db && this.dbOpen) {
277
+ this.closeConnection();
278
+ }
279
+
280
+ return new Promise<boolean>((resolve: Function, reject: Function) => {
281
+ const deleteDbRequest = window.indexedDB.deleteDatabase(DB_NAME);
282
+ deleteDbRequest.addEventListener("success", () => resolve(true));
283
+ deleteDbRequest.addEventListener("blocked", () => resolve(true));
284
+ deleteDbRequest.addEventListener("error", () => reject(false));
285
+ });
286
+ }
287
+ }
@@ -0,0 +1,36 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ export interface IAsyncStorage<T> {
7
+ /**
8
+ * Get the item from the asynchronous storage object matching the given key.
9
+ * @param key
10
+ */
11
+ getItem(key: string): Promise<T | null>;
12
+
13
+ /**
14
+ * Sets the item in the asynchronous storage object with the given key.
15
+ * @param key
16
+ * @param value
17
+ */
18
+ setItem(key: string, value: T): Promise<void>;
19
+
20
+ /**
21
+ * Removes the item in the asynchronous storage object matching the given key.
22
+ * @param key
23
+ */
24
+ removeItem(key: string): Promise<void>;
25
+
26
+ /**
27
+ * Get all the keys from the asynchronous storage object as an iterable array of strings.
28
+ */
29
+ getKeys(): Promise<string[]>;
30
+
31
+ /**
32
+ * Returns true or false if the given key is present in the cache.
33
+ * @param key
34
+ */
35
+ containsKey(key: string): Promise<boolean>;
36
+ }
@@ -0,0 +1,21 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { ExternalTokenResponse } from "@azure/msal-common";
7
+ import { SilentRequest } from "../request/SilentRequest";
8
+ import { LoadTokenOptions } from "./TokenCache";
9
+ import { AuthenticationResult } from "../response/AuthenticationResult";
10
+
11
+ export interface ITokenCache {
12
+ /**
13
+ * API to side-load tokens to MSAL cache
14
+ * @returns `AuthenticationResult` for the response that was loaded.
15
+ */
16
+ loadExternalTokens(
17
+ request: SilentRequest,
18
+ response: ExternalTokenResponse,
19
+ options: LoadTokenOptions
20
+ ): AuthenticationResult;
21
+ }
@@ -0,0 +1,36 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ export interface IWindowStorage<T> {
7
+ /**
8
+ * Get the item from the window storage object matching the given key.
9
+ * @param key
10
+ */
11
+ getItem(key: string): T | null;
12
+
13
+ /**
14
+ * Sets the item in the window storage object with the given key.
15
+ * @param key
16
+ * @param value
17
+ */
18
+ setItem(key: string, value: T): void;
19
+
20
+ /**
21
+ * Removes the item in the window storage object matching the given key.
22
+ * @param key
23
+ */
24
+ removeItem(key: string): void;
25
+
26
+ /**
27
+ * Get all the keys from the window storage object as an iterable array of strings.
28
+ */
29
+ getKeys(): string[];
30
+
31
+ /**
32
+ * Returns true or false if the given key is present in the cache.
33
+ * @param key
34
+ */
35
+ containsKey(key: string): boolean;
36
+ }
@@ -0,0 +1,42 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { IWindowStorage } from "./IWindowStorage";
7
+
8
+ export class MemoryStorage<T> implements IWindowStorage<T> {
9
+ private cache: Map<string, T>;
10
+
11
+ constructor() {
12
+ this.cache = new Map<string, T>();
13
+ }
14
+
15
+ getItem(key: string): T | null {
16
+ return this.cache.get(key) || null;
17
+ }
18
+
19
+ setItem(key: string, value: T): void {
20
+ this.cache.set(key, value);
21
+ }
22
+
23
+ removeItem(key: string): void {
24
+ this.cache.delete(key);
25
+ }
26
+
27
+ getKeys(): string[] {
28
+ const cacheKeys: string[] = [];
29
+ this.cache.forEach((value: T, key: string) => {
30
+ cacheKeys.push(key);
31
+ });
32
+ return cacheKeys;
33
+ }
34
+
35
+ containsKey(key: string): boolean {
36
+ return this.cache.has(key);
37
+ }
38
+
39
+ clear(): void {
40
+ this.cache.clear();
41
+ }
42
+ }