From d357fb7dba66be7ecf33ef413b0af18b639ded91 Mon Sep 17 00:00:00 2001 From: Eason Goodale Date: Tue, 12 Aug 2025 00:25:50 -0700 Subject: [PATCH] remove write_new_auth_json --- codex-rs/login/src/auth_store.rs | 32 ++++---------------------------- codex-rs/login/src/lib_tests.rs | 29 ++++++++++++++++++++--------- codex-rs/login/src/server.rs | 25 +++++++++++++++++-------- 3 files changed, 41 insertions(+), 45 deletions(-) diff --git a/codex-rs/login/src/auth_store.rs b/codex-rs/login/src/auth_store.rs index ea5e0f1633..fdab2bf90f 100644 --- a/codex-rs/login/src/auth_store.rs +++ b/codex-rs/login/src/auth_store.rs @@ -49,7 +49,10 @@ pub fn try_read_auth_json(auth_file: &Path) -> std::io::Result { Ok(auth_dot_json) } -fn write_auth_json(auth_file: &Path, auth_dot_json: &AuthDotJson) -> std::io::Result<()> { +pub(crate) fn write_auth_json( + auth_file: &Path, + auth_dot_json: &AuthDotJson, +) -> std::io::Result<()> { let json_data = serde_json::to_string_pretty(auth_dot_json)?; let mut options = OpenOptions::new(); options.truncate(true).write(true).create(true); @@ -144,30 +147,3 @@ pub(crate) fn update_tokens( write_auth_json(auth_file, &auth)?; try_read_auth_json(auth_file) } - -/// Write a fresh auth.json to `codex_home` with the provided values. -/// Creates the directory if needed. -pub(crate) fn write_new_auth_json( - codex_home: &Path, - api_key: Option, - id_token: &str, - access_token: &str, - refresh_token: &str, - account_id: Option, -) -> std::io::Result<()> { - std::fs::create_dir_all(codex_home)?; - let auth_file = get_auth_file(codex_home); - let tokens = crate::token_data::TokenData::from_raw( - id_token.to_string(), - access_token.to_string(), - refresh_token.to_string(), - account_id, - ) - .map_err(std::io::Error::other)?; - let auth_dot_json = AuthDotJson { - openai_api_key: api_key, - tokens: Some(tokens), - last_refresh: Some(Utc::now()), - }; - write_auth_json(&auth_file, &auth_dot_json) -} diff --git a/codex-rs/login/src/lib_tests.rs b/codex-rs/login/src/lib_tests.rs index ef7bb17e12..a7b7a2e3ca 100644 --- a/codex-rs/login/src/lib_tests.rs +++ b/codex-rs/login/src/lib_tests.rs @@ -289,12 +289,18 @@ fn update_tokens_preserves_id_token_as_string() { } #[test] -fn write_new_auth_json_is_python_compatible_shape() { +fn write_auth_json_is_python_compatible_shape() { let dir = tempdir().unwrap(); let id_token = { #[derive(Serialize)] - struct Header { alg: &'static str, typ: &'static str } - let header = Header { alg: "none", typ: "JWT" }; + struct Header { + alg: &'static str, + typ: &'static str, + } + let header = Header { + alg: "none", + typ: "JWT", + }; let payload = serde_json::json!({"sub": "123"}); let b64 = |b: &[u8]| base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(b); let header_b64 = b64(&serde_json::to_vec(&header).unwrap()); @@ -303,15 +309,20 @@ fn write_new_auth_json_is_python_compatible_shape() { format!("{header_b64}.{payload_b64}.{signature_b64}") }; - crate::auth_store::write_new_auth_json( - dir.path(), - Some("sk-test".to_string()), - &id_token, - "a1", - "r1", + let tokens = crate::token_data::TokenData::from_raw( + id_token.clone(), + "a1".to_string(), + "r1".to_string(), Some("acc".to_string()), ) .unwrap(); + let auth = crate::auth_store::AuthDotJson { + openai_api_key: Some("sk-test".to_string()), + tokens: Some(tokens), + last_refresh: Some(chrono::Utc::now()), + }; + crate::auth_store::write_auth_json(&crate::auth_store::get_auth_file(dir.path()), &auth) + .unwrap(); let raw = std::fs::read_to_string(dir.path().join("auth.json")).unwrap(); let val: serde_json::Value = serde_json::from_str(&raw).unwrap(); diff --git a/codex-rs/login/src/server.rs b/codex-rs/login/src/server.rs index 879128dede..b992f3b36e 100644 --- a/codex-rs/login/src/server.rs +++ b/codex-rs/login/src/server.rs @@ -13,9 +13,12 @@ use tiny_http::Server; use url::Url; use url::form_urlencoded; -use crate::auth_store::write_new_auth_json; +use crate::auth_store::AuthDotJson; +use crate::auth_store::get_auth_file; +use crate::auth_store::write_auth_json; use crate::pkce::generate_pkce; use crate::success_url::build_success_url; +use crate::token_data::TokenData; use crate::token_data::extract_login_context_from_tokens; use tracing::error; use tracing::trace; @@ -374,14 +377,20 @@ pub fn process_callback_headless( let api_key = None; - write_new_auth_json( - &opts.codex_home, - api_key.clone(), - &id_token, - &access_token, - &refresh_token, + let tokens_struct = TokenData::from_raw( + id_token.clone(), + access_token.clone(), + refresh_token.clone(), account_id, - )?; + ) + .map_err(std::io::Error::other)?; + let auth = AuthDotJson { + openai_api_key: api_key.clone(), + tokens: Some(tokens_struct), + last_refresh: Some(chrono::Utc::now()), + }; + let auth_file = get_auth_file(&opts.codex_home); + write_auth_json(&auth_file, &auth)?; // Intentionally not redeeming credits here