Clarify API key login and status messages (#55407)

## Why

Saving an API key does not validate it. Login and status messages should make clear that authentication is checked when a request is sent.

## What changed

Report `API key saved` after API key login and `API key configured` in `login status`. Both messages now explain when authentication is checked.

## Testing

Update the API key login integration test with snapshots for login and status output, while retaining assertions that the key is saved to `auth.json`.

GitOrigin-RevId: 2f36ebb270a48771be25060a596cf9731e8d0325
This commit is contained in:
Felipe Coury
2026-10-19 23:56:57 +00:00
committed by copyberry
parent c937a6eb7e
commit 1ccc0d64e8
2 changed files with 30 additions and 12 deletions
+4 -2
View File
@@ -46,6 +46,7 @@ const API_KEY_LOGIN_DISABLED_MESSAGE: &str =
const ACCESS_TOKEN_LOGIN_DISABLED_MESSAGE: &str =
"Access token login is disabled. Use API key login instead.";
const LOGIN_SUCCESS_MESSAGE: &str = "Successfully logged in";
const API_KEY_AUTHENTICATION_NOTICE: &str = "Authentication is checked when you send a request.";
const DEFAULT_LOGIN_LOG_FILTER: &str =
"codex_cli=info,codex_core=info,codex_login=info,codex_otel::auth_storage=warn";
@@ -228,7 +229,7 @@ pub async fn run_login_with_api_key(
config.auth_keyring_backend_kind(),
) {
Ok(_) => {
eprintln!("{LOGIN_SUCCESS_MESSAGE}");
eprintln!("API key saved. {API_KEY_AUTHENTICATION_NOTICE}");
std::process::exit(0);
}
Err(e) => {
@@ -468,7 +469,8 @@ pub async fn run_login_status(cli_config_overrides: CliConfigOverrides) -> ! {
Ok(Some(auth)) => match auth.auth_mode() {
AuthMode::ApiKey => match auth.get_token() {
Ok(api_key) => {
eprintln!("Logged in using an API key - {}", safe_format_key(&api_key));
eprintln!("API key configured - {}", safe_format_key(&api_key));
eprintln!("{API_KEY_AUTHENTICATION_NOTICE}");
std::process::exit(0);
}
Err(e) => {
+26 -10
View File
@@ -52,22 +52,38 @@ fn login_with_api_key_reads_stdin_and_writes_auth_json() -> Result<()> {
write_file_auth_config(codex_home.path())?;
let mut cmd = codex_command(codex_home.path())?;
cmd.args([
"-c",
"forced_login_method=\"api\"",
"login",
"--with-api-key",
])
.write_stdin("sk-test\n")
.assert()
.success()
.stderr(contains("Successfully logged in"));
let login = cmd
.args([
"-c",
"forced_login_method=\"api\"",
"login",
"--with-api-key",
])
.write_stdin("sk-test\n")
.assert()
.success();
insta::assert_snapshot!(String::from_utf8_lossy(&login.get_output().stderr), @r"
Reading API key from stdin...
API key saved. Authentication is checked when you send a request.
");
let auth = read_auth_json(codex_home.path())?;
assert_eq!(auth["OPENAI_API_KEY"], "sk-test");
assert!(auth.get("tokens").is_none());
assert!(auth.get("agent_identity").is_none());
let status = codex_command(codex_home.path())?
.env_remove(CODEX_ACCESS_TOKEN_ENV_VAR)
.env_remove(OPENAI_FEDERATION_RULE_ID_ENV_VAR)
.env_remove(OPENAI_IDENTITY_TOKEN_FILE_ENV_VAR)
.args(["login", "status"])
.assert()
.success();
insta::assert_snapshot!(String::from_utf8_lossy(&status.get_output().stderr), @r"
API key configured - ***
Authentication is checked when you send a request.
");
Ok(())
}