-
Notifications
You must be signed in to change notification settings - Fork 14.3k
refactor: unify external auth resolution #31421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pakrym-oai
wants to merge
10
commits into
main
Choose a base branch
from
pakrym/unify-external-auth-resolution
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
74e1df4
refactor: unify external auth resolution
pakrym-oai 1b6c68b
refactor: make external auth own credentials
pakrym-oai 77f76f1
fix: retain message processor timeout import
pakrym-oai 544653f
refactor: tighten external auth state
pakrym-oai 8393b8e
refactor: commit external auth in one step
pakrym-oai 9f8d177
fix: preserve external auth 401 recovery
pakrym-oai ce1f9e4
test: exercise external auth refresh in cloud config
pakrym-oai b31594d
Simplify external auth ownership
pakrym-oai 7077b30
codex: fix CI failure on PR #31421
pakrym-oai 3ae9aab
codex: address PR review feedback (#31421)
pakrym-oai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| use std::sync::Arc; | ||
| use std::sync::RwLock; | ||
|
|
||
| use codex_app_server_protocol::ChatgptAuthTokensRefreshParams; | ||
| use codex_app_server_protocol::ChatgptAuthTokensRefreshReason; | ||
| use codex_app_server_protocol::ChatgptAuthTokensRefreshResponse; | ||
| use codex_app_server_protocol::ServerRequestPayload; | ||
| use codex_login::CodexAuth; | ||
| use codex_login::ExternalAuthFuture; | ||
| use codex_login::auth::ExternalAuth; | ||
| use codex_login::auth::ExternalAuthRefreshContext; | ||
| use codex_login::auth::ExternalAuthRefreshReason; | ||
| use tokio::time::Duration; | ||
| use tokio::time::timeout; | ||
|
|
||
| use crate::outgoing_message::OutgoingMessageSender; | ||
|
|
||
| const EXTERNAL_AUTH_REFRESH_TIMEOUT: Duration = Duration::from_secs(10); | ||
|
|
||
| pub(crate) struct ExternalAuthBridge { | ||
| outgoing: Arc<OutgoingMessageSender>, | ||
| auth: RwLock<CodexAuth>, | ||
| } | ||
|
|
||
| impl ExternalAuthBridge { | ||
| pub(crate) fn new(outgoing: Arc<OutgoingMessageSender>, auth: CodexAuth) -> Self { | ||
| Self { | ||
| outgoing, | ||
| auth: RwLock::new(auth), | ||
| } | ||
| } | ||
|
|
||
| async fn refresh(&self, context: ExternalAuthRefreshContext) -> std::io::Result<CodexAuth> { | ||
| let reason = match context.reason { | ||
| ExternalAuthRefreshReason::Unauthorized => ChatgptAuthTokensRefreshReason::Unauthorized, | ||
| }; | ||
| let params = ChatgptAuthTokensRefreshParams { | ||
| reason, | ||
| previous_account_id: context.previous_account_id, | ||
| }; | ||
|
|
||
| let (request_id, rx) = self | ||
| .outgoing | ||
| .send_request(ServerRequestPayload::ChatgptAuthTokensRefresh(params)) | ||
| .await; | ||
| let result = match timeout(EXTERNAL_AUTH_REFRESH_TIMEOUT, rx).await { | ||
| Ok(result) => { | ||
| let result = result.map_err(|err| { | ||
| std::io::Error::other(format!("auth refresh request canceled: {err}")) | ||
| })?; | ||
| result.map_err(|err| { | ||
| std::io::Error::other(format!( | ||
| "auth refresh request failed: code={} message={}", | ||
| err.code, err.message | ||
| )) | ||
| })? | ||
| } | ||
| Err(_) => { | ||
| let _canceled = self.outgoing.cancel_request(&request_id).await; | ||
| return Err(std::io::Error::other(format!( | ||
| "auth refresh request timed out after {}s", | ||
| EXTERNAL_AUTH_REFRESH_TIMEOUT.as_secs() | ||
| ))); | ||
| } | ||
| }; | ||
|
|
||
| let response: ChatgptAuthTokensRefreshResponse = | ||
| serde_json::from_value(result).map_err(std::io::Error::other)?; | ||
| let auth = CodexAuth::from_external_chatgpt_tokens( | ||
| response.access_token.as_str(), | ||
| response.chatgpt_account_id.as_str(), | ||
| response.chatgpt_plan_type.as_deref(), | ||
| )?; | ||
| *self | ||
| .auth | ||
| .write() | ||
| .map_err(|_| std::io::Error::other("external auth lock is poisoned"))? = auth.clone(); | ||
|
pakrym-oai marked this conversation as resolved.
|
||
| Ok(auth) | ||
| } | ||
| } | ||
|
|
||
| impl ExternalAuth for ExternalAuthBridge { | ||
| fn resolve(&self) -> ExternalAuthFuture<'_, CodexAuth> { | ||
| Box::pin(async { | ||
| self.auth | ||
| .read() | ||
| .map(|auth| auth.clone()) | ||
| .map_err(|_| std::io::Error::other("external auth lock is poisoned")) | ||
| }) | ||
| } | ||
|
|
||
| fn refresh(&self, context: ExternalAuthRefreshContext) -> ExternalAuthFuture<'_, CodexAuth> { | ||
| Box::pin(ExternalAuthBridge::refresh(self, context)) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| use super::*; | ||
| use crate::auth_mode::auth_mode_to_api; | ||
| use crate::external_auth::ExternalAuthBridge; | ||
| use chrono::DateTime; | ||
|
|
||
| mod rate_limit_resets; | ||
|
|
@@ -617,14 +618,19 @@ impl AccountRequestProcessor { | |
| ))); | ||
| } | ||
|
|
||
| login_with_chatgpt_auth_tokens( | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was saving tokens to ephemeral storage directly and had normal reload pick them up instead of having external auth bridge be the source of truth. |
||
| &self.config.codex_home, | ||
| let auth = CodexAuth::from_external_chatgpt_tokens( | ||
| &access_token, | ||
| &chatgpt_account_id, | ||
| chatgpt_plan_type.as_deref(), | ||
| ) | ||
| .map_err(|err| internal_error(format!("failed to set external auth: {err}")))?; | ||
| self.auth_manager.reload().await; | ||
| self.auth_manager | ||
|
pakrym-oai marked this conversation as resolved.
|
||
| .set_external_auth(Arc::new(ExternalAuthBridge::new( | ||
| Arc::clone(&self.outgoing), | ||
| auth, | ||
| ))) | ||
|
pakrym-oai marked this conversation as resolved.
|
||
| .await | ||
| .map_err(|err| internal_error(format!("failed to set external auth: {err}")))?; | ||
| self.config_manager.replace_cloud_config_bundle_loader( | ||
| self.auth_manager.clone(), | ||
| self.config.chatgpt_base_url.clone(), | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.