agentmux_srv/server/
keychain_probe.rs1#![cfg_attr(not(target_os = "macos"), allow(dead_code))]
13
14pub(crate) const NO_DEFAULT_KEYCHAIN: &str = "no-default-keychain";
16
17pub(crate) fn keychain_problem(
21 exited_ok: bool,
22 stdout: &str,
23 exists: impl Fn(&std::path::Path) -> bool,
24) -> Option<&'static str> {
25 if !exited_ok {
26 return Some(NO_DEFAULT_KEYCHAIN);
27 }
28 let path = stdout.split('"').nth(1)?;
29 if path.is_empty() || exists(std::path::Path::new(path)) {
30 return None;
31 }
32 Some(NO_DEFAULT_KEYCHAIN)
33}
34
35#[cfg(target_os = "macos")]
37pub(crate) async fn no_keychain_result() -> Option<crate::backend::rpc_types::CheckCliAuthResult> {
38 let problem = credential_store_problem().await?;
39 tracing::info!(problem, "claude auth check: no usable keychain (macOS)");
40 Some(crate::backend::rpc_types::CheckCliAuthResult {
41 authenticated: false,
42 email: None,
43 auth_method: None,
44 raw_output: "no usable keychain".to_string(),
45 store_problem: Some(problem.to_string()),
46 })
47}
48
49#[cfg(target_os = "macos")]
51pub(crate) async fn credential_store_problem() -> Option<&'static str> {
52 let mut cmd = tokio::process::Command::new("/usr/bin/security");
53 crate::backend::pane_env::sanitize_external_command(&mut cmd);
54 cmd.args(["default-keychain", "-d", "user"])
55 .stdin(std::process::Stdio::null())
56 .kill_on_drop(true);
57 let output = tokio::time::timeout(std::time::Duration::from_secs(3), cmd.output())
58 .await
59 .ok()?
60 .ok()?;
61 keychain_problem(
62 output.status.success(),
63 &String::from_utf8_lossy(&output.stdout),
64 |p| p.exists(),
65 )
66}
67
68#[cfg(test)]
69#[path = "keychain_probe_tests.rs"]
70mod tests;