SerializableTokenCache Class
This serialization can be a starting point to implement your own persistence.
This class does NOT actually persist the cache on disk/db/etc.. Depending on your need, the following simple recipe for file-based persistence may be sufficient:
import os, atexit, msal
cache = msal.SerializableTokenCache()
if os.path.exists("my_cache.bin"):
cache.deserialize(open("my_cache.bin", "r").read())
atexit.register(lambda:
open("my_cache.bin", "w").write(cache.serialize())
# Hint: The following optional line persists only when state changed
if cache.has_state_changed else None
)
app = msal.ClientApplication(..., token_cache=cache)
...
- Inheritance
-
SerializableTokenCache
Constructor
SerializableTokenCache()
Variables
- has_state_changed
- bool
Indicates whether the cache state in the memory has changed since last serialize or deserialize call.
Methods
| add |
Handle a token obtaining event, and add tokens into cache. Known side effects: This function modifies the input event in place. |
| deserialize |
Deserialize the cache from a state previously obtained by serialize() |
| modify | |
| serialize |
Serialize the current cache state into a string. |
add
Handle a token obtaining event, and add tokens into cache.
Known side effects: This function modifies the input event in place.
add(event, **kwargs)
Parameters
- event
deserialize
Deserialize the cache from a state previously obtained by serialize()
deserialize(state: Optional[str]) -> None
Parameters
- state
modify
modify(credential_type, old_entry, new_key_value_pairs=None)
Parameters
- credential_type
- old_entry
- new_key_value_pairs
serialize
Serialize the current cache state into a string.
serialize() -> str
Attributes
has_state_changed
has_state_changed = False
Feedback
Submit and view feedback for