Skip to main content

agentmux_srv/server/
vault_prompt_share.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! A shared sign-in for a site's HTTP prompt, in an agent's own browser profile
5//! (docs/specs/SPEC_VAULT_AGENTS_ENTER_CREDENTIALS_WHEREVER_ASKED_2026_11_16.md A11).
6//!
7//! The host's credential broker asks for a pane's profile, origin and realm, as it does for the profile's own
8//! sign-ins ([`super::service::credential`]). When the profile is an agent's own and holds none, the login the person
9//! shared with that agent answers. This module says whose profile it is, what the host is told, and which uses the
10//! person has already said yes to: an "Always" from the share card, or the card's yes itself, which covers the one
11//! prompt that was waiting for it.
12
13use std::collections::HashMap;
14use std::sync::LazyLock;
15use std::time::{Duration, Instant};
16
17use parking_lot::Mutex;
18
19use crate::backend::browser_profiles_store::agent_profile_id;
20use crate::backend::vault::shares;
21use crate::backend::vault::signins::{self, ProfileOwner, Space};
22
23use super::AppState;
24
25/// How long the person's yes on a share card also answers the prompt that waited for it.
26const YES_COVERS: Duration = Duration::from_secs(5 * 60);
27
28/// The yeses given on a share card that no sign-in has used yet, by entry and agent uid, with when each lapses.
29static YESES: LazyLock<Mutex<HashMap<(String, String), Instant>>> = LazyLock::new(|| Mutex::new(HashMap::new()));
30
31/// The person said yes on the share card of `entry` for agent `uid`: the next prompt it answers in the next
32/// [`YES_COVERS`] is not asked about again.
33pub(crate) fn yes_given(entry: &str, uid: &str) {
34    yes_given_until(entry, uid, Instant::now() + YES_COVERS);
35}
36
37fn yes_given_until(entry: &str, uid: &str, until: Instant) {
38    let mut yeses = YESES.lock();
39    let now = Instant::now();
40    yeses.retain(|_, lapses| *lapses > now);
41    yeses.insert((entry.to_string(), uid.to_string()), until);
42}
43
44fn yes_waits(entry: &str, uid: &str) -> bool {
45    YESES.lock().get(&(entry.to_string(), uid.to_string())).is_some_and(|lapses| *lapses > Instant::now())
46}
47
48/// The sign-in the yes was for has been given to the browser: the next one asks.
49fn yes_used(entry: &str, uid: &str) {
50    YESES.lock().remove(&(entry.to_string(), uid.to_string()));
51}
52
53/// The share of `entry` with `who` (an agent's uid, or `*`) was removed: a yes still waiting to be used goes with it.
54pub(crate) fn share_removed(entry: &str, who: &str) {
55    YESES.lock().retain(|(e, uid), _| e != entry || (who != shares::EVERY_AGENT && uid != who));
56}
57
58/// The agent whose own browser profile `profile` is: how its Vaults name it, and the name the person reads.
59/// `None` for Personal, a named profile, and a profile whose agent is gone.
60pub(crate) fn owner_of(state: &AppState, profile: &str) -> Option<(ProfileOwner, String)> {
61    let id = profile.strip_prefix("profile:").filter(|id| id.starts_with("agent-"))?;
62    let defs = state.mstore.agent_def_list().ok()?;
63    let def = defs.into_iter().find(|d| agent_profile_id(&d.id) == id)?;
64    Some((ProfileOwner { names: vec![def.slug.clone(), def.id.clone()], uid: def.id }, def.name))
65}
66
67/// What the host is told of a shared sign-in that answers a prompt: never the password, and the user name masked.
68pub(crate) struct Found {
69    pub username: String,
70    /// The agent the login is shared with, as the person reads it.
71    pub agent: String,
72    /// The person already said yes to this use: the browser answers without asking.
73    pub granted: bool,
74}
75
76/// The shared sign-in that answers `space`, when its profile is an agent's own.
77pub(crate) fn lookup(state: &AppState, space: &Space) -> Result<Option<Found>, String> {
78    let Some((owner, agent)) = owner_of(state, &space.profile) else { return Ok(None) };
79    let store = &state.identity_store;
80    let Some((row, username)) = signins::find_shared(store, space, &owner)? else { return Ok(None) };
81    let granted = shares::prompt_always(store, &row, &owner.uid, &space.profile) || yes_waits(&row.id, &owner.uid);
82    Ok(Some(Found { username: signins::mask(&username), agent, granted }))
83}
84
85/// The user name and password of the shared sign-in that answers `space`, for the browser to give the site. The use
86/// is recorded as the agent's, and a yes from the share card is used up by it.
87pub(crate) fn fill(state: &AppState, space: &Space) -> Result<Option<(String, zeroize::Zeroizing<String>)>, String> {
88    let Some((owner, _)) = owner_of(state, &space.profile) else { return Ok(None) };
89    let actor = owner.names.first().cloned().unwrap_or_default();
90    let Some((row, username, password)) = signins::fill_shared(&state.identity_store, space, &owner, &actor)? else { return Ok(None) };
91    yes_used(&row.id, &owner.uid);
92    Ok(Some((username, password)))
93}
94
95/// The site refused the shared sign-in given for `space`: it is marked, as the profile's own would be.
96pub(crate) fn refused(state: &AppState, space: &Space) -> Result<(), String> {
97    let Some((owner, _)) = owner_of(state, &space.profile) else { return Ok(()) };
98    signins::refused_shared(&state.identity_store, space, &owner)
99}
100
101#[cfg(test)]
102mod tests {
103    use super::*;
104
105    #[test]
106    fn a_yes_on_the_card_covers_one_use_and_lapses() {
107        yes_given("login-a", "uid-1");
108        assert!(yes_waits("login-a", "uid-1"));
109        assert!(!yes_waits("login-a", "uid-2"), "another agent's prompt is still asked about");
110        assert!(!yes_waits("login-b", "uid-1"), "another login's prompt is still asked about");
111        yes_used("login-a", "uid-1");
112        assert!(!yes_waits("login-a", "uid-1"), "the second prompt asks again");
113
114        yes_given_until("login-c", "uid-1", Instant::now() - Duration::from_secs(1));
115        assert!(!yes_waits("login-c", "uid-1"), "a yes nobody used in time is gone");
116    }
117
118    #[test]
119    fn removing_the_share_ends_a_yes_that_still_waits() {
120        yes_given("login-d", "uid-1");
121        yes_given("login-d", "uid-2");
122        yes_given("login-e", "uid-1");
123        share_removed("login-d", "uid-1");
124        assert!(!yes_waits("login-d", "uid-1"));
125        assert!(yes_waits("login-d", "uid-2") && yes_waits("login-e", "uid-1"), "only the removed share's yes ends");
126        share_removed("login-d", shares::EVERY_AGENT);
127        assert!(!yes_waits("login-d", "uid-2"), "removing every agent's share ends them all for the login");
128        assert!(yes_waits("login-e", "uid-1"));
129    }
130}