agentmux_srv/server/
vault_prompt_share.rs1use 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
25const YES_COVERS: Duration = Duration::from_secs(5 * 60);
27
28static YESES: LazyLock<Mutex<HashMap<(String, String), Instant>>> = LazyLock::new(|| Mutex::new(HashMap::new()));
30
31pub(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
48fn yes_used(entry: &str, uid: &str) {
50 YESES.lock().remove(&(entry.to_string(), uid.to_string()));
51}
52
53pub(crate) fn share_removed(entry: &str, who: &str) {
55 YESES.lock().retain(|(e, uid), _| e != entry || (who != shares::EVERY_AGENT && uid != who));
56}
57
58pub(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
67pub(crate) struct Found {
69 pub username: String,
70 pub agent: String,
72 pub granted: bool,
74}
75
76pub(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
85pub(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
95pub(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}