@azure/msal-node 2.7.0-alpha.0 → 2.7.1-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 (245) hide show
  1. package/README.md +1 -1
  2. package/dist/cache/ITokenCache.d.ts +15 -15
  3. package/dist/cache/NodeStorage.cjs +423 -423
  4. package/dist/cache/NodeStorage.cjs.map +1 -1
  5. package/dist/cache/NodeStorage.d.ts +193 -193
  6. package/dist/cache/NodeStorage.d.ts.map +1 -1
  7. package/dist/cache/NodeStorage.mjs +424 -424
  8. package/dist/cache/NodeStorage.mjs.map +1 -1
  9. package/dist/cache/TokenCache.cjs +265 -265
  10. package/dist/cache/TokenCache.d.ts +90 -90
  11. package/dist/cache/TokenCache.mjs +265 -265
  12. package/dist/cache/distributed/DistributedCachePlugin.cjs +26 -26
  13. package/dist/cache/distributed/DistributedCachePlugin.d.ts +10 -10
  14. package/dist/cache/distributed/DistributedCachePlugin.mjs +26 -26
  15. package/dist/cache/distributed/ICacheClient.d.ts +17 -17
  16. package/dist/cache/distributed/IPartitionManager.d.ts +28 -28
  17. package/dist/cache/serializer/Deserializer.cjs +167 -170
  18. package/dist/cache/serializer/Deserializer.cjs.map +1 -1
  19. package/dist/cache/serializer/Deserializer.d.ts +42 -42
  20. package/dist/cache/serializer/Deserializer.d.ts.map +1 -1
  21. package/dist/cache/serializer/Deserializer.mjs +168 -171
  22. package/dist/cache/serializer/Deserializer.mjs.map +1 -1
  23. package/dist/cache/serializer/Serializer.cjs +137 -137
  24. package/dist/cache/serializer/Serializer.d.ts +39 -39
  25. package/dist/cache/serializer/Serializer.mjs +137 -137
  26. package/dist/cache/serializer/SerializerTypes.d.ts +102 -102
  27. package/dist/client/ClientApplication.cjs +335 -333
  28. package/dist/client/ClientApplication.cjs.map +1 -1
  29. package/dist/client/ClientApplication.d.ts +143 -142
  30. package/dist/client/ClientApplication.d.ts.map +1 -1
  31. package/dist/client/ClientApplication.mjs +336 -334
  32. package/dist/client/ClientApplication.mjs.map +1 -1
  33. package/dist/client/ClientAssertion.cjs +116 -116
  34. package/dist/client/ClientAssertion.d.ts +46 -46
  35. package/dist/client/ClientAssertion.mjs +116 -116
  36. package/dist/client/ClientCredentialClient.cjs +194 -194
  37. package/dist/client/ClientCredentialClient.cjs.map +1 -1
  38. package/dist/client/ClientCredentialClient.d.ts +33 -33
  39. package/dist/client/ClientCredentialClient.d.ts.map +1 -1
  40. package/dist/client/ClientCredentialClient.mjs +195 -195
  41. package/dist/client/ClientCredentialClient.mjs.map +1 -1
  42. package/dist/client/ConfidentialClientApplication.cjs +166 -163
  43. package/dist/client/ConfidentialClientApplication.cjs.map +1 -1
  44. package/dist/client/ConfidentialClientApplication.d.ts +58 -58
  45. package/dist/client/ConfidentialClientApplication.d.ts.map +1 -1
  46. package/dist/client/ConfidentialClientApplication.mjs +168 -165
  47. package/dist/client/ConfidentialClientApplication.mjs.map +1 -1
  48. package/dist/client/DeviceCodeClient.cjs +207 -207
  49. package/dist/client/DeviceCodeClient.d.ts +53 -53
  50. package/dist/client/DeviceCodeClient.mjs +207 -207
  51. package/dist/client/IConfidentialClientApplication.d.ts +39 -39
  52. package/dist/client/IPublicClientApplication.d.ts +42 -42
  53. package/dist/client/ManagedIdentityApplication.cjs +75 -80
  54. package/dist/client/ManagedIdentityApplication.cjs.map +1 -1
  55. package/dist/client/ManagedIdentityApplication.d.ts +23 -23
  56. package/dist/client/ManagedIdentityApplication.d.ts.map +1 -1
  57. package/dist/client/ManagedIdentityApplication.mjs +76 -81
  58. package/dist/client/ManagedIdentityApplication.mjs.map +1 -1
  59. package/dist/client/ManagedIdentityClient.cjs +40 -36
  60. package/dist/client/ManagedIdentityClient.cjs.map +1 -1
  61. package/dist/client/ManagedIdentityClient.d.ts +19 -24
  62. package/dist/client/ManagedIdentityClient.d.ts.map +1 -1
  63. package/dist/client/ManagedIdentityClient.mjs +40 -36
  64. package/dist/client/ManagedIdentityClient.mjs.map +1 -1
  65. package/dist/client/ManagedIdentitySources/AppService.cjs +42 -69
  66. package/dist/client/ManagedIdentitySources/AppService.cjs.map +1 -1
  67. package/dist/client/ManagedIdentitySources/AppService.d.ts +16 -16
  68. package/dist/client/ManagedIdentitySources/AppService.d.ts.map +1 -1
  69. package/dist/client/ManagedIdentitySources/AppService.mjs +43 -70
  70. package/dist/client/ManagedIdentitySources/AppService.mjs.map +1 -1
  71. package/dist/client/ManagedIdentitySources/AzureArc.cjs +81 -92
  72. package/dist/client/ManagedIdentitySources/AzureArc.cjs.map +1 -1
  73. package/dist/client/ManagedIdentitySources/AzureArc.d.ts +18 -18
  74. package/dist/client/ManagedIdentitySources/AzureArc.d.ts.map +1 -1
  75. package/dist/client/ManagedIdentitySources/AzureArc.mjs +84 -95
  76. package/dist/client/ManagedIdentitySources/AzureArc.mjs.map +1 -1
  77. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.cjs +116 -77
  78. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.cjs.map +1 -1
  79. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.d.ts +30 -18
  80. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.d.ts.map +1 -1
  81. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.mjs +118 -80
  82. package/dist/client/ManagedIdentitySources/BaseManagedIdentitySource.mjs.map +1 -1
  83. package/dist/client/ManagedIdentitySources/CloudShell.cjs +47 -0
  84. package/dist/client/ManagedIdentitySources/CloudShell.cjs.map +1 -0
  85. package/dist/client/ManagedIdentitySources/CloudShell.d.ts +15 -6
  86. package/dist/client/ManagedIdentitySources/CloudShell.d.ts.map +1 -1
  87. package/dist/client/ManagedIdentitySources/CloudShell.mjs +45 -0
  88. package/dist/client/ManagedIdentitySources/CloudShell.mjs.map +1 -0
  89. package/dist/client/ManagedIdentitySources/Imds.cjs +43 -52
  90. package/dist/client/ManagedIdentitySources/Imds.cjs.map +1 -1
  91. package/dist/client/ManagedIdentitySources/Imds.d.ts +12 -12
  92. package/dist/client/ManagedIdentitySources/Imds.d.ts.map +1 -1
  93. package/dist/client/ManagedIdentitySources/Imds.mjs +44 -53
  94. package/dist/client/ManagedIdentitySources/Imds.mjs.map +1 -1
  95. package/dist/client/ManagedIdentitySources/ServiceFabric.cjs +61 -0
  96. package/dist/client/ManagedIdentitySources/ServiceFabric.cjs.map +1 -0
  97. package/dist/client/ManagedIdentitySources/ServiceFabric.d.ts +16 -6
  98. package/dist/client/ManagedIdentitySources/ServiceFabric.d.ts.map +1 -1
  99. package/dist/client/ManagedIdentitySources/ServiceFabric.mjs +59 -0
  100. package/dist/client/ManagedIdentitySources/ServiceFabric.mjs.map +1 -0
  101. package/dist/client/OnBehalfOfClient.cjs +202 -202
  102. package/dist/client/OnBehalfOfClient.cjs.map +1 -1
  103. package/dist/client/OnBehalfOfClient.d.ts +47 -47
  104. package/dist/client/OnBehalfOfClient.d.ts.map +1 -1
  105. package/dist/client/OnBehalfOfClient.mjs +203 -203
  106. package/dist/client/OnBehalfOfClient.mjs.map +1 -1
  107. package/dist/client/PublicClientApplication.cjs +229 -229
  108. package/dist/client/PublicClientApplication.d.ts +71 -71
  109. package/dist/client/PublicClientApplication.mjs +229 -229
  110. package/dist/client/UsernamePasswordClient.cjs +95 -95
  111. package/dist/client/UsernamePasswordClient.cjs.map +1 -1
  112. package/dist/client/UsernamePasswordClient.d.ts +25 -25
  113. package/dist/client/UsernamePasswordClient.d.ts.map +1 -1
  114. package/dist/client/UsernamePasswordClient.mjs +96 -96
  115. package/dist/client/UsernamePasswordClient.mjs.map +1 -1
  116. package/dist/config/Configuration.cjs +102 -86
  117. package/dist/config/Configuration.cjs.map +1 -1
  118. package/dist/config/Configuration.d.ts +126 -122
  119. package/dist/config/Configuration.d.ts.map +1 -1
  120. package/dist/config/Configuration.mjs +102 -86
  121. package/dist/config/Configuration.mjs.map +1 -1
  122. package/dist/config/ManagedIdentityId.cjs +48 -48
  123. package/dist/config/ManagedIdentityId.d.ts +11 -11
  124. package/dist/config/ManagedIdentityId.mjs +48 -48
  125. package/dist/config/ManagedIdentityRequestParameters.cjs +28 -29
  126. package/dist/config/ManagedIdentityRequestParameters.cjs.map +1 -1
  127. package/dist/config/ManagedIdentityRequestParameters.d.ts +11 -11
  128. package/dist/config/ManagedIdentityRequestParameters.d.ts.map +1 -1
  129. package/dist/config/ManagedIdentityRequestParameters.mjs +28 -29
  130. package/dist/config/ManagedIdentityRequestParameters.mjs.map +1 -1
  131. package/dist/crypto/CryptoProvider.cjs +75 -75
  132. package/dist/crypto/CryptoProvider.d.ts +52 -52
  133. package/dist/crypto/CryptoProvider.mjs +75 -75
  134. package/dist/crypto/GuidGenerator.cjs +22 -22
  135. package/dist/crypto/GuidGenerator.d.ts +14 -14
  136. package/dist/crypto/GuidGenerator.mjs +22 -22
  137. package/dist/crypto/HashUtils.cjs +13 -13
  138. package/dist/crypto/HashUtils.d.ts +8 -8
  139. package/dist/crypto/HashUtils.mjs +13 -13
  140. package/dist/crypto/PkceGenerator.cjs +49 -49
  141. package/dist/crypto/PkceGenerator.d.ts +22 -22
  142. package/dist/crypto/PkceGenerator.mjs +49 -49
  143. package/dist/error/ManagedIdentityError.cjs +38 -28
  144. package/dist/error/ManagedIdentityError.cjs.map +1 -1
  145. package/dist/error/ManagedIdentityError.d.ts +26 -21
  146. package/dist/error/ManagedIdentityError.d.ts.map +1 -1
  147. package/dist/error/ManagedIdentityError.mjs +39 -29
  148. package/dist/error/ManagedIdentityError.mjs.map +1 -1
  149. package/dist/error/ManagedIdentityErrorCodes.cjs +26 -15
  150. package/dist/error/ManagedIdentityErrorCodes.cjs.map +1 -1
  151. package/dist/error/ManagedIdentityErrorCodes.d.ts +18 -9
  152. package/dist/error/ManagedIdentityErrorCodes.d.ts.map +1 -1
  153. package/dist/error/ManagedIdentityErrorCodes.mjs +23 -14
  154. package/dist/error/ManagedIdentityErrorCodes.mjs.map +1 -1
  155. package/dist/error/NodeAuthError.cjs +85 -85
  156. package/dist/error/NodeAuthError.d.ts +65 -65
  157. package/dist/error/NodeAuthError.mjs +85 -85
  158. package/dist/index.cjs +21 -21
  159. package/dist/index.d.ts +44 -44
  160. package/dist/index.d.ts.map +1 -1
  161. package/dist/index.mjs +1 -1
  162. package/dist/internals.cjs +8 -8
  163. package/dist/internals.d.ts +6 -6
  164. package/dist/internals.mjs +8 -8
  165. package/dist/network/HttpClient.cjs +281 -283
  166. package/dist/network/HttpClient.cjs.map +1 -1
  167. package/dist/network/HttpClient.d.ts +25 -26
  168. package/dist/network/HttpClient.d.ts.map +1 -1
  169. package/dist/network/HttpClient.mjs +281 -283
  170. package/dist/network/HttpClient.mjs.map +1 -1
  171. package/dist/network/HttpClientWithRetries.cjs +44 -0
  172. package/dist/network/HttpClientWithRetries.cjs.map +1 -0
  173. package/dist/network/HttpClientWithRetries.d.ts +12 -0
  174. package/dist/network/HttpClientWithRetries.d.ts.map +1 -0
  175. package/dist/network/HttpClientWithRetries.mjs +42 -0
  176. package/dist/network/HttpClientWithRetries.mjs.map +1 -0
  177. package/dist/network/ILoopbackClient.d.ts +10 -10
  178. package/dist/network/LoopbackClient.cjs +77 -77
  179. package/dist/network/LoopbackClient.d.ts +21 -21
  180. package/dist/network/LoopbackClient.mjs +77 -77
  181. package/dist/packageMetadata.cjs +4 -4
  182. package/dist/packageMetadata.d.ts +2 -2
  183. package/dist/packageMetadata.mjs +4 -4
  184. package/dist/request/AuthorizationCodeRequest.d.ts +21 -21
  185. package/dist/request/AuthorizationUrlRequest.d.ts +33 -33
  186. package/dist/request/ClientCredentialRequest.d.ts +14 -14
  187. package/dist/request/ClientCredentialRequest.d.ts.map +1 -1
  188. package/dist/request/DeviceCodeRequest.d.ts +15 -15
  189. package/dist/request/InteractiveRequest.d.ts +22 -22
  190. package/dist/request/ManagedIdentityRequest.d.ts +8 -8
  191. package/dist/request/ManagedIdentityRequestParams.d.ts +9 -9
  192. package/dist/request/OnBehalfOfRequest.d.ts +14 -14
  193. package/dist/request/RefreshTokenRequest.d.ts +17 -17
  194. package/dist/request/SignOutRequest.d.ts +5 -5
  195. package/dist/request/SilentFlowRequest.d.ts +16 -16
  196. package/dist/request/UsernamePasswordRequest.d.ts +19 -19
  197. package/dist/response/ManagedIdentityTokenResponse.d.ts +24 -24
  198. package/dist/retry/IHttpRetryPolicy.d.ts +6 -0
  199. package/dist/retry/IHttpRetryPolicy.d.ts.map +1 -0
  200. package/dist/retry/LinearRetryPolicy.cjs +47 -0
  201. package/dist/retry/LinearRetryPolicy.cjs.map +1 -0
  202. package/dist/retry/LinearRetryPolicy.d.ts +12 -0
  203. package/dist/retry/LinearRetryPolicy.d.ts.map +1 -0
  204. package/dist/retry/LinearRetryPolicy.mjs +45 -0
  205. package/dist/retry/LinearRetryPolicy.mjs.map +1 -0
  206. package/dist/utils/Constants.cjs +142 -103
  207. package/dist/utils/Constants.cjs.map +1 -1
  208. package/dist/utils/Constants.d.ts +132 -106
  209. package/dist/utils/Constants.d.ts.map +1 -1
  210. package/dist/utils/Constants.mjs +134 -100
  211. package/dist/utils/Constants.mjs.map +1 -1
  212. package/dist/utils/EncodingUtils.cjs +44 -44
  213. package/dist/utils/EncodingUtils.d.ts +26 -26
  214. package/dist/utils/EncodingUtils.mjs +44 -44
  215. package/dist/utils/NetworkUtils.cjs +38 -38
  216. package/dist/utils/NetworkUtils.d.ts +5 -5
  217. package/dist/utils/NetworkUtils.mjs +38 -38
  218. package/package.json +5 -5
  219. package/src/cache/NodeStorage.ts +4 -9
  220. package/src/cache/serializer/Deserializer.ts +1 -5
  221. package/src/client/ClientApplication.ts +24 -10
  222. package/src/client/ClientCredentialClient.ts +14 -6
  223. package/src/client/ConfidentialClientApplication.ts +10 -5
  224. package/src/client/ManagedIdentityApplication.ts +8 -16
  225. package/src/client/ManagedIdentityClient.ts +23 -11
  226. package/src/client/ManagedIdentitySources/AppService.ts +68 -97
  227. package/src/client/ManagedIdentitySources/AzureArc.ts +59 -68
  228. package/src/client/ManagedIdentitySources/BaseManagedIdentitySource.ts +82 -11
  229. package/src/client/ManagedIdentitySources/CloudShell.ts +93 -8
  230. package/src/client/ManagedIdentitySources/Imds.ts +54 -40
  231. package/src/client/ManagedIdentitySources/ServiceFabric.ts +126 -5
  232. package/src/client/OnBehalfOfClient.ts +18 -7
  233. package/src/client/UsernamePasswordClient.ts +16 -7
  234. package/src/config/Configuration.ts +50 -18
  235. package/src/config/ManagedIdentityRequestParameters.ts +1 -6
  236. package/src/error/ManagedIdentityError.ts +15 -4
  237. package/src/error/ManagedIdentityErrorCodes.ts +19 -1
  238. package/src/index.ts +1 -0
  239. package/src/network/HttpClient.ts +27 -39
  240. package/src/network/HttpClientWithRetries.ts +80 -0
  241. package/src/packageMetadata.ts +1 -1
  242. package/src/request/ClientCredentialRequest.ts +6 -3
  243. package/src/retry/IHttpRetryPolicy.ts +18 -0
  244. package/src/retry/LinearRetryPolicy.ts +71 -0
  245. package/src/utils/Constants.ts +47 -10
@@ -1,273 +1,273 @@
1
- /*! @azure/msal-node v2.7.0-alpha.0 2023-12-15 */
1
+ /*! @azure/msal-node v2.7.1-alpha.0 2024-05-01 */
2
2
  'use strict';
3
3
  import { TokenCacheContext, AccountEntity } 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
- 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 (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 (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, true);
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 (homeAccountId && allAccounts && allAccounts.length) {
105
- return (allAccounts.filter((accountObj) => accountObj.homeAccountId === homeAccountId)[0] || null);
106
- }
107
- else {
108
- return null;
109
- }
110
- }
111
- /**
112
- * Returns the signed in account matching localAccountId.
113
- * (the account object is created at the time of successful login)
114
- * or null when no matching account is found
115
- * @param localAccountId - unique identifier of an account (sub/obj when homeAccountId cannot be populated)
116
- */
117
- async getAccountByLocalId(localAccountId) {
118
- const allAccounts = await this.getAllAccounts();
119
- if (localAccountId && allAccounts && allAccounts.length) {
120
- return (allAccounts.filter((accountObj) => accountObj.localAccountId === localAccountId)[0] || null);
121
- }
122
- else {
123
- return null;
124
- }
125
- }
126
- /**
127
- * API to remove a specific account and the relevant data from cache
128
- * @param account - AccountInfo passed by the user
129
- */
130
- async removeAccount(account) {
131
- this.logger.trace("removeAccount called");
132
- let cacheContext;
133
- try {
134
- if (this.persistence) {
135
- cacheContext = new TokenCacheContext(this, true);
136
- await this.persistence.beforeCacheAccess(cacheContext);
137
- }
138
- await this.storage.removeAccount(AccountEntity.generateAccountCacheKey(account));
139
- }
140
- finally {
141
- if (this.persistence && cacheContext) {
142
- await this.persistence.afterCacheAccess(cacheContext);
143
- }
144
- }
145
- }
146
- /**
147
- * Called when the cache has changed state.
148
- */
149
- handleChangeEvent() {
150
- this.cacheHasChanged = true;
151
- }
152
- /**
153
- * Merge in memory cache with the cache snapshot.
154
- * @param oldState - cache before changes
155
- * @param currentState - current cache state in the library
156
- */
157
- mergeState(oldState, currentState) {
158
- this.logger.trace("Merging in-memory cache with cache snapshot");
159
- const stateAfterRemoval = this.mergeRemovals(oldState, currentState);
160
- return this.mergeUpdates(stateAfterRemoval, currentState);
161
- }
162
- /**
163
- * Deep update of oldState based on newState values
164
- * @param oldState - cache before changes
165
- * @param newState - updated cache
166
- */
167
- mergeUpdates(oldState, newState) {
168
- Object.keys(newState).forEach((newKey) => {
169
- const newValue = newState[newKey];
170
- // if oldState does not contain value but newValue does, add it
171
- if (!oldState.hasOwnProperty(newKey)) {
172
- if (newValue !== null) {
173
- oldState[newKey] = newValue;
174
- }
175
- }
176
- else {
177
- // both oldState and newState contain the key, do deep update
178
- const newValueNotNull = newValue !== null;
179
- const newValueIsObject = typeof newValue === "object";
180
- const newValueIsNotArray = !Array.isArray(newValue);
181
- const oldStateNotUndefinedOrNull = typeof oldState[newKey] !== "undefined" &&
182
- oldState[newKey] !== null;
183
- if (newValueNotNull &&
184
- newValueIsObject &&
185
- newValueIsNotArray &&
186
- oldStateNotUndefinedOrNull) {
187
- this.mergeUpdates(oldState[newKey], newValue);
188
- }
189
- else {
190
- oldState[newKey] = newValue;
191
- }
192
- }
193
- });
194
- return oldState;
195
- }
196
- /**
197
- * Removes entities in oldState that the were removed from newState. If there are any unknown values in root of
198
- * oldState that are not recognized, they are left untouched.
199
- * @param oldState - cache before changes
200
- * @param newState - updated cache
201
- */
202
- mergeRemovals(oldState, newState) {
203
- this.logger.trace("Remove updated entries in cache");
204
- const accounts = oldState.Account
205
- ? this.mergeRemovalsDict(oldState.Account, newState.Account)
206
- : oldState.Account;
207
- const accessTokens = oldState.AccessToken
208
- ? this.mergeRemovalsDict(oldState.AccessToken, newState.AccessToken)
209
- : oldState.AccessToken;
210
- const refreshTokens = oldState.RefreshToken
211
- ? this.mergeRemovalsDict(oldState.RefreshToken, newState.RefreshToken)
212
- : oldState.RefreshToken;
213
- const idTokens = oldState.IdToken
214
- ? this.mergeRemovalsDict(oldState.IdToken, newState.IdToken)
215
- : oldState.IdToken;
216
- const appMetadata = oldState.AppMetadata
217
- ? this.mergeRemovalsDict(oldState.AppMetadata, newState.AppMetadata)
218
- : oldState.AppMetadata;
219
- return {
220
- ...oldState,
221
- Account: accounts,
222
- AccessToken: accessTokens,
223
- RefreshToken: refreshTokens,
224
- IdToken: idTokens,
225
- AppMetadata: appMetadata,
226
- };
227
- }
228
- /**
229
- * Helper to merge new cache with the old one
230
- * @param oldState - cache before changes
231
- * @param newState - updated cache
232
- */
233
- mergeRemovalsDict(oldState, newState) {
234
- const finalState = { ...oldState };
235
- Object.keys(oldState).forEach((oldKey) => {
236
- if (!newState || !newState.hasOwnProperty(oldKey)) {
237
- delete finalState[oldKey];
238
- }
239
- });
240
- return finalState;
241
- }
242
- /**
243
- * Helper to overlay as a part of cache merge
244
- * @param passedInCache - cache read from the blob
245
- */
246
- overlayDefaults(passedInCache) {
247
- this.logger.trace("Overlaying input cache with the default cache");
248
- return {
249
- Account: {
250
- ...defaultSerializedCache.Account,
251
- ...passedInCache.Account,
252
- },
253
- IdToken: {
254
- ...defaultSerializedCache.IdToken,
255
- ...passedInCache.IdToken,
256
- },
257
- AccessToken: {
258
- ...defaultSerializedCache.AccessToken,
259
- ...passedInCache.AccessToken,
260
- },
261
- RefreshToken: {
262
- ...defaultSerializedCache.RefreshToken,
263
- ...passedInCache.RefreshToken,
264
- },
265
- AppMetadata: {
266
- ...defaultSerializedCache.AppMetadata,
267
- ...passedInCache.AppMetadata,
268
- },
269
- };
270
- }
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 (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 (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, true);
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 (homeAccountId && allAccounts && allAccounts.length) {
105
+ return (allAccounts.filter((accountObj) => accountObj.homeAccountId === homeAccountId)[0] || null);
106
+ }
107
+ else {
108
+ return null;
109
+ }
110
+ }
111
+ /**
112
+ * Returns the signed in account matching localAccountId.
113
+ * (the account object is created at the time of successful login)
114
+ * or null when no matching account is found
115
+ * @param localAccountId - unique identifier of an account (sub/obj when homeAccountId cannot be populated)
116
+ */
117
+ async getAccountByLocalId(localAccountId) {
118
+ const allAccounts = await this.getAllAccounts();
119
+ if (localAccountId && allAccounts && allAccounts.length) {
120
+ return (allAccounts.filter((accountObj) => accountObj.localAccountId === localAccountId)[0] || null);
121
+ }
122
+ else {
123
+ return null;
124
+ }
125
+ }
126
+ /**
127
+ * API to remove a specific account and the relevant data from cache
128
+ * @param account - AccountInfo passed by the user
129
+ */
130
+ async removeAccount(account) {
131
+ this.logger.trace("removeAccount called");
132
+ let cacheContext;
133
+ try {
134
+ if (this.persistence) {
135
+ cacheContext = new TokenCacheContext(this, true);
136
+ await this.persistence.beforeCacheAccess(cacheContext);
137
+ }
138
+ await this.storage.removeAccount(AccountEntity.generateAccountCacheKey(account));
139
+ }
140
+ finally {
141
+ if (this.persistence && cacheContext) {
142
+ await this.persistence.afterCacheAccess(cacheContext);
143
+ }
144
+ }
145
+ }
146
+ /**
147
+ * Called when the cache has changed state.
148
+ */
149
+ handleChangeEvent() {
150
+ this.cacheHasChanged = true;
151
+ }
152
+ /**
153
+ * Merge in memory cache with the cache snapshot.
154
+ * @param oldState - cache before changes
155
+ * @param currentState - current cache state in the library
156
+ */
157
+ mergeState(oldState, currentState) {
158
+ this.logger.trace("Merging in-memory cache with cache snapshot");
159
+ const stateAfterRemoval = this.mergeRemovals(oldState, currentState);
160
+ return this.mergeUpdates(stateAfterRemoval, currentState);
161
+ }
162
+ /**
163
+ * Deep update of oldState based on newState values
164
+ * @param oldState - cache before changes
165
+ * @param newState - updated cache
166
+ */
167
+ mergeUpdates(oldState, newState) {
168
+ Object.keys(newState).forEach((newKey) => {
169
+ const newValue = newState[newKey];
170
+ // if oldState does not contain value but newValue does, add it
171
+ if (!oldState.hasOwnProperty(newKey)) {
172
+ if (newValue !== null) {
173
+ oldState[newKey] = newValue;
174
+ }
175
+ }
176
+ else {
177
+ // both oldState and newState contain the key, do deep update
178
+ const newValueNotNull = newValue !== null;
179
+ const newValueIsObject = typeof newValue === "object";
180
+ const newValueIsNotArray = !Array.isArray(newValue);
181
+ const oldStateNotUndefinedOrNull = typeof oldState[newKey] !== "undefined" &&
182
+ oldState[newKey] !== null;
183
+ if (newValueNotNull &&
184
+ newValueIsObject &&
185
+ newValueIsNotArray &&
186
+ oldStateNotUndefinedOrNull) {
187
+ this.mergeUpdates(oldState[newKey], newValue);
188
+ }
189
+ else {
190
+ oldState[newKey] = newValue;
191
+ }
192
+ }
193
+ });
194
+ return oldState;
195
+ }
196
+ /**
197
+ * Removes entities in oldState that the were removed from newState. If there are any unknown values in root of
198
+ * oldState that are not recognized, they are left untouched.
199
+ * @param oldState - cache before changes
200
+ * @param newState - updated cache
201
+ */
202
+ mergeRemovals(oldState, newState) {
203
+ this.logger.trace("Remove updated entries in cache");
204
+ const accounts = oldState.Account
205
+ ? this.mergeRemovalsDict(oldState.Account, newState.Account)
206
+ : oldState.Account;
207
+ const accessTokens = oldState.AccessToken
208
+ ? this.mergeRemovalsDict(oldState.AccessToken, newState.AccessToken)
209
+ : oldState.AccessToken;
210
+ const refreshTokens = oldState.RefreshToken
211
+ ? this.mergeRemovalsDict(oldState.RefreshToken, newState.RefreshToken)
212
+ : oldState.RefreshToken;
213
+ const idTokens = oldState.IdToken
214
+ ? this.mergeRemovalsDict(oldState.IdToken, newState.IdToken)
215
+ : oldState.IdToken;
216
+ const appMetadata = oldState.AppMetadata
217
+ ? this.mergeRemovalsDict(oldState.AppMetadata, newState.AppMetadata)
218
+ : oldState.AppMetadata;
219
+ return {
220
+ ...oldState,
221
+ Account: accounts,
222
+ AccessToken: accessTokens,
223
+ RefreshToken: refreshTokens,
224
+ IdToken: idTokens,
225
+ AppMetadata: appMetadata,
226
+ };
227
+ }
228
+ /**
229
+ * Helper to merge new cache with the old one
230
+ * @param oldState - cache before changes
231
+ * @param newState - updated cache
232
+ */
233
+ mergeRemovalsDict(oldState, newState) {
234
+ const finalState = { ...oldState };
235
+ Object.keys(oldState).forEach((oldKey) => {
236
+ if (!newState || !newState.hasOwnProperty(oldKey)) {
237
+ delete finalState[oldKey];
238
+ }
239
+ });
240
+ return finalState;
241
+ }
242
+ /**
243
+ * Helper to overlay as a part of cache merge
244
+ * @param passedInCache - cache read from the blob
245
+ */
246
+ overlayDefaults(passedInCache) {
247
+ this.logger.trace("Overlaying input cache with the default cache");
248
+ return {
249
+ Account: {
250
+ ...defaultSerializedCache.Account,
251
+ ...passedInCache.Account,
252
+ },
253
+ IdToken: {
254
+ ...defaultSerializedCache.IdToken,
255
+ ...passedInCache.IdToken,
256
+ },
257
+ AccessToken: {
258
+ ...defaultSerializedCache.AccessToken,
259
+ ...passedInCache.AccessToken,
260
+ },
261
+ RefreshToken: {
262
+ ...defaultSerializedCache.RefreshToken,
263
+ ...passedInCache.RefreshToken,
264
+ },
265
+ AppMetadata: {
266
+ ...defaultSerializedCache.AppMetadata,
267
+ ...passedInCache.AppMetadata,
268
+ },
269
+ };
270
+ }
271
271
  }
272
272
 
273
273
  export { TokenCache };
@@ -1,34 +1,34 @@
1
- /*! @azure/msal-node v2.7.0-alpha.0 2023-12-15 */
1
+ /*! @azure/msal-node v2.7.1-alpha.0 2024-05-01 */
2
2
  'use strict';
3
3
  'use strict';
4
4
 
5
5
  var msalCommon = require('@azure/msal-common');
6
6
 
7
- /*
8
- * Copyright (c) Microsoft Corporation. All rights reserved.
9
- * Licensed under the MIT License.
10
- */
11
- class DistributedCachePlugin {
12
- constructor(client, partitionManager) {
13
- this.client = client;
14
- this.partitionManager = partitionManager;
15
- }
16
- async beforeCacheAccess(cacheContext) {
17
- const partitionKey = await this.partitionManager.getKey();
18
- const cacheData = await this.client.get(partitionKey);
19
- cacheContext.tokenCache.deserialize(cacheData);
20
- }
21
- async afterCacheAccess(cacheContext) {
22
- if (cacheContext.cacheHasChanged) {
23
- const kvStore = cacheContext.tokenCache.getKVStore();
24
- const accountEntities = Object.values(kvStore).filter((value) => msalCommon.AccountEntity.isAccountEntity(value));
25
- if (accountEntities.length > 0) {
26
- const accountEntity = accountEntities[0];
27
- const partitionKey = await this.partitionManager.extractKey(accountEntity);
28
- await this.client.set(partitionKey, cacheContext.tokenCache.serialize());
29
- }
30
- }
31
- }
7
+ /*
8
+ * Copyright (c) Microsoft Corporation. All rights reserved.
9
+ * Licensed under the MIT License.
10
+ */
11
+ class DistributedCachePlugin {
12
+ constructor(client, partitionManager) {
13
+ this.client = client;
14
+ this.partitionManager = partitionManager;
15
+ }
16
+ async beforeCacheAccess(cacheContext) {
17
+ const partitionKey = await this.partitionManager.getKey();
18
+ const cacheData = await this.client.get(partitionKey);
19
+ cacheContext.tokenCache.deserialize(cacheData);
20
+ }
21
+ async afterCacheAccess(cacheContext) {
22
+ if (cacheContext.cacheHasChanged) {
23
+ const kvStore = cacheContext.tokenCache.getKVStore();
24
+ const accountEntities = Object.values(kvStore).filter((value) => msalCommon.AccountEntity.isAccountEntity(value));
25
+ if (accountEntities.length > 0) {
26
+ const accountEntity = accountEntities[0];
27
+ const partitionKey = await this.partitionManager.extractKey(accountEntity);
28
+ await this.client.set(partitionKey, cacheContext.tokenCache.serialize());
29
+ }
30
+ }
31
+ }
32
32
  }
33
33
 
34
34
  exports.DistributedCachePlugin = DistributedCachePlugin;
@@ -1,11 +1,11 @@
1
- import { ICachePlugin, TokenCacheContext } from "@azure/msal-common";
2
- import { IPartitionManager } from "./IPartitionManager.js";
3
- import { ICacheClient } from "./ICacheClient.js";
4
- export declare class DistributedCachePlugin implements ICachePlugin {
5
- private client;
6
- private partitionManager;
7
- constructor(client: ICacheClient, partitionManager: IPartitionManager);
8
- beforeCacheAccess(cacheContext: TokenCacheContext): Promise<void>;
9
- afterCacheAccess(cacheContext: TokenCacheContext): Promise<void>;
10
- }
1
+ import { ICachePlugin, TokenCacheContext } from "@azure/msal-common";
2
+ import { IPartitionManager } from "./IPartitionManager.js";
3
+ import { ICacheClient } from "./ICacheClient.js";
4
+ export declare class DistributedCachePlugin implements ICachePlugin {
5
+ private client;
6
+ private partitionManager;
7
+ constructor(client: ICacheClient, partitionManager: IPartitionManager);
8
+ beforeCacheAccess(cacheContext: TokenCacheContext): Promise<void>;
9
+ afterCacheAccess(cacheContext: TokenCacheContext): Promise<void>;
10
+ }
11
11
  //# sourceMappingURL=DistributedCachePlugin.d.ts.map