Renew the device certificate from the browser, with the recovery phrase
Within the last 30 days the owner page offers it. The node signs a new certificate for the same device key (peer ID and listings stay), saves it, and signs again own listings that would outlive the old certificate. The phrase is used for one request and not kept; another identity's phrase and no-phrase identities are refused. Device.Current gives the certificate to sign with while a renewal happens. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { afterEach, test } from "node:test";
|
||||
|
||||
import { NOW, openPage, startGateway, type Gateway, type Page } from "./helpers.ts";
|
||||
|
||||
const TOKEN = "ef56".repeat(16);
|
||||
const DAY = 86_400;
|
||||
const PHRASE = "legal winner thank year wave sausage worth useful legal winner thank yellow";
|
||||
|
||||
let gw: Gateway | undefined;
|
||||
let page: Page | undefined;
|
||||
let calls: { method: string; path: string; body: string; auth: string }[] = [];
|
||||
|
||||
afterEach(async () => {
|
||||
page?.close();
|
||||
page = undefined;
|
||||
await gw?.close();
|
||||
gw = undefined;
|
||||
});
|
||||
|
||||
/** A node whose certificate runs out in `days` days, with an owner who may sign in. */
|
||||
async function node(days: number | null, canRenew = true, answer?: { status: number; body: unknown }): Promise<void> {
|
||||
gw = await startGateway({ listings: [], ...(days === null ? {} : { cert: { expires_at: NOW + days * DAY, can_renew: canRenew } }) });
|
||||
calls = [];
|
||||
gw.owner = (r) => {
|
||||
calls.push({ method: r.method, path: r.path, body: r.body, auth: r.auth });
|
||||
if (r.auth !== `Bearer ${TOKEN}`) return { status: 401, body: { error: "unauthorized" } };
|
||||
if (r.path === "/api/v1/mine") return { status: 200, body: [] };
|
||||
if (r.path === "/api/v1/certificate" && r.method === "PUT") {
|
||||
return answer ?? { status: 200, body: { cert_expires_at: NOW + 365 * DAY } };
|
||||
}
|
||||
return undefined;
|
||||
};
|
||||
page = openPage(gw.url, `#/login/${TOKEN}`);
|
||||
await page.waitFor(() => page!.document.querySelector(".pane-listings") !== null, "the owner page");
|
||||
}
|
||||
|
||||
const text = () => page!.document.body.textContent ?? "";
|
||||
const panel = () => page!.document.querySelector("section.certificate");
|
||||
const renew = () => [...page!.document.querySelectorAll("button")].find((b) => b.textContent === "Renew certificate") as HTMLButtonElement | undefined;
|
||||
|
||||
function submit(words: string): void {
|
||||
const form = panel()!.querySelector("form")!;
|
||||
(form.querySelector("input[name=phrase]") as HTMLInputElement).value = words;
|
||||
form.dispatchEvent(new page!.window.Event("submit", { cancelable: true }));
|
||||
}
|
||||
|
||||
test("with plenty of time left the owner page says nothing about the certificate", async () => {
|
||||
await node(200);
|
||||
assert.equal(panel(), null);
|
||||
assert.doesNotMatch(text(), /certificate/i);
|
||||
});
|
||||
|
||||
test("an older node that does not report the certificate shows no panel", async () => {
|
||||
await node(null);
|
||||
assert.equal(panel(), null);
|
||||
});
|
||||
|
||||
test("in the last 30 days it says when the certificate ends and offers to renew it", async () => {
|
||||
await node(12);
|
||||
assert.ok(panel(), "the panel");
|
||||
assert.match(panel()!.textContent!, /expires on .* \(in 12 days\)/);
|
||||
assert.ok(renew(), "the renew button");
|
||||
const input = panel()!.querySelector("input[name=phrase]") as HTMLInputElement;
|
||||
assert.equal(input.type, "password", "the words are not shown while typing");
|
||||
assert.equal(input.getAttribute("autocomplete"), "off");
|
||||
});
|
||||
|
||||
test("exactly 30 days left is the first day it asks, 31 is not", async () => {
|
||||
await node(30);
|
||||
assert.ok(panel(), "30 days left");
|
||||
page!.close();
|
||||
await gw!.close();
|
||||
await node(31);
|
||||
assert.equal(panel(), null, "31 days left");
|
||||
});
|
||||
|
||||
test("renewing sends the words once, with the token, and the panel then confirms", async () => {
|
||||
await node(12);
|
||||
submit(` ${PHRASE} `);
|
||||
await page!.waitFor(() => /Renewed\. The certificate is now valid until/.test(text()), "the confirmation");
|
||||
const puts = calls.filter((c) => c.path === "/api/v1/certificate");
|
||||
assert.equal(puts.length, 1);
|
||||
assert.equal(puts[0]!.method, "PUT");
|
||||
assert.equal(puts[0]!.auth, `Bearer ${TOKEN}`);
|
||||
assert.deepEqual(JSON.parse(puts[0]!.body), { phrase: PHRASE }, "the words arrive trimmed, in a JSON body");
|
||||
assert.equal(panel()!.querySelector("form"), null, "the form is gone");
|
||||
const left = [...page!.document.querySelectorAll("input, textarea")].filter((i) => (i as HTMLInputElement).value.includes("winner"));
|
||||
assert.equal(left.length, 0, "the words are not left in any field on the page");
|
||||
});
|
||||
|
||||
test("empty words are not sent", async () => {
|
||||
await node(12);
|
||||
submit(" ");
|
||||
assert.match(panel()!.textContent!, /That did not work/);
|
||||
assert.equal(calls.filter((c) => c.path === "/api/v1/certificate").length, 0);
|
||||
});
|
||||
|
||||
test("a refusal is shown, the words stay for another try, and the button works again", async () => {
|
||||
await node(12, true, { status: 409, body: { error: "these words belong to a different identity than this node" } });
|
||||
submit(PHRASE);
|
||||
await page!.waitFor(() => /different identity/.test(text()), "the refusal");
|
||||
assert.match(panel()!.textContent!, /That did not work: these words belong to a different identity than this node/);
|
||||
assert.equal(renew()!.disabled, false);
|
||||
assert.equal((panel()!.querySelector("input[name=phrase]") as HTMLInputElement).value, PHRASE);
|
||||
assert.ok(panel()!.querySelector("form"), "the form is still there");
|
||||
});
|
||||
|
||||
test("an identity without a recovery phrase is told so and gets no form", async () => {
|
||||
await node(5, false);
|
||||
assert.match(panel()!.textContent!, /no recovery phrase/);
|
||||
assert.equal(panel()!.querySelector("form"), null);
|
||||
assert.equal(renew(), undefined);
|
||||
});
|
||||
|
||||
test("a certificate that already ran out says so", async () => {
|
||||
await node(-2);
|
||||
assert.match(panel()!.textContent!, /expired on/);
|
||||
assert.ok(renew(), "it can still be renewed while the node runs");
|
||||
});
|
||||
+4
-1
@@ -204,6 +204,8 @@ export interface GatewayOptions {
|
||||
statusFails?: boolean;
|
||||
/** The regions the node reports; default ["u1x"]. */
|
||||
regions?: string[];
|
||||
/** The device certificate the node reports in /status; unset, it says nothing (an older node). */
|
||||
cert?: { expires_at: number; can_renew: boolean };
|
||||
}
|
||||
|
||||
export async function startGateway(opts: GatewayOptions): Promise<Gateway> {
|
||||
@@ -245,7 +247,7 @@ export async function startGateway(opts: GatewayOptions): Promise<Gateway> {
|
||||
}
|
||||
// The owner's token-protected routes are played by the test, if it sets `owner`.
|
||||
const writesListing = req.method !== "GET" && /^\/api\/v1\/listings(\/[0-9a-f]{32})?$/.test(url.pathname);
|
||||
if (writesListing || /^\/api\/v1\/(mine|inbox|regions|blocked|signals|reports)|^\/api\/v1\/listings\/[0-9a-f]{32}\/address$/.test(url.pathname)) {
|
||||
if (writesListing || /^\/api\/v1\/(mine|inbox|regions|blocked|signals|reports|certificate)|^\/api\/v1\/listings\/[0-9a-f]{32}\/address$/.test(url.pathname)) {
|
||||
let body = "";
|
||||
req.on("data", (c) => (body += c));
|
||||
req.on("end", () => {
|
||||
@@ -292,6 +294,7 @@ export async function startGateway(opts: GatewayOptions): Promise<Gateway> {
|
||||
listings: opts.listings.length,
|
||||
difficulty: DIFFICULTY,
|
||||
message_difficulty: 6,
|
||||
...(opts.cert ? { cert_expires_at: opts.cert.expires_at, can_renew: opts.cert.can_renew } : {}),
|
||||
});
|
||||
}
|
||||
if (url.pathname === "/api/v1/listings") {
|
||||
|
||||
Reference in New Issue
Block a user