| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528 |
- import { afterEach, describe, expect, it, vi } from "vitest";
- import {
- loginOpenAICodex,
- loginOpenAICodexDeviceCode,
- openaiCodexOAuthProvider,
- refreshOpenAICodexToken,
- } from "../src/utils/oauth/openai-codex.ts";
- function jsonResponse(body: unknown, status: number = 200): Response {
- return new Response(JSON.stringify(body), {
- status,
- headers: { "Content-Type": "application/json" },
- });
- }
- function getUrl(input: unknown): string {
- if (typeof input === "string") return input;
- if (input instanceof URL) return input.toString();
- if (input instanceof Request) return input.url;
- throw new Error(`Unsupported fetch input: ${String(input)}`);
- }
- function createAccessToken(accountId: string): string {
- const header = Buffer.from(JSON.stringify({ alg: "none" })).toString("base64");
- const payload = Buffer.from(
- JSON.stringify({
- "https://api.openai.com/auth": {
- chatgpt_account_id: accountId,
- },
- }),
- ).toString("base64");
- return `${header}.${payload}.signature`;
- }
- function deviceAuthPendingResponse(): Response {
- return jsonResponse(
- {
- error: {
- message: "Device authorization is pending. Please try again.",
- type: "invalid_request_error",
- param: null,
- code: "deviceauth_authorization_pending",
- },
- },
- 403,
- );
- }
- describe("OpenAI Codex OAuth", () => {
- afterEach(() => {
- vi.restoreAllMocks();
- vi.unstubAllGlobals();
- vi.useRealTimers();
- });
- it("uses the injected fetch for browser authorization code exchange", async () => {
- const accessToken = createAccessToken("account-browser");
- let authorizationUrl = "";
- const injectedFetch = vi.fn(async (input: unknown, init?: RequestInit): Promise<Response> => {
- expect(getUrl(input)).toBe("https://auth.openai.com/oauth/token");
- expect(init?.method).toBe("POST");
- expect(init?.headers).toMatchObject({ "Content-Type": "application/x-www-form-urlencoded" });
- const params = new URLSearchParams(String(init?.body));
- expect(params.get("grant_type")).toBe("authorization_code");
- expect(params.get("code")).toBe("oauth-code");
- return jsonResponse({
- access_token: accessToken,
- refresh_token: "refresh-token",
- expires_in: 3600,
- });
- });
- const globalFetch = vi.fn(async () => {
- throw new Error("global fetch should not be used");
- });
- vi.stubGlobal("fetch", globalFetch);
- await expect(
- loginOpenAICodex({
- onAuth: ({ url }) => {
- authorizationUrl = url;
- },
- onPrompt: async () => {
- throw new Error("Prompt should not be used");
- },
- onManualCodeInput: async () => {
- const state = new URL(authorizationUrl).searchParams.get("state");
- if (!state) throw new Error("Authorization state was not provided");
- return `http://localhost:1455/auth/callback?code=oauth-code&state=${state}`;
- },
- onSelect: async () => "browser",
- fetch: injectedFetch as typeof fetch,
- }),
- ).resolves.toMatchObject({
- access: accessToken,
- refresh: "refresh-token",
- accountId: "account-browser",
- });
- expect(injectedFetch).toHaveBeenCalledTimes(1);
- expect(globalFetch).not.toHaveBeenCalled();
- });
- it("logs in with the OpenAI Codex device code flow", async () => {
- vi.useFakeTimers();
- const startTime = new Date("2026-05-20T00:00:00Z");
- vi.setSystemTime(startTime);
- const accessToken = createAccessToken("account-123");
- const deviceInfos: Array<{
- userCode: string;
- verificationUri: string;
- instructions?: string;
- intervalSeconds?: number;
- expiresInSeconds?: number;
- }> = [];
- const pollTimes: number[] = [];
- const pollResponses = [
- deviceAuthPendingResponse(),
- jsonResponse({
- authorization_code: "oauth-code",
- code_challenge: "device-code-challenge",
- code_verifier: "device-code-verifier",
- }),
- ];
- const fetchMock = vi.fn(async (input: unknown, init?: RequestInit): Promise<Response> => {
- const url = getUrl(input);
- if (url === "https://auth.openai.com/api/accounts/deviceauth/usercode") {
- expect(init?.method).toBe("POST");
- expect(init?.headers).toMatchObject({ "Content-Type": "application/json" });
- expect(JSON.parse(String(init?.body))).toEqual({ client_id: "app_EMoamEEZ73f0CkXaXp7hrann" });
- return jsonResponse({
- device_auth_id: "device-auth-id",
- user_code: "ABCD-1234",
- interval: "5",
- });
- }
- if (url === "https://auth.openai.com/api/accounts/deviceauth/token") {
- pollTimes.push(Date.now());
- expect(init?.method).toBe("POST");
- expect(init?.headers).toMatchObject({ "Content-Type": "application/json" });
- expect(JSON.parse(String(init?.body))).toEqual({
- device_auth_id: "device-auth-id",
- user_code: "ABCD-1234",
- });
- const response = pollResponses.shift();
- if (!response) {
- throw new Error("Unexpected extra device auth poll");
- }
- return response;
- }
- if (url === "https://auth.openai.com/oauth/token") {
- expect(init?.method).toBe("POST");
- expect(init?.headers).toMatchObject({ "Content-Type": "application/x-www-form-urlencoded" });
- const params = new URLSearchParams(String(init?.body));
- expect(params.get("grant_type")).toBe("authorization_code");
- expect(params.get("client_id")).toBe("app_EMoamEEZ73f0CkXaXp7hrann");
- expect(params.get("code")).toBe("oauth-code");
- expect(params.get("redirect_uri")).toBe("https://auth.openai.com/deviceauth/callback");
- expect(params.get("code_verifier")).toBe("device-code-verifier");
- return jsonResponse({
- access_token: accessToken,
- refresh_token: "refresh-token",
- expires_in: 3600,
- });
- }
- throw new Error(`Unexpected fetch URL: ${url}`);
- });
- vi.stubGlobal("fetch", fetchMock);
- const credentialsPromise = loginOpenAICodexDeviceCode({
- onDeviceCode: (info) => deviceInfos.push(info),
- });
- for (let i = 0; i < 5 && pollTimes.length === 0; i++) {
- await vi.advanceTimersByTimeAsync(0);
- }
- expect(deviceInfos).toEqual([
- {
- userCode: "ABCD-1234",
- verificationUri: "https://auth.openai.com/codex/device",
- intervalSeconds: 5,
- expiresInSeconds: 900,
- },
- ]);
- expect(pollTimes).toEqual([startTime.getTime()]);
- await vi.advanceTimersByTimeAsync(4999);
- expect(pollTimes).toEqual([startTime.getTime()]);
- await vi.advanceTimersByTimeAsync(1);
- await expect(credentialsPromise).resolves.toMatchObject({
- access: accessToken,
- refresh: "refresh-token",
- expires: startTime.getTime() + 5000 + 3600 * 1000,
- accountId: "account-123",
- });
- expect(pollTimes).toEqual([startTime.getTime(), startTime.getTime() + 5000]);
- });
- it("offers browser login first and uses the selected OpenAI Codex device code flow", async () => {
- const accessToken = createAccessToken("account-456");
- const selectPrompts: Array<{
- message: string;
- options: Array<{ id: string; label: string }>;
- }> = [];
- const deviceInfos: Array<{
- userCode: string;
- verificationUri: string;
- intervalSeconds?: number;
- expiresInSeconds?: number;
- }> = [];
- vi.stubGlobal(
- "fetch",
- vi.fn(async (input: unknown, init?: RequestInit): Promise<Response> => {
- const url = getUrl(input);
- if (url === "https://auth.openai.com/api/accounts/deviceauth/usercode") {
- expect(JSON.parse(String(init?.body))).toEqual({ client_id: "app_EMoamEEZ73f0CkXaXp7hrann" });
- return jsonResponse({
- device_auth_id: "device-auth-id",
- user_code: "WXYZ-7890",
- interval: "5",
- });
- }
- if (url === "https://auth.openai.com/api/accounts/deviceauth/token") {
- return jsonResponse({
- authorization_code: "oauth-code",
- code_challenge: "device-code-challenge",
- code_verifier: "device-code-verifier",
- });
- }
- if (url === "https://auth.openai.com/oauth/token") {
- return jsonResponse({
- access_token: accessToken,
- refresh_token: "refresh-token",
- expires_in: 3600,
- });
- }
- throw new Error(`Unexpected fetch URL: ${url}`);
- }),
- );
- await expect(
- openaiCodexOAuthProvider.login({
- onAuth: () => {
- throw new Error("Browser login should not start");
- },
- onDeviceCode: (info) => deviceInfos.push(info),
- onPrompt: async () => {
- throw new Error("Prompt should not be used");
- },
- onSelect: async (prompt) => {
- selectPrompts.push(prompt);
- return "device_code";
- },
- }),
- ).resolves.toMatchObject({
- access: accessToken,
- refresh: "refresh-token",
- accountId: "account-456",
- });
- expect(selectPrompts).toEqual([
- {
- message: "Select OpenAI Codex login method:",
- options: [
- { id: "browser", label: "Browser login (default)" },
- { id: "device_code", label: "Device code login (headless)" },
- ],
- },
- ]);
- expect(deviceInfos).toEqual([
- {
- userCode: "WXYZ-7890",
- verificationUri: "https://auth.openai.com/codex/device",
- intervalSeconds: 5,
- expiresInSeconds: 900,
- },
- ]);
- });
- it("refreshes tokens through the injected fetch implementation", async () => {
- const accessToken = createAccessToken("account-refresh");
- const injectedFetch = vi.fn(async (input: unknown, init?: RequestInit): Promise<Response> => {
- expect(getUrl(input)).toBe("https://auth.openai.com/oauth/token");
- expect(init?.method).toBe("POST");
- expect(init?.headers).toMatchObject({ "Content-Type": "application/x-www-form-urlencoded" });
- const params = new URLSearchParams(String(init?.body));
- expect(params.get("grant_type")).toBe("refresh_token");
- expect(params.get("refresh_token")).toBe("refresh-token");
- return jsonResponse({
- access_token: accessToken,
- refresh_token: "next-refresh-token",
- expires_in: 3600,
- });
- });
- const globalFetch = vi.fn(async () => {
- throw new Error("global fetch should not be used");
- });
- vi.stubGlobal("fetch", globalFetch);
- await expect(refreshOpenAICodexToken("refresh-token", injectedFetch as typeof fetch)).resolves.toMatchObject({
- access: accessToken,
- refresh: "next-refresh-token",
- accountId: "account-refresh",
- });
- expect(injectedFetch).toHaveBeenCalledTimes(1);
- expect(globalFetch).not.toHaveBeenCalled();
- });
- it("cancels when OpenAI Codex login method selection is cancelled", async () => {
- await expect(
- openaiCodexOAuthProvider.login({
- onAuth: () => {},
- onDeviceCode: () => {},
- onPrompt: async () => "",
- onSelect: async () => undefined,
- }),
- ).rejects.toThrow("Login cancelled");
- });
- it("cancels the OpenAI Codex device code flow while waiting", async () => {
- vi.useFakeTimers();
- const controller = new AbortController();
- const pollTimes: number[] = [];
- vi.stubGlobal(
- "fetch",
- vi.fn(async (input: unknown, init?: RequestInit): Promise<Response> => {
- const url = getUrl(input);
- if (url === "https://auth.openai.com/api/accounts/deviceauth/usercode") {
- expect(JSON.parse(String(init?.body))).toEqual({ client_id: "app_EMoamEEZ73f0CkXaXp7hrann" });
- return jsonResponse({
- device_auth_id: "device-auth-id",
- user_code: "ABCD-1234",
- interval: "5",
- });
- }
- if (url === "https://auth.openai.com/api/accounts/deviceauth/token") {
- pollTimes.push(Date.now());
- return deviceAuthPendingResponse();
- }
- throw new Error(`Unexpected fetch URL: ${url}`);
- }),
- );
- const credentialsPromise = loginOpenAICodexDeviceCode({
- onDeviceCode: () => {},
- signal: controller.signal,
- });
- const rejectionPromise = credentialsPromise.then(
- () => new Error("Expected login to fail"),
- (error: unknown) => error,
- );
- for (let i = 0; i < 5 && pollTimes.length === 0; i++) {
- await vi.advanceTimersByTimeAsync(0);
- }
- expect(pollTimes).toHaveLength(1);
- controller.abort();
- const rejection = await rejectionPromise;
- expect(rejection).toBeInstanceOf(Error);
- expect((rejection as Error).message).toBe("Login cancelled");
- });
- it("times out the OpenAI Codex device code flow after 15 minutes", async () => {
- vi.useFakeTimers();
- const pollTimes: number[] = [];
- vi.stubGlobal(
- "fetch",
- vi.fn(async (input: unknown, init?: RequestInit): Promise<Response> => {
- const url = getUrl(input);
- if (url === "https://auth.openai.com/api/accounts/deviceauth/usercode") {
- expect(JSON.parse(String(init?.body))).toEqual({ client_id: "app_EMoamEEZ73f0CkXaXp7hrann" });
- return jsonResponse({
- device_auth_id: "device-auth-id",
- user_code: "ABCD-1234",
- interval: "60",
- });
- }
- if (url === "https://auth.openai.com/api/accounts/deviceauth/token") {
- pollTimes.push(Date.now());
- return deviceAuthPendingResponse();
- }
- throw new Error(`Unexpected fetch URL: ${url}`);
- }),
- );
- const credentialsPromise = loginOpenAICodexDeviceCode({
- onDeviceCode: () => {},
- });
- const rejectionPromise = credentialsPromise.then(
- () => new Error("Expected login to fail"),
- (error: unknown) => error,
- );
- for (let i = 0; i < 5 && pollTimes.length === 0; i++) {
- await vi.advanceTimersByTimeAsync(0);
- }
- expect(pollTimes).toHaveLength(1);
- await vi.advanceTimersByTimeAsync(15 * 60 * 1000);
- const rejection = await rejectionPromise;
- expect(rejection).toBeInstanceOf(Error);
- expect((rejection as Error).message).toBe("Device flow timed out");
- });
- it("treats OpenAI Codex device auth 403 and 404 responses as pending", async () => {
- vi.useFakeTimers();
- const accessToken = createAccessToken("account-403-404");
- const pollTimes: number[] = [];
- const pollResponses = [
- jsonResponse({ error: "access_denied", error_description: "denied" }, 403),
- new Response("not ready", { status: 404, headers: { "Content-Type": "text/plain" } }),
- jsonResponse({
- authorization_code: "oauth-code",
- code_challenge: "device-code-challenge",
- code_verifier: "device-code-verifier",
- }),
- ];
- vi.stubGlobal(
- "fetch",
- vi.fn(async (input: unknown): Promise<Response> => {
- const url = getUrl(input);
- if (url === "https://auth.openai.com/api/accounts/deviceauth/usercode") {
- return jsonResponse({
- device_auth_id: "device-auth-id",
- user_code: "ABCD-1234",
- interval: "1",
- });
- }
- if (url === "https://auth.openai.com/api/accounts/deviceauth/token") {
- pollTimes.push(Date.now());
- const response = pollResponses.shift();
- if (!response) {
- throw new Error("Unexpected extra device auth poll");
- }
- return response;
- }
- if (url === "https://auth.openai.com/oauth/token") {
- return jsonResponse({
- access_token: accessToken,
- refresh_token: "refresh-token",
- expires_in: 3600,
- });
- }
- throw new Error(`Unexpected fetch URL: ${url}`);
- }),
- );
- const credentialsPromise = loginOpenAICodexDeviceCode({
- onDeviceCode: () => {},
- });
- for (let i = 0; i < 5 && pollTimes.length === 0; i++) {
- await vi.advanceTimersByTimeAsync(0);
- }
- await vi.advanceTimersByTimeAsync(1000);
- await vi.advanceTimersByTimeAsync(1000);
- await expect(credentialsPromise).resolves.toMatchObject({
- access: accessToken,
- refresh: "refresh-token",
- accountId: "account-403-404",
- });
- expect(pollTimes).toHaveLength(3);
- });
- it("includes the response body in OpenAI Codex device auth poll failures", async () => {
- vi.stubGlobal(
- "fetch",
- vi.fn(async (input: unknown): Promise<Response> => {
- const url = getUrl(input);
- if (url === "https://auth.openai.com/api/accounts/deviceauth/usercode") {
- return jsonResponse({
- device_auth_id: "device-auth-id",
- user_code: "ABCD-1234",
- interval: "5",
- });
- }
- if (url === "https://auth.openai.com/api/accounts/deviceauth/token") {
- return jsonResponse({ error: "server_error", error_description: "try again later" }, 500);
- }
- throw new Error(`Unexpected fetch URL: ${url}`);
- }),
- );
- await expect(
- loginOpenAICodexDeviceCode({
- onDeviceCode: () => {},
- }),
- ).rejects.toThrow(
- 'OpenAI Codex device auth failed with status 500: {"error":"server_error","error_description":"try again later"}',
- );
- });
- it("does not write token refresh failures to stderr", async () => {
- const consoleError = vi.spyOn(console, "error").mockImplementation(() => {});
- vi.stubGlobal(
- "fetch",
- vi.fn(async (): Promise<Response> => {
- return new Response(
- JSON.stringify({
- error: {
- message: "Could not validate your token. Please try signing in again.",
- type: "invalid_request_error",
- },
- }),
- { status: 401, statusText: "Unauthorized", headers: { "Content-Type": "application/json" } },
- );
- }),
- );
- await expect(refreshOpenAICodexToken("invalid-refresh-token")).rejects.toThrow(
- /OpenAI Codex token refresh failed \(401\).*Could not validate your token/,
- );
- expect(consoleError).not.toHaveBeenCalled();
- });
- });
|