()
| 255 | } |
| 256 | |
| 257 | async function loadCodexAuth(): Promise<Info | null> { |
| 258 | const authFile = await readJsonFile<CodexCliAuthFile>(path.join(resolveCodexHome(), "auth.json")) |
| 259 | if (!authFile) return null |
| 260 | |
| 261 | const tokens = authFile.tokens ?? undefined |
| 262 | const idToken = extractCodexIdToken(tokens?.id_token) |
| 263 | if (tokens?.access_token && tokens?.refresh_token && idToken) { |
| 264 | const idInfo = parseCodexIdToken(idToken) |
| 265 | |
| 266 | // Extract real expiry time from access token JWT instead of forcing refresh |
| 267 | let expiresAt = Date.now() + 3600 * 1000 // Default: 1 hour from now |
| 268 | try { |
| 269 | const parts = tokens.access_token.split(".") |
| 270 | if (parts.length === 3) { |
| 271 | const payloadB64 = parts[1] |
| 272 | let base64 = payloadB64.replace(/-/g, "+").replace(/_/g, "/") |
| 273 | while (base64.length % 4) { |
| 274 | base64 += "=" |
| 275 | } |
| 276 | const payloadBytes = Buffer.from(base64, "base64").toString("utf-8") |
| 277 | const payload = JSON.parse(payloadBytes) |
| 278 | if (payload.exp && typeof payload.exp === "number") { |
| 279 | expiresAt = payload.exp * 1000 // Convert Unix timestamp to milliseconds |
| 280 | } |
| 281 | } |
| 282 | } catch { |
| 283 | // If decoding fails, use default expiry |
| 284 | } |
| 285 | |
| 286 | return { |
| 287 | type: "codex", |
| 288 | accessToken: tokens.access_token, |
| 289 | refreshToken: tokens.refresh_token, |
| 290 | idToken, |
| 291 | accountId: tokens.account_id ?? idInfo?.chatgpt_account_id ?? undefined, |
| 292 | email: idInfo?.email, |
| 293 | planType: idInfo?.chatgpt_plan_type, |
| 294 | expiresAt, |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | if (authFile.OPENAI_API_KEY && authFile.OPENAI_API_KEY.trim().length > 0) { |
| 299 | return { |
| 300 | type: "api", |
| 301 | key: authFile.OPENAI_API_KEY, |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | return null |
| 306 | } |
| 307 | |
| 308 | async function loadGeminiAuth(): Promise<Info | null> { |
| 309 | const authFile = await readJsonFile<GeminiOauthFile>(path.join(os.homedir(), ".gemini", "oauth_creds.json")) |
no test coverage detected