Skip to content

Refresh ChatGPT auth token - #2484

Merged
aibrahim-oai merged 9 commits into
mainfrom
refresh-token
Aug 20, 2025
Merged

Refresh ChatGPT auth token#2484
aibrahim-oai merged 9 commits into
mainfrom
refresh-token

Conversation

@aibrahim-oai

Copy link
Copy Markdown
Contributor

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.

@gpeal gpeal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 thread codex-rs/login/src/lib.rs Outdated
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,
};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would read cleaner if you broke out a couple of functions:

  1. time_until_expiry
  2. expiry_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

Comment thread codex-rs/login/src/lib.rs Outdated
.await
{
Ok(updated) => {
#[allow(clippy::unwrap_used)]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is unused here?

Comment thread codex-rs/login/src/lib.rs Outdated
}
break;
}
Err(_e) => {}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe log?

Comment thread codex-rs/login/src/lib.rs Outdated
write_auth_json(&get_auth_file(codex_home), &auth_dot_json)
}

async fn run_auto_refresh_loop(auth: CodexAuth) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can potentially replace all of this with a change to this check to auto-refresh if the token expires in the next ~day.

@aibrahim-oai
aibrahim-oai requested a review from gpeal August 20, 2025 03:21
@aibrahim-oai
aibrahim-oai merged commit d1f1e36 into main Aug 20, 2025
15 checks passed
@aibrahim-oai
aibrahim-oai deleted the refresh-token branch August 20, 2025 04:01
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 20, 2025
swordfish444 pushed a commit to swordfish444/codex that referenced this pull request Feb 26, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants