app-server: centralize AuthManager initialization (#16764)

Extract a shared helper that builds AuthManager from Config and applies
the forced ChatGPT workspace override in one place.

Create the shared AuthManager at MessageProcessor call sites so that
upcoming new transport's initialization can reuse the same handle, and
keep only external auth refresher wiring inside `MessageProcessor`.

Remove the now-unused `AuthManager::shared_with_external_auth` helper.
This commit is contained in:
Ruslan Nigmatullin authored and GitHub committed 2026-04-06 12:46:55 -07:00
1 parent 46b7e4fb2c
commit 1525bbdb9a
10 files changed
+69 -48

No files matched your search

+25 -9
View File
@@ -1108,6 +1108,23 @@ pub struct AuthManager {
external_auth: RwLock<Option<Arc<dyn ExternalAuth>>>,
}
/// Configuration view required to construct a shared [`AuthManager`].
///
/// Implementations should return the auth-related config values for the
/// already-resolved runtime configuration. The primary implementation is
/// `codex_core::config::Config`, but this trait keeps `codex-login` independent
/// from `codex-core`.
pub trait AuthManagerConfig {
/// Returns the Codex home directory used for auth storage.
fn codex_home(&self) -> PathBuf;
/// Returns the CLI auth credential storage mode for auth loading.
fn cli_auth_credentials_store_mode(&self) -> AuthCredentialsStoreMode;
/// Returns the workspace ID that ChatGPT auth should be restricted to, if any.
fn forced_chatgpt_workspace_id(&self) -> Option<String>;
}
impl Debug for AuthManager {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AuthManager")
@@ -1404,19 +1421,18 @@ impl AuthManager {
))
}
pub fn shared_with_external_auth(
codex_home: PathBuf,
/// Convenience constructor returning an `Arc` wrapper from resolved config.
pub fn shared_from_config(
config: &impl AuthManagerConfig,
enable_codex_api_key_env: bool,
auth_credentials_store_mode: AuthCredentialsStoreMode,
external_auth: Arc<dyn ExternalAuth>,
) -> Arc<Self> {
let manager = Self::shared(
codex_home,
let auth_manager = Self::shared(
config.codex_home(),
enable_codex_api_key_env,
auth_credentials_store_mode,
config.cli_auth_credentials_store_mode(),
);
manager.set_external_auth(external_auth);
manager
auth_manager.set_forced_chatgpt_workspace_id(config.forced_chatgpt_workspace_id());
auth_manager
}
pub fn unauthorized_recovery(self: &Arc<Self>) -> UnauthorizedRecovery {
+1
View File
@@ -23,6 +23,7 @@ pub use auth::AuthConfig;
pub use auth::AuthCredentialsStoreMode;
pub use auth::AuthDotJson;
pub use auth::AuthManager;
pub use auth::AuthManagerConfig;
pub use auth::CLIENT_ID;
pub use auth::CODEX_API_KEY_ENV_VAR;
pub use auth::CodexAuth;