mirror of
https://github.com/openai/codex.git
synced 2026-11-17 22:10:37 +00:00
Support device-code login for config servers (#62079)
## Why `codex login --device-auth` still used browser authorization when config server sign-in was required, preventing the selected device-code flow from being used there. ## What changed - Use device authorization for config server sign-in when `--device-auth` is selected, display the verification URL and code, and report a missing `config_server.oauth.device_authorization_url` explicitly. - Include the configured OAuth `resource` in device authorization and token polling requests. - Reserve sign-in per credential across Codex processes, reuse saved grants, and release the reservation when sign-in completes or is canceled. - Report config server sign-in failures with guidance specific to `config_server.oauth` and the credential store. ## Testing Add or extend tests for the device-code prompt, config server failure messages, OAuth resource parameters, policy reload after credential replacement, and rejection of concurrent sign-in with retry after cancellation. GitOrigin-RevId: 013176d0e10128c347dd072b818e22b5038a2791
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
//! into a one-shot CLI command while still producing a durable `codex-login.log` artifact that
|
||||
//! support can request from users.
|
||||
|
||||
use codex_cloud_config::ConfigServerLoginMethod;
|
||||
use codex_config::types::AuthCredentialsStoreMode;
|
||||
use codex_core::config::Config;
|
||||
use codex_core::config::edit::ConfigEdit;
|
||||
@@ -15,6 +16,7 @@ use codex_login::AuthKeyringBackendKind;
|
||||
use codex_login::AuthManager;
|
||||
use codex_login::AuthRouteConfig;
|
||||
use codex_login::CLIENT_ID;
|
||||
use codex_login::OAuthDeviceCodePrompt;
|
||||
use codex_login::ServerOptions;
|
||||
use codex_login::is_workload_identity_selected;
|
||||
use codex_login::login_with_access_token;
|
||||
@@ -176,6 +178,7 @@ pub async fn login_with_chatgpt(
|
||||
async fn sign_in_to_config_server_and_reload_config(
|
||||
config: &mut Config,
|
||||
cli_config_overrides: &CliConfigOverrides,
|
||||
method: ConfigServerLoginMethod,
|
||||
) -> std::io::Result<()> {
|
||||
let requirements = config.config_layer_stack.requirements_toml();
|
||||
let network = codex_app_server::in_process::EmbeddedNetworkPolicy::load(
|
||||
@@ -184,7 +187,7 @@ async fn sign_in_to_config_server_and_reload_config(
|
||||
.await;
|
||||
let bootstrap_auth = network.bind_bootstrap_auth(config.auth_config());
|
||||
let auth_route_config = bootstrap_auth.auth_route_config.clone();
|
||||
codex_cloud_config::ensure_config_server_login(bootstrap_auth, requirements).await?;
|
||||
codex_cloud_config::ensure_config_server_login(bootstrap_auth, requirements, method).await?;
|
||||
*config = crate::cloud_config::load_config(cli_config_overrides, Default::default())
|
||||
.await
|
||||
.map_err(|error| std::io::Error::other(format!("{error:#}")))?;
|
||||
@@ -208,6 +211,7 @@ fn finish_login(result: std::io::Result<()>) -> ! {
|
||||
|
||||
async fn run_interactive_login<F>(
|
||||
cli_config_overrides: CliConfigOverrides,
|
||||
config_server_login_method: ConfigServerLoginMethod,
|
||||
chatgpt_login: impl FnOnce(Config) -> F,
|
||||
) -> !
|
||||
where
|
||||
@@ -223,14 +227,18 @@ where
|
||||
finish_login(chatgpt_login(config).await);
|
||||
}
|
||||
|
||||
let result =
|
||||
sign_in_to_config_server_and_reload_config(&mut config, &cli_config_overrides).await;
|
||||
let result = sign_in_to_config_server_and_reload_config(
|
||||
&mut config,
|
||||
&cli_config_overrides,
|
||||
config_server_login_method,
|
||||
)
|
||||
.await;
|
||||
let _login_log_guard = init_login_file_logging(&config);
|
||||
finish_login(result)
|
||||
}
|
||||
|
||||
pub async fn run_login_with_browser(cli_config_overrides: CliConfigOverrides) -> ! {
|
||||
run_interactive_login(cli_config_overrides, |config| async move {
|
||||
let chatgpt_login = |config: Config| async move {
|
||||
tracing::info!("starting browser login flow");
|
||||
|
||||
if !config
|
||||
@@ -250,7 +258,12 @@ pub async fn run_login_with_browser(cli_config_overrides: CliConfigOverrides) ->
|
||||
config.auth_route_config(),
|
||||
)
|
||||
.await
|
||||
})
|
||||
};
|
||||
run_interactive_login(
|
||||
cli_config_overrides,
|
||||
ConfigServerLoginMethod::Browser,
|
||||
chatgpt_login,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
@@ -374,7 +387,7 @@ pub async fn run_login_with_device_code(
|
||||
issuer_base_url: Option<String>,
|
||||
client_id: Option<String>,
|
||||
) -> ! {
|
||||
run_interactive_login(cli_config_overrides, |config| async move {
|
||||
let chatgpt_login = |config: Config| async move {
|
||||
tracing::info!("starting device code login flow");
|
||||
if !config
|
||||
.auth_config()
|
||||
@@ -404,10 +417,25 @@ pub async fn run_login_with_device_code(
|
||||
opts.issuer = iss;
|
||||
}
|
||||
run_device_code_login(opts).await
|
||||
})
|
||||
};
|
||||
run_interactive_login(
|
||||
cli_config_overrides,
|
||||
ConfigServerLoginMethod::DeviceCode {
|
||||
present: |prompt| eprintln!("{}", config_server_device_code_message(prompt)),
|
||||
},
|
||||
chatgpt_login,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
fn config_server_device_code_message(prompt: &OAuthDeviceCodePrompt) -> String {
|
||||
let OAuthDeviceCodePrompt {
|
||||
verification_uri,
|
||||
user_code,
|
||||
} = prompt;
|
||||
format!("To sign in to the config server, open {verification_uri} and enter code {user_code}.")
|
||||
}
|
||||
|
||||
/// Prefers device-code login (with `open_browser = false`) when headless environment is detected, but keeps
|
||||
/// `codex login` working in environments where device-code may be disabled/feature-gated.
|
||||
/// If `run_device_code_login` returns `ErrorKind::NotFound` ("device-code unsupported"), this
|
||||
@@ -662,8 +690,12 @@ async fn load_managed_login_config_or_exit(cli_config_overrides: CliConfigOverri
|
||||
.config_layer_stack
|
||||
.requirements_toml()
|
||||
.requires_remote_config()
|
||||
&& let Err(error) =
|
||||
sign_in_to_config_server_and_reload_config(&mut config, &cli_config_overrides).await
|
||||
&& let Err(error) = sign_in_to_config_server_and_reload_config(
|
||||
&mut config,
|
||||
&cli_config_overrides,
|
||||
ConfigServerLoginMethod::Browser,
|
||||
)
|
||||
.await
|
||||
{
|
||||
finish_login(Err(error));
|
||||
}
|
||||
@@ -711,14 +743,27 @@ mod storage_tests;
|
||||
mod tests {
|
||||
use codex_config::types::AuthCredentialsStoreMode;
|
||||
use codex_login::AuthKeyringBackendKind;
|
||||
use codex_login::OAuthDeviceCodePrompt;
|
||||
use codex_login::load_auth_dot_json;
|
||||
use codex_login::login_with_api_key;
|
||||
use pretty_assertions::assert_eq;
|
||||
use tempfile::tempdir;
|
||||
|
||||
use super::clear_existing_auth_before_login;
|
||||
use super::config_server_device_code_message;
|
||||
use super::safe_format_key;
|
||||
|
||||
#[test]
|
||||
fn config_server_device_code_prompt_shows_verification_url_and_code() {
|
||||
let prompt = OAuthDeviceCodePrompt {
|
||||
verification_uri: "https://login.example/device"
|
||||
.parse()
|
||||
.expect("valid verification URL"),
|
||||
user_code: "ABCD-EFGH".into(),
|
||||
};
|
||||
insta::assert_snapshot!(config_server_device_code_message(&prompt), @"To sign in to the config server, open https://login.example/device and enter code ABCD-EFGH.");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn clears_existing_auth_before_login() {
|
||||
let codex_home = tempdir().expect("create temporary Codex home");
|
||||
|
||||
Reference in New Issue
Block a user