@azure/msal-node 2.0.0-alpha.1 → 2.0.0-alpha.2

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