Show workload identity account details in login status (#65317)

## Why

`codex login status` confirms that workload identity is active, but its
generic status line leaves users unable to tell which identity Codex is using.

## What changed

- For ChatGPT, show the principal ID, workspace ID, and account email when
  available.
- For API access, show the service account ID and project ID.

Read these details from cached authentication metadata and keep the generic
status line when account details are unavailable.

## Testing

Add output snapshots for ChatGPT identities with and without an email and
for API service accounts.

GitOrigin-RevId: a0fb2aafff4a52a2e890e324e939b132c59f5831
This commit is contained in:
Alvin
2026-12-02 19:25:53 +00:00
committed by copyberry
parent f85a6e05eb
commit 0f5e7be1a7
4 changed files with 121 additions and 2 deletions
+36 -2
View File
@@ -18,6 +18,7 @@ use codex_login::AuthRouteConfig;
use codex_login::CLIENT_ID;
use codex_login::OAuthDeviceCodePrompt;
use codex_login::ServerOptions;
use codex_login::WorkloadIdentityAccount;
use codex_login::is_workload_identity_selected;
use codex_login::login_with_access_token;
use codex_login::login_with_api_key;
@@ -577,8 +578,11 @@ pub async fn run_login_status(cli_config_overrides: CliConfigOverrides) -> ! {
if is_workload_identity_selected() {
match AuthManager::shared_from_config(&config, /*enable_codex_api_key_env*/ false).await {
Ok(_) => {
eprintln!("Logged in using workload identity");
Ok(manager) => {
eprintln!(
"{}",
workload_identity_status(manager.workload_identity_account())
);
std::process::exit(0);
}
Err(err) => {
@@ -640,6 +644,32 @@ pub async fn run_login_status(cli_config_overrides: CliConfigOverrides) -> ! {
}
}
fn workload_identity_status(account: Option<WorkloadIdentityAccount>) -> String {
let mut lines = vec!["Logged in using workload identity".to_string()];
match account {
Some(WorkloadIdentityAccount::Chatgpt {
principal_id,
workspace_id,
email,
}) => {
lines.push(format!("ChatGPT principal ID: {principal_id}"));
lines.push(format!("Workspace ID: {workspace_id}"));
if let Some(email) = email {
lines.push(format!("Account email: {email}"));
}
}
Some(WorkloadIdentityAccount::Api {
service_account_id,
project_id,
}) => {
lines.push(format!("API service account ID: {service_account_id}"));
lines.push(format!("API project ID: {project_id}"));
}
None => {}
}
lines.join("\n")
}
async fn logout_config_server(
config: &Config,
loader_overrides: &codex_config::LoaderOverrides,
@@ -812,6 +842,10 @@ fn safe_format_key(key: &str) -> String {
format!("{prefix}***{suffix}")
}
#[cfg(test)]
#[path = "login_status_tests.rs"]
mod status_tests;
#[cfg(test)]
#[path = "login_storage_tests.rs"]
mod storage_tests;
+50
View File
@@ -0,0 +1,50 @@
#![cfg(test)]
use codex_login::WorkloadIdentityAccount;
use super::workload_identity_status;
#[test]
fn workload_identity_status_shows_chatgpt_principal_workspace_and_email() {
let account = WorkloadIdentityAccount::Chatgpt {
principal_id: "user-one".to_string(),
workspace_id: "workspace-one".to_string(),
email: Some("service@example.com".to_string()),
};
insta::assert_snapshot!(workload_identity_status(Some(account)), @r"
Logged in using workload identity
ChatGPT principal ID: user-one
Workspace ID: workspace-one
Account email: service@example.com
");
}
#[test]
fn workload_identity_status_shows_chatgpt_identity_without_email() {
let account = WorkloadIdentityAccount::Chatgpt {
principal_id: "user-one".to_string(),
workspace_id: "workspace-one".to_string(),
email: None,
};
insta::assert_snapshot!(workload_identity_status(Some(account)), @r"
Logged in using workload identity
ChatGPT principal ID: user-one
Workspace ID: workspace-one
");
}
#[test]
fn workload_identity_status_shows_api_service_account_and_project() {
let account = WorkloadIdentityAccount::Api {
service_account_id: "service-account-one".to_string(),
project_id: "project-one".to_string(),
};
insta::assert_snapshot!(workload_identity_status(Some(account)), @r"
Logged in using workload identity
API service account ID: service-account-one
API project ID: project-one
");
}
+34
View File
@@ -212,6 +212,19 @@ impl Debug for ApiKeyAuth {
}
}
/// Account metadata from the selected workload identity exchange.
pub enum WorkloadIdentityAccount {
Chatgpt {
principal_id: String,
workspace_id: String,
email: Option<String>,
},
Api {
service_account_id: String,
project_id: String,
},
}
#[derive(Debug, Clone)]
pub struct ChatgptAuth {
state: ChatgptAuthState,
@@ -3345,6 +3358,27 @@ impl AuthManager {
self.workload_identity_selected
}
/// Returns cached workload identity account details without refreshing credentials.
pub fn workload_identity_account(&self) -> Option<WorkloadIdentityAccount> {
if !self.is_workload_identity_selected() {
return None;
}
let auth = self.auth_cached()?;
if let Some(metadata) = auth.api_workload_identity() {
return Some(WorkloadIdentityAccount::Api {
service_account_id: metadata.user_id.clone(),
project_id: metadata.api_project_id.clone(),
});
}
Some(WorkloadIdentityAccount::Chatgpt {
principal_id: auth.get_chatgpt_user_id()?,
workspace_id: auth.get_account_id()?,
email: auth.get_account_email(),
})
}
pub fn is_external_chatgpt_auth_active(&self) -> bool {
self.auth_cached()
.as_ref()
+1
View File
@@ -63,6 +63,7 @@ pub use auth::REVOKE_TOKEN_URL_OVERRIDE_ENV_VAR;
pub use auth::RefreshTokenError;
pub use auth::RequestAuthChange;
pub use auth::UnauthorizedRecovery;
pub use auth::WorkloadIdentityAccount;
pub use auth::default_client;
pub use auth::enforce_login_restrictions;
pub use auth::is_workload_identity_selected;