Skip to content

Commit ea650a9

Browse files
authored
auth: generalize external auth tokens for bearer-only sources (#16286)
## Summary `ExternalAuthRefresher` was still shaped around external ChatGPT auth: `ExternalAuthTokens` always implied ChatGPT account metadata even when a caller only needed a bearer token. This PR generalizes that contract so bearer-only sources are first-class, while keeping the existing ChatGPT paths strict anywhere we persist or rebuild ChatGPT auth state. ## Motivation This is the first step toward #15189. The follow-on provider-auth work needs one shared external-auth contract that can do both of these things: - resolve the current bearer token before a request is sent - return a refreshed bearer token after a `401` That should not require a second token result type just because there is no ChatGPT account metadata attached. ## What Changed - change `ExternalAuthTokens` to carry `access_token` plus optional `ExternalAuthChatgptMetadata` - add helper constructors for bearer-only tokens and ChatGPT-backed tokens - add `ExternalAuthRefresher::resolve()` with a default no-op implementation so refreshers can optionally provide the current token before a request is sent - keep ChatGPT-only persistence strict by continuing to require ChatGPT metadata anywhere the login layer seeds or reloads ChatGPT auth state - update the app-server bridge to construct the new token shape for external ChatGPT auth refreshes ## Testing - `cargo test -p codex-login` --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/openai/codex/pull/16286). * #16288 * #16287 * __->__ #16286
1 parent 19f0d19 commit ea650a9

File tree

4 files changed

+82
-18
lines changed

4 files changed

+82
-18
lines changed

codex-rs/app-server/src/message_processor.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,11 @@ impl ExternalAuthRefresher for ExternalAuthRefreshBridge {
144144
let response: ChatgptAuthTokensRefreshResponse =
145145
serde_json::from_value(result).map_err(std::io::Error::other)?;
146146

147-
Ok(ExternalAuthTokens {
148-
access_token: response.access_token,
149-
chatgpt_account_id: response.chatgpt_account_id,
150-
chatgpt_plan_type: response.chatgpt_plan_type,
151-
})
147+
Ok(ExternalAuthTokens::chatgpt(
148+
response.access_token,
149+
response.chatgpt_account_id,
150+
response.chatgpt_plan_type,
151+
))
152152
}
153153
}
154154

codex-rs/login/src/auth/auth_tests.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,19 @@ fn refresh_failure_is_scoped_to_the_matching_auth_snapshot() {
252252
assert_eq!(manager.refresh_failure_for_auth(&updated_auth), None);
253253
}
254254

255+
#[test]
256+
fn external_auth_tokens_without_chatgpt_metadata_cannot_seed_chatgpt_auth() {
257+
let err = AuthDotJson::from_external_tokens(&ExternalAuthTokens::access_token_only(
258+
"test-access-token",
259+
))
260+
.expect_err("bearer-only external auth should not seed ChatGPT auth");
261+
262+
assert_eq!(
263+
err.to_string(),
264+
"external auth tokens are missing ChatGPT metadata"
265+
);
266+
}
267+
255268
struct AuthFileParams {
256269
openai_api_key: Option<String>,
257270
chatgpt_plan_type: Option<String>,

codex-rs/login/src/auth/manager.rs

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,40 @@ pub enum RefreshTokenError {
9393
#[derive(Clone, Debug, PartialEq, Eq)]
9494
pub struct ExternalAuthTokens {
9595
pub access_token: String,
96-
pub chatgpt_account_id: String,
97-
pub chatgpt_plan_type: Option<String>,
96+
pub chatgpt_metadata: Option<ExternalAuthChatgptMetadata>,
97+
}
98+
99+
#[derive(Clone, Debug, PartialEq, Eq)]
100+
pub struct ExternalAuthChatgptMetadata {
101+
pub account_id: String,
102+
pub plan_type: Option<String>,
103+
}
104+
105+
impl ExternalAuthTokens {
106+
pub fn access_token_only(access_token: impl Into<String>) -> Self {
107+
Self {
108+
access_token: access_token.into(),
109+
chatgpt_metadata: None,
110+
}
111+
}
112+
113+
pub fn chatgpt(
114+
access_token: impl Into<String>,
115+
chatgpt_account_id: impl Into<String>,
116+
chatgpt_plan_type: Option<String>,
117+
) -> Self {
118+
Self {
119+
access_token: access_token.into(),
120+
chatgpt_metadata: Some(ExternalAuthChatgptMetadata {
121+
account_id: chatgpt_account_id.into(),
122+
plan_type: chatgpt_plan_type,
123+
}),
124+
}
125+
}
126+
127+
pub fn chatgpt_metadata(&self) -> Option<&ExternalAuthChatgptMetadata> {
128+
self.chatgpt_metadata.as_ref()
129+
}
98130
}
99131

100132
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -110,6 +142,10 @@ pub struct ExternalAuthRefreshContext {
110142

111143
#[async_trait]
112144
pub trait ExternalAuthRefresher: Send + Sync {
145+
async fn resolve(&self) -> std::io::Result<Option<ExternalAuthTokens>> {
146+
Ok(None)
147+
}
148+
113149
async fn refresh(
114150
&self,
115151
context: ExternalAuthRefreshContext,
@@ -736,11 +772,16 @@ fn refresh_token_endpoint() -> String {
736772

737773
impl AuthDotJson {
738774
fn from_external_tokens(external: &ExternalAuthTokens) -> std::io::Result<Self> {
775+
let Some(chatgpt_metadata) = external.chatgpt_metadata() else {
776+
return Err(std::io::Error::other(
777+
"external auth tokens are missing ChatGPT metadata",
778+
));
779+
};
739780
let mut token_info =
740781
parse_chatgpt_jwt_claims(&external.access_token).map_err(std::io::Error::other)?;
741-
token_info.chatgpt_account_id = Some(external.chatgpt_account_id.clone());
742-
token_info.chatgpt_plan_type = external
743-
.chatgpt_plan_type
782+
token_info.chatgpt_account_id = Some(chatgpt_metadata.account_id.clone());
783+
token_info.chatgpt_plan_type = chatgpt_metadata
784+
.plan_type
744785
.as_deref()
745786
.map(InternalPlanType::from_raw_value)
746787
.or(token_info.chatgpt_plan_type)
@@ -749,7 +790,7 @@ impl AuthDotJson {
749790
id_token: token_info,
750791
access_token: external.access_token.clone(),
751792
refresh_token: String::new(),
752-
account_id: Some(external.chatgpt_account_id.clone()),
793+
account_id: Some(chatgpt_metadata.account_id.clone()),
753794
};
754795

755796
Ok(Self {
@@ -765,11 +806,11 @@ impl AuthDotJson {
765806
chatgpt_account_id: &str,
766807
chatgpt_plan_type: Option<&str>,
767808
) -> std::io::Result<Self> {
768-
let external = ExternalAuthTokens {
769-
access_token: access_token.to_string(),
770-
chatgpt_account_id: chatgpt_account_id.to_string(),
771-
chatgpt_plan_type: chatgpt_plan_type.map(str::to_string),
772-
};
809+
let external = ExternalAuthTokens::chatgpt(
810+
access_token,
811+
chatgpt_account_id,
812+
chatgpt_plan_type.map(str::to_string),
813+
);
773814
Self::from_external_tokens(&external)
774815
}
775816

@@ -1457,13 +1498,18 @@ impl AuthManager {
14571498
};
14581499

14591500
let refreshed = refresher.refresh(context).await?;
1501+
let Some(chatgpt_metadata) = refreshed.chatgpt_metadata() else {
1502+
return Err(RefreshTokenError::Transient(std::io::Error::other(
1503+
"external auth refresh did not return ChatGPT metadata",
1504+
)));
1505+
};
14601506
if let Some(expected_workspace_id) = forced_chatgpt_workspace_id.as_deref()
1461-
&& refreshed.chatgpt_account_id != expected_workspace_id
1507+
&& chatgpt_metadata.account_id != expected_workspace_id
14621508
{
14631509
return Err(RefreshTokenError::Transient(std::io::Error::other(
14641510
format!(
14651511
"external auth refresh returned workspace {:?}, expected {expected_workspace_id:?}",
1466-
refreshed.chatgpt_account_id,
1512+
chatgpt_metadata.account_id,
14671513
),
14681514
)));
14691515
}

codex-rs/login/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ pub use auth::AuthManager;
2222
pub use auth::CLIENT_ID;
2323
pub use auth::CODEX_API_KEY_ENV_VAR;
2424
pub use auth::CodexAuth;
25+
pub use auth::ExternalAuthChatgptMetadata;
26+
pub use auth::ExternalAuthRefreshContext;
27+
pub use auth::ExternalAuthRefreshReason;
28+
pub use auth::ExternalAuthRefresher;
29+
pub use auth::ExternalAuthTokens;
2530
pub use auth::OPENAI_API_KEY_ENV_VAR;
2631
pub use auth::REFRESH_TOKEN_URL_OVERRIDE_ENV_VAR;
2732
pub use auth::RefreshTokenError;

0 commit comments

Comments
 (0)