cloud / internal-packages / installer / worker / first-owner.jsmain at aacbc74
// first-owner.js — THE OWNER'S TICKET. A deployment that is new has no session and no passkey,
// so its owner would wait for a mailed code. A first deploy makes a ticket instead: the stack is
// told its hash (ITERATE_FIRST_OWNER_TICKET, core/alchemy/src/stack.ts) and the person's page is
// given the ticket, once. https://iterate.<domain>/keys/first takes it once, signs the owner in
// and lets them add a passkey. The Cloud stores neither the ticket nor its hash.
//
// A passkey is the only way to sign in to a stack once its member has one. So an owner who lost
// every passkey asks for a recovery run: an update that makes a ticket too. The stack honours it
// for an address its own config names as an owner, and for no other. Whoever can deploy to the
// Cloudflare account is the root; a run that nobody asked to recover makes no ticket.
// the stack refuses an end more than 24 hours ahead (core/auth-worker FIRST_OWNER_TICKET_MS); ten minutes spare
const LIFE_MS = 24 * 3600_000 - 10 * 60_000;
/** `{ ticket, expires, variable }`: 32 random bytes as base64url, when the ticket ends (epoch ms),
* and the variable's value for the run: the owner's address, the SHA-256 of the ticket's text as
* lowercase hex, and the end. */
export async function firstOwnerTicket(email) {
const ticket = btoa(String.fromCharCode(...crypto.getRandomValues(new Uint8Array(32))))
.replace(/\+/g, "-")
.replace(/\//g, "_")
.replace(/=+$/, "");
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(ticket));
const hash = [...new Uint8Array(digest)].map((b) => b.toString(16).padStart(2, "0")).join("");
const expires = Date.now() + LIFE_MS;
return { ticket, expires, variable: JSON.stringify({ email, hash, expires }) };
}
/** A run's answer with the ticket in its result, as the person's page gets it. */
export const withTicket = (answer, { ticket, expires }) => ({
...answer,
result: { ...answer.result, firstOwnerTicket: ticket, firstOwnerTicketEnds: expires },
});