-
Notifications
You must be signed in to change notification settings - Fork 2.7k
add distributed token cache to ExpressTestApp sample #3986
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, sample works as expected and a great starting point for distributed caching.
Reminder: The next release is scheduled for next week and this PR appears to be stale :( If changes have been requested please address feedback. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a couple nits but otherwise looks good
} | ||
|
||
if (sessionData) { | ||
persistenceHelper.get(JSON.parse(sessionData).account.homeAccountId, (err, cacheData) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably should wrap the JSON.parse
in a try/catch
const kvStore = cacheContext.tokenCache.getKVStore(); | ||
|
||
// getting homeAccountId from account entity in kvStore | ||
const homeAccountId = Object.values(kvStore)[1]["homeAccountId"]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should validate that kvStore
even has any values and then validate that homeAccountId
is defined before moving on to the next part
@@ -1,2260 +0,0 @@ | |||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can add an .npmrc
file with package-lock=false
to this directory to prevent package-locks from being generated in the future.
@@ -21,7 +21,7 @@ | |||
"typescript": "^4.0.5" | |||
}, | |||
"dependencies": { | |||
"@azure/msal-node": "^1.1.0", | |||
"@azure/msal-node": "file:../../../lib/msal-node", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's leave this versioned. You can add scripts to help going back and forth between local and published versions. Some of our other samples demonstrate this.
This adds distributed caching support to the ExpressTestApp sample using Redis as persistence client (via node-redis).
The custom cache plugin implements the ICachePlugin. The persistent cache here is a simple key-value store. For each user, there are 2 records:
sessionId
homeAccountId
homeAccountId
The way this works is roughly as follows:
initializeTokenCachePlugin
is used pass the session variable to the custom cache plugin.* Before cache access, the plugin checks the persistence store for the given
sessionId
. If a record is found, it grabs thehomeAccountId
from the session record, and then gets the relevant cache blob usinghomeAccountId
as key. The blob then is deserialized into msal's in-memory cache.* After cache access, the plugin checks if the cache has changed (in-memory vs persistence). If so, it serializes the in-memory cache back to persistence store.
Couple of observations:
tokenCache
is a private member ofClientApplication
, this leads to compile errors.acquireTokenSilent
to trigger a cache access, (so thatbeforeCacheAccess
/afterCacheAccess
runs). This wasn't the case, and I had to callgetAccountByHomeId
to do the job. This seems off, the dev might be storing the account object in a session or etc. so might not necessarily call this API.