mirror of
https://github.com/openai/codex.git
synced 2026-11-22 23:00:42 +00:00
## What changed Add a SHA-256 identity derived from provider routing, headers, and authentication scope. Return it with each model catalog response and persist it in `ModelsCacheEntry`. ChatGPT credentials with stable account and user metadata retain the same identity across token refreshes; opaque API credentials contribute to the digest. ## Testing Add identity tests covering account, user, email, plan, auth mode, provider routing, headers, and API credential changes, plus stability across ChatGPT token refreshes. Update cache tests to include the persisted identity. GitOrigin-RevId: 3f51c6cabcb01bc03505150a768a61dfe5d6569f
87 lines
3.4 KiB
Rust
87 lines
3.4 KiB
Rust
//! Model catalog cache identity follows provider routing and effective auth.
|
|
//! Only a digest is persisted; access tokens for ChatGPT are excluded so token
|
|
//! rotation does not discard a catalog for the same account, user, and plan.
|
|
|
|
use codex_login::CodexAuth;
|
|
use codex_model_provider_info::ModelProviderInfo;
|
|
use codex_protocol::error::Result as CoreResult;
|
|
use sha2::Digest;
|
|
use sha2::Sha256;
|
|
|
|
use crate::auth::resolve_provider_auth;
|
|
use crate::provider::enforce_managed_residency;
|
|
|
|
pub(crate) fn identity(
|
|
provider_info: &ModelProviderInfo,
|
|
auth: Option<&CodexAuth>,
|
|
) -> CoreResult<String> {
|
|
let mut provider = provider_info.to_api_provider(auth.map(CodexAuth::auth_mode))?;
|
|
enforce_managed_residency(&mut provider);
|
|
let mut digest = Sha256::new();
|
|
// Length-prefix every field to avoid ambiguity between adjacent values.
|
|
let mut field = |value: &[u8]| {
|
|
digest.update((value.len() as u64).to_le_bytes());
|
|
digest.update(value);
|
|
};
|
|
field(b"models-cache-v1");
|
|
field(provider.name.as_bytes());
|
|
field(provider.base_url.as_bytes());
|
|
let mut query: Vec<_> = provider.query_params.iter().flatten().collect();
|
|
query.sort();
|
|
field(&(query.len() as u64).to_le_bytes());
|
|
for (name, value) in query {
|
|
field(name.as_bytes());
|
|
field(value.as_bytes());
|
|
}
|
|
field(&[u8::from(provider_info.requires_openai_auth)]);
|
|
field(&[u8::from(provider_info.has_command_auth())]);
|
|
field(format!("{:?}", auth.map(CodexAuth::api_auth_mode)).as_bytes());
|
|
if let Some(auth) = auth {
|
|
field(format!("{:?}", auth.get_account_id()).as_bytes());
|
|
field(format!("{:?}", auth.get_chatgpt_user_id()).as_bytes());
|
|
field(format!("{:?}", auth.get_account_email()).as_bytes());
|
|
field(format!("{:?}", auth.account_plan_type()).as_bytes());
|
|
field(
|
|
format!(
|
|
"{:?}",
|
|
auth.get_token_data()
|
|
.ok()
|
|
.and_then(|tokens| tokens.id_token.get_chatgpt_plan_type_raw())
|
|
)
|
|
.as_bytes(),
|
|
);
|
|
field(&[u8::from(auth.is_fedramp_account())]);
|
|
}
|
|
let has_stable_account = auth.is_some_and(|auth| {
|
|
matches!(
|
|
auth,
|
|
CodexAuth::Chatgpt(_) | CodexAuth::ChatgptAuthTokens(_) | CodexAuth::AgentIdentity(_)
|
|
) && auth.get_account_id().is_some()
|
|
&& (auth.get_chatgpt_user_id().is_some() || auth.get_account_email().is_some())
|
|
});
|
|
let explicit_bearer =
|
|
provider_info.api_key()?.is_some() || provider_info.experimental_bearer_token.is_some();
|
|
// Command auth also returns opaque API keys. A changed key may belong to a
|
|
// different account, so only credentials with stable owner metadata can be reused.
|
|
if !has_stable_account || explicit_bearer {
|
|
let headers = resolve_provider_auth(auth, provider_info)?.to_auth_headers();
|
|
provider.headers.extend(headers);
|
|
}
|
|
let mut headers: Vec<_> = provider.headers.iter().collect();
|
|
headers.sort_by(|(a, av), (b, bv)| {
|
|
a.as_str()
|
|
.cmp(b.as_str())
|
|
.then(av.as_bytes().cmp(bv.as_bytes()))
|
|
});
|
|
field(&(headers.len() as u64).to_le_bytes());
|
|
for (name, value) in headers {
|
|
field(name.as_str().as_bytes());
|
|
field(value.as_bytes());
|
|
}
|
|
Ok(format!("{:x}", digest.finalize()))
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[path = "models_identity_tests.rs"]
|
|
mod tests;
|