Skip to main content

agentmux_srv/server/
identity_auth_persist.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! `IdentityAccount` persistence on a successful OAuth handshake.
5//!
6//! Split out of `identity_handlers.rs` (module-organization pass, see
7//! `docs/specs/PLAN_LOGIN_SINGLE_PATH_CONSOLIDATION_2026_07_20.md`) —
8//! called from `identity_auth_spawn`'s drain/post-exit success paths
9//! (`spawn_auth_cli` + `spawn_auth_cli_pty`, two call sites each).
10
11use std::sync::Arc;
12
13use crate::backend::storage::store::{IdentityAccount, SecretRef, Store};
14use crate::backend::mps::Broker;
15
16/// Upserts the `IdentityAccount` (`SecretRef::OAuthConfigDir`, status
17/// "valid") on a successful OAuth handshake (CLI exited 0 +
18/// authCheckCommand confirmed). The actual `agent_identity_link` write
19/// happens later, once the agent exists (the launch-flow write-through
20/// reconcile) — this function only makes sure the account itself exists
21/// and is ready to be linked.
22///
23/// Publishes `identityaccounts:changed` (the same broad event
24/// `account.key.verify`/`upsertidentityaccount` already use) rather
25/// than a bundle-scoped event, since there's no bundle id to scope to.
26///
27/// Returns `None` on any persistence failure (dir never resolved, or
28/// the account upsert itself failed) — same "log + skip, session still
29/// succeeds" contract as the bundle path, just without a synthetic
30/// placeholder to fall back to (direct-account mode has no "ambient"
31/// concept to fall back to; the caller surfaces `account_id: None` on
32/// the wire and the frontend treats that as "nothing to select").
33fn persist_oauth_direct_account(
34    mstore: &Arc<Store>,
35    identity_store: &Arc<Store>,
36    broker: &Arc<Broker>,
37    account_id: &str,
38    provider_id: &str,
39    dir: Option<&str>,
40    _session_id: &str,
41    email: Option<&str>,
42) -> Option<String> {
43    let dir = match dir.filter(|s| !s.is_empty()) {
44        Some(d) => d,
45        None => {
46            tracing::warn!(
47                target: "identity",
48                account_id,
49                provider_id,
50                "auth success (direct-account): dir unresolved — skipping account persist"
51            );
52            return None;
53        }
54    };
55    let now = agentmux_common::time::now_ms();
56    let account = IdentityAccount {
57        id: account_id.to_string(),
58        name: format!("{provider_id}-oauth"),
59        provider: provider_id.to_string(),
60        kind: "oauth".to_string(),
61        // `display_name` is deliberately left for the USER to set — see
62        // SPEC_ACCOUNT_EMAIL_IN_ARMORY_2026_09_23.md §2. The login email is
63        // provider metadata and belongs in `context`, so naming an account
64        // "work" later cannot clobber it and vice versa.
65        display_name: String::new(),
66        secret_ref: SecretRef::OAuthConfigDir { dir: dir.to_string() },
67        // The key is OMITTED, not set empty, when the provider reported no
68        // email — so "this provider does not surface one" stays
69        // distinguishable from "logged in as a blank address" downstream.
70        // Falls back to the email the CLI recorded in the account's own
71        // config dir (Claude's login transcript prints none) — §4.
72        context: match email
73            .map(str::trim)
74            .filter(|e| !e.is_empty())
75            .map(str::to_string)
76            .or_else(|| crate::identity::account_email::email_from_oauth_dir(provider_id, dir))
77        {
78            Some(e) => serde_json::json!({ "email": e }),
79            None => serde_json::json!({}),
80        },
81        // Same rationale as the bundle path: a binding the user JUST
82        // OAuth'd into is `valid` by definition.
83        status: crate::identity::resolver::oauth_status::VALID.to_string(),
84        created_at: now,
85        updated_at: now,
86    };
87    // identity_upsert_with_mirror — reagentx P0 review on PR #2632: this
88    // is THE primary OAuth account-creation path (auth.start), so without
89    // the mirror write every newly-OAuth'd account had no fallback entry
90    // and reproduced the reported bug on its own next channel switch.
91    if let Err(e) = mstore.identity_upsert_with_mirror(identity_store, &account) {
92        tracing::warn!(
93            target: "identity",
94            account_id,
95            provider_id,
96            error = %e,
97            "auth success (direct-account): identity_upsert failed"
98        );
99        return None;
100    }
101    broker.publish_event::<crate::backend::mps_events::IdentityAccountsChanged>(&(), vec![], 0);
102    tracing::info!(
103        target: "identity",
104        account_id,
105        provider_id,
106        dir,
107        "auth success (direct-account): OAuth account persisted"
108    );
109    Some(account_id.to_string())
110}
111
112/// Shared by all 4 OAuth-success call sites (pipes drain/post-exit, PTY
113/// drain/post-exit) — persists the account and builds the
114/// `(bundle_id, account_id)` pair `AuthSessionManager::finish_success`
115/// expects. `bundle_id` is always empty now: bundle mode (`db_identity_bundles`
116/// binding) was retired in Phase 4c of SPEC_PRESET_TO_BUNDLE_REFACTOR_2026_07_02.md
117/// — no frontend caller sends `intoBundleId`. `_direct_account`/`_into_bundle_id` stay as
118/// parameters so the wire request shape and the 4 call sites don't need
119/// touching. `dir` is the account's own isolation dir, resolved once at
120/// spawn time by `compute_and_ensure_*_dir` in the `auth.start` handler.
121///
122/// Guards on `account_id` being non-empty before persisting: `auth.start`
123/// only populates a real account_id when `direct_account` is true (via
124/// `compute_and_ensure_account_dir`, which always mints/reuses a real id);
125/// when `direct_account` is false (the wire default, and what the agent
126/// pane's login sends: it persists the account itself), `account_id`
127/// is `""`. Without this guard an empty id would flow into
128/// `persist_oauth_direct_account`'s `identity_upsert`, whose
129/// `ON CONFLICT(id) DO UPDATE` would silently overwrite/corrupt any prior
130/// row that happened to have `id=""`. Reagent P1.
131#[allow(clippy::too_many_arguments)]
132pub(crate) fn persist_oauth_success(
133    mstore: &Arc<Store>,
134    identity_store: &Arc<Store>,
135    broker: &Arc<Broker>,
136    _direct_account: bool,
137    account_id: &str,
138    _into_bundle_id: Option<&str>,
139    provider_id: &str,
140    dir: Option<&str>,
141    session_id: &str,
142    email: Option<&str>,
143) -> (String, Option<String>) {
144    if account_id.is_empty() {
145        return (String::new(), None);
146    }
147    let persisted = persist_oauth_direct_account(mstore, identity_store, broker, account_id, provider_id, dir, session_id, email);
148    // A completed sign-in proves the account works, so its agents stop being
149    // signed out (#2237).
150    if let Some(id) = &persisted {
151        crate::server::account_episodes::after_sign_in(id);
152    }
153    (String::new(), persisted)
154}
155
156#[cfg(test)]
157mod tests {
158    use super::*;
159
160    #[test]
161    fn persist_oauth_direct_account_round_trip() {
162        let mstore = Arc::new(Store::open_in_memory().unwrap());
163        let identity_store = Arc::new(Store::open_in_memory().unwrap());
164        let broker = Arc::new(crate::backend::mps::Broker::new());
165        let r = persist_oauth_direct_account(
166            &mstore,
167            &identity_store,
168            &broker,
169            "acc-1",
170            "claude",
171            Some("/some/account/dir"),
172            "sess-z",
173            None,
174        );
175        assert_eq!(r, Some("acc-1".to_string()));
176
177        let acct = mstore.identity_get("acc-1").unwrap().expect("account row exists");
178        assert_eq!(acct.provider, "claude");
179        assert_eq!(acct.kind, "oauth");
180        assert_eq!(acct.status, "valid");
181        match acct.secret_ref {
182            SecretRef::OAuthConfigDir { dir } => assert_eq!(dir, "/some/account/dir"),
183            other => panic!("expected OAuthConfigDir, got {other:?}"),
184        }
185    }
186
187    #[test]
188    fn persist_oauth_direct_account_returns_none_when_dir_unresolved() {
189        let mstore = Arc::new(Store::open_in_memory().unwrap());
190        let identity_store = Arc::new(Store::open_in_memory().unwrap());
191        let broker = Arc::new(crate::backend::mps::Broker::new());
192        let r = persist_oauth_direct_account(&mstore, &identity_store, &broker, "acc-1", "claude", None, "sess-z", None);
193        assert!(r.is_none());
194        assert!(mstore.identity_get("acc-1").unwrap().is_none(), "nothing persisted when dir is unresolved");
195    }
196
197    // SPEC_ACCOUNT_EMAIL_IN_ARMORY_2026_09_23.md. The login email is provider
198    // metadata and goes in `context`, NOT `display_name` — that field belongs
199    // to the user, so writing an email there would clobber any label they set
200    // (and vice versa).
201    #[test]
202    fn persist_records_the_login_email_in_context_leaving_display_name_free() {
203        let mstore = Arc::new(Store::open_in_memory().unwrap());
204        let identity_store = Arc::new(Store::open_in_memory().unwrap());
205        let broker = Arc::new(crate::backend::mps::Broker::new());
206        persist_oauth_direct_account(
207            &mstore,
208            &identity_store,
209            &broker,
210            "acc-1",
211            "claude",
212            Some("/dir"),
213            "sess-z",
214            Some("user@example.com"),
215        );
216        let acct = mstore.identity_get("acc-1").unwrap().expect("account row");
217        assert_eq!(acct.context["email"], "user@example.com");
218        assert_eq!(acct.display_name, "", "display_name stays the user's to set");
219    }
220
221    // Absent, not empty. A provider whose CLI reports no email must stay
222    // distinguishable from one that reported a blank address — otherwise the
223    // Connectors cannot tell "nothing to show" from "logged in as ''".
224    #[test]
225    fn persist_omits_the_email_key_entirely_when_none_was_captured() {
226        let mstore = Arc::new(Store::open_in_memory().unwrap());
227        let identity_store = Arc::new(Store::open_in_memory().unwrap());
228        let broker = Arc::new(crate::backend::mps::Broker::new());
229        for email in [None, Some(""), Some("   ")] {
230            let id = format!("acc-{}", email.unwrap_or("none").len());
231            persist_oauth_direct_account(
232                &mstore, &identity_store, &broker, &id, "claude", Some("/dir"), "sess-z", email,
233            );
234            let acct = mstore.identity_get(&id).unwrap().expect("account row");
235            assert!(
236                acct.context.get("email").is_none(),
237                "blank email {email:?} must not create the key"
238            );
239        }
240    }
241
242    #[test]
243    fn persist_oauth_success_always_routes_direct_account_mode() {
244        // Bundle mode was retired in Phase 4c of
245        // SPEC_PRESET_TO_BUNDLE_REFACTOR_2026_07_02.md — persist_oauth_success
246        // always persists a direct account now, regardless of the
247        // (now-vestigial) direct_account/into_bundle_id parameters.
248        let mstore = Arc::new(Store::open_in_memory().unwrap());
249        let identity_store = Arc::new(Store::open_in_memory().unwrap());
250        let broker = Arc::new(crate::backend::mps::Broker::new());
251        let (bundle_id, account_id) = persist_oauth_success(
252            &mstore,
253            &identity_store,
254            &broker,
255            true,
256            "acc-1",
257            None,
258            "claude",
259            Some("/some/dir"),
260            "sess-route",
261            None,
262        );
263        assert_eq!(bundle_id, "", "bundle id is always empty now");
264        assert_eq!(account_id, Some("acc-1".to_string()));
265        assert!(mstore.identity_get("acc-1").unwrap().is_some());
266    }
267
268    #[test]
269    fn persist_oauth_success_skips_persistence_when_account_id_is_empty() {
270        // Reagent P1: `auth.start` sets account_id = "" whenever
271        // `direct_account` is false (the wire default, and what the agent
272        // pane's login sends) — so this path is live. Without the
273        // empty-id guard, persist_oauth_direct_account's identity_upsert
274        // would silently write/overwrite a db_accounts row with id="".
275        let mstore = Arc::new(Store::open_in_memory().unwrap());
276        let identity_store = Arc::new(Store::open_in_memory().unwrap());
277        let broker = Arc::new(crate::backend::mps::Broker::new());
278        let (bundle_id, account_id) = persist_oauth_success(
279            &mstore,
280            &identity_store,
281            &broker,
282            false,
283            "",
284            None,
285            "claude",
286            Some("/some/dir"),
287            "sess-empty",
288            None,
289        );
290        assert_eq!(bundle_id, "");
291        assert_eq!(account_id, None, "empty account_id must not be persisted");
292        assert!(
293            mstore.identity_get("").unwrap().is_none(),
294            "no row with id=\"\" should ever be written"
295        );
296    }
297}