Avoid refreshing Codex token in settings

This commit is contained in:
chengyongru 2026-06-18 16:57:53 +08:00 committed by Xubin Ren
parent e3c9aff41e
commit 7b153c5aa9
2 changed files with 21 additions and 18 deletions

View File

@ -283,7 +283,8 @@ def _oauth_provider_status(spec: Any) -> dict[str, Any]:
if spec.name == "openai_codex": if spec.name == "openai_codex":
try: try:
from oauth_cli_kit import get_token as get_codex_token from oauth_cli_kit.providers import OPENAI_CODEX_PROVIDER
from oauth_cli_kit.storage import FileTokenStorage
except Exception: except Exception:
return { return {
"configured": False, "configured": False,
@ -293,10 +294,14 @@ def _oauth_provider_status(spec: Any) -> dict[str, Any]:
} }
token = None token = None
with suppress(Exception): with suppress(Exception):
token = get_codex_token() token = FileTokenStorage(
token_filename=OPENAI_CODEX_PROVIDER.token_filename,
).load()
expires_at = getattr(token, "expires", None) if token else None expires_at = getattr(token, "expires", None) if token else None
return { return {
"configured": bool(token and token.access), "configured": bool(
token and token.access and expires_at and expires_at > int(time.time() * 1000)
),
"account": getattr(token, "account_id", None) if token else None, "account": getattr(token, "account_id", None) if token else None,
"expires_at": expires_at, "expires_at": expires_at,
"login_supported": True, "login_supported": True,

View File

@ -788,8 +788,7 @@ def test_update_network_safety_settings_default_access_is_webui_only(
def test_openai_codex_oauth_status_uses_available_token( def test_openai_codex_oauth_status_uses_available_token(
monkeypatch: pytest.MonkeyPatch, monkeypatch: pytest.MonkeyPatch,
) -> None: ) -> None:
def fake_get_token(): token = type(
return type(
"Token", "Token",
(), (),
{ {
@ -799,8 +798,7 @@ def test_openai_codex_oauth_status_uses_available_token(
"account_id": "acct-codex", "account_id": "acct-codex",
}, },
)() )()
monkeypatch.setattr("oauth_cli_kit.storage.FileTokenStorage.load", lambda _self: token)
monkeypatch.setattr("oauth_cli_kit.get_token", fake_get_token)
status = _oauth_provider_status(find_by_name("openai_codex")) status = _oauth_provider_status(find_by_name("openai_codex"))
@ -811,10 +809,10 @@ def test_openai_codex_oauth_status_uses_available_token(
def test_openai_codex_oauth_status_rejects_unavailable_token( def test_openai_codex_oauth_status_rejects_unavailable_token(
monkeypatch: pytest.MonkeyPatch, monkeypatch: pytest.MonkeyPatch,
) -> None: ) -> None:
def fake_get_token(): def fake_load(_self):
raise RuntimeError("refresh failed") raise RuntimeError("refresh failed")
monkeypatch.setattr("oauth_cli_kit.get_token", fake_get_token) monkeypatch.setattr("oauth_cli_kit.storage.FileTokenStorage.load", fake_load)
status = _oauth_provider_status(find_by_name("openai_codex")) status = _oauth_provider_status(find_by_name("openai_codex"))