Port login server to rust (#2294)
Port the login server to rust. --------- Co-authored-by: pakrym-oai <pakrym@openai.com>
This commit is contained in:
@@ -1,20 +1,54 @@
|
||||
use std::env;
|
||||
|
||||
use codex_common::CliConfigOverrides;
|
||||
use codex_core::config::Config;
|
||||
use codex_core::config::ConfigOverrides;
|
||||
use codex_login::AuthMode;
|
||||
use codex_login::CLIENT_ID;
|
||||
use codex_login::CodexAuth;
|
||||
use codex_login::LoginServerInfo;
|
||||
use codex_login::OPENAI_API_KEY_ENV_VAR;
|
||||
use codex_login::ServerOptions;
|
||||
use codex_login::login_with_api_key;
|
||||
use codex_login::login_with_chatgpt;
|
||||
use codex_login::logout;
|
||||
use codex_login::run_server_blocking_with_notify;
|
||||
use std::env;
|
||||
use std::path::Path;
|
||||
use std::sync::mpsc;
|
||||
|
||||
pub async fn login_with_chatgpt(codex_home: &Path) -> std::io::Result<()> {
|
||||
let (tx, rx) = mpsc::channel::<LoginServerInfo>();
|
||||
let client_id = CLIENT_ID;
|
||||
let codex_home = codex_home.to_path_buf();
|
||||
tokio::spawn(async move {
|
||||
match rx.recv() {
|
||||
Ok(LoginServerInfo {
|
||||
auth_url,
|
||||
actual_port,
|
||||
}) => {
|
||||
eprintln!(
|
||||
"Starting local login server on http://localhost:{actual_port}.\nIf your browser did not open, navigate to this URL to authenticate:\n\n{auth_url}",
|
||||
);
|
||||
}
|
||||
_ => {
|
||||
tracing::error!("Failed to receive login server info");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
tokio::task::spawn_blocking(move || {
|
||||
let opts = ServerOptions::new(&codex_home, client_id);
|
||||
run_server_blocking_with_notify(opts, Some(tx), None)
|
||||
})
|
||||
.await
|
||||
.map_err(std::io::Error::other)??;
|
||||
|
||||
eprintln!("Successfully logged in");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn run_login_with_chatgpt(cli_config_overrides: CliConfigOverrides) -> ! {
|
||||
let config = load_config_or_exit(cli_config_overrides);
|
||||
|
||||
let capture_output = false;
|
||||
match login_with_chatgpt(&config.codex_home, capture_output).await {
|
||||
match login_with_chatgpt(&config.codex_home).await {
|
||||
Ok(_) => {
|
||||
eprintln!("Successfully logged in");
|
||||
std::process::exit(0);
|
||||
|
||||
Reference in New Issue
Block a user