Repository navigation
Expand file tree
/
Copy pathindex.ts
More file actions
740 lines (690 loc) · 35.4 KB
/
Copy pathindex.ts
File metadata and controls
740 lines (690 loc) · 35.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
import type { OAuthController, OAuthCredentials } from "./types";
import { parseCallbackInput } from "./callback-server";
import type { OcxConfig, OcxProviderConfig, RefreshPolicy } from "../types";
import { loadConfig, resolveEnvValue, saveConfig } from "../config";
import { maskEmail } from "../lib/privacy";
import { getAccountCredential, getAccountSet, saveAccountCredential, saveCredential, markAccountNeedsReauth, getCredential, credentialGeneration, createOAuthRefreshIntentLock, mergeAccountCredential, markAccountNeedsReauthIfGeneration, readOAuthRefreshIntent, writeOAuthRefreshIntent, clearOAuthRefreshIntent } from "./store";
import { loginXai, refreshXaiToken, XAI_LOCAL_CLI_DETACH_WARNING, XaiTokenRequestError } from "./xai";
import { ANTHROPIC_OAUTH_BETA, AnthropicTokenError, loginAnthropic, refreshAnthropicToken } from "./anthropic";
import { loginKimi, refreshKimiToken } from "./kimi";
import { loginKiro, readKiroCliSqlite, refreshKiroToken } from "./kiro";
import { loginChatGPT, refreshChatGPTToken } from "./chatgpt";
import { loginAntigravity, refreshAntigravityToken } from "./google-antigravity";
import { loginCursor, refreshCursorToken } from "./cursor";
import { loginGithubCopilot, refreshGithubCopilotToken, validateCopilotApiBaseUrl } from "./github-copilot";
import { deriveOAuthDefaultModel, deriveOAuthProviderConfig } from "../providers/derive";
import { effectiveGoogleMode } from "../providers/registry";
import { resolveProviderTransport } from "../providers/xai-transport";
import { detectClaudeCodeToken, detectGrokCliToken, hasComparableGrokIdentity, isSameGrokIdentity, shouldAdoptGrokGeneration } from "./local-token-detect";
const REFRESH_SKEW_MS = 60_000;
export interface OAuthAccessSnapshot {
provider: string;
accountId: string;
generation: string;
accessToken: string;
}
const tokenRefreshes = new Map<string, Promise<OAuthAccessSnapshot>>();
const XAI_PERMANENT_FAILURE_TTL_MS=30_000;
const permanentRefreshFailures=new Map<string,number>();
interface XaiRefreshDeps { intentLock?:ReturnType<typeof createOAuthRefreshIntentLock>; now?:()=>number; afterPrePersistRead?:()=>void|Promise<void> }
interface AnthropicRefreshDeps { intentLock?:ReturnType<typeof createOAuthRefreshIntentLock>; now?:()=>number; afterPrePersistRead?:()=>void|Promise<void> }
function verdictKey(p:string,a:string,c:OAuthCredentials){return `${p}\0${a}\0${credentialGeneration(c)}`;}
function cached(p:string,a:string,c:OAuthCredentials,now:()=>number){const k=verdictKey(p,a,c),u=permanentRefreshFailures.get(k);if(u===undefined)return false;if(u<=now()){permanentRefreshFailures.delete(k);return false;}return true;}
export interface LoginOpts { forceLogin?: boolean; /** When set, persist into this account slot and require matching identity. */ reauthAccountId?: string }
interface OAuthProviderDef {
login(ctrl: OAuthController, opts?: LoginOpts): Promise<OAuthCredentials>;
refresh(refreshToken: string, signal?: AbortSignal): Promise<OAuthCredentials>;
/** provider entry written into config.json on first login. */
providerConfig: OcxProviderConfig;
defaultModel: string;
/**
* Built-in proactive-refresh policy, risk-tiered by the provider's ToS exposure (devlog
* 260703_oauth-multi-account-refresh-and-tos). A user's per-provider `config.providers[x].refreshPolicy`
* overrides this. Default when unset here: "lazy-only".
*/
defaultRefreshPolicy?: RefreshPolicy;
}
function oauthConfig(id: string): OcxProviderConfig {
const config = deriveOAuthProviderConfig(id);
if (!config) throw new Error(`OAuth provider missing from registry: ${id}`);
return config;
}
function oauthDefaultModel(id: string): string {
const model = deriveOAuthDefaultModel(id);
if (!model) throw new Error(`OAuth provider missing default model in registry: ${id}`);
return model;
}
export const OAUTH_PROVIDERS: Record<string, OAuthProviderDef> = {
xai: {
// forceLogin skips the local grok-cli import so a SECOND account can be chosen in the browser.
login: (ctrl, opts) => loginXai(ctrl, { importLocal: opts?.forceLogin ? "off" : "fallback" }),
refresh: refreshXaiToken,
providerConfig: oauthConfig("xai"),
defaultModel: oauthDefaultModel("xai"),
},
anthropic: {
login: (ctrl, opts) => loginAnthropic(ctrl, { importLocal: opts?.forceLogin ? "off" : "fallback" }),
refresh: refreshAnthropicToken,
providerConfig: oauthConfig("anthropic"),
defaultModel: oauthDefaultModel("anthropic"),
// Anthropic actively server-side-blocks subscription OAuth outside its own clients (Feb 2026).
// Never generate background refresh traffic for it — grade 20, highest ToS risk.
defaultRefreshPolicy: "disabled",
},
kimi: {
login: (ctrl) => loginKimi(ctrl),
refresh: refreshKimiToken,
providerConfig: oauthConfig("kimi"),
defaultModel: oauthDefaultModel("kimi"),
},
kiro: {
login: (ctrl) => loginKiro(ctrl),
refresh: (rt, signal) => refreshKiroToken(rt, signal),
providerConfig: oauthConfig("kiro"),
defaultModel: oauthDefaultModel("kiro"),
},
"google-antigravity": {
login: (ctrl, opts) => loginAntigravity(ctrl, { forceAccountSelect: opts?.forceLogin === true }),
refresh: refreshAntigravityToken,
providerConfig: oauthConfig("google-antigravity"),
defaultModel: oauthDefaultModel("google-antigravity"),
},
cursor: {
login: (ctrl) => loginCursor(ctrl),
refresh: refreshCursorToken,
providerConfig: oauthConfig("cursor"),
defaultModel: oauthDefaultModel("cursor"),
},
"github-copilot": {
login: (ctrl) => loginGithubCopilot(ctrl),
refresh: (rt, signal) => refreshGithubCopilotToken(rt, signal),
providerConfig: oauthConfig("github-copilot"),
defaultModel: oauthDefaultModel("github-copilot"),
// Unofficial Copilot bridge — keep proactive traffic lazy-only (no background guardian spam).
defaultRefreshPolicy: "lazy-only",
},
chatgpt: {
login: loginChatGPT,
refresh: (rt) => refreshChatGPTToken(rt),
providerConfig: { adapter: "openai-responses", baseUrl: "https://chatgpt.com/backend-api/codex", authMode: "forward" as const },
defaultModel: "gpt-5.4",
},
};
export function isOAuthProvider(name: string): boolean {
return name in OAUTH_PROVIDERS;
}
export function isPublicOAuthProvider(name: string): boolean {
return name !== "chatgpt" && isOAuthProvider(name);
}
function isRefreshPolicy(value: unknown): value is RefreshPolicy {
return value === "proactive" || value === "lazy-only" || value === "disabled";
}
/**
* The effective proactive-refresh policy for a provider: the user's per-provider
* `config.providers[provider].refreshPolicy` if set, else the provider def's risk-tiered default,
* else "lazy-only". The guardian acts only when this resolves to "proactive".
*/
export function resolveRefreshPolicy(provider: string, config: OcxConfig): RefreshPolicy {
const override = config.providers[provider]?.refreshPolicy;
if (isRefreshPolicy(override)) return override;
const def = OAUTH_PROVIDERS[provider];
return def?.defaultRefreshPolicy ?? "lazy-only";
}
/** The discovered project id stored on an OAuth credential (Antigravity CCA), if any. */
export function getOAuthCredentialProjectId(provider: string): string | undefined {
return getCredential(provider)?.projectId;
}
/** Allowlisted Copilot API origin from the active credential, if still valid. */
export function getOAuthCredentialApiBaseUrl(provider: string): string | undefined {
return validateCopilotApiBaseUrl(getCredential(provider)?.apiBaseUrl);
}
/** Provider ids that support real OAuth login (drives the GUI's "Log in with …" buttons). */
export function listOAuthProviders(): string[] {
return Object.keys(OAUTH_PROVIDERS).filter(isPublicOAuthProvider);
}
export class UnsupportedOAuthProviderError extends Error {
constructor(provider: string) {
super(`Unsupported OAuth provider in config: ${provider}`);
this.name = "UnsupportedOAuthProviderError";
}
}
export class OAuthLoginRequiredError extends Error {
constructor(provider: string) {
super(`Not logged in to ${provider}. Run: ocx login ${provider}`);
this.name = "OAuthLoginRequiredError";
}
}
function accessSnapshot(provider: string, accountId: string, cred: OAuthCredentials): OAuthAccessSnapshot {
return {
provider,
accountId,
generation: credentialGeneration(cred),
accessToken: cred.access,
};
}
async function resolveAccessSnapshotForAccount(
provider: string,
accountId: string,
rejectedGeneration?: string,
): Promise<OAuthAccessSnapshot> {
const def = OAUTH_PROVIDERS[provider];
if (!def) throw new UnsupportedOAuthProviderError(provider);
const cred = getAccountCredential(provider, accountId);
if (!cred) throw new OAuthLoginRequiredError(provider);
const current = accessSnapshot(provider, accountId, cred);
if (rejectedGeneration !== undefined && current.generation !== rejectedGeneration) return current;
if (rejectedGeneration === undefined && cred.expires > Date.now() + REFRESH_SKEW_MS) return current;
const key = `${provider}\u0000${accountId}`;
const existing = tokenRefreshes.get(key);
if (existing) return existing;
const refresh = (async (): Promise<OAuthAccessSnapshot> => {
const accessToken = await refreshAndPersistAccessToken(provider, accountId, def, cred);
const persisted = getAccountCredential(provider, accountId);
if (!persisted) throw new OAuthLoginRequiredError(provider);
if (persisted.access !== accessToken) {
throw new Error(`OAuth refresh persisted an unexpected access token for ${provider}`);
}
return accessSnapshot(provider, accountId, persisted);
})().finally(() => {
if (tokenRefreshes.get(key) === refresh) tokenRefreshes.delete(key);
});
tokenRefreshes.set(key, refresh);
return refresh;
}
export async function getValidAccessTokenSnapshot(provider: string): Promise<OAuthAccessSnapshot> {
const set = getAccountSet(provider);
if (!set) throw new OAuthLoginRequiredError(provider);
return resolveAccessSnapshotForAccount(provider, set.activeAccountId);
}
/** Providers whose upstream-401 replay path may force a snapshot refresh. */
const FORCE_REFRESH_PROVIDERS = new Set(["xai", "github-copilot"]);
export async function forceRefreshOAuthAccessSnapshot(
rejected: OAuthAccessSnapshot,
): Promise<OAuthAccessSnapshot> {
if (!FORCE_REFRESH_PROVIDERS.has(rejected.provider)) throw new UnsupportedOAuthProviderError(rejected.provider);
return resolveAccessSnapshotForAccount(rejected.provider, rejected.accountId, rejected.generation);
}
/** Return a valid access token for the ACTIVE account, refreshing + persisting if expired. */
export async function getValidAccessToken(provider: string): Promise<string> {
return (await getValidAccessTokenSnapshot(provider)).accessToken;
}
/**
* Account-scoped token resolver (multiauth): refresh is single-flighted per
* (provider, account), and the rotated credential is persisted for THAT account only —
* a guardian refresh of a background account never switches the active account.
*/
export async function getValidAccessTokenForAccount(provider: string, accountId: string): Promise<string> {
return (await resolveAccessSnapshotForAccount(provider, accountId)).accessToken;
}
function readFreshKiroCliCredential(): OAuthCredentials | undefined {
const imported = readKiroCliSqlite();
if (!imported || imported.expires <= Date.now() + REFRESH_SKEW_MS) return undefined;
return { access: imported.access, refresh: imported.refresh, expires: imported.expires, source: "local-cli" };
}
/** Terminal refresh failures (revoked/rotated-away grants) — retrying cannot succeed. */
function isTerminalRefreshError(err: unknown): boolean {
const msg = (err instanceof Error ? err.message : String(err)).toLowerCase();
return msg.includes("invalid_grant")
|| msg.includes("refresh_token_reused")
|| msg.includes("revoked")
// GitHub Copilot refresh surfaces allowlisted OAuth codes (github-copilot.ts):
|| msg.includes("access_denied")
|| msg.includes("expired_token");
}
function terminal(error:unknown):boolean{
if(error instanceof XaiTokenRequestError)return ["invalid_grant","refresh_token_reused","revoked_token"].includes(error.oauthError??"");
if(error instanceof AnthropicTokenError)return (error.httpStatus===400||error.httpStatus===401)&&["invalid_grant","refresh_token_reused","revoked","revoked_token","refresh_token_revoked"].includes(error.oauthError??"");
return isTerminalRefreshError(error);
}
function authoritative(stored:OAuthCredentials,active:boolean,now:()=>number):OAuthCredentials{if(stored.source!=="local-cli")return stored;const disk=detectGrokCliToken();if(!disk)return stored;const allowed=isSameGrokIdentity(stored,disk)||(active&&!hasComparableGrokIdentity(stored,disk));return allowed&&shouldAdoptGrokGeneration(stored,disk,now(),REFRESH_SKEW_MS)?disk:stored;}
function merged(fresh: OAuthCredentials, previous: OAuthCredentials): OAuthCredentials {
return {
...fresh,
source: previous.source === "local-cli" ? "oauth" : fresh.source ?? previous.source ?? "oauth",
...(fresh.projectId === undefined && previous.projectId ? { projectId: previous.projectId } : {}),
...(fresh.apiBaseUrl === undefined && previous.apiBaseUrl ? { apiBaseUrl: previous.apiBaseUrl } : {}),
...(fresh.email === undefined && previous.email ? { email: previous.email } : {}),
...(fresh.accountId === undefined && previous.accountId ? { accountId: previous.accountId } : {}),
};
}
export async function refreshXaiAccountWithLock(provider:string,accountId:string,def:OAuthProviderDef,callerCredential:OAuthCredentials,deps:XaiRefreshDeps={}):Promise<string>{const now=deps.now??Date.now;const guard=await(deps.intentLock??createOAuthRefreshIntentLock(provider,accountId)).acquire();try{const stored=getAccountCredential(provider,accountId);if(!stored)throw new OAuthLoginRequiredError(provider);const active=getAccountSet(provider)?.activeAccountId===accountId,candidate=authoritative(stored,active,now);if(credentialGeneration(candidate)!==credentialGeneration(callerCredential)&&candidate.expires>now()+REFRESH_SKEW_MS){if(credentialGeneration(candidate)!==credentialGeneration(stored)){const o=await mergeAccountCredential(provider,accountId,candidate,{expectedGeneration:credentialGeneration(stored),afterPrePersistRead:deps.afterPrePersistRead});if(o.superseded){if(o.stored.expires>now()+REFRESH_SKEW_MS)return o.stored.access;throw new OAuthLoginRequiredError(provider);}}return candidate.access;}if(cached(provider,accountId,candidate,now))throw new OAuthLoginRequiredError(provider);const generation=credentialGeneration(candidate);try{const fresh=merged(await def.refresh(candidate.refresh),candidate);const o=await mergeAccountCredential(provider,accountId,fresh,{expectedGeneration:generation,afterPrePersistRead:deps.afterPrePersistRead});if(o.superseded){if(o.stored.expires>now()+REFRESH_SKEW_MS)return o.stored.access;throw new OAuthLoginRequiredError(provider);}permanentRefreshFailures.delete(verdictKey(provider,accountId,candidate));if(candidate.source==="local-cli")console.warn(XAI_LOCAL_CLI_DETACH_WARNING);return fresh.access;}catch(error){if(!terminal(error))throw error;permanentRefreshFailures.set(verdictKey(provider,accountId,candidate),now()+XAI_PERMANENT_FAILURE_TTL_MS);await markAccountNeedsReauthIfGeneration(provider,accountId,generation);throw new OAuthLoginRequiredError(provider);}}finally{guard.release();}}
function newerClaudeCredential(stored: OAuthCredentials, now: number): OAuthCredentials | undefined {
if (stored.source !== "local-cli") return undefined;
const disk = detectClaudeCodeToken();
if (!disk || disk.expires <= now + REFRESH_SKEW_MS) return undefined;
return credentialGeneration(disk) !== credentialGeneration(stored) ? disk : undefined;
}
export async function refreshAnthropicAccountWithLock(
provider: string,
accountId: string,
def: OAuthProviderDef,
callerCredential: OAuthCredentials,
deps: AnthropicRefreshDeps = {},
): Promise<string> {
const now = deps.now ?? Date.now;
const guard = await (deps.intentLock ?? createOAuthRefreshIntentLock(provider, accountId)).acquire();
try {
const stored = getAccountCredential(provider, accountId);
if (!stored) throw new OAuthLoginRequiredError(provider);
const account = getAccountSet(provider)?.accounts.find(candidate => candidate.id === accountId);
const generation = credentialGeneration(stored);
const pendingIntent = readOAuthRefreshIntent(provider, accountId);
const disk = newerClaudeCredential(stored, now());
if (disk) {
const outcome = await mergeAccountCredential(provider, accountId, disk, {
expectedGeneration: credentialGeneration(stored),
afterPrePersistRead: deps.afterPrePersistRead,
});
if (outcome.superseded) {
if (pendingIntent) clearOAuthRefreshIntent(provider, accountId, pendingIntent.generation);
if (outcome.stored.expires > now() + REFRESH_SKEW_MS) return outcome.stored.access;
throw new OAuthLoginRequiredError(provider);
}
if (pendingIntent) clearOAuthRefreshIntent(provider, accountId, pendingIntent.generation);
F438
div>
return disk.access;
}
if (pendingIntent?.uncertain || pendingIntent?.generation === generation) {
await markAccountNeedsReauthIfGeneration(provider, accountId, generation);
throw new OAuthLoginRequiredError(provider);
}
if (pendingIntent) clearOAuthRefreshIntent(provider, accountId, pendingIntent.generation);
if (account?.needsReauth) {
throw new OAuthLoginRequiredError(provider);
}
if (credentialGeneration(stored) !== credentialGeneration(callerCredential) && stored.expires > now() + REFRESH_SKEW_MS) {
return stored.access;
}
try {
writeOAuthRefreshIntent(provider, accountId, generation, now());
const fresh = merged(await def.refresh(stored.refresh), stored);
const outcome = await mergeAccountCredential(provider, accountId, fresh, {
expectedGeneration: generation,
afterPrePersistRead: deps.afterPrePersistRead,
});
if (outcome.superseded) {
clearOAuthRefreshIntent(provider, accountId, generation);
if (outcome.stored.expires > now() + REFRESH_SKEW_MS) return outcome.stored.access;
throw new OAuthLoginRequiredError(provider);
}
clearOAuthRefreshIntent(provider, accountId, generation);
return fresh.access;
} catch (error) {
if (!terminal(error)) throw error;
await markAccountNeedsReauthIfGeneration(provider, accountId, generation);
clearOAuthRefreshIntent(provider, accountId, generation);
throw new OAuthLoginRequiredError(provider);
}
} finally {
guard.release();
}
}
async function refreshAndPersistAccessToken(
provider: string,
accountId: string,
def: OAuthProviderDef,
cred: OAuthCredentials,
): Promise<string> {
// Local-CLI import fallback only for the ACTIVE account: importing another identity's
// token under a background account id would silently contaminate that account.
const isActive = getAccountSet(provider)?.activeAccountId === accountId;
if (provider === "kiro" && isActive) {
const imported = readFreshKiroCliCredential();
if (imported) {
await saveCredential(provider, imported);
return imported.access;
}
}
if (provider === "xai") return refreshXaiAccountWithLock(provider, accountId, def, cred);
if (provider === "anthropic") return refreshAnthropicAccountWithLock(provider, accountId, def, cred);
try {
const fresh = await def.refresh(cred.refresh);
const detachedLocalCli = provider === "xai" && cred.source === "local-cli";
if (detachedLocalCli) console.warn(XAI_LOCAL_CLI_DETACH_WARNING);
// Persist to THIS account (rotation-safe: new refresh token hits disk before use) without
// touching activeAccountId.
await saveAccountCredential(provider, accountId, {
...fresh,
source: detachedLocalCli ? "oauth" : fresh.source ?? cred.source ?? "oauth",
// Preserve a previously-discovered project id when a refresh-time re-discovery comes back empty
// (e.g. a transient network blip), so Antigravity does not lose its CCA project across refresh.
...(fresh.projectId === undefined && cred.projectId ? { projectId: cred.projectId } : {}),
...(fresh.apiBaseUrl === undefined && cred.apiBaseUrl ? { apiBaseUrl: cred.apiBaseUrl } : {}),
// Preserve identity fields the refresh response may omit, so identity matching stays stable.
...(fresh.email === undefined && cred.email ? { email: cred.email } : {}),
...(fresh.accountId === undefined && cred.accountId ? { accountId: cred.accountId } : {}),
});
return fresh.access;
} catch (err) {
if (provider === "kiro" && isActive) {
const imported = readFreshKiroCliCredential();
if (imported) {
await saveCredential(provider, imported);
return imported.access;
}
}
if (isTerminalRefreshError(err)) {
await markAccountNeedsReauth(provider, accountId, true);
throw new OAuthLoginRequiredError(provider);
}
throw err;
}
}
/**
* Shared bearer-token resolver for /models listing — used by BOTH server.ts:fetchAllModels and
* codex-catalog.ts:fetchProviderModels so OAuth providers' models are listed once logged in.
* Returns undefined for forward-mode or oauth-not-logged-in (caller skips).
*/
export async function resolveModelsAuthToken(name: string, prov: OcxProviderConfig): Promise<string | undefined> {
if (prov.authMode === "forward") return undefined;
if (prov.authMode === "oauth") {
try {
return await getValidAccessToken(name);
} catch {
return undefined;
}
}
return resolveEnvValue(prov.apiKey);
}
/**
* Provider-correct `GET /models` request (URL + headers), so both model-listing paths fetch the
* LIVE catalog correctly per adapter. Anthropic is the special case: its endpoint is `/v1/models`
* (not `/models`), it needs `anthropic-version`, and it authenticates with `x-api-key` (key) or
* `Authorization: Bearer` + the OAuth beta (oauth) — not a bare Bearer. Google (ai-studio mode)
* is the other special case: `x-goog-api-key` + `/v1beta/models`, returning `{ models: [...] }`.
* The catalog authority gate intentionally degrades that non-OpenAI shape to stale/static data.
* Everyone else uses the OpenAI-style `/models` + Bearer with a `{ data: [{ id, owned_by? }] }`
* response.
*/
export function buildModelsRequest(prov: OcxProviderConfig, apiKey: string | undefined, providerName = ""): { url: string; headers: Record<string, string> } {
const effectiveProvider = resolveProviderTransport(
providerName,
prov,
undefined,
providerName === "github-copilot" ? getOAuthCredentialApiBaseUrl(providerName) : undefined,
);
const headers: Record<string, string> = { ...(effectiveProvider.headers ?? {}) };
if (effectiveGoogleMode(providerName, effectiveProvider) === "ai-studio") {
// Generative Language API: API key goes in x-goog-api-key (never Authorization: Bearer),
// models live under /v1beta (v1 misses preview models), and pageSize maxes at 1000 —
// enough to list everything without a pageToken loop. Vertex/antigravity keep the
// generic branch (they fall back to their static model lists).
if (apiKey) headers["x-goog-api-key"] = apiKey;
return { url: `${effectiveProvider.baseUrl}/v1beta/models?pageSize=1000`, headers };
}
if (effectiveProvider.adapter === "anthropic") {
headers["anthropic-version"] = "2023-06-01";
if (effectiveProvider.authMode === "oauth") {
headers["anthropic-beta"] = ANTHROPIC_OAUTH_BETA;
if (apiKey) headers["Authorization"] = `Bearer ${apiKey}`;
} else if (apiKey) {
headers["x-api-key"] = apiKey;
}
return { url: `${effectiveProvider.baseUrl}/v1/models?limit=1000`, headers };
}
if (apiKey) headers["Authorization"] = `Bearer ${apiKey}`;
return { url: `${effectiveProvider.baseUrl}/models`, headers };
}
/**
* Refresh OAuth-managed provider presets (`models`, `noReasoningModels`, and a stale `defaultModel`)
* from the registry so a proxy update that revises a provider's models — e.g. dropping deprecated
* Claude snapshots or adding a new grok endpoint not in the live `/models` — reaches EXISTING
* configs on the next `ocx start`, instead of only fresh installs. The live `/models` fetch stays
* the primary source; this keeps the static fallback (and models-not-in-/models) current.
*
* Only touches providers that are registry-managed AND still `authMode: "oauth"`, and only the
* preset fields (never apiKey/baseUrl/user toggles). Persists + returns true when anything changed.
*/
function cloneProviderField(value: unknown): unknown {
if (Array.isArray(value)) return [...value];
if (value && typeof value === "object") return JSON.parse(JSON.stringify(value));
return value;
}
const OAUTH_RECONCILE_FIELDS: (keyof OcxProviderConfig)[] = [
"models",
"contextWindow",
"modelContextWindows",
"modelInputModalities",
"noReasoningModels",
"noVisionModels",
"reasoningEfforts",
"modelReasoningEfforts",
"reasoningEffortMap",
"modelReasoningEffortMap",
"noTemperatureModels",
"noTopPModels",
"noPenaltyModels",
"autoToolChoiceOnlyModels",
"preserveReasoningContentModels",
];
export function reconcileOAuthProviders(config: OcxConfig): boolean {
let changed = false;
for (const [name, prov] of Object.entries(config.providers)) {
const def = OAUTH_PROVIDERS[name];
if (!def || prov.authMode !== "oauth") continue;
const preset = def.providerConfig;
for (const field of OAUTH_RECONCILE_FIELDS) {
if (JSON.stringify(prov[field]) === JSON.stringify(preset[field])) continue;
if (preset[field] !== undefined) {
prov[field] = cloneProviderField(preset[field]) as never;
} else {
delete prov[field];
}
changed = true;
}
// Heal a defaultModel that no longer exists in the refreshed list (e.g. a deprecated snapshot).
if (prov.defaultModel && preset.defaultModel && !(prov.models ?? []).includes(prov.defaultModel)) {
prov.defaultModel = preset.defaultModel;
changed = true;
}
}
if (changed) saveConfig(config);
return changed;
}
/** Add/refresh an OAuth provider's config entry on a config object (does not persist). */
export function upsertOAuthProvider(config: OcxConfig, provider: string): void {
if (provider === "chatgpt") return;
const def = OAUTH_PROVIDERS[provider];
if (!def) return;
config.providers[provider] = { ...def.providerConfig };
}
/** Run the login flow, persist the credential + upsert the provider entry to disk, return cred. */
export async function runLogin(provider: string, ctrl: OAuthController, opts?: LoginOpts): Promise<OAuthCredentials> {
const def = OAUTH_PROVIDERS[provider];
if (!def) throw new UnsupportedOAuthProviderError(provider);
const rawCred = await def.login(ctrl, opts);
const cred: OAuthCredentials = rawCred.source ? rawCred : { ...rawCred, source: "oauth" };
if (opts?.reauthAccountId) {
const existing = getAccountCredential(provider, opts.reauthAccountId);
if (!existing) throw new Error(`Unknown account for reauth: ${opts.reauthAccountId}`);
const expected = existing.accountId ?? existing.email;
const got = cred.accountId ?? cred.email;
if (!expected) {
throw new Error("Could not verify signed-in account identity for reauth.");
}
if (!got || expected !== got) {
throw new Error("Signed-in account does not match the selected account. Sign in with the same account.");
}
await saveAccountCredential(provider, opts.reauthAccountId, cred);
} else {
await saveCredential(provider, cred);
}
if (provider === "chatgpt") return cred;
const config = loadConfig();
upsertOAuthProvider(config, provider);
saveConfig(config);
return cred;
}
/**
* GUI async login: start the flow, return the auth URL EARLY (the flow keeps running in the
* background until the callback server captures the redirect), with a concurrency guard and an
* error surfaced via getLoginStatus().
*
* Manual fallback: when the browser cannot reach the loopback callback (remote GUI, SSH, blocked
* localhost), the GUI can POST the final redirect URL or authorization code via
* submitManualLoginCode(), which feeds OAuthController.onManualCodeInput.
*/
const loginState = new Map<string, { error?: string; done: boolean }>();
const loginAbort = new Map<string, AbortController>();
/** Pending paste for a login in progress: either a waiter or a stashed early submission. */
interface ManualCodeSlot {
pendingInput?: string;
resolve?: (value: string) => void;
/** Registered by the callback flow so submits can validate state synchronously. */
expectedState?: string;
}
const loginManual = new Map<string, ManualCodeSlot>();
function clearManualCodeSlot(provider: string): void {
loginManual.delete(provider);
}
function ensureManualCodeSlot(provider: string): ManualCodeSlot {
let slot = loginManual.get(provider);
if (!slot) {
slot = {};
loginManual.set(provider, slot);
}
return slot;
}
/** Wait for a GUI/CLI paste of the OAuth redirect URL or code (or return a stashed early submit). */
function waitForManualLoginCode(provider: string, signal: AbortSignal, expectedState?: string): Promise<string> {
if (signal.aborted) {
return Promise.reject(new Error(`OAuth callback cancelled: ${signal.reason}`));
}
const slot = ensureManualCodeSlot(provider);
if (expectedState !== undefined) slot.expectedState = expectedState;
if (slot.pendingInput !== undefined) {
const value = slot.pendingInput;
slot.pendingInput = undefined;
return Promise.resolve(value);
}
return new Promise<string>((resolve, reject) => {
const => {
if (slot.resolve === resolve) slot.resolve = undefined;
reject(new Error(`OAuth callback cancelled: ${signal.reason}`));
};
signal.addEventListener("abort", onAbort, { once: true });
slot.resolve = (value: string) => {
signal.removeEventListener("abort", onAbort);
if (slot.resolve === resolve) slot.resolve = undefined;
resolve(value);
};
});
}
/**
* Feed a pasted redirect URL or authorization code into an in-progress GUI login.
* Returns ok:false when no login is waiting (or input is empty). Invalid pastes are accepted
* here and re-prompted by the OAuth callback loop if they cannot be parsed / fail state checks.
*/
export function submitManualLoginCode(provider: string, input: string): { ok: true } | { ok: false; error: string } {
const trimmed = input.trim();
if (!trimmed) return { ok: false, error: "empty code" };
const st = loginState.get(provider);
if (!st || st.done) return { ok: false, error: "no login in progress" };
const slot = ensureManualCodeSlot(provider);
// Synchronous validation (validated request/ack): reject un-parseable input and
// authorization responses (url/query kind) whose state is missing or mismatched
// once the flow has registered its expected state. Raw codes stay in-session-PKCE
// protected. Early posts (flow not yet waiting, no expectedState) are stashed and
// re-validated by the callback loop.
const parsed = parseCallbackInput(trimmed);
if (!parsed.code) return { ok: false, error: "no authorization code found in input" };
if (parsed.kind !== "raw" && slot.expectedState !== undefined) {
if (parsed.state === undefined) return { ok: false, error: "redirect URL is missing the state parameter" };
if (parsed.state !== slot.expectedState) return { ok: false, error: "state mismatch — paste the redirect URL from THIS login attempt" };
}
if (slot.resolve) {
const resolve = slot.resolve;
slot.resolve = undefined;
resolve(trimmed);
} else {
// Race: GUI may POST before the flow reaches onManualCodeInput — stash for the waiter.
slot.pendingInput = trimmed;
}
return { ok: true };
}
export interface OAuthAccountSummary { id: string; alias?: string; email?: string; active: boolean; needsReauth?: boolean; expiresAt?: number }
export function getLoginStatus(provider: string): { loggedIn: boolean; email?: string; source?: OAuthCredentials["source"]; error?: string; done: boolean; activeAccountId?: string; accounts?: OAuthAccountSummary[] } {
const cred = getCredential(provider);
const st = loginState.get(provider);
const set = getAccountSet(provider);
const accounts: OAuthAccountSummary[] | undefined = set?.accounts.map(a => ({
id: a.id,
...(a.alias ? { alias: a.alias } : {}),
email: maskEmail(a.credential.email) ?? undefined,
active: a.id === set.activeAccountId,
...(a.needsReauth ? { needsReauth: true } : {}),
expiresAt: a.credential.expires,
}));
return {
loggedIn: !!cred,
email: maskEmail(cred?.email) ?? undefined,
source: cred?.source,
error: st?.error,
done: st?.done ?? false,
...(set ? { activeAccountId: set.activeAccountId, accounts } : {}),
};
}
/** Token-safe per-provider login state for the CLI `ocx status` logins section (no tokens, masked email). */
export function oauthLoginSummary(): Array<{ provider: string; loggedIn: boolean; email?: string }> {
return listOAuthProviders().map(provider => {
const status = getLoginStatus(provider);
return { provider, loggedIn: status.loggedIn, ...(status.email ? { email: status.email } : {}) };
});
}
export function clearLoginState(provider: string): void {
loginAbort.get(provider)?.abort("cleared");
loginAbort.delete(provider);
clearManualCodeSlot(provider);
loginState.delete(provider);
}
export function cancelLoginFlow(provider: string): boolean {
const ctrl = loginAbort.get(provider);
const existing = loginState.get(provider);
if (!ctrl && (!existing || existing.done)) return false;
ctrl?.abort("cancelled");
loginAbort.delete(provider);
clearManualCodeSlot(provider);
loginState.set(provider, { done: true, error: "Login cancelled" });
return true;
}
export async function startLoginFlow(provider: string, opts?: LoginOpts): Promise<{ url: string; instructions?: string; deviceCode?: string }> {
const def = OAUTH_PROVIDERS[provider];
if (!def) throw new UnsupportedOAuthProviderError(provider);
const existing = loginState.get(provider);
if (existing && !existing.done) {
throw new Error(`A login for ${provider} is already in progress`);
}
clearManualCodeSlot(provider);
loginState.set(provider, { done: false });
const abort = new AbortController();
loginAbort.set(provider, abort);
return new Promise((resolve, reject) => {
let urlResolved = false;
const ctrl: OAuthController = {
onAuth: ({ url, instructions, deviceCode }) => {
urlResolved = true;
resolve({ url, instructions, deviceCode });
},
onProgress: () => {},
// GUI fallback when the browser cannot hit the loopback callback server.
onManualCodeInput: (expectedState?: string) => waitForManualLoginCode(provider, abort.signal, expectedState),
signal: abort.signal,
};
// Background: runLogin persists the credential + upserts the provider entry to disk config.
runLogin(provider, ctrl, opts)
.then(() => {
loginAbort.delete(provider);
clearManualCodeSlot(provider);
loginState.set(provider, { done: true });
// Local-token import (grok-cli / Claude Code keychain) completes WITHOUT firing onAuth —
// resolve so the GUI call returns instead of hanging.
if (!urlResolved) resolve({ url: "", instructions: "Logged in via an existing local CLI/keychain token — no browser needed." });
})
.catch((e: unknown) => {
loginAbort.delete(provider);
clearManualCodeSlot(provider);
const msg = e instanceof Error ? e.message : String(e);
loginState.set(provider, { done: true, error: msg });
if (!urlResolved) reject(e);
});
});
}