-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix(codex): handle nested error object in token refresh and classify refresh_token_invalidated as revoked #4737
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1155,6 +1155,82 @@ describe("codex-account-store CRUD", () => { | |
| } | ||
| }); | ||
|
|
||
| test("nested error object with refresh_token_invalidated classifies as revoked", async () => { | ||
| const { forceRefreshCodexPoolToken, readCodexAccountRecord, saveCodexAccountCredential, TokenRefreshError } = | ||
| await import("../../src/codex/account-store"); | ||
| saveCodexAccountCredential("invalidated-grant", { | ||
| accessToken: "rejected", | ||
| refreshToken: "grant", | ||
| expiresAt: Date.now() + 3600_000, | ||
| chatgptAccountId: "acc", | ||
| }); | ||
| const generation = readCodexAccountRecord("invalidated-grant")!.generation; | ||
| const originalFetch = globalThis.fetch; | ||
| globalThis.fetch = (async () => | ||
| Response.json( | ||
| { | ||
| error: { | ||
| message: "Your session has ended. Please log in again.", | ||
| type: "invalid_request_error", | ||
| param: null, | ||
| code: "refresh_token_invalidated", | ||
| }, | ||
| }, | ||
| { status: 401 }, | ||
| )) as typeof fetch; | ||
|
|
||
| try { | ||
| await forceRefreshCodexPoolToken("invalidated-grant", { | ||
| rejectedGeneration: generation, | ||
| rejectedAccessToken: "rejected", | ||
| }); | ||
| throw new Error("expected a TokenRefreshError"); | ||
| } catch (error) { | ||
| expect(error).toBeInstanceOf(TokenRefreshError); | ||
| expect((error as InstanceType<typeof TokenRefreshError>).reason).toBe("revoked"); | ||
| } finally { | ||
| globalThis.fetch = originalFetch; | ||
| } | ||
| }); | ||
|
|
||
| test("nested error object with refresh_token_expired classifies as expired", async () => { | ||
| const { forceRefreshCodexPoolToken, readCodexAccountRecord, saveCodexAccountCredential, TokenRefreshError } = | ||
| await import("../../src/codex/account-store"); | ||
| saveCodexAccountCredential("expired-grant", { | ||
| accessToken: "rejected", | ||
| refreshToken: "grant", | ||
| expiresAt: Date.now() + 3600_000, | ||
| chatgptAccountId: "acc", | ||
| }); | ||
| const generation = readCodexAccountRecord("expired-grant")!.generation; | ||
| const originalFetch = globalThis.fetch; | ||
| globalThis.fetch = (async () => | ||
| Response.json( | ||
| { | ||
| error: { | ||
| message: "The refresh token has expired.", | ||
| type: "invalid_request_error", | ||
| param: null, | ||
| code: "refresh_token_expired", | ||
| }, | ||
| }, | ||
| { status: 401 }, | ||
| )) as typeof fetch; | ||
|
|
||
| try { | ||
| await forceRefreshCodexPoolToken("expired-grant", { | ||
| rejectedGeneration: generation, | ||
| rejectedAccessToken: "rejected", | ||
| }); | ||
| throw new Error("expected a TokenRefreshError"); | ||
| } catch (error) { | ||
| expect(error).toBeInstanceOf(TokenRefreshError); | ||
| expect((error as InstanceType<typeof TokenRefreshError>).reason).toBe("expired"); | ||
| } finally { | ||
| globalThis.fetch = originalFetch; | ||
| } | ||
| }); | ||
|
|
||
| test("a replacement landing mid-refresh is not reported as this call's own lineage (#2887 review)", async () => { | ||
| // `selfRefreshed` is what gates the affinity handoff. An external replacement must not | ||
| // set it: that credential may be a different upstream identity, so inheriting the | ||
|
Comment on lines
1155
to
1236
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win Add coverage for 🤖 Prompt for AI Agents |
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.