Skip to content

Migrate MSAL Python Cache to MSAL Extensions

Abhidnya edited this page Aug 4, 2020 · 1 revision

Scenario

App already has an MSAL Python cache stored in a file in unencrypted format. App now wants to move to using MSAL Extensions for Python and still use the same file for cache persistence.

Solution

This will be a migration scenario that needs to be performed only once before starting to use MSAL Extensions for Python. Following is the code which explains such a migration scenario:

old_cache = msal.SerializableTokenCache() # MSAL Python Cache Object

if os.path.exists("my_cache.bin"): 
    old_cache.deserialize(open("my_cache.bin", "r").read()) # Read the existing cache into MSAL Python cache_object

persistence = build_persistence("my_cache.bin") # MSAL Extensions Persistence layer initialization using same location
persistence.save(old_cache.serialize()) # Perform save to make sure old cache is stored in new cache format

new_cache = PersistedTokenCache(persistence) #Initialize MSAL Extensions cache object

# Now you can use it in an msal application like this:
app_2 = msal.PublicClientApplication(client_id='client_id', authority='authority', token_cache=new_cache)

Other Solutions

A completely new file can be used if that is an option, that would mean we can skip this migration step.

Clone this wiki locally