Skip to main content

agentmux_srv/server/
keychain_probe.rs

1// Copyright 2025-2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Whether macOS has a login keychain the Claude CLI can store a sign-in in.
5//!
6//! The CLI keeps its credential in the user's default keychain. Under a HOME with no login keychain (the app
7//! started with HOME pointed at a scratch folder, or an ssh-only or service account) macOS opens its own "Keychain
8//! Not Found" dialog and the CLI waits on it, so a sign-in the person finished reads as "timed out". This asks the
9//! system with `security default-keychain`, which only reads: it never creates, resets or changes a keychain.
10
11// Only macOS asks; the check itself is plain logic that every platform tests.
12#![cfg_attr(not(target_os = "macos"), allow(dead_code))]
13
14/// The problem code the sign-in panel turns into words.
15pub(crate) const NO_DEFAULT_KEYCHAIN: &str = "no-default-keychain";
16
17/// The problem with the keychain `security default-keychain` reported, if it is one. A default keychain is
18/// printed as a quoted path; one that was reported but is not on disk is as unusable as none. An answer that
19/// cannot be read is no verdict: nothing is blocked on a guess.
20pub(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/// The auth-check answer for an account whose sign-in cannot be kept, or None when the keychain is usable.
36#[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/// The keychain problem for the user this process runs as, or None (usable, or no answer in time).
50#[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;