askrobots / dbbasic-browser
Public
A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.
danq9
toolbar: Back and Forward are greyed out when there's nowhere to go
cc08c7a
1 week, 2 days ago
History
cli.ts · 4.3 KB · 118 lines
| 1 | #!/usr/bin/env node |
| 2 | /** |
| 3 | * dbbasic-vault — standalone CLI for the vault. |
| 4 | * |
| 5 | * dbbasic-vault init |
| 6 | * dbbasic-vault add <origin> <username> [--generate [length]] |
| 7 | * dbbasic-vault list |
| 8 | * dbbasic-vault get <origin|id> [username] prints the password (and only that) to stdout |
| 9 | * dbbasic-vault rm <id> |
| 10 | * dbbasic-vault gen [length] |
| 11 | * |
| 12 | * Vault file: --vault <path> | $DBB_VAULT | ~/.dbbasic/vault.json |
| 13 | * Master password: $DBB_VAULT_PASSWORD (for scripts/CI) or a hidden prompt. |
| 14 | */ |
| 15 | |
| 16 | import { mkdirSync } from "node:fs"; |
| 17 | import { homedir } from "node:os"; |
| 18 | import { dirname, join } from "node:path"; |
| 19 | import { Vault, VaultError, generatePassword, normalizeOrigin } from "./vault.ts"; |
| 20 | |
| 21 | const argv = process.argv.slice(2); |
| 22 | const flagIdx = argv.indexOf("--vault"); |
| 23 | const vaultPath = flagIdx >= 0 ? (argv.splice(flagIdx, 2)[1] ?? "") : process.env.DBB_VAULT || join(homedir(), ".dbbasic", "vault.json"); |
| 24 | const [cmd, ...rest] = argv; |
| 25 | |
| 26 | /** Read a line from the terminal without echoing it. */ |
| 27 | function readSecret(prompt: string): Promise<string> { |
| 28 | const { stdin, stderr } = process; |
| 29 | if (!stdin.isTTY) return Promise.reject(new Error("no terminal for a hidden prompt — set DBB_VAULT_PASSWORD")); |
| 30 | return new Promise((resolve) => { |
| 31 | stderr.write(prompt); |
| 32 | let buf = ""; |
| 33 | stdin.setRawMode(true); |
| 34 | stdin.resume(); |
| 35 | stdin.setEncoding("utf8"); |
| 36 | const onData = (ch: string) => { |
| 37 | for (const c of ch) { |
| 38 | if (c === "\r" || c === "\n" || c === "\u0004") { |
| 39 | stdin.setRawMode(false); |
| 40 | stdin.pause(); |
| 41 | stdin.off("data", onData); |
| 42 | stderr.write("\n"); |
| 43 | return resolve(buf); |
| 44 | } |
| 45 | if (c === "\u0003") { stdin.setRawMode(false); stderr.write("\n"); process.exit(130); } |
| 46 | if (c === "\u007f") buf = buf.slice(0, -1); |
| 47 | else buf += c; |
| 48 | } |
| 49 | }; |
| 50 | stdin.on("data", onData); |
| 51 | }); |
| 52 | } |
| 53 | |
| 54 | const master = async (confirm = false): Promise<string> => { |
| 55 | if (process.env.DBB_VAULT_PASSWORD) return process.env.DBB_VAULT_PASSWORD; |
| 56 | const m = await readSecret("Master password: "); |
| 57 | if (confirm && m !== (await readSecret("Again: "))) throw new Error("passwords did not match"); |
| 58 | return m; |
| 59 | }; |
| 60 | |
| 61 | const open = async () => Vault.unlock(vaultPath, await master()); |
| 62 | |
| 63 | function fail(msg: string): never { |
| 64 | process.stderr.write(`dbbasic-vault: ${msg}\n`); |
| 65 | process.exit(1); |
| 66 | } |
| 67 | |
| 68 | try { |
| 69 | switch (cmd) { |
| 70 | case "init": { |
| 71 | mkdirSync(dirname(vaultPath), { recursive: true, mode: 0o700 }); |
| 72 | await Vault.create(vaultPath, await master(true)); |
| 73 | console.error(`created ${vaultPath}`); |
| 74 | break; |
| 75 | } |
| 76 | case "add": { |
| 77 | const [origin, username, ...flags] = rest; |
| 78 | if (!origin || !username) fail("usage: add <origin> <username> [--generate [length]]"); |
| 79 | const gi = flags.indexOf("--generate"); |
| 80 | const password = gi >= 0 ? generatePassword(Number(flags[gi + 1]) || 20) : await readSecret(`Password for ${username}@${normalizeOrigin(origin)}: `); |
| 81 | const v = await open(); |
| 82 | const s = await v.add({ origin, username, password }); |
| 83 | console.error(`saved ${s.username} @ ${s.origin} (${s.id})`); |
| 84 | if (gi >= 0) console.log(password); |
| 85 | break; |
| 86 | } |
| 87 | case "list": { |
| 88 | const v = await open(); |
| 89 | for (const e of v.list()) console.log(`${e.id} ${e.origin} ${e.username}`); |
| 90 | break; |
| 91 | } |
| 92 | case "get": { |
| 93 | const [key, username] = rest; |
| 94 | if (!key) fail("usage: get <origin|id> [username]"); |
| 95 | const v = await open(); |
| 96 | const byId = v.list().find((e) => e.id === key); |
| 97 | const matches = byId ? [byId] : v.find(key).filter((e) => !username || e.username === username); |
| 98 | if (matches.length === 0) fail("no matching credential"); |
| 99 | if (matches.length > 1) fail(`${matches.length} credentials match — pass a username or an id`); |
| 100 | process.stdout.write(v.get(matches[0]!.id).password + (process.stdout.isTTY ? "\n" : "")); |
| 101 | break; |
| 102 | } |
| 103 | case "rm": { |
| 104 | if (!rest[0]) fail("usage: rm <id>"); |
| 105 | await (await open()).remove(rest[0]); |
| 106 | console.error("removed"); |
| 107 | break; |
| 108 | } |
| 109 | case "gen": |
| 110 | console.log(generatePassword(Number(rest[0]) || 20)); |
| 111 | break; |
| 112 | default: |
| 113 | console.error("usage: dbbasic-vault [--vault <path>] <init|add|list|get|rm|gen> …"); |
| 114 | process.exit(cmd ? 2 : 0); |
| 115 | } |
| 116 | } catch (e) { |
| 117 | fail(e instanceof VaultError || e instanceof Error ? e.message : String(e)); |
| 118 | } |