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.
121 lines
4.6 KiB
JavaScript
121 lines
4.6 KiB
JavaScript
/**
|
|
* `atomcode-signing-v1` — the request signature the AtomGit LLM gateway
|
|
* requires (`X-AtomCode-*` headers).
|
|
*
|
|
* Port of the community-documented implementation (Atom2Api, MIT). Two facts
|
|
* this code must not drift from:
|
|
*
|
|
* - the salt is `uid bytes || 0x01 || le64(hour-bucket) || sha256(oauth_token)
|
|
* || sha256(client_version)`, and HKDF's info is the bare string
|
|
* `"atomcode-signing-v1"` (no trailing 0x01), 32 bytes out;
|
|
* - the canonical is `v1\nMETHOD\nPATH\nTS\nNONCE-hex\nsha256(body)-hex`,
|
|
* where PATH is the full request path with its `/v1` prefix.
|
|
*
|
|
* `test/sign.test.mjs` holds Atom2Api's independent fixture as the guard.
|
|
*
|
|
* The signature always covers the bytes actually sent: patch the body first,
|
|
* sign second. Nothing here reads the network or the clock silently — both are
|
|
* parameters for tests.
|
|
*
|
|
* @module magpie-atomcode/sign
|
|
*/
|
|
|
|
import { createHash, createHmac, hkdfSync, randomBytes } from "node:crypto";
|
|
|
|
/**
|
|
* The hard-coded master key Atom2Api ships (two independent community
|
|
* implementations agree on it). Overridable per request in case upstream
|
|
* rotates it without warning.
|
|
*/
|
|
export const MASTER_KEY_HEX = "e97250f05303162c8ecd68c688b2f55c1d81e508d243d88466472e7f54637123";
|
|
|
|
/** HKDF info label, exactly as upstream spells it. */
|
|
export const SIGNING_INFO = "atomcode-signing-v1";
|
|
|
|
/** A fresh 16-byte nonce (32 hex chars once stringified). */
|
|
export function newNonce() {
|
|
return randomBytes(16);
|
|
}
|
|
|
|
/** Coerce a request body into what `createHash().update()` accepts. */
|
|
function toHashInput(body) {
|
|
if (body === undefined || body === null || body === "") return "";
|
|
if (typeof body === "string") return body;
|
|
if (Buffer.isBuffer(body)) return body;
|
|
if (body instanceof Uint8Array) return body;
|
|
if (ArrayBuffer.isView(body)) {
|
|
return new Uint8Array(body.buffer, body.byteOffset, body.byteLength);
|
|
}
|
|
if (body instanceof ArrayBuffer) return new Uint8Array(body);
|
|
throw new Error("unsupported request body type for signing");
|
|
}
|
|
|
|
/**
|
|
* Build the five `X-AtomCode-*` headers for one request.
|
|
*
|
|
* @param {object} params
|
|
* @param {string} params.method - HTTP method ("POST" …).
|
|
* @param {string} params.path - full request path incl. `/v1`, no query.
|
|
* @param {string|Buffer|ArrayBufferView|ArrayBuffer} [params.body] - what will be sent; "" hashes as the empty body.
|
|
* @param {string} params.oauthToken - the bearer token.
|
|
* @param {string} params.userId - the upstream `user.id` (enters the salt).
|
|
* @param {string} params.clientVersion - `X-AtomCode-Ver` value, e.g. "5.2.3".
|
|
* @param {number} [params.timestamp] - unix seconds; defaults to now.
|
|
* @param {Buffer} [params.nonce] - 16 bytes; defaults to `newNonce()`.
|
|
* @param {string} [params.masterKeyHex] - 64 hex chars; defaults to `MASTER_KEY_HEX`.
|
|
* @returns {object} the five headers, ready to merge into a Headers object.
|
|
*/
|
|
export function signAtomGitRequest({
|
|
method,
|
|
path,
|
|
body = "",
|
|
oauthToken,
|
|
userId,
|
|
clientVersion,
|
|
timestamp = Math.floor(Date.now() / 1000),
|
|
nonce = newNonce(),
|
|
masterKeyHex = MASTER_KEY_HEX,
|
|
}) {
|
|
if (typeof method !== "string" || !method) throw new Error("signing requires a method");
|
|
if (typeof path !== "string" || !path.startsWith("/")) {
|
|
throw new Error("signing requires an absolute request path (starting with /)");
|
|
}
|
|
if (!oauthToken) throw new Error("signing requires an oauth token");
|
|
if (userId === undefined || userId === null || userId === "") {
|
|
throw new Error("signing requires the account's user id");
|
|
}
|
|
if (!clientVersion) throw new Error("signing requires a client version");
|
|
|
|
const masterKey = Buffer.from(masterKeyHex, "hex");
|
|
const timeBucket = Buffer.alloc(8);
|
|
timeBucket.writeBigUInt64LE(BigInt(Math.floor(timestamp / 3600)));
|
|
const salt = Buffer.concat([
|
|
Buffer.from(String(userId), "utf8"),
|
|
Buffer.from([1]),
|
|
timeBucket,
|
|
createHash("sha256").update(oauthToken).digest(),
|
|
createHash("sha256").update(String(clientVersion)).digest(),
|
|
]);
|
|
const signingKey = Buffer.from(hkdfSync("sha256", masterKey, salt, Buffer.from(SIGNING_INFO), 32));
|
|
|
|
const bodyHash = createHash("sha256").update(toHashInput(body)).digest("hex");
|
|
const nonceHex = Buffer.isBuffer(nonce) ? nonce.toString("hex") : String(nonce);
|
|
const canonical = [
|
|
"v1",
|
|
method.toUpperCase(),
|
|
path,
|
|
String(timestamp),
|
|
nonceHex,
|
|
bodyHash,
|
|
].join("\n");
|
|
|
|
const sig = createHmac("sha256", signingKey).update(canonical).digest("hex");
|
|
return {
|
|
"X-AtomCode-Sig": "v1:" + sig,
|
|
"X-AtomCode-Ts": String(timestamp),
|
|
"X-AtomCode-Nonce": nonceHex,
|
|
"X-AtomCode-Alg": "1",
|
|
"X-AtomCode-Ver": String(clientVersion),
|
|
};
|
|
}
|