Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/azure-cli-core/azure/cli/core/adal_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

import datetime
import time
import requests
import adal
Expand Down Expand Up @@ -75,6 +76,13 @@ def get_token(self, *scopes, **kwargs): # pylint:disable=unused-argument
logger.debug("AdalAuthentication.get_token invoked by Track 2 SDK with scopes=%s", scopes)

_, token, full_token, _ = self._get_token(_try_scopes_to_resource(scopes))

try:
expires_on = full_token.get('expiresOn', full_token['expires_on'])
Copy link
Member

@jiasli jiasli Nov 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a user token (which has expiresOn), full_token['expires_on'] will be evaluated first and result in a KeyError.

Suggested change
expires_on = full_token.get('expiresOn', full_token['expires_on'])
expires_on = full_token['expiresOn']

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will also break Cloud Shell because expires_on is an epoch time and should be used directly. Parsing it will result in failure.

return AccessToken(token, int(datetime.datetime.strptime(expires_on, '%Y-%m-%d %H:%M:%S.%f').timestamp()))
except: # pylint: disable=bare-except
pass # To avoid crashes due to some unexpected token formats

try:
return AccessToken(token, int(full_token['expiresIn'] + time.time()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still we need to figure out why expiresIn gets corrupted in the first place.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original code is really "carving the boat to seek the sunk sword" (刻舟求剑).

except KeyError: # needed to deal with differing unserialized MSI token payload
Expand Down