mirror of
https://github.com/openai/codex.git
synced 2026-11-15 21:50:46 +00:00
Bind reusable backend clients to runtime authentication (#59255)
## Why Reusable clients need fresh authentication for each request while preserving the credential source and transport policy selected when the client was created. ## What changed - Add runtime-aware backend client construction and per-request authentication. Allow token refresh within the original credential source, reject credential replacement, and validate destination and account/organization selectors. - Let runtime authentication finish persisting rotated tokens when the requesting task is cancelled. - Apply asynchronous authentication to file creation and finalization requests, generating fresh assertions while keeping credentials off blob uploads. - Preserve direct sandbox routing without enabling legacy custom-CA fallback for protected clients that reject redirects. - Exclude login account metadata from model cache identities when provider bearer credentials override the login, and record authentication selection outcomes and duration. ## Testing Add regression coverage for credential refresh and replacement, organization selection, destination restrictions, transport policy preservation, cancellation during token rotation, credential stripping on redirects, and file upload authentication in both runtime authentication modes. GitOrigin-RevId: d045595e869dfcdfd9f705a6228cf0eb4c2d2c12
This commit is contained in:
@@ -135,16 +135,25 @@ impl RuntimeAuthManager {
|
||||
}
|
||||
|
||||
/// Select auth and a runtime for the requested organization and project.
|
||||
#[expect(
|
||||
clippy::await_holding_invalid_type,
|
||||
reason = "runtime initialization serializes registration and refresh for one credential source"
|
||||
)]
|
||||
pub async fn auth_with_selection(
|
||||
&self,
|
||||
selection: &PlatformResourceSelection,
|
||||
) -> Result<RuntimeAuth, RuntimeAuthError> {
|
||||
self.select_auth(selection, /*expected_source*/ None).await
|
||||
}
|
||||
|
||||
#[expect(
|
||||
clippy::await_holding_invalid_type,
|
||||
reason = "runtime initialization serializes registration and refresh for one credential source"
|
||||
)]
|
||||
async fn select_auth(
|
||||
&self,
|
||||
selection: &PlatformResourceSelection,
|
||||
expected_source: Option<u64>,
|
||||
) -> Result<RuntimeAuth, RuntimeAuthError> {
|
||||
// This initializer's lock can span IO. No AuthManager lock is held across registration.
|
||||
let mut state = self.state.lock().await;
|
||||
self.check_source(expected_source)?;
|
||||
let (auth, http_client_factory) = self
|
||||
.credentials
|
||||
.auth_with_http_client_factory()
|
||||
@@ -152,7 +161,13 @@ impl RuntimeAuthManager {
|
||||
.ok_or(RuntimeAuthError::Unavailable)?;
|
||||
let mut recovery = self.credentials.unauthorized_recovery();
|
||||
let mut result = self
|
||||
.auth_once(&mut state, auth, http_client_factory, selection)
|
||||
.auth_once(
|
||||
&mut state,
|
||||
auth,
|
||||
http_client_factory,
|
||||
selection,
|
||||
expected_source,
|
||||
)
|
||||
.await;
|
||||
while matches!(
|
||||
&result,
|
||||
@@ -164,6 +179,7 @@ impl RuntimeAuthManager {
|
||||
))
|
||||
) && recovery.has_next()
|
||||
{
|
||||
self.check_source(expected_source)?;
|
||||
recovery.next().await.map_err(|error| match error {
|
||||
RefreshTokenError::Policy(denied) => RuntimeAuthError::Policy(denied),
|
||||
RefreshTokenError::Permanent(_) | RefreshTokenError::Transient(_) => {
|
||||
@@ -176,7 +192,13 @@ impl RuntimeAuthManager {
|
||||
.await
|
||||
.ok_or(RuntimeAuthError::Unavailable)?;
|
||||
result = self
|
||||
.auth_once(&mut state, auth, http_client_factory, selection)
|
||||
.auth_once(
|
||||
&mut state,
|
||||
auth,
|
||||
http_client_factory,
|
||||
selection,
|
||||
expected_source,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
result
|
||||
@@ -188,10 +210,12 @@ impl RuntimeAuthManager {
|
||||
auth: CodexAuth,
|
||||
http_client_factory: HttpClientFactory,
|
||||
selection: &PlatformResourceSelection,
|
||||
expected_source: Option<u64>,
|
||||
) -> Result<RuntimeAuth, RuntimeAuthError> {
|
||||
let actor_auth = actor_authorization(&auth, selection)?;
|
||||
let http_client_factory = http_client_factory.with_sensitive_header_protection();
|
||||
let change_state = self.auth_state(&auth)?;
|
||||
self.check_source(expected_source)?;
|
||||
// Registered runtimes can serve different projects. Pending and denied
|
||||
// registrations retain their original scope and idempotency key.
|
||||
if state.as_ref().is_some_and(|cached| {
|
||||
@@ -269,6 +293,45 @@ impl RuntimeAuthManager {
|
||||
}
|
||||
}
|
||||
|
||||
/// Resolve auth for work whose routing, cache key, or payload uses a captured credential.
|
||||
/// A same-source refresh is allowed; a different source must not take over that work.
|
||||
/// This binds credential identity, not when the caller captured it. Restoring the exact
|
||||
/// same credential can select a new runtime after logout; a different source cannot.
|
||||
pub async fn auth_for(
|
||||
&self,
|
||||
expected: &CodexAuth,
|
||||
selection: &PlatformResourceSelection,
|
||||
) -> Result<RuntimeAuth, RuntimeAuthError> {
|
||||
let source = self
|
||||
.credentials
|
||||
.auth_change_state_for(expected)
|
||||
.ok_or(RuntimeAuthError::Changed)?;
|
||||
self.auth_for_source(source.source_generation, selection)
|
||||
.await
|
||||
}
|
||||
|
||||
/// Select another request within a reusable client's original credential source.
|
||||
pub async fn auth_for_source(
|
||||
&self,
|
||||
source_generation: u64,
|
||||
selection: &PlatformResourceSelection,
|
||||
) -> Result<RuntimeAuth, RuntimeAuthError> {
|
||||
self.select_auth(selection, Some(source_generation)).await
|
||||
}
|
||||
|
||||
fn check_source(&self, expected: Option<u64>) -> Result<(), RuntimeAuthError> {
|
||||
if expected.is_some_and(|expected| {
|
||||
self.credentials
|
||||
.auth_change_state_receiver()
|
||||
.borrow()
|
||||
.source_generation
|
||||
!= expected
|
||||
}) {
|
||||
return Err(RuntimeAuthError::Changed);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn auth_state(&self, auth: &CodexAuth) -> Result<AuthChangeState, RuntimeAuthError> {
|
||||
let state = self
|
||||
.credentials
|
||||
|
||||
Reference in New Issue
Block a user