turno-cli 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,193 +0,0 @@
1
- import { DRY_RUN, JSON_FLAG, bool, flagsOf, parseFlags, str } from "../args.js";
2
- import { group } from "../group.js";
3
- import { nextStep } from "../hints.js";
4
- import { fail, needsAnswer, noInputReason } from "../io.js";
5
- import { emit, jsonOutput } from "../output.js";
6
- import { readHidden, readStdinText, readVisible, stdinIsTty } from "../secret-input.js";
7
- import { OPTIONAL, notInstalled } from "../optional.js";
8
- /**
9
- * `turno browser vault`: the credential vault of a browser identity. Values go
10
- * from a hidden prompt or stdin straight to turno-browser, which keeps them in the secret store;
11
- * nothing here prints, logs or passes one as an argument.
12
- */
13
- /** `turno browser vault list --json`. */
14
- const VAULT_JSON = jsonOutput(["identity", "submit", "sites"]);
15
- export const terminalInput = {
16
- isTty: stdinIsTty,
17
- hidden: readHidden,
18
- visible: readVisible,
19
- stdin: readStdinText,
20
- };
21
- const ADD_FLAGS = {
22
- url: { type: "string", multiple: true },
23
- username: { type: "string" },
24
- totp: { type: "boolean" },
25
- ...DRY_RUN,
26
- };
27
- function ids(mod, io, args, count, usage) {
28
- const words = args.slice(0, count);
29
- if (words.length < count || words.some((w) => !w))
30
- return fail(`usage: turno browser vault ${usage}`);
31
- try {
32
- mod.assertIdentityId(words[0]);
33
- }
34
- catch (err) {
35
- return fail(err.message);
36
- }
37
- if (!mod.identityExists(words[0], io.env))
38
- return fail(`browser identity "${words[0]}" does not exist (turno browser add ${words[0]})`);
39
- return words;
40
- }
41
- async function add(mod, args, io, input) {
42
- const p = parseFlags(args, ADD_FLAGS, 2);
43
- const [id, site] = ids(mod, io, p.positionals, 2, "add <identity> <site> --url <sign-in page> [--username <name>] [--totp]");
44
- const urls = p.values.url ?? [];
45
- if (!urls.length)
46
- return fail("give the sign-in page with --url (for example --url https://github.com/login)");
47
- const withTotp = bool(p, "totp");
48
- if (bool(p, "dry-run")) {
49
- io.out(`(dry run) would store a username, a password${withTotp ? " and a TOTP secret" : ""} for ${site} in browser identity "${id}"`);
50
- return 0;
51
- }
52
- let username = str(p, "username");
53
- let password;
54
- let totp;
55
- if (input.isTty()) {
56
- // Hidden prompts wait for a person: never with --no-input or inside an agent session.
57
- if (noInputReason(io.env))
58
- needsAnswer("browser vault add", "the password on stdin (and --username)");
59
- username ??= await input.visible(`Username for ${site}: `);
60
- password = await input.hidden(`Password for ${site} (input hidden): `);
61
- if (withTotp)
62
- totp = await input.hidden("TOTP secret or otpauth:// URI (input hidden): ");
63
- }
64
- else {
65
- if (!username)
66
- return fail("give --username when the password comes on stdin");
67
- const lines = (await input.stdin()).split(/\r?\n/);
68
- password = lines[0] ?? "";
69
- if (withTotp)
70
- totp = lines[1]?.trim() || undefined;
71
- if (withTotp && !totp)
72
- return fail("--totp reads the TOTP secret from the second line of stdin, and it is empty");
73
- }
74
- if (!password)
75
- return fail("the password is empty; nothing was stored");
76
- const entry = await mod.vaultAdd(id, site, { urls, username: username ?? "", password, ...(totp ? { totp } : {}) }, { env: io.env });
77
- io.out(`stored the credentials of ${entry.site} for browser identity "${id}" (username, password${entry.totp ? ", TOTP secret" : ""}) in the secret store`);
78
- io.out(`origins: ${entry.origins.join(", ")}`);
79
- nextStep(io, `an agent on browser-${id} signs in with the browser_login tool (site "${entry.site}") on that sign-in page`);
80
- return 0;
81
- }
82
- async function list(mod, args, io) {
83
- const p = parseFlags(args, JSON_FLAG, 1);
84
- const [id] = ids(mod, io, p.positionals, 1, "list <identity> [--json]");
85
- const info = await mod.vaultList(id, { env: io.env });
86
- if (bool(p, "json")) {
87
- emit(io, p, VAULT_JSON, info);
88
- return 0;
89
- }
90
- io.out(`browser identity "${id}": ${info.submit === "auto" ? "browser_login submits the form itself" : "browser_login fills, the agent clicks sign in"} (turno browser vault submit ${id} auto|confirm)`);
91
- if (!info.sites.length) {
92
- io.out(`No credentials yet. Add some with \`turno browser vault add ${id} <site> --url <sign-in page>\`.`);
93
- return 0;
94
- }
95
- const width = Math.max(...info.sites.map((s) => s.site.length));
96
- for (const s of info.sites) {
97
- const state = s.stored ? "" : " MISSING from the secret store";
98
- io.out(`${s.site.padEnd(width)} ${s.totp ? "password+totp" : "password "} ${s.origins.join(", ")}${state}`);
99
- }
100
- return info.sites.every((s) => s.stored) ? 0 : 1;
101
- }
102
- async function remove(mod, args, io) {
103
- const p = parseFlags(args, DRY_RUN, 2);
104
- const [id, site] = ids(mod, io, p.positionals, 2, "remove <identity> <site> [--dry-run]");
105
- if (bool(p, "dry-run")) {
106
- io.out(`(dry run) would delete the credentials of ${site} from browser identity "${id}"`);
107
- return 0;
108
- }
109
- if (!(await mod.vaultRemove(id, site, { env: io.env }))) {
110
- io.out(`browser identity "${id}" has no credentials for ${site}`);
111
- return 1;
112
- }
113
- io.out(`removed the credentials of ${site} from browser identity "${id}"`);
114
- return 0;
115
- }
116
- function submit(mod, args, io) {
117
- const p = parseFlags(args, DRY_RUN, 2);
118
- const [id, mode] = ids(mod, io, p.positionals, 2, "submit <identity> <confirm|auto>");
119
- if (mode !== "confirm" && mode !== "auto")
120
- return fail(`submit takes "confirm" (the agent clicks sign in) or "auto" (browser_login submits), not "${mode}"`);
121
- if (bool(p, "dry-run")) {
122
- io.out(`(dry run) would set browser_login of "${id}" to ${mode}`);
123
- return 0;
124
- }
125
- mod.vaultSetSubmit(id, mode, { env: io.env });
126
- io.out(mode === "auto"
127
- ? `browser_login now submits the sign-in form itself for "${id}"`
128
- : `browser_login now only fills the form for "${id}"; the agent clicks sign in`);
129
- return 0;
130
- }
131
- const FOOTER = `The vault keeps a username, a password and optionally a TOTP secret per site for one
132
- browser identity, only in the secret store (the macOS Keychain or the Linux Secret Service).
133
- The browser_login tool fills them into the site's sign-in page without the agent seeing them,
134
- and only on the origins given with --url. In a terminal the password and the TOTP secret are
135
- asked with hidden prompts; from a pipe, the first line of stdin is the password and, with
136
- --totp, the second line is the TOTP secret (base32, or the otpauth:// URI of the QR code).`;
137
- /** The `browser vault` group; `load` is the browser group's loader. */
138
- export function vaultCommand(deps, input = terminalInput) {
139
- const withModule = (run) => async (args, io) => {
140
- const mod = await deps.load(io);
141
- if (!mod)
142
- return fail(notInstalled(OPTIONAL.browser));
143
- try {
144
- return await run(mod, args, io);
145
- }
146
- catch (err) {
147
- const name = err.name;
148
- if (name === "VaultError" || name === "IdentityError" || name === "TotpError")
149
- return fail(err.message);
150
- throw err;
151
- }
152
- };
153
- const subs = {
154
- add: {
155
- summary: "store a site's username, password and optional TOTP secret (asked hidden, or on stdin)",
156
- synopsis: "<identity> <site> --url <sign-in page> [--url <url>]... [--username <name>] [--totp] [--dry-run]",
157
- flags: flagsOf(ADD_FLAGS),
158
- run: withModule((mod, args, io) => add(mod, args, io, input)),
159
- },
160
- list: {
161
- summary: "the sites in an identity's vault (never the values)",
162
- synopsis: "<identity> [--json]",
163
- flags: ["--json"],
164
- json: VAULT_JSON,
165
- run: withModule(list),
166
- },
167
- remove: {
168
- summary: "delete a site's credentials",
169
- synopsis: "<identity> <site> [--dry-run]",
170
- flags: ["--dry-run"],
171
- run: withModule(remove),
172
- },
173
- rm: { summary: "same as remove", hidden: true, run: withModule(remove) },
174
- submit: {
175
- summary: "let browser_login submit the form (auto) or leave the click to the agent (confirm, default)",
176
- synopsis: "<identity> <confirm|auto> [--dry-run]",
177
- flags: ["--dry-run"],
178
- run: withModule(submit),
179
- },
180
- };
181
- return group("browser vault", "credentials browser_login fills without showing them", subs, {
182
- footer: FOOTER,
183
- examples: [
184
- "turno browser vault add work github --url https://github.com/login --username alice --totp",
185
- "printf '%s\\n' \"$PASSWORD\" | turno browser vault add work mail --url https://mail.example.com --username alice@example.com",
186
- "turno browser vault list work",
187
- "turno browser vault list work --json",
188
- "turno browser vault submit work auto",
189
- "turno browser vault remove work github --dry-run",
190
- ],
191
- });
192
- }
193
- //# sourceMappingURL=browser-vault.js.map