@@ -150,9 +150,7 @@ def get_headers() -> Dict[str, str]:
150150 else :
151151 # Token is from a different host, need to exchange
152152 logger .debug ("Token from different host, attempting exchange" )
153- return self ._try_token_exchange_or_fallback (
154- access_token , token_type
155- )
153+ return self ._try_token_exchange_or_fallback (access_token , token_type )
156154 except Exception as e :
157155 logger .error (f"Error processing token: { str (e )} " )
158156 # Fall back to original headers in case of error
@@ -172,9 +170,7 @@ def _init_oidc_discovery(self):
172170
173171 if idp_endpoints :
174172 # Get the OpenID configuration URL
175- openid_config_url = idp_endpoints .get_openid_config_url (
176- self .hostname
177- )
173+ openid_config_url = idp_endpoints .get_openid_config_url (self .hostname )
178174
179175 # Fetch the OpenID configuration
180176 response = requests .get (openid_config_url )
@@ -185,7 +181,8 @@ def _init_oidc_discovery(self):
185181 logger .info (f"Discovered token endpoint: { self .token_endpoint } " )
186182 else :
187183 logger .warning (
188- f"Failed to fetch OpenID configuration from { openid_config_url } : { response .status_code } "
184+ f"Failed to fetch OpenID configuration from { openid_config_url } : "
185+ f"{ response .status_code } "
189186 )
190187 except Exception as e :
191188 logger .warning (
@@ -282,9 +279,15 @@ def _refresh_token(self, access_token: str, token_type: str) -> Dict[str, str]:
282279 self .last_external_token = access_token
283280
284281 # Update the headers with the new token
285- return {"Authorization" : f"{ exchanged_token .token_type } { exchanged_token .access_token } " }
282+ return {
283+ "Authorization" : (
284+ f"{ exchanged_token .token_type } { exchanged_token .access_token } "
285+ )
286+ }
286287 except Exception as e :
287- logger .error (f"Token refresh failed: { str (e )} , falling back to original token" )
288+ logger .error (
289+ f"Token refresh failed: { str (e )} , falling back to original token"
290+ )
288291 return self .external_provider_headers
289292
290293 def _try_token_exchange_or_fallback (
@@ -305,12 +308,20 @@ def _try_token_exchange_or_fallback(
305308 self .last_exchanged_token = exchanged_token
306309 self .last_external_token = access_token
307310
308- return {"Authorization" : f"{ exchanged_token .token_type } { exchanged_token .access_token } " }
311+ return {
312+ "Authorization" : (
313+ f"{ exchanged_token .token_type } { exchanged_token .access_token } "
314+ )
315+ }
309316 except Exception as e :
310- logger .warning (f"Token exchange failed: { str (e )} , falling back to original token" )
317+ logger .warning (
318+ f"Token exchange failed: { str (e )} , falling back to original token"
319+ )
311320 return self .external_provider_headers
312321
313- def _send_token_exchange_request (self , token_exchange_data : Dict [str , str ]) -> Dict [str , Any ]:
322+ def _send_token_exchange_request (
323+ self , token_exchange_data : Dict [str , str ]
324+ ) -> Dict [str , Any ]:
314325 """
315326 Send the token exchange request to the token endpoint.
316327
@@ -325,20 +336,19 @@ def _send_token_exchange_request(self, token_exchange_data: Dict[str, str]) -> D
325336 """
326337 if not self .token_endpoint :
327338 raise ValueError ("Token endpoint not initialized" )
328-
339+
329340 headers = {"Accept" : "*/*" , "Content-Type" : "application/x-www-form-urlencoded" }
330-
341+
331342 response = requests .post (
332- self .token_endpoint ,
333- data = token_exchange_data ,
334- headers = headers
343+ self .token_endpoint , data = token_exchange_data , headers = headers
335344 )
336-
345+
337346 if response .status_code != 200 :
338347 raise ValueError (
339- f"Token exchange failed with status code { response .status_code } : { response .text } "
348+ f"Token exchange failed with status code { response .status_code } : "
349+ f"{ response .text } "
340350 )
341-
351+
342352 return response .json ()
343353
344354 def _exchange_token (self , access_token : str ) -> Token :
@@ -365,26 +375,28 @@ def _exchange_token(self, access_token: str) -> Token:
365375 try :
366376 # Send the token exchange request
367377 resp_data = self ._send_token_exchange_request (token_exchange_data )
368-
378+
369379 # Extract token information
370380 new_access_token = resp_data .get ("access_token" )
371381 if not new_access_token :
372382 raise ValueError ("No access token in exchange response" )
373-
383+
374384 token_type = resp_data .get ("token_type" , "Bearer" )
375385 refresh_token = resp_data .get ("refresh_token" , "" )
376-
386+
377387 # Parse expiry time from token claims if possible
378388 expiry = datetime .now (tz = timezone .utc )
379-
389+
380390 # First try to get expiry from the response's expires_in field
381391 if "expires_in" in resp_data and resp_data ["expires_in" ]:
382392 try :
383393 expires_in = int (resp_data ["expires_in" ])
384- expiry = datetime .now (tz = timezone .utc ) + timedelta (seconds = expires_in )
394+ expiry = datetime .now (tz = timezone .utc ) + timedelta (
395+ seconds = expires_in
396+ )
385397 except (ValueError , TypeError ) as e :
386398 logger .warning (f"Invalid expires_in value: { str (e )} " )
387-
399+
388400 # If that didn't work, try to parse JWT claims for expiry
389401 if expiry == datetime .now (tz = timezone .utc ):
390402 token_claims = self ._parse_jwt_claims (new_access_token )
@@ -394,9 +406,9 @@ def _exchange_token(self, access_token: str) -> Token:
394406 expiry = datetime .fromtimestamp (exp_timestamp , tz = timezone .utc )
395407 except (ValueError , TypeError ) as e :
396408 logger .warning (f"Invalid exp claim in token: { str (e )} " )
397-
409+
398410 return Token (new_access_token , token_type , refresh_token , expiry )
399-
411+
400412 except Exception as e :
401413 logger .error (f"Token exchange failed: { str (e )} " )
402414 raise
0 commit comments