@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,277 +1,277 @@
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
  import { StringUtils, TokenCacheContext, AccountEntity } from '@azure/msal-common';
4
4
  import { Deserializer } from './serializer/Deserializer.esm.js';
5
5
  import { Serializer } from './serializer/Serializer.esm.js';
6
6
 
7
- /*
8
- * Copyright (c) Microsoft Corporation. All rights reserved.
9
- * Licensed under the MIT License.
10
- */
11
- const defaultSerializedCache = {
12
- Account: {},
13
- IdToken: {},
14
- AccessToken: {},
15
- RefreshToken: {},
16
- AppMetadata: {},
17
- };
18
- /**
19
- * In-memory token cache manager
20
- * @public
21
- */
22
- class TokenCache {
23
- constructor(storage, logger, cachePlugin) {
24
- this.cacheHasChanged = false;
25
- this.storage = storage;
26
- this.storage.registerChangeEmitter(this.handleChangeEvent.bind(this));
27
- if (cachePlugin) {
28
- this.persistence = cachePlugin;
29
- }
30
- this.logger = logger;
31
- }
32
- /**
33
- * Set to true if cache state has changed since last time serialize or writeToPersistence was called
34
- */
35
- hasChanged() {
36
- return this.cacheHasChanged;
37
- }
38
- /**
39
- * Serializes in memory cache to JSON
40
- */
41
- serialize() {
42
- this.logger.trace("Serializing in-memory cache");
43
- let finalState = Serializer.serializeAllCache(this.storage.getInMemoryCache());
44
- // if cacheSnapshot not null or empty, merge
45
- if (!StringUtils.isEmpty(this.cacheSnapshot)) {
46
- this.logger.trace("Reading cache snapshot from disk");
47
- finalState = this.mergeState(JSON.parse(this.cacheSnapshot), finalState);
48
- }
49
- else {
50
- this.logger.trace("No cache snapshot to merge");
51
- }
52
- this.cacheHasChanged = false;
53
- return JSON.stringify(finalState);
54
- }
55
- /**
56
- * Deserializes JSON to in-memory cache. JSON should be in MSAL cache schema format
57
- * @param cache - blob formatted cache
58
- */
59
- deserialize(cache) {
60
- this.logger.trace("Deserializing JSON to in-memory cache");
61
- this.cacheSnapshot = cache;
62
- if (!StringUtils.isEmpty(this.cacheSnapshot)) {
63
- this.logger.trace("Reading cache snapshot from disk");
64
- const deserializedCache = Deserializer.deserializeAllCache(this.overlayDefaults(JSON.parse(this.cacheSnapshot)));
65
- this.storage.setInMemoryCache(deserializedCache);
66
- }
67
- else {
68
- this.logger.trace("No cache snapshot to deserialize");
69
- }
70
- }
71
- /**
72
- * Fetches the cache key-value map
73
- */
74
- getKVStore() {
75
- return this.storage.getCache();
76
- }
77
- /**
78
- * API that retrieves all accounts currently in cache to the user
79
- */
80
- async getAllAccounts() {
81
- this.logger.trace("getAllAccounts called");
82
- let cacheContext;
83
- try {
84
- if (this.persistence) {
85
- cacheContext = new TokenCacheContext(this, false);
86
- await this.persistence.beforeCacheAccess(cacheContext);
87
- }
88
- return this.storage.getAllAccounts();
89
- }
90
- finally {
91
- if (this.persistence && cacheContext) {
92
- await this.persistence.afterCacheAccess(cacheContext);
93
- }
94
- }
95
- }
96
- /**
97
- * Returns the signed in account matching homeAccountId.
98
- * (the account object is created at the time of successful login)
99
- * or null when no matching account is found
100
- * @param homeAccountId - unique identifier for an account (uid.utid)
101
- */
102
- async getAccountByHomeId(homeAccountId) {
103
- const allAccounts = await this.getAllAccounts();
104
- if (!StringUtils.isEmpty(homeAccountId) &&
105
- allAccounts &&
106
- allAccounts.length) {
107
- return (allAccounts.filter((accountObj) => accountObj.homeAccountId === homeAccountId)[0] || null);
108
- }
109
- else {
110
- return null;
111
- }
112
- }
113
- /**
114
- * Returns the signed in account matching localAccountId.
115
- * (the account object is created at the time of successful login)
116
- * or null when no matching account is found
117
- * @param localAccountId - unique identifier of an account (sub/obj when homeAccountId cannot be populated)
118
- */
119
- async getAccountByLocalId(localAccountId) {
120
- const allAccounts = await this.getAllAccounts();
121
- if (!StringUtils.isEmpty(localAccountId) &&
122
- allAccounts &&
123
- allAccounts.length) {
124
- return (allAccounts.filter((accountObj) => accountObj.localAccountId === localAccountId)[0] || null);
125
- }
126
- else {
127
- return null;
128
- }
129
- }
130
- /**
131
- * API to remove a specific account and the relevant data from cache
132
- * @param account - AccountInfo passed by the user
133
- */
134
- async removeAccount(account) {
135
- this.logger.trace("removeAccount called");
136
- let cacheContext;
137
- try {
138
- if (this.persistence) {
139
- cacheContext = new TokenCacheContext(this, true);
140
- await this.persistence.beforeCacheAccess(cacheContext);
141
- }
142
- await this.storage.removeAccount(AccountEntity.generateAccountCacheKey(account));
143
- }
144
- finally {
145
- if (this.persistence && cacheContext) {
146
- await this.persistence.afterCacheAccess(cacheContext);
147
- }
148
- }
149
- }
150
- /**
151
- * Called when the cache has changed state.
152
- */
153
- handleChangeEvent() {
154
- this.cacheHasChanged = true;
155
- }
156
- /**
157
- * Merge in memory cache with the cache snapshot.
158
- * @param oldState - cache before changes
159
- * @param currentState - current cache state in the library
160
- */
161
- mergeState(oldState, currentState) {
162
- this.logger.trace("Merging in-memory cache with cache snapshot");
163
- const stateAfterRemoval = this.mergeRemovals(oldState, currentState);
164
- return this.mergeUpdates(stateAfterRemoval, currentState);
165
- }
166
- /**
167
- * Deep update of oldState based on newState values
168
- * @param oldState - cache before changes
169
- * @param newState - updated cache
170
- */
171
- mergeUpdates(oldState, newState) {
172
- Object.keys(newState).forEach((newKey) => {
173
- const newValue = newState[newKey];
174
- // if oldState does not contain value but newValue does, add it
175
- if (!oldState.hasOwnProperty(newKey)) {
176
- if (newValue !== null) {
177
- oldState[newKey] = newValue;
178
- }
179
- }
180
- else {
181
- // both oldState and newState contain the key, do deep update
182
- const newValueNotNull = newValue !== null;
183
- const newValueIsObject = typeof newValue === "object";
184
- const newValueIsNotArray = !Array.isArray(newValue);
185
- const oldStateNotUndefinedOrNull = typeof oldState[newKey] !== "undefined" &&
186
- oldState[newKey] !== null;
187
- if (newValueNotNull &&
188
- newValueIsObject &&
189
- newValueIsNotArray &&
190
- oldStateNotUndefinedOrNull) {
191
- this.mergeUpdates(oldState[newKey], newValue);
192
- }
193
- else {
194
- oldState[newKey] = newValue;
195
- }
196
- }
197
- });
198
- return oldState;
199
- }
200
- /**
201
- * Removes entities in oldState that the were removed from newState. If there are any unknown values in root of
202
- * oldState that are not recognized, they are left untouched.
203
- * @param oldState - cache before changes
204
- * @param newState - updated cache
205
- */
206
- mergeRemovals(oldState, newState) {
207
- this.logger.trace("Remove updated entries in cache");
208
- const accounts = oldState.Account
209
- ? this.mergeRemovalsDict(oldState.Account, newState.Account)
210
- : oldState.Account;
211
- const accessTokens = oldState.AccessToken
212
- ? this.mergeRemovalsDict(oldState.AccessToken, newState.AccessToken)
213
- : oldState.AccessToken;
214
- const refreshTokens = oldState.RefreshToken
215
- ? this.mergeRemovalsDict(oldState.RefreshToken, newState.RefreshToken)
216
- : oldState.RefreshToken;
217
- const idTokens = oldState.IdToken
218
- ? this.mergeRemovalsDict(oldState.IdToken, newState.IdToken)
219
- : oldState.IdToken;
220
- const appMetadata = oldState.AppMetadata
221
- ? this.mergeRemovalsDict(oldState.AppMetadata, newState.AppMetadata)
222
- : oldState.AppMetadata;
223
- return {
224
- ...oldState,
225
- Account: accounts,
226
- AccessToken: accessTokens,
227
- RefreshToken: refreshTokens,
228
- IdToken: idTokens,
229
- AppMetadata: appMetadata,
230
- };
231
- }
232
- /**
233
- * Helper to merge new cache with the old one
234
- * @param oldState - cache before changes
235
- * @param newState - updated cache
236
- */
237
- mergeRemovalsDict(oldState, newState) {
238
- const finalState = { ...oldState };
239
- Object.keys(oldState).forEach((oldKey) => {
240
- if (!newState || !newState.hasOwnProperty(oldKey)) {
241
- delete finalState[oldKey];
242
- }
243
- });
244
- return finalState;
245
- }
246
- /**
247
- * Helper to overlay as a part of cache merge
248
- * @param passedInCache - cache read from the blob
249
- */
250
- overlayDefaults(passedInCache) {
251
- this.logger.trace("Overlaying input cache with the default cache");
252
- return {
253
- Account: {
254
- ...defaultSerializedCache.Account,
255
- ...passedInCache.Account,
256
- },
257
- IdToken: {
258
- ...defaultSerializedCache.IdToken,
259
- ...passedInCache.IdToken,
260
- },
261
- AccessToken: {
262
- ...defaultSerializedCache.AccessToken,
263
- ...passedInCache.AccessToken,
264
- },
265
- RefreshToken: {
266
- ...defaultSerializedCache.RefreshToken,
267
- ...passedInCache.RefreshToken,
268
- },
269
- AppMetadata: {
270
- ...defaultSerializedCache.AppMetadata,
271
- ...passedInCache.AppMetadata,
272
- },
273
- };
274
- }
7
+ /*
8
+ * Copyright (c) Microsoft Corporation. All rights reserved.
9
+ * Licensed under the MIT License.
10
+ */
11
+ const defaultSerializedCache = {
12
+ Account: {},
13
+ IdToken: {},
14
+ AccessToken: {},
15
+ RefreshToken: {},
16
+ AppMetadata: {},
17
+ };
18
+ /**
19
+ * In-memory token cache manager
20
+ * @public
21
+ */
22
+ class TokenCache {
23
+ constructor(storage, logger, cachePlugin) {
24
+ this.cacheHasChanged = false;
25
+ this.storage = storage;
26
+ this.storage.registerChangeEmitter(this.handleChangeEvent.bind(this));
27
+ if (cachePlugin) {
28
+ this.persistence = cachePlugin;
29
+ }
30
+ this.logger = logger;
31
+ }
32
+ /**
33
+ * Set to true if cache state has changed since last time serialize or writeToPersistence was called
34
+ */
35
+ hasChanged() {
36
+ return this.cacheHasChanged;
37
+ }
38
+ /**
39
+ * Serializes in memory cache to JSON
40
+ */
41
+ serialize() {
42
+ this.logger.trace("Serializing in-memory cache");
43
+ let finalState = Serializer.serializeAllCache(this.storage.getInMemoryCache());
44
+ // if cacheSnapshot not null or empty, merge
45
+ if (!StringUtils.isEmpty(this.cacheSnapshot)) {
46
+ this.logger.trace("Reading cache snapshot from disk");
47
+ finalState = this.mergeState(JSON.parse(this.cacheSnapshot), finalState);
48
+ }
49
+ else {
50
+ this.logger.trace("No cache snapshot to merge");
51
+ }
52
+ this.cacheHasChanged = false;
53
+ return JSON.stringify(finalState);
54
+ }
55
+ /**
56
+ * Deserializes JSON to in-memory cache. JSON should be in MSAL cache schema format
57
+ * @param cache - blob formatted cache
58
+ */
59
+ deserialize(cache) {
60
+ this.logger.trace("Deserializing JSON to in-memory cache");
61
+ this.cacheSnapshot = cache;
62
+ if (!StringUtils.isEmpty(this.cacheSnapshot)) {
63
+ this.logger.trace("Reading cache snapshot from disk");
64
+ const deserializedCache = Deserializer.deserializeAllCache(this.overlayDefaults(JSON.parse(this.cacheSnapshot)));
65
+ this.storage.setInMemoryCache(deserializedCache);
66
+ }
67
+ else {
68
+ this.logger.trace("No cache snapshot to deserialize");
69
+ }
70
+ }
71
+ /**
72
+ * Fetches the cache key-value map
73
+ */
74
+ getKVStore() {
75
+ return this.storage.getCache();
76
+ }
77
+ /**
78
+ * API that retrieves all accounts currently in cache to the user
79
+ */
80
+ async getAllAccounts() {
81
+ this.logger.trace("getAllAccounts called");
82
+ let cacheContext;
83
+ try {
84
+ if (this.persistence) {
85
+ cacheContext = new TokenCacheContext(this, false);
86
+ await this.persistence.beforeCacheAccess(cacheContext);
87
+ }
88
+ return this.storage.getAllAccounts();
89
+ }
90
+ finally {
91
+ if (this.persistence && cacheContext) {
92
+ await this.persistence.afterCacheAccess(cacheContext);
93
+ }
94
+ }
95
+ }
96
+ /**
97
+ * Returns the signed in account matching homeAccountId.
98
+ * (the account object is created at the time of successful login)
99
+ * or null when no matching account is found
100
+ * @param homeAccountId - unique identifier for an account (uid.utid)
101
+ */
102
+ async getAccountByHomeId(homeAccountId) {
103
+ const allAccounts = await this.getAllAccounts();
104
+ if (!StringUtils.isEmpty(homeAccountId) &&
105
+ allAccounts &&
106
+ allAccounts.length) {
107
+ return (allAccounts.filter((accountObj) => accountObj.homeAccountId === homeAccountId)[0] || null);
108
+ }
109
+ else {
110
+ return null;
111
+ }
112
+ }
113
+ /**
114
+ * Returns the signed in account matching localAccountId.
115
+ * (the account object is created at the time of successful login)
116
+ * or null when no matching account is found
117
+ * @param localAccountId - unique identifier of an account (sub/obj when homeAccountId cannot be populated)
118
+ */
119
+ async getAccountByLocalId(localAccountId) {
120
+ const allAccounts = await this.getAllAccounts();
121
+ if (!StringUtils.isEmpty(localAccountId) &&
122
+ allAccounts &&
123
+ allAccounts.length) {
124
+ return (allAccounts.filter((accountObj) => accountObj.localAccountId === localAccountId)[0] || null);
125
+ }
126
+ else {
127
+ return null;
128
+ }
129
+ }
130
+ /**
131
+ * API to remove a specific account and the relevant data from cache
132
+ * @param account - AccountInfo passed by the user
133
+ */
134
+ async removeAccount(account) {
135
+ this.logger.trace("removeAccount called");
136
+ let cacheContext;
137
+ try {
138
+ if (this.persistence) {
139
+ cacheContext = new TokenCacheContext(this, true);
140
+ await this.persistence.beforeCacheAccess(cacheContext);
141
+ }
142
+ await this.storage.removeAccount(AccountEntity.generateAccountCacheKey(account));
143
+ }
144
+ finally {
145
+ if (this.persistence && cacheContext) {
146
+ await this.persistence.afterCacheAccess(cacheContext);
147
+ }
148
+ }
149
+ }
150
+ /**
151
+ * Called when the cache has changed state.
152
+ */
153
+ handleChangeEvent() {
154
+ this.cacheHasChanged = true;
155
+ }
156
+ /**
157
+ * Merge in memory cache with the cache snapshot.
158
+ * @param oldState - cache before changes
159
+ * @param currentState - current cache state in the library
160
+ */
161
+ mergeState(oldState, currentState) {
162
+ this.logger.trace("Merging in-memory cache with cache snapshot");
163
+ const stateAfterRemoval = this.mergeRemovals(oldState, currentState);
164
+ return this.mergeUpdates(stateAfterRemoval, currentState);
165
+ }
166
+ /**
167
+ * Deep update of oldState based on newState values
168
+ * @param oldState - cache before changes
169
+ * @param newState - updated cache
170
+ */
171
+ mergeUpdates(oldState, newState) {
172
+ Object.keys(newState).forEach((newKey) => {
173
+ const newValue = newState[newKey];
174
+ // if oldState does not contain value but newValue does, add it
175
+ if (!oldState.hasOwnProperty(newKey)) {
176
+ if (newValue !== null) {
177
+ oldState[newKey] = newValue;
178
+ }
179
+ }
180
+ else {
181
+ // both oldState and newState contain the key, do deep update
182
+ const newValueNotNull = newValue !== null;
183
+ const newValueIsObject = typeof newValue === "object";
184
+ const newValueIsNotArray = !Array.isArray(newValue);
185
+ const oldStateNotUndefinedOrNull = typeof oldState[newKey] !== "undefined" &&
186
+ oldState[newKey] !== null;
187
+ if (newValueNotNull &&
188
+ newValueIsObject &&
189
+ newValueIsNotArray &&
190
+ oldStateNotUndefinedOrNull) {
191
+ this.mergeUpdates(oldState[newKey], newValue);
192
+ }
193
+ else {
194
+ oldState[newKey] = newValue;
195
+ }
196
+ }
197
+ });
198
+ return oldState;
199
+ }
200
+ /**
201
+ * Removes entities in oldState that the were removed from newState. If there are any unknown values in root of
202
+ * oldState that are not recognized, they are left untouched.
203
+ * @param oldState - cache before changes
204
+ * @param newState - updated cache
205
+ */
206
+ mergeRemovals(oldState, newState) {
207
+ this.logger.trace("Remove updated entries in cache");
208
+ const accounts = oldState.Account
209
+ ? this.mergeRemovalsDict(oldState.Account, newState.Account)
210
+ : oldState.Account;
211
+ const accessTokens = oldState.AccessToken
212
+ ? this.mergeRemovalsDict(oldState.AccessToken, newState.AccessToken)
213
+ : oldState.AccessToken;
214
+ const refreshTokens = oldState.RefreshToken
215
+ ? this.mergeRemovalsDict(oldState.RefreshToken, newState.RefreshToken)
216
+ : oldState.RefreshToken;
217
+ const idTokens = oldState.IdToken
218
+ ? this.mergeRemovalsDict(oldState.IdToken, newState.IdToken)
219
+ : oldState.IdToken;
220
+ const appMetadata = oldState.AppMetadata
221
+ ? this.mergeRemovalsDict(oldState.AppMetadata, newState.AppMetadata)
222
+ : oldState.AppMetadata;
223
+ return {
224
+ ...oldState,
225
+ Account: accounts,
226
+ AccessToken: accessTokens,
227
+ RefreshToken: refreshTokens,
228
+ IdToken: idTokens,
229
+ AppMetadata: appMetadata,
230
+ };
231
+ }
232
+ /**
233
+ * Helper to merge new cache with the old one
234
+ * @param oldState - cache before changes
235
+ * @param newState - updated cache
236
+ */
237
+ mergeRemovalsDict(oldState, newState) {
238
+ const finalState = { ...oldState };
239
+ Object.keys(oldState).forEach((oldKey) => {
240
+ if (!newState || !newState.hasOwnProperty(oldKey)) {
241
+ delete finalState[oldKey];
242
+ }
243
+ });
244
+ return finalState;
245
+ }
246
+ /**
247
+ * Helper to overlay as a part of cache merge
248
+ * @param passedInCache - cache read from the blob
249
+ */
250
+ overlayDefaults(passedInCache) {
251
+ this.logger.trace("Overlaying input cache with the default cache");
252
+ return {
253
+ Account: {
254
+ ...defaultSerializedCache.Account,
255
+ ...passedInCache.Account,
256
+ },
257
+ IdToken: {
258
+ ...defaultSerializedCache.IdToken,
259
+ ...passedInCache.IdToken,
260
+ },
261
+ AccessToken: {
262
+ ...defaultSerializedCache.AccessToken,
263
+ ...passedInCache.AccessToken,
264
+ },
265
+ RefreshToken: {
266
+ ...defaultSerializedCache.RefreshToken,
267
+ ...passedInCache.RefreshToken,
268
+ },
269
+ AppMetadata: {
270
+ ...defaultSerializedCache.AppMetadata,
271
+ ...passedInCache.AppMetadata,
272
+ },
273
+ };
274
+ }
275
275
  }
276
276
 
277
277
  export { TokenCache };