@@ -10,6 +10,7 @@ import {
1010 ICachePlugin ,
1111 buildStaticAuthorityOptions ,
1212 Constants ,
13+ AuthorityMetadataEntity ,
1314} from "@azure/msal-common" ;
1415import { NodeStorage } from "../../src/cache/NodeStorage.js" ;
1516import { TokenCache } from "../../src/cache/TokenCache.js" ;
@@ -283,6 +284,70 @@ describe("TokenCache tests", () => {
283284 ) ;
284285 } ) ;
285286
287+ it ( "overwriteCache should preserve authority metadata" , async ( ) => {
288+ const cachePath = "./test/cache/cache-test-files/default-cache.json" ;
289+ const beforeCacheAccess = async ( context : TokenCacheContext ) => {
290+ context . tokenCache . deserialize (
291+ await promises . readFile ( cachePath , Constants . EncodingTypes . UTF8 )
292+ ) ;
293+ } ;
294+ const afterCacheAccess = async ( context : TokenCacheContext ) => {
295+ await promises . writeFile ( cachePath , context . tokenCache . serialize ( ) ) ;
296+ } ;
297+
298+ const cachePlugin : ICachePlugin = {
299+ beforeCacheAccess,
300+ afterCacheAccess,
301+ } ;
302+
303+ const tokenCache = new TokenCache ( storage , logger , cachePlugin ) ;
304+
305+ // Populate authority metadata in the storage before overwriting
306+ const authorityMetadataKey =
307+ "authority-metadata-mock_client_id-login.microsoftonline.com" ;
308+ const authorityMetadata = {
309+ aliases : [
310+ "login.microsoftonline.com" ,
311+ "login.windows.net" ,
312+ "login.microsoft.com" ,
313+ ] ,
314+ preferred_cache : "login.windows.net" ,
315+ preferred_network : "login.microsoftonline.com" ,
316+ canonical_authority : "https://login.microsoftonline.com/common" ,
317+ authorization_endpoint :
318+ "https://login.microsoftonline.com/common/oauth2/v2.0/authorize" ,
319+ token_endpoint :
320+ "https://login.microsoftonline.com/common/oauth2/v2.0/token" ,
321+ end_session_endpoint :
322+ "https://login.microsoftonline.com/common/oauth2/v2.0/logout" ,
323+ issuer : "https://login.microsoftonline.com/{tenantid}/v2.0" ,
324+ jwks_uri :
325+ "https://login.microsoftonline.com/common/discovery/v2.0/keys" ,
326+ aliasesFromNetwork : true ,
327+ endpointsFromNetwork : true ,
328+ expiresAt :
329+ msalCommon . CacheHelpers . generateAuthorityMetadataExpiresAt ( ) ,
330+ } ;
331+ storage . setAuthorityMetadata (
332+ authorityMetadataKey ,
333+ authorityMetadata as AuthorityMetadataEntity
334+ ) ;
335+
336+ expect ( storage . getAuthorityMetadata ( authorityMetadataKey ) ) . toEqual (
337+ authorityMetadata
338+ ) ;
339+
340+ await tokenCache . overwriteCache ( ) ;
341+
342+ const restoredMetadata =
343+ storage . getAuthorityMetadata ( authorityMetadataKey ) ;
344+ expect ( restoredMetadata ) . not . toBeNull ( ) ;
345+ expect ( restoredMetadata ! . aliases ) . toEqual ( authorityMetadata . aliases ) ;
346+ expect ( restoredMetadata ! . preferred_cache ) . toEqual (
347+ authorityMetadata . preferred_cache
348+ ) ;
349+ } ) ;
350+
286351 it ( "overwriteCache should not throw and simply return if persistent cache does not exist" , async ( ) => {
287352 const tokenCache = new TokenCache ( storage , logger ) ;
288353
0 commit comments