Skip to main content

agentmux_srv_core/backend/vault/
changes.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! A password an agent changes on a site that already has an account
5//! (docs/specs/SPEC_VAULT_GENERATE_CAPTURE_AND_BROWSER_SIGNINS_2026_10_28.md ยง7.3).
6//!
7//! The new password is a **pending version** of the saved sign-in for that site and user
8//! ([`super::versions`], as a rotation's is), not an entry of its own and not the entry's value: the
9//! saved password stays the one that works until the site was sent the form with the new one. When the
10//! page's listener reports a password-change form sent with exactly the pending value, the pending
11//! value becomes the entry's and the old one is kept 7 days, replaced by the agent that made the change,
12//! for the person to put back. A form never sent (or refused) leaves the saved password as it was; the
13//! pending value stays for the person in the entry's earlier values, as an interrupted rotation's does.
14
15use 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
21/// What the version is called in the entry's history.
22pub const HOW: &str = "password change";
23/// What the Activity line says when the site was sent the new password.
24pub const CHANGED: &str = "changed password";
25
26/// The saved sign-in the agent means: the one `profile` keeps for `s`'s site, for `username` (the one
27/// the agent or the form names) or, when none is named, the site's only one. It must be in a Vault the
28/// agent may use, and not one made on a sign-up that was never sent.
29pub 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
49/// Keeps `password` as the pending change of `row`'s password, made by `agent`. A change made earlier and
50/// never sent is set aside for the person (the site may still hold it).
51pub 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
56/// The form of a change was never typed into: nothing saw the pending value, so none is kept.
57pub fn abandon(store: &Store, change: &Rotation) -> Result<(), String> {
58    versions::abandon_rotation(store, change)
59}
60
61/// A password-change form of `s` was sent with `password`. If that is the pending change of one of the
62/// site's saved sign-ins, it becomes the sign-in's password and the old one is kept 7 days. Gives the
63/// entry. `None` for any other password: the person's own, offered "Update password?" like any sign-in.
64pub 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        // The site took it: a sign-in it had turned down is worth offering again.
69        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/// Whether entry `id`'s current password is `password`: for tests and callers that must not hand it out.
80#[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;