mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Notify opted-in stdio MCP servers of auth changes (#43428)
## What changed Advertise the experimental `codex/auth-change` capability for stdio MCP connections with an auth manager. When the server opts in, send `notifications/codex/authChanged` after initialization and on subsequent auth changes, with credential and owner generation counters and no credentials. Track owner changes separately from credential refreshes so consumers can detect login, logout, or user, workspace, and auth-mode changes even when notifications coalesce. Treat credential changes with incomplete owner identity as owner changes as well. Tie the notification watcher to the managed client's lifetime, limit each send to five seconds, and close the connection if a subsequent notification fails. ## Testing Add tests for credential refreshes versus owner changes, coalesced logout and account switches, capability opt-in, notification payloads, and watcher cleanup. GitOrigin-RevId: 35a99f5252bd48ca60a71fbdd1f708a8158e812c
This commit is contained in:
1 parent
0b263a3331
commit
0df39752cb
12 files changed
+345
-1
No files matched your search
@@ -36,6 +36,8 @@ use super::agent_identity::record_needs_task_registration;
|
||||
use super::agent_identity::register_managed_chatgpt_agent_identity;
|
||||
use super::agent_identity::require_agent_identity_authapi_base_url;
|
||||
use super::agent_identity::verified_record_from_jwt;
|
||||
use super::change_state::AuthChangeState;
|
||||
use super::change_state::same_owner;
|
||||
use super::external_bearer::BearerTokenRefresher;
|
||||
use super::revoke::revoke_auth_tokens;
|
||||
use super::workload_identity::WorkloadIdentityExternalAuth;
|
||||
@@ -2034,6 +2036,7 @@ pub struct AuthManager {
|
||||
codex_home: PathBuf,
|
||||
inner: RwLock<CachedAuth>,
|
||||
auth_change_tx: watch::Sender<u64>,
|
||||
auth_change_state_tx: watch::Sender<AuthChangeState>,
|
||||
enable_codex_api_key_env: bool,
|
||||
auth_credentials_store_mode: AuthCredentialsStoreMode,
|
||||
keyring_backend_kind: AuthKeyringBackendKind,
|
||||
@@ -2170,6 +2173,7 @@ impl AuthManager {
|
||||
permanent_refresh_failure: None,
|
||||
}),
|
||||
auth_change_tx,
|
||||
auth_change_state_tx: watch::channel(AuthChangeState::default()).0,
|
||||
enable_codex_api_key_env,
|
||||
auth_credentials_store_mode,
|
||||
keyring_backend_kind,
|
||||
@@ -2204,6 +2208,7 @@ impl AuthManager {
|
||||
codex_home: PathBuf::from("non-existent"),
|
||||
inner: RwLock::new(cached),
|
||||
auth_change_tx,
|
||||
auth_change_state_tx: watch::channel(AuthChangeState::default()).0,
|
||||
enable_codex_api_key_env: false,
|
||||
auth_credentials_store_mode: AuthCredentialsStoreMode::File,
|
||||
keyring_backend_kind: AuthKeyringBackendKind::default(),
|
||||
@@ -2232,6 +2237,7 @@ impl AuthManager {
|
||||
codex_home,
|
||||
inner: RwLock::new(cached),
|
||||
auth_change_tx,
|
||||
auth_change_state_tx: watch::channel(AuthChangeState::default()).0,
|
||||
enable_codex_api_key_env: false,
|
||||
auth_credentials_store_mode: AuthCredentialsStoreMode::File,
|
||||
keyring_backend_kind: AuthKeyringBackendKind::default(),
|
||||
@@ -2264,6 +2270,7 @@ impl AuthManager {
|
||||
codex_home: PathBuf::from("non-existent"),
|
||||
inner: RwLock::new(cached),
|
||||
auth_change_tx,
|
||||
auth_change_state_tx: watch::channel(AuthChangeState::default()).0,
|
||||
enable_codex_api_key_env: false,
|
||||
auth_credentials_store_mode: AuthCredentialsStoreMode::File,
|
||||
keyring_backend_kind: AuthKeyringBackendKind::default(),
|
||||
@@ -2294,6 +2301,7 @@ impl AuthManager {
|
||||
permanent_refresh_failure: None,
|
||||
}),
|
||||
auth_change_tx,
|
||||
auth_change_state_tx: watch::channel(AuthChangeState::default()).0,
|
||||
enable_codex_api_key_env: false,
|
||||
auth_credentials_store_mode: AuthCredentialsStoreMode::File,
|
||||
keyring_backend_kind: AuthKeyringBackendKind::default(),
|
||||
@@ -2328,6 +2336,11 @@ impl AuthManager {
|
||||
self.auth_change_tx.subscribe()
|
||||
}
|
||||
|
||||
/// Subscribes to credential and owner revisions published together, including when changes coalesce.
|
||||
pub fn auth_change_state_receiver(&self) -> watch::Receiver<AuthChangeState> {
|
||||
self.auth_change_state_tx.subscribe()
|
||||
}
|
||||
|
||||
pub fn refresh_failure_for_auth(&self, auth: &CodexAuth) -> Option<RefreshTokenFailedError> {
|
||||
self.inner.read().ok().and_then(|cached| {
|
||||
cached
|
||||
@@ -2575,12 +2588,20 @@ impl AuthManager {
|
||||
let changed = !AuthManager::auths_equal(previous, new_auth.as_ref());
|
||||
let auth_changed_for_refresh =
|
||||
!Self::auths_equal_for_refresh(previous, new_auth.as_ref());
|
||||
let owner_changed =
|
||||
auth_changed_for_refresh && !same_owner(previous, new_auth.as_ref());
|
||||
if auth_changed_for_refresh {
|
||||
guard.permanent_refresh_failure = None;
|
||||
}
|
||||
tracing::info!("Reloaded auth, changed: {changed}");
|
||||
guard.auth = new_auth;
|
||||
if auth_changed_for_refresh {
|
||||
self.auth_change_state_tx.send_modify(|state| {
|
||||
state.generation += 1;
|
||||
if owner_changed {
|
||||
state.owner_generation += 1;
|
||||
}
|
||||
});
|
||||
self.auth_change_tx.send_modify(|revision| *revision += 1);
|
||||
}
|
||||
changed
|
||||
@@ -3045,3 +3066,7 @@ fn auth_config_from(config: &impl AuthManagerConfig) -> AuthConfig {
|
||||
#[cfg(test)]
|
||||
#[path = "auth_tests.rs"]
|
||||
mod tests;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "change_state_tests.rs"]
|
||||
mod change_state_tests;
|
||||
Reference in new issue
Block a user