|
6 | 6 | IBM_JWT_PUBLIC_KEY_URL is configured. |
7 | 7 | fetch_ibm_public_key — fetch and cache IBM's public key PEM. |
8 | 8 | """ |
| 9 | + |
9 | 10 | import asyncio |
10 | 11 |
|
11 | 12 | import httpx |
@@ -59,6 +60,7 @@ def extract_ibm_credentials(basic_credentials: str) -> tuple[str, str]: |
59 | 60 | Returns ("unknown", "") if decoding fails. |
60 | 61 | """ |
61 | 62 | import base64 |
| 63 | + |
62 | 64 | try: |
63 | 65 | raw = basic_credentials[6:] if basic_credentials.startswith("Basic ") else basic_credentials |
64 | 66 | decoded = base64.b64decode(raw).decode("utf-8") |
@@ -92,29 +94,32 @@ def validate_ibm_jwt(token: str, public_key) -> dict | None: |
92 | 94 | logger.warning("IBM JWT validation failed", error=str(exc)) |
93 | 95 | return None |
94 | 96 |
|
| 97 | + |
95 | 98 | async def refresh_ibm_jwt(token: str) -> str | None: |
96 | 99 | """Refresh the IBM JWT token using the configured PLATFORM_REFRESH_URL.""" |
97 | 100 | if not PLATFORM_REFRESH_URL: |
98 | 101 | return None |
99 | 102 |
|
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"} |
104 | 104 |
|
105 | 105 | async with httpx.AsyncClient() as client: |
106 | 106 | for attempt in range(10): |
107 | 107 | try: |
108 | 108 | resp = await client.post(PLATFORM_REFRESH_URL, headers=headers, timeout=30.0) |
109 | 109 | if resp.status_code == 200: |
110 | 110 | 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: |
113 | 118 | logger.info("Successfully refreshed IBM JWT token.") |
114 | 119 | return new_token |
115 | 120 | except Exception as e: |
116 | 121 | logger.warning(f"Failed to refresh IBM token (attempt {attempt + 1}): {e}") |
117 | | - |
| 122 | + |
118 | 123 | await asyncio.sleep(10) |
119 | | - |
| 124 | + |
120 | 125 | return None |
0 commit comments