Skip to main content

agentmux_srv_core/backend/vault/
signups.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Sign-ins made on a sign-up form (spec SPEC_VAULT_GENERATE_CAPTURE_AND_BROWSER_SIGNINS_2026_10_28.md
5//! §6.2): the password srv generates for a new-password field is a `login`
6//! entry of its own before the page sees it, tagged [`UNCONFIRMED`] until the
7//! form is sent with it. It never joins another entry, even one for the same
8//! user name: a sign-up the site refuses must not replace a password that works.
9
10use std::collections::BTreeMap;
11
12use super::signins::{self, Space};
13use super::{insert_in, record, seal, EntryChange, NewEntry, Parts, PERSON};
14use crate::backend::storage::store::Store;
15
16/// On a sign-in made on a sign-up form that hasn't been sent with it yet.
17pub const UNCONFIRMED: &str = "unconfirmed: made on sign-up, not yet sent";
18
19/// On a password an agent made in an Incognito tab, which keeps no sign-ins: it
20/// is kept in Personal, since the account it opened is real (spec §9.2).
21pub const FROM_INCOGNITO: &str = "made in an Incognito tab";
22
23/// Keeps `password`, made for a sign-up on `s`, as a new entry with the user
24/// name typed so far. Returns its id and label.
25pub fn create(store: &Store, s: &Space, username: &str, password: &str) -> Result<(String, String), String> {
26    create_tagged(store, s, username, password, None)
27}
28
29/// [`create`], with one more tag.
30pub fn create_tagged(store: &Store, s: &Space, username: &str, password: &str, extra: Option<&str>) -> Result<(String, String), String> {
31    let mut fields = BTreeMap::from([(signins::SITE.to_string(), s.origin.clone()), (signins::PROFILE.to_string(), s.profile.clone())]);
32    if !username.is_empty() {
33        fields.insert(signins::USERNAME.to_string(), username.to_string());
34    }
35    let mut parts = Parts::default();
36    parts.0.insert("password".to_string(), password.to_string());
37    let label = signins::label_for(s);
38    let entry = NewEntry { kind: "login".into(), label: label.clone(), fields, tags: std::iter::once(UNCONFIRMED).chain(extra).map(str::to_string).collect(), parts };
39    let id = insert_in(store, entry, PERSON, "made", "on a sign-up form", &signins::vault_for(store, &s.profile)?)?;
40    Ok((id, label))
41}
42
43/// The site refused the password made for entry `id` and the person asked for
44/// another: the new one replaces it, the refused one kept as a previous
45/// version. Only while the entry is still unconfirmed; `None` otherwise, and
46/// the caller makes a new entry.
47pub fn replace(store: &Store, id: &str, password: &str) -> Result<Option<String>, String> {
48    let Some(row) = store.vault_get(id).map_err(|e| e.to_string())? else { return Ok(None) };
49    let tags = signins::tags_of(&row);
50    if !tags.iter().any(|t| t == UNCONFIRMED) {
51        return Ok(None);
52    }
53    let mut parts = Parts::default();
54    parts.0.insert("password".to_string(), password.to_string());
55    super::update(store, EntryChange { id: row.id.clone(), label: row.label.clone(), fields: signins::fields_of(&row), tags, parts, clear: Vec::new() })?;
56    Ok(Some(row.label))
57}
58
59/// A sign-up form was sent with `password` as `username`: if that is the
60/// password made for entry `id`, which is still unconfirmed, the entry records
61/// the user name sent and loses its tag. False for any other password (the
62/// person typed their own), or an entry already confirmed or gone.
63pub fn confirm(store: &Store, id: &str, username: &str, password: &str) -> Result<bool, String> {
64    let Some(row) = store.vault_get(id).map_err(|e| e.to_string())? else { return Ok(false) };
65    let mut tags = signins::tags_of(&row);
66    if row.kind != "login" || !tags.iter().any(|t| t == UNCONFIRMED) {
67        return Ok(false);
68    }
69    let parts = seal::open(&seal::key(false)?, &row.id, &row.sealed)?;
70    if parts.get("password") != Some(password) {
71        return Ok(false);
72    }
73    tags.retain(|t| t != UNCONFIRMED);
74    let mut fields = signins::fields_of(&row);
75    let site = fields.get(signins::SITE).cloned().unwrap_or_default();
76    if !username.is_empty() {
77        fields.insert(signins::USERNAME.to_string(), username.to_string());
78    }
79    super::update(store, EntryChange { id: row.id.clone(), label: row.label.clone(), fields, tags, parts: Parts::default(), clear: Vec::new() })?;
80    record(store, &row.id, &row.label, PERSON, "sent on sign-up", &site, "done");
81    Ok(true)
82}
83
84/// A sign-up form of `s` was sent with `password` as `username`: confirm the
85/// unconfirmed entry made for `s` with exactly that password, if there is one.
86/// Found in the Vault, not in memory, so a form sent after an srv restart, or
87/// long after the password was made, still confirms it. Returns its id.
88pub fn confirm_sent(store: &Store, s: &Space, username: &str, password: &str) -> Result<Option<String>, String> {
89    let key = seal::key(false)?;
90    for row in store.vault_list().map_err(|e| e.to_string())? {
91        let fields = signins::fields_of(&row);
92        let get = |k: &str| fields.get(k).map(String::as_str).unwrap_or("");
93        if row.kind != "login" || get(signins::PROFILE) != s.profile || !signins::same_site(get(signins::SITE), &s.origin) || !signins::tags_of(&row).iter().any(|t| t == UNCONFIRMED) {
94            continue;
95        }
96        if seal::open(&key, &row.id, &row.sealed)?.get("password") == Some(password) && confirm(store, &row.id, username, password)? {
97            return Ok(Some(row.id));
98        }
99    }
100    Ok(None)
101}
102
103#[cfg(test)]
104#[path = "signups_tests.rs"]
105mod tests;