Skip to content

Commit 3af5586

Browse files
committed
DOMO-352334: Adding exception-handling to expiration extraction to avoid fatal failures for a somewhat trivial change
1 parent a2baad1 commit 3af5586

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

pydomo/Transport.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,19 +94,26 @@ def _renew_access_token(self):
9494
raise Exception("Error retrieving a Domo API Access Token: " + response.text)
9595

9696
def _extract_expiration(self, access_token):
97-
EXPIRATION_DATE_KEY = 'exp'
98-
99-
token_parts = access_token.split('.')
100-
payload_bytes = bytes(token_parts[1], 'utf-8')
101-
# Padding required for the base64 library
102-
decoded_payload_bytes = base64.urlsafe_b64decode(payload_bytes + b'==')
103-
payload_string = decoded_payload_bytes.decode('utf-8')
104-
decoded_payload_dict = json.loads(payload_string)
105-
if EXPIRATION_DATE_KEY in decoded_payload_dict.keys():
106-
expiration_date = decoded_payload_dict[EXPIRATION_DATE_KEY]
107-
self.logger.debug('Token expiration: {}'.format(expiration_date))
108-
return expiration_date
109-
return 0
97+
expiration_date = 0
98+
try:
99+
token_parts = access_token.split('.')
100+
payload_bytes = bytes(token_parts[1], 'utf-8')
101+
102+
# Padding required for the base64 library
103+
decoded_payload_bytes = base64.urlsafe_b64decode(payload_bytes + b'==')
104+
payload_string = decoded_payload_bytes.decode('utf-8')
105+
decoded_payload_dict = json.loads(payload_string)
106+
107+
if 'exp' in decoded_payload_dict.keys():
108+
expiration_date = decoded_payload_dict['exp']
109+
self.logger.debug('Token expiration: {}'.format(expiration_date))
110+
except Exception as err:
111+
# If an Exception is raised, log and continue. expiration_date will
112+
# either be 0 or set to the value in the JWT.
113+
self.logger.debug('Ran into error parsing token for expiration. '
114+
'Setting expiration date to 0. '
115+
'{}: {}'.format(type(err).__name__, err))
116+
return expiration_date
110117

111118
def dump_response(self, response):
112119
data = dump.dump_all(response)

0 commit comments

Comments
 (0)