agentmux_srv_core/backend/vault/
changes.rs1use super::signins::{self, Space};
16use super::versions::{self, Rotation};
17use super::{record, seal, EntryChange, Parts};
18use crate::backend::storage::store::Store;
19use crate::backend::storage::vault::VaultRow;
20
21pub const HOW: &str = "password change";
23pub const CHANGED: &str = "changed password";
25
26pub fn saved_login(store: &Store, s: &Space, username: &str, agent: &str) -> Result<VaultRow, String> {
30 let mut rows = Vec::new();
31 for row in signins::held(store, s)? {
32 let never_sent = signins::tags_of(&row).iter().any(|t| t == super::signups::UNCONFIRMED);
33 if !never_sent && super::vaults::entry_usable(store, &row.id, Some(agent))? {
34 rows.push(row);
35 }
36 }
37 let named = |r: &VaultRow| signins::fields_of(r).get(signins::USERNAME).map(String::as_str).unwrap_or("") == username;
38 let found: Vec<VaultRow> = if username.is_empty() { rows } else { rows.into_iter().filter(named).collect() };
39 match found.len() {
40 1 => found.into_iter().next().ok_or_else(|| "no sign-in".to_string()),
41 0 => Err(format!(
42 "the Vault has no saved sign-in for {} in this tab's profile that {agent} may use, so there is no password to change: sign in with it first, or ask the person",
43 s.origin
44 )),
45 n => Err(format!("{n} sign-ins are saved for {} in this tab's profile: give username, the user name this account signs in with", s.origin)),
46 }
47}
48
49pub fn start(store: &Store, row: &VaultRow, password: &str, agent: &str) -> Result<Rotation, String> {
52 versions::set_aside_pending(store, row, HOW)?;
53 versions::start_pending(store, &row.id, password, agent, HOW)
54}
55
56pub fn abandon(store: &Store, change: &Rotation) -> Result<(), String> {
58 versions::abandon_rotation(store, change)
59}
60
61pub fn confirm_sent(store: &Store, s: &Space, password: &str) -> Result<Option<String>, String> {
65 for row in signins::held(store, s)? {
66 let Some(change) = versions::pending_is(store, &row, password, HOW)? else { continue };
67 versions::finish_rotation(store, &row.id, &change, Ok(()), &change.by)?;
68 let tags: Vec<String> = signins::tags_of(&row).into_iter().filter(|t| t != signins::REFUSED).collect();
70 if tags != signins::tags_of(&row) {
71 super::update(store, EntryChange { id: row.id.clone(), label: row.label.clone(), fields: signins::fields_of(&row), tags, parts: Parts::default(), clear: Vec::new() })?;
72 }
73 record(store, &row.id, &row.label, &change.by, CHANGED, &s.origin, "done");
74 return Ok(Some(row.id));
75 }
76 Ok(None)
77}
78
79#[cfg(test)]
81pub(super) fn current_is(store: &Store, id: &str, password: &str) -> bool {
82 let Ok(Some(row)) = store.vault_get(id) else { return false };
83 seal::key(false).ok().and_then(|k| seal::open(&k, &row.id, &row.sealed).ok()).is_some_and(|p| p.get("password") == Some(password))
84}
85
86#[cfg(test)]
87#[path = "changes_tests.rs"]
88mod tests;