Skip to content

fix(codex-auth): handle nested refresh errors and prefer access_token expiry - #3286

Open
littlemoonly wants to merge 1 commit into
HKUDS:devfrom
littlemoonly:fix/codex-oauth-refresh
Open

littlemoonly wants to merge 1 commit into
HKUDS:devfrom
littlemoonly:fix/codex-oauth-refresh

Conversation

@littlemoonly

Copy link
Copy Markdown

Description

修复 Codex OAuth Token 刷新流程中的两个问题:

  1. 兼容嵌套字典格式的刷新错误响应,避免 TypeError: unhashable type: 'dict'
    deeptutor/services/codex_auth/oauth.py 原先直接判断 rejection.get("error") in {"invalid_grant", "invalid_token"}。当 OpenAI 鉴权接口在 400/401 响应中返回嵌套对象(例如 {"error": {"type": "invalid_request_error", "code": "refresh_token_reused", ...}})时,对 dict 做集合查找会直接抛出 TypeError,导致未能标记 token_refresh_rejected / 引导用户重新登录,而是每次请求重复报错 Codex request failed。
    现新增 _is_refresh_rejection(),安全提取字符串或嵌套 error.code / error.type(覆盖 invalid_grant、invalid_token、refresh_token_expired、refresh_token_reused、refresh_token_invalidated),非拒绝类或不可哈希错误不再崩溃,统一按 token_refresh_failed 处理。

  2. 优先使用 access_token 的过期时间,避免每小时无效刷新
    deeptutor/services/codex_auth/service.py 的 _credentials_from_payload() 原先按 (id_token, access_token) 顺序提取 expires_at,会优先取到有效期仅 1 小时的 id_token.exp,覆盖掉有效期约 10 天的 access_token.exp,导致实际有效的 access_token 每小时都被强制触发 refresh_token。
    现调整为优先取 access_token 的 JWT exp(其次 expires_in,最后回退 id_token.exp),account_id 解析顺序保持不变。

复现步骤

问题 1(刷新错误触发 TypeError):

  1. 登录 Codex OAuth,等待或使本地 refresh_token 在服务端失效/被复用。
  2. 当本地凭证满足刷新条件(expires_at - now <= 300)时发起任意 Codex 对话请求。
  3. OpenAI /oauth/token 返回 HTTP 400/401 且响应体为 {"error": {"code": "refresh_token_reused", ...}}。
  4. 后端在 oauth.py::_token_request 抛出 TypeError: unhashable type: 'dict',未触发 mark_authentication_rejected(),前端仅收到泛化的 Codex request failed. Please try again.,后续请求持续重复报错。

问题 2(误用 id_token.exp 导致每小时刷新):

  1. 完成一次 Codex OAuth 登录,查看本地 credentials.v1.json。
  2. 对比 id_token(exp 为 1 小时后)与 access_token(exp 为 10 天后),可见存储的 expires_at 被写成了 id_token.exp。
  3. 1 小时后发起对话,即便 access_token 仍有约 10 天有效期,仍会强制走 _refresh_credentials()。

Related Issues

  • N/A

Module(s) Affected

  • services
  • tests

Checklist

  • This PR targets dev (not main — see the base-branch selector above).
  • I have read and followed the contribution guidelines.
  • My code follows the project's coding standards.
  • I have run pre-commit run --all-files and fixed any issues.
  • I have added relevant tests for my changes.
  • I have updated the documentation (if necessary).
  • My changes do not introduce any new security vulnerabilities.

Additional Notes

测试与检查结果:

  • pytest tests/services/codex_auth/ tests/services/llm/test_openai_codex_oauth_provider.py tests/api/test_codex_oauth_callback.py tests/api/test_codex_oauth_scope.py:340 passed
  • pytest tests/multi_user/test_personal_codex_models.py:10 passed
  • ruff check & ruff format --check(改动文件):全部通过

@github-actions github-actions Bot added backend Python runtime, capabilities, tools, API & CLI bug Something isn't working labels Dec 5, 2026

This branch has not been deployed

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

Labels

backend Python runtime, capabilities, tools, API & CLI bug Something isn't working

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

1 participant