remove write_new_auth_json

This commit is contained in:
Eason Goodale
2025-08-12 00:25:50 -07:00
parent 423f92a71b
commit d357fb7dba
3 changed files with 41 additions and 45 deletions

View File

@@ -49,7 +49,10 @@ pub fn try_read_auth_json(auth_file: &Path) -> std::io::Result<AuthDotJson> {
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<String>,
id_token: &str,
access_token: &str,
refresh_token: &str,
account_id: Option<String>,
) -> 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)
}

View File

@@ -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();

View File

@@ -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