Skip to content

Commit 8dd07ee

Browse files
style: ruff format (auto)
1 parent 459d2c9 commit 8dd07ee

2 files changed

Lines changed: 19 additions & 11 deletions

File tree

src/auth/ibm_auth.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
IBM_JWT_PUBLIC_KEY_URL is configured.
77
fetch_ibm_public_key — fetch and cache IBM's public key PEM.
88
"""
9+
910
import asyncio
1011

1112
import httpx
@@ -59,6 +60,7 @@ def extract_ibm_credentials(basic_credentials: str) -> tuple[str, str]:
5960
Returns ("unknown", "") if decoding fails.
6061
"""
6162
import base64
63+
6264
try:
6365
raw = basic_credentials[6:] if basic_credentials.startswith("Basic ") else basic_credentials
6466
decoded = base64.b64decode(raw).decode("utf-8")
@@ -92,29 +94,32 @@ def validate_ibm_jwt(token: str, public_key) -> dict | None:
9294
logger.warning("IBM JWT validation failed", error=str(exc))
9395
return None
9496

97+
9598
async def refresh_ibm_jwt(token: str) -> str | None:
9699
"""Refresh the IBM JWT token using the configured PLATFORM_REFRESH_URL."""
97100
if not PLATFORM_REFRESH_URL:
98101
return None
99102

100-
headers = {
101-
"Authorization": f"Bearer {token}",
102-
"User-Agent": "curl/7.64.1"
103-
}
103+
headers = {"Authorization": f"Bearer {token}", "User-Agent": "curl/7.64.1"}
104104

105105
async with httpx.AsyncClient() as client:
106106
for attempt in range(10):
107107
try:
108108
resp = await client.post(PLATFORM_REFRESH_URL, headers=headers, timeout=30.0)
109109
if resp.status_code == 200:
110110
data = resp.json()
111-
new_token = data.get("token") or data.get("access_token") or data.get("refresh_token") or resp.text.strip().strip('"')
112-
if '.' in new_token:
111+
new_token = (
112+
data.get("token")
113+
or data.get("access_token")
114+
or data.get("refresh_token")
115+
or resp.text.strip().strip('"')
116+
)
117+
if "." in new_token:
113118
logger.info("Successfully refreshed IBM JWT token.")
114119
return new_token
115120
except Exception as e:
116121
logger.warning(f"Failed to refresh IBM token (attempt {attempt + 1}): {e}")
117-
122+
118123
await asyncio.sleep(10)
119-
124+
120125
return None

src/dependencies.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,13 +407,13 @@ async def _get_ibm_user(request: Request, response: Response, required: bool) ->
407407
user_id = claims.get("username", sub)
408408
email = claims.get("username", sub)
409409
name = claims.get("display_name", claims.get("username", sub))
410-
410+
411411
exp = claims.get("exp")
412412
if exp:
413413
import datetime
414414

415415
from config.settings import PLATFORM_REFRESH_PERIOD
416-
416+
417417
now = datetime.datetime.now(datetime.UTC).timestamp()
418418
if exp - now < PLATFORM_REFRESH_PERIOD:
419419
logger.info("IBM JWT token is expiring soon, attempting refresh...")
@@ -431,7 +431,10 @@ async def _get_ibm_user(request: Request, response: Response, required: bool) ->
431431
else:
432432
if now >= exp:
433433
if required:
434-
raise HTTPException(status_code=401, detail="IBM session expired. Please log in again.")
434+
raise HTTPException(
435+
status_code=401,
436+
detail="IBM session expired. Please log in again.",
437+
)
435438
return None
436439

437440
if lh_credentials and lh_credentials.strip() != "":

0 commit comments

Comments
 (0)