From e11fea080bb7efb67d18bc0309af936da70646a8 Mon Sep 17 00:00:00 2001 From: Bin Ma Date: Wed, 11 Nov 2020 17:00:04 +0800 Subject: [PATCH 1/6] init --- src/azure-cli-core/azure/cli/core/adal_authentication.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/azure-cli-core/azure/cli/core/adal_authentication.py b/src/azure-cli-core/azure/cli/core/adal_authentication.py index 4d42159d5f9..c2718cd4217 100644 --- a/src/azure-cli-core/azure/cli/core/adal_authentication.py +++ b/src/azure-cli-core/azure/cli/core/adal_authentication.py @@ -6,6 +6,7 @@ import time import requests import adal +import datetime from msrest.authentication import Authentication from msrestazure.azure_active_directory import MSIAuthentication @@ -69,6 +70,11 @@ 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)) + + if 'expiresOn' in full_token: + return AccessToken(token, int(time.mktime( + datetime.datetime.strptime(full_token['expiresOn'], '%Y-%m-%d %H:%M:%S.%f').timetuple()))) + try: return AccessToken(token, int(full_token['expiresIn'] + time.time())) except KeyError: # needed to deal with differing unserialized MSI token payload From a7d9cf09409db90758384b7e1ca8db2c4d26fffc Mon Sep 17 00:00:00 2001 From: Bin Ma Date: Wed, 11 Nov 2020 17:31:16 +0800 Subject: [PATCH 2/6] updated --- src/azure-cli-core/azure/cli/core/adal_authentication.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/azure-cli-core/azure/cli/core/adal_authentication.py b/src/azure-cli-core/azure/cli/core/adal_authentication.py index df8e97e2779..0f2395b6c1d 100644 --- a/src/azure-cli-core/azure/cli/core/adal_authentication.py +++ b/src/azure-cli-core/azure/cli/core/adal_authentication.py @@ -3,10 +3,10 @@ # Licensed under the MIT License. See License.txt in the project root for license information. # -------------------------------------------------------------------------------------------- +import datetime import time import requests import adal -import datetime from msrest.authentication import Authentication from msrestazure.azure_active_directory import MSIAuthentication @@ -78,8 +78,8 @@ def get_token(self, *scopes, **kwargs): # pylint:disable=unused-argument _, token, full_token, _ = self._get_token(_try_scopes_to_resource(scopes)) if 'expiresOn' in full_token: - return AccessToken(token, int(time.mktime( - datetime.datetime.strptime(full_token['expiresOn'], '%Y-%m-%d %H:%M:%S.%f').timetuple()))) + return AccessToken(token, int( + datetime.datetime.strptime(full_token['expiresOn'], '%Y-%m-%d %H:%M:%S.%f').timestamp())) try: return AccessToken(token, int(full_token['expiresIn'] + time.time())) From 03d466cfe5449a2af5d2d62f7de19b4ad1905879 Mon Sep 17 00:00:00 2001 From: Bin Ma Date: Thu, 12 Nov 2020 11:09:39 +0800 Subject: [PATCH 3/6] Add fallback --- src/azure-cli-core/azure/cli/core/adal_authentication.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/azure-cli-core/azure/cli/core/adal_authentication.py b/src/azure-cli-core/azure/cli/core/adal_authentication.py index 0f2395b6c1d..704abb9a79d 100644 --- a/src/azure-cli-core/azure/cli/core/adal_authentication.py +++ b/src/azure-cli-core/azure/cli/core/adal_authentication.py @@ -78,8 +78,11 @@ def get_token(self, *scopes, **kwargs): # pylint:disable=unused-argument _, token, full_token, _ = self._get_token(_try_scopes_to_resource(scopes)) if 'expiresOn' in full_token: - return AccessToken(token, int( - datetime.datetime.strptime(full_token['expiresOn'], '%Y-%m-%d %H:%M:%S.%f').timestamp())) + try: + return AccessToken(token, int( + datetime.datetime.strptime(full_token['expiresOn'], '%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())) From 0f28a7cef93fadb5e39698efb27a27ce9dceb4ff Mon Sep 17 00:00:00 2001 From: Bin Ma Date: Thu, 12 Nov 2020 11:22:27 +0800 Subject: [PATCH 4/6] Remove 'if' --- .../azure/cli/core/adal_authentication.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/azure-cli-core/azure/cli/core/adal_authentication.py b/src/azure-cli-core/azure/cli/core/adal_authentication.py index 704abb9a79d..6203d4df8d2 100644 --- a/src/azure-cli-core/azure/cli/core/adal_authentication.py +++ b/src/azure-cli-core/azure/cli/core/adal_authentication.py @@ -77,12 +77,11 @@ def get_token(self, *scopes, **kwargs): # pylint:disable=unused-argument _, token, full_token, _ = self._get_token(_try_scopes_to_resource(scopes)) - if 'expiresOn' in full_token: - try: - return AccessToken(token, int( - datetime.datetime.strptime(full_token['expiresOn'], '%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( + datetime.datetime.strptime(full_token['expiresOn'], '%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())) From 2bb621c42356b3a7fba8892e668f6a20d3a3c653 Mon Sep 17 00:00:00 2001 From: Bin Ma Date: Thu, 12 Nov 2020 11:25:43 +0800 Subject: [PATCH 5/6] updated --- src/azure-cli-core/azure/cli/core/adal_authentication.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/azure-cli-core/azure/cli/core/adal_authentication.py b/src/azure-cli-core/azure/cli/core/adal_authentication.py index 6203d4df8d2..adafa7e212f 100644 --- a/src/azure-cli-core/azure/cli/core/adal_authentication.py +++ b/src/azure-cli-core/azure/cli/core/adal_authentication.py @@ -78,8 +78,9 @@ def get_token(self, *scopes, **kwargs): # pylint:disable=unused-argument _, token, full_token, _ = self._get_token(_try_scopes_to_resource(scopes)) try: + expires_on = full_token.get('expiresOn', full_token['expires_on']) return AccessToken(token, int( - datetime.datetime.strptime(full_token['expiresOn'], '%Y-%m-%d %H:%M:%S.%f').timestamp())) + 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 From 175f819363f4de1b71004422dd782b7fc3d0011e Mon Sep 17 00:00:00 2001 From: Bin Ma Date: Thu, 12 Nov 2020 11:27:57 +0800 Subject: [PATCH 6/6] refine format --- src/azure-cli-core/azure/cli/core/adal_authentication.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/azure-cli-core/azure/cli/core/adal_authentication.py b/src/azure-cli-core/azure/cli/core/adal_authentication.py index adafa7e212f..67ef3f6c74b 100644 --- a/src/azure-cli-core/azure/cli/core/adal_authentication.py +++ b/src/azure-cli-core/azure/cli/core/adal_authentication.py @@ -79,8 +79,7 @@ def get_token(self, *scopes, **kwargs): # pylint:disable=unused-argument try: expires_on = full_token.get('expiresOn', full_token['expires_on']) - return AccessToken(token, int( - datetime.datetime.strptime(expires_on, '%Y-%m-%d %H:%M:%S.%f').timestamp())) + 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