@xiaolikt/ai-vault 0.4.1 → 0.6.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.
- package/Cargo.lock +1 -1
- package/README.md +94 -12
- package/crates/ai-vault/Cargo.toml +1 -1
- package/crates/ai-vault/src/bwsession.rs +82 -0
- package/crates/ai-vault/src/cli.rs +364 -28
- package/crates/ai-vault/src/config.rs +171 -0
- package/crates/ai-vault/src/main.rs +2 -0
- package/crates/ai-vault/src/paths.rs +15 -0
- package/crates/ai-vault/src/session.rs +54 -4
- package/package.json +5 -5
- package/packages/pi-ai-vault/README.md +70 -0
- package/packages/pi-ai-vault/package.json +47 -0
- package/{src → packages/pi-ai-vault/src}/bitwarden.ts +62 -0
- package/{src → packages/pi-ai-vault/src}/cli.ts +21 -5
- package/{src → packages/pi-ai-vault/src}/index.ts +299 -51
- package/packages/pi-ai-vault/src/settings.ts +59 -0
- package/test/cli.sh +36 -0
- package/test/smoke.sh +2 -2
- package/tests/bitwarden-tools.test.mjs +1 -1
- package/tests/helpers.mjs +2 -0
- package/tests/import-map.test.mjs +1 -1
- package/tests/mocks/bin/zenity +9 -1
- package/tests/prompt.test.mjs +1 -1
- package/tests/register.test.mjs +1 -1
- package/tests/scrub.test.mjs +1 -1
- package/tests/vault-cli.test.mjs +152 -1
- /package/{src → packages/pi-ai-vault/src}/host.ts +0 -0
- /package/{src → packages/pi-ai-vault/src}/import-map.ts +0 -0
- /package/{src → packages/pi-ai-vault/src}/prompt.ts +0 -0
- /package/{src → packages/pi-ai-vault/src}/scrub.ts +0 -0
|
@@ -9,6 +9,8 @@ use std::path::PathBuf;
|
|
|
9
9
|
use clap::{Parser, Subcommand};
|
|
10
10
|
use serde_json::json;
|
|
11
11
|
|
|
12
|
+
use crate::bwsession;
|
|
13
|
+
use crate::config::{self, ImportPolicy, UnlockMode, UnlockPolicy};
|
|
12
14
|
use crate::crypto::{self, Kdf};
|
|
13
15
|
use crate::dialog::{self, Answer};
|
|
14
16
|
use crate::import;
|
|
@@ -66,6 +68,36 @@ enum Cmd {
|
|
|
66
68
|
#[arg(long, value_name = "MIN")]
|
|
67
69
|
idle_min: Option<i64>,
|
|
68
70
|
},
|
|
71
|
+
/// Show or change machine-wide settings
|
|
72
|
+
Settings {
|
|
73
|
+
/// Dotted key (vault.unlock, vault.hours, bitwarden.unlock,
|
|
74
|
+
/// bitwarden.hours, bitwarden.import); omit to print everything
|
|
75
|
+
key: Option<String>,
|
|
76
|
+
/// New value; omit to print the current one
|
|
77
|
+
value: Option<String>,
|
|
78
|
+
/// Read the vault master password from stdin (changing vault.unlock)
|
|
79
|
+
#[arg(long)]
|
|
80
|
+
password_stdin: bool,
|
|
81
|
+
},
|
|
82
|
+
/// The persisted `bw` session key (bitwarden.unlock = never | time)
|
|
83
|
+
BwSession {
|
|
84
|
+
/// get | put | clear
|
|
85
|
+
action: String,
|
|
86
|
+
},
|
|
87
|
+
/// Show or change how often the master password is asked for
|
|
88
|
+
Mode {
|
|
89
|
+
/// "get" or "set"
|
|
90
|
+
#[arg(default_value = "get")]
|
|
91
|
+
action: String,
|
|
92
|
+
/// never | time | session (for `set`)
|
|
93
|
+
value: Option<String>,
|
|
94
|
+
/// Hours between prompts in `time` mode
|
|
95
|
+
#[arg(long, value_name = "HOURS")]
|
|
96
|
+
hours: Option<i64>,
|
|
97
|
+
/// Read the master password from stdin (changing the mode needs it)
|
|
98
|
+
#[arg(long)]
|
|
99
|
+
password_stdin: bool,
|
|
100
|
+
},
|
|
69
101
|
/// Session state, item count, and remaining read grants
|
|
70
102
|
Status {
|
|
71
103
|
#[arg(long, env = "AI_VAULT_TOKEN")]
|
|
@@ -265,10 +297,17 @@ fn dispatch(cmd: Cmd, paths: &Paths, json: bool) -> Outcome {
|
|
|
265
297
|
}
|
|
266
298
|
Cmd::Doctor => doctor(paths, &out),
|
|
267
299
|
Cmd::Init { password_stdin, requester, idle_min } => {
|
|
268
|
-
init(paths, &out, password_stdin, &requester,
|
|
300
|
+
init(paths, &out, password_stdin, &requester, idle_min)
|
|
269
301
|
}
|
|
270
302
|
Cmd::Unlock { password_stdin, requester, idle_min } => {
|
|
271
|
-
unlock(paths, &out, password_stdin, &requester,
|
|
303
|
+
unlock(paths, &out, password_stdin, &requester, idle_min)
|
|
304
|
+
}
|
|
305
|
+
Cmd::Settings { key, value, password_stdin } => {
|
|
306
|
+
settings(paths, &out, key.as_deref(), value.as_deref(), None, password_stdin)
|
|
307
|
+
}
|
|
308
|
+
Cmd::BwSession { action } => bw_session(paths, &out, &action),
|
|
309
|
+
Cmd::Mode { action, value, hours, password_stdin } => {
|
|
310
|
+
mode(paths, &out, &action, value.as_deref(), hours, password_stdin)
|
|
272
311
|
}
|
|
273
312
|
Cmd::Status { token } => status(paths, &out, token),
|
|
274
313
|
Cmd::Lock { token, all } => lock(paths, &out, token, all),
|
|
@@ -318,6 +357,25 @@ fn idle_ms(minutes: Option<i64>) -> i64 {
|
|
|
318
357
|
minutes.map(|m| m.clamp(1, 600) * 60_000).unwrap_or(IDLE_MS_DEFAULT)
|
|
319
358
|
}
|
|
320
359
|
|
|
360
|
+
/// How long a token made now stays valid. `--idle-min` keeps the old
|
|
361
|
+
/// idle-based behaviour; otherwise the machine's mode decides:
|
|
362
|
+
/// never -> no deadline (and the token is stored machine-wide)
|
|
363
|
+
/// time -> absolute deadline, `hours` from now
|
|
364
|
+
/// session -> no deadline, with a 7-day ceiling in case an agent dies
|
|
365
|
+
/// without locking
|
|
366
|
+
fn policy(paths: &Paths, idle_min: Option<i64>) -> (i64, Option<i64>, UnlockPolicy) {
|
|
367
|
+
let policy = config::load(paths).unlock;
|
|
368
|
+
if let Some(minutes) = idle_min {
|
|
369
|
+
return (idle_ms(Some(minutes)), None, policy);
|
|
370
|
+
}
|
|
371
|
+
let now = session::now_ms();
|
|
372
|
+
match policy.mode {
|
|
373
|
+
UnlockMode::Never => (0, None, policy),
|
|
374
|
+
UnlockMode::Time => (0, Some(now + policy.hours.clamp(1, 24 * 90) * 3_600_000), policy),
|
|
375
|
+
UnlockMode::Session => (0, Some(now + 7 * 24 * 3_600_000), policy),
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
321
379
|
// ---------------------------------------------------------------- input
|
|
322
380
|
|
|
323
381
|
fn read_password() -> Result<String, Fault> {
|
|
@@ -386,17 +444,42 @@ fn items_with(session: &Session, store: &crypto::StoreFile) -> Result<Vec<Item>,
|
|
|
386
444
|
})
|
|
387
445
|
}
|
|
388
446
|
|
|
447
|
+
/// The auto session is the `never` mode token: no deadline, no password, and
|
|
448
|
+
/// it belongs to the machine rather than to one agent.
|
|
449
|
+
fn auto_session(paths: &Paths) -> Option<Session> {
|
|
450
|
+
let session = session::load_auto(paths).ok().flatten()?;
|
|
451
|
+
let store = crypto::read_store(&paths.store()).ok().flatten()?;
|
|
452
|
+
if crypto::store_id(&store) != session.store_id {
|
|
453
|
+
session::delete_auto(paths);
|
|
454
|
+
return None;
|
|
455
|
+
}
|
|
456
|
+
Some(session)
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/// An explicit token, else the machine-wide auto session.
|
|
389
460
|
fn load_session(paths: &Paths, token: Option<&str>) -> Result<Session, Fault> {
|
|
390
|
-
let token = token.filter(|t| !t.is_empty())
|
|
461
|
+
if let Some(token) = token.filter(|t| !t.is_empty()) {
|
|
462
|
+
return session::load(paths, token).map_err(Fault::Auth);
|
|
463
|
+
}
|
|
464
|
+
auto_session(paths).ok_or_else(|| {
|
|
391
465
|
Fault::Auth("vault is locked (no session token) — unlock first".into())
|
|
392
|
-
})
|
|
393
|
-
|
|
466
|
+
})
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/// Write a session back where it came from: auto sessions stay machine-wide,
|
|
470
|
+
/// the others stay in the per-agent token file.
|
|
471
|
+
fn persist(paths: &Paths, session: &Session) {
|
|
472
|
+
let auto = session::load_auto(paths)
|
|
473
|
+
.ok()
|
|
474
|
+
.flatten()
|
|
475
|
+
.is_some_and(|saved| saved.token == session.token);
|
|
476
|
+
let _ = if auto { session::save_auto(paths, session) } else { session.save(paths) };
|
|
394
477
|
}
|
|
395
478
|
|
|
396
479
|
fn touch(paths: &Paths, session: &mut Session) {
|
|
397
480
|
session.last_active = session::now_ms();
|
|
398
481
|
let _ = session.prune_grants(session.last_active);
|
|
399
|
-
|
|
482
|
+
persist(paths, session);
|
|
400
483
|
}
|
|
401
484
|
|
|
402
485
|
/// Read-modify-write under one advisory lock. The closure never opens a dialog
|
|
@@ -433,6 +516,8 @@ fn doctor(paths: &Paths, out: &Out) -> Outcome {
|
|
|
433
516
|
"audit": paths.audit(),
|
|
434
517
|
"display": dialog::has_display(),
|
|
435
518
|
"zenity": which("zenity"),
|
|
519
|
+
"mode": config::load(paths).unlock.mode.as_str(),
|
|
520
|
+
"auto": paths.auto_session().exists(),
|
|
436
521
|
});
|
|
437
522
|
out.value(value.clone(), || {
|
|
438
523
|
let mut lines = Vec::new();
|
|
@@ -450,7 +535,7 @@ fn which(program: &str) -> bool {
|
|
|
450
535
|
})
|
|
451
536
|
}
|
|
452
537
|
|
|
453
|
-
fn init(paths: &Paths, out: &Out, password_stdin: bool, requester: &str,
|
|
538
|
+
fn init(paths: &Paths, out: &Out, password_stdin: bool, requester: &str, idle_min: Option<i64>) -> Outcome {
|
|
454
539
|
if paths.store().exists() {
|
|
455
540
|
return Err(Fault::Error(format!(
|
|
456
541
|
"vault already initialized — {}",
|
|
@@ -487,21 +572,28 @@ fn init(paths: &Paths, out: &Out, password_stdin: bool, requester: &str, idle_ms
|
|
|
487
572
|
crypto::write_atomic(&paths.store(), &bytes)?;
|
|
488
573
|
}
|
|
489
574
|
session::ensure_session_dir(paths)?;
|
|
490
|
-
let
|
|
491
|
-
session
|
|
575
|
+
let (idle, deadline, policy) = policy(paths, idle_min);
|
|
576
|
+
let session = Session::new(requester, idle, deadline, &sealed, &key);
|
|
577
|
+
if policy.mode == UnlockMode::Never {
|
|
578
|
+
session::save_auto(paths, &session)?;
|
|
579
|
+
} else {
|
|
580
|
+
session.save(paths)?;
|
|
581
|
+
}
|
|
492
582
|
audit::append(&paths.audit(), requester, "init", None, None);
|
|
493
583
|
let value = json!({
|
|
494
584
|
"ok": true,
|
|
495
585
|
"path": paths.store(),
|
|
496
586
|
"token": session.token,
|
|
497
|
-
"expiresAt": session.
|
|
587
|
+
"expiresAt": session.expiry(),
|
|
498
588
|
"items": 0,
|
|
589
|
+
"mode": policy.mode.as_str(),
|
|
590
|
+
"auto": policy.mode == UnlockMode::Never,
|
|
499
591
|
});
|
|
500
592
|
out.value(value, || format!("created {}", paths.store().display()));
|
|
501
593
|
Ok(())
|
|
502
594
|
}
|
|
503
595
|
|
|
504
|
-
fn unlock(paths: &Paths, out: &Out, password_stdin: bool, requester: &str,
|
|
596
|
+
fn unlock(paths: &Paths, out: &Out, password_stdin: bool, requester: &str, idle_min: Option<i64>) -> Outcome {
|
|
505
597
|
let store = open_store(paths)?;
|
|
506
598
|
let password = if password_stdin {
|
|
507
599
|
read_password()?
|
|
@@ -517,21 +609,238 @@ fn unlock(paths: &Paths, out: &Out, password_stdin: bool, requester: &str, idle_
|
|
|
517
609
|
})?;
|
|
518
610
|
session::ensure_session_dir(paths)?;
|
|
519
611
|
session::sweep(paths);
|
|
520
|
-
let
|
|
521
|
-
session
|
|
522
|
-
|
|
612
|
+
let (idle, deadline, policy) = policy(paths, idle_min);
|
|
613
|
+
let session = Session::new(requester, idle, deadline, &store, &key);
|
|
614
|
+
if policy.mode == UnlockMode::Never {
|
|
615
|
+
session::save_auto(paths, &session)?;
|
|
616
|
+
} else {
|
|
617
|
+
session.save(paths)?;
|
|
618
|
+
}
|
|
619
|
+
audit::append(&paths.audit(), requester, "unlock", None, Some(policy.mode.as_str()));
|
|
523
620
|
let value = json!({
|
|
524
621
|
"ok": true,
|
|
525
622
|
"token": session.token,
|
|
526
|
-
"expiresAt": session.
|
|
623
|
+
"expiresAt": session.expiry(),
|
|
527
624
|
"items": items.len(),
|
|
528
625
|
"requester": requester,
|
|
626
|
+
"mode": policy.mode.as_str(),
|
|
627
|
+
"auto": policy.mode == UnlockMode::Never,
|
|
529
628
|
"path": paths.store(),
|
|
530
629
|
});
|
|
531
630
|
out.value(value, || format!("unlocked {} ({} entries)", paths.store().display(), items.len()));
|
|
532
631
|
Ok(())
|
|
533
632
|
}
|
|
534
633
|
|
|
634
|
+
/// Dotted setting names, with the aliases a human is likely to type.
|
|
635
|
+
fn setting_key(raw: &str) -> Option<&'static str> {
|
|
636
|
+
match raw.trim().to_lowercase().as_str() {
|
|
637
|
+
"vault.unlock" | "vault.mode" | "unlock" | "mode" => Some("vault.unlock"),
|
|
638
|
+
"vault.hours" | "hours" => Some("vault.hours"),
|
|
639
|
+
"bitwarden.unlock" | "bitwarden.mode" | "bw.unlock" | "bw.mode" => Some("bitwarden.unlock"),
|
|
640
|
+
"bitwarden.hours" | "bw.hours" => Some("bitwarden.hours"),
|
|
641
|
+
"bitwarden.import" | "bw.import" | "import" => Some("bitwarden.import"),
|
|
642
|
+
_ => None,
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
fn settings_value(config: &config::Config, paths: &Paths) -> serde_json::Value {
|
|
647
|
+
let bw_saved = bwsession::load(paths).ok().flatten();
|
|
648
|
+
json!({
|
|
649
|
+
"ok": true,
|
|
650
|
+
"vault": {
|
|
651
|
+
"unlock": config.unlock.mode.as_str(),
|
|
652
|
+
"hours": config.unlock.hours,
|
|
653
|
+
"auto": paths.auto_session().exists(),
|
|
654
|
+
},
|
|
655
|
+
"bitwarden": {
|
|
656
|
+
"unlock": config.bitwarden.unlock.as_str(),
|
|
657
|
+
"hours": config.bitwarden.hours,
|
|
658
|
+
"import": config.bitwarden.import.as_str(),
|
|
659
|
+
"persisted": bw_saved.is_some(),
|
|
660
|
+
"expiresAt": bw_saved.as_ref().and_then(|s| s.expires_at),
|
|
661
|
+
},
|
|
662
|
+
})
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
fn settings_human(config: &config::Config, paths: &Paths) -> String {
|
|
666
|
+
[
|
|
667
|
+
format!("vault.unlock: {} (never | time | session)", config.unlock.mode.as_str()),
|
|
668
|
+
format!("vault.hours: {} (time 模式)", config.unlock.hours),
|
|
669
|
+
format!(
|
|
670
|
+
"vault 机器级会话: {}",
|
|
671
|
+
if paths.auto_session().exists() { "有(免密)" } else { "无" }
|
|
672
|
+
),
|
|
673
|
+
format!("bitwarden.unlock: {} (never | time | session)", config.bitwarden.unlock.as_str()),
|
|
674
|
+
format!("bitwarden.hours: {}", config.bitwarden.hours),
|
|
675
|
+
format!("bitwarden.import: {} (ask | always | never)", config.bitwarden.import.as_str()),
|
|
676
|
+
format!(
|
|
677
|
+
"bitwarden 已落盘的会话: {}",
|
|
678
|
+
match bwsession::load(paths).ok().flatten() {
|
|
679
|
+
Some(saved) => match saved.expires_at {
|
|
680
|
+
Some(at) => format!("有({} 后过期)", ((at - session::now_ms()) / 60_000).max(0)),
|
|
681
|
+
None => "有(不过期)".to_string(),
|
|
682
|
+
},
|
|
683
|
+
None => "无".to_string(),
|
|
684
|
+
}
|
|
685
|
+
),
|
|
686
|
+
]
|
|
687
|
+
.join("\n")
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
fn settings(
|
|
691
|
+
paths: &Paths,
|
|
692
|
+
out: &Out,
|
|
693
|
+
key: Option<&str>,
|
|
694
|
+
value: Option<&str>,
|
|
695
|
+
hours: Option<i64>,
|
|
696
|
+
password_stdin: bool,
|
|
697
|
+
) -> Outcome {
|
|
698
|
+
let mut config = config::load(paths);
|
|
699
|
+
|
|
700
|
+
let Some(raw_key) = key.filter(|k| !k.trim().is_empty()) else {
|
|
701
|
+
let value = settings_value(&config, paths);
|
|
702
|
+
out.value(value, || settings_human(&config, paths));
|
|
703
|
+
return Ok(());
|
|
704
|
+
};
|
|
705
|
+
let key = setting_key(raw_key).ok_or_else(|| {
|
|
706
|
+
Fault::Usage(format!(
|
|
707
|
+
"unknown setting \"{raw_key}\" — vault.unlock, vault.hours, bitwarden.unlock, bitwarden.hours, bitwarden.import"
|
|
708
|
+
))
|
|
709
|
+
})?;
|
|
710
|
+
|
|
711
|
+
let Some(raw_value) = value.map(str::trim).filter(|v| !v.is_empty()) else {
|
|
712
|
+
let shown = match key {
|
|
713
|
+
"vault.unlock" => config.unlock.mode.as_str().to_string(),
|
|
714
|
+
"vault.hours" => config.unlock.hours.to_string(),
|
|
715
|
+
"bitwarden.unlock" => config.bitwarden.unlock.as_str().to_string(),
|
|
716
|
+
"bitwarden.hours" => config.bitwarden.hours.to_string(),
|
|
717
|
+
_ => config.bitwarden.import.as_str().to_string(),
|
|
718
|
+
};
|
|
719
|
+
out.value(json!({ "ok": true, "key": key, "value": shown }), || format!("{key}: {shown}"));
|
|
720
|
+
return Ok(());
|
|
721
|
+
};
|
|
722
|
+
|
|
723
|
+
match key {
|
|
724
|
+
"vault.unlock" => {
|
|
725
|
+
let mode = UnlockMode::parse(raw_value)
|
|
726
|
+
.ok_or_else(|| Fault::Usage(format!("unknown mode \"{raw_value}\" — never, time or session")))?;
|
|
727
|
+
let store = open_store(paths)?;
|
|
728
|
+
// Changing when the vault opens is a security decision: always verify.
|
|
729
|
+
let password = if password_stdin {
|
|
730
|
+
read_password()?
|
|
731
|
+
} else {
|
|
732
|
+
ask_password("ai-vault · 主密码", "切换解锁模式")?
|
|
733
|
+
};
|
|
734
|
+
let (_, vault_key) = crypto::open_with_passphrase(&store, &password).map_err(|e| match e {
|
|
735
|
+
crypto::OpenError::WrongPassword => Fault::Auth("wrong master password".into()),
|
|
736
|
+
crypto::OpenError::Broken(message) => Fault::Error(message),
|
|
737
|
+
})?;
|
|
738
|
+
config.unlock = UnlockPolicy { mode, hours: hours.unwrap_or(config.unlock.hours).clamp(1, 24 * 90) };
|
|
739
|
+
config::save(paths, &config)?;
|
|
740
|
+
session::ensure_session_dir(paths)?;
|
|
741
|
+
if mode == UnlockMode::Never {
|
|
742
|
+
let auto = Session::new("mode", 0, None, &store, &vault_key);
|
|
743
|
+
session::save_auto(paths, &auto)?;
|
|
744
|
+
} else {
|
|
745
|
+
session::delete_auto(paths);
|
|
746
|
+
}
|
|
747
|
+
audit::append(&paths.audit(), "cli", "mode", None, Some(mode.as_str()));
|
|
748
|
+
}
|
|
749
|
+
"vault.hours" => {
|
|
750
|
+
let hours: i64 = raw_value
|
|
751
|
+
.parse()
|
|
752
|
+
.map_err(|_| Fault::Usage(format!("\"{raw_value}\" is not a number of hours")))?;
|
|
753
|
+
config.unlock.hours = hours.clamp(1, 24 * 90);
|
|
754
|
+
config::save(paths, &config)?;
|
|
755
|
+
}
|
|
756
|
+
"bitwarden.unlock" => {
|
|
757
|
+
let mode = UnlockMode::parse(raw_value)
|
|
758
|
+
.ok_or_else(|| Fault::Usage(format!("unknown mode \"{raw_value}\" — never, time or session")))?;
|
|
759
|
+
config.bitwarden.unlock = mode;
|
|
760
|
+
if let Some(hours) = hours {
|
|
761
|
+
config.bitwarden.hours = hours.clamp(1, 24 * 90);
|
|
762
|
+
}
|
|
763
|
+
config::save(paths, &config)?;
|
|
764
|
+
// Going back to per-session must not leave a key on disk.
|
|
765
|
+
if mode == UnlockMode::Session {
|
|
766
|
+
bwsession::clear(paths);
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
"bitwarden.hours" => {
|
|
770
|
+
let hours: i64 = raw_value
|
|
771
|
+
.parse()
|
|
772
|
+
.map_err(|_| Fault::Usage(format!("\"{raw_value}\" is not a number of hours")))?;
|
|
773
|
+
config.bitwarden.hours = hours.clamp(1, 24 * 90);
|
|
774
|
+
config::save(paths, &config)?;
|
|
775
|
+
}
|
|
776
|
+
_ => {
|
|
777
|
+
let import = ImportPolicy::parse(raw_value).ok_or_else(|| {
|
|
778
|
+
Fault::Usage(format!("unknown import policy \"{raw_value}\" — ask, always or never"))
|
|
779
|
+
})?;
|
|
780
|
+
config.bitwarden.import = import;
|
|
781
|
+
config::save(paths, &config)?;
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
let value = settings_value(&config, paths);
|
|
786
|
+
out.value(value, || settings_human(&config, paths));
|
|
787
|
+
Ok(())
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
/// `/vault mode` is a shortcut for the `vault.unlock` setting.
|
|
791
|
+
fn mode(
|
|
792
|
+
paths: &Paths,
|
|
793
|
+
out: &Out,
|
|
794
|
+
action: &str,
|
|
795
|
+
value: Option<&str>,
|
|
796
|
+
hours: Option<i64>,
|
|
797
|
+
password_stdin: bool,
|
|
798
|
+
) -> Outcome {
|
|
799
|
+
match action.trim() {
|
|
800
|
+
"" | "get" | "status" => settings(paths, out, Some("vault.unlock"), None, None, password_stdin),
|
|
801
|
+
"set" => settings(paths, out, Some("vault.unlock"), value, hours, password_stdin),
|
|
802
|
+
other => Err(Fault::Usage(format!("unknown mode action \"{other}\""))),
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
fn bw_session(paths: &Paths, out: &Out, action: &str) -> Outcome {
|
|
807
|
+
match action.trim() {
|
|
808
|
+
"" | "get" => {
|
|
809
|
+
let stored = bwsession::load(paths)?
|
|
810
|
+
.ok_or_else(|| Fault::Auth("no persisted Bitwarden session — unlock Bitwarden first".into()))?;
|
|
811
|
+
out.value(
|
|
812
|
+
json!({ "ok": true, "key": stored.key, "savedAt": stored.saved_at, "expiresAt": stored.expires_at }),
|
|
813
|
+
|| stored.key.clone(),
|
|
814
|
+
);
|
|
815
|
+
Ok(())
|
|
816
|
+
}
|
|
817
|
+
"put" | "save" => {
|
|
818
|
+
let key = read_stdin_text()?.trim().to_string();
|
|
819
|
+
if key.is_empty() {
|
|
820
|
+
return Err(Fault::Usage("bw-session put expects the session key on stdin".into()));
|
|
821
|
+
}
|
|
822
|
+
let expires_at = bwsession::save(paths, &key)?;
|
|
823
|
+
audit::append(&paths.audit(), "cli", "bw_session", None, Some("put"));
|
|
824
|
+
out.value(json!({ "ok": true, "expiresAt": expires_at }), || {
|
|
825
|
+
match expires_at {
|
|
826
|
+
Some(at) => format!("saved (expires in {} min)", ((at - session::now_ms()) / 60_000).max(0)),
|
|
827
|
+
None => "saved (no expiry)".to_string(),
|
|
828
|
+
}
|
|
829
|
+
});
|
|
830
|
+
Ok(())
|
|
831
|
+
}
|
|
832
|
+
"clear" | "delete" => {
|
|
833
|
+
let removed = bwsession::clear(paths);
|
|
834
|
+
audit::append(&paths.audit(), "cli", "bw_session", None, Some("clear"));
|
|
835
|
+
out.value(json!({ "ok": true, "removed": removed }), || {
|
|
836
|
+
if removed { "cleared".to_string() } else { "nothing stored".to_string() }
|
|
837
|
+
});
|
|
838
|
+
Ok(())
|
|
839
|
+
}
|
|
840
|
+
other => Err(Fault::Usage(format!("unknown bw-session action \"{other}\""))),
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
|
|
535
844
|
fn status(paths: &Paths, out: &Out, token: Option<String>) -> Outcome {
|
|
536
845
|
let mut session = load_session(paths, token.as_deref())?;
|
|
537
846
|
let store = open_store(paths)?;
|
|
@@ -539,29 +848,42 @@ fn status(paths: &Paths, out: &Out, token: Option<String>) -> Outcome {
|
|
|
539
848
|
let now = session::now_ms();
|
|
540
849
|
session.prune_grants(now);
|
|
541
850
|
session.last_active = now;
|
|
542
|
-
|
|
851
|
+
persist(paths, &session);
|
|
543
852
|
let grants: serde_json::Map<String, serde_json::Value> = session
|
|
544
853
|
.grants
|
|
545
854
|
.iter()
|
|
546
855
|
.map(|(name, until)| (name.clone(), json!({ "expiresAt": until, "leftMs": until - now })))
|
|
547
856
|
.collect();
|
|
857
|
+
let policy = config::load(paths).unlock;
|
|
858
|
+
let auto = session::load_auto(paths)
|
|
859
|
+
.ok()
|
|
860
|
+
.flatten()
|
|
861
|
+
.is_some_and(|saved| saved.token == session.token);
|
|
548
862
|
let value = json!({
|
|
549
863
|
"ok": true,
|
|
550
864
|
"unlocked": true,
|
|
551
865
|
"items": items.len(),
|
|
552
866
|
"requester": session.requester,
|
|
553
867
|
"idleMs": session.idle_ms,
|
|
554
|
-
"expiresAt": session.
|
|
868
|
+
"expiresAt": session.expiry(),
|
|
869
|
+
"mode": policy.mode.as_str(),
|
|
870
|
+
"hours": policy.hours,
|
|
871
|
+
"auto": auto,
|
|
555
872
|
"store": paths.store(),
|
|
556
873
|
"grants": grants,
|
|
557
874
|
});
|
|
558
875
|
out.value(value.clone(), || {
|
|
876
|
+
let expiry = match session.expiry() {
|
|
877
|
+
Some(at) => format!("in {}s", ((at - now) / 1000).max(0)),
|
|
878
|
+
None => "never".to_string(),
|
|
879
|
+
};
|
|
559
880
|
let mut lines = vec![
|
|
560
881
|
format!("store: {}", paths.store().display()),
|
|
561
882
|
format!("requester: {}", session.requester),
|
|
562
883
|
"state: unlocked".to_string(),
|
|
884
|
+
format!("mode: {}{}", policy.mode.as_str(), if auto { " (machine-wide)" } else { "" }),
|
|
563
885
|
format!("items: {}", items.len()),
|
|
564
|
-
format!("expires:
|
|
886
|
+
format!("expires: {expiry}"),
|
|
565
887
|
];
|
|
566
888
|
for (name, until) in &session.grants {
|
|
567
889
|
lines.push(format!("grant: {name} ({}s left)", ((until - now) / 1000).max(0)));
|
|
@@ -573,17 +895,26 @@ fn status(paths: &Paths, out: &Out, token: Option<String>) -> Outcome {
|
|
|
573
895
|
|
|
574
896
|
fn lock(paths: &Paths, out: &Out, token: Option<String>, all: bool) -> Outcome {
|
|
575
897
|
let mut count = 0;
|
|
898
|
+
let mut auto = false;
|
|
576
899
|
if all {
|
|
577
900
|
count = session::delete_all(paths);
|
|
901
|
+
auto = session::delete_auto(paths);
|
|
578
902
|
} else if let Some(token) = token.filter(|t| !t.is_empty()) {
|
|
579
903
|
if session::delete(paths, &token) {
|
|
580
904
|
count = 1;
|
|
581
905
|
}
|
|
582
906
|
} else {
|
|
583
|
-
|
|
907
|
+
// No token: the only thing to lock here is the machine-wide session.
|
|
908
|
+
auto = session::delete_auto(paths);
|
|
909
|
+
if !auto {
|
|
910
|
+
return Err(Fault::Usage("pass --token or --all".into()));
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
if auto {
|
|
914
|
+
count += 1;
|
|
584
915
|
}
|
|
585
916
|
audit::append(&paths.audit(), "cli", "lock", None, Some(if all { "all" } else { "token" }));
|
|
586
|
-
out.value(json!({ "ok": true, "removed": count }), || format!("locked {count} session(s)"));
|
|
917
|
+
out.value(json!({ "ok": true, "removed": count, "auto": auto }), || format!("locked {count} session(s)"));
|
|
587
918
|
Ok(())
|
|
588
919
|
}
|
|
589
920
|
|
|
@@ -624,7 +955,12 @@ fn get(
|
|
|
624
955
|
})?;
|
|
625
956
|
|
|
626
957
|
let now = session::now_ms();
|
|
627
|
-
|
|
958
|
+
// `never` mode is the "the agent may use these freely" mode: the machine-wide
|
|
959
|
+
// session is the authorization, so no window opens on each read.
|
|
960
|
+
let auto = token.as_deref().map_or(true, |t| t.trim().is_empty());
|
|
961
|
+
let via = if auto {
|
|
962
|
+
"no-password"
|
|
963
|
+
} else if session.grant(&item.name, now).is_some() {
|
|
628
964
|
"existing-grant"
|
|
629
965
|
} else {
|
|
630
966
|
let message = format!(
|
|
@@ -662,7 +998,7 @@ fn get(
|
|
|
662
998
|
};
|
|
663
999
|
|
|
664
1000
|
session.last_active = session::now_ms();
|
|
665
|
-
|
|
1001
|
+
persist(paths, &session);
|
|
666
1002
|
audit::append(&paths.audit(), &session.requester, "read_allow", Some(&item.name), reason);
|
|
667
1003
|
|
|
668
1004
|
let mut value = serde_json::to_value(&item).map_err(|e| Fault::Error(e.to_string()))?;
|
|
@@ -757,7 +1093,7 @@ fn add(paths: &Paths, out: &Out, token: Option<String>, req: AddRequest) -> Outc
|
|
|
757
1093
|
Ok(())
|
|
758
1094
|
})?;
|
|
759
1095
|
session.last_active = session::now_ms();
|
|
760
|
-
|
|
1096
|
+
persist(paths, &session);
|
|
761
1097
|
audit::append(&paths.audit(), &session.requester, "add", Some(&name), None);
|
|
762
1098
|
out.value(json!({ "ok": true, "item": name, "created": inserted }), || {
|
|
763
1099
|
format!("saved \"{name}\" (password entered in the dialog, not shown)")
|
|
@@ -790,7 +1126,7 @@ fn save(paths: &Paths, out: &Out, token: Option<String>, file: Option<PathBuf>,
|
|
|
790
1126
|
Ok(())
|
|
791
1127
|
})?;
|
|
792
1128
|
session.last_active = session::now_ms();
|
|
793
|
-
|
|
1129
|
+
persist(paths, &session);
|
|
794
1130
|
audit::append(&paths.audit(), &session.requester, "import", Some(&format!("drafts:{count}")), None);
|
|
795
1131
|
out.value(json!({ "ok": true, "count": count, "names": names }), || format!("saved {count} entries"));
|
|
796
1132
|
Ok(())
|
|
@@ -817,7 +1153,7 @@ fn remove(paths: &Paths, out: &Out, token: Option<String>, name: &str, yes: bool
|
|
|
817
1153
|
})?;
|
|
818
1154
|
session.grants.remove(&name.to_lowercase());
|
|
819
1155
|
session.last_active = session::now_ms();
|
|
820
|
-
|
|
1156
|
+
persist(paths, &session);
|
|
821
1157
|
audit::append(&paths.audit(), &session.requester, "remove", Some(name), None);
|
|
822
1158
|
out.value(json!({ "ok": true, "removed": removed }), || if removed { "removed".to_string() } else { "not found".to_string() });
|
|
823
1159
|
Ok(())
|
|
@@ -832,7 +1168,7 @@ fn grant(paths: &Paths, out: &Out, token: Option<String>, name: &str, minutes: O
|
|
|
832
1168
|
let until = session::now_ms() + minutes * 60_000;
|
|
833
1169
|
session.grants.insert(item.name.to_lowercase(), until);
|
|
834
1170
|
session.last_active = session::now_ms();
|
|
835
|
-
|
|
1171
|
+
persist(paths, &session);
|
|
836
1172
|
audit::append(&paths.audit(), &session.requester, "grant", Some(&item.name), Some("manual"));
|
|
837
1173
|
out.value(json!({ "ok": true, "name": item.name, "minutes": minutes, "expiresAt": until }), || {
|
|
838
1174
|
format!("granted \"{}\" for {minutes}m", item.name)
|
|
@@ -850,7 +1186,7 @@ fn revoke(paths: &Paths, out: &Out, token: Option<String>, name: Option<&str>, a
|
|
|
850
1186
|
return Err(Fault::Usage("pass an entry name or --all".into()));
|
|
851
1187
|
}
|
|
852
1188
|
session.last_active = session::now_ms();
|
|
853
|
-
|
|
1189
|
+
persist(paths, &session);
|
|
854
1190
|
audit::append(&paths.audit(), &session.requester, "revoke", name, Some(if all { "all" } else { "entry" }));
|
|
855
1191
|
out.value(json!({ "ok": true }), || "revoked".to_string());
|
|
856
1192
|
Ok(())
|
|
@@ -914,7 +1250,7 @@ fn import_file(paths: &Paths, out: &Out, token: Option<String>, file: &PathBuf,
|
|
|
914
1250
|
Ok(())
|
|
915
1251
|
})?;
|
|
916
1252
|
session.last_active = session::now_ms();
|
|
917
|
-
|
|
1253
|
+
persist(paths, &session);
|
|
918
1254
|
audit::append(&paths.audit(), &session.requester, "import", Some(&format!("json:{count}")), None);
|
|
919
1255
|
out.value(json!({ "ok": true, "count": count, "names": names }), || format!("imported {count} login items"));
|
|
920
1256
|
Ok(())
|