pi-ai-vault 0.13.0 → 0.14.1
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.
- package/README.md +3 -2
- package/README.zh.md +4 -3
- package/package.json +2 -2
- package/src/bitwarden.ts +3 -3
- package/src/cli.ts +11 -5
- package/src/ente.ts +2 -2
- package/src/index.ts +121 -51
- package/src/settings.ts +14 -2
package/README.md
CHANGED
|
@@ -56,11 +56,12 @@ When the local store asks for its master password is a per-machine setting (`/va
|
|
|
56
56
|
|
|
57
57
|
| Mode | Behaviour |
|
|
58
58
|
|---|---|
|
|
59
|
+
| `every` | asks on every call; nothing is kept that could open the store |
|
|
59
60
|
| `never` | never asks, and reads open no window — the "the agent may use these freely" passwords |
|
|
60
61
|
|
|
61
|
-
By default (`
|
|
62
|
+
By default (`time`) a read opens no window either: the master password that unlocked the session is the approval, and every read is audited. `/vault confirm on` puts a window on every read.
|
|
62
63
|
| `time` | asks again N hours after the unlock (default 8) |
|
|
63
|
-
| `
|
|
64
|
+
| `time` (default, 4 h) | once, then every pi/omp process on the machine uses that unlock for N hours |
|
|
64
65
|
|
|
65
66
|
`vault_get` searches Bitwarden on a local miss; a hit is shown to you, then read once, then — following `bitwarden.import` — offered for import into ai-vault (so it never touches Bitwarden again) or used just this once. So **only the passwords you approve for import ever enter ai-vault**.
|
|
66
67
|
|
package/README.zh.md
CHANGED
|
@@ -51,11 +51,12 @@ omp plugin install npm:pi-ai-vault # omp,同一个包
|
|
|
51
51
|
|
|
52
52
|
| 模式 | 行为 |
|
|
53
53
|
|---|---|
|
|
54
|
+
| `every` | 每次调用都问;不留任何能打开库的东西 |
|
|
54
55
|
| `never` | 永远不问,读取也不弹确认窗——「agent 可以自由使用」的那批密码 |
|
|
55
56
|
|
|
56
|
-
默认(`
|
|
57
|
-
| `time
|
|
58
|
-
| `
|
|
57
|
+
默认(`time`)读取同样不弹窗:解锁会话的主密码就是授权,每次读取仍记审计。`/vault confirm on` 让每次读取都弹窗。
|
|
58
|
+
| `time`(默认,4 小时) | 问一次,之后机器上所有 pi/omp 进程共用这次解锁 N 小时 |
|
|
59
|
+
| `every` | 每次调用都问;不留任何能打开库的东西 |
|
|
59
60
|
|
|
60
61
|
`vault_get` 找不到本地条目时会去 Bitwarden 搜;命中后按 `bitwarden.import` 处理:
|
|
61
62
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-ai-vault",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.1",
|
|
4
4
|
"description": "pi extension for ai-vault: one machine-wide encrypted secret store, live Bitwarden and Ente Auth. Works in pi and in omp (the omp fork reads the same manifest).",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"security"
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@xiaolikt/ai-vault": "0.
|
|
28
|
+
"@xiaolikt/ai-vault": "0.14.1"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
31
|
"@earendil-works/pi-coding-agent": "^1.5.0",
|
package/src/bitwarden.ts
CHANGED
|
@@ -57,7 +57,7 @@ import type { Draft } from "./types.js";
|
|
|
57
57
|
import { onSessionLeave, sessionLabel, withTier } from "./host.js";
|
|
58
58
|
import { confirmWindow, hasDisplay, promptSecret } from "./prompt.js";
|
|
59
59
|
import { npmShimLaunch, type CliLaunch } from "./cli.js";
|
|
60
|
-
import { getSettingsSafe, settingsCli, settingsToken } from "./settings.js";
|
|
60
|
+
import { getSettingsSafe, settingsCli, settingsOpts, settingsToken } from "./settings.js";
|
|
61
61
|
import { tr } from "./i18n.js";
|
|
62
62
|
import { scrubDeep, scrubText } from "./scrub.js";
|
|
63
63
|
|
|
@@ -676,7 +676,7 @@ async function ensureUnlocked(st: BwState, ctx: ExtensionContext | undefined, si
|
|
|
676
676
|
// directory), so Bitwarden costs no window at all.
|
|
677
677
|
if (policy.unlock !== "session") {
|
|
678
678
|
try {
|
|
679
|
-
const saved = await settingsCli().json<{ key: string }>(["bw-session", "get"], { signal });
|
|
679
|
+
const saved = await settingsCli().json<{ key: string }>(["bw-session", "get"], { signal, ...settingsOpts() });
|
|
680
680
|
if (saved?.key) {
|
|
681
681
|
const verify = await bwStatus(saved.key, signal);
|
|
682
682
|
if (verify?.status === "unlocked") {
|
|
@@ -720,7 +720,7 @@ async function ensureUnlocked(st: BwState, ctx: ExtensionContext | undefined, si
|
|
|
720
720
|
// Keep the key where the CLI can hand it back; the file's expiry comes
|
|
721
721
|
// from the bitwarden.hours policy.
|
|
722
722
|
const token = settingsToken();
|
|
723
|
-
await settingsCli().run(["bw-session", "put", ...(token ? ["--token", token] : [])], { stdin: key }).catch(() => undefined);
|
|
723
|
+
await settingsCli().run(["bw-session", "put", ...(token ? ["--token", token] : [])], { stdin: key, ...settingsOpts() }).catch(() => undefined);
|
|
724
724
|
}
|
|
725
725
|
await auditEvent("unlock", "ok", st.sessionLabel);
|
|
726
726
|
// Refresh in the background: a full sync can take seconds on large vaults.
|
package/src/cli.ts
CHANGED
|
@@ -326,7 +326,7 @@ export class VaultCli {
|
|
|
326
326
|
return process.env.AI_VAULT_DIR ?? join(homedir(), ".ai-vault");
|
|
327
327
|
}
|
|
328
328
|
|
|
329
|
-
async run(args: string[], opts: { stdin?: string; signal?: AbortSignal } = {}): Promise<CliRun> {
|
|
329
|
+
async run(args: string[], opts: { stdin?: string; signal?: AbortSignal; password?: string } = {}): Promise<CliRun> {
|
|
330
330
|
const launch = this.launch;
|
|
331
331
|
if (!launch) {
|
|
332
332
|
throw new CliError(EXIT.error, undefined, "ai-vault CLI not found", "");
|
|
@@ -337,7 +337,10 @@ export class VaultCli {
|
|
|
337
337
|
const run = await new Promise<CliRun>((resolve, reject) => {
|
|
338
338
|
// A program Node will not start fails here like any other CLI failure,
|
|
339
339
|
// not as a bare exception from inside the promise.
|
|
340
|
-
|
|
340
|
+
// `every` mode: the master password travels in the child's environment,
|
|
341
|
+
// never on the command line, and lives for exactly this call.
|
|
342
|
+
const passwordEnv = opts.password !== undefined ? { AI_VAULT_PASSWORD: opts.password } : {};
|
|
343
|
+
const child = startCli(launch, args, { ...process.env, ...this.host.env(), ...passwordEnv }, () => {
|
|
341
344
|
opts.signal?.removeEventListener("abort", onAbort);
|
|
342
345
|
reject(new CliError(EXIT.error, undefined, `cannot run ${launch.program}: ${lastStartError}`, ""));
|
|
343
346
|
});
|
|
@@ -376,14 +379,14 @@ export class VaultCli {
|
|
|
376
379
|
* `init` or `unlock`: returns the parsed result with the token, and keeps
|
|
377
380
|
* the host secret the CLI minted (or used ours) for later signing.
|
|
378
381
|
*/
|
|
379
|
-
async open<T extends { hostSecret?: string }>(subcommand: "init" | "unlock", args: string[], opts: { stdin?: string; signal?: AbortSignal } = {}): Promise<T> {
|
|
382
|
+
async open<T extends { hostSecret?: string }>(subcommand: "init" | "unlock", args: string[], opts: { stdin?: string; signal?: AbortSignal; password?: string } = {}): Promise<T> {
|
|
380
383
|
const result = await this.json<T>([subcommand, ...args], opts);
|
|
381
384
|
this.host.adopt(result.hostSecret);
|
|
382
385
|
return result;
|
|
383
386
|
}
|
|
384
387
|
|
|
385
388
|
/** One CLI call whose output is a JSON object. */
|
|
386
|
-
async json<T>(args: string[], opts: { stdin?: string; signal?: AbortSignal } = {}): Promise<T> {
|
|
389
|
+
async json<T>(args: string[], opts: { stdin?: string; signal?: AbortSignal; password?: string } = {}): Promise<T> {
|
|
387
390
|
const run = await this.run([...args, "--json"], opts);
|
|
388
391
|
try {
|
|
389
392
|
return JSON.parse(run.stdout) as T;
|
|
@@ -396,8 +399,11 @@ export class VaultCli {
|
|
|
396
399
|
async withPassword(
|
|
397
400
|
args: string[],
|
|
398
401
|
ask: () => Promise<string | undefined>,
|
|
399
|
-
opts: { stdin?: string; signal?: AbortSignal } = {},
|
|
402
|
+
opts: { stdin?: string; signal?: AbortSignal; password?: string } = {},
|
|
400
403
|
): Promise<CliRun> {
|
|
404
|
+
// In `every` mode the call already carries the master password: the CLI
|
|
405
|
+
// reuses it for the verification instead of asking for it again.
|
|
406
|
+
if (opts.password !== undefined) return this.run(args, opts);
|
|
401
407
|
try {
|
|
402
408
|
return await this.run(args, opts);
|
|
403
409
|
} catch (error) {
|
package/src/ente.ts
CHANGED
|
@@ -26,7 +26,7 @@ import { Type } from "typebox";
|
|
|
26
26
|
import { auditEvent } from "./bitwarden.js";
|
|
27
27
|
import { confirmWindow, forDialog } from "./prompt.js";
|
|
28
28
|
import { withTier } from "./host.js";
|
|
29
|
-
import { getSettingsSafe, settingsCli } from "./settings.js";
|
|
29
|
+
import { getSettingsSafe, settingsCli, settingsOpts } from "./settings.js";
|
|
30
30
|
|
|
31
31
|
export interface EnteCodeMeta {
|
|
32
32
|
id: string;
|
|
@@ -94,7 +94,7 @@ async function enteEnv(signal?: AbortSignal): Promise<Record<string, string | un
|
|
|
94
94
|
const settings = await getSettingsSafe(signal);
|
|
95
95
|
if (settings.ente.unlock === "session") return env;
|
|
96
96
|
try {
|
|
97
|
-
const sealed = await settingsCli().json<{ key: string }>(["ente-session", "get"], { signal });
|
|
97
|
+
const sealed = await settingsCli().json<{ key: string }>(["ente-session", "get"], { signal, ...settingsOpts() });
|
|
98
98
|
if (sealed?.key) env.ENTE_CLI_VAULT_KEY = sealed.key;
|
|
99
99
|
} catch {
|
|
100
100
|
// No key saved yet, or it expired: ente will fall back to the keychain,
|
package/src/index.ts
CHANGED
|
@@ -29,17 +29,19 @@ import type { Draft, ItemType } from "./types.js";
|
|
|
29
29
|
import { registerEnte } from "./ente.js";
|
|
30
30
|
import { isOmpHost, onSessionLeave, sessionLabel, subagentId, withTier } from "./host.js";
|
|
31
31
|
import { confirmWindow, forDialog, promptSecret, promptText, type PromptHost } from "./prompt.js";
|
|
32
|
-
import { forgetSettings, getSettingsSafe, setSettingsToken, settingsCli, settingsToken, type Settings } from "./settings.js";
|
|
32
|
+
import { forgetSettings, getSettingsSafe, setEveryPassword, setSettingsToken, settingsCli, settingsOpts, settingsToken, type Settings } from "./settings.js";
|
|
33
33
|
|
|
34
34
|
type Ctx = ExtensionToolContext | ExtensionCommandContext;
|
|
35
35
|
|
|
36
36
|
const GRANT_MS_DEFAULT = 5 * 60_000;
|
|
37
37
|
|
|
38
|
-
type UnlockModeName = "never" | "time" | "
|
|
38
|
+
type UnlockModeName = "never" | "time" | "every";
|
|
39
39
|
|
|
40
40
|
interface StatusResult {
|
|
41
41
|
ok: boolean;
|
|
42
42
|
unlocked: boolean;
|
|
43
|
+
/** The session the call ran under; a binding that came without a token adopts it. */
|
|
44
|
+
token?: string | null;
|
|
43
45
|
items: number;
|
|
44
46
|
requester: string;
|
|
45
47
|
idleMs: number;
|
|
@@ -97,6 +99,9 @@ interface Scope {
|
|
|
97
99
|
mode?: UnlockModeName;
|
|
98
100
|
/** True when reads use the machine-wide session instead of a token. */
|
|
99
101
|
auto?: boolean;
|
|
102
|
+
/** `vault.unlock = every`: the master password for the call being made, asked
|
|
103
|
+
* for before each one and dropped after it. Never stored anywhere else. */
|
|
104
|
+
everyPassword?: string;
|
|
100
105
|
unlocking?: Promise<boolean>;
|
|
101
106
|
}
|
|
102
107
|
|
|
@@ -149,6 +154,7 @@ async function lockNow(scope: Scope): Promise<void> {
|
|
|
149
154
|
scope.epoch++;
|
|
150
155
|
scope.items = 0;
|
|
151
156
|
setSettingsToken(undefined);
|
|
157
|
+
setEveryPassword(undefined);
|
|
152
158
|
forgetSettings();
|
|
153
159
|
if (!token || !cli.available) return;
|
|
154
160
|
try {
|
|
@@ -160,6 +166,9 @@ async function lockNow(scope: Scope): Promise<void> {
|
|
|
160
166
|
|
|
161
167
|
/** Args for a CLI call: this binding's token, or the machine-wide session. */
|
|
162
168
|
const tokenArgs = (scope: Scope): string[] => (scope.token ? ["--token", scope.token] : []);
|
|
169
|
+
/** The CLI options a vault call needs beyond its arguments: the `every` password. */
|
|
170
|
+
const callOpts = (scope: Scope, signal?: AbortSignal): { signal?: AbortSignal; password?: string } =>
|
|
171
|
+
scope.everyPassword !== undefined ? { signal, password: scope.everyPassword } : { signal };
|
|
163
172
|
|
|
164
173
|
/** Make this binding's unlock happen; never throws. */
|
|
165
174
|
async function ensureUnlocked(scope: Scope, ctx: Ctx, signal?: AbortSignal): Promise<boolean> {
|
|
@@ -190,25 +199,63 @@ async function ensureUnlocked(scope: Scope, ctx: Ctx, signal?: AbortSignal): Pro
|
|
|
190
199
|
}
|
|
191
200
|
}
|
|
192
201
|
}
|
|
193
|
-
//
|
|
194
|
-
//
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
note(ctx,
|
|
205
|
-
return
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
202
|
+
// No token (first call, or `never`/`every`). Ask the CLI what that means:
|
|
203
|
+
// `never` answers outright; `every` says it needs a password — for this
|
|
204
|
+
// call only, since that mode keeps nothing between calls.
|
|
205
|
+
{
|
|
206
|
+
const askEveryPassword = async (): Promise<string | undefined> => {
|
|
207
|
+
const password = await promptSecret(
|
|
208
|
+
dialogs(ctx),
|
|
209
|
+
tr("ai-vault · unlock", "ai-vault · 解锁"),
|
|
210
|
+
tr("master password (asked on every call)", "主密码(每次调用都会问)"),
|
|
211
|
+
signal,
|
|
212
|
+
);
|
|
213
|
+
if (!password && !signal?.aborted) note(ctx, tr("no master password entered", "没有输入主密码"), "warning");
|
|
214
|
+
return password;
|
|
215
|
+
};
|
|
216
|
+
try {
|
|
217
|
+
const status = await cli.json<StatusResult>(["status"], { signal });
|
|
218
|
+
scope.items = status.items;
|
|
219
|
+
scope.mode = status.mode;
|
|
220
|
+
scope.auto = status.auto;
|
|
221
|
+
// In `time` mode the CLI found an existing unlock and minted a
|
|
222
|
+
// session of our own under it: keep that token, so relayed
|
|
223
|
+
// approvals are signed for the session that will check them.
|
|
224
|
+
scope.token = status.token ?? undefined;
|
|
225
|
+
setSettingsToken(scope.token);
|
|
226
|
+
return true;
|
|
227
|
+
} catch (error) {
|
|
228
|
+
if (error instanceof CliError && /not initialized/i.test(error.message)) {
|
|
229
|
+
note(ctx, NOT_INITIALIZED, "warning");
|
|
230
|
+
return false;
|
|
231
|
+
}
|
|
232
|
+
if (error instanceof CliError && error.needsUi === "password") {
|
|
233
|
+
// `every`: the password opens the store for this call alone.
|
|
234
|
+
const password = await askEveryPassword();
|
|
235
|
+
if (!password) return false;
|
|
236
|
+
try {
|
|
237
|
+
const status = await cli.json<StatusResult>(["status"], { signal, password });
|
|
238
|
+
scope.items = status.items;
|
|
239
|
+
scope.mode = "every";
|
|
240
|
+
scope.everyPassword = password;
|
|
241
|
+
setEveryPassword(password);
|
|
242
|
+
return true;
|
|
243
|
+
} catch (retryError) {
|
|
244
|
+
if (retryError instanceof CliError && retryError.auth) {
|
|
245
|
+
note(ctx, tr("ai-vault: wrong master password", "ai-vault: 主密码错误"), "error");
|
|
246
|
+
return false;
|
|
247
|
+
}
|
|
248
|
+
note(ctx, `ai-vault: ${describe(retryError)}`, "error");
|
|
249
|
+
return false;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
if (!(error instanceof CliError) || !error.auth) {
|
|
253
|
+
note(ctx, `ai-vault: ${describe(error)}`, "error");
|
|
254
|
+
return false;
|
|
255
|
+
}
|
|
210
256
|
}
|
|
211
257
|
}
|
|
258
|
+
let lastUnlockPassword: string | undefined;
|
|
212
259
|
const attempt = async (password?: string): Promise<UnlockResult> => {
|
|
213
260
|
const args = ["--requester", sessionLabel(ctx)];
|
|
214
261
|
if (password !== undefined) args.push("--password-stdin");
|
|
@@ -228,19 +275,27 @@ async function ensureUnlocked(scope: Scope, ctx: Ctx, signal?: AbortSignal): Pro
|
|
|
228
275
|
if (!signal?.aborted) note(ctx, tr("no master password entered (the window was cancelled, or there is no desktop/UI)", "没有输入主密码(窗口已取消,或没有桌面/界面)"), "warning");
|
|
229
276
|
return false;
|
|
230
277
|
}
|
|
278
|
+
lastUnlockPassword = password;
|
|
231
279
|
result = await attempt(password);
|
|
232
280
|
}
|
|
233
281
|
// /vault lock or a session switch ran while the window was open.
|
|
234
282
|
if (epoch !== scope.epoch || signal?.aborted) {
|
|
235
|
-
await cli.run(["lock", "--token", result.token]).catch(() => undefined);
|
|
283
|
+
if (result.token) await cli.run(["lock", "--token", result.token]).catch(() => undefined);
|
|
236
284
|
return false;
|
|
237
285
|
}
|
|
238
286
|
scope.mode = result.mode;
|
|
239
287
|
scope.auto = result.mode === "never";
|
|
240
288
|
// In `never` mode the CLI wrote the machine-wide session; keeping a
|
|
241
289
|
// per-binding token would bring back a confirm window on every read.
|
|
242
|
-
|
|
290
|
+
// In `every` mode no token exists: the store was just proved open by
|
|
291
|
+
// the password that was typed, so this call proceeds on it, and the
|
|
292
|
+
// next call asks again.
|
|
293
|
+
scope.token = result.mode === "never" || result.mode === "every" ? undefined : result.token;
|
|
243
294
|
scope.items = result.items;
|
|
295
|
+
if (result.mode === "every") {
|
|
296
|
+
scope.everyPassword = lastUnlockPassword;
|
|
297
|
+
setEveryPassword(lastUnlockPassword);
|
|
298
|
+
}
|
|
244
299
|
setSettingsToken(scope.token);
|
|
245
300
|
return true;
|
|
246
301
|
} catch (error) {
|
|
@@ -277,7 +332,7 @@ function vaultListTool(scope: Scope): ToolDefinition<typeof vaultListParams, { o
|
|
|
277
332
|
async execute(_id, _params, signal, _onUpdate, ctx) {
|
|
278
333
|
if (!(await ensureUnlocked(scope, ctx, signal))) return fail(LOCKED);
|
|
279
334
|
try {
|
|
280
|
-
const result = await cli.json<ListResult>(["list",
|
|
335
|
+
const result = await cli.json<ListResult>(["list", ...tokenArgs(scope)], callOpts(scope, signal));
|
|
281
336
|
scope.items = result.count;
|
|
282
337
|
setStatus(ctx, scope);
|
|
283
338
|
return {
|
|
@@ -286,6 +341,9 @@ function vaultListTool(scope: Scope): ToolDefinition<typeof vaultListParams, { o
|
|
|
286
341
|
};
|
|
287
342
|
} catch (error) {
|
|
288
343
|
return failure(scope, error);
|
|
344
|
+
} finally {
|
|
345
|
+
scope.everyPassword = undefined;
|
|
346
|
+
setEveryPassword(undefined);
|
|
289
347
|
}
|
|
290
348
|
},
|
|
291
349
|
};
|
|
@@ -337,7 +395,7 @@ async function bitwardenFallback(
|
|
|
337
395
|
try {
|
|
338
396
|
const saved = await cli.json<SaveResult>(["save", "--yes", ...tokenArgs(scope)], {
|
|
339
397
|
stdin: JSON.stringify([draft]),
|
|
340
|
-
signal,
|
|
398
|
+
...callOpts(scope, signal),
|
|
341
399
|
});
|
|
342
400
|
scope.items += saved.count;
|
|
343
401
|
setStatus(ctx, scope);
|
|
@@ -398,8 +456,9 @@ function vaultGetTool(scope: Scope, bw: BwState): ToolDefinition<typeof vaultGet
|
|
|
398
456
|
if (!(await ensureUnlocked(scope, ctx, signal))) return fail(LOCKED);
|
|
399
457
|
const base = ["get", params.name, ...(params.reason ? ["--reason", params.reason] : []), ...tokenArgs(scope)];
|
|
400
458
|
try {
|
|
459
|
+
// (the `every` password is dropped when this call ends, in the finally below)
|
|
401
460
|
try {
|
|
402
|
-
const run = await cli.run([...base, "--json"],
|
|
461
|
+
const run = await cli.run([...base, "--json"], callOpts(scope, signal));
|
|
403
462
|
setStatus(ctx, scope, "·ok");
|
|
404
463
|
return {
|
|
405
464
|
content: [{ type: "text" as const, text: run.stdout.trim() }],
|
|
@@ -424,7 +483,7 @@ function vaultGetTool(scope: Scope, bw: BwState): ToolDefinition<typeof vaultGet
|
|
|
424
483
|
}
|
|
425
484
|
const run = await cli.run([...base, "--json", "--answer-stdin", "--approval", approval], {
|
|
426
485
|
stdin: allowed ? "y\n" : "n\n",
|
|
427
|
-
signal,
|
|
486
|
+
...callOpts(scope, signal),
|
|
428
487
|
});
|
|
429
488
|
setStatus(ctx, scope, "·granted");
|
|
430
489
|
return {
|
|
@@ -440,6 +499,9 @@ function vaultGetTool(scope: Scope, bw: BwState): ToolDefinition<typeof vaultGet
|
|
|
440
499
|
} catch (error) {
|
|
441
500
|
if (error instanceof CliError && error.denied) setStatus(ctx, scope, "·denied");
|
|
442
501
|
return failure(scope, error);
|
|
502
|
+
} finally {
|
|
503
|
+
scope.everyPassword = undefined;
|
|
504
|
+
setEveryPassword(undefined);
|
|
443
505
|
}
|
|
444
506
|
},
|
|
445
507
|
};
|
|
@@ -566,7 +628,7 @@ async function saveLocalEntry(scope: Scope, ctx: Ctx, req: SaveRequest): Promise
|
|
|
566
628
|
if (!name) return "cancelled: no entry name";
|
|
567
629
|
|
|
568
630
|
const kind = req.type ?? "login";
|
|
569
|
-
const args = ["add", "--name", name, "--type", kind,
|
|
631
|
+
const args = ["add", "--name", name, "--type", kind, ...tokenArgs(scope)];
|
|
570
632
|
if (req.username !== undefined) args.push("--username", req.username);
|
|
571
633
|
if (req.uri?.trim()) args.push("--uri", req.uri.trim());
|
|
572
634
|
if (req.notes?.trim()) args.push("--notes", req.notes.trim());
|
|
@@ -587,7 +649,7 @@ async function saveLocalEntry(scope: Scope, ctx: Ctx, req: SaveRequest): Promise
|
|
|
587
649
|
for (let attempt = 0; attempt < 8; attempt++) {
|
|
588
650
|
flush();
|
|
589
651
|
try {
|
|
590
|
-
const run = await cli.run([...args, "--json"], {
|
|
652
|
+
const run = await cli.run([...args, "--json"], { ...callOpts(scope, req.signal) });
|
|
591
653
|
const saved = JSON.parse(run.stdout) as { item?: string; created?: boolean; type?: string };
|
|
592
654
|
if (saved.created) scope.items += 1;
|
|
593
655
|
setStatus(ctx, scope);
|
|
@@ -674,9 +736,9 @@ async function importBitwarden(
|
|
|
674
736
|
return { text: "nothing matched; nothing imported", ok: false, count: 0 };
|
|
675
737
|
}
|
|
676
738
|
try {
|
|
677
|
-
const saved = await cli.json<SaveResult>(["save", "--yes",
|
|
739
|
+
const saved = await cli.json<SaveResult>(["save", "--yes", ...tokenArgs(scope)], {
|
|
678
740
|
stdin: JSON.stringify(drafts),
|
|
679
|
-
signal,
|
|
741
|
+
...callOpts(scope, signal),
|
|
680
742
|
});
|
|
681
743
|
scope.items += saved.count;
|
|
682
744
|
setStatus(ctx, scope);
|
|
@@ -807,7 +869,7 @@ function commandHandler(scope: Scope, bw: BwState) {
|
|
|
807
869
|
}
|
|
808
870
|
case "status": {
|
|
809
871
|
try {
|
|
810
|
-
const status = await cli.json<StatusResult>(["status", ...tokenArgs(scope)]);
|
|
872
|
+
const status = await cli.json<StatusResult>(["status", ...tokenArgs(scope)], callOpts(scope));
|
|
811
873
|
scope.items = status.items;
|
|
812
874
|
scope.mode = status.mode;
|
|
813
875
|
scope.auto = status.auto;
|
|
@@ -852,14 +914,15 @@ function commandHandler(scope: Scope, bw: BwState) {
|
|
|
852
914
|
case "ente-key": {
|
|
853
915
|
// Ente's unattended key: generated by `ente vault key generate`.
|
|
854
916
|
// Saved sealed, so `ente.unlock = never | time` can use it.
|
|
917
|
+
if (!(await ensureUnlocked(scope, ctx))) return;
|
|
855
918
|
const key = await promptSecret(dialogs(ctx), tr("ai-vault · Ente key", "ai-vault · Ente 密钥"), tr("the key printed by ente vault key generate", "ente vault key generate 输出的密钥"));
|
|
856
919
|
if (!key) {
|
|
857
920
|
note(ctx, tr("cancelled", "已取消"), "warning");
|
|
858
921
|
return;
|
|
859
922
|
}
|
|
860
923
|
try {
|
|
861
|
-
const args = ["ente-session", "put"];
|
|
862
|
-
await cli.run(args, { stdin: key });
|
|
924
|
+
const args = ["ente-session", "put", ...tokenArgs(scope)];
|
|
925
|
+
await cli.run(args, { stdin: key, ...callOpts(scope) });
|
|
863
926
|
forgetSettings();
|
|
864
927
|
note(ctx, tr("saved (sealed). You can now set /vault settings ente.unlock never", "已保存(封存)。现在可以设置 /vault settings ente.unlock never"));
|
|
865
928
|
} catch (error) {
|
|
@@ -871,9 +934,10 @@ function commandHandler(scope: Scope, bw: BwState) {
|
|
|
871
934
|
// /vault confirm show
|
|
872
935
|
// /vault confirm off|on turn the per-read window off (asks the master password) / on
|
|
873
936
|
const [arg] = rest;
|
|
937
|
+
if (!(await ensureUnlocked(scope, ctx))) return;
|
|
874
938
|
if (!arg) {
|
|
875
939
|
try {
|
|
876
|
-
const current = await cli.json<Settings>(["settings", ...tokenArgs(scope)]);
|
|
940
|
+
const current = await cli.json<Settings>(["settings", ...tokenArgs(scope)], callOpts(scope));
|
|
877
941
|
note(ctx, `vault.confirm: ${current.vault.confirm}${current.vault.confirm ? tr(" (a window per read while unlocked)", " (解锁后每次读取弹确认窗)") : tr(" (the unlock is the approval; reads are audited)", " (解锁即授权;读取仍记审计)")}`);
|
|
878
942
|
} catch (error) {
|
|
879
943
|
note(ctx, `ai-vault: ${describe(error)}`, "error");
|
|
@@ -889,7 +953,8 @@ function commandHandler(scope: Scope, bw: BwState) {
|
|
|
889
953
|
try {
|
|
890
954
|
const run = await cli.withPassword(["settings", "vault.confirm", turnOff ? "false" : "true", ...tokenArgs(scope)], () =>
|
|
891
955
|
promptSecret(dialogs(ctx), tr("ai-vault · master password", "ai-vault · 主密码"), tr("turn off the per-read confirmation", "关闭每次读取的确认窗")),
|
|
892
|
-
);
|
|
956
|
+
callOpts(scope));
|
|
957
|
+
|
|
893
958
|
forgetSettings();
|
|
894
959
|
const updated = JSON.parse(run.stdout) as Settings;
|
|
895
960
|
note(
|
|
@@ -909,13 +974,16 @@ function commandHandler(scope: Scope, bw: BwState) {
|
|
|
909
974
|
case "settings": {
|
|
910
975
|
const [key, ...valueParts] = rest;
|
|
911
976
|
const value = valueParts.join(" ").trim();
|
|
977
|
+
// Reading the settings means opening the store: in `every` mode
|
|
978
|
+
// that is a password for this call, asked here.
|
|
979
|
+
if (!(await ensureUnlocked(scope, ctx))) return;
|
|
912
980
|
try {
|
|
913
981
|
if (!key) {
|
|
914
|
-
note(ctx, settingsText(await cli.json<Settings>(["settings", ...tokenArgs(scope)])));
|
|
982
|
+
note(ctx, settingsText(await cli.json<Settings>(["settings", ...tokenArgs(scope)], callOpts(scope))));
|
|
915
983
|
return;
|
|
916
984
|
}
|
|
917
985
|
if (!value) {
|
|
918
|
-
const one = await cli.json<{ key: string; value: string }>(["settings", key, ...tokenArgs(scope)]);
|
|
986
|
+
const one = await cli.json<{ key: string; value: string }>(["settings", key, ...tokenArgs(scope)], callOpts(scope));
|
|
919
987
|
note(ctx, `${one.key}: ${one.value}`);
|
|
920
988
|
return;
|
|
921
989
|
}
|
|
@@ -923,7 +991,8 @@ function commandHandler(scope: Scope, bw: BwState) {
|
|
|
923
991
|
// needs-ui); the other keys do not prompt at all.
|
|
924
992
|
const run = await cli.withPassword(["settings", key, value, ...tokenArgs(scope)], () =>
|
|
925
993
|
promptSecret(dialogs(ctx), tr("ai-vault · master password", "ai-vault · 主密码"), tr("changing the unlock policy needs verification", "改变解锁模式需要验证")),
|
|
926
|
-
);
|
|
994
|
+
callOpts(scope));
|
|
995
|
+
|
|
927
996
|
forgetSettings();
|
|
928
997
|
const updated = JSON.parse(run.stdout) as Settings;
|
|
929
998
|
if (key.startsWith("vault.")) await lockNow(scope);
|
|
@@ -935,6 +1004,7 @@ function commandHandler(scope: Scope, bw: BwState) {
|
|
|
935
1004
|
}
|
|
936
1005
|
case "mode": {
|
|
937
1006
|
const [sub, hoursArg] = rest;
|
|
1007
|
+
if (!(await ensureUnlocked(scope, ctx))) return;
|
|
938
1008
|
if (!sub || sub === "get" || sub === "status") {
|
|
939
1009
|
try {
|
|
940
1010
|
const current = await cli.json<{ mode: string; hours: number; auto: boolean }>(["mode"]);
|
|
@@ -953,8 +1023,8 @@ function commandHandler(scope: Scope, bw: BwState) {
|
|
|
953
1023
|
}
|
|
954
1024
|
return;
|
|
955
1025
|
}
|
|
956
|
-
if (!["
|
|
957
|
-
note(ctx, tr("usage: /vault mode [
|
|
1026
|
+
if (!["every", "time", "never", "no-password", "every-time"].includes(sub)) {
|
|
1027
|
+
note(ctx, tr("usage: /vault mode [every|time|never] [hours]", "用法: /vault mode [every|time|never] [小时]"), "warning");
|
|
958
1028
|
return;
|
|
959
1029
|
}
|
|
960
1030
|
const args = ["mode", "set", sub];
|
|
@@ -970,9 +1040,9 @@ function commandHandler(scope: Scope, bw: BwState) {
|
|
|
970
1040
|
const how =
|
|
971
1041
|
result.mode === "never"
|
|
972
1042
|
? tr("no password from now on (the key is on disk: anything that can read your files can read every password in the store)", "以后免密(密钥已写盘,任何能读你文件的程序都能读到库里的密码)")
|
|
973
|
-
: result.mode === "
|
|
974
|
-
? tr(
|
|
975
|
-
: tr(
|
|
1043
|
+
: result.mode === "every"
|
|
1044
|
+
? tr("the password is asked on every call from now on; nothing that can open the store is kept", "以后每次调用都要密码;不留任何能打开库的东西")
|
|
1045
|
+
: tr(`the password is asked again every ${result.hours} hours from now on`, `以后每 ${result.hours} 小时要一次密码`);
|
|
976
1046
|
note(ctx, `mode → ${result.mode},${how}`);
|
|
977
1047
|
} catch (error) {
|
|
978
1048
|
note(ctx, `ai-vault: ${error instanceof CliError && error.denied ? tr("cancelled", "已取消") : describe(error)}`, "warning");
|
|
@@ -982,7 +1052,7 @@ function commandHandler(scope: Scope, bw: BwState) {
|
|
|
982
1052
|
case "list": {
|
|
983
1053
|
if (!(await ensureUnlocked(scope, ctx))) return;
|
|
984
1054
|
try {
|
|
985
|
-
const result = await cli.json<ListResult>(["list",
|
|
1055
|
+
const result = await cli.json<ListResult>(["list", ...tokenArgs(scope)], callOpts(scope));
|
|
986
1056
|
scope.items = result.count;
|
|
987
1057
|
const rows = result.items.map((i) => `${i.name}\t${i.username ?? ""}\t${i.uris[0] ?? ""}`);
|
|
988
1058
|
note(ctx, rows.join("\n") || "(empty)");
|
|
@@ -1026,9 +1096,9 @@ function commandHandler(scope: Scope, bw: BwState) {
|
|
|
1026
1096
|
note(ctx, tr("usage: /vault remove <name>", "用法: /vault remove <名称>"), "warning");
|
|
1027
1097
|
return;
|
|
1028
1098
|
}
|
|
1029
|
-
const args = ["remove", name,
|
|
1099
|
+
const args = ["remove", name, ...tokenArgs(scope)];
|
|
1030
1100
|
try {
|
|
1031
|
-
const result = await cli.json<RemovingResult>(args);
|
|
1101
|
+
const result = await cli.json<RemovingResult>(args, callOpts(scope));
|
|
1032
1102
|
if (result.removed) scope.items = Math.max(0, scope.items - 1);
|
|
1033
1103
|
setStatus(ctx, scope);
|
|
1034
1104
|
note(ctx, result.removed ? "removed" : "not found", result.removed ? "info" : "warning");
|
|
@@ -1040,7 +1110,7 @@ function commandHandler(scope: Scope, bw: BwState) {
|
|
|
1040
1110
|
const sure = await confirmWindow(dialogs(ctx), tr("ai-vault · delete entry", "ai-vault · 删除条目"), tr(`Delete "${forDialog(name)}"? This cannot be undone.`, `删除「${forDialog(name)}」?删除后无法恢复。`));
|
|
1041
1111
|
if (!sure) return;
|
|
1042
1112
|
try {
|
|
1043
|
-
const result = await cli.json<RemovingResult>([...args, "--yes"]);
|
|
1113
|
+
const result = await cli.json<RemovingResult>([...args, "--yes"], callOpts(scope));
|
|
1044
1114
|
if (result.removed) scope.items = Math.max(0, scope.items - 1);
|
|
1045
1115
|
setStatus(ctx, scope);
|
|
1046
1116
|
note(ctx, result.removed ? "removed" : "not found", result.removed ? "info" : "warning");
|
|
@@ -1060,7 +1130,7 @@ function commandHandler(scope: Scope, bw: BwState) {
|
|
|
1060
1130
|
note(ctx, tr("unlock first; usage: /vault grant <name> [minutes] (quote names with spaces)", "先解锁;用法: /vault grant <名称> [分钟](带空格的名称加引号)"), "warning");
|
|
1061
1131
|
return;
|
|
1062
1132
|
}
|
|
1063
|
-
const args = ["grant", name,
|
|
1133
|
+
const args = ["grant", name, ...tokenArgs(scope)];
|
|
1064
1134
|
if (minutes) args.push("--minutes", minutes);
|
|
1065
1135
|
try {
|
|
1066
1136
|
// A standing grant costs the master password, like a mode change.
|
|
@@ -1078,7 +1148,7 @@ function commandHandler(scope: Scope, bw: BwState) {
|
|
|
1078
1148
|
case "revoke": {
|
|
1079
1149
|
if (!(await ensureUnlocked(scope, ctx))) return;
|
|
1080
1150
|
const target = rest[0] === "all" ? "all" : rest.join(" ").trim();
|
|
1081
|
-
const args = ["revoke",
|
|
1151
|
+
const args = ["revoke", ...tokenArgs(scope)];
|
|
1082
1152
|
if (target === "all") args.push("--all");
|
|
1083
1153
|
else if (target) args.push(target);
|
|
1084
1154
|
else {
|
|
@@ -1115,9 +1185,9 @@ function commandHandler(scope: Scope, bw: BwState) {
|
|
|
1115
1185
|
return;
|
|
1116
1186
|
}
|
|
1117
1187
|
if (!(await ensureUnlocked(scope, ctx))) return;
|
|
1118
|
-
const args = ["import", path,
|
|
1188
|
+
const args = ["import", path, ...tokenArgs(scope)];
|
|
1119
1189
|
try {
|
|
1120
|
-
const result = await cli.json<SaveResult>(args);
|
|
1190
|
+
const result = await cli.json<SaveResult>(args, callOpts(scope));
|
|
1121
1191
|
scope.items += result.count;
|
|
1122
1192
|
setStatus(ctx, scope);
|
|
1123
1193
|
note(ctx, `imported ${result.count} login items`);
|
|
@@ -1133,7 +1203,7 @@ function commandHandler(scope: Scope, bw: BwState) {
|
|
|
1133
1203
|
);
|
|
1134
1204
|
if (!sure) return;
|
|
1135
1205
|
try {
|
|
1136
|
-
const result = await cli.json<SaveResult>([...args, "--yes"]);
|
|
1206
|
+
const result = await cli.json<SaveResult>([...args, "--yes"], callOpts(scope));
|
|
1137
1207
|
scope.items += result.count;
|
|
1138
1208
|
setStatus(ctx, scope);
|
|
1139
1209
|
note(ctx, `imported ${result.count} login items`);
|
package/src/settings.ts
CHANGED
|
@@ -24,7 +24,7 @@ export interface Settings {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
export const DEFAULT_SETTINGS: Settings = {
|
|
27
|
-
vault: { unlock: "
|
|
27
|
+
vault: { unlock: "time", hours: 4, confirm: false, auto: false },
|
|
28
28
|
bitwarden: { unlock: "session", hours: 8, import: "ask", persisted: false, expiresAt: null },
|
|
29
29
|
ente: { unlock: "session", hours: 8, persisted: false, expiresAt: null },
|
|
30
30
|
};
|
|
@@ -50,6 +50,8 @@ let cache: { at: number; value: Settings } | undefined;
|
|
|
50
50
|
* the binding that unlocked sets this before asking.
|
|
51
51
|
*/
|
|
52
52
|
let sessionToken: string | undefined;
|
|
53
|
+
/** In `every` mode there is no token: the settings are read with the call's password. */
|
|
54
|
+
let everyPassword: string | undefined;
|
|
53
55
|
|
|
54
56
|
export function setSettingsToken(token: string | undefined): void {
|
|
55
57
|
if (token !== sessionToken) {
|
|
@@ -58,17 +60,27 @@ export function setSettingsToken(token: string | undefined): void {
|
|
|
58
60
|
}
|
|
59
61
|
}
|
|
60
62
|
|
|
63
|
+
/** The password an `every` call was opened with, for reading the settings under it. */
|
|
64
|
+
export function setEveryPassword(password: string | undefined): void {
|
|
65
|
+
everyPassword = password;
|
|
66
|
+
}
|
|
67
|
+
|
|
61
68
|
export function settingsToken(): string | undefined {
|
|
62
69
|
return sessionToken;
|
|
63
70
|
}
|
|
64
71
|
|
|
72
|
+
/** CLI options for a settings read under whatever this binding holds. */
|
|
73
|
+
export function settingsOpts(): { password?: string } {
|
|
74
|
+
return everyPassword !== undefined ? { password: everyPassword } : {};
|
|
75
|
+
}
|
|
76
|
+
|
|
65
77
|
/**
|
|
66
78
|
* Settings change rarely, so a short cache keeps a Bitwarden call from paying
|
|
67
79
|
* for the read. `/vault settings ...` drops it explicitly.
|
|
68
80
|
*/
|
|
69
81
|
export async function getSettings(signal?: AbortSignal, options: { fresh?: boolean } = {}): Promise<Settings> {
|
|
70
82
|
if (!options.fresh && cache && Date.now() - cache.at < 30_000) return cache.value;
|
|
71
|
-
const value = await settingsCli().json<Settings>(["settings", ...(sessionToken ? ["--token", sessionToken] : [])], { signal });
|
|
83
|
+
const value = await settingsCli().json<Settings>(["settings", ...(sessionToken ? ["--token", sessionToken] : [])], { signal, ...settingsOpts() });
|
|
72
84
|
cache = { at: Date.now(), value };
|
|
73
85
|
return value;
|
|
74
86
|
}
|