Skip to content

[Bug]: Valid Codex OAuth credentials are rejected solely because last_refresh is older than 8 days #533

Closed

Description

@le-shi

Existing issues

What happened?

Win-CodexBar rejects otherwise valid Codex CLI OAuth credentials solely because auth.json.last_refresh is older than eight days.

The current provider gate runs before the usage request:

let is_stale = credentials
    .last_refresh
    .is_none_or(|last| now - last > EXTERNAL_OAUTH_STALENESS_WINDOW);
if is_stale {
    return Err(ProviderError::AuthRequired);
}

This makes last_refresh act as a token-expiry timestamp even though it only records when the CLI last refreshed the credential set. The same code already parses the access-token JWT exp, which is a more direct validity signal.

Issue #468 contains a concrete reproduction: last_refresh was about 215 hours old, Codex CLI was logged in, and the ChatGPT usage endpoint returned HTTP 200, but Win-CodexBar returned Authentication required. #468 was closed after adding a Refresh login action, with the explicit note that the 8-day gate remained intentional.

The workaround is unsuitable for unattended serve --metrics nodes. It requires periodic interactive login, and #468 also records an organization where device authentication is not allowed.

Expected behavior: an old last_refresh should not by itself make credentials unusable. When JWT expiry is available, use it as the local expiry check. When the token is opaque, allow the read-only usage request and let an actual HTTP 401/403 establish that authentication is invalid.

Affected area

  • Tray panel
  • Settings UI
  • Config file / settings persistence
  • CLI
  • Provider-specific behavior
  • Installer / release packaging
  • Startup / background behavior
  • Other

Steps to reproduce

  1. Sign in to Codex CLI using ChatGPT OAuth and keep the resulting native auth.json unchanged for more than eight days.
  2. Confirm that Codex CLI remains usable. If testing the transport directly, confirm the same bearer token can receive HTTP 200 from the ChatGPT usage endpoint without publishing the token.
  3. Run codexbar-cli diagnose -p codex --source oauth --pretty, refresh the Codex tray provider, or scrape an enabled serve --metrics endpoint.
  4. Observe Authentication required / codexbar_provider_up{provider="codex"} 0 before Win-CodexBar sends the usage request.
  5. Refresh the Codex login and observe that only the last_refresh age change is sufficient to make usage work again.

A deterministic regression can construct credentials with last_refresh = now - 10 days and an access-token JWT whose exp is still in the future.

Logs, screenshots, or recordings

Observed provider warning:
Codex API fetch failed: Authentication required

Related reproduction in #468:
- last_refresh approximately 215 hours old
- Codex CLI logged in
- ChatGPT usage endpoint returned HTTP 200
- Win-CodexBar returned Authentication required

App version

v0.56.8 and current main

Windows version

Windows 10/11

Additional context

This is independent of the Prometheus endpoint implementation in #528; /metrics only exposes the existing Codex provider failure.

Suggested behavior and safety boundaries:

  • Do not reject credentials only because last_refresh is older than eight days.
  • Keep rejecting a JWT that is expired or within the existing refresh window.
  • If JWT exp is unavailable but the file has native CLI refresh metadata, make the read-only request and classify an actual 401/403 as Authentication required.
  • Preserve fail-closed behavior for unknown external OAuth files that have no last_refresh metadata unless the existing opt-in setting allows them.
  • Do not redeem the refresh token and do not write to the Codex CLI-owned auth.json.

I validated a local minimal patch with these cases:

  • old last_refresh plus future JWT expiry is accepted;
  • old last_refresh plus an opaque token is allowed to reach server validation;
  • near-expiry JWT is still rejected;
  • missing last_refresh metadata remains fail-closed;
  • all 40 Codex API provider tests pass.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions