Files
plugins/packages/atomcode/auth-file.mjs
T
LEOLI 8765cf8f9c atomcode 0.3.0: AtomGit CodingPlan provider plugin (LILEOO, #716)
Adds packages/atomcode (@magpie-community/opencode-atomcode-auth 0.3.0), an AtomGit CodingPlan provider: browser OAuth and AtomCode CLI sign-in (~/.atomcode/auth.toml), signed requests to AtomGit's own gateway, per-account models and quota, daily claim, a loopback-only diagnostics page, and OpenCode 1.x/2.x entries. Registry and README entries added. By LILEOO.

Reviewed at c57b099fff4689c0198c1c6414f04b27614b4092. The last blocking point (an inline TOML comment on a known line went into the uid and was written back) is fixed: values end at an unquoted #, quoted/escaped/literal strings keep their #, and a rewritten line keeps its comment.

Run on a separate test machine (Bun 1.4.2, sandbox HOME):
- PR head merged onto origin/main 24da52b8: bun scripts/check.mjs exit 0 (atomcode ok); whole-repo bun test 6103 pass, 0 fail. origin/main alone: 5942 pass, 0 fail.
- packages/atomcode at the head: bun test 161 pass; node --test test/*.test.mjs 161 pass.
- The new auth-file test against 55ff272's auth-file.mjs: red (1 fail), green at the head.
- Probe: the review's file (id = "123456" # c) parses to uid 123456 and renders back as id = "123456" # c; also #c with no space, CRLF with a tab before #, "1#2", and a literal-string id all round-trip to the same uid. At 55ff272 each gave a uid with the comment in it.

Not tried: a real AtomGit account. A [user] header with a trailing comment is refused ("no user id"), not corrupted; the CLI never writes one. First npm version needs an owner publish; until then magpie installs it from the dist branch.
2026-11-05 16:54:02 +08:00

345 lines
11 KiB
JavaScript

/**
* Read and update the sign-in AtomCode's own CLI keeps in
* `~/.atomcode/auth.toml`.
*
* This is deliberately a narrow TOML reader, not a general parser: the file is
* owned by the official client, so unknown lines and sections are preserved
* whenever the plugin writes a renewed token back.
*
* @module magpie-atomcode/auth-file
*/
import { chmod, lstat, mkdir, open, readFile, rename, unlink } from "node:fs/promises";
import { homedir } from "node:os";
import { dirname, isAbsolute, join, normalize } from "node:path";
import { randomBytes } from "node:crypto";
const ROOT_KEYS = new Set(["access_token", "refresh_token", "created_at", "expires_in"]);
const USER_KEYS = new Set(["id", "username", "name", "email", "avatar_url"]);
export function defaultAuthFilePath(env = process.env) {
const home = env.HOME || env.USERPROFILE || homedir();
return join(home, ".atomcode", "auth.toml");
}
/**
* Resolve a user-supplied CLI auth path.
*
* Empty means the default. Only `~`, `~/...` and absolute paths are accepted;
* relative paths and `~other-user` would make the account source depend on the
* host's working directory, so they are rejected.
*
* @param {unknown} value
* @param {object} [env]
* @returns {string}
*/
export function resolveAuthFilePath(value, env = process.env) {
const raw = value === undefined || value === null ? "" : String(value).trim();
if (!raw) return defaultAuthFilePath(env);
if (raw.includes("\0")) throw new Error("AtomCode auth file path contains a NUL byte");
if (raw.length > 4096) throw new Error("AtomCode auth file path is too long");
const home = env.HOME || env.USERPROFILE || homedir();
let resolved;
if (raw === "~") {
resolved = home;
} else if (raw.startsWith("~/") || raw.startsWith("~\\")) {
resolved = join(home, raw.slice(2));
} else if (raw.startsWith("~")) {
throw new Error("AtomCode auth file path must not use ~another-user");
} else if (isAbsolute(raw)) {
resolved = normalize(raw);
} else {
throw new Error("AtomCode auth file path must be absolute or start with ~/");
}
if (resolved.length > 4096) throw new Error("AtomCode auth file path is too long");
return resolved;
}
function splitComment(raw) {
const text = String(raw ?? "");
let quote = "";
let escaped = false;
for (let i = 0; i < text.length; i++) {
const char = text[i];
if (quote === '"' && escaped) {
escaped = false;
} else if (quote === '"' && char === "\\") {
escaped = true;
} else if (quote && char === quote) {
quote = "";
} else if (!quote && (char === '"' || char === "'")) {
quote = char;
} else if (!quote && char === "#") {
const value = text.slice(0, i);
const end = value.trimEnd().length;
return { value: value.slice(0, end), comment: text.slice(end) };
}
}
return { value: text, comment: "" };
}
function scalar(raw) {
const value = splitComment(raw).value.trim();
if (value.startsWith('"') && value.endsWith('"')) {
try {
return JSON.parse(value);
} catch {
return value.slice(1, -1);
}
}
if (value.startsWith("'") && value.endsWith("'")) return value.slice(1, -1);
return value;
}
function collect(text) {
const root = {};
const user = {};
let section = "";
for (const line of String(text).split(/\r?\n/)) {
const header = line.match(/^\s*\[([^\]]+)\]\s*$/);
if (header) {
section = header[1].trim();
continue;
}
const pair = line.match(/^\s*([A-Za-z_][A-Za-z0-9_-]*)\s*=\s*(.*?)\s*$/);
if (!pair) continue;
const key = pair[1];
if ((!section && ROOT_KEYS.has(key)) || (section === "user" && USER_KEYS.has(key))) {
(section === "user" ? user : root)[key] = scalar(pair[2]);
}
}
return { root, user };
}
/**
* Parse the fields this plugin needs from AtomCode's auth.toml.
*
* @param {string} text
* @returns {object}
*/
export function parseAuthFile(text) {
const { root, user } = collect(text);
const access = typeof root.access_token === "string" ? root.access_token : "";
const uid = user.id === undefined || user.id === null ? "" : String(user.id);
if (!access) throw new Error("AtomCode auth.toml has no access token");
if (!uid) throw new Error("AtomCode auth.toml has no user id");
const createdAt = Number(root.created_at);
const expiresIn = Number(root.expires_in);
const expires =
Number.isFinite(createdAt) && Number.isFinite(expiresIn) && expiresIn > 0
? (createdAt + expiresIn) * 1000
: undefined;
const email = typeof user.email === "string" && user.email ? user.email : undefined;
const username = typeof user.username === "string" && user.username ? user.username : undefined;
const name =
typeof user.name === "string" && user.name ? user.name : username;
return {
type: "oauth",
source: "atomcode-cli",
access,
...(typeof root.refresh_token === "string" && root.refresh_token
? { refresh: root.refresh_token }
: {}),
...(expires !== undefined ? { expires } : {}),
uid,
accountId: email || username || uid,
...(email ? { email } : {}),
...(name ? { name } : {}),
};
}
function tomlString(value) {
return JSON.stringify(String(value));
}
function scalarKind(raw) {
const text = splitComment(raw).value.trim();
if (text.startsWith('"') || text.startsWith("'")) return "string";
if (text === "true" || text === "false") return "boolean";
const numeric = text.replaceAll("_", "");
if (/^[+-]?\d+$/.test(numeric)) return "integer";
if (/^[+-]?(?:\d+\.\d*|\.\d+|\d+)(?:[eE][+-]?\d+)?$/.test(numeric)) {
return "float";
}
return "string";
}
function tomlValue(key, value, existing) {
if (existing !== undefined) {
const kind = scalarKind(existing);
if (kind === "integer") {
const number = Number(value);
return Number.isInteger(number) ? String(number) : tomlString(value);
}
if (kind === "float") {
const number = Number(value);
if (!Number.isFinite(number)) return tomlString(value);
const text = String(number);
return /[.eE]/.test(text) ? text : `${text}.0`;
}
if (kind === "boolean") {
return value === true || String(value).toLowerCase() === "true"
? "true"
: "false";
}
return tomlString(value);
}
if (key === "created_at" || key === "expires_in") {
const number = Number(value);
if (Number.isInteger(number)) return String(number);
}
return tomlString(value);
}
/**
* Return auth.toml text with only known credential lines changed.
*
* @param {string} text
* @param {object} next
* @returns {string}
*/
export function renderAuthFile(text, next) {
const current = collect(text);
const rootValues = {
access_token: next.access,
refresh_token: next.refresh,
created_at: next.createdAt,
expires_in: next.expiresIn,
};
const user = { ...current.user, ...(next.user ?? {}) };
const userValues = Object.fromEntries(Object.entries(user).filter(([key]) => USER_KEYS.has(key)));
const wanted = { "": rootValues, user: userValues };
const remaining = {
"": new Set(Object.entries(rootValues).filter(([, value]) => value !== undefined).map(([key]) => key)),
user: new Set(Object.entries(userValues).filter(([, value]) => value !== undefined).map(([key]) => key)),
};
const eol = String(text).includes("\r\n") ? "\r\n" : "\n";
const lines = String(text).split(/\r?\n/);
const output = [];
let section = "";
const appendMissing = () => {
for (const key of remaining[section] ?? []) {
const value = wanted[section]?.[key];
if (value !== undefined) output.push(`${key} = ${tomlValue(key, value)}`);
remaining[section]?.delete(key);
}
};
for (const line of lines) {
const header = line.match(/^\s*\[([^\]]+)\]\s*$/);
if (header) {
appendMissing();
section = header[1].trim() === "user" ? "user" : header[1].trim();
output.push(line);
continue;
}
const pair = line.match(/^(\s*)([A-Za-z_][A-Za-z0-9_-]*)(\s*=\s*)(.*)$/);
if (pair && wanted[section] && remaining[section]?.has(pair[2])) {
const value = wanted[section][pair[2]];
if (value !== undefined) {
const { comment } = splitComment(pair[4]);
output.push(
`${pair[1]}${pair[2]}${pair[3]}${tomlValue(pair[2], value, pair[4])}${comment}`,
);
} else {
output.push(line);
}
remaining[section].delete(pair[2]);
continue;
}
output.push(line);
}
appendMissing();
if (remaining.user?.size) {
output.push("[user]");
for (const key of remaining.user) {
const value = userValues[key];
if (value !== undefined) output.push(`${key} = ${tomlValue(key, value)}`);
}
}
return output.join(eol);
}
async function inspect(path) {
let info;
try {
info = await lstat(path);
} catch (error) {
if (error.code === "ENOENT") throw new Error("AtomCode CLI is not signed in on this computer");
throw error;
}
if (info.isSymbolicLink() || !info.isFile()) {
throw new Error("AtomCode auth.toml must be a regular file, not a symlink");
}
if (process.platform !== "win32" && (info.mode & 0o077) !== 0) {
throw new Error("AtomCode auth.toml permissions are wider than 0600");
}
return info;
}
/**
* Read and validate the CLI's auth file.
*
* @param {string} [path]
* @returns {Promise<object & {path: string, mtimeMs: number}>}
*/
export async function readAuthFile(path = defaultAuthFilePath()) {
const info = await inspect(path);
const text = await readFile(path, "utf8");
return { ...parseAuthFile(text), path, mtimeMs: info.mtimeMs };
}
async function atomicWrite(path, text) {
await mkdir(dirname(path), { recursive: true, mode: 0o700 });
const temp = `${path}.${process.pid}.${randomBytes(6).toString("hex")}.tmp`;
let handle;
try {
handle = await open(temp, "wx", 0o600);
await handle.writeFile(text, "utf8");
await handle.sync();
await handle.close();
handle = undefined;
await chmod(temp, 0o600).catch(() => {});
await rename(temp, path);
} catch (error) {
await handle?.close().catch(() => {});
await unlink(temp).catch(() => {});
throw error;
}
}
/**
* Atomically renew the CLI's credentials while preserving unrelated lines.
*
* @param {string} path
* @param {object} next
* @returns {Promise<object>}
*/
export async function writeAuthFile(path, next) {
await inspect(path);
const currentText = await readFile(path, "utf8");
parseAuthFile(currentText);
const createdAt = Number.isFinite(next.createdAt)
? Math.floor(next.createdAt)
: Math.floor(Date.now() / 1000);
const expiresIn = Number(next.expiresIn);
if (!next.access || !Number.isFinite(expiresIn) || expiresIn <= 0) {
throw new Error("refusing to write incomplete AtomCode credentials");
}
const text = renderAuthFile(currentText, {
access: next.access,
...(next.refresh ? { refresh: next.refresh } : {}),
createdAt,
expiresIn: Math.floor(expiresIn),
user: next.user,
});
parseAuthFile(text);
await atomicWrite(path, text);
return readAuthFile(path);
}