mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Enforce managed authentication requirements locally (#37132)
## Why Authentication restrictions must apply before stored or environment-provided credentials can be used, including during bootstrap before cloud requirements are fetched. ## What changed - Add local `requirements.toml` allowlists for login methods and ChatGPT workspaces. Ignore these fields in cloud-provided requirements. - Combine managed workspace allowlists with existing workspace restrictions by intersection, and fail closed when the resulting policy permits no usable login method. - Centralize policy checks in the authentication manager so CLI, TUI, app-server, external-auth, and credential-loading paths consistently reject disallowed authentication before token hydration or network requests. ## Testing - Cover policy composition, workspace intersection, invalid stored and external credentials, bootstrap enforcement, and login endpoint restrictions. GitOrigin-RevId: efef22b248f3c3333e9aa55423e539efa2d2dd48
This commit is contained in:
1 parent
4cb8676d3a
commit
2994f545a7
36 files changed
+1121
-328
No files matched your search
@@ -141,6 +141,33 @@ async fn login_with_access_token_writes_agent_identity_jwt() {
|
||||
server.verify().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn login_with_access_token_rejects_agent_identity_workspace_mismatch() {
|
||||
let dir = tempdir().unwrap();
|
||||
let record = agent_identity_record(WORKSPACE_ID_DISALLOWED);
|
||||
let agent_identity =
|
||||
signed_agent_identity_jwt(&record, json!(record.plan_type)).expect("signed agent identity");
|
||||
let server = MockServer::start().await;
|
||||
let chatgpt_base_url = format!("{}/backend-api", server.uri());
|
||||
let allowed_workspaces = [WORKSPACE_ID_ALLOWED.to_string()];
|
||||
|
||||
let err = super::login_with_access_token(
|
||||
dir.path(),
|
||||
&agent_identity,
|
||||
AuthCredentialsStoreMode::File,
|
||||
Some(&allowed_workspaces),
|
||||
Some(&chatgpt_base_url),
|
||||
AuthKeyringBackendKind::default(),
|
||||
&crate::test_support::transport_default_auth_route_config(),
|
||||
)
|
||||
.await
|
||||
.expect_err("agent identity workspace mismatch should fail");
|
||||
|
||||
assert_eq!(err.kind(), std::io::ErrorKind::PermissionDenied);
|
||||
assert!(!get_auth_file(dir.path()).exists());
|
||||
assert!(server.received_requests().await.unwrap().is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[serial(codex_auth_env)]
|
||||
async fn stored_agent_identity_jwt_keeps_auth_json_unchanged() -> anyhow::Result<()> {
|
||||
@@ -178,6 +205,7 @@ async fn stored_agent_identity_jwt_keeps_auth_json_unchanged() -> anyhow::Result
|
||||
codex_home.path(),
|
||||
/*enable_codex_api_key_env*/ false,
|
||||
AuthCredentialsStoreMode::File,
|
||||
/*allowed_login_methods*/ None,
|
||||
/*forced_chatgpt_workspace_id*/ None,
|
||||
Some(&chatgpt_base_url),
|
||||
AuthKeyringBackendKind::Direct,
|
||||
@@ -366,6 +394,7 @@ async fn chatgpt_auth_registers_agent_identity_when_enabled() -> anyhow::Result<
|
||||
codex_home.path(),
|
||||
/*enable_codex_api_key_env*/ false,
|
||||
AuthCredentialsStoreMode::File,
|
||||
/*allowed_login_methods*/ None,
|
||||
/*forced_chatgpt_workspace_id*/ None,
|
||||
/*chatgpt_base_url*/ None,
|
||||
AuthKeyringBackendKind::Direct,
|
||||
@@ -448,6 +477,7 @@ async fn chatgpt_auth_registers_agent_identity_when_enabled() -> anyhow::Result<
|
||||
codex_home.path(),
|
||||
/*enable_codex_api_key_env*/ false,
|
||||
AuthCredentialsStoreMode::File,
|
||||
/*allowed_login_methods*/ None,
|
||||
/*forced_chatgpt_workspace_id*/ None,
|
||||
/*chatgpt_base_url*/ None,
|
||||
AuthKeyringBackendKind::Direct,
|
||||
@@ -490,6 +520,7 @@ async fn chatgpt_auth_retries_transient_agent_identity_registration() -> anyhow:
|
||||
codex_home.path(),
|
||||
/*enable_codex_api_key_env*/ false,
|
||||
AuthCredentialsStoreMode::File,
|
||||
/*allowed_login_methods*/ None,
|
||||
/*forced_chatgpt_workspace_id*/ None,
|
||||
/*chatgpt_base_url*/ None,
|
||||
AuthKeyringBackendKind::Direct,
|
||||
@@ -556,6 +587,7 @@ async fn chatgpt_auth_registration_retry_exhaustion_is_fallback_eligible() -> an
|
||||
codex_home.path(),
|
||||
/*enable_codex_api_key_env*/ false,
|
||||
AuthCredentialsStoreMode::File,
|
||||
/*allowed_login_methods*/ None,
|
||||
/*forced_chatgpt_workspace_id*/ None,
|
||||
/*chatgpt_base_url*/ None,
|
||||
AuthKeyringBackendKind::Direct,
|
||||
@@ -617,6 +649,7 @@ async fn chatgpt_auth_task_registration_retry_exhaustion_is_fallback_eligible()
|
||||
codex_home.path(),
|
||||
/*enable_codex_api_key_env*/ false,
|
||||
AuthCredentialsStoreMode::File,
|
||||
/*allowed_login_methods*/ None,
|
||||
/*forced_chatgpt_workspace_id*/ None,
|
||||
/*chatgpt_base_url*/ None,
|
||||
AuthKeyringBackendKind::Direct,
|
||||
@@ -673,6 +706,7 @@ async fn chatgpt_auth_non_retryable_registration_error_is_hard_failure() -> anyh
|
||||
codex_home.path(),
|
||||
/*enable_codex_api_key_env*/ false,
|
||||
AuthCredentialsStoreMode::File,
|
||||
/*allowed_login_methods*/ None,
|
||||
/*forced_chatgpt_workspace_id*/ None,
|
||||
/*chatgpt_base_url*/ None,
|
||||
AuthKeyringBackendKind::Direct,
|
||||
@@ -816,6 +850,7 @@ async fn pro_account_with_no_api_key_uses_chatgpt_auth() {
|
||||
codex_home.path(),
|
||||
/*enable_codex_api_key_env*/ false,
|
||||
AuthCredentialsStoreMode::File,
|
||||
/*allowed_login_methods*/ None,
|
||||
/*forced_chatgpt_workspace_id*/ None,
|
||||
/*chatgpt_base_url*/ None,
|
||||
AuthKeyringBackendKind::Direct,
|
||||
@@ -878,6 +913,7 @@ async fn loads_api_key_from_auth_json() {
|
||||
dir.path(),
|
||||
/*enable_codex_api_key_env*/ false,
|
||||
AuthCredentialsStoreMode::File,
|
||||
/*allowed_login_methods*/ None,
|
||||
/*forced_chatgpt_workspace_id*/ None,
|
||||
/*chatgpt_base_url*/ None,
|
||||
AuthKeyringBackendKind::Direct,
|
||||
@@ -974,6 +1010,7 @@ async fn refresh_failure_is_scoped_to_the_matching_auth_snapshot() {
|
||||
codex_home.path(),
|
||||
/*enable_codex_api_key_env*/ false,
|
||||
AuthCredentialsStoreMode::File,
|
||||
/*allowed_login_methods*/ None,
|
||||
/*forced_chatgpt_workspace_id*/ None,
|
||||
/*chatgpt_base_url*/ None,
|
||||
AuthKeyringBackendKind::Direct,
|
||||
@@ -1343,6 +1380,7 @@ async fn build_config(
|
||||
keyring_backend_kind: AuthKeyringBackendKind::Direct,
|
||||
forced_login_method,
|
||||
forced_chatgpt_workspace_id,
|
||||
managed_auth_policy: ManagedAuthPolicy::default(),
|
||||
chatgpt_base_url: None,
|
||||
auth_route_config: crate::test_support::transport_default_auth_route_config(),
|
||||
}
|
||||
@@ -1423,6 +1461,7 @@ async fn load_auth_reads_access_token_from_env() {
|
||||
codex_home.path(),
|
||||
/*enable_codex_api_key_env*/ false,
|
||||
AuthCredentialsStoreMode::File,
|
||||
/*allowed_login_methods*/ None,
|
||||
/*forced_chatgpt_workspace_id*/ None,
|
||||
Some(&chatgpt_base_url),
|
||||
AuthKeyringBackendKind::Direct,
|
||||
@@ -1471,6 +1510,7 @@ async fn load_auth_reads_personal_access_token_from_env() {
|
||||
codex_home.path(),
|
||||
/*enable_codex_api_key_env*/ false,
|
||||
auth_credentials_store_mode,
|
||||
/*allowed_login_methods*/ None,
|
||||
/*forced_chatgpt_workspace_id*/ None,
|
||||
/*chatgpt_base_url*/ None,
|
||||
AuthKeyringBackendKind::default(),
|
||||
@@ -1644,6 +1684,7 @@ async fn load_auth_keeps_codex_api_key_env_precedence() {
|
||||
codex_home.path(),
|
||||
/*enable_codex_api_key_env*/ true,
|
||||
AuthCredentialsStoreMode::File,
|
||||
/*allowed_login_methods*/ None,
|
||||
/*forced_chatgpt_workspace_id*/ None,
|
||||
/*chatgpt_base_url*/ None,
|
||||
AuthKeyringBackendKind::Direct,
|
||||
@@ -1687,6 +1728,162 @@ async fn enforce_login_restrictions_logs_out_for_method_mismatch() {
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[serial(codex_auth_env)]
|
||||
async fn auth_manager_rejects_disallowed_stored_and_external_auth() {
|
||||
let codex_home = tempdir().unwrap();
|
||||
let _access_token_guard = remove_access_token_env_var();
|
||||
login_with_api_key(
|
||||
codex_home.path(),
|
||||
"sk-test",
|
||||
AuthCredentialsStoreMode::File,
|
||||
AuthKeyringBackendKind::default(),
|
||||
)
|
||||
.expect("seed api key");
|
||||
let mut config = build_config(
|
||||
codex_home.path(),
|
||||
/*forced_login_method*/ None,
|
||||
/*forced_chatgpt_workspace_id*/ None,
|
||||
)
|
||||
.await;
|
||||
config.managed_auth_policy.allowed_login_methods = Some(vec![ForcedLoginMethod::Chatgpt]);
|
||||
let manager =
|
||||
AuthManager::shared_from_auth_config(config, /*enable_codex_api_key_env*/ false).await;
|
||||
|
||||
assert_eq!(manager.auth().await, None);
|
||||
assert!(
|
||||
manager
|
||||
.set_external_auth(Arc::new(StaticExternalAuth(CodexAuth::from_api_key(
|
||||
"sk-external",
|
||||
))))
|
||||
.await
|
||||
.is_err(),
|
||||
"external auth cannot bypass managed login policy"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[serial(codex_auth_env)]
|
||||
async fn api_only_policy_rejects_access_tokens_before_hydration() {
|
||||
let codex_home = tempdir().unwrap();
|
||||
let server = MockServer::start().await;
|
||||
let _authapi_guard = EnvVarGuard::set("CODEX_AUTHAPI_BASE_URL", &server.uri());
|
||||
let _access_token_guard = EnvVarGuard::set(CODEX_ACCESS_TOKEN_ENV_VAR, "at-rejected");
|
||||
let mut config = build_config(
|
||||
codex_home.path(),
|
||||
/*forced_login_method*/ None,
|
||||
/*forced_chatgpt_workspace_id*/ None,
|
||||
)
|
||||
.await;
|
||||
config.managed_auth_policy.allowed_login_methods = Some(vec![ForcedLoginMethod::Api]);
|
||||
let manager =
|
||||
AuthManager::shared_from_auth_config(config, /*enable_codex_api_key_env*/ false).await;
|
||||
|
||||
assert_eq!(manager.auth().await, None);
|
||||
assert!(
|
||||
server
|
||||
.received_requests()
|
||||
.await
|
||||
.expect("inspect auth requests")
|
||||
.is_empty(),
|
||||
"rejected access tokens must not call whoami or register Agent Identity"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[serial(codex_auth_env)]
|
||||
async fn workspace_policy_rejects_agent_identity_before_hydration() {
|
||||
let codex_home = tempdir().unwrap();
|
||||
let server = MockServer::start().await;
|
||||
let record = agent_identity_record(WORKSPACE_ID_DISALLOWED);
|
||||
let agent_identity =
|
||||
signed_agent_identity_jwt(&record, json!(record.plan_type)).expect("signed agent identity");
|
||||
let _authapi_guard = EnvVarGuard::set("CODEX_AUTHAPI_BASE_URL", &server.uri());
|
||||
let _access_token_reset = remove_access_token_env_var();
|
||||
let access_token_guard = EnvVarGuard::set(CODEX_ACCESS_TOKEN_ENV_VAR, &agent_identity);
|
||||
let mut config = build_config(
|
||||
codex_home.path(),
|
||||
/*forced_login_method*/ None,
|
||||
/*forced_chatgpt_workspace_id*/ None,
|
||||
)
|
||||
.await;
|
||||
config.managed_auth_policy.allowed_chatgpt_workspaces =
|
||||
Some(vec![WORKSPACE_ID_ALLOWED.to_string()]);
|
||||
let manager =
|
||||
AuthManager::shared_from_auth_config(config, /*enable_codex_api_key_env*/ false).await;
|
||||
|
||||
assert_eq!(manager.auth().await, None);
|
||||
drop(access_token_guard);
|
||||
|
||||
for stored_agent_identity in [
|
||||
AgentIdentityStorage::Jwt(agent_identity),
|
||||
AgentIdentityStorage::Record(record),
|
||||
] {
|
||||
save_auth(
|
||||
codex_home.path(),
|
||||
&AuthDotJson {
|
||||
auth_mode: Some(AuthMode::AgentIdentity),
|
||||
openai_api_key: None,
|
||||
tokens: None,
|
||||
last_refresh: None,
|
||||
agent_identity: Some(stored_agent_identity),
|
||||
personal_access_token: None,
|
||||
bedrock_api_key: None,
|
||||
},
|
||||
AuthCredentialsStoreMode::File,
|
||||
AuthKeyringBackendKind::Direct,
|
||||
)
|
||||
.expect("store agent identity");
|
||||
let mut config = build_config(
|
||||
codex_home.path(),
|
||||
/*forced_login_method*/ None,
|
||||
/*forced_chatgpt_workspace_id*/ None,
|
||||
)
|
||||
.await;
|
||||
config.managed_auth_policy.allowed_chatgpt_workspaces =
|
||||
Some(vec![WORKSPACE_ID_ALLOWED.to_string()]);
|
||||
let manager =
|
||||
AuthManager::shared_from_auth_config(config, /*enable_codex_api_key_env*/ false).await;
|
||||
assert_eq!(manager.auth().await, None);
|
||||
}
|
||||
|
||||
assert!(
|
||||
server.received_requests().await.unwrap().is_empty(),
|
||||
"rejected agent identities must not fetch JWKS or register"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[serial(codex_auth_env)]
|
||||
async fn workspace_policy_checks_the_selected_request_account() {
|
||||
let codex_home = tempdir().unwrap();
|
||||
let _access_token_guard = remove_access_token_env_var();
|
||||
write_auth_file(
|
||||
AuthFileParams {
|
||||
openai_api_key: None,
|
||||
chatgpt_plan_type: Some("pro".to_string()),
|
||||
chatgpt_account_id: Some(WORKSPACE_ID_ALLOWED.to_string()),
|
||||
},
|
||||
codex_home.path(),
|
||||
)
|
||||
.expect("seed ChatGPT credentials");
|
||||
let auth_path = codex_home.path().join("auth.json");
|
||||
let mut stored: serde_json::Value =
|
||||
serde_json::from_slice(&std::fs::read(&auth_path).unwrap()).unwrap();
|
||||
stored["tokens"]["account_id"] = json!(WORKSPACE_ID_DISALLOWED);
|
||||
std::fs::write(&auth_path, serde_json::to_vec(&stored).unwrap()).unwrap();
|
||||
let config = build_config(
|
||||
codex_home.path(),
|
||||
/*forced_login_method*/ None,
|
||||
Some(vec![WORKSPACE_ID_ALLOWED.to_string()]),
|
||||
)
|
||||
.await;
|
||||
let manager =
|
||||
AuthManager::shared_from_auth_config(config, /*enable_codex_api_key_env*/ false).await;
|
||||
|
||||
assert_eq!(manager.auth().await, None);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[serial(codex_auth_env)]
|
||||
async fn enforce_login_restrictions_logs_out_for_workspace_mismatch() {
|
||||
@@ -1756,6 +1953,7 @@ async fn enforce_login_restrictions_logs_out_for_personal_access_token_workspace
|
||||
keyring_backend_kind: AuthKeyringBackendKind::default(),
|
||||
forced_login_method: None,
|
||||
forced_chatgpt_workspace_id: Some(vec![WORKSPACE_ID_ALLOWED.to_string()]),
|
||||
managed_auth_policy: ManagedAuthPolicy::default(),
|
||||
chatgpt_base_url: None,
|
||||
auth_route_config: crate::test_support::transport_default_auth_route_config(),
|
||||
};
|
||||
@@ -1880,6 +2078,7 @@ async fn enforce_login_restrictions_logs_out_for_agent_identity_workspace_mismat
|
||||
keyring_backend_kind: AuthKeyringBackendKind::Direct,
|
||||
forced_login_method: None,
|
||||
forced_chatgpt_workspace_id: Some(vec![WORKSPACE_ID_ALLOWED.to_string()]),
|
||||
managed_auth_policy: ManagedAuthPolicy::default(),
|
||||
chatgpt_base_url: Some(chatgpt_base_url),
|
||||
auth_route_config: crate::test_support::transport_default_auth_route_config(),
|
||||
};
|
||||
@@ -2162,6 +2361,7 @@ async fn plan_type_maps_known_plan() {
|
||||
codex_home.path(),
|
||||
/*enable_codex_api_key_env*/ false,
|
||||
AuthCredentialsStoreMode::File,
|
||||
/*allowed_login_methods*/ None,
|
||||
/*forced_chatgpt_workspace_id*/ None,
|
||||
/*chatgpt_base_url*/ None,
|
||||
AuthKeyringBackendKind::Direct,
|
||||
@@ -2194,6 +2394,7 @@ async fn plan_type_maps_self_serve_business_usage_based_plan() {
|
||||
codex_home.path(),
|
||||
/*enable_codex_api_key_env*/ false,
|
||||
AuthCredentialsStoreMode::File,
|
||||
/*allowed_login_methods*/ None,
|
||||
/*forced_chatgpt_workspace_id*/ None,
|
||||
/*chatgpt_base_url*/ None,
|
||||
AuthKeyringBackendKind::Direct,
|
||||
@@ -2229,6 +2430,7 @@ async fn plan_type_maps_enterprise_cbp_usage_based_plan() {
|
||||
codex_home.path(),
|
||||
/*enable_codex_api_key_env*/ false,
|
||||
AuthCredentialsStoreMode::File,
|
||||
/*allowed_login_methods*/ None,
|
||||
/*forced_chatgpt_workspace_id*/ None,
|
||||
/*chatgpt_base_url*/ None,
|
||||
AuthKeyringBackendKind::Direct,
|
||||
@@ -2264,6 +2466,7 @@ async fn plan_type_maps_unknown_to_unknown() {
|
||||
codex_home.path(),
|
||||
/*enable_codex_api_key_env*/ false,
|
||||
AuthCredentialsStoreMode::File,
|
||||
/*allowed_login_methods*/ None,
|
||||
/*forced_chatgpt_workspace_id*/ None,
|
||||
/*chatgpt_base_url*/ None,
|
||||
AuthKeyringBackendKind::Direct,
|
||||
@@ -2296,6 +2499,7 @@ async fn missing_plan_type_maps_to_unknown() {
|
||||
codex_home.path(),
|
||||
/*enable_codex_api_key_env*/ false,
|
||||
AuthCredentialsStoreMode::File,
|
||||
/*allowed_login_methods*/ None,
|
||||
/*forced_chatgpt_workspace_id*/ None,
|
||||
/*chatgpt_base_url*/ None,
|
||||
AuthKeyringBackendKind::Direct,
|
||||
|
||||
@@ -56,6 +56,7 @@ use crate::outbound_proxy::AuthRouteConfig;
|
||||
use crate::token_data::TokenData;
|
||||
use crate::token_data::parse_chatgpt_jwt_claims;
|
||||
use crate::token_data::parse_jwt_expiration;
|
||||
use codex_config::ManagedAuthPolicy;
|
||||
use codex_config::types::AuthCredentialsStoreMode;
|
||||
use codex_http_client::HttpClient;
|
||||
use codex_http_client::HttpClientFactory;
|
||||
@@ -361,6 +362,7 @@ impl CodexAuth {
|
||||
codex_home,
|
||||
/*enable_codex_api_key_env*/ false,
|
||||
auth_credentials_store_mode,
|
||||
/*allowed_login_methods*/ None,
|
||||
/*forced_chatgpt_workspace_id*/ None,
|
||||
chatgpt_base_url,
|
||||
keyring_backend_kind,
|
||||
@@ -940,7 +942,7 @@ pub async fn login_with_access_token(
|
||||
let auth_dot_json = match classify_codex_access_token(access_token) {
|
||||
CodexAccessToken::PersonalAccessToken(access_token) => {
|
||||
let auth = PersonalAccessTokenAuth::load(access_token, auth_route_config).await?;
|
||||
ensure_personal_access_token_workspace_allowed(forced_chatgpt_workspace_id, &auth)?;
|
||||
ensure_auth_workspace_allowed(forced_chatgpt_workspace_id, auth.account_id())?;
|
||||
AuthDotJson {
|
||||
// Infer PAT auth from the credential field so older Codex builds can still
|
||||
// deserialize auth.json after a rollback.
|
||||
@@ -954,6 +956,8 @@ pub async fn login_with_access_token(
|
||||
}
|
||||
}
|
||||
CodexAccessToken::AgentIdentityJwt(jwt) => {
|
||||
let record = AgentIdentityAuthRecord::from_agent_identity_jwt(jwt)?;
|
||||
ensure_auth_workspace_allowed(forced_chatgpt_workspace_id, &record.account_id)?;
|
||||
let base_url = chatgpt_base_url
|
||||
.unwrap_or(ChatGptEnvironment::default().chatgpt_base_url())
|
||||
.trim_end_matches('/')
|
||||
@@ -978,14 +982,33 @@ pub async fn login_with_access_token(
|
||||
)
|
||||
}
|
||||
|
||||
fn ensure_personal_access_token_workspace_allowed(
|
||||
fn ensure_auth_workspace_allowed(
|
||||
expected_workspace_ids: Option<&[String]>,
|
||||
auth: &PersonalAccessTokenAuth,
|
||||
account_id: &str,
|
||||
) -> std::io::Result<()> {
|
||||
crate::server::ensure_workspace_account_allowed(expected_workspace_ids, auth.account_id())
|
||||
crate::server::ensure_workspace_account_allowed(expected_workspace_ids, account_id)
|
||||
.map_err(|message| std::io::Error::new(std::io::ErrorKind::PermissionDenied, message))
|
||||
}
|
||||
|
||||
fn ensure_agent_identity_workspace_allowed(
|
||||
expected_workspace_ids: Option<&[String]>,
|
||||
agent_identity: &AgentIdentityStorage,
|
||||
) -> std::io::Result<()> {
|
||||
let Some(expected_workspace_ids) = expected_workspace_ids else {
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
match agent_identity {
|
||||
AgentIdentityStorage::Jwt(jwt) => {
|
||||
let record = AgentIdentityAuthRecord::from_agent_identity_jwt(jwt)?;
|
||||
ensure_auth_workspace_allowed(Some(expected_workspace_ids), &record.account_id)
|
||||
}
|
||||
AgentIdentityStorage::Record(record) => {
|
||||
ensure_auth_workspace_allowed(Some(expected_workspace_ids), &record.account_id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Writes an in-memory auth payload for externally managed ChatGPT tokens.
|
||||
pub fn login_with_chatgpt_auth_tokens(
|
||||
codex_home: &Path,
|
||||
@@ -1047,9 +1070,131 @@ pub struct AuthConfig {
|
||||
pub forced_login_method: Option<ForcedLoginMethod>,
|
||||
pub chatgpt_base_url: Option<String>,
|
||||
pub forced_chatgpt_workspace_id: Option<Vec<String>>,
|
||||
pub managed_auth_policy: ManagedAuthPolicy,
|
||||
pub auth_route_config: AuthRouteConfig,
|
||||
}
|
||||
|
||||
impl AuthConfig {
|
||||
pub fn is_login_method_allowed(&self, method: ForcedLoginMethod) -> bool {
|
||||
self.managed_auth_policy.allows_login_method(
|
||||
method,
|
||||
self.forced_login_method,
|
||||
self.forced_chatgpt_workspace_id.as_deref(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn effective_chatgpt_workspaces(&self) -> Option<Vec<String>> {
|
||||
self.managed_auth_policy
|
||||
.effective_chatgpt_workspaces(self.forced_chatgpt_workspace_id.as_deref())
|
||||
}
|
||||
|
||||
pub fn validate(&self) -> std::io::Result<()> {
|
||||
if self.is_login_method_allowed(ForcedLoginMethod::Api)
|
||||
|| self.is_login_method_allowed(ForcedLoginMethod::Chatgpt)
|
||||
{
|
||||
Ok(())
|
||||
} else {
|
||||
Err(std::io::Error::new(
|
||||
std::io::ErrorKind::PermissionDenied,
|
||||
"authentication requirements do not permit any usable login method",
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn allows_auth(&self, auth: &CodexAuth) -> bool {
|
||||
let allowed_login_methods = self.allowed_login_methods();
|
||||
let workspaces = self.effective_chatgpt_workspaces();
|
||||
validate_auth_restrictions(Some(&allowed_login_methods), workspaces.as_deref(), auth)
|
||||
.is_ok()
|
||||
}
|
||||
|
||||
pub async fn load_auth(
|
||||
&self,
|
||||
enable_codex_api_key_env: bool,
|
||||
) -> std::io::Result<Option<CodexAuth>> {
|
||||
let allowed_login_methods = self.allowed_login_methods();
|
||||
let workspaces = self.effective_chatgpt_workspaces();
|
||||
let agent_identity_authapi_base_url =
|
||||
agent_identity_authapi_base_url(self.chatgpt_base_url.as_deref()).ok();
|
||||
let auth = load_auth(
|
||||
&self.codex_home,
|
||||
enable_codex_api_key_env,
|
||||
self.auth_credentials_store_mode,
|
||||
Some(&allowed_login_methods),
|
||||
workspaces.as_deref(),
|
||||
self.chatgpt_base_url.as_deref(),
|
||||
self.keyring_backend_kind,
|
||||
agent_identity_authapi_base_url.as_deref(),
|
||||
&self.auth_route_config,
|
||||
)
|
||||
.await?;
|
||||
Ok(auth.filter(|auth| self.allows_auth(auth)))
|
||||
}
|
||||
|
||||
fn allowed_login_methods(&self) -> Vec<ForcedLoginMethod> {
|
||||
self.managed_auth_policy.allowed_login_methods(
|
||||
self.forced_login_method,
|
||||
self.forced_chatgpt_workspace_id.as_deref(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn auth_mode_is_allowed(
|
||||
allowed_login_methods: Option<&[ForcedLoginMethod]>,
|
||||
mode: AuthMode,
|
||||
) -> bool {
|
||||
let method = if mode.uses_codex_backend() {
|
||||
ForcedLoginMethod::Chatgpt
|
||||
} else {
|
||||
ForcedLoginMethod::Api
|
||||
};
|
||||
allowed_login_methods.is_none_or(|allowed| allowed.contains(&method))
|
||||
}
|
||||
|
||||
fn validate_auth_restrictions(
|
||||
allowed_login_methods: Option<&[ForcedLoginMethod]>,
|
||||
expected_workspaces: Option<&[String]>,
|
||||
auth: &CodexAuth,
|
||||
) -> Result<(), String> {
|
||||
if !auth_mode_is_allowed(allowed_login_methods, auth.auth_mode()) {
|
||||
return Err(match allowed_login_methods {
|
||||
Some(methods) if methods.contains(&ForcedLoginMethod::Api) => {
|
||||
"API key login is required".to_string()
|
||||
}
|
||||
Some(_) => "ChatGPT login is required".to_string(),
|
||||
None => unreachable!("unrestricted login methods accept every auth mode"),
|
||||
});
|
||||
}
|
||||
|
||||
let Some(expected_workspaces) = expected_workspaces else {
|
||||
return Ok(());
|
||||
};
|
||||
if matches!(
|
||||
auth,
|
||||
CodexAuth::ApiKey(_) | CodexAuth::Headers(_) | CodexAuth::BedrockApiKey(_)
|
||||
) {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let actual_workspace = auth.get_account_id().or_else(|| {
|
||||
auth.get_token_data()
|
||||
.ok()
|
||||
.and_then(|tokens| tokens.id_token.chatgpt_account_id)
|
||||
});
|
||||
if actual_workspace
|
||||
.as_ref()
|
||||
.is_some_and(|workspace| expected_workspaces.contains(workspace))
|
||||
{
|
||||
Ok(())
|
||||
} else {
|
||||
let actual = actual_workspace.unwrap_or_else(|| "unknown".to_string());
|
||||
Err(format!(
|
||||
"Login is restricted to workspace(s) {}, but current credentials belong to {actual}",
|
||||
expected_workspaces.join(", ")
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
/// Enforces configured login restrictions using auth-owned HTTP settings.
|
||||
pub async fn enforce_login_restrictions(config: &AuthConfig) -> std::io::Result<()> {
|
||||
let agent_identity_authapi_base_url =
|
||||
@@ -1065,10 +1210,16 @@ async fn enforce_login_restrictions_with_agent_identity_authapi_base_url(
|
||||
config: &AuthConfig,
|
||||
agent_identity_authapi_base_url: Option<&str>,
|
||||
) -> std::io::Result<()> {
|
||||
// Managed-only restrictions are enforced by AuthManager.
|
||||
if config.forced_login_method.is_none() && config.forced_chatgpt_workspace_id.is_none() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let Some(auth) = load_auth(
|
||||
&config.codex_home,
|
||||
/*enable_codex_api_key_env*/ true,
|
||||
config.auth_credentials_store_mode,
|
||||
/*allowed_login_methods*/ None,
|
||||
/*forced_chatgpt_workspace_id*/ None,
|
||||
config.chatgpt_base_url.as_deref(),
|
||||
config.keyring_backend_kind,
|
||||
@@ -1218,6 +1369,7 @@ async fn load_auth(
|
||||
codex_home: &Path,
|
||||
enable_codex_api_key_env: bool,
|
||||
auth_credentials_store_mode: AuthCredentialsStoreMode,
|
||||
allowed_login_methods: Option<&[ForcedLoginMethod]>,
|
||||
forced_chatgpt_workspace_id: Option<&[String]>,
|
||||
chatgpt_base_url: Option<&str>,
|
||||
keyring_backend_kind: AuthKeyringBackendKind,
|
||||
@@ -1225,7 +1377,10 @@ async fn load_auth(
|
||||
auth_route_config: &AuthRouteConfig,
|
||||
) -> std::io::Result<Option<CodexAuth>> {
|
||||
// API key via env var takes precedence over any other auth method.
|
||||
if enable_codex_api_key_env && let Some(api_key) = read_codex_api_key_from_env() {
|
||||
if enable_codex_api_key_env
|
||||
&& auth_mode_is_allowed(allowed_login_methods, AuthMode::ApiKey)
|
||||
&& let Some(api_key) = read_codex_api_key_from_env()
|
||||
{
|
||||
return Ok(Some(CodexAuth::from_api_key(api_key.as_str())));
|
||||
}
|
||||
|
||||
@@ -1236,7 +1391,12 @@ async fn load_auth(
|
||||
AuthCredentialsStoreMode::Ephemeral,
|
||||
AuthKeyringBackendKind::default(),
|
||||
);
|
||||
if let Some(auth_dot_json) = ephemeral_storage.load()? {
|
||||
if let Some(auth_dot_json) = ephemeral_storage.load()?
|
||||
&& auth_mode_is_allowed(allowed_login_methods, auth_dot_json.resolved_mode())
|
||||
{
|
||||
if let Some(agent_identity) = auth_dot_json.agent_identity.as_ref() {
|
||||
ensure_agent_identity_workspace_allowed(forced_chatgpt_workspace_id, agent_identity)?;
|
||||
}
|
||||
let auth = CodexAuth::from_auth_dot_json(
|
||||
codex_home,
|
||||
auth_dot_json,
|
||||
@@ -1248,19 +1408,23 @@ async fn load_auth(
|
||||
)
|
||||
.await?;
|
||||
if let CodexAuth::PersonalAccessToken(auth) = &auth {
|
||||
ensure_personal_access_token_workspace_allowed(forced_chatgpt_workspace_id, auth)?;
|
||||
ensure_auth_workspace_allowed(forced_chatgpt_workspace_id, auth.account_id())?;
|
||||
}
|
||||
return Ok(Some(auth));
|
||||
}
|
||||
|
||||
if let Some(access_token) = read_codex_access_token_from_env() {
|
||||
if auth_mode_is_allowed(allowed_login_methods, AuthMode::AgentIdentity)
|
||||
&& let Some(access_token) = read_codex_access_token_from_env()
|
||||
{
|
||||
return match classify_codex_access_token(&access_token) {
|
||||
CodexAccessToken::PersonalAccessToken(access_token) => {
|
||||
let auth = PersonalAccessTokenAuth::load(access_token, auth_route_config).await?;
|
||||
ensure_personal_access_token_workspace_allowed(forced_chatgpt_workspace_id, &auth)?;
|
||||
ensure_auth_workspace_allowed(forced_chatgpt_workspace_id, auth.account_id())?;
|
||||
Ok(Some(CodexAuth::PersonalAccessToken(auth)))
|
||||
}
|
||||
CodexAccessToken::AgentIdentityJwt(jwt) => {
|
||||
let record = AgentIdentityAuthRecord::from_agent_identity_jwt(jwt)?;
|
||||
ensure_auth_workspace_allowed(forced_chatgpt_workspace_id, &record.account_id)?;
|
||||
CodexAuth::from_agent_identity_jwt_with_authapi_base_url(
|
||||
jwt,
|
||||
chatgpt_base_url,
|
||||
@@ -1288,6 +1452,12 @@ async fn load_auth(
|
||||
Some(auth) => auth,
|
||||
None => return Ok(None),
|
||||
};
|
||||
if !auth_mode_is_allowed(allowed_login_methods, auth_dot_json.resolved_mode()) {
|
||||
return Ok(None);
|
||||
}
|
||||
if let Some(agent_identity) = auth_dot_json.agent_identity.as_ref() {
|
||||
ensure_agent_identity_workspace_allowed(forced_chatgpt_workspace_id, agent_identity)?;
|
||||
}
|
||||
|
||||
let auth = CodexAuth::from_auth_dot_json(
|
||||
codex_home,
|
||||
@@ -1300,7 +1470,7 @@ async fn load_auth(
|
||||
)
|
||||
.await?;
|
||||
if let CodexAuth::PersonalAccessToken(auth) = &auth {
|
||||
ensure_personal_access_token_workspace_allowed(forced_chatgpt_workspace_id, auth)?;
|
||||
ensure_auth_workspace_allowed(forced_chatgpt_workspace_id, auth.account_id())?;
|
||||
}
|
||||
Ok(Some(auth))
|
||||
}
|
||||
@@ -1773,7 +1943,9 @@ pub struct AuthManager {
|
||||
enable_codex_api_key_env: bool,
|
||||
auth_credentials_store_mode: AuthCredentialsStoreMode,
|
||||
keyring_backend_kind: AuthKeyringBackendKind,
|
||||
forced_login_method: Option<ForcedLoginMethod>,
|
||||
forced_chatgpt_workspace_id: RwLock<Option<Vec<String>>>,
|
||||
managed_auth_policy: ManagedAuthPolicy,
|
||||
chatgpt_base_url: Option<String>,
|
||||
agent_identity_authapi_base_url: Option<String>,
|
||||
refresh_lock: Semaphore,
|
||||
@@ -1799,9 +1971,15 @@ pub trait AuthManagerConfig {
|
||||
/// Returns the backend to use when CLI auth keyring storage is selected.
|
||||
fn auth_keyring_backend_kind(&self) -> AuthKeyringBackendKind;
|
||||
|
||||
/// Returns the resolved login-method restriction, if any.
|
||||
fn forced_login_method(&self) -> Option<ForcedLoginMethod>;
|
||||
|
||||
/// Returns the workspace IDs that ChatGPT auth should be restricted to, if any.
|
||||
fn forced_chatgpt_workspace_id(&self) -> Option<Vec<String>>;
|
||||
|
||||
/// Returns administrator-managed authentication restrictions.
|
||||
fn managed_auth_policy(&self) -> ManagedAuthPolicy;
|
||||
|
||||
/// Returns the ChatGPT backend base URL used for first-party backend authorization.
|
||||
fn chatgpt_base_url(&self) -> String;
|
||||
|
||||
@@ -1820,10 +1998,12 @@ impl Debug for AuthManager {
|
||||
&self.auth_credentials_store_mode,
|
||||
)
|
||||
.field("keyring_backend_kind", &self.keyring_backend_kind)
|
||||
.field("forced_login_method", &self.forced_login_method)
|
||||
.field(
|
||||
"forced_chatgpt_workspace_id",
|
||||
&self.forced_chatgpt_workspace_id,
|
||||
)
|
||||
.field("managed_auth_policy", &self.managed_auth_policy)
|
||||
.field("chatgpt_base_url", &self.chatgpt_base_url)
|
||||
.field("auth_route_config", &self.auth_route_config)
|
||||
.field("has_external_auth", &self.has_external_auth())
|
||||
@@ -1849,21 +2029,40 @@ impl AuthManager {
|
||||
keyring_backend_kind: AuthKeyringBackendKind,
|
||||
auth_route_config: AuthRouteConfig,
|
||||
) -> Self {
|
||||
let agent_identity_authapi_base_url =
|
||||
agent_identity_authapi_base_url(chatgpt_base_url.as_deref()).ok();
|
||||
let managed_auth = load_auth(
|
||||
&codex_home,
|
||||
Self::new_from_auth_config(
|
||||
AuthConfig {
|
||||
codex_home,
|
||||
auth_credentials_store_mode,
|
||||
keyring_backend_kind,
|
||||
forced_login_method: None,
|
||||
chatgpt_base_url,
|
||||
forced_chatgpt_workspace_id,
|
||||
managed_auth_policy: ManagedAuthPolicy::default(),
|
||||
auth_route_config,
|
||||
},
|
||||
enable_codex_api_key_env,
|
||||
auth_credentials_store_mode,
|
||||
forced_chatgpt_workspace_id.as_deref(),
|
||||
chatgpt_base_url.as_deref(),
|
||||
keyring_backend_kind,
|
||||
agent_identity_authapi_base_url.as_deref(),
|
||||
&auth_route_config,
|
||||
)
|
||||
.await
|
||||
.ok()
|
||||
.flatten();
|
||||
}
|
||||
|
||||
async fn new_from_auth_config(auth_config: AuthConfig, enable_codex_api_key_env: bool) -> Self {
|
||||
let managed_auth = auth_config
|
||||
.load_auth(enable_codex_api_key_env)
|
||||
.await
|
||||
.ok()
|
||||
.flatten();
|
||||
let AuthConfig {
|
||||
codex_home,
|
||||
auth_credentials_store_mode,
|
||||
keyring_backend_kind,
|
||||
forced_login_method,
|
||||
chatgpt_base_url,
|
||||
forced_chatgpt_workspace_id,
|
||||
managed_auth_policy,
|
||||
auth_route_config,
|
||||
} = auth_config;
|
||||
let agent_identity_authapi_base_url =
|
||||
agent_identity_authapi_base_url(chatgpt_base_url.as_deref()).ok();
|
||||
let (auth_change_tx, _auth_change_rx) = watch::channel(0);
|
||||
Self {
|
||||
codex_home,
|
||||
@@ -1875,7 +2074,9 @@ impl AuthManager {
|
||||
enable_codex_api_key_env,
|
||||
auth_credentials_store_mode,
|
||||
keyring_backend_kind,
|
||||
forced_login_method,
|
||||
forced_chatgpt_workspace_id: RwLock::new(forced_chatgpt_workspace_id),
|
||||
managed_auth_policy,
|
||||
chatgpt_base_url,
|
||||
agent_identity_authapi_base_url,
|
||||
refresh_lock: Semaphore::new(/*permits*/ 1),
|
||||
@@ -1901,7 +2102,9 @@ impl AuthManager {
|
||||
enable_codex_api_key_env: false,
|
||||
auth_credentials_store_mode: AuthCredentialsStoreMode::File,
|
||||
keyring_backend_kind: AuthKeyringBackendKind::default(),
|
||||
forced_login_method: None,
|
||||
forced_chatgpt_workspace_id: RwLock::new(None),
|
||||
managed_auth_policy: ManagedAuthPolicy::default(),
|
||||
chatgpt_base_url: None,
|
||||
agent_identity_authapi_base_url: default_agent_identity_authapi_base_url(),
|
||||
refresh_lock: Semaphore::new(/*permits*/ 1),
|
||||
@@ -1926,7 +2129,9 @@ impl AuthManager {
|
||||
enable_codex_api_key_env: false,
|
||||
auth_credentials_store_mode: AuthCredentialsStoreMode::File,
|
||||
keyring_backend_kind: AuthKeyringBackendKind::default(),
|
||||
forced_login_method: None,
|
||||
forced_chatgpt_workspace_id: RwLock::new(None),
|
||||
managed_auth_policy: ManagedAuthPolicy::default(),
|
||||
chatgpt_base_url: None,
|
||||
agent_identity_authapi_base_url: default_agent_identity_authapi_base_url(),
|
||||
refresh_lock: Semaphore::new(/*permits*/ 1),
|
||||
@@ -1955,7 +2160,9 @@ impl AuthManager {
|
||||
enable_codex_api_key_env: false,
|
||||
auth_credentials_store_mode: AuthCredentialsStoreMode::File,
|
||||
keyring_backend_kind: AuthKeyringBackendKind::default(),
|
||||
forced_login_method: None,
|
||||
forced_chatgpt_workspace_id: RwLock::new(None),
|
||||
managed_auth_policy: ManagedAuthPolicy::default(),
|
||||
chatgpt_base_url: None,
|
||||
agent_identity_authapi_base_url: Some(
|
||||
agent_identity_authapi_base_url
|
||||
@@ -1982,7 +2189,9 @@ impl AuthManager {
|
||||
enable_codex_api_key_env: false,
|
||||
auth_credentials_store_mode: AuthCredentialsStoreMode::File,
|
||||
keyring_backend_kind: AuthKeyringBackendKind::default(),
|
||||
forced_login_method: None,
|
||||
forced_chatgpt_workspace_id: RwLock::new(None),
|
||||
managed_auth_policy: ManagedAuthPolicy::default(),
|
||||
chatgpt_base_url: None,
|
||||
agent_identity_authapi_base_url: default_agent_identity_authapi_base_url(),
|
||||
refresh_lock: Semaphore::new(/*permits*/ 1),
|
||||
@@ -2056,10 +2265,10 @@ impl AuthManager {
|
||||
.acquire()
|
||||
.await
|
||||
.map_err(std::io::Error::other)?;
|
||||
let forced_chatgpt_workspace_id = self.forced_chatgpt_workspace_id();
|
||||
let effective_chatgpt_workspaces = self.effective_chatgpt_workspaces();
|
||||
let cooldown_key = ManagedChatGptAgentIdentityBinding::from_auth(
|
||||
&auth,
|
||||
forced_chatgpt_workspace_id.clone(),
|
||||
effective_chatgpt_workspaces.clone(),
|
||||
)
|
||||
.and_then(|binding| {
|
||||
self.agent_identity_authapi_base_url
|
||||
@@ -2079,7 +2288,7 @@ impl AuthManager {
|
||||
.agent_identity_auth(
|
||||
policy,
|
||||
self.agent_identity_authapi_base_url.as_deref(),
|
||||
forced_chatgpt_workspace_id,
|
||||
effective_chatgpt_workspaces,
|
||||
&self.auth_route_config,
|
||||
session_source,
|
||||
)
|
||||
@@ -2098,7 +2307,7 @@ impl AuthManager {
|
||||
auth.agent_identity_auth(
|
||||
policy,
|
||||
self.agent_identity_authapi_base_url.as_deref(),
|
||||
self.forced_chatgpt_workspace_id(),
|
||||
self.effective_chatgpt_workspaces(),
|
||||
&self.auth_route_config,
|
||||
session_source,
|
||||
)
|
||||
@@ -2209,12 +2418,14 @@ impl AuthManager {
|
||||
};
|
||||
}
|
||||
|
||||
let forced_chatgpt_workspace_id = self.forced_chatgpt_workspace_id();
|
||||
let allowed_login_methods = self.allowed_login_methods();
|
||||
let effective_chatgpt_workspaces = self.effective_chatgpt_workspaces();
|
||||
load_auth(
|
||||
&self.codex_home,
|
||||
self.enable_codex_api_key_env,
|
||||
self.auth_credentials_store_mode,
|
||||
forced_chatgpt_workspace_id.as_deref(),
|
||||
Some(&allowed_login_methods),
|
||||
effective_chatgpt_workspaces.as_deref(),
|
||||
self.chatgpt_base_url.as_deref(),
|
||||
self.keyring_backend_kind,
|
||||
self.agent_identity_authapi_base_url.as_deref(),
|
||||
@@ -2223,6 +2434,14 @@ impl AuthManager {
|
||||
.await
|
||||
.ok()
|
||||
.flatten()
|
||||
.filter(|auth| {
|
||||
validate_auth_restrictions(
|
||||
Some(&allowed_login_methods),
|
||||
effective_chatgpt_workspaces.as_deref(),
|
||||
auth,
|
||||
)
|
||||
.is_ok()
|
||||
})
|
||||
}
|
||||
|
||||
fn set_cached_auth(&self, new_auth: Option<CodexAuth>) -> bool {
|
||||
@@ -2279,6 +2498,26 @@ impl AuthManager {
|
||||
.and_then(|guard| guard.clone())
|
||||
}
|
||||
|
||||
pub fn effective_chatgpt_workspaces(&self) -> Option<Vec<String>> {
|
||||
self.managed_auth_policy
|
||||
.effective_chatgpt_workspaces(self.forced_chatgpt_workspace_id().as_deref())
|
||||
}
|
||||
|
||||
pub fn is_login_method_allowed(&self, method: ForcedLoginMethod) -> bool {
|
||||
self.managed_auth_policy.allows_login_method(
|
||||
method,
|
||||
self.forced_login_method,
|
||||
self.forced_chatgpt_workspace_id().as_deref(),
|
||||
)
|
||||
}
|
||||
|
||||
fn allowed_login_methods(&self) -> Vec<ForcedLoginMethod> {
|
||||
self.managed_auth_policy.allowed_login_methods(
|
||||
self.forced_login_method,
|
||||
self.forced_chatgpt_workspace_id().as_deref(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn has_external_auth(&self) -> bool {
|
||||
self.external_auth().is_some()
|
||||
}
|
||||
@@ -2322,18 +2561,30 @@ impl AuthManager {
|
||||
config: &impl AuthManagerConfig,
|
||||
enable_codex_api_key_env: bool,
|
||||
) -> Arc<Self> {
|
||||
Self::shared(
|
||||
config.codex_home(),
|
||||
Self::shared_from_auth_config(
|
||||
AuthConfig {
|
||||
codex_home: config.codex_home(),
|
||||
auth_credentials_store_mode: config.cli_auth_credentials_store_mode(),
|
||||
keyring_backend_kind: config.auth_keyring_backend_kind(),
|
||||
forced_login_method: config.forced_login_method(),
|
||||
chatgpt_base_url: Some(config.chatgpt_base_url()),
|
||||
forced_chatgpt_workspace_id: config.forced_chatgpt_workspace_id(),
|
||||
managed_auth_policy: config.managed_auth_policy(),
|
||||
auth_route_config: config.auth_route_config(),
|
||||
},
|
||||
enable_codex_api_key_env,
|
||||
config.cli_auth_credentials_store_mode(),
|
||||
config.forced_chatgpt_workspace_id(),
|
||||
Some(config.chatgpt_base_url()),
|
||||
config.auth_keyring_backend_kind(),
|
||||
config.auth_route_config(),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
/// Builds a shared manager using restrictions resolved before authentication.
|
||||
pub async fn shared_from_auth_config(
|
||||
auth_config: AuthConfig,
|
||||
enable_codex_api_key_env: bool,
|
||||
) -> Arc<Self> {
|
||||
Arc::new(Self::new_from_auth_config(auth_config, enable_codex_api_key_env).await)
|
||||
}
|
||||
|
||||
pub fn unauthorized_recovery(self: &Arc<Self>) -> UnauthorizedRecovery {
|
||||
UnauthorizedRecovery::new(Arc::clone(self))
|
||||
}
|
||||
@@ -2582,16 +2833,13 @@ impl AuthManager {
|
||||
}
|
||||
|
||||
fn validate_external_auth(&self, auth: &CodexAuth) -> Result<(), RefreshTokenError> {
|
||||
if let Some(account_id) = auth.get_account_id()
|
||||
&& let Some(expected_workspace_ids) = self.forced_chatgpt_workspace_id()
|
||||
&& !expected_workspace_ids.contains(&account_id)
|
||||
{
|
||||
return Err(RefreshTokenError::Transient(std::io::Error::other(
|
||||
format!(
|
||||
"external auth returned workspace {account_id:?}, expected one of {expected_workspace_ids:?}"
|
||||
),
|
||||
)));
|
||||
}
|
||||
let allowed_login_methods = self.allowed_login_methods();
|
||||
validate_auth_restrictions(
|
||||
Some(&allowed_login_methods),
|
||||
self.effective_chatgpt_workspaces().as_deref(),
|
||||
auth,
|
||||
)
|
||||
.map_err(std::io::Error::other)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user