Skip to content

Use Claude Code with your ChatGPT, Kimi, Cursor or Grok subscription via a local Anthropic-compatible proxy

Rust

share cursor auth token parsing

Raine Virtaafcf9daaf086
diff --git a/src/providers/cursor/auth/login.ts b/src/providers/cursor/auth/login.ts
index 3abc043..f1e2eda 100644
--- a/src/providers/cursor/auth/login.ts
+++ b/src/providers/cursor/auth/login.ts
@@ -4,6 +4,7 @@ import { platform } from "node:os";
import { promisify } from "node:util";
import { cursorBaseUrl } from "../../../config.ts";
import { saveCursorAuth, type CursorAuth } from "./token-store.ts";
+import { parseCursorAuthTokens } from "./token-parser.ts";
const execFileAsync = promisify(execFile);
const CURSOR_WEBSITE_URL = "https://cursor.com";
@@ -72,13 +73,9 @@ export async function waitForCursorLogin(
continue;
}
const parsed = await resp.json();
- if (parsed && typeof parsed === "object") {
- const obj = parsed as Record<string, unknown>;
- if (typeof obj.accessToken === "string" && typeof obj.refreshToken === "string") {
- return { accessToken: obj.accessToken, refreshToken: obj.refreshToken };
- }
- }
- return undefined;
+ const parsedTokens = parseCursorAuthTokens(parsed);
+ if (!parsedTokens) return undefined;
+ return parsedTokens;
} catch {
consecutiveErrors += 1;
if (consecutiveErrors >= 3) return undefined;
diff --git a/src/providers/cursor/auth/token-parser.ts b/src/providers/cursor/auth/token-parser.ts
new file mode 100644
index 0000000..8eee99a
--- /dev/null
+++ b/src/providers/cursor/auth/token-parser.ts
@@ -0,0 +1,13 @@
+type ParsedCursorAuthTokens = {
+ accessToken: string;
+ refreshToken: string;
+};
+
+export function parseCursorAuthTokens(parsed: unknown): ParsedCursorAuthTokens | undefined {
+ if (!parsed || typeof parsed !== "object") return undefined;
+ const obj = parsed as Partial<Record<string, unknown>>;
+ if (typeof obj.accessToken === "string" && typeof obj.refreshToken === "string") {
+ return { accessToken: obj.accessToken, refreshToken: obj.refreshToken };
+ }
+ return undefined;
+}
diff --git a/src/providers/cursor/auth/token-store.ts b/src/providers/cursor/auth/token-store.ts
index 317060c..ddaf203 100644
--- a/src/providers/cursor/auth/token-store.ts
+++ b/src/providers/cursor/auth/token-store.ts
@@ -3,6 +3,7 @@ import { dirname, join } from "node:path";
import { cursorBaseUrl } from "../../../config.ts";
import { keychainDelete, keychainGet, keychainSet } from "../../../keychain.ts";
import { cursorAuthFile, legacyConfigDir } from "../../../paths.ts";
+import { parseCursorAuthTokens } from "./token-parser.ts";
import { parseJwtClaims, tokenExpiryMs } from "./jwt.ts";
export interface CursorAuth {
@@ -135,13 +136,7 @@ async function refreshCursorAuth(refreshToken: string): Promise<{ accessToken: s
});
if (!resp.ok) return undefined;
const parsed = await resp.json();
- if (parsed && typeof parsed === "object") {
- const obj = parsed as Record<string, unknown>;
- if (typeof obj.accessToken === "string" && typeof obj.refreshToken === "string") {
- return { accessToken: obj.accessToken, refreshToken: obj.refreshToken };
- }
- }
- return undefined;
+ return parseCursorAuthTokens(parsed);
}
function authFileCandidates(): string[] {