Skip to content

Commit ae07f28

Browse files
committed
Switch to MSAL 1.27+'s TokenCache._find()
1 parent a10d092 commit ae07f28

7 files changed

+25
-25
lines changed

docker_run.sh

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ docker build -t $IMAGE_NAME - < Dockerfile
66
echo "==== Integration Test for Persistence on Linux (libsecret) ===="
77
echo "After seeing the bash prompt, run the following to test encryption on Linux:"
88
echo " pip install -e ."
9-
echo " pytest"
9+
echo " pytest -s tests/chosen_test_file.py"
10+
echo "Note that you probably need to set up ENV VAR for the test cases to run"
1011
docker run --rm -it \
1112
--privileged \
13+
--env-file .env \
1214
-w /home -v $PWD:/home \
1315
$IMAGE_NAME \
1416
$1

msal_extensions/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Provides auxiliary functionality to the `msal` package."""
2-
__version__ = "1.1.0"
2+
__version__ = "1.2.0b1" # Note: During/after release, copy this number to Dockerfile
33

44
from .persistence import (
55
FilePersistence,

msal_extensions/token_cache.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def modify(self, credential_type, old_entry, new_key_value_pairs=None):
6969
self._persistence.save(self.serialize())
7070
self._last_sync = time.time()
7171

72-
def find(self, credential_type, **kwargs): # pylint: disable=arguments-differ
72+
def _find(self, credential_type, **kwargs): # pylint: disable=arguments-differ
7373
# Use optimistic locking rather than CrossPlatLock(self._lock_location)
7474
retry = 3
7575
for attempt in range(1, retry + 1):
@@ -83,6 +83,6 @@ def find(self, credential_type, **kwargs): # pylint: disable=arguments-differ
8383
else:
8484
raise # End of retry. Re-raise the exception as-is.
8585
else: # If reload encountered no error, the data is considered intact
86-
return super(PersistedTokenCache, self).find(credential_type, **kwargs)
86+
return super(PersistedTokenCache, self)._find(credential_type, **kwargs)
8787
return [] # Not really reachable here. Just to keep pylint happy.
8888

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
package_data={'': ['LICENSE']},
2020
python_requires=">=3.7",
2121
install_requires=[
22-
'msal>=0.4.1,<2.0.0',
22+
'msal>=1.27,<1.29', # MSAL Python 1.29+ may not have TokenCache._find()
2323
'portalocker<3,>=1.4',
2424

2525
## We choose to NOT define a hard dependency on this.

tests/test_agnostic_backend.py

+16-20
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,28 @@ def temp_location():
1515
yield os.path.join(test_folder, 'token_cache.bin')
1616
shutil.rmtree(test_folder, ignore_errors=True)
1717

18-
19-
def _test_token_cache_roundtrip(cache):
18+
def _test_token_cache_roundtrip(persistence):
2019
client_id = os.getenv('AZURE_CLIENT_ID')
2120
client_secret = os.getenv('AZURE_CLIENT_SECRET')
2221
if not (client_id and client_secret):
2322
pytest.skip('no credentials present to test TokenCache round-trip with.')
2423

25-
app = msal.ConfidentialClientApplication(
26-
client_id=client_id,
27-
client_credential=client_secret,
28-
token_cache=cache)
2924
desired_scopes = ['https://graph.microsoft.com/.default']
30-
token1 = app.acquire_token_for_client(scopes=desired_scopes)
31-
os.utime( # Mock having another process update the cache
32-
cache._persistence.get_location(), None)
33-
token2 = app.acquire_token_silent(scopes=desired_scopes, account=None)
34-
assert token1['access_token'] == token2['access_token']
35-
36-
def test_file_token_cache_roundtrip(temp_location):
37-
_test_token_cache_roundtrip(PersistedTokenCache(FilePersistence(temp_location)))
38-
39-
def test_current_platform_cache_roundtrip_with_persistence_builder(temp_location):
40-
_test_token_cache_roundtrip(PersistedTokenCache(build_encrypted_persistence(temp_location)))
41-
42-
def test_persisted_token_cache(temp_location):
43-
_test_token_cache_roundtrip(PersistedTokenCache(FilePersistence(temp_location)))
25+
apps = [ # Multiple apps sharing same persistence
26+
msal.ConfidentialClientApplication(
27+
client_id, client_credential=client_secret,
28+
token_cache=PersistedTokenCache(persistence)) for i in range(2)]
29+
token1 = apps[0].acquire_token_for_client(scopes=desired_scopes)
30+
assert token1["token_source"] == "identity_provider", "Initial token should come from IdP"
31+
token2 = apps[1].acquire_token_for_client(scopes=desired_scopes) # Hit token cache in MSAL 1.23+
32+
assert token2["token_source"] == "cache", "App2 should hit cache written by app1"
33+
assert token1['access_token'] == token2['access_token'], "Cache should hit"
34+
35+
def test_token_cache_roundtrip_with_persistence_biulder(temp_location):
36+
_test_token_cache_roundtrip(build_encrypted_persistence(temp_location))
37+
38+
def test_token_cache_roundtrip_with_file_persistence(temp_location):
39+
_test_token_cache_roundtrip(FilePersistence(temp_location))
4440

4541
def test_file_not_found_error_is_not_raised():
4642
persistence = FilePersistence('non_existing_file')

tests/test_macos_backend.py

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def test_osx_token_cache_roundtrip():
3939
token_cache=subject)
4040
desired_scopes = ['https://graph.microsoft.com/.default']
4141
token1 = app.acquire_token_for_client(scopes=desired_scopes)
42+
# TODO: Modify this to same approach in test_agnostic_backend.py
4243
os.utime(cache_file, None) # Mock having another process update the cache.
4344
token2 = app.acquire_token_silent(scopes=desired_scopes, account=None)
4445
assert token1['access_token'] == token2['access_token']

tests/test_windows_backend.py

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def test_windows_token_cache_roundtrip():
9393
token_cache=subject)
9494
desired_scopes = ['https://graph.microsoft.com/.default']
9595
token1 = app.acquire_token_for_client(scopes=desired_scopes)
96+
# TODO: Modify this to same approach in test_agnostic_backend.py
9697
os.utime(cache_file, None) # Mock having another process update the cache.
9798
token2 = app.acquire_token_silent(scopes=desired_scopes, account=None)
9899
assert token1['access_token'] == token2['access_token']

0 commit comments

Comments
 (0)