Skip to content

Agent Zero AI framework

Identify Agent Zero OAuth refresh requests

Cooper Gamble808629942ac3
diff --git a/plugins/_oauth/helpers/codex.py b/plugins/_oauth/helpers/codex.py
index 3bdd579d6..ad32cd271 100644
--- a/plugins/_oauth/helpers/codex.py
+++ b/plugins/_oauth/helpers/codex.py
@@ -500,7 +500,10 @@ def refresh_tokens(refresh_token: str) -> dict[str, str]:
cfg = codex_config()
response = requests.post(
cfg["token_url"],
- headers={"Content-Type": "application/json"},
+ headers={
+ "Content-Type": "application/json",
+ "User-Agent": resolve_agent_zero_user_agent(),
+ },
json={
"client_id": cfg["client_id"],
"grant_type": "refresh_token",
@@ -522,6 +525,16 @@ def refresh_tokens(refresh_token: str) -> dict[str, str]:
}
+def resolve_agent_zero_user_agent() -> str:
+ try:
+ from helpers import git
+
+ version = git.get_version()
+ except Exception:
+ version = "unknown"
+ return f"agent-zero/{version or 'unknown'}"
+
+
def should_refresh(access_token: str, last_refresh: str) -> bool:
if not access_token:
return True
diff --git a/tests/test_oauth_codex.py b/tests/test_oauth_codex.py
index 8ddf6ae8e..af84f8b44 100644
--- a/tests/test_oauth_codex.py
+++ b/tests/test_oauth_codex.py
@@ -258,6 +258,60 @@ def test_token_error_message_prefers_description():
assert codex._token_error_message(FakeResponse()) == "refresh token was already used"
+def test_refresh_tokens_sends_agent_zero_user_agent(monkeypatch):
+ requests: list[dict] = []
+
+ class FakeResponse:
+ ok = True
+
+ @staticmethod
+ def json():
+ return {
+ "access_token": "access-1",
+ "refresh_token": "refresh-1",
+ }
+
+ def post(url, *, headers, json, timeout):
+ requests.append(
+ {
+ "url": url,
+ "headers": headers,
+ "json": json,
+ "timeout": timeout,
+ }
+ )
+ return FakeResponse()
+
+ monkeypatch.setattr(
+ codex,
+ "codex_config",
+ lambda: {"token_url": "https://auth.example/oauth/token", "client_id": "client"},
+ )
+ monkeypatch.setattr(codex, "resolve_agent_zero_user_agent", lambda: "agent-zero/v1.18")
+ monkeypatch.setattr(codex.requests, "post", post)
+
+ assert codex.refresh_tokens("refresh-0") == {
+ "id_token": "",
+ "access_token": "access-1",
+ "refresh_token": "refresh-1",
+ }
+ assert requests == [
+ {
+ "url": "https://auth.example/oauth/token",
+ "headers": {
+ "Content-Type": "application/json",
+ "User-Agent": "agent-zero/v1.18",
+ },
+ "json": {
+ "client_id": "client",
+ "grant_type": "refresh_token",
+ "refresh_token": "refresh-0",
+ },
+ "timeout": 30,
+ }
+ ]
+
+
def test_default_auth_file_ignores_codex_cli_credentials(tmp_path, monkeypatch):
shared_auth = tmp_path / ".codex" / "auth.json"
private_auth = tmp_path / "usr" / "plugins" / "_oauth" / "codex" / "auth.json"