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.
115 lines
4.0 KiB
JavaScript
115 lines
4.0 KiB
JavaScript
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
|
|
import {
|
|
checkLogin,
|
|
refreshTokens,
|
|
startLogin,
|
|
stripForceLogin,
|
|
tokenFor,
|
|
} from "../oauth.mjs";
|
|
|
|
function sequenceFetch(responses) {
|
|
const calls = [];
|
|
const fetchImpl = async (url, init = {}) => {
|
|
calls.push({ url, init });
|
|
const next = responses[Math.min(calls.length - 1, responses.length - 1)];
|
|
if (next.error) throw next.error;
|
|
return new Response(next.body ?? "", { status: next.status ?? 200 });
|
|
};
|
|
return { fetchImpl, calls };
|
|
}
|
|
|
|
test("startLogin strips force_login and validates the returned URL", async () => {
|
|
const { fetchImpl, calls } = sequenceFetch([
|
|
{
|
|
body: JSON.stringify({
|
|
login_url: "https://atomgit.com/login?force_login=true&next=%2Fhome",
|
|
state: "state-1",
|
|
}),
|
|
},
|
|
]);
|
|
const result = await startLogin("https://acs.atomgit.com", { fetchImpl });
|
|
|
|
assert.equal(result.loginUrl, "https://atomgit.com/login?next=%2Fhome");
|
|
assert.equal(result.state, "state-1");
|
|
assert.equal(calls[0].init.redirect, "error");
|
|
});
|
|
|
|
test("startLogin refuses a broker-supplied login URL outside AtomGit", async () => {
|
|
const { fetchImpl } = sequenceFetch([
|
|
{ body: JSON.stringify({ login_url: "https://evil.example/login", state: "state-1" }) },
|
|
]);
|
|
|
|
await assert.rejects(() => startLogin("https://acs.atomgit.com", { fetchImpl }), /untrusted login URL/);
|
|
});
|
|
|
|
test("checkLogin encodes state and only accepts a strict true", async () => {
|
|
const { fetchImpl, calls } = sequenceFetch([{ body: JSON.stringify({ valid: true }) }]);
|
|
assert.equal(await checkLogin("https://acs.atomgit.com", "a/b c", { fetchImpl }), true);
|
|
assert.equal(new URL(calls[0].url).searchParams.get("state"), "a/b c");
|
|
});
|
|
|
|
test("tokenFor validates and maps the credential response", async () => {
|
|
const { fetchImpl } = sequenceFetch([
|
|
{
|
|
body: JSON.stringify({
|
|
access_token: "access",
|
|
refresh_token: "refresh",
|
|
expires_in: 3600,
|
|
token_type: "Bearer",
|
|
user: { id: 42, email: "u@example.com" },
|
|
}),
|
|
},
|
|
]);
|
|
const token = await tokenFor("https://acs.atomgit.com", "state", { fetchImpl });
|
|
assert.equal(token.accessToken, "access");
|
|
assert.equal(token.refreshToken, "refresh");
|
|
assert.equal(token.expiresIn, 3600);
|
|
assert.equal(token.user.id, 42);
|
|
});
|
|
|
|
test("refreshTokens keeps the old refresh token when upstream does not rotate", async () => {
|
|
const { fetchImpl, calls } = sequenceFetch([
|
|
{ body: JSON.stringify({ access_token: "new-access", expires_in: 7200 }) },
|
|
]);
|
|
const token = await refreshTokens("https://acs.atomgit.com", "old-refresh", { fetchImpl });
|
|
|
|
assert.equal(token.accessToken, "new-access");
|
|
assert.equal(token.refreshToken, "old-refresh");
|
|
assert.equal(token.expiresIn, 7200);
|
|
assert.equal(JSON.parse(calls[0].init.body).refresh_token, "old-refresh");
|
|
assert.equal(calls[0].init.redirect, "error");
|
|
});
|
|
|
|
test("refreshTokens marks only credential failures as expired", async () => {
|
|
for (const status of [400, 401, 403]) {
|
|
const { fetchImpl } = sequenceFetch([{ status, body: "{}" }]);
|
|
await assert.rejects(
|
|
() => refreshTokens("https://acs.atomgit.com", "refresh", { fetchImpl }),
|
|
(error) => error.signIn === "expired",
|
|
);
|
|
}
|
|
|
|
const { fetchImpl } = sequenceFetch([{ status: 503, body: "unavailable" }]);
|
|
await assert.rejects(
|
|
() => refreshTokens("https://acs.atomgit.com", "refresh", { fetchImpl }),
|
|
(error) => error.status === 503 && error.signIn === undefined,
|
|
);
|
|
});
|
|
|
|
test("OAuth responses are bounded before JSON parsing", async () => {
|
|
const chunk = new Uint8Array(64 * 1024);
|
|
const stream = new ReadableStream({
|
|
pull(controller) {
|
|
controller.enqueue(chunk);
|
|
},
|
|
});
|
|
const { fetchImpl } = sequenceFetch([{ body: stream }]);
|
|
await assert.rejects(() => checkLogin("https://acs.atomgit.com", "state", { fetchImpl }), /exceeds 1 MiB/);
|
|
});
|
|
|
|
test("stripForceLogin tolerates malformed URLs with the legacy fallback", () => {
|
|
assert.equal(stripForceLogin("not a url&force_login=true"), "not a url");
|
|
});
|