Skip to content

Commit f88564c

Browse files
committed
fix(auth): expose needsReauth on __main__ account and mark on 401/403 (lidge-jun#327)
1 parent c35f7dc commit f88564c

2 files changed

Lines changed: 42 additions & 3 deletions

File tree

‎src/codex/auth-api.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { deleteCodexAccount } from "./account-lifecycle";
1313
import { checkAccountIdCollision, readCodexTokens } from "./auth-collision";
1414
export { checkAccountIdCollision, getMainChatgptAccountId } from "./auth-collision";
1515
export { clearAccountNeedsReauth, isAccountNeedsReauth, markAccountNeedsReauth } from "./account-runtime-state";
16-
import { clearAccountNeedsReauth, isAccountNeedsReauth } from "./account-runtime-state";
16+
import { clearAccountNeedsReauth, isAccountNeedsReauth, markAccountNeedsReauth } from "./account-runtime-state";
1717
import {
1818
clearAccountQuota,
1919
getAccountQuota,
@@ -228,7 +228,11 @@ export async function fetchMainAccountInfo(forceRefresh = false): Promise<{ emai
228228
headers: { Authorization: `Bearer ${tokens.access_token}`, "ChatGPT-Account-Id": tokens.account_id },
229229
signal: AbortSignal.timeout(8000),
230230
});
231-
if (!resp.ok) return { email: null, plan: null, quota: null };
231+
if (!resp.ok) {
232+
if (resp.status === 401 || resp.status === 403) markAccountNeedsReauth(MAIN_CODEX_ACCOUNT_ID);
233+
return { email: null, plan: null, quota: null };
234+
}
235+
clearAccountNeedsReauth(MAIN_CODEX_ACCOUNT_ID);
232236
const data = (await resp.json()) as WhamUsageResponse;
233237
const result = {
234238
email: data.email ?? null,
@@ -269,7 +273,7 @@ export interface CodexAuthAccountDto {
269273
logLabel?: string;
270274
isMain: boolean;
271275
quota: (StoredAccountQuota | (Omit<StoredAccountQuota, "updatedAt"> & { updatedAt: number })) | null;
272-
needsReauth?: boolean;
276+
needsReauth: boolean;
273277
hasCredential: boolean;
274278
}
275279

@@ -378,6 +382,7 @@ export async function listCodexAuthAccounts(config: OcxConfig, forceRefresh = fa
378382
email: maskEmail(mainInfo.email) ?? "Codex App login",
379383
plan: mainInfo.plan,
380384
isMain: true,
385+
needsReauth: isAccountNeedsReauth(MAIN_CODEX_ACCOUNT_ID),
381386
hasCredential: true,
382387
quota: mainInfo.quota ? { ...quotaForPlan({ ...mainInfo.quota, updatedAt: Date.now() }, mainInfo.plan) } : null,
383388
};

‎tests/codex-auth-api.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,13 @@ beforeEach(() => {
109109
process.env.OPENCODEX_HOME = TEST_DIR;
110110
process.env.CODEX_HOME = TEST_CODEX_HOME;
111111
delete process.env[MANUAL_IMPORT_ENV];
112+
clearAccountNeedsReauth("__main__");
112113
clearAccountQuota();
113114
clearCodexWebSocketRegistry();
114115
});
115116

116117
afterEach(() => {
118+
clearAccountNeedsReauth("__main__");
117119
clearAccountQuota();
118120
clearCodexWebSocketRegistry();
119121
globalThis.fetch = previousFetch;
@@ -138,6 +140,38 @@ describe("codex-auth API", () => {
138140
expect(main).toBeTruthy();
139141
});
140142

143+
test("BUG-R327: main account exposes and updates needsReauth from WHAM auth responses", async () => {
144+
writeFileSync(join(TEST_CODEX_HOME, "auth.json"), JSON.stringify({
145+
tokens: {
146+
access_token: "main-access",
147+
account_id: "main-account",
148+
},
149+
}));
150+
let status = 401;
151+
globalThis.fetch = (async (input: RequestInfo | URL) => {
152+
expect(String(input)).toBe("https://chatgpt.com/backend-api/wham/usage");
153+
return status === 200
154+
? new Response(JSON.stringify({ email: "[email protected]", plan_type: "plus" }), {
155+
status,
156+
headers: { "Content-Type": "application/json" },
157+
})
158+
: new Response(null, { status });
159+
}) as typeof fetch;
160+
161+
const listMain = async (): Promise<{ needsReauth: boolean }> => {
162+
const req = new Request("http://localhost/api/codex-auth/accounts?refresh=1", { method: "GET" });
163+
const resp = await handleCodexAuthAPI(req, new URL(req.url), makeConfig());
164+
const data = await resp!.json() as { accounts: Array<{ id: string; needsReauth: boolean }> };
165+
return data.accounts.find(account => account.id === "__main__")!;
166+
};
167+
168+
expect((await listMain()).needsReauth).toBe(true);
169+
status = 403;
170+
expect((await listMain()).needsReauth).toBe(true);
171+
status = 200;
172+
expect((await listMain()).needsReauth).toBe(false);
173+
});
174+
141175
test("maskEmail hides local account names", () => {
142176
expect(maskEmail("[email protected]")).toBe("*@example.test");
143177
expect(maskEmail("[email protected]")).toBe("a*@example.test");

0 commit comments

Comments
 (0)