mirror of
https://github.com/openai/codex.git
synced 2026-10-21 17:48:44 +00:00
simplify
This commit is contained in:
@@ -326,9 +326,8 @@ impl AccountRequestProcessor {
|
||||
config.forced_chatgpt_workspace_id.clone(),
|
||||
config.cli_auth_credentials_store_mode,
|
||||
)
|
||||
.with_network_config(config.network.as_ref())
|
||||
};
|
||||
let mut opts = opts;
|
||||
opts.set_network_config(config.network.as_ref());
|
||||
#[cfg(debug_assertions)]
|
||||
let opts = {
|
||||
let mut opts = opts;
|
||||
|
||||
@@ -119,13 +119,13 @@ pub async fn login_with_chatgpt(
|
||||
cli_auth_credentials_store_mode: AuthCredentialsStoreMode,
|
||||
network: Option<&codex_config::types::NetworkConfigToml>,
|
||||
) -> std::io::Result<()> {
|
||||
let mut opts = ServerOptions::new(
|
||||
let opts = ServerOptions::new(
|
||||
codex_home,
|
||||
CLIENT_ID.to_string(),
|
||||
forced_chatgpt_workspace_id,
|
||||
cli_auth_credentials_store_mode,
|
||||
);
|
||||
opts.set_network_config(network);
|
||||
)
|
||||
.with_network_config(network);
|
||||
let server = run_login_server(opts)?;
|
||||
|
||||
print_login_server_start(server.actual_port, &server.auth_url);
|
||||
@@ -285,8 +285,8 @@ pub async fn run_login_with_device_code(
|
||||
client_id.unwrap_or(CLIENT_ID.to_string()),
|
||||
forced_chatgpt_workspace_id,
|
||||
config.cli_auth_credentials_store_mode,
|
||||
);
|
||||
opts.set_network_config(config.network.as_ref());
|
||||
)
|
||||
.with_network_config(config.network.as_ref());
|
||||
if let Some(iss) = issuer_base_url {
|
||||
opts.issuer = iss;
|
||||
}
|
||||
@@ -325,8 +325,8 @@ pub async fn run_login_with_device_code_fallback_to_browser(
|
||||
client_id.unwrap_or(CLIENT_ID.to_string()),
|
||||
forced_chatgpt_workspace_id,
|
||||
config.cli_auth_credentials_store_mode,
|
||||
);
|
||||
opts.set_network_config(config.network.as_ref());
|
||||
)
|
||||
.with_network_config(config.network.as_ref());
|
||||
if let Some(iss) = issuer_base_url {
|
||||
opts.issuer = iss;
|
||||
}
|
||||
|
||||
@@ -254,14 +254,28 @@ pub fn try_build_reqwest_client_with_proxy_config(
|
||||
return build_reqwest_client_with_custom_ca(builder).map_err(Into::into);
|
||||
}
|
||||
|
||||
build_reqwest_client_for_route(
|
||||
builder,
|
||||
DEFAULT_AUTH_ROUTE_URL,
|
||||
RouteTarget::Auth,
|
||||
build_auth_reqwest_client_with_builder(builder, DEFAULT_AUTH_ROUTE_URL, outbound_proxy_config)
|
||||
}
|
||||
|
||||
pub(crate) fn build_auth_reqwest_client_with_proxy_config(
|
||||
endpoint: &str,
|
||||
outbound_proxy_config: Option<&OutboundProxyConfig>,
|
||||
) -> Result<reqwest::Client, BuildProxiedHttpClientError> {
|
||||
build_auth_reqwest_client_with_builder(
|
||||
reqwest::Client::builder(),
|
||||
endpoint,
|
||||
outbound_proxy_config,
|
||||
)
|
||||
}
|
||||
|
||||
fn build_auth_reqwest_client_with_builder(
|
||||
builder: reqwest::ClientBuilder,
|
||||
endpoint: &str,
|
||||
outbound_proxy_config: Option<&OutboundProxyConfig>,
|
||||
) -> Result<reqwest::Client, BuildProxiedHttpClientError> {
|
||||
build_reqwest_client_for_route(builder, endpoint, RouteTarget::Auth, outbound_proxy_config)
|
||||
}
|
||||
|
||||
pub fn default_headers() -> HeaderMap {
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert("originator", originator().header_value);
|
||||
|
||||
@@ -6,10 +6,9 @@ use serde::de::{self};
|
||||
use std::time::Duration;
|
||||
use std::time::Instant;
|
||||
|
||||
use crate::default_client::build_auth_reqwest_client_with_proxy_config;
|
||||
use crate::pkce::PkceCodes;
|
||||
use crate::server::ServerOptions;
|
||||
use codex_client::RouteTarget;
|
||||
use codex_client::build_reqwest_client_for_route;
|
||||
use std::io;
|
||||
|
||||
const ANSI_BLUE: &str = "\x1b[94m";
|
||||
@@ -161,10 +160,8 @@ pub async fn request_device_code(opts: &ServerOptions) -> std::io::Result<Device
|
||||
let base_url = opts.issuer.trim_end_matches('/');
|
||||
let api_base_url = format!("{base_url}/api/accounts");
|
||||
let user_code_endpoint = format!("{api_base_url}/deviceauth/usercode");
|
||||
let client = build_reqwest_client_for_route(
|
||||
reqwest::Client::builder(),
|
||||
let client = build_auth_reqwest_client_with_proxy_config(
|
||||
&user_code_endpoint,
|
||||
RouteTarget::Auth,
|
||||
opts.outbound_proxy_config.as_ref(),
|
||||
)?;
|
||||
let uc = request_user_code(&client, &api_base_url, &opts.client_id).await?;
|
||||
@@ -184,10 +181,8 @@ pub async fn complete_device_code_login(
|
||||
let base_url = opts.issuer.trim_end_matches('/');
|
||||
let api_base_url = format!("{base_url}/api/accounts");
|
||||
let token_endpoint = format!("{api_base_url}/deviceauth/token");
|
||||
let client = build_reqwest_client_for_route(
|
||||
reqwest::Client::builder(),
|
||||
let client = build_auth_reqwest_client_with_proxy_config(
|
||||
&token_endpoint,
|
||||
RouteTarget::Auth,
|
||||
opts.outbound_proxy_config.as_ref(),
|
||||
)?;
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@ use crate::auth::load_auth_dot_json;
|
||||
use crate::auth::revoke_auth_tokens_with_proxy_config;
|
||||
use crate::auth::save_auth;
|
||||
use crate::auth::should_revoke_auth_tokens;
|
||||
use crate::default_client::build_auth_reqwest_client_with_proxy_config;
|
||||
use crate::default_client::originator;
|
||||
use crate::outbound_proxy::outbound_proxy_config_from_network_config;
|
||||
use crate::pkce::PkceCodes;
|
||||
@@ -39,8 +40,6 @@ use base64::Engine;
|
||||
use chrono::Utc;
|
||||
use codex_app_server_protocol::AuthMode;
|
||||
use codex_client::OutboundProxyConfig;
|
||||
use codex_client::RouteTarget;
|
||||
use codex_client::build_reqwest_client_for_route;
|
||||
use codex_client::emit_auth_http_status;
|
||||
use codex_client::emit_auth_network_environment_snapshot;
|
||||
use codex_client::emit_auth_transport_failure;
|
||||
@@ -104,8 +103,9 @@ impl ServerOptions {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_network_config(&mut self, network: Option<&NetworkConfigToml>) {
|
||||
pub fn with_network_config(mut self, network: Option<&NetworkConfigToml>) -> Self {
|
||||
self.outbound_proxy_config = network.map(outbound_proxy_config_from_network_config);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
@@ -754,12 +754,8 @@ pub(crate) async fn exchange_code_for_tokens(
|
||||
|
||||
let token_endpoint = format!("{}/oauth/token", issuer.trim_end_matches('/'));
|
||||
emit_auth_network_environment_snapshot(/*operation*/ "oauth_token_exchange");
|
||||
let client = build_reqwest_client_for_route(
|
||||
reqwest::Client::builder(),
|
||||
&token_endpoint,
|
||||
RouteTarget::Auth,
|
||||
outbound_proxy_config,
|
||||
)?;
|
||||
let client =
|
||||
build_auth_reqwest_client_with_proxy_config(&token_endpoint, outbound_proxy_config)?;
|
||||
info!(
|
||||
issuer = %sanitize_url_for_logging(issuer),
|
||||
token_endpoint = %sanitize_url_for_logging(&token_endpoint),
|
||||
@@ -1169,12 +1165,8 @@ pub(crate) async fn obtain_api_key(
|
||||
}
|
||||
let token_endpoint = format!("{}/oauth/token", issuer.trim_end_matches('/'));
|
||||
emit_auth_network_environment_snapshot(/*operation*/ "api_key_exchange");
|
||||
let client = build_reqwest_client_for_route(
|
||||
reqwest::Client::builder(),
|
||||
&token_endpoint,
|
||||
RouteTarget::Auth,
|
||||
outbound_proxy_config,
|
||||
)?;
|
||||
let client =
|
||||
build_auth_reqwest_client_with_proxy_config(&token_endpoint, outbound_proxy_config)?;
|
||||
let resp = client
|
||||
.post(token_endpoint)
|
||||
.header("Content-Type", "application/x-www-form-urlencoded")
|
||||
|
||||
Reference in New Issue
Block a user