Refresh ChatGPT auth token - #2484
Merged
Merged
Conversation
gpeal
reviewed
Aug 20, 2025
gpeal
left a comment
Contributor
There was a problem hiding this comment.
Few ideas.
For what it's worth, the more correct thing to do (which matches Android but is out of scope for a quick fix) is to have an http interceptor for all chatgpt requests that intercepts 401s, does a token refresh, then retries the original request. Then it happens transparently and on-demand for all requests.
Comment on lines
+292
to
+315
| let default_sleep = Duration::from_secs(55 * 60); | ||
| loop { | ||
| // Determine next refresh time from access_token exp - 5 minutes. | ||
| let maybe_exp; | ||
| { | ||
| let auth_json = auth.get_current_auth_json(); | ||
| if let Some(AuthDotJson { | ||
| tokens: Some(tokens), | ||
| .. | ||
| }) = auth_json | ||
| { | ||
| let exp = decode_jwt_exp(&tokens.access_token); | ||
| maybe_exp = exp.map(|dt| dt - chrono::Duration::minutes(5)); | ||
| } else { | ||
| maybe_exp = None; | ||
| } | ||
| } | ||
|
|
||
| let now = chrono::Utc::now(); | ||
| let sleep_for = match maybe_exp { | ||
| Some(when) if when > now => (when - now).to_std().unwrap_or(Duration::from_secs(0)), | ||
| Some(_) => Duration::from_secs(0), | ||
| None => default_sleep, | ||
| }; |
Contributor
There was a problem hiding this comment.
I think this would read cleaner if you broke out a couple of functions:
time_until_expiryexpiry_sleep(time_until_expiry): Option<…>which would do the -5 minutes and also potentially return None if it's >1 day in the future or something
| .await | ||
| { | ||
| Ok(updated) => { | ||
| #[allow(clippy::unwrap_used)] |
| } | ||
| break; | ||
| } | ||
| Err(_e) => {} |
| write_auth_json(&get_auth_file(codex_home), &auth_dot_json) | ||
| } | ||
|
|
||
| async fn run_auto_refresh_loop(auth: CodexAuth) { |
Contributor
There was a problem hiding this comment.
You can potentially replace all of this with a change to this check to auto-refresh if the token expires in the next ~day.
easong-openai
approved these changes
Aug 20, 2025
seratch
added a commit
that referenced
this pull request
Aug 20, 2025
seratch
added a commit
that referenced
this pull request
Aug 25, 2025
swordfish444
pushed a commit
to swordfish444/codex
that referenced
this pull request
Feb 26, 2026
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
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
ChatGPT token's live for only 1 hour. If the session is longer we don't refresh the token. We should get the expiry timestamp and attempt to refresh before it.