Repository Public
1// Operator commands, run on the node that hosts SP Message.2import { config } from "./config.js"3import { migrate, sql } from "./db.js"4import { randomToken, sha256 } from "./util.js"56const [cmd, ...args] = process.argv.slice(2)7const flag = (name: string, d?: string) => {8 const i = args.indexOf(`--${name}`)9 return i >= 0 ? args[i + 1] : d10}1112async function main() {13 await migrate()14 switch (cmd) {15 case "bootstrap": {16 // One-time creation of the first administrator. Refused as soon as an administrator exists,17 // unless --recover is given (lost passkeys): the new administrator is then an additional account.18 const [{ n }] = await sql`select count(*)::int as n from users where role = 'admin' and status = 'active'`19 if (n > 0 && !args.includes("--recover")) throw new Error("An administrator already exists. Use --recover to create an additional one.")20 const ttl = Math.min(24 * 60, Math.max(1, Number(flag("ttl", "30"))))21 const token = randomToken(32)22 await sql`delete from bootstrap_tokens where used_at is null`23 await sql`insert into bootstrap_tokens (token_hash, expires_at) values (${sha256(token)}, now() + make_interval(mins => ${ttl}))`24 await sql`insert into audit_log (action, meta) values ('bootstrap.issue', ${sql.json({ ttl })})`25 console.log(`One-time administrator link (valid ${ttl} min, single use):\n\n ${config.origin}/bootstrap#${token}\n`)26 break27 }28 case "invite": {29 // Operator-issued invitation (for example before any administrator has signed in).30 const recipient = flag("for") ?? null31 const hours = Math.min(24 * 30, Math.max(1, Number(flag("hours", "72"))))32 const token = randomToken(32)33 const [admin] = await sql`select id from users where role = 'admin' and status = 'active' order by created_at limit 1`34 await sql`insert into invitations (token_hash, created_by, recipient, expires_at, max_uses, require_approval)35 values (${sha256(token)}, ${admin?.id ?? null}, ${recipient}, now() + make_interval(hours => ${hours}), 1, ${args.includes("--approval")})`36 await sql`insert into audit_log (action, meta) values ('invitation.create-cli', ${sql.json({ recipient, hours })})`37 console.log(`Invitation link${recipient ? ` for ${recipient}` : ""} (valid ${hours} h, single use):\n\n ${config.origin}/invite#${token}\n`)38 break39 }40 case "status": {41 const users = await sql`select handle, role, status, (identity_key is not null) as identity, created_at from users order by created_at`42 console.table(users)43 const [s] = await sql`select (select count(*)::int from conversations) as conversations, (select count(*)::int from messages) as ciphertexts, (select count(*)::int from files) as files`44 console.log(s)45 break46 }47 case "revoke-sessions": {48 const handle = args[0]49 if (!handle) throw new Error("usage: revoke-sessions <handle>")50 const r = await sql`update sessions set revoked_at = now() where revoked_at is null and user_id = (select id from users where handle = ${handle}) returning 1`51 console.log(`${r.length} session(s) revoked`)52 break53 }54 case "migrate":55 console.log("migrations up to date")56 break57 default:58 console.log("usage: cli <bootstrap [--ttl min] [--recover] | invite [--for name] [--hours n] [--approval] | status | revoke-sessions <handle> | migrate>")59 }60 await sql.end()61}62main().catch(async (e) => {63 console.error(String(e.message ?? e))64 await sql.end()65 process.exit(1)66})67