-
Notifications
You must be signed in to change notification settings - Fork 202
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
Example of persistence with a DB or KV store #98
Comments
@viperfx Thank you for all these excellent questions!
Short answer: Yes, MSAL cache system internally maintains tokens-and-account relationship. But that "account" concept is probably different than what you think of "user", so you may not really need/want to split them into a per-account data structure/storage. Long answer: MSAL and its token cache were optimized for Public Client, such as a mobile app running on one end user's device. So the implication here is:
Therefore, MSAL Python token cache system stores all tokens as a list of json objects, in memory. During cache look-up, MSAL Python will filter tokens by account. Such setup works well for public client apps, such as Azure CLI
The MSAL cache value is one big blob, with specific internal structure which MSAL token cache logic relies on. It is not encrypted, but we provide basically only serialize() and deserialize() as the public API, so you are not expected to peek into it. The one-cache-per-user approach we used the sample above, can be configured (via Flask-Session) to use Redis, Memcache, or MongoDB as actual storage system.
I guess now you do not need to look into the MSAL token cache internals, do you? |
Thanks for the answer. Let me explain my use cases further so hopefully, it will make sense why a one-cache-per-user approach is really needed. I currently have two immediate use cases that we have already prototyped and is working with this library but as you said, cache storage right now stored as one blog will not scale. The use cases are the following:
The first one used for sign-in can be accepted as a session-based storage. However, for calendar, we will be requesting a Delegated Token, and hoping to refresh the token to sync the Calendar without user input. Let's say I have a long running app, and I have multiples users signing into the app and getting tokens and affecting the cache. I would prefer to have the cache value stored in a table or field related to the user. Or have a redis key related to the userID and value is the cache. So that when I am about to refresh the token for example, I fetch the cache for only that user based on their key. Would you follow the approach in the example given my use case? Would appreciate your input. |
@viperfx |
I had success implementing the distributed cache in Redis like this
The init() method of As @rayluo pointed out - there is no need to worry about the data structure in the cache. MSAL is taking care of this through it's API. The instantiation of the relevant client class allows passing the cache. This cache can take your personal storage preference into account, for example prefixing all keys with def get_location(self):
"""Returns the location in the cache"""
location = f"msal:{self.user_account['homeAccountId']}"
return location Simliar data modifications can be applied in the |
Hi there,
It would be great to see an example implementation of how to modify the tokenCache to store in a simple DB/Cache system such as Redis.
microsoft-authentication-library-for-python/msal/token_cache.py
Lines 283 to 304 in e4510a3
For a DB such as Redis:
I also have a couple of other high-level questions:
Thanks
The text was updated successfully, but these errors were encountered: