Use Claude Code with your ChatGPT, Kimi, Cursor or Grok subscription via a local Anthropic-compatible proxy
Rust
cursor: add config dir auth storage
Raine Virta38b256f93a9b
diff --git a/README.md b/README.mdindex 0ad5f40..80c5a99 100644--- a/README.md+++ b/README.md@@ -82,7 +82,8 @@ tokens. It does not read Cursor Agent's Keychain/auth.json. You can also setOn macOS credentials go to Keychain. On Windows they are written under`%APPDATA%\claude-code-proxy\<provider>\auth.json`; on Linux they are writtenunder `${XDG_CONFIG_HOME:-$HOME/.config}/claude-code-proxy/<provider>/auth.json`-(mode 0600 where supported).+(mode 0600 where supported). Set `CCP_CONFIG_DIR` before `cursor auth login` to+store a separate Cursor login at `$CCP_CONFIG_DIR/cursor/auth.json`.Verify:@@ -590,7 +591,8 @@ config file is optional — env-var-only setups continue to work unchanged.The file lives at `~/.config/claude-code-proxy/config.json` on macOS(deliberately not `~/Library`), at `%APPDATA%\claude-code-proxy\config.json` onWindows, and at-`${XDG_CONFIG_HOME:-$HOME/.config}/claude-code-proxy/config.json` on Linux.+`${XDG_CONFIG_HOME:-$HOME/.config}/claude-code-proxy/config.json` on Linux. Set+`CCP_CONFIG_DIR` to use a separate config and auth directory for that process.```json{@@ -626,6 +628,7 @@ Windows, and at| Variable | Config key | Default | Purpose || -------------------------------- | -------------------------- | ------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- || `PORT` | `port` | `18765` | Proxy listen port |+| `CCP_CONFIG_DIR` | unset | platform config dir | Per-process config directory; Cursor auth uses it for file storage || `XDG_STATE_HOME` | — | `~/.local/state` | Linux/macOS base dir for `proxy.log` || `CCP_LOG_STDERR` | `log.stderr` | unset | Also mirror log lines to stderr || `CCP_LOG_VERBOSE` | `log.verbose` | unset | Log full request/response bodies + every SSE event |@@ -686,7 +689,8 @@ sticky sessions or shared state before enabling continuation.- `config.json` — optional configuration file (see table above). It lives at`~/.config/claude-code-proxy/config.json` on macOS,`${XDG_CONFIG_HOME:-$HOME/.config}/claude-code-proxy/config.json` on Linux,- and `%APPDATA%\claude-code-proxy\config.json` on Windows.+ and `%APPDATA%\claude-code-proxy\config.json` on Windows. `CCP_CONFIG_DIR`+ replaces the platform config directory for the current process.- Codex tokens — macOS uses Keychain under service `claude-code-proxy.codex`.Linux uses`${XDG_CONFIG_HOME:-$HOME/.config}/claude-code-proxy/codex/auth.json`.@@ -702,7 +706,9 @@ sticky sessions or shared state before enabling continuation.- Cursor tokens — macOS uses Keychain under service`claude-code-proxy.cursor`. Linux uses`${XDG_CONFIG_HOME:-$HOME/.config}/claude-code-proxy/cursor/auth.json`.- Windows uses `%APPDATA%\claude-code-proxy\cursor\auth.json`.+ Windows uses `%APPDATA%\claude-code-proxy\cursor\auth.json`. When+ `CCP_CONFIG_DIR` is set, Cursor tokens are written to `cursor/auth.json` under+ that directory, including on macOS.`CCP_CURSOR_AUTH_TOKEN` overrides local proxy-owned storage.## Limitationsdiff --git a/src/paths.test.ts b/src/paths.test.tsindex 038d5f6..52fcc63 100644--- a/src/paths.test.ts+++ b/src/paths.test.ts@@ -10,6 +10,12 @@ import {} from "./paths.ts";describe("resolveConfigDir", () => {+ it("uses CCP_CONFIG_DIR when set", () => {+ expect(+ resolveConfigDir({ platform: "darwin", env: { CCP_CONFIG_DIR: "/tmp/ccp-a" }, home: "/home/u" }),+ ).toBe("/tmp/ccp-a");+ });+it("uses ~/.config on darwin even when XDG_CONFIG_HOME is set", () => {expect(resolveConfigDir({ platform: "darwin", env: { XDG_CONFIG_HOME: "/x" }, home: "/home/u" }),diff --git a/src/paths.ts b/src/paths.tsindex a3791f6..89fdf7b 100644--- a/src/paths.ts+++ b/src/paths.ts@@ -7,6 +7,10 @@ export interface DirResolverEnv {home: string;}+function explicitConfigDir(deps: DirResolverEnv): string | undefined {+ return deps.env.CCP_CONFIG_DIR;+}+function defaults(): DirResolverEnv {return { platform: process.platform, env: process.env, home: homedir() };}@@ -27,6 +31,9 @@ function windowsLocalAppData(deps: DirResolverEnv): string {// (which would redirect to ~/Library/Application Support). This matches where// auth tokens have always been stored on macOS.export function resolveConfigDir(deps: DirResolverEnv): string {+ const override = explicitConfigDir(deps);+ if (override) return override;+const path = pathFor(deps.platform);if (deps.platform === "win32") {return path.join(windowsRoamingAppData(deps), "claude-code-proxy");diff --git a/src/providers/cursor/auth/token-store.test.ts b/src/providers/cursor/auth/token-store.test.tsindex 83d763e..53e01a1 100644--- a/src/providers/cursor/auth/token-store.test.ts+++ b/src/providers/cursor/auth/token-store.test.ts@@ -1,9 +1,21 @@-import { describe, expect, it } from "bun:test";-import { loadCursorAuth } from "./token-store.ts";+import { afterEach, describe, expect, it } from "bun:test";+import { mkdtempSync, rmSync } from "node:fs";+import { readFile } from "node:fs/promises";+import { tmpdir } from "node:os";+import { join } from "node:path";+import { clearCursorAuth, cursorAuthLocation, loadCursorAuth, saveCursorAuth } from "./token-store.ts";import { parseJwtClaims, tokenExpiryMs } from "./jwt.ts";import { jwt } from "../cursor-test-helpers.ts";describe("Cursor auth token discovery", () => {+ const originalConfigDir = process.env.CCP_CONFIG_DIR;+ const tempDirs: string[] = [];++ afterEach(() => {+ restoreEnv("CCP_CONFIG_DIR", originalConfigDir);+ for (const dir of tempDirs.splice(0)) rmSync(dir, { recursive: true, force: true });+ });+it("loads CCP_CURSOR_AUTH_TOKEN without reading Cursor storage", async () => {const token = jwt({ sub: "user_1", email: "[email protected]", exp: 2_000_000_000 });@@ -22,4 +34,33 @@ describe("Cursor auth token discovery", () => {expect(parseJwtClaims(token)?.sub).toBe("user_2");expect(tokenExpiryMs(token)).toBe(123_000);});++ it("uses CCP_CONFIG_DIR for Cursor auth storage", async () => {+ const dir = tempConfigDir(tempDirs);+ process.env.CCP_CONFIG_DIR = dir;+ const token = jwt({ sub: "user_3", email: "[email protected]", exp: 2_000_000_000 });++ const saved = await saveCursorAuth({ accessToken: token, refreshToken: "refresh-token" });+ const raw = await readFile(join(dir, "cursor", "auth.json"), "utf8");+ const loaded = await loadCursorAuth({});+ await clearCursorAuth();++ expect(saved.source).toBe(join(dir, "cursor", "auth.json"));+ expect(JSON.parse(raw)).toEqual({ accessToken: token, refreshToken: "refresh-token" });+ expect(loaded?.accessToken).toBe(token);+ expect(loaded?.source).toBe(join(dir, "cursor", "auth.json"));+ expect(cursorAuthLocation()).toBe(join(dir, "cursor", "auth.json"));+ await expect(readFile(join(dir, "cursor", "auth.json"), "utf8")).rejects.toThrow();+ });});++function restoreEnv(key: string, value: string | undefined): void {+ if (value === undefined) delete process.env[key];+ else process.env[key] = value;+}++function tempConfigDir(tempDirs: string[]): string {+ const dir = mkdtempSync(join(tmpdir(), "ccp-cursor-auth-"));+ tempDirs.push(dir);+ return dir;+}diff --git a/src/providers/cursor/auth/token-store.ts b/src/providers/cursor/auth/token-store.tsindex d83e06b..3823423 100644--- a/src/providers/cursor/auth/token-store.ts+++ b/src/providers/cursor/auth/token-store.ts@@ -27,11 +27,13 @@ const KEYCHAIN_SERVICE = "claude-code-proxy.cursor";const KEYCHAIN_ACCOUNT = "auth";const REFRESH_EXPIRY_SKEW_MS = 60_000;+type CursorAuthStorage = "file" | "keychain";+export async function loadCursorAuth(env: NodeJS.ProcessEnv = process.env): Promise<CursorAuth | undefined> {const envToken = env.CCP_CURSOR_AUTH_TOKEN || env.CURSOR_AUTH_TOKEN;if (envToken) return authFromToken(envToken, "environment");- if (process.platform === "darwin") {+ if (cursorAuthStorage() === "keychain") {const raw = keychainGet(KEYCHAIN_SERVICE, KEYCHAIN_ACCOUNT);if (!raw) return undefined;const parsed = JSON.parse(raw) as CursorAuthFile;@@ -53,7 +55,7 @@ export async function loadCursorAuth(env: NodeJS.ProcessEnv = process.env): Prom}export async function clearCursorAuth(): Promise<void> {- if (process.platform === "darwin") {+ if (cursorAuthStorage() === "keychain") {keychainDelete(KEYCHAIN_SERVICE, KEYCHAIN_ACCOUNT);return;}@@ -69,7 +71,7 @@ export async function clearCursorAuth(): Promise<void> {export async function saveCursorAuth(auth: CursorAuthFile): Promise<CursorAuth> {if (!auth.accessToken) throw new Error("Cursor auth accessToken is required");- if (process.platform === "darwin") {+ if (cursorAuthStorage() === "keychain") {keychainSet(KEYCHAIN_SERVICE, KEYCHAIN_ACCOUNT, JSON.stringify(auth));return enrich({ ...auth, accessToken: auth.accessToken, source: cursorAuthLocation() });}@@ -80,7 +82,7 @@ export async function saveCursorAuth(auth: CursorAuthFile): Promise<CursorAuth>}export function cursorAuthLocation(): string {- return process.platform === "darwin" ? "macOS Keychain (claude-code-proxy.cursor)" : cursorAuthFile();+ return cursorAuthStorage() === "keychain" ? "macOS Keychain (claude-code-proxy.cursor)" : cursorAuthFile();}export function missingAuthMessage(): string {@@ -139,6 +141,18 @@ async function refreshCursorAuth(refreshToken: string): Promise<{ accessToken: sfunction authFileCandidates(): string[] {const primary = cursorAuthFile();+ if (hasConfigDirOverride()) return [primary];const legacy = join(legacyConfigDir(), "cursor", "auth.json");return legacy === primary ? [primary] : [primary, legacy];}++function cursorAuthStorage(): CursorAuthStorage {+ if (process.platform === "darwin" && !hasConfigDirOverride()) {+ return "keychain";+ }+ return "file";+}++function hasConfigDirOverride(): boolean {+ return !!process.env.CCP_CONFIG_DIR;+}