{Core} Fix the incorrect token expiration on ADAL tokens#15875
{Core} Fix the incorrect token expiration on ADAL tokens#15875
Conversation
| return AccessToken(token, int(time.mktime( | ||
| datetime.datetime.strptime(full_token['expiresOn'], '%Y-%m-%d %H:%M:%S.%f').timetuple()))) |
There was a problem hiding this comment.
datetime.timestamp() is an easier way to acquire epoch time.
There was a problem hiding this comment.
Good idea, updated.
| datetime.datetime.strptime(full_token['expiresOn'], '%Y-%m-%d %H:%M:%S.%f').timetuple()))) | ||
|
|
||
| try: | ||
| return AccessToken(token, int(full_token['expiresIn'] + time.time())) |
There was a problem hiding this comment.
Still we need to figure out why expiresIn gets corrupted in the first place.
There was a problem hiding this comment.
@jiasli From my understanding, relative values like expiresIn should not be saved and used. Relative values would become meaningless as time goes by. Or we can recalculate it everytime when we use it, but that will be no difference with using the absolute value expiresOn directly.
There was a problem hiding this comment.
The original code is really "carving the boat to seek the sunk sword" (刻舟求剑).
| _, token, full_token, _ = self._get_token(_try_scopes_to_resource(scopes)) | ||
|
|
||
| try: | ||
| expires_on = full_token.get('expiresOn', full_token['expires_on']) |
There was a problem hiding this comment.
For a user token (which has expiresOn), full_token['expires_on'] will be evaluated first and result in a KeyError.
| expires_on = full_token.get('expiresOn', full_token['expires_on']) | |
| expires_on = full_token['expiresOn'] |
There was a problem hiding this comment.
This will also break Cloud Shell because expires_on is an epoch time and should be used directly. Parsing it will result in failure.
|
I also met this problem! |
Currently I'm facing with a token expiration issue, and I think I have found the root cause of this. When using ADAL token retriever, token data will be loaded from the local cache (
~/.azure/accessTokens.json), incluing two important properties:expiresInandexpiresOn:Local cache file:

Unfortunatelly, our CLI framework just loads all data directly from the cache without any changes:

Based on the observation, our current code is incorrect. It just use
expiresIn + nowto calculate the expiration time, which will be larger than the real one:azure-cli/src/azure-cli-core/azure/cli/core/adal_authentication.py
Lines 77 to 79 in 4eb137b
This checklist is used to make sure that common guidelines for a pull request are followed.
The PR title and description has followed the guideline in Submitting Pull Requests.
I adhere to the Command Guidelines.