Skip to content

Commit 34ee024

Browse files
sameeragRobbie-Microsofttnorling
authored
Persist authority in in-memory cache (#4081)
* Persist authority in inmemory cache * Change files * Revering making `inMemoryCache` a class variable * added clearCache() functionality to assist silent flow e2e tests * Apply suggestions from code review Co-authored-by: Robbie-Microsoft <[email protected]> Co-authored-by: Thomas Norling <[email protected]>
1 parent eb5b950 commit 34ee024

8 files changed

+30
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "patch",
3+
"comment": "Persist authority in in memory cache #4081",
4+
"packageName": "@azure/msal-node",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}

lib/msal-node/docs/caching.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const cca = new msal.ConfidentialClientApplication({
1414
}
1515
});
1616

17-
/**
17+
/**
1818
* login* and acquireToken* APIs return an account object containing "homeAccountId"
1919
* you should keep a record of this in your app and use it later on when calling acquireTokenSilent
2020
* For more, see: https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-node/docs/accounts.md
@@ -58,6 +58,9 @@ MSAL Node's cache schema is compatible with other MSALs. By default, MSAL's cach
5858
}
5959
```
6060

61+
## Cache in memory
62+
If a user chooses not to persist cache, the `TokenCache` interface is still available to access the tokens cached in memory. The life time of in memory cache is the same as MSAL instance. If the MSAL instance restarts, the cache is erased when the process lifecycle finishes. We recommend persisting the cache with encryption for all real life applications both for security and desired cache longevity.
63+
6164
## Persistence
6265

6366
MSAL Node fires events when the cache is accessed, apps can choose whether to serialize or deserialize the cache. This often constitutes two actions:

lib/msal-node/src/cache/NodeStorage.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ export class NodeStorage extends CacheManager {
5757
* @param cache - key value store
5858
*/
5959
cacheToInMemoryCache(cache: CacheKVStore): InMemoryCache {
60-
6160
const inMemoryCache: InMemoryCache = {
6261
accounts: {},
6362
idTokens: {},
@@ -90,16 +89,20 @@ export class NodeStorage extends CacheManager {
9089
* @param inMemoryCache - kvstore map for inmemory
9190
*/
9291
inMemoryCacheToCache(inMemoryCache: InMemoryCache): CacheKVStore {
92+
9393
// convert in memory cache to a flat Key-Value map
9494
let cache = this.getCache();
9595

9696
cache = {
97+
...cache,
9798
...inMemoryCache.accounts,
9899
...inMemoryCache.idTokens,
99100
...inMemoryCache.accessTokens,
100101
...inMemoryCache.refreshTokens,
101102
...inMemoryCache.appMetadata
102103
};
104+
105+
// convert in memory cache to a flat Key-Value map
103106
return cache;
104107
}
105108

lib/msal-node/src/client/ClientApplication.ts

+7
Original file line numberDiff line numberDiff line change
@@ -420,4 +420,11 @@ export abstract class ClientApplication {
420420

421421
return await AuthorityFactory.createDiscoveredInstance(authorityUrl, this.config.system.networkClient, this.storage, authorityOptions);
422422
}
423+
424+
/**
425+
* Clear the cache
426+
*/
427+
clearCache(): void {
428+
this.storage.clear();
429+
}
423430
}

lib/msal-node/src/client/IConfidentialClientApplication.ts

+3
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,7 @@ export interface IConfidentialClientApplication {
4848

4949
/** Replaces the default logger set in configurations with new Logger with new configurations */
5050
setLogger(logger: Logger): void;
51+
52+
/** Clear the cache */
53+
clearCache(): void;
5154
}

lib/msal-node/src/client/IPublicClientApplication.ts

+3
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,7 @@ export interface IPublicClientApplication {
4444

4545
/** Replaces the default logger set in configurations with new Logger with new configurations */
4646
setLogger(logger: Logger): void;
47+
48+
/** Clear the cache */
49+
clearCache(): void;
4750
}

samples/msal-node-samples/silent-flow/test/silent-flow-aad.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ describe("Silent Flow AAD PPE Tests", () => {
182182
beforeEach(async () => {
183183
context = await browser.createIncognitoBrowserContext();
184184
page = await context.newPage();
185+
await publicClientApplication.clearCache();
185186
});
186187

187188
afterEach(async () => {

samples/msal-node-samples/silent-flow/test/silent-flow-adfs.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ describe("Silent Flow ADFS 2019 Tests", () => {
180180
beforeEach(async () => {
181181
context = await browser.createIncognitoBrowserContext();
182182
page = await context.newPage();
183+
await publicClientApplication.clearCache();
183184
await page.goto(homeRoute);
184185
});
185186

0 commit comments

Comments
 (0)