@@ -41,9 +41,7 @@ def __init__(self, access_token: str, token_type: str, refresh_token: str):
4141
4242 def is_expired (self ) -> bool :
4343 try :
44- decoded_token = jwt .decode (
45- self .access_token , options = {"verify_signature" : False }
46- )
44+ decoded_token = jwt .decode (self .access_token , options = {"verify_signature" : False })
4745 exp_time = decoded_token .get ("exp" )
4846 current_time = time .time ()
4947 buffer_time = 30 # 30 seconds buffer
@@ -134,9 +132,7 @@ def __fetch_well_known_config(self, hostname: str):
134132 def __get_challenge ():
135133 verifier_string = OAuthManager .__token_urlsafe (32 )
136134 digest = hashlib .sha256 (verifier_string .encode ("UTF-8" )).digest ()
137- challenge_string = (
138- base64 .urlsafe_b64encode (digest ).decode ("UTF-8" ).replace ("=" , "" )
139- )
135+ challenge_string = base64 .urlsafe_b64encode (digest ).decode ("UTF-8" ).replace ("=" , "" )
140136 return verifier_string , challenge_string
141137
142138 def __get_authorization_code (self , client , auth_url , scope , state , challenge ):
@@ -158,9 +154,7 @@ def __get_authorization_code(self, client, auth_url, scope, state, challenge):
158154 logger .info (f"Opening { auth_req_uri } " )
159155
160156 webbrowser .open_new (auth_req_uri )
161- logger .info (
162- f"Listening for OAuth authorization callback at { redirect_url } "
163- )
157+ logger .info (f"Listening for OAuth authorization callback at { redirect_url } " )
164158 httpd .handle_request ()
165159 self .redirect_port = port
166160 break
@@ -182,9 +176,7 @@ def __get_authorization_code(self, client, auth_url, scope, state, challenge):
182176 raise RuntimeError (msg )
183177 # This is a kludge because the parsing library expects https callbacks
184178 # We should probably set it up using https
185- full_redirect_url = (
186- f"https://localhost:{ self .redirect_port } /{ handler .request_path } "
187- )
179+ full_redirect_url = f"https://localhost:{ self .redirect_port } /{ handler .request_path } "
188180 try :
189181 authorization_code_response = client .parse_request_uri_response (
190182 full_redirect_url , state = state
@@ -197,9 +189,7 @@ def __get_authorization_code(self, client, auth_url, scope, state, challenge):
197189 def __send_auth_code_token_request (
198190 self , client , token_request_url , redirect_url , code , verifier
199191 ):
200- token_request_body = client .prepare_request_body (
201- code = code , redirect_uri = redirect_url
202- )
192+ token_request_body = client .prepare_request_body (code = code , redirect_uri = redirect_url )
203193 data = f"{ token_request_body } &code_verifier={ verifier } "
204194 return self .__send_token_request (token_request_url , data )
205195
@@ -227,15 +217,11 @@ def __send_refresh_token_request(self, hostname, refresh_token):
227217 def __get_tokens_from_response (oauth_response ):
228218 access_token = oauth_response ["access_token" ]
229219 refresh_token = (
230- oauth_response ["refresh_token" ]
231- if "refresh_token" in oauth_response
232- else None
220+ oauth_response ["refresh_token" ] if "refresh_token" in oauth_response else None
233221 )
234222 return access_token , refresh_token
235223
236- def check_and_refresh_access_token (
237- self , hostname : str , access_token : str , refresh_token : str
238- ):
224+ def check_and_refresh_access_token (self , hostname : str , access_token : str , refresh_token : str ):
239225 now = datetime .now (tz = timezone .utc )
240226 # If we can't decode an expiration time, this will be expired by default.
241227 expiration_time = now
@@ -246,9 +232,7 @@ def check_and_refresh_access_token(
246232 # an unnecessary signature verification.
247233 access_token_payload = access_token .split ("." )[1 ]
248234 # add padding
249- access_token_payload = access_token_payload + "=" * (
250- - len (access_token_payload ) % 4
251- )
235+ access_token_payload = access_token_payload + "=" * (- len (access_token_payload ) % 4 )
252236 decoded = json .loads (base64 .standard_b64decode (access_token_payload ))
253237 expiration_time = datetime .fromtimestamp (decoded ["exp" ], tz = timezone .utc )
254238 except Exception as e :
@@ -265,13 +249,9 @@ def check_and_refresh_access_token(
265249 raise RuntimeError (msg )
266250
267251 # Try to refresh using the refresh token
268- logger .debug (
269- f"Attempting to refresh OAuth access token that expired on { expiration_time } "
270- )
252+ logger .debug (f"Attempting to refresh OAuth access token that expired on { expiration_time } " )
271253 oauth_response = self .__send_refresh_token_request (hostname , refresh_token )
272- fresh_access_token , fresh_refresh_token = self .__get_tokens_from_response (
273- oauth_response
274- )
254+ fresh_access_token , fresh_refresh_token = self .__get_tokens_from_response (oauth_response )
275255 return fresh_access_token , fresh_refresh_token , True
276256
277257 def get_tokens (self , hostname : str , scope = None ):
@@ -285,9 +265,7 @@ def get_tokens(self, hostname: str, scope=None):
285265 client = oauthlib .oauth2 .WebApplicationClient (self .client_id )
286266
287267 try :
288- auth_response = self .__get_authorization_code (
289- client , auth_url , scope , state , challenge
290- )
268+ auth_response = self .__get_authorization_code (client , auth_url , scope , state , challenge )
291269 except OAuth2Error as e :
292270 msg = f"OAuth Authorization Error: { e .description } "
293271 logger .error (msg )
@@ -359,6 +337,4 @@ def refresh(self) -> Token:
359337 oauth_response .refresh_token ,
360338 )
361339 else :
362- raise Exception (
363- f"Failed to get token: { response .status_code } { response .text } "
364- )
340+ raise Exception (f"Failed to get token: { response .status_code } { response .text } " )
0 commit comments