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:
@@ -14,6 +14,10 @@ export interface Status {
|
||||
difficulty: number;
|
||||
/** Proof-of-work bits a message needs; absent on nodes without messaging. */
|
||||
message_difficulty?: number;
|
||||
/** When this node's device certificate runs out, in unix seconds; absent on older nodes. */
|
||||
cert_expires_at?: number;
|
||||
/** Whether the owner can renew that certificate here (it needs the recovery phrase). */
|
||||
can_renew?: boolean;
|
||||
}
|
||||
|
||||
async function getJSON(url: string): Promise<unknown> {
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
// Renewing the node's device certificate from the browser.
|
||||
//
|
||||
// A certificate lasts at most a year. Within the last 30 days the owner's page
|
||||
// says so and offers the renewal: the owner types the twelve words once, the
|
||||
// node signs a new certificate with them and forgets them. The device key does
|
||||
// not change, so the node keeps its peer ID and every listing stays valid.
|
||||
|
||||
import { HttpError } from "./api.ts";
|
||||
import { append, clear, h } from "./dom.ts";
|
||||
import { date } from "./format.ts";
|
||||
import { t } from "./i18n.ts";
|
||||
import { Unauthorized, renewCertificate } from "./ownerApi.ts";
|
||||
import { nowSeconds, state } from "./state.ts";
|
||||
|
||||
/** How long before the end the page starts asking. */
|
||||
export const RENEW_WINDOW_DAYS = 30;
|
||||
|
||||
function describe(e: unknown): string {
|
||||
if (e instanceof Unauthorized) return t("reg.signedOut");
|
||||
if (e instanceof HttpError) return e.detail;
|
||||
return t("send.network");
|
||||
}
|
||||
|
||||
/**
|
||||
* The renewal panel, or null when there is nothing to say: the certificate has
|
||||
* plenty of time left, or the node is too old to report it.
|
||||
*/
|
||||
export function certificatePanel(): HTMLElement | null {
|
||||
const expires = state.node?.cert_expires_at;
|
||||
if (expires === undefined) return null;
|
||||
const days = Math.floor((expires - nowSeconds()) / 86_400);
|
||||
if (days > RENEW_WINDOW_DAYS) return null;
|
||||
|
||||
const root = h("section", { class: "panel certificate" });
|
||||
const lead = days >= 0 ? t("cert.soon", { date: date(expires), n: days }) : t("cert.expired", { date: date(expires) });
|
||||
append(root, [h("h2", { class: "pane-title" }, t("cert.title")), h("p", { class: "note warn", role: "status" }, lead)]);
|
||||
|
||||
if (!state.node?.can_renew) {
|
||||
append(root, [h("p", { class: "muted" }, t("cert.noPhrase"))]);
|
||||
return root;
|
||||
}
|
||||
|
||||
const phrase = h("input", {
|
||||
type: "password",
|
||||
name: "phrase",
|
||||
autocomplete: "off",
|
||||
autocapitalize: "none",
|
||||
spellcheck: "false", // a bare false would drop the attribute
|
||||
"aria-label": t("cert.phrase"),
|
||||
placeholder: t("cert.phrase"),
|
||||
}) as HTMLInputElement;
|
||||
const status = h("p", { class: "muted", role: "status" });
|
||||
const problem = h("p", { class: "error", role: "alert" });
|
||||
const button = h("button", { type: "submit", class: "btn primary" }, t("cert.renew")) as HTMLButtonElement;
|
||||
const form = h(
|
||||
"form",
|
||||
{
|
||||
class: "compose",
|
||||
onsubmit: (ev: Event) => {
|
||||
ev.preventDefault();
|
||||
const words = phrase.value.trim();
|
||||
problem.textContent = "";
|
||||
if (!words) {
|
||||
problem.textContent = t("cert.fail", { detail: t("cert.phrase") });
|
||||
return;
|
||||
}
|
||||
button.disabled = true;
|
||||
status.textContent = t("cert.working");
|
||||
void renewCertificate(words)
|
||||
.then((until) => {
|
||||
phrase.value = ""; // the words are not kept anywhere on this page
|
||||
if (state.node) state.node.cert_expires_at = until;
|
||||
clear(root);
|
||||
append(root, [h("h2", { class: "pane-title" }, t("cert.title")), h("p", { role: "status" }, t("cert.done", { date: date(until) }))]);
|
||||
})
|
||||
.catch((e: unknown) => {
|
||||
button.disabled = false;
|
||||
status.textContent = "";
|
||||
problem.textContent = t("cert.fail", { detail: describe(e) });
|
||||
});
|
||||
},
|
||||
},
|
||||
h("p", { class: "muted" }, t("cert.intro")),
|
||||
phrase,
|
||||
h("div", { class: "actions" }, button),
|
||||
status,
|
||||
problem,
|
||||
);
|
||||
root.append(form);
|
||||
return root;
|
||||
}
|
||||
@@ -94,6 +94,16 @@ const de = {
|
||||
"rep.done": "Gemeldet.",
|
||||
"rep.withdrawn": "Zurückgezogen.",
|
||||
"rep.mine": "Deine Meldung: {what}.",
|
||||
"cert.title": "Geräte-Zertifikat",
|
||||
"cert.soon": "Das Zertifikat dieses Knotens läuft am {date} ab (in {n} Tagen). Danach geht der Knoten nicht mehr an und deine Anzeigen verschwinden.",
|
||||
"cert.expired": "Das Zertifikat dieses Knotens ist am {date} abgelaufen. Erneuere es jetzt.",
|
||||
"cert.noPhrase": "Diese Identität hat keine Wiederherstellungsphrase und lässt sich nicht erneuern. Lege mit „allmende init --no-phrase“ in einem leeren Datenverzeichnis eine neue an.",
|
||||
"cert.intro": "Gib die zwölf Wörter deiner Wiederherstellungsphrase ein. Der Knoten stellt damit ein neues Zertifikat aus und vergisst sie sofort wieder. Der Schlüssel des Geräts bleibt gleich, nichts wird widerrufen.",
|
||||
"cert.phrase": "Die zwölf Wörter",
|
||||
"cert.renew": "Zertifikat erneuern",
|
||||
"cert.working": "Wird erneuert …",
|
||||
"cert.done": "Erneuert. Das Zertifikat gilt jetzt bis zum {date}.",
|
||||
"cert.fail": "Das hat nicht geklappt: {detail}",
|
||||
"rep.title": "Meldungen",
|
||||
"rep.tipsIntro": "Hinweise von Besuchern dieses Knotens. Sie blenden nichts aus: du entscheidest, ob du selbst meldest.",
|
||||
"rep.none": "Keine Hinweise von Besuchern.",
|
||||
@@ -407,6 +417,16 @@ const en: Record<keyof typeof de, string> = {
|
||||
"rep.done": "Reported.",
|
||||
"rep.withdrawn": "Withdrawn.",
|
||||
"rep.mine": "Your report: {what}.",
|
||||
"cert.title": "Device certificate",
|
||||
"cert.soon": "This node's certificate expires on {date} (in {n} days). After that the node will not start and your listings disappear.",
|
||||
"cert.expired": "This node's certificate expired on {date}. Renew it now.",
|
||||
"cert.noPhrase": "This identity has no recovery phrase, so it cannot be renewed. Make a new one with “allmende init --no-phrase” in an empty data directory.",
|
||||
"cert.intro": "Enter the twelve words of your recovery phrase. The node signs a new certificate with them and forgets them at once. The device key stays the same and nothing is revoked.",
|
||||
"cert.phrase": "The twelve words",
|
||||
"cert.renew": "Renew certificate",
|
||||
"cert.working": "Renewing…",
|
||||
"cert.done": "Renewed. The certificate is now valid until {date}.",
|
||||
"cert.fail": "That did not work: {detail}",
|
||||
"rep.title": "Reports",
|
||||
"rep.tipsIntro": "Tips from visitors of this node. They hide nothing: you decide whether to report yourself.",
|
||||
"rep.none": "No tips from visitors.",
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
// with textContent only, never as links.
|
||||
|
||||
import { HttpError } from "./api.ts";
|
||||
import { certificatePanel } from "./certificate.ts";
|
||||
import { append, clear, h, sendOnCtrlEnter } from "./dom.ts";
|
||||
import { keepFresh } from "./fresh.ts";
|
||||
import { date, price, when } from "./format.ts";
|
||||
@@ -87,6 +88,7 @@ export async function showOwner(listingId?: string, thread?: string): Promise<vo
|
||||
const listingsPane = h(
|
||||
"section",
|
||||
{ class: "pane pane-listings" },
|
||||
certificatePanel(),
|
||||
h("h2", { class: "pane-title" }, t("own.listings")),
|
||||
h("p", null, h("a", { class: "btn primary", href: "#/mine/new" }, t("own.new"))),
|
||||
listingsBox,
|
||||
|
||||
@@ -224,6 +224,15 @@ export async function setRegions(regions: string[]): Promise<string[]> {
|
||||
return ((await call("PUT", "/api/v1/regions", { regions })) as { regions: string[] }).regions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renews the node's device certificate from the recovery phrase, which is
|
||||
* used for this one request. The device key stays, so nothing else changes.
|
||||
* Returns when the new certificate runs out, in unix seconds.
|
||||
*/
|
||||
export async function renewCertificate(phrase: string): Promise<number> {
|
||||
return ((await call("PUT", "/api/v1/certificate", { phrase })) as { cert_expires_at: number }).cert_expires_at;
|
||||
}
|
||||
|
||||
export async function createListing(d: Draft): Promise<Listing> {
|
||||
return (await call("POST", "/api/v1/listings", d)) as Listing;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user