(provider)
| 3393 | } |
| 3394 | |
| 3395 | function importCurrentAuthToPool(provider) { |
| 3396 | if (provider === 'google') { |
| 3397 | return importCurrentGoogleAuthToPool(); |
| 3398 | } |
| 3399 | |
| 3400 | const authCfg = loadAuthConfig(); |
| 3401 | if (!authCfg || !authCfg[provider]) return; |
| 3402 | |
| 3403 | const creds = authCfg[provider]; |
| 3404 | let email = creds.email; |
| 3405 | |
| 3406 | if (!email && provider === 'openai' && creds.access) { |
| 3407 | const decoded = decodeJWT(creds.access); |
| 3408 | if (decoded && decoded['https://api.openai.com/profile']?.email) { |
| 3409 | email = decoded['https://api.openai.com/profile'].email; |
| 3410 | } |
| 3411 | } |
| 3412 | |
| 3413 | const name = email || creds.accountId || creds.id || 'primary'; |
| 3414 | const profileDir = path.join(AUTH_PROFILES_DIR, provider); |
| 3415 | if (!fs.existsSync(profileDir)) fs.mkdirSync(profileDir, { recursive: true }); |
| 3416 | |
| 3417 | const profilePath = path.join(profileDir, `${name}.json`); |
| 3418 | |
| 3419 | let shouldSync = true; |
| 3420 | if (fs.existsSync(profilePath)) { |
| 3421 | try { |
| 3422 | const current = JSON.parse(fs.readFileSync(profilePath, 'utf8')); |
| 3423 | if (JSON.stringify(current) === JSON.stringify(creds)) { |
| 3424 | shouldSync = false; |
| 3425 | } |
| 3426 | } catch { |
| 3427 | } |
| 3428 | } |
| 3429 | |
| 3430 | if (shouldSync) { |
| 3431 | console.log(`[Auth] Syncing ${provider} login for ${name} to pool.`); |
| 3432 | atomicWriteFileSync(profilePath, JSON.stringify(creds, null, 2)); |
| 3433 | |
| 3434 | const metadata = loadPoolMetadata(); |
| 3435 | if (!metadata[provider]) metadata[provider] = {}; |
| 3436 | |
| 3437 | metadata[provider][name] = { |
| 3438 | ...(metadata[provider][name] || {}), |
| 3439 | email: email || null, |
| 3440 | createdAt: metadata[provider][name]?.createdAt || Date.now(), |
| 3441 | lastUsed: Date.now(), |
| 3442 | usageCount: metadata[provider][name]?.usageCount || 0, |
| 3443 | imported: true |
| 3444 | }; |
| 3445 | savePoolMetadata(metadata); |
| 3446 | |
| 3447 | const studio = loadStudioConfig(); |
| 3448 | if (!studio.activeProfiles) studio.activeProfiles = {}; |
| 3449 | if (studio.activeProfiles[provider] !== name) { |
| 3450 | studio.activeProfiles[provider] = name; |
| 3451 | saveStudioConfig(studio); |
| 3452 | } |
no test coverage detected