Enterprise agent platform for building, deploying, and operating AI teammates.
fix(oauth): treat Zoom's invalid_grant refresh as a dead token (#3768)
蓝鱼bluefish3e38085f009d
diff --git a/src/xagent/web/tools/config.py b/src/xagent/web/tools/config.pyindex d73fc394d..199e64dc0 100644--- a/src/xagent/web/tools/config.py+++ b/src/xagent/web/tools/config.py@@ -914,6 +914,9 @@ _PROVIDER_DEAD_REFRESH_TOKEN_ERROR_CODES: dict[str, frozenset[str]] = {# in _finish_legacy_oauth_access_token_resolution; the actor flow holds# its row lock across the refresh instead."zendesk": frozenset({"invalid_grant"}),+ # Assumes, as for Zendesk, that a client change answers invalid_client (the+ # refresh path's default client-rejected code), not invalid_grant.+ "zoom": frozenset({"invalid_grant"}),}diff --git a/tests/web/test_zoom_oauth.py b/tests/web/test_zoom_oauth.pyindex 33e134a4e..954d4267e 100644--- a/tests/web/test_zoom_oauth.py+++ b/tests/web/test_zoom_oauth.py@@ -357,6 +357,92 @@ async def test_zoom_expired_token_refresh_uses_http_basic_auth_with_capitalized_assert "client_secret" not in kwargs["data"]+def _expired_zoom_grant(db, user):+ db.add(+ OAuthProvider(+ provider_name="zoom",+ name="Zoom",+ client_id=encrypt_value("zoom-client-id"),+ client_secret=encrypt_value("zoom-client-secret"),+ auth_url="https://zoom.us/oauth/authorize",+ token_url="https://zoom.us/oauth/token",+ redirect_uri="https://app.example.com/api/auth/zoom/callback",+ userinfo_url="https://api.zoom.us/v2/users/me",+ user_id_path="id",+ email_path="email",+ default_scopes=["meeting:read:meeting"],+ )+ )+ oauth_account = UserOAuth(+ user_id=user.id,+ provider="zoom",+ access_token="old-token",+ refresh_token="old-refresh",+ expires_at=datetime.now(timezone.utc) - timedelta(minutes=1),+ provider_user_id="zoom-user-1",+ )+ db.add(oauth_account)+ db.commit()+ return oauth_account+++def _zoom_token_endpoint(monkeypatch, data, status_code):+ class FakeAsyncClient:+ async def __aenter__(self):+ return self++ async def __aexit__(self, exc_type, exc, tb):+ return None++ async def post(self, url, **kwargs):+ return MockResponse(data, status_code=status_code)++ monkeypatch.setattr(tool_config, "create_public_async_client", FakeAsyncClient)+++async def test_zoom_refresh_invalid_grant_is_permanent(db_session, monkeypatch):+ """A grant superseded by a newer authorization of the same Zoom user+ answers invalid_grant; retrying cannot fix it, so it must be classified+ like Zendesk's dead token."""+ db, user = db_session+ grant = _expired_zoom_grant(db, user)+ _zoom_token_endpoint(+ monkeypatch, {"error": "invalid_grant", "error_description": "expired"}, 400+ )++ with pytest.raises(tool_config._OAuthRefreshPermanentlyInvalid):+ await tool_config.refresh_oauth_token_if_needed(db, grant, "zoom")+++async def test_zoom_refresh_invalid_client_is_not_a_dead_grant(db_session, monkeypatch):+ """Zoom answers a rejected platform client with invalid_client, which+ says nothing about this user's grant -- keeping it avoids tombstoning+ every connection on one admin credential change."""+ db, user = db_session+ grant = _expired_zoom_grant(db, user)+ _zoom_token_endpoint(monkeypatch, {"error": "invalid_client"}, 400)++ with pytest.raises(tool_config._OAuthRefreshClientRejected):+ await tool_config.refresh_oauth_token_if_needed(db, grant, "zoom")++ assert grant.refresh_token == "old-refresh"+++async def test_zoom_refresh_5xx_with_invalid_grant_stays_transient(+ db_session, monkeypatch+):+ """A 5xx is Zoom's own outage, never proof this grant is dead, even when+ the body carries a code that is trusted for Zoom on a 4xx."""+ db, user = db_session+ grant = _expired_zoom_grant(db, user)+ _zoom_token_endpoint(monkeypatch, {"error": "invalid_grant"}, 502)++ assert await tool_config.refresh_oauth_token_if_needed(db, grant, "zoom") is False++@pytest.mark.asyncioasync def test_generic_provider_refresh_sends_client_id_and_secret_in_body(db_session, monkeypatchdiff --git a/tests/web/tools/test_oauth_token_resolver_hook.py b/tests/web/tools/test_oauth_token_resolver_hook.pyindex 27032a72b..1e40d4a9d 100644--- a/tests/web/tools/test_oauth_token_resolver_hook.py+++ b/tests/web/tools/test_oauth_token_resolver_hook.py@@ -2180,6 +2180,86 @@ async def test_user_oauth_refresh_google_revoked_token_keeps_reconnect_tombstoneassert tombstone.expires_at is None+async def test_user_oauth_refresh_zoom_invalid_grant_keeps_reconnect_tombstone(+ db_session, monkeypatch+):+ """Zoom answers invalid_grant once the same user re-authorizes the app+ (only the latest grant works) or revokes it; the connection must be+ marked for reconnect instead of retried on every load, with the account+ identity kept for the reconnect flow.+ """+ db, user = db_session+ db.add(+ OAuthProvider(+ provider_name="zoom",+ name="Zoom",+ client_id=encrypt_value("zoom-client-id"),+ client_secret=encrypt_value("zoom-client-secret"),+ auth_url="https://zoom.us/oauth/authorize",+ token_url="https://zoom.us/oauth/token",+ )+ )+ oauth_server = _add_oauth_server(+ db,+ user,+ name="Zoom",+ app_id="resolver-zoom",+ provider="zoom",+ launch_config=_launch_config("ZOOM_ACCESS_TOKEN"),+ )+ oauth_account = _add_user_oauth(+ db, user, provider="zoom", access_token="expired-token"+ )+ oauth_account.refresh_token = "superseded-refresh-token"+ oauth_account.expires_at = datetime.now(timezone.utc) - timedelta(minutes=1)+ account_id = oauth_account.id+ db.commit()++ isolated_session_factory = sessionmaker(+ bind=db.get_bind(), autoflush=False, autocommit=False+ )++ class DeadRefreshTokenAsyncClient:+ async def __aenter__(self):+ return self++ async def __aexit__(self, exc_type, exc, traceback):+ return False++ async def post(self, *args, **kwargs):+ return httpx.Response(+ 400,+ json={+ "error": "invalid_grant",+ "error_description": "Invalid refresh token",+ },+ )++ monkeypatch.setattr(+ web_tools_config, "create_public_async_client", DeadRefreshTokenAsyncClient+ )++ configs = await _tool_config(+ db, user, db_factory=isolated_session_factory+ ).get_mcp_server_configs()++ assert [config["name"] for config in configs] == ["Zoom"]+ _assert_unavailable_mcp_config(+ configs[0],+ oauth_server,+ reason="oauth_token_refresh_failed",+ oauth_token_required=True,+ )+ with isolated_session_factory() as verification_db:+ tombstone = verification_db.get(UserOAuth, account_id)+ assert tombstone is not None+ assert tombstone.access_token == ""+ assert tombstone.refresh_token is None+ assert tombstone.expires_at is None+ assert tombstone.provider == "zoom"++def test_google_refresh_classifies_explicit_revocation():with pytest.raises(web_tools_config._OAuthRefreshPermanentlyInvalid):web_tools_config._log_and_classify_failed_refresh(@@ -2240,7 +2320,7 @@ def test_google_refresh_classifies_explicit_revocation():},),(- "zoom",+ "linear",400,{"error": "invalid_grant",