@azure/msal-node 2.0.2 → 2.2.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (218) hide show
  1. package/README.md +3 -1
  2. package/dist/cache/ITokenCache.d.ts +15 -15
  3. package/dist/cache/NodeStorage.cjs +401 -401
  4. package/dist/cache/NodeStorage.cjs.map +1 -1
  5. package/dist/cache/NodeStorage.d.ts +182 -182
  6. package/dist/cache/NodeStorage.d.ts.map +1 -1
  7. package/dist/cache/NodeStorage.mjs +401 -401
  8. package/dist/cache/NodeStorage.mjs.map +1 -1
  9. package/dist/cache/TokenCache.cjs +265 -269
  10. package/dist/cache/TokenCache.cjs.map +1 -1
  11. package/dist/cache/TokenCache.d.ts +90 -90
  12. package/dist/cache/TokenCache.d.ts.map +1 -1
  13. package/dist/cache/TokenCache.mjs +266 -270
  14. package/dist/cache/TokenCache.mjs.map +1 -1
  15. package/dist/cache/distributed/DistributedCachePlugin.cjs +26 -26
  16. package/dist/cache/distributed/DistributedCachePlugin.cjs.map +1 -1
  17. package/dist/cache/distributed/DistributedCachePlugin.d.ts +10 -10
  18. package/dist/cache/distributed/DistributedCachePlugin.d.ts.map +1 -1
  19. package/dist/cache/distributed/DistributedCachePlugin.mjs +26 -26
  20. package/dist/cache/distributed/DistributedCachePlugin.mjs.map +1 -1
  21. package/dist/cache/distributed/ICacheClient.d.ts +17 -17
  22. package/dist/cache/distributed/IPartitionManager.d.ts +28 -28
  23. package/dist/cache/serializer/Deserializer.cjs +173 -175
  24. package/dist/cache/serializer/Deserializer.cjs.map +1 -1
  25. package/dist/cache/serializer/Deserializer.d.ts +42 -42
  26. package/dist/cache/serializer/Deserializer.d.ts.map +1 -1
  27. package/dist/cache/serializer/Deserializer.mjs +174 -176
  28. package/dist/cache/serializer/Deserializer.mjs.map +1 -1
  29. package/dist/cache/serializer/Serializer.cjs +134 -134
  30. package/dist/cache/serializer/Serializer.cjs.map +1 -1
  31. package/dist/cache/serializer/Serializer.d.ts +39 -39
  32. package/dist/cache/serializer/Serializer.d.ts.map +1 -1
  33. package/dist/cache/serializer/Serializer.mjs +134 -134
  34. package/dist/cache/serializer/Serializer.mjs.map +1 -1
  35. package/dist/cache/serializer/SerializerTypes.d.ts +101 -101
  36. package/dist/client/ClientApplication.cjs +333 -333
  37. package/dist/client/ClientApplication.cjs.map +1 -1
  38. package/dist/client/ClientApplication.d.ts +142 -142
  39. package/dist/client/ClientApplication.d.ts.map +1 -1
  40. package/dist/client/ClientApplication.mjs +334 -334
  41. package/dist/client/ClientApplication.mjs.map +1 -1
  42. package/dist/client/ClientAssertion.cjs +116 -116
  43. package/dist/client/ClientAssertion.cjs.map +1 -1
  44. package/dist/client/ClientAssertion.d.ts +46 -46
  45. package/dist/client/ClientAssertion.d.ts.map +1 -1
  46. package/dist/client/ClientAssertion.mjs +117 -117
  47. package/dist/client/ClientAssertion.mjs.map +1 -1
  48. package/dist/client/ClientCredentialClient.cjs +189 -168
  49. package/dist/client/ClientCredentialClient.cjs.map +1 -1
  50. package/dist/client/ClientCredentialClient.d.ts +33 -33
  51. package/dist/client/ClientCredentialClient.d.ts.map +1 -1
  52. package/dist/client/ClientCredentialClient.mjs +190 -169
  53. package/dist/client/ClientCredentialClient.mjs.map +1 -1
  54. package/dist/client/ConfidentialClientApplication.cjs +163 -164
  55. package/dist/client/ConfidentialClientApplication.cjs.map +1 -1
  56. package/dist/client/ConfidentialClientApplication.d.ts +58 -58
  57. package/dist/client/ConfidentialClientApplication.d.ts.map +1 -1
  58. package/dist/client/ConfidentialClientApplication.mjs +164 -165
  59. package/dist/client/ConfidentialClientApplication.mjs.map +1 -1
  60. package/dist/client/DeviceCodeClient.cjs +207 -207
  61. package/dist/client/DeviceCodeClient.cjs.map +1 -1
  62. package/dist/client/DeviceCodeClient.d.ts +53 -53
  63. package/dist/client/DeviceCodeClient.d.ts.map +1 -1
  64. package/dist/client/DeviceCodeClient.mjs +208 -208
  65. package/dist/client/DeviceCodeClient.mjs.map +1 -1
  66. package/dist/client/IConfidentialClientApplication.d.ts +39 -39
  67. package/dist/client/IConfidentialClientApplication.d.ts.map +1 -1
  68. package/dist/client/IPublicClientApplication.d.ts +42 -42
  69. package/dist/client/IPublicClientApplication.d.ts.map +1 -1
  70. package/dist/client/OnBehalfOfClient.cjs +203 -204
  71. package/dist/client/OnBehalfOfClient.cjs.map +1 -1
  72. package/dist/client/OnBehalfOfClient.d.ts +47 -47
  73. package/dist/client/OnBehalfOfClient.d.ts.map +1 -1
  74. package/dist/client/OnBehalfOfClient.mjs +204 -205
  75. package/dist/client/OnBehalfOfClient.mjs.map +1 -1
  76. package/dist/client/PublicClientApplication.cjs +229 -175
  77. package/dist/client/PublicClientApplication.cjs.map +1 -1
  78. package/dist/client/PublicClientApplication.d.ts +71 -65
  79. package/dist/client/PublicClientApplication.d.ts.map +1 -1
  80. package/dist/client/PublicClientApplication.mjs +232 -178
  81. package/dist/client/PublicClientApplication.mjs.map +1 -1
  82. package/dist/client/UsernamePasswordClient.cjs +95 -95
  83. package/dist/client/UsernamePasswordClient.cjs.map +1 -1
  84. package/dist/client/UsernamePasswordClient.d.ts +25 -25
  85. package/dist/client/UsernamePasswordClient.d.ts.map +1 -1
  86. package/dist/client/UsernamePasswordClient.mjs +95 -95
  87. package/dist/client/UsernamePasswordClient.mjs.map +1 -1
  88. package/dist/config/Configuration.cjs +72 -72
  89. package/dist/config/Configuration.cjs.map +1 -1
  90. package/dist/config/Configuration.d.ts +107 -107
  91. package/dist/config/Configuration.mjs +72 -72
  92. package/dist/config/Configuration.mjs.map +1 -1
  93. package/dist/crypto/CryptoProvider.cjs +75 -75
  94. package/dist/crypto/CryptoProvider.cjs.map +1 -1
  95. package/dist/crypto/CryptoProvider.d.ts +52 -52
  96. package/dist/crypto/CryptoProvider.mjs +75 -75
  97. package/dist/crypto/CryptoProvider.mjs.map +1 -1
  98. package/dist/crypto/GuidGenerator.cjs +22 -22
  99. package/dist/crypto/GuidGenerator.cjs.map +1 -1
  100. package/dist/crypto/GuidGenerator.d.ts +14 -14
  101. package/dist/crypto/GuidGenerator.mjs +22 -22
  102. package/dist/crypto/GuidGenerator.mjs.map +1 -1
  103. package/dist/crypto/HashUtils.cjs +13 -13
  104. package/dist/crypto/HashUtils.cjs.map +1 -1
  105. package/dist/crypto/HashUtils.d.ts +8 -8
  106. package/dist/crypto/HashUtils.mjs +13 -13
  107. package/dist/crypto/HashUtils.mjs.map +1 -1
  108. package/dist/crypto/PkceGenerator.cjs +49 -49
  109. package/dist/crypto/PkceGenerator.cjs.map +1 -1
  110. package/dist/crypto/PkceGenerator.d.ts +22 -22
  111. package/dist/crypto/PkceGenerator.mjs +49 -49
  112. package/dist/crypto/PkceGenerator.mjs.map +1 -1
  113. package/dist/error/NodeAuthError.cjs +85 -85
  114. package/dist/error/NodeAuthError.cjs.map +1 -1
  115. package/dist/error/NodeAuthError.d.ts +65 -65
  116. package/dist/error/NodeAuthError.mjs +85 -85
  117. package/dist/error/NodeAuthError.mjs.map +1 -1
  118. package/dist/index.cjs +17 -1
  119. package/dist/index.cjs.map +1 -1
  120. package/dist/index.d.ts +42 -42
  121. package/dist/index.d.ts.map +1 -1
  122. package/dist/index.mjs +2 -2
  123. package/dist/internals.cjs +8 -8
  124. package/dist/internals.cjs.map +1 -1
  125. package/dist/internals.d.ts +6 -6
  126. package/dist/internals.d.ts.map +1 -1
  127. package/dist/internals.mjs +8 -8
  128. package/dist/internals.mjs.map +1 -1
  129. package/dist/network/HttpClient.cjs +277 -276
  130. package/dist/network/HttpClient.cjs.map +1 -1
  131. package/dist/network/HttpClient.d.ts +25 -25
  132. package/dist/network/HttpClient.d.ts.map +1 -1
  133. package/dist/network/HttpClient.mjs +278 -277
  134. package/dist/network/HttpClient.mjs.map +1 -1
  135. package/dist/network/ILoopbackClient.d.ts +10 -10
  136. package/dist/network/LoopbackClient.cjs +75 -84
  137. package/dist/network/LoopbackClient.cjs.map +1 -1
  138. package/dist/network/LoopbackClient.d.ts +21 -21
  139. package/dist/network/LoopbackClient.d.ts.map +1 -1
  140. package/dist/network/LoopbackClient.mjs +77 -86
  141. package/dist/network/LoopbackClient.mjs.map +1 -1
  142. package/dist/packageMetadata.cjs +4 -4
  143. package/dist/packageMetadata.cjs.map +1 -1
  144. package/dist/packageMetadata.d.ts +2 -2
  145. package/dist/packageMetadata.mjs +4 -4
  146. package/dist/packageMetadata.mjs.map +1 -1
  147. package/dist/request/AuthorizationCodeRequest.d.ts +21 -21
  148. package/dist/request/AuthorizationUrlRequest.d.ts +33 -33
  149. package/dist/request/ClientCredentialRequest.d.ts +14 -14
  150. package/dist/request/DeviceCodeRequest.d.ts +15 -15
  151. package/dist/request/InteractiveRequest.d.ts +22 -22
  152. package/dist/request/InteractiveRequest.d.ts.map +1 -1
  153. package/dist/request/OnBehalfOfRequest.d.ts +14 -14
  154. package/dist/request/RefreshTokenRequest.d.ts +17 -17
  155. package/dist/request/SignOutRequest.d.ts +5 -5
  156. package/dist/request/SilentFlowRequest.d.ts +16 -16
  157. package/dist/request/UsernamePasswordRequest.d.ts +19 -19
  158. package/dist/utils/Constants.cjs +80 -90
  159. package/dist/utils/Constants.cjs.map +1 -1
  160. package/dist/utils/Constants.d.ts +86 -96
  161. package/dist/utils/Constants.d.ts.map +1 -1
  162. package/dist/utils/Constants.mjs +81 -90
  163. package/dist/utils/Constants.mjs.map +1 -1
  164. package/dist/utils/EncodingUtils.cjs +44 -44
  165. package/dist/utils/EncodingUtils.cjs.map +1 -1
  166. package/dist/utils/EncodingUtils.d.ts +26 -26
  167. package/dist/utils/EncodingUtils.mjs +44 -44
  168. package/dist/utils/EncodingUtils.mjs.map +1 -1
  169. package/dist/utils/NetworkUtils.cjs +38 -38
  170. package/dist/utils/NetworkUtils.cjs.map +1 -1
  171. package/dist/utils/NetworkUtils.d.ts +5 -5
  172. package/dist/utils/NetworkUtils.mjs +38 -38
  173. package/dist/utils/NetworkUtils.mjs.map +1 -1
  174. package/package.json +8 -5
  175. package/src/cache/ITokenCache.ts +24 -0
  176. package/src/cache/NodeStorage.ts +533 -0
  177. package/src/cache/TokenCache.ts +358 -0
  178. package/src/cache/distributed/DistributedCachePlugin.ts +56 -0
  179. package/src/cache/distributed/ICacheClient.ts +23 -0
  180. package/src/cache/distributed/IPartitionManager.ts +35 -0
  181. package/src/cache/serializer/Deserializer.ts +218 -0
  182. package/src/cache/serializer/Serializer.ts +173 -0
  183. package/src/cache/serializer/SerializerTypes.ts +121 -0
  184. package/src/client/ClientApplication.ts +596 -0
  185. package/src/client/ClientAssertion.ts +165 -0
  186. package/src/client/ClientCredentialClient.ts +346 -0
  187. package/src/client/ConfidentialClientApplication.ts +268 -0
  188. package/src/client/DeviceCodeClient.ts +369 -0
  189. package/src/client/IConfidentialClientApplication.ts +72 -0
  190. package/src/client/IPublicClientApplication.ts +72 -0
  191. package/src/client/OnBehalfOfClient.ts +368 -0
  192. package/src/client/PublicClientApplication.ts +339 -0
  193. package/src/client/UsernamePasswordClient.ts +180 -0
  194. package/src/config/Configuration.ts +201 -0
  195. package/src/crypto/CryptoProvider.ts +98 -0
  196. package/src/crypto/GuidGenerator.ts +28 -0
  197. package/src/crypto/HashUtils.ts +17 -0
  198. package/src/crypto/PkceGenerator.ts +63 -0
  199. package/src/error/NodeAuthError.ts +117 -0
  200. package/src/index.ts +125 -0
  201. package/src/internals.ts +12 -0
  202. package/src/network/HttpClient.ts +425 -0
  203. package/src/network/ILoopbackClient.ts +19 -0
  204. package/src/network/LoopbackClient.ts +110 -0
  205. package/src/packageMetadata.ts +3 -0
  206. package/src/request/AuthorizationCodeRequest.ts +39 -0
  207. package/src/request/AuthorizationUrlRequest.ts +50 -0
  208. package/src/request/ClientCredentialRequest.ts +29 -0
  209. package/src/request/DeviceCodeRequest.ts +34 -0
  210. package/src/request/InteractiveRequest.ts +39 -0
  211. package/src/request/OnBehalfOfRequest.ts +30 -0
  212. package/src/request/RefreshTokenRequest.ts +34 -0
  213. package/src/request/SignOutRequest.ts +11 -0
  214. package/src/request/SilentFlowRequest.ts +32 -0
  215. package/src/request/UsernamePasswordRequest.ts +36 -0
  216. package/src/utils/Constants.ts +103 -0
  217. package/src/utils/EncodingUtils.ts +50 -0
  218. package/src/utils/NetworkUtils.ts +49 -0
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
+ "$schema": "https://json.schemastore.org/package.json",
2
3
  "name": "@azure/msal-node",
3
- "version": "2.0.2",
4
+ "version": "2.2.0-alpha.0",
4
5
  "author": {
5
6
  "name": "Microsoft",
6
7
  "email": "nugetaad@microsoft.com",
@@ -34,7 +35,8 @@
34
35
  "./package.json": "./package.json"
35
36
  },
36
37
  "files": [
37
- "dist"
38
+ "dist",
39
+ "src"
38
40
  ],
39
41
  "scripts": {
40
42
  "build": "npm run clean && rollup -c --strictDeprecations --bundleConfigAsCjs",
@@ -43,8 +45,8 @@
43
45
  "test": "jest",
44
46
  "test:watch": "jest --watch",
45
47
  "test:coverage": "jest --coverage",
46
- "lint": "cd ../../ && npm run lint:node",
47
- "lint:fix": "npm run lint -- -- --fix",
48
+ "lint": "eslint src --ext .ts",
49
+ "lint:fix": "npm run lint -- --fix",
48
50
  "build:all": "cd ../.. && npm run build --workspace=@azure/msal-common --workspace=@azure/msal-node",
49
51
  "prepack": "npm run build:all",
50
52
  "format:check": "npx prettier --ignore-path .gitignore --check src test",
@@ -64,6 +66,7 @@
64
66
  "@types/node": "^20.3.1",
65
67
  "@types/sinon": "^7.5.0",
66
68
  "@types/uuid": "^7.0.0",
69
+ "eslint-config-msal": "^0.0.0",
67
70
  "jest": "^29.5.0",
68
71
  "prettier": "2.8.7",
69
72
  "rollup": "^3.20.1",
@@ -74,7 +77,7 @@
74
77
  "yargs": "^17.3.1"
75
78
  },
76
79
  "dependencies": {
77
- "@azure/msal-common": "14.0.2",
80
+ "@azure/msal-common": "14.1.0-alpha.0",
78
81
  "jsonwebtoken": "^9.0.0",
79
82
  "uuid": "^8.3.0"
80
83
  },
@@ -0,0 +1,24 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { AccountInfo } from "@azure/msal-common";
7
+
8
+ /**
9
+ * Token cache interface for the client, giving access to cache APIs
10
+ * @public
11
+ */
12
+ export interface ITokenCache {
13
+ /** API that retrieves all accounts currently in cache to the user */
14
+ getAllAccounts(): Promise<AccountInfo[]>;
15
+
16
+ /** Returns the signed in account matching homeAccountId */
17
+ getAccountByHomeId(homeAccountId: string): Promise<AccountInfo | null>;
18
+
19
+ /** Returns the signed in account matching localAccountId */
20
+ getAccountByLocalId(localAccountId: string): Promise<AccountInfo | null>;
21
+
22
+ /** API to remove a specific account and the relevant data from cache */
23
+ removeAccount(account: AccountInfo): Promise<void>;
24
+ }
@@ -0,0 +1,533 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import {
7
+ TokenKeys,
8
+ AccountEntity,
9
+ IdTokenEntity,
10
+ AccessTokenEntity,
11
+ RefreshTokenEntity,
12
+ AppMetadataEntity,
13
+ ServerTelemetryEntity,
14
+ ThrottlingEntity,
15
+ CacheManager,
16
+ Logger,
17
+ ValidCacheType,
18
+ ICrypto,
19
+ AuthorityMetadataEntity,
20
+ ValidCredentialType,
21
+ } from "@azure/msal-common";
22
+
23
+ import { Deserializer } from "./serializer/Deserializer.js";
24
+ import { Serializer } from "./serializer/Serializer.js";
25
+ import {
26
+ InMemoryCache,
27
+ JsonCache,
28
+ CacheKVStore,
29
+ } from "./serializer/SerializerTypes.js";
30
+
31
+ /**
32
+ * This class implements Storage for node, reading cache from user specified storage location or an extension library
33
+ * @public
34
+ */
35
+ export class NodeStorage extends CacheManager {
36
+ // Cache configuration, either set by user or default values.
37
+ private logger: Logger;
38
+ private cache: CacheKVStore = {};
39
+ private changeEmitters: Array<Function> = [];
40
+
41
+ constructor(logger: Logger, clientId: string, cryptoImpl: ICrypto) {
42
+ super(clientId, cryptoImpl, logger);
43
+ this.logger = logger;
44
+ }
45
+
46
+ /**
47
+ * Queue up callbacks
48
+ * @param func - a callback function for cache change indication
49
+ */
50
+ registerChangeEmitter(func: () => void): void {
51
+ this.changeEmitters.push(func);
52
+ }
53
+
54
+ /**
55
+ * Invoke the callback when cache changes
56
+ */
57
+ emitChange(): void {
58
+ this.changeEmitters.forEach((func) => func.call(null));
59
+ }
60
+
61
+ /**
62
+ * Converts cacheKVStore to InMemoryCache
63
+ * @param cache - key value store
64
+ */
65
+ cacheToInMemoryCache(cache: CacheKVStore): InMemoryCache {
66
+ const inMemoryCache: InMemoryCache = {
67
+ accounts: {},
68
+ idTokens: {},
69
+ accessTokens: {},
70
+ refreshTokens: {},
71
+ appMetadata: {},
72
+ };
73
+
74
+ for (const key in cache) {
75
+ if (cache[key as string] instanceof AccountEntity) {
76
+ inMemoryCache.accounts[key] = cache[key] as AccountEntity;
77
+ } else if (cache[key] instanceof IdTokenEntity) {
78
+ inMemoryCache.idTokens[key] = cache[key] as IdTokenEntity;
79
+ } else if (cache[key] instanceof AccessTokenEntity) {
80
+ inMemoryCache.accessTokens[key] = cache[
81
+ key
82
+ ] as AccessTokenEntity;
83
+ } else if (cache[key] instanceof RefreshTokenEntity) {
84
+ inMemoryCache.refreshTokens[key] = cache[
85
+ key
86
+ ] as RefreshTokenEntity;
87
+ } else if (cache[key] instanceof AppMetadataEntity) {
88
+ inMemoryCache.appMetadata[key] = cache[
89
+ key
90
+ ] as AppMetadataEntity;
91
+ } else {
92
+ continue;
93
+ }
94
+ }
95
+
96
+ return inMemoryCache;
97
+ }
98
+
99
+ /**
100
+ * converts inMemoryCache to CacheKVStore
101
+ * @param inMemoryCache - kvstore map for inmemory
102
+ */
103
+ inMemoryCacheToCache(inMemoryCache: InMemoryCache): CacheKVStore {
104
+ // convert in memory cache to a flat Key-Value map
105
+ let cache = this.getCache();
106
+
107
+ cache = {
108
+ ...cache,
109
+ ...inMemoryCache.accounts,
110
+ ...inMemoryCache.idTokens,
111
+ ...inMemoryCache.accessTokens,
112
+ ...inMemoryCache.refreshTokens,
113
+ ...inMemoryCache.appMetadata,
114
+ };
115
+
116
+ // convert in memory cache to a flat Key-Value map
117
+ return cache;
118
+ }
119
+
120
+ /**
121
+ * gets the current in memory cache for the client
122
+ */
123
+ getInMemoryCache(): InMemoryCache {
124
+ this.logger.trace("Getting in-memory cache");
125
+
126
+ // convert the cache key value store to inMemoryCache
127
+ const inMemoryCache = this.cacheToInMemoryCache(this.getCache());
128
+ return inMemoryCache;
129
+ }
130
+
131
+ /**
132
+ * sets the current in memory cache for the client
133
+ * @param inMemoryCache - key value map in memory
134
+ */
135
+ setInMemoryCache(inMemoryCache: InMemoryCache): void {
136
+ this.logger.trace("Setting in-memory cache");
137
+
138
+ // convert and append the inMemoryCache to cacheKVStore
139
+ const cache = this.inMemoryCacheToCache(inMemoryCache);
140
+ this.setCache(cache);
141
+
142
+ this.emitChange();
143
+ }
144
+
145
+ /**
146
+ * get the current cache key-value store
147
+ */
148
+ getCache(): CacheKVStore {
149
+ this.logger.trace("Getting cache key-value store");
150
+ return this.cache;
151
+ }
152
+
153
+ /**
154
+ * sets the current cache (key value store)
155
+ * @param cacheMap - key value map
156
+ */
157
+ setCache(cache: CacheKVStore): void {
158
+ this.logger.trace("Setting cache key value store");
159
+ this.cache = cache;
160
+
161
+ // mark change in cache
162
+ this.emitChange();
163
+ }
164
+
165
+ /**
166
+ * Gets cache item with given key.
167
+ * @param key - lookup key for the cache entry
168
+ */
169
+ getItem(key: string): ValidCacheType {
170
+ this.logger.tracePii(`Item key: ${key}`);
171
+
172
+ // read cache
173
+ const cache = this.getCache();
174
+ return cache[key];
175
+ }
176
+
177
+ /**
178
+ * Gets cache item with given key-value
179
+ * @param key - lookup key for the cache entry
180
+ * @param value - value of the cache entry
181
+ */
182
+ setItem(key: string, value: ValidCacheType): void {
183
+ this.logger.tracePii(`Item key: ${key}`);
184
+
185
+ // read cache
186
+ const cache = this.getCache();
187
+ cache[key] = value;
188
+
189
+ // write to cache
190
+ this.setCache(cache);
191
+ }
192
+
193
+ getAccountKeys(): string[] {
194
+ const inMemoryCache = this.getInMemoryCache();
195
+ const accountKeys = Object.keys(inMemoryCache.accounts);
196
+
197
+ return accountKeys;
198
+ }
199
+
200
+ getTokenKeys(): TokenKeys {
201
+ const inMemoryCache = this.getInMemoryCache();
202
+ const tokenKeys = {
203
+ idToken: Object.keys(inMemoryCache.idTokens),
204
+ accessToken: Object.keys(inMemoryCache.accessTokens),
205
+ refreshToken: Object.keys(inMemoryCache.refreshTokens),
206
+ };
207
+
208
+ return tokenKeys;
209
+ }
210
+
211
+ /**
212
+ * fetch the account entity
213
+ * @param accountKey - lookup key to fetch cache type AccountEntity
214
+ */
215
+ getAccount(accountKey: string): AccountEntity | null {
216
+ const account = this.getItem(accountKey) as AccountEntity;
217
+ if (AccountEntity.isAccountEntity(account)) {
218
+ return account;
219
+ }
220
+ return null;
221
+ }
222
+
223
+ /**
224
+ * set account entity
225
+ * @param account - cache value to be set of type AccountEntity
226
+ */
227
+ setAccount(account: AccountEntity): void {
228
+ const accountKey = account.generateAccountKey();
229
+ this.setItem(accountKey, account);
230
+ }
231
+
232
+ /**
233
+ * fetch the idToken credential
234
+ * @param idTokenKey - lookup key to fetch cache type IdTokenEntity
235
+ */
236
+ getIdTokenCredential(idTokenKey: string): IdTokenEntity | null {
237
+ const idToken = this.getItem(idTokenKey) as IdTokenEntity;
238
+ if (IdTokenEntity.isIdTokenEntity(idToken)) {
239
+ return idToken;
240
+ }
241
+ return null;
242
+ }
243
+
244
+ /**
245
+ * set idToken credential
246
+ * @param idToken - cache value to be set of type IdTokenEntity
247
+ */
248
+ setIdTokenCredential(idToken: IdTokenEntity): void {
249
+ const idTokenKey = idToken.generateCredentialKey();
250
+ this.setItem(idTokenKey, idToken);
251
+ }
252
+
253
+ /**
254
+ * fetch the accessToken credential
255
+ * @param accessTokenKey - lookup key to fetch cache type AccessTokenEntity
256
+ */
257
+ getAccessTokenCredential(accessTokenKey: string): AccessTokenEntity | null {
258
+ const accessToken = this.getItem(accessTokenKey) as AccessTokenEntity;
259
+ if (AccessTokenEntity.isAccessTokenEntity(accessToken)) {
260
+ return accessToken;
261
+ }
262
+ return null;
263
+ }
264
+
265
+ /**
266
+ * set accessToken credential
267
+ * @param accessToken - cache value to be set of type AccessTokenEntity
268
+ */
269
+ setAccessTokenCredential(accessToken: AccessTokenEntity): void {
270
+ const accessTokenKey = accessToken.generateCredentialKey();
271
+ this.setItem(accessTokenKey, accessToken);
272
+ }
273
+
274
+ /**
275
+ * fetch the refreshToken credential
276
+ * @param refreshTokenKey - lookup key to fetch cache type RefreshTokenEntity
277
+ */
278
+ getRefreshTokenCredential(
279
+ refreshTokenKey: string
280
+ ): RefreshTokenEntity | null {
281
+ const refreshToken = this.getItem(
282
+ refreshTokenKey
283
+ ) as RefreshTokenEntity;
284
+ if (RefreshTokenEntity.isRefreshTokenEntity(refreshToken)) {
285
+ return refreshToken as RefreshTokenEntity;
286
+ }
287
+ return null;
288
+ }
289
+
290
+ /**
291
+ * set refreshToken credential
292
+ * @param refreshToken - cache value to be set of type RefreshTokenEntity
293
+ */
294
+ setRefreshTokenCredential(refreshToken: RefreshTokenEntity): void {
295
+ const refreshTokenKey = refreshToken.generateCredentialKey();
296
+ this.setItem(refreshTokenKey, refreshToken);
297
+ }
298
+
299
+ /**
300
+ * fetch appMetadata entity from the platform cache
301
+ * @param appMetadataKey - lookup key to fetch cache type AppMetadataEntity
302
+ */
303
+ getAppMetadata(appMetadataKey: string): AppMetadataEntity | null {
304
+ const appMetadata: AppMetadataEntity = this.getItem(
305
+ appMetadataKey
306
+ ) as AppMetadataEntity;
307
+ if (
308
+ AppMetadataEntity.isAppMetadataEntity(appMetadataKey, appMetadata)
309
+ ) {
310
+ return appMetadata;
311
+ }
312
+ return null;
313
+ }
314
+
315
+ /**
316
+ * set appMetadata entity to the platform cache
317
+ * @param appMetadata - cache value to be set of type AppMetadataEntity
318
+ */
319
+ setAppMetadata(appMetadata: AppMetadataEntity): void {
320
+ const appMetadataKey = appMetadata.generateAppMetadataKey();
321
+ this.setItem(appMetadataKey, appMetadata);
322
+ }
323
+
324
+ /**
325
+ * fetch server telemetry entity from the platform cache
326
+ * @param serverTelemetrykey - lookup key to fetch cache type ServerTelemetryEntity
327
+ */
328
+ getServerTelemetry(
329
+ serverTelemetrykey: string
330
+ ): ServerTelemetryEntity | null {
331
+ const serverTelemetryEntity: ServerTelemetryEntity = this.getItem(
332
+ serverTelemetrykey
333
+ ) as ServerTelemetryEntity;
334
+ if (
335
+ serverTelemetryEntity &&
336
+ ServerTelemetryEntity.isServerTelemetryEntity(
337
+ serverTelemetrykey,
338
+ serverTelemetryEntity
339
+ )
340
+ ) {
341
+ return serverTelemetryEntity;
342
+ }
343
+ return null;
344
+ }
345
+
346
+ /**
347
+ * set server telemetry entity to the platform cache
348
+ * @param serverTelemetryKey - lookup key to fetch cache type ServerTelemetryEntity
349
+ * @param serverTelemetry - cache value to be set of type ServerTelemetryEntity
350
+ */
351
+ setServerTelemetry(
352
+ serverTelemetryKey: string,
353
+ serverTelemetry: ServerTelemetryEntity
354
+ ): void {
355
+ this.setItem(serverTelemetryKey, serverTelemetry);
356
+ }
357
+
358
+ /**
359
+ * fetch authority metadata entity from the platform cache
360
+ * @param key - lookup key to fetch cache type AuthorityMetadataEntity
361
+ */
362
+ getAuthorityMetadata(key: string): AuthorityMetadataEntity | null {
363
+ const authorityMetadataEntity: AuthorityMetadataEntity = this.getItem(
364
+ key
365
+ ) as AuthorityMetadataEntity;
366
+ if (
367
+ authorityMetadataEntity &&
368
+ AuthorityMetadataEntity.isAuthorityMetadataEntity(
369
+ key,
370
+ authorityMetadataEntity
371
+ )
372
+ ) {
373
+ return authorityMetadataEntity;
374
+ }
375
+ return null;
376
+ }
377
+
378
+ /**
379
+ * Get all authority metadata keys
380
+ */
381
+ getAuthorityMetadataKeys(): Array<string> {
382
+ return this.getKeys().filter((key) => {
383
+ return this.isAuthorityMetadata(key);
384
+ });
385
+ }
386
+
387
+ /**
388
+ * set authority metadata entity to the platform cache
389
+ * @param key - lookup key to fetch cache type AuthorityMetadataEntity
390
+ * @param metadata - cache value to be set of type AuthorityMetadataEntity
391
+ */
392
+ setAuthorityMetadata(key: string, metadata: AuthorityMetadataEntity): void {
393
+ this.setItem(key, metadata);
394
+ }
395
+
396
+ /**
397
+ * fetch throttling entity from the platform cache
398
+ * @param throttlingCacheKey - lookup key to fetch cache type ThrottlingEntity
399
+ */
400
+ getThrottlingCache(throttlingCacheKey: string): ThrottlingEntity | null {
401
+ const throttlingCache: ThrottlingEntity = this.getItem(
402
+ throttlingCacheKey
403
+ ) as ThrottlingEntity;
404
+ if (
405
+ throttlingCache &&
406
+ ThrottlingEntity.isThrottlingEntity(
407
+ throttlingCacheKey,
408
+ throttlingCache
409
+ )
410
+ ) {
411
+ return throttlingCache;
412
+ }
413
+ return null;
414
+ }
415
+
416
+ /**
417
+ * set throttling entity to the platform cache
418
+ * @param throttlingCacheKey - lookup key to fetch cache type ThrottlingEntity
419
+ * @param throttlingCache - cache value to be set of type ThrottlingEntity
420
+ */
421
+ setThrottlingCache(
422
+ throttlingCacheKey: string,
423
+ throttlingCache: ThrottlingEntity
424
+ ): void {
425
+ this.setItem(throttlingCacheKey, throttlingCache);
426
+ }
427
+
428
+ /**
429
+ * Removes the cache item from memory with the given key.
430
+ * @param key - lookup key to remove a cache entity
431
+ * @param inMemory - key value map of the cache
432
+ */
433
+ removeItem(key: string): boolean {
434
+ this.logger.tracePii(`Item key: ${key}`);
435
+
436
+ // read inMemoryCache
437
+ let result: boolean = false;
438
+ const cache = this.getCache();
439
+
440
+ if (!!cache[key]) {
441
+ delete cache[key];
442
+ result = true;
443
+ }
444
+
445
+ // write to the cache after removal
446
+ if (result) {
447
+ this.setCache(cache);
448
+ this.emitChange();
449
+ }
450
+ return result;
451
+ }
452
+
453
+ /**
454
+ * Checks whether key is in cache.
455
+ * @param key - look up key for a cache entity
456
+ */
457
+ containsKey(key: string): boolean {
458
+ return this.getKeys().includes(key);
459
+ }
460
+
461
+ /**
462
+ * Gets all keys in window.
463
+ */
464
+ getKeys(): string[] {
465
+ this.logger.trace("Retrieving all cache keys");
466
+
467
+ // read cache
468
+ const cache = this.getCache();
469
+ return [...Object.keys(cache)];
470
+ }
471
+
472
+ /**
473
+ * Clears all cache entries created by MSAL (except tokens).
474
+ */
475
+ async clear(): Promise<void> {
476
+ this.logger.trace("Clearing cache entries created by MSAL");
477
+
478
+ // read inMemoryCache
479
+ const cacheKeys = this.getKeys();
480
+
481
+ // delete each element
482
+ cacheKeys.forEach((key) => {
483
+ this.removeItem(key);
484
+ });
485
+ this.emitChange();
486
+ }
487
+
488
+ /**
489
+ * Initialize in memory cache from an exisiting cache vault
490
+ * @param cache - blob formatted cache (JSON)
491
+ */
492
+ static generateInMemoryCache(cache: string): InMemoryCache {
493
+ return Deserializer.deserializeAllCache(
494
+ Deserializer.deserializeJSONBlob(cache)
495
+ );
496
+ }
497
+
498
+ /**
499
+ * retrieves the final JSON
500
+ * @param inMemoryCache - itemised cache read from the JSON
501
+ */
502
+ static generateJsonCache(inMemoryCache: InMemoryCache): JsonCache {
503
+ return Serializer.serializeAllCache(inMemoryCache);
504
+ }
505
+
506
+ /**
507
+ * Updates a credential's cache key if the current cache key is outdated
508
+ */
509
+ updateCredentialCacheKey(
510
+ currentCacheKey: string,
511
+ credential: ValidCredentialType
512
+ ): string {
513
+ const updatedCacheKey = credential.generateCredentialKey();
514
+
515
+ if (currentCacheKey !== updatedCacheKey) {
516
+ const cacheItem = this.getItem(currentCacheKey);
517
+ if (cacheItem) {
518
+ this.removeItem(currentCacheKey);
519
+ this.setItem(updatedCacheKey, cacheItem);
520
+ this.logger.verbose(
521
+ `Updated an outdated ${credential.credentialType} cache key`
522
+ );
523
+ return updatedCacheKey;
524
+ } else {
525
+ this.logger.error(
526
+ `Attempted to update an outdated ${credential.credentialType} cache key but no item matching the outdated key was found in storage`
527
+ );
528
+ }
529
+ }
530
+
531
+ return currentCacheKey;
532
+ }
533
+ }