@azure/msal-node 2.1.0 → 2.2.0-alpha.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 (134) hide show
  1. package/dist/cache/ITokenCache.d.ts +15 -15
  2. package/dist/cache/NodeStorage.cjs +401 -401
  3. package/dist/cache/NodeStorage.d.ts +182 -182
  4. package/dist/cache/NodeStorage.mjs +401 -401
  5. package/dist/cache/TokenCache.cjs +265 -265
  6. package/dist/cache/TokenCache.d.ts +90 -90
  7. package/dist/cache/TokenCache.mjs +265 -265
  8. package/dist/cache/distributed/DistributedCachePlugin.cjs +26 -26
  9. package/dist/cache/distributed/DistributedCachePlugin.d.ts +10 -10
  10. package/dist/cache/distributed/DistributedCachePlugin.mjs +26 -26
  11. package/dist/cache/distributed/ICacheClient.d.ts +17 -17
  12. package/dist/cache/distributed/IPartitionManager.d.ts +28 -28
  13. package/dist/cache/serializer/Deserializer.cjs +173 -173
  14. package/dist/cache/serializer/Deserializer.d.ts +42 -42
  15. package/dist/cache/serializer/Deserializer.mjs +173 -173
  16. package/dist/cache/serializer/Serializer.cjs +134 -134
  17. package/dist/cache/serializer/Serializer.d.ts +39 -39
  18. package/dist/cache/serializer/Serializer.mjs +134 -134
  19. package/dist/cache/serializer/SerializerTypes.d.ts +101 -101
  20. package/dist/client/ClientApplication.cjs +333 -333
  21. package/dist/client/ClientApplication.cjs.map +1 -1
  22. package/dist/client/ClientApplication.d.ts +142 -142
  23. package/dist/client/ClientApplication.d.ts.map +1 -1
  24. package/dist/client/ClientApplication.mjs +334 -334
  25. package/dist/client/ClientApplication.mjs.map +1 -1
  26. package/dist/client/ClientAssertion.cjs +116 -116
  27. package/dist/client/ClientAssertion.cjs.map +1 -1
  28. package/dist/client/ClientAssertion.d.ts +46 -46
  29. package/dist/client/ClientAssertion.d.ts.map +1 -1
  30. package/dist/client/ClientAssertion.mjs +117 -117
  31. package/dist/client/ClientAssertion.mjs.map +1 -1
  32. package/dist/client/ClientCredentialClient.cjs +189 -190
  33. package/dist/client/ClientCredentialClient.cjs.map +1 -1
  34. package/dist/client/ClientCredentialClient.d.ts +33 -34
  35. package/dist/client/ClientCredentialClient.d.ts.map +1 -1
  36. package/dist/client/ClientCredentialClient.mjs +190 -191
  37. package/dist/client/ClientCredentialClient.mjs.map +1 -1
  38. package/dist/client/ConfidentialClientApplication.cjs +163 -163
  39. package/dist/client/ConfidentialClientApplication.cjs.map +1 -1
  40. package/dist/client/ConfidentialClientApplication.d.ts +58 -58
  41. package/dist/client/ConfidentialClientApplication.d.ts.map +1 -1
  42. package/dist/client/ConfidentialClientApplication.mjs +164 -164
  43. package/dist/client/ConfidentialClientApplication.mjs.map +1 -1
  44. package/dist/client/DeviceCodeClient.cjs +207 -207
  45. package/dist/client/DeviceCodeClient.cjs.map +1 -1
  46. package/dist/client/DeviceCodeClient.d.ts +53 -53
  47. package/dist/client/DeviceCodeClient.d.ts.map +1 -1
  48. package/dist/client/DeviceCodeClient.mjs +208 -208
  49. package/dist/client/DeviceCodeClient.mjs.map +1 -1
  50. package/dist/client/IConfidentialClientApplication.d.ts +39 -39
  51. package/dist/client/IPublicClientApplication.d.ts +42 -42
  52. package/dist/client/OnBehalfOfClient.cjs +202 -202
  53. package/dist/client/OnBehalfOfClient.cjs.map +1 -1
  54. package/dist/client/OnBehalfOfClient.d.ts +47 -47
  55. package/dist/client/OnBehalfOfClient.d.ts.map +1 -1
  56. package/dist/client/OnBehalfOfClient.mjs +203 -203
  57. package/dist/client/OnBehalfOfClient.mjs.map +1 -1
  58. package/dist/client/PublicClientApplication.cjs +229 -182
  59. package/dist/client/PublicClientApplication.cjs.map +1 -1
  60. package/dist/client/PublicClientApplication.d.ts +71 -65
  61. package/dist/client/PublicClientApplication.d.ts.map +1 -1
  62. package/dist/client/PublicClientApplication.mjs +231 -184
  63. package/dist/client/PublicClientApplication.mjs.map +1 -1
  64. package/dist/client/UsernamePasswordClient.cjs +95 -95
  65. package/dist/client/UsernamePasswordClient.d.ts +25 -25
  66. package/dist/client/UsernamePasswordClient.mjs +95 -95
  67. package/dist/config/Configuration.cjs +72 -72
  68. package/dist/config/Configuration.d.ts +107 -107
  69. package/dist/config/Configuration.mjs +72 -72
  70. package/dist/crypto/CryptoProvider.cjs +75 -75
  71. package/dist/crypto/CryptoProvider.d.ts +52 -52
  72. package/dist/crypto/CryptoProvider.mjs +75 -75
  73. package/dist/crypto/GuidGenerator.cjs +22 -22
  74. package/dist/crypto/GuidGenerator.d.ts +14 -14
  75. package/dist/crypto/GuidGenerator.mjs +22 -22
  76. package/dist/crypto/HashUtils.cjs +13 -13
  77. package/dist/crypto/HashUtils.d.ts +8 -8
  78. package/dist/crypto/HashUtils.mjs +13 -13
  79. package/dist/crypto/PkceGenerator.cjs +49 -49
  80. package/dist/crypto/PkceGenerator.d.ts +22 -22
  81. package/dist/crypto/PkceGenerator.mjs +49 -49
  82. package/dist/error/NodeAuthError.cjs +85 -85
  83. package/dist/error/NodeAuthError.d.ts +65 -65
  84. package/dist/error/NodeAuthError.mjs +85 -85
  85. package/dist/index.cjs +17 -1
  86. package/dist/index.cjs.map +1 -1
  87. package/dist/index.d.ts +42 -42
  88. package/dist/index.d.ts.map +1 -1
  89. package/dist/index.mjs +2 -2
  90. package/dist/internals.cjs +8 -8
  91. package/dist/internals.d.ts +6 -6
  92. package/dist/internals.mjs +8 -8
  93. package/dist/network/HttpClient.cjs +276 -276
  94. package/dist/network/HttpClient.d.ts +25 -25
  95. package/dist/network/HttpClient.mjs +276 -276
  96. package/dist/network/ILoopbackClient.d.ts +10 -10
  97. package/dist/network/LoopbackClient.cjs +75 -84
  98. package/dist/network/LoopbackClient.cjs.map +1 -1
  99. package/dist/network/LoopbackClient.d.ts +21 -21
  100. package/dist/network/LoopbackClient.d.ts.map +1 -1
  101. package/dist/network/LoopbackClient.mjs +76 -85
  102. package/dist/network/LoopbackClient.mjs.map +1 -1
  103. package/dist/packageMetadata.cjs +3 -3
  104. package/dist/packageMetadata.d.ts +2 -2
  105. package/dist/packageMetadata.mjs +3 -3
  106. package/dist/request/AuthorizationCodeRequest.d.ts +21 -21
  107. package/dist/request/AuthorizationUrlRequest.d.ts +33 -33
  108. package/dist/request/ClientCredentialRequest.d.ts +14 -14
  109. package/dist/request/DeviceCodeRequest.d.ts +15 -15
  110. package/dist/request/InteractiveRequest.d.ts +22 -22
  111. package/dist/request/OnBehalfOfRequest.d.ts +14 -14
  112. package/dist/request/RefreshTokenRequest.d.ts +17 -17
  113. package/dist/request/SignOutRequest.d.ts +5 -5
  114. package/dist/request/SilentFlowRequest.d.ts +16 -16
  115. package/dist/request/UsernamePasswordRequest.d.ts +19 -19
  116. package/dist/utils/Constants.cjs +80 -80
  117. package/dist/utils/Constants.d.ts +86 -86
  118. package/dist/utils/Constants.mjs +80 -80
  119. package/dist/utils/EncodingUtils.cjs +44 -44
  120. package/dist/utils/EncodingUtils.d.ts +26 -26
  121. package/dist/utils/EncodingUtils.mjs +44 -44
  122. package/dist/utils/NetworkUtils.cjs +38 -38
  123. package/dist/utils/NetworkUtils.d.ts +5 -5
  124. package/dist/utils/NetworkUtils.mjs +38 -38
  125. package/package.json +2 -2
  126. package/src/client/ClientApplication.ts +3 -2
  127. package/src/client/ClientAssertion.ts +7 -2
  128. package/src/client/ClientCredentialClient.ts +32 -34
  129. package/src/client/ConfidentialClientApplication.ts +11 -4
  130. package/src/client/DeviceCodeClient.ts +16 -7
  131. package/src/client/OnBehalfOfClient.ts +11 -4
  132. package/src/client/PublicClientApplication.ts +69 -13
  133. package/src/index.ts +4 -0
  134. package/src/network/LoopbackClient.ts +17 -33
@@ -1,409 +1,409 @@
1
- /*! @azure/msal-node v2.1.0 2023-09-05 */
1
+ /*! @azure/msal-node v2.2.0-alpha.0 2023-09-20 */
2
2
  'use strict';
3
3
  import { CacheManager, AccountEntity, IdTokenEntity, AccessTokenEntity, RefreshTokenEntity, AppMetadataEntity, ServerTelemetryEntity, AuthorityMetadataEntity, ThrottlingEntity } from '@azure/msal-common';
4
4
  import { Deserializer } from './serializer/Deserializer.mjs';
5
5
  import { Serializer } from './serializer/Serializer.mjs';
6
6
 
7
- /*
8
- * Copyright (c) Microsoft Corporation. All rights reserved.
9
- * Licensed under the MIT License.
10
- */
11
- /**
12
- * This class implements Storage for node, reading cache from user specified storage location or an extension library
13
- * @public
14
- */
15
- class NodeStorage extends CacheManager {
16
- constructor(logger, clientId, cryptoImpl) {
17
- super(clientId, cryptoImpl, logger);
18
- this.cache = {};
19
- this.changeEmitters = [];
20
- this.logger = logger;
21
- }
22
- /**
23
- * Queue up callbacks
24
- * @param func - a callback function for cache change indication
25
- */
26
- registerChangeEmitter(func) {
27
- this.changeEmitters.push(func);
28
- }
29
- /**
30
- * Invoke the callback when cache changes
31
- */
32
- emitChange() {
33
- this.changeEmitters.forEach((func) => func.call(null));
34
- }
35
- /**
36
- * Converts cacheKVStore to InMemoryCache
37
- * @param cache - key value store
38
- */
39
- cacheToInMemoryCache(cache) {
40
- const inMemoryCache = {
41
- accounts: {},
42
- idTokens: {},
43
- accessTokens: {},
44
- refreshTokens: {},
45
- appMetadata: {},
46
- };
47
- for (const key in cache) {
48
- if (cache[key] instanceof AccountEntity) {
49
- inMemoryCache.accounts[key] = cache[key];
50
- }
51
- else if (cache[key] instanceof IdTokenEntity) {
52
- inMemoryCache.idTokens[key] = cache[key];
53
- }
54
- else if (cache[key] instanceof AccessTokenEntity) {
55
- inMemoryCache.accessTokens[key] = cache[key];
56
- }
57
- else if (cache[key] instanceof RefreshTokenEntity) {
58
- inMemoryCache.refreshTokens[key] = cache[key];
59
- }
60
- else if (cache[key] instanceof AppMetadataEntity) {
61
- inMemoryCache.appMetadata[key] = cache[key];
62
- }
63
- else {
64
- continue;
65
- }
66
- }
67
- return inMemoryCache;
68
- }
69
- /**
70
- * converts inMemoryCache to CacheKVStore
71
- * @param inMemoryCache - kvstore map for inmemory
72
- */
73
- inMemoryCacheToCache(inMemoryCache) {
74
- // convert in memory cache to a flat Key-Value map
75
- let cache = this.getCache();
76
- cache = {
77
- ...cache,
78
- ...inMemoryCache.accounts,
79
- ...inMemoryCache.idTokens,
80
- ...inMemoryCache.accessTokens,
81
- ...inMemoryCache.refreshTokens,
82
- ...inMemoryCache.appMetadata,
83
- };
84
- // convert in memory cache to a flat Key-Value map
85
- return cache;
86
- }
87
- /**
88
- * gets the current in memory cache for the client
89
- */
90
- getInMemoryCache() {
91
- this.logger.trace("Getting in-memory cache");
92
- // convert the cache key value store to inMemoryCache
93
- const inMemoryCache = this.cacheToInMemoryCache(this.getCache());
94
- return inMemoryCache;
95
- }
96
- /**
97
- * sets the current in memory cache for the client
98
- * @param inMemoryCache - key value map in memory
99
- */
100
- setInMemoryCache(inMemoryCache) {
101
- this.logger.trace("Setting in-memory cache");
102
- // convert and append the inMemoryCache to cacheKVStore
103
- const cache = this.inMemoryCacheToCache(inMemoryCache);
104
- this.setCache(cache);
105
- this.emitChange();
106
- }
107
- /**
108
- * get the current cache key-value store
109
- */
110
- getCache() {
111
- this.logger.trace("Getting cache key-value store");
112
- return this.cache;
113
- }
114
- /**
115
- * sets the current cache (key value store)
116
- * @param cacheMap - key value map
117
- */
118
- setCache(cache) {
119
- this.logger.trace("Setting cache key value store");
120
- this.cache = cache;
121
- // mark change in cache
122
- this.emitChange();
123
- }
124
- /**
125
- * Gets cache item with given key.
126
- * @param key - lookup key for the cache entry
127
- */
128
- getItem(key) {
129
- this.logger.tracePii(`Item key: ${key}`);
130
- // read cache
131
- const cache = this.getCache();
132
- return cache[key];
133
- }
134
- /**
135
- * Gets cache item with given key-value
136
- * @param key - lookup key for the cache entry
137
- * @param value - value of the cache entry
138
- */
139
- setItem(key, value) {
140
- this.logger.tracePii(`Item key: ${key}`);
141
- // read cache
142
- const cache = this.getCache();
143
- cache[key] = value;
144
- // write to cache
145
- this.setCache(cache);
146
- }
147
- getAccountKeys() {
148
- const inMemoryCache = this.getInMemoryCache();
149
- const accountKeys = Object.keys(inMemoryCache.accounts);
150
- return accountKeys;
151
- }
152
- getTokenKeys() {
153
- const inMemoryCache = this.getInMemoryCache();
154
- const tokenKeys = {
155
- idToken: Object.keys(inMemoryCache.idTokens),
156
- accessToken: Object.keys(inMemoryCache.accessTokens),
157
- refreshToken: Object.keys(inMemoryCache.refreshTokens),
158
- };
159
- return tokenKeys;
160
- }
161
- /**
162
- * fetch the account entity
163
- * @param accountKey - lookup key to fetch cache type AccountEntity
164
- */
165
- getAccount(accountKey) {
166
- const account = this.getItem(accountKey);
167
- if (AccountEntity.isAccountEntity(account)) {
168
- return account;
169
- }
170
- return null;
171
- }
172
- /**
173
- * set account entity
174
- * @param account - cache value to be set of type AccountEntity
175
- */
176
- setAccount(account) {
177
- const accountKey = account.generateAccountKey();
178
- this.setItem(accountKey, account);
179
- }
180
- /**
181
- * fetch the idToken credential
182
- * @param idTokenKey - lookup key to fetch cache type IdTokenEntity
183
- */
184
- getIdTokenCredential(idTokenKey) {
185
- const idToken = this.getItem(idTokenKey);
186
- if (IdTokenEntity.isIdTokenEntity(idToken)) {
187
- return idToken;
188
- }
189
- return null;
190
- }
191
- /**
192
- * set idToken credential
193
- * @param idToken - cache value to be set of type IdTokenEntity
194
- */
195
- setIdTokenCredential(idToken) {
196
- const idTokenKey = idToken.generateCredentialKey();
197
- this.setItem(idTokenKey, idToken);
198
- }
199
- /**
200
- * fetch the accessToken credential
201
- * @param accessTokenKey - lookup key to fetch cache type AccessTokenEntity
202
- */
203
- getAccessTokenCredential(accessTokenKey) {
204
- const accessToken = this.getItem(accessTokenKey);
205
- if (AccessTokenEntity.isAccessTokenEntity(accessToken)) {
206
- return accessToken;
207
- }
208
- return null;
209
- }
210
- /**
211
- * set accessToken credential
212
- * @param accessToken - cache value to be set of type AccessTokenEntity
213
- */
214
- setAccessTokenCredential(accessToken) {
215
- const accessTokenKey = accessToken.generateCredentialKey();
216
- this.setItem(accessTokenKey, accessToken);
217
- }
218
- /**
219
- * fetch the refreshToken credential
220
- * @param refreshTokenKey - lookup key to fetch cache type RefreshTokenEntity
221
- */
222
- getRefreshTokenCredential(refreshTokenKey) {
223
- const refreshToken = this.getItem(refreshTokenKey);
224
- if (RefreshTokenEntity.isRefreshTokenEntity(refreshToken)) {
225
- return refreshToken;
226
- }
227
- return null;
228
- }
229
- /**
230
- * set refreshToken credential
231
- * @param refreshToken - cache value to be set of type RefreshTokenEntity
232
- */
233
- setRefreshTokenCredential(refreshToken) {
234
- const refreshTokenKey = refreshToken.generateCredentialKey();
235
- this.setItem(refreshTokenKey, refreshToken);
236
- }
237
- /**
238
- * fetch appMetadata entity from the platform cache
239
- * @param appMetadataKey - lookup key to fetch cache type AppMetadataEntity
240
- */
241
- getAppMetadata(appMetadataKey) {
242
- const appMetadata = this.getItem(appMetadataKey);
243
- if (AppMetadataEntity.isAppMetadataEntity(appMetadataKey, appMetadata)) {
244
- return appMetadata;
245
- }
246
- return null;
247
- }
248
- /**
249
- * set appMetadata entity to the platform cache
250
- * @param appMetadata - cache value to be set of type AppMetadataEntity
251
- */
252
- setAppMetadata(appMetadata) {
253
- const appMetadataKey = appMetadata.generateAppMetadataKey();
254
- this.setItem(appMetadataKey, appMetadata);
255
- }
256
- /**
257
- * fetch server telemetry entity from the platform cache
258
- * @param serverTelemetrykey - lookup key to fetch cache type ServerTelemetryEntity
259
- */
260
- getServerTelemetry(serverTelemetrykey) {
261
- const serverTelemetryEntity = this.getItem(serverTelemetrykey);
262
- if (serverTelemetryEntity &&
263
- ServerTelemetryEntity.isServerTelemetryEntity(serverTelemetrykey, serverTelemetryEntity)) {
264
- return serverTelemetryEntity;
265
- }
266
- return null;
267
- }
268
- /**
269
- * set server telemetry entity to the platform cache
270
- * @param serverTelemetryKey - lookup key to fetch cache type ServerTelemetryEntity
271
- * @param serverTelemetry - cache value to be set of type ServerTelemetryEntity
272
- */
273
- setServerTelemetry(serverTelemetryKey, serverTelemetry) {
274
- this.setItem(serverTelemetryKey, serverTelemetry);
275
- }
276
- /**
277
- * fetch authority metadata entity from the platform cache
278
- * @param key - lookup key to fetch cache type AuthorityMetadataEntity
279
- */
280
- getAuthorityMetadata(key) {
281
- const authorityMetadataEntity = this.getItem(key);
282
- if (authorityMetadataEntity &&
283
- AuthorityMetadataEntity.isAuthorityMetadataEntity(key, authorityMetadataEntity)) {
284
- return authorityMetadataEntity;
285
- }
286
- return null;
287
- }
288
- /**
289
- * Get all authority metadata keys
290
- */
291
- getAuthorityMetadataKeys() {
292
- return this.getKeys().filter((key) => {
293
- return this.isAuthorityMetadata(key);
294
- });
295
- }
296
- /**
297
- * set authority metadata entity to the platform cache
298
- * @param key - lookup key to fetch cache type AuthorityMetadataEntity
299
- * @param metadata - cache value to be set of type AuthorityMetadataEntity
300
- */
301
- setAuthorityMetadata(key, metadata) {
302
- this.setItem(key, metadata);
303
- }
304
- /**
305
- * fetch throttling entity from the platform cache
306
- * @param throttlingCacheKey - lookup key to fetch cache type ThrottlingEntity
307
- */
308
- getThrottlingCache(throttlingCacheKey) {
309
- const throttlingCache = this.getItem(throttlingCacheKey);
310
- if (throttlingCache &&
311
- ThrottlingEntity.isThrottlingEntity(throttlingCacheKey, throttlingCache)) {
312
- return throttlingCache;
313
- }
314
- return null;
315
- }
316
- /**
317
- * set throttling entity to the platform cache
318
- * @param throttlingCacheKey - lookup key to fetch cache type ThrottlingEntity
319
- * @param throttlingCache - cache value to be set of type ThrottlingEntity
320
- */
321
- setThrottlingCache(throttlingCacheKey, throttlingCache) {
322
- this.setItem(throttlingCacheKey, throttlingCache);
323
- }
324
- /**
325
- * Removes the cache item from memory with the given key.
326
- * @param key - lookup key to remove a cache entity
327
- * @param inMemory - key value map of the cache
328
- */
329
- removeItem(key) {
330
- this.logger.tracePii(`Item key: ${key}`);
331
- // read inMemoryCache
332
- let result = false;
333
- const cache = this.getCache();
334
- if (!!cache[key]) {
335
- delete cache[key];
336
- result = true;
337
- }
338
- // write to the cache after removal
339
- if (result) {
340
- this.setCache(cache);
341
- this.emitChange();
342
- }
343
- return result;
344
- }
345
- /**
346
- * Checks whether key is in cache.
347
- * @param key - look up key for a cache entity
348
- */
349
- containsKey(key) {
350
- return this.getKeys().includes(key);
351
- }
352
- /**
353
- * Gets all keys in window.
354
- */
355
- getKeys() {
356
- this.logger.trace("Retrieving all cache keys");
357
- // read cache
358
- const cache = this.getCache();
359
- return [...Object.keys(cache)];
360
- }
361
- /**
362
- * Clears all cache entries created by MSAL (except tokens).
363
- */
364
- async clear() {
365
- this.logger.trace("Clearing cache entries created by MSAL");
366
- // read inMemoryCache
367
- const cacheKeys = this.getKeys();
368
- // delete each element
369
- cacheKeys.forEach((key) => {
370
- this.removeItem(key);
371
- });
372
- this.emitChange();
373
- }
374
- /**
375
- * Initialize in memory cache from an exisiting cache vault
376
- * @param cache - blob formatted cache (JSON)
377
- */
378
- static generateInMemoryCache(cache) {
379
- return Deserializer.deserializeAllCache(Deserializer.deserializeJSONBlob(cache));
380
- }
381
- /**
382
- * retrieves the final JSON
383
- * @param inMemoryCache - itemised cache read from the JSON
384
- */
385
- static generateJsonCache(inMemoryCache) {
386
- return Serializer.serializeAllCache(inMemoryCache);
387
- }
388
- /**
389
- * Updates a credential's cache key if the current cache key is outdated
390
- */
391
- updateCredentialCacheKey(currentCacheKey, credential) {
392
- const updatedCacheKey = credential.generateCredentialKey();
393
- if (currentCacheKey !== updatedCacheKey) {
394
- const cacheItem = this.getItem(currentCacheKey);
395
- if (cacheItem) {
396
- this.removeItem(currentCacheKey);
397
- this.setItem(updatedCacheKey, cacheItem);
398
- this.logger.verbose(`Updated an outdated ${credential.credentialType} cache key`);
399
- return updatedCacheKey;
400
- }
401
- else {
402
- this.logger.error(`Attempted to update an outdated ${credential.credentialType} cache key but no item matching the outdated key was found in storage`);
403
- }
404
- }
405
- return currentCacheKey;
406
- }
7
+ /*
8
+ * Copyright (c) Microsoft Corporation. All rights reserved.
9
+ * Licensed under the MIT License.
10
+ */
11
+ /**
12
+ * This class implements Storage for node, reading cache from user specified storage location or an extension library
13
+ * @public
14
+ */
15
+ class NodeStorage extends CacheManager {
16
+ constructor(logger, clientId, cryptoImpl) {
17
+ super(clientId, cryptoImpl, logger);
18
+ this.cache = {};
19
+ this.changeEmitters = [];
20
+ this.logger = logger;
21
+ }
22
+ /**
23
+ * Queue up callbacks
24
+ * @param func - a callback function for cache change indication
25
+ */
26
+ registerChangeEmitter(func) {
27
+ this.changeEmitters.push(func);
28
+ }
29
+ /**
30
+ * Invoke the callback when cache changes
31
+ */
32
+ emitChange() {
33
+ this.changeEmitters.forEach((func) => func.call(null));
34
+ }
35
+ /**
36
+ * Converts cacheKVStore to InMemoryCache
37
+ * @param cache - key value store
38
+ */
39
+ cacheToInMemoryCache(cache) {
40
+ const inMemoryCache = {
41
+ accounts: {},
42
+ idTokens: {},
43
+ accessTokens: {},
44
+ refreshTokens: {},
45
+ appMetadata: {},
46
+ };
47
+ for (const key in cache) {
48
+ if (cache[key] instanceof AccountEntity) {
49
+ inMemoryCache.accounts[key] = cache[key];
50
+ }
51
+ else if (cache[key] instanceof IdTokenEntity) {
52
+ inMemoryCache.idTokens[key] = cache[key];
53
+ }
54
+ else if (cache[key] instanceof AccessTokenEntity) {
55
+ inMemoryCache.accessTokens[key] = cache[key];
56
+ }
57
+ else if (cache[key] instanceof RefreshTokenEntity) {
58
+ inMemoryCache.refreshTokens[key] = cache[key];
59
+ }
60
+ else if (cache[key] instanceof AppMetadataEntity) {
61
+ inMemoryCache.appMetadata[key] = cache[key];
62
+ }
63
+ else {
64
+ continue;
65
+ }
66
+ }
67
+ return inMemoryCache;
68
+ }
69
+ /**
70
+ * converts inMemoryCache to CacheKVStore
71
+ * @param inMemoryCache - kvstore map for inmemory
72
+ */
73
+ inMemoryCacheToCache(inMemoryCache) {
74
+ // convert in memory cache to a flat Key-Value map
75
+ let cache = this.getCache();
76
+ cache = {
77
+ ...cache,
78
+ ...inMemoryCache.accounts,
79
+ ...inMemoryCache.idTokens,
80
+ ...inMemoryCache.accessTokens,
81
+ ...inMemoryCache.refreshTokens,
82
+ ...inMemoryCache.appMetadata,
83
+ };
84
+ // convert in memory cache to a flat Key-Value map
85
+ return cache;
86
+ }
87
+ /**
88
+ * gets the current in memory cache for the client
89
+ */
90
+ getInMemoryCache() {
91
+ this.logger.trace("Getting in-memory cache");
92
+ // convert the cache key value store to inMemoryCache
93
+ const inMemoryCache = this.cacheToInMemoryCache(this.getCache());
94
+ return inMemoryCache;
95
+ }
96
+ /**
97
+ * sets the current in memory cache for the client
98
+ * @param inMemoryCache - key value map in memory
99
+ */
100
+ setInMemoryCache(inMemoryCache) {
101
+ this.logger.trace("Setting in-memory cache");
102
+ // convert and append the inMemoryCache to cacheKVStore
103
+ const cache = this.inMemoryCacheToCache(inMemoryCache);
104
+ this.setCache(cache);
105
+ this.emitChange();
106
+ }
107
+ /**
108
+ * get the current cache key-value store
109
+ */
110
+ getCache() {
111
+ this.logger.trace("Getting cache key-value store");
112
+ return this.cache;
113
+ }
114
+ /**
115
+ * sets the current cache (key value store)
116
+ * @param cacheMap - key value map
117
+ */
118
+ setCache(cache) {
119
+ this.logger.trace("Setting cache key value store");
120
+ this.cache = cache;
121
+ // mark change in cache
122
+ this.emitChange();
123
+ }
124
+ /**
125
+ * Gets cache item with given key.
126
+ * @param key - lookup key for the cache entry
127
+ */
128
+ getItem(key) {
129
+ this.logger.tracePii(`Item key: ${key}`);
130
+ // read cache
131
+ const cache = this.getCache();
132
+ return cache[key];
133
+ }
134
+ /**
135
+ * Gets cache item with given key-value
136
+ * @param key - lookup key for the cache entry
137
+ * @param value - value of the cache entry
138
+ */
139
+ setItem(key, value) {
140
+ this.logger.tracePii(`Item key: ${key}`);
141
+ // read cache
142
+ const cache = this.getCache();
143
+ cache[key] = value;
144
+ // write to cache
145
+ this.setCache(cache);
146
+ }
147
+ getAccountKeys() {
148
+ const inMemoryCache = this.getInMemoryCache();
149
+ const accountKeys = Object.keys(inMemoryCache.accounts);
150
+ return accountKeys;
151
+ }
152
+ getTokenKeys() {
153
+ const inMemoryCache = this.getInMemoryCache();
154
+ const tokenKeys = {
155
+ idToken: Object.keys(inMemoryCache.idTokens),
156
+ accessToken: Object.keys(inMemoryCache.accessTokens),
157
+ refreshToken: Object.keys(inMemoryCache.refreshTokens),
158
+ };
159
+ return tokenKeys;
160
+ }
161
+ /**
162
+ * fetch the account entity
163
+ * @param accountKey - lookup key to fetch cache type AccountEntity
164
+ */
165
+ getAccount(accountKey) {
166
+ const account = this.getItem(accountKey);
167
+ if (AccountEntity.isAccountEntity(account)) {
168
+ return account;
169
+ }
170
+ return null;
171
+ }
172
+ /**
173
+ * set account entity
174
+ * @param account - cache value to be set of type AccountEntity
175
+ */
176
+ setAccount(account) {
177
+ const accountKey = account.generateAccountKey();
178
+ this.setItem(accountKey, account);
179
+ }
180
+ /**
181
+ * fetch the idToken credential
182
+ * @param idTokenKey - lookup key to fetch cache type IdTokenEntity
183
+ */
184
+ getIdTokenCredential(idTokenKey) {
185
+ const idToken = this.getItem(idTokenKey);
186
+ if (IdTokenEntity.isIdTokenEntity(idToken)) {
187
+ return idToken;
188
+ }
189
+ return null;
190
+ }
191
+ /**
192
+ * set idToken credential
193
+ * @param idToken - cache value to be set of type IdTokenEntity
194
+ */
195
+ setIdTokenCredential(idToken) {
196
+ const idTokenKey = idToken.generateCredentialKey();
197
+ this.setItem(idTokenKey, idToken);
198
+ }
199
+ /**
200
+ * fetch the accessToken credential
201
+ * @param accessTokenKey - lookup key to fetch cache type AccessTokenEntity
202
+ */
203
+ getAccessTokenCredential(accessTokenKey) {
204
+ const accessToken = this.getItem(accessTokenKey);
205
+ if (AccessTokenEntity.isAccessTokenEntity(accessToken)) {
206
+ return accessToken;
207
+ }
208
+ return null;
209
+ }
210
+ /**
211
+ * set accessToken credential
212
+ * @param accessToken - cache value to be set of type AccessTokenEntity
213
+ */
214
+ setAccessTokenCredential(accessToken) {
215
+ const accessTokenKey = accessToken.generateCredentialKey();
216
+ this.setItem(accessTokenKey, accessToken);
217
+ }
218
+ /**
219
+ * fetch the refreshToken credential
220
+ * @param refreshTokenKey - lookup key to fetch cache type RefreshTokenEntity
221
+ */
222
+ getRefreshTokenCredential(refreshTokenKey) {
223
+ const refreshToken = this.getItem(refreshTokenKey);
224
+ if (RefreshTokenEntity.isRefreshTokenEntity(refreshToken)) {
225
+ return refreshToken;
226
+ }
227
+ return null;
228
+ }
229
+ /**
230
+ * set refreshToken credential
231
+ * @param refreshToken - cache value to be set of type RefreshTokenEntity
232
+ */
233
+ setRefreshTokenCredential(refreshToken) {
234
+ const refreshTokenKey = refreshToken.generateCredentialKey();
235
+ this.setItem(refreshTokenKey, refreshToken);
236
+ }
237
+ /**
238
+ * fetch appMetadata entity from the platform cache
239
+ * @param appMetadataKey - lookup key to fetch cache type AppMetadataEntity
240
+ */
241
+ getAppMetadata(appMetadataKey) {
242
+ const appMetadata = this.getItem(appMetadataKey);
243
+ if (AppMetadataEntity.isAppMetadataEntity(appMetadataKey, appMetadata)) {
244
+ return appMetadata;
245
+ }
246
+ return null;
247
+ }
248
+ /**
249
+ * set appMetadata entity to the platform cache
250
+ * @param appMetadata - cache value to be set of type AppMetadataEntity
251
+ */
252
+ setAppMetadata(appMetadata) {
253
+ const appMetadataKey = appMetadata.generateAppMetadataKey();
254
+ this.setItem(appMetadataKey, appMetadata);
255
+ }
256
+ /**
257
+ * fetch server telemetry entity from the platform cache
258
+ * @param serverTelemetrykey - lookup key to fetch cache type ServerTelemetryEntity
259
+ */
260
+ getServerTelemetry(serverTelemetrykey) {
261
+ const serverTelemetryEntity = this.getItem(serverTelemetrykey);
262
+ if (serverTelemetryEntity &&
263
+ ServerTelemetryEntity.isServerTelemetryEntity(serverTelemetrykey, serverTelemetryEntity)) {
264
+ return serverTelemetryEntity;
265
+ }
266
+ return null;
267
+ }
268
+ /**
269
+ * set server telemetry entity to the platform cache
270
+ * @param serverTelemetryKey - lookup key to fetch cache type ServerTelemetryEntity
271
+ * @param serverTelemetry - cache value to be set of type ServerTelemetryEntity
272
+ */
273
+ setServerTelemetry(serverTelemetryKey, serverTelemetry) {
274
+ this.setItem(serverTelemetryKey, serverTelemetry);
275
+ }
276
+ /**
277
+ * fetch authority metadata entity from the platform cache
278
+ * @param key - lookup key to fetch cache type AuthorityMetadataEntity
279
+ */
280
+ getAuthorityMetadata(key) {
281
+ const authorityMetadataEntity = this.getItem(key);
282
+ if (authorityMetadataEntity &&
283
+ AuthorityMetadataEntity.isAuthorityMetadataEntity(key, authorityMetadataEntity)) {
284
+ return authorityMetadataEntity;
285
+ }
286
+ return null;
287
+ }
288
+ /**
289
+ * Get all authority metadata keys
290
+ */
291
+ getAuthorityMetadataKeys() {
292
+ return this.getKeys().filter((key) => {
293
+ return this.isAuthorityMetadata(key);
294
+ });
295
+ }
296
+ /**
297
+ * set authority metadata entity to the platform cache
298
+ * @param key - lookup key to fetch cache type AuthorityMetadataEntity
299
+ * @param metadata - cache value to be set of type AuthorityMetadataEntity
300
+ */
301
+ setAuthorityMetadata(key, metadata) {
302
+ this.setItem(key, metadata);
303
+ }
304
+ /**
305
+ * fetch throttling entity from the platform cache
306
+ * @param throttlingCacheKey - lookup key to fetch cache type ThrottlingEntity
307
+ */
308
+ getThrottlingCache(throttlingCacheKey) {
309
+ const throttlingCache = this.getItem(throttlingCacheKey);
310
+ if (throttlingCache &&
311
+ ThrottlingEntity.isThrottlingEntity(throttlingCacheKey, throttlingCache)) {
312
+ return throttlingCache;
313
+ }
314
+ return null;
315
+ }
316
+ /**
317
+ * set throttling entity to the platform cache
318
+ * @param throttlingCacheKey - lookup key to fetch cache type ThrottlingEntity
319
+ * @param throttlingCache - cache value to be set of type ThrottlingEntity
320
+ */
321
+ setThrottlingCache(throttlingCacheKey, throttlingCache) {
322
+ this.setItem(throttlingCacheKey, throttlingCache);
323
+ }
324
+ /**
325
+ * Removes the cache item from memory with the given key.
326
+ * @param key - lookup key to remove a cache entity
327
+ * @param inMemory - key value map of the cache
328
+ */
329
+ removeItem(key) {
330
+ this.logger.tracePii(`Item key: ${key}`);
331
+ // read inMemoryCache
332
+ let result = false;
333
+ const cache = this.getCache();
334
+ if (!!cache[key]) {
335
+ delete cache[key];
336
+ result = true;
337
+ }
338
+ // write to the cache after removal
339
+ if (result) {
340
+ this.setCache(cache);
341
+ this.emitChange();
342
+ }
343
+ return result;
344
+ }
345
+ /**
346
+ * Checks whether key is in cache.
347
+ * @param key - look up key for a cache entity
348
+ */
349
+ containsKey(key) {
350
+ return this.getKeys().includes(key);
351
+ }
352
+ /**
353
+ * Gets all keys in window.
354
+ */
355
+ getKeys() {
356
+ this.logger.trace("Retrieving all cache keys");
357
+ // read cache
358
+ const cache = this.getCache();
359
+ return [...Object.keys(cache)];
360
+ }
361
+ /**
362
+ * Clears all cache entries created by MSAL (except tokens).
363
+ */
364
+ async clear() {
365
+ this.logger.trace("Clearing cache entries created by MSAL");
366
+ // read inMemoryCache
367
+ const cacheKeys = this.getKeys();
368
+ // delete each element
369
+ cacheKeys.forEach((key) => {
370
+ this.removeItem(key);
371
+ });
372
+ this.emitChange();
373
+ }
374
+ /**
375
+ * Initialize in memory cache from an exisiting cache vault
376
+ * @param cache - blob formatted cache (JSON)
377
+ */
378
+ static generateInMemoryCache(cache) {
379
+ return Deserializer.deserializeAllCache(Deserializer.deserializeJSONBlob(cache));
380
+ }
381
+ /**
382
+ * retrieves the final JSON
383
+ * @param inMemoryCache - itemised cache read from the JSON
384
+ */
385
+ static generateJsonCache(inMemoryCache) {
386
+ return Serializer.serializeAllCache(inMemoryCache);
387
+ }
388
+ /**
389
+ * Updates a credential's cache key if the current cache key is outdated
390
+ */
391
+ updateCredentialCacheKey(currentCacheKey, credential) {
392
+ const updatedCacheKey = credential.generateCredentialKey();
393
+ if (currentCacheKey !== updatedCacheKey) {
394
+ const cacheItem = this.getItem(currentCacheKey);
395
+ if (cacheItem) {
396
+ this.removeItem(currentCacheKey);
397
+ this.setItem(updatedCacheKey, cacheItem);
398
+ this.logger.verbose(`Updated an outdated ${credential.credentialType} cache key`);
399
+ return updatedCacheKey;
400
+ }
401
+ else {
402
+ this.logger.error(`Attempted to update an outdated ${credential.credentialType} cache key but no item matching the outdated key was found in storage`);
403
+ }
404
+ }
405
+ return currentCacheKey;
406
+ }
407
407
  }
408
408
 
409
409
  export { NodeStorage };