Files
codex/codex-rs/app-server-transport/src/connection_auth.rs
T
Anton Panasenko 1bff94edb6 Bind remote-control sessions to their authentication owner (#44341)
## Why

Remote-control connections and queued operations must not carry over to a different signed-in user or account. Token refreshes for the same identity should preserve the live relay connection.

## What changed

- Scope relay state and authentication recovery to a login lifetime. Retire the session on logout or identity changes and leave remote control disabled until enabled again.
- Reject stale incoming messages and queued RPCs, and cancel pending remote-control operations when their authentication owner changes.
- Start replacement sessions with fresh client, replay, and enrollment state, and prevent retired sessions from publishing status into their replacements.
- Serialize enrollment and preference persistence across sessions, retaining write permits through caller cancellation and draining admitted writes on shutdown.

## Testing

Add regression coverage for same-owner refreshes, user and account changes, stale pairing and client-revocation work, unauthorized enrollment recovery after logging in again, and persistence during cancellation.

GitOrigin-RevId: 5e6ecd23588aab1e2e6168a21f575fb24bbb341c
2026-09-09 23:59:14 +00:00

52 lines
1.5 KiB
Rust

//! Binds a transport connection to the authentication owner that established it.
//! Owner revisions invalidate queued work even before transport closure is delivered.
use codex_login::AuthChangeState;
use codex_login::AuthManager;
use std::io;
use tokio::sync::watch;
#[derive(Clone, Debug)]
pub struct ConnectionAuth {
changes: watch::Receiver<AuthChangeState>,
owner_generation: u64,
}
impl ConnectionAuth {
pub(crate) fn capture(auth_manager: &AuthManager) -> Self {
let changes = auth_manager.auth_change_state_receiver();
let owner_generation = changes.borrow().owner_generation;
Self::new(changes, owner_generation)
}
pub(crate) fn ensure_current(&self) -> io::Result<()> {
if self.is_current() {
Ok(())
} else {
Err(io::Error::new(
io::ErrorKind::Interrupted,
"remote control authentication changed",
))
}
}
pub(crate) fn new(changes: watch::Receiver<AuthChangeState>, owner_generation: u64) -> Self {
Self {
changes,
owner_generation,
}
}
pub fn is_current(&self) -> bool {
self.changes.borrow().owner_generation == self.owner_generation
&& self.changes.has_changed().is_ok()
}
pub(crate) async fn invalidated(&self) {
let mut changes = self.changes.clone();
let _ = changes
.wait_for(|state| state.owner_generation != self.owner_generation)
.await;
}
}