Skip to content

Commit c4152d2

Browse files
committed
Suggests to use XDG_RUNTIME_DIR for token cache
1 parent dd4dbe7 commit c4152d2

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

msal/token_cache.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,11 +353,14 @@ class SerializableTokenCache(TokenCache):
353353
the following simple recipe for file-based persistence may be sufficient::
354354
355355
import os, atexit, msal
356+
cache_filename = os.path.join( # Persist cache into this file
357+
os.getenv("XDG_RUNTIME_DIR", ""), # Automatically wipe out the cache from Linux when user's ssh session ends. See also https://github.com/AzureAD/microsoft-authentication-library-for-python/issues/690
358+
"my_cache.bin")
356359
cache = msal.SerializableTokenCache()
357-
if os.path.exists("my_cache.bin"):
358-
cache.deserialize(open("my_cache.bin", "r").read())
360+
if os.path.exists(cache_filename):
361+
cache.deserialize(open(cache_filename, "r").read())
359362
atexit.register(lambda:
360-
open("my_cache.bin", "w").write(cache.serialize())
363+
open(cache_filename, "w").write(cache.serialize())
361364
# Hint: The following optional line persists only when state changed
362365
if cache.has_state_changed else None
363366
)

0 commit comments

Comments
 (0)