Support credential export commands for gateway authentication (#59787)

## What changed

Allow `model_providers.<id>.gateway_oauth.credential_export` to supply a gateway access token instead of browser OAuth. Run the configured command on first use, cache its token in memory, and deliver it through the configured header or cookie. Export failures surface directly, and command-backed providers do not require an OAuth session.

Require mutually exclusive OAuth and exporter settings, same-origin model catalog access, and rejection of HTTP redirects for command-backed gateway credentials. Share the bounded, timeout-controlled command runner with AWS credential export.

## Testing

Add tests for inference and model listing without OAuth, token caching and recovery after invalid output, preservation of primary authentication, and rejection of mixed OAuth/exporter configuration.

GitOrigin-RevId: e3dbfd8823b1992045f1c1efd56ec98ee17e07a5
This commit is contained in:
alexsong-oai
2026-11-06 02:00:39 +00:00
committed by copyberry
parent 5e5bdee745
commit b1263c593d
27 changed files with 753 additions and 297 deletions
@@ -46,7 +46,7 @@ pub(crate) fn spawn(
.gateway_auth_manager() else {
continue;
};
if manager.config() != &change.config {
if !matches!(manager.oauth_session(), Some(oauth) if oauth.config() == &change.config) {
continue;
}
let (status, error) = status_fields(change.status);
@@ -117,7 +117,7 @@ pub(crate) struct AccountRequestProcessor {
workspace_routing_fetches: Arc<Mutex<workspace_routing::WorkspaceRoutingFetches>>,
workspace_routing_shutdown: CancellationToken,
gateway_login: Arc<std::sync::Mutex<Option<gateway_oauth::ActiveGatewayLogin>>>,
gateway_client: Arc<std::sync::Mutex<Option<Arc<codex_login::GatewayAuthManager>>>>,
gateway_client: Arc<std::sync::Mutex<Option<Arc<codex_model_provider::GatewayAuthManager>>>>,
_gateway_notifications: Arc<tokio_util::task::AbortOnDropHandle<()>>,
pub(super) enterprise_login: Arc<enterprise_login::EnterpriseLoginState>,
}
@@ -7,7 +7,8 @@ use crate::transport::ConnectionId;
use codex_app_server_protocol::GatewayOAuthCancelResponse;
use codex_app_server_protocol::GatewayOAuthLoginResponse;
use codex_app_server_protocol::GatewayOAuthReadResponse;
use codex_login::GatewayAuthManager;
use codex_login::OAuthSessionManager;
use codex_model_provider::GatewayAuthManager;
use std::sync::PoisonError;
pub(super) struct ActiveGatewayLogin {
@@ -29,7 +30,7 @@ impl Drop for LoginGuard {
impl AccountRequestProcessor {
async fn gateway_client(
&self,
) -> Result<(Config, Option<Arc<GatewayAuthManager>>), JSONRPCErrorError> {
) -> Result<(Config, Option<Arc<OAuthSessionManager>>), JSONRPCErrorError> {
let config = self
.config_manager
.load_latest_config(/*fallback_cwd*/ None)
@@ -43,11 +44,14 @@ impl AccountRequestProcessor {
)
.gateway_auth_manager()
.map_err(|error| internal_error(error.to_string()))?;
let session = manager
.as_deref()
.and_then(GatewayAuthManager::oauth_session);
*self
.gateway_client
.lock()
.unwrap_or_else(PoisonError::into_inner) = manager.clone();
Ok((config, manager))
.unwrap_or_else(PoisonError::into_inner) = manager;
Ok((config, session))
}
pub(crate) async fn gateway_oauth_read(
@@ -216,7 +216,7 @@ impl CatalogRequestProcessor {
.check_thread_model_provider(&config)
.await
.map_err(|err| config_load_error(&err))?;
if gateway.resolve_access_token().await.is_err() {
if let Err(error) = gateway.resolve_access_token().await {
let current = self
.config_manager
.load_latest_config(/*fallback_cwd*/ None)
@@ -232,9 +232,7 @@ impl CatalogRequestProcessor {
"Model provider settings changed. Restart Codex to apply them, then retry fetching the model list",
));
}
return Err(invalid_request(
"Gateway sign-in required or unavailable. Complete gateway sign-in and retry fetching the model list",
));
return Err(invalid_request(error.to_string()));
}
}
self.list_models(params, config, models_manager)