Files
codex/codex-rs/app-server/src/gateway_oauth_notifications.rs
T
alexsong-oai 064e701b0f Add explicit gateway OAuth sign-in to app-server (#47207)
## Why

Clients with a gateway sign-in UI need to control when browser authorization starts and inspect credential readiness before making authenticated requests.

## What changed

- Add `initialize.capabilities.explicitGatewayOauth` to require explicit login while allowing existing credentials to refresh. Preserve automatic authorization for clients that omit the capability, and prevent later connections from undoing explicit opt-in.
- Add `account/gatewayOAuth/read`, `account/gatewayOAuth/login`, and `account/gatewayOAuth/cancel`, plus `account/gatewayOAuth/changed` notifications. Send the authorization URL only to the initiating connection and cancel its login on disconnect.
- Check gateway authentication before returning cached results from `model/list`, with restart guidance when provider settings have changed.
- Update protocol schemas, TypeScript and Python bindings, and document capability probing and the sign-in lifecycle.

## Testing

Add tests for passive readiness reads, credential reuse after login, token exchange failures, cancellation and immediate retry, connection ownership, legacy versus explicit login, and model-list authentication checks.

GitOrigin-RevId: a26e116a62c9f3db84dc238a40b7f4e816eb1594
2026-09-22 06:36:09 +00:00

66 lines
2.4 KiB
Rust

//! Forward OAuth progress for the app's current gateway.
use crate::config_manager::ConfigManager;
use crate::outgoing_message::OutgoingMessageSender;
use codex_app_server_protocol::GatewayOAuthChangedNotification;
use codex_app_server_protocol::GatewayOAuthStatus;
use codex_app_server_protocol::ServerNotification;
use codex_login::AuthManager;
use codex_login::GatewayAuthStatus;
use std::sync::Arc;
use tokio::sync::broadcast::error::RecvError;
use tokio_util::task::AbortOnDropHandle;
pub(crate) fn status_fields(status: GatewayAuthStatus) -> (GatewayOAuthStatus, Option<String>) {
match status {
GatewayAuthStatus::NotReady => (GatewayOAuthStatus::NotReady, None),
GatewayAuthStatus::Started => (GatewayOAuthStatus::Started, None),
GatewayAuthStatus::Succeeded => (GatewayOAuthStatus::Succeeded, None),
GatewayAuthStatus::Failed { message } => (GatewayOAuthStatus::Failed, Some(message)),
}
}
pub(crate) fn spawn(
auth_manager: Arc<AuthManager>,
config_manager: ConfigManager,
outgoing: Arc<OutgoingMessageSender>,
) -> AbortOnDropHandle<()> {
let mut events = codex_login::subscribe_gateway_auth_status(&auth_manager.runtime_config());
AbortOnDropHandle::new(tokio::spawn(async move {
loop {
let change = match events.recv().await {
Ok(change) => change,
Err(RecvError::Lagged(_)) => continue,
Err(RecvError::Closed) => break,
};
let Ok(config) = config_manager
.load_latest_config(/*fallback_cwd*/ None)
.await
else {
continue;
};
let Ok(Some(manager)) = codex_model_provider::create_model_provider(
config.model_provider.clone(),
Some(Arc::clone(&auth_manager)),
)
.gateway_auth_manager() else {
continue;
};
if manager.config() != &change.config {
continue;
}
let (status, error) = status_fields(change.status);
outgoing
.send_server_notification(ServerNotification::GatewayOAuthChanged(
GatewayOAuthChangedNotification {
auth_url: None,
provider_id: config.model_provider_id,
status,
error,
},
))
.await;
}
}))
}