Skip to content

Commit 31bc31e

Browse files
committed
Schema defines cached_at, expires_on, ext_expires_on as string
1 parent a0eab4e commit 31bc31e

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

msal/application.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,12 +292,14 @@ def acquire_token_silent(
292292
})
293293
now = time.time()
294294
for entry in matches:
295-
if entry["expires_on"] - now < 5*60:
295+
expires_in = int(entry["expires_on"]) - now
296+
if expires_in < 5*60:
296297
continue # Removal is not necessary, it will be overwritten
298+
logger.debug("Cache hit an AT")
297299
return { # Mimic a real response
298300
"access_token": entry["secret"],
299301
"token_type": "Bearer",
300-
"expires_in": entry["expires_on"] - now,
302+
"expires_in": int(expires_in), # OAuth2 specs defines it as int
301303
}
302304

303305
matches = self.token_cache.find(
@@ -311,6 +313,7 @@ def acquire_token_silent(
311313
})
312314
client = self._build_client(self.client_credential, the_authority)
313315
for entry in matches:
316+
logger.debug("Cache hit an RT")
314317
response = client.obtain_token_by_refresh_token(
315318
entry, rt_getter=lambda token_item: token_item["secret"],
316319
scope=decorate_scope(scopes, self.client_id))

msal/token_cache.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ def add(self, event, now=None):
8787
target,
8888
]).lower()
8989
now = time.time() if now is None else now
90+
expires_in = response.get("expires_in", 3599)
9091
self._cache.setdefault(self.CredentialType.ACCESS_TOKEN, {})[key] = {
9192
"credential_type": self.CredentialType.ACCESS_TOKEN,
9293
"secret": access_token,
@@ -95,9 +96,10 @@ def add(self, event, now=None):
9596
"client_id": event.get("client_id"),
9697
"target": target,
9798
"realm": realm,
98-
"cached_at": now,
99-
"expires_on": now + response.get("expires_in", 3599),
100-
"extended_expires_on": now + response.get("ext_expires_in", 0),
99+
"cached_at": str(int(now)), # Schema defines it as a string
100+
"expires_on": str(int(now + expires_in)), # Same here
101+
"extended_expires_on": str(int( # Same here
102+
now + response.get("ext_expires_in", expires_in))),
101103
}
102104

103105
if client_info:

tests/test_token_cache.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ def testAdd(self):
3939
}, now=1000)
4040
self.assertEqual(
4141
{
42-
'cached_at': 1000,
42+
'cached_at': "1000",
4343
'client_id': 'my_client_id',
4444
'credential_type': 'AccessToken',
4545
'environment': 'login.example.com',
46-
'expires_on': 4600,
47-
'extended_expires_on': 1000,
46+
'expires_on': "4600",
47+
'extended_expires_on': "4600",
4848
'home_account_id': "uid.utid",
4949
'realm': 'contoso',
5050
'secret': 'an access token',

0 commit comments

Comments
 (0)