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