Files
Netcatty/electron/bridges/aiBridge/agentAuthProbes.cjs
T
8b2a3d23d4 feat(ai): support Cursor CLI login for subscription quota (#2412)
* feat(ai): support Cursor CLI login for subscription quota

Allow the managed Cursor agent to authenticate via local `agent` CLI
login as an alternative to CURSOR_API_KEY, so Catty can use subscription
Auto quota. Keep API key and CLI login mutually exclusive, isolate
resume sessions by auth mode, and expose selectable CLI models with
auto as the default.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(ai): harden Cursor CLI login path after review

Address multi-agent review findings: soft-cancel abort without late
stream errors, prefer cursor-agent over ambiguous agent binaries,
mode-aware availability without wiping API keys or sticky-disabling,
and concurrent MCP merge refcount restore. Add unit coverage for the
new contracts.

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: bincxz <16399091+binaricat@users.noreply.github.com>
2026-07-23 22:54:59 +08:00

245 lines
8.6 KiB
JavaScript

"use strict";
/**
* Layer-3 (authentication) CLI probes for the managed backends.
* Each probe is dependency-injected (runners / fileExists) for unit testing;
* the discovery handler wires the real implementations.
*
* Returns: { authenticated: boolean, authSource: string|null }
*/
const { existsSync, readFileSync } = require("node:fs");
const os = require("node:os");
const path = require("node:path");
const { execFileSync } = require("node:child_process");
function defaultFileExists(p) {
try { return existsSync(p); } catch { return false; }
}
function defaultReadFile(p) {
try { return readFileSync(p, "utf-8"); } catch { return null; }
}
// ── Claude ──
function defaultRunSecurity() {
// macOS keychain lookup for the Claude Code OAuth credentials entry.
try {
const stdout = execFileSync(
"security",
["find-generic-password", "-s", "Claude Code-credentials", "-w"],
{ encoding: "utf8", timeout: 4000, stdio: ["pipe", "pipe", "pipe"] },
);
return { exitCode: 0, stdout };
} catch (err) {
return { exitCode: err?.status ?? 1, stdout: "" };
}
}
function probeClaudeAuth({ env, platform, runSecurity, fileExists, homeDir } = {}) {
const e = env || process.env;
const plat = platform || process.platform;
const fx = fileExists || defaultFileExists;
const home = homeDir || os.homedir();
const apiKey = typeof e.ANTHROPIC_API_KEY === "string" ? e.ANTHROPIC_API_KEY.trim() : "";
const oauthToken = typeof e.CLAUDE_CODE_OAUTH_TOKEN === "string" ? e.CLAUDE_CODE_OAUTH_TOKEN.trim() : "";
const authToken = typeof e.ANTHROPIC_AUTH_TOKEN === "string" ? e.ANTHROPIC_AUTH_TOKEN.trim() : "";
if (apiKey || oauthToken || authToken) return { authenticated: true, authSource: "env" };
if (plat === "darwin") {
const sec = (runSecurity || defaultRunSecurity)();
if (sec && sec.exitCode === 0 && String(sec.stdout || "").trim()) {
return { authenticated: true, authSource: "keychain" };
}
}
const configDir = typeof e.CLAUDE_CONFIG_DIR === "string" && e.CLAUDE_CONFIG_DIR.trim()
? e.CLAUDE_CONFIG_DIR.trim()
: path.join(home, ".claude");
if (fx(path.join(configDir, ".credentials.json"))) {
return { authenticated: true, authSource: "credentials-file" };
}
return { authenticated: false, authSource: null };
}
// ── Copilot ──
function defaultRunGhAuthStatus() {
try {
const out = execFileSync("gh", ["auth", "status"], {
encoding: "utf8", timeout: 5000, stdio: ["pipe", "pipe", "pipe"],
});
return { exitCode: 0, stdout: out, stderr: "" };
} catch (err) {
return { exitCode: err?.status ?? 1, stdout: "", stderr: String(err?.stderr || err?.message || "") };
}
}
function probeCopilotAuth({ runGhAuthStatus } = {}) {
const res = (runGhAuthStatus || defaultRunGhAuthStatus)();
if (res && res.exitCode === 0) return { authenticated: true, authSource: "gh" };
return { authenticated: false, authSource: null };
}
// ── Codex ──
function probeCodexAuth({ runLoginStatus, fileExists, homeDir } = {}) {
const fx = fileExists || defaultFileExists;
const home = homeDir || os.homedir();
const res = runLoginStatus ? runLoginStatus() : { exitCode: 1, stdout: "" };
const out = String((res && (res.stdout || res.stderr)) || "").toLowerCase();
if (out.includes("logged in using chatgpt")) return { authenticated: true, authSource: "chatgpt" };
if (out.includes("logged in using an api key") || out.includes("logged in using api key")) {
return { authenticated: true, authSource: "api-key" };
}
if (fx(path.join(home, ".codex", "auth.json"))) {
return { authenticated: true, authSource: "auth-file" };
}
return { authenticated: false, authSource: null };
}
// ── CodeBuddy ──
// SDK supports CODEBUDDY_API_KEY, CODEBUDDY_AUTH_TOKEN (OAuth), and CLI login
// state (~/.codebuddy/settings.json with authToken/apiKeyHelper).
function probeCodebuddyAuth({ env, fileExists, readFile, homeDir } = {}) {
const e = env || process.env;
const fx = fileExists || defaultFileExists;
const rf = readFile || defaultReadFile;
const home = homeDir || os.homedir();
const apiKey = typeof e.CODEBUDDY_API_KEY === "string" ? e.CODEBUDDY_API_KEY.trim() : "";
const authToken = typeof e.CODEBUDDY_AUTH_TOKEN === "string" ? e.CODEBUDDY_AUTH_TOKEN.trim() : "";
if (apiKey) return { authenticated: true, authSource: "api-key" };
if (authToken) return { authenticated: true, authSource: "auth-token" };
// Check CLI login state in settings.json (authToken / apiKeyHelper fields).
const settingsPath = path.join(home, ".codebuddy", "settings.json");
const content = rf(settingsPath);
if (content !== null) {
try {
const parsed = JSON.parse(content);
if (parsed && typeof parsed === "object") {
if (typeof parsed.authToken === "string" && parsed.authToken.trim()) {
return { authenticated: true, authSource: "settings-file" };
}
if (typeof parsed.apiKeyHelper === "string" && parsed.apiKeyHelper.trim()) {
return { authenticated: true, authSource: "settings-file" };
}
}
} catch { /* Malformed JSON — treat as no auth */ }
}
return { authenticated: false, authSource: null };
}
// ── Cursor CLI login (agent / cursor-agent) ──
// Prefer `cursor-agent`: bare `agent` collides with other tools on PATH (e.g. Grok).
const CURSOR_CLI_BINARY_CANDIDATES = ["cursor-agent", "agent"];
function stripCursorApiKeyFromProbeEnv(env) {
const out = { ...(env || {}) };
delete out.CURSOR_API_KEY;
return out;
}
function defaultResolveCursorCliBinary(name, env) {
try {
const whichCmd = process.platform === "win32" ? "where" : "which";
const out = execFileSync(whichCmd, [name], {
encoding: "utf8",
timeout: 4000,
env: env || process.env,
stdio: ["pipe", "pipe", "pipe"],
});
const first = String(out || "").split(/\r?\n/).map((l) => l.trim()).find(Boolean);
return first || null;
} catch {
return null;
}
}
function defaultRunCursorStatus(binPath, env) {
try {
const stdout = execFileSync(binPath, ["status", "--format", "json"], {
encoding: "utf8",
timeout: 8000,
env: env || process.env,
stdio: ["pipe", "pipe", "pipe"],
});
return { exitCode: 0, stdout: String(stdout || ""), stderr: "" };
} catch (err) {
return {
exitCode: err?.status ?? 1,
stdout: String(err?.stdout || ""),
stderr: String(err?.stderr || err?.message || ""),
};
}
}
function parseCursorStatusJson(stdout) {
try {
const parsed = JSON.parse(String(stdout || "").trim());
if (!parsed || typeof parsed !== "object") return null;
// Real Cursor status always exposes isAuthenticated and/or status.
// Reject unrelated CLIs that accept unknown flags or emit other JSON.
if (typeof parsed.isAuthenticated !== "boolean" && typeof parsed.status !== "string") {
return null;
}
return parsed;
} catch {
return null;
}
}
/**
* Probe local Cursor Agent CLI login (subscription session).
* Tries `cursor-agent` then `agent`, keeping the first binary that returns a
* Cursor-shaped status JSON. Strips CURSOR_API_KEY so "cli-login" is not
* proven by a metered API key alone.
*
* @returns {{ authenticated: boolean, authSource: string|null, email: string|null, binPath: string|null }}
*/
function probeCursorCliAuth({ env, resolveBinary, runStatus } = {}) {
const e = stripCursorApiKeyFromProbeEnv(env || process.env);
const resolve = resolveBinary || ((name) => defaultResolveCursorCliBinary(name, e));
const run = runStatus || ((bin) => defaultRunCursorStatus(bin, e));
let cursorShapedBinPath = null;
for (const name of CURSOR_CLI_BINARY_CANDIDATES) {
const binPath = resolve(name);
if (!binPath) continue;
const res = run(binPath);
if (!res) continue;
// Accept JSON even on non-zero exit if present (some CLIs exit 1 when logged out).
const parsed = parseCursorStatusJson(res.stdout);
if (!parsed) continue;
if (!cursorShapedBinPath) cursorShapedBinPath = binPath;
const authenticated = Boolean(
parsed.isAuthenticated === true || parsed.status === "authenticated",
);
if (!authenticated) continue;
const email = typeof parsed?.userInfo?.email === "string" ? parsed.userInfo.email : null;
return { authenticated: true, authSource: "cli-login", email, binPath };
}
return {
authenticated: false,
authSource: null,
email: null,
binPath: cursorShapedBinPath,
};
}
module.exports = {
probeClaudeAuth,
probeCopilotAuth,
probeCodexAuth,
probeCodebuddyAuth,
probeCursorCliAuth,
CURSOR_CLI_BINARY_CANDIDATES,
defaultRunSecurity,
defaultRunGhAuthStatus,
};