@@ -150,9 +150,7 @@ def get_headers() -> Dict[str, str]:
150
150
else :
151
151
# Token is from a different host, need to exchange
152
152
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 )
156
154
except Exception as e :
157
155
logger .error (f"Error processing token: { str (e )} " )
158
156
# Fall back to original headers in case of error
@@ -172,9 +170,7 @@ def _init_oidc_discovery(self):
172
170
173
171
if idp_endpoints :
174
172
# 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 )
178
174
179
175
# Fetch the OpenID configuration
180
176
response = requests .get (openid_config_url )
@@ -185,7 +181,8 @@ def _init_oidc_discovery(self):
185
181
logger .info (f"Discovered token endpoint: { self .token_endpoint } " )
186
182
else :
187
183
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 } "
189
186
)
190
187
except Exception as e :
191
188
logger .warning (
@@ -282,9 +279,15 @@ def _refresh_token(self, access_token: str, token_type: str) -> Dict[str, str]:
282
279
self .last_external_token = access_token
283
280
284
281
# 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
+ }
286
287
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
+ )
288
291
return self .external_provider_headers
289
292
290
293
def _try_token_exchange_or_fallback (
@@ -305,12 +308,20 @@ def _try_token_exchange_or_fallback(
305
308
self .last_exchanged_token = exchanged_token
306
309
self .last_external_token = access_token
307
310
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
+ }
309
316
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
+ )
311
320
return self .external_provider_headers
312
321
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 ]:
314
325
"""
315
326
Send the token exchange request to the token endpoint.
316
327
@@ -325,20 +336,19 @@ def _send_token_exchange_request(self, token_exchange_data: Dict[str, str]) -> D
325
336
"""
326
337
if not self .token_endpoint :
327
338
raise ValueError ("Token endpoint not initialized" )
328
-
339
+
329
340
headers = {"Accept" : "*/*" , "Content-Type" : "application/x-www-form-urlencoded" }
330
-
341
+
331
342
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
335
344
)
336
-
345
+
337
346
if response .status_code != 200 :
338
347
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 } "
340
350
)
341
-
351
+
342
352
return response .json ()
343
353
344
354
def _exchange_token (self , access_token : str ) -> Token :
@@ -365,26 +375,28 @@ def _exchange_token(self, access_token: str) -> Token:
365
375
try :
366
376
# Send the token exchange request
367
377
resp_data = self ._send_token_exchange_request (token_exchange_data )
368
-
378
+
369
379
# Extract token information
370
380
new_access_token = resp_data .get ("access_token" )
371
381
if not new_access_token :
372
382
raise ValueError ("No access token in exchange response" )
373
-
383
+
374
384
token_type = resp_data .get ("token_type" , "Bearer" )
375
385
refresh_token = resp_data .get ("refresh_token" , "" )
376
-
386
+
377
387
# Parse expiry time from token claims if possible
378
388
expiry = datetime .now (tz = timezone .utc )
379
-
389
+
380
390
# First try to get expiry from the response's expires_in field
381
391
if "expires_in" in resp_data and resp_data ["expires_in" ]:
382
392
try :
383
393
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
+ )
385
397
except (ValueError , TypeError ) as e :
386
398
logger .warning (f"Invalid expires_in value: { str (e )} " )
387
-
399
+
388
400
# If that didn't work, try to parse JWT claims for expiry
389
401
if expiry == datetime .now (tz = timezone .utc ):
390
402
token_claims = self ._parse_jwt_claims (new_access_token )
@@ -394,9 +406,9 @@ def _exchange_token(self, access_token: str) -> Token:
394
406
expiry = datetime .fromtimestamp (exp_timestamp , tz = timezone .utc )
395
407
except (ValueError , TypeError ) as e :
396
408
logger .warning (f"Invalid exp claim in token: { str (e )} " )
397
-
409
+
398
410
return Token (new_access_token , token_type , refresh_token , expiry )
399
-
411
+
400
412
except Exception as e :
401
413
logger .error (f"Token exchange failed: { str (e )} " )
402
414
raise
0 commit comments