@@ -94,19 +94,26 @@ def _renew_access_token(self):
94
94
raise Exception ("Error retrieving a Domo API Access Token: " + response .text )
95
95
96
96
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
110
117
111
118
def dump_response (self , response ):
112
119
data = dump .dump_all (response )
0 commit comments