AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend): lock every rotating OAuth refresh, and keep Notion's and Todoist's refresh tokens (#15647)
Reinier van der Leer7f7412b96c6e
diff --git a/autogpt_platform/autogpt_libs/autogpt_libs/utils/synchronize.py b/autogpt_platform/autogpt_libs/autogpt_libs/utils/synchronize.pyindex a8c5c204ee..a51677e109 100644--- a/autogpt_platform/autogpt_libs/autogpt_libs/utils/synchronize.py+++ b/autogpt_platform/autogpt_libs/autogpt_libs/utils/synchronize.py@@ -44,10 +44,13 @@ class AsyncRedisKeyedMutex:if (await lock.locked()) and (await lock.owned()):await lock.release()- async def acquire(self, key: Any) -> "AsyncRedisLock":- """Acquires and returns a lock with the given key"""+ async def acquire(+ self, key: Any, *, blocking_timeout: float | None = None+ ) -> "AsyncRedisLock":+ """Acquire a lock, raising TimeoutError if its blocking timeout expires."""lock = self.redis.lock(str(key), self.timeout, thread_local=False)- await lock.acquire()+ if not await lock.acquire(blocking_timeout=blocking_timeout):+ raise TimeoutError("Timed out waiting for a Redis lock")# No await between taking the key and registering it: a caller that# wraps this in wait_for and cleans up with release(key) must always# find a lock it was cancelled after taking.diff --git a/autogpt_platform/backend/backend/blocks/airtable/_oauth.py b/autogpt_platform/backend/backend/blocks/airtable/_oauth.pyindex 95db25a0c0..c120c70bde 100644--- a/autogpt_platform/backend/backend/blocks/airtable/_oauth.py+++ b/autogpt_platform/backend/backend/blocks/airtable/_oauth.py@@ -5,7 +5,7 @@ Airtable OAuth handler implementation.import timefrom enum import Enumfrom logging import getLogger-from typing import Optional+from typing import ClassVar, Optionalfrom backend.sdk import (BaseOAuthHandler,@@ -64,6 +64,7 @@ class AirtableOAuthHandler(BaseOAuthHandler):"""PROVIDER_NAME = ProviderName("airtable")+ ROTATES_REFRESH_TOKEN: ClassVar[bool] = TrueDEFAULT_SCOPES = [v.valuefor v in [diff --git a/autogpt_platform/backend/backend/blocks/mcp/oauth.py b/autogpt_platform/backend/backend/blocks/mcp/oauth.pyindex 0fa07dd98a..10c6e6b5a9 100644--- a/autogpt_platform/backend/backend/blocks/mcp/oauth.py+++ b/autogpt_platform/backend/backend/blocks/mcp/oauth.py@@ -37,6 +37,8 @@ class MCPOAuthHandler(BaseOAuthHandler):PROVIDER_NAME: ClassVar[ProviderName | str] = ProviderName.MCPDEFAULT_SCOPES: ClassVar[list[str]] = []+ # MCP requires authorization servers to rotate public clients' refresh tokens.+ ROTATES_REFRESH_TOKEN: ClassVar[bool] = Truedef __init__(self,diff --git a/autogpt_platform/backend/backend/copilot/integration_creds.py b/autogpt_platform/backend/backend/copilot/integration_creds.pyindex 56284b43f3..5e3ee0fa75 100644--- a/autogpt_platform/backend/backend/copilot/integration_creds.py+++ b/autogpt_platform/backend/backend/copilot/integration_creds.py@@ -241,7 +241,7 @@ async def get_provider_token(# Credentials covering the requested scopes come first, then ones with# "repo" (full git access, where a public-data-only token lacks push/pull).# The sort is stable, so ties keep their stored order, as the card does.- # lock=False — background injection; not worth a distributed lock acquisition.+ # Rotating providers still lock and reload when lock=False needs a refresh.def rank(creds: OAuth2Credentials) -> tuple[int, int]:granted = set(creds.scopes or [])return (diff --git a/autogpt_platform/backend/backend/copilot/integration_refresh_test.py b/autogpt_platform/backend/backend/copilot/integration_refresh_test.pynew file mode 100644index 0000000000..8d20c5e77c--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/integration_refresh_test.py@@ -0,0 +1,187 @@+"""Copilot and the idle sweep must share a rotating credential's refresh."""++import asyncio+import time+from unittest.mock import AsyncMock, MagicMock, PropertyMock++import pytest+from pydantic import SecretStr+from pytest_mock import MockerFixture+from redis.asyncio.lock import Lock++from backend.blocks.airtable._oauth import AirtableOAuthHandler+from backend.blocks.linear._oauth import LinearOAuthHandler+from backend.copilot import integration_creds+from backend.data.model import OAuth2Credentials+from backend.integrations.creds_manager import IntegrationCredentialsManager+from backend.integrations.oauth.base import BaseOAuthHandler+from backend.integrations.oauth.github import GitHubOAuthHandler++_USER = "refresh-race-owner"+++ params=[+ pytest.param(GitHubOAuthHandler, id="github"),+ pytest.param(AirtableOAuthHandler, id="airtable"),+ pytest.param(LinearOAuthHandler, id="linear"),+ ]+)+def scenario(request: pytest.FixtureRequest, mocker: MockerFixture):+ handler = request.param("fake-client", "fake-secret", "https://example.com")+ return _RefreshScenario(mocker, handler)+++async def test_copilot_reloads_a_snapshot_taken_before_the_idle_sweep(scenario):+ await asyncio.wait_for(scenario.manager.refresh_idle_credentials(), timeout=2)++ token = await asyncio.wait_for(+ integration_creds.get_provider_token(_USER, scenario.stored.provider), timeout=2+ )++ scenario.assert_one_refresh(token)+++async def test_copilot_waits_for_the_idle_sweep_to_finish_rotating(scenario):+ scenario.finish_refresh.clear()+ sweep = asyncio.create_task(scenario.manager.refresh_idle_credentials())+ tasks = [sweep]+ try:+ await asyncio.wait_for(scenario.refresh_started.wait(), timeout=2)+ copilot = asyncio.create_task(+ integration_creds.get_provider_token(_USER, scenario.stored.provider)+ )+ tasks.append(copilot)+ await asyncio.wait_for(scenario.snapshot_read.wait(), timeout=2)+ scenario.finish_refresh.set()+ _, token = await asyncio.wait_for(asyncio.gather(*tasks), timeout=2)++ scenario.assert_one_refresh(token)+ finally:+ scenario.finish_refresh.set()+ for task in tasks:+ task.cancel()+ await asyncio.gather(*tasks, return_exceptions=True)+++class _RefreshScenario:+ def __init__(self, mocker: MockerFixture, handler: BaseOAuthHandler):+ self.stored = OAuth2Credentials(+ id="refresh-race-credential",+ provider=handler.PROVIDER_NAME,+ access_token=SecretStr("access-old"),+ refresh_token=SecretStr("refresh-old"),+ access_token_expires_at=int(time.time()) - 15 * 86400,+ scopes=["read"],+ )+ self.snapshot = self.stored.model_copy(deep=True)+ self.attempts: list[str] = []+ self.provider_refresh_token = "refresh-old"+ self.locks: dict[tuple[str, ...], asyncio.Lock] = {}+ self.refresh_started = asyncio.Event()+ self.finish_refresh = asyncio.Event()+ self.finish_refresh.set()+ self.snapshot_read = asyncio.Event()+ self.manager = IntegrationCredentialsManager()+ self._mock_storage(mocker)+ self._mock_runtime(mocker, handler)++ def _mock_storage(self, mocker: MockerFixture):+ mocker.patch.object(+ self.manager.store, "get_creds_by_id", AsyncMock(side_effect=self.read)+ )+ mocker.patch.object(+ self.manager.store,+ "get_creds_by_provider",+ AsyncMock(side_effect=self.read_snapshot),+ )+ self.writes = mocker.patch.object(+ self.manager.store, "update_creds", AsyncMock(side_effect=self.write)+ )+ mocker.patch.object(+ type(self.manager.store),+ "db_manager",+ new_callable=PropertyMock,+ return_value=MagicMock(+ get_near_expiry_oauth_credentials=AsyncMock(+ return_value=[(_USER, self.snapshot)]+ )+ ),+ )++ def _mock_runtime(self, mocker: MockerFixture, handler: BaseOAuthHandler):+ mocker.patch.object(+ self.manager, "_get_oauth_handler", AsyncMock(return_value=handler)+ )+ mocker.patch.object(+ self.manager,+ "locks",+ AsyncMock(return_value=MagicMock(acquire=self.acquire)),+ )+ mocker.patch(+ "backend.integrations.creds_manager._invoke_creds_changed_hook", AsyncMock()+ )+ mocker.patch(+ "backend.util.request.Requests.post", AsyncMock(side_effect=self.exchange)+ )+ mocker.patch.object(+ GitHubOAuthHandler, "_request_username", AsyncMock(return_value="fake-user")+ )+ mocker.patch.object(integration_creds, "_manager", self.manager)+ mocker.patch.object(integration_creds, "_ensure_cache_invalidation_listener")+ mocker.patch.object(integration_creds, "_token_cache", {})+ mocker.patch.object(integration_creds, "_null_cache", {})++ async def read(self, user_id: str, credential_id: str):+ assert (user_id, credential_id) == (_USER, self.stored.id)+ return self.stored.model_copy(deep=True)++ async def read_snapshot(self, user_id: str, provider: str):+ assert (user_id, provider) == (_USER, self.stored.provider)+ self.snapshot_read.set()+ return [self.snapshot.model_copy(deep=True)]++ async def write(self, user_id: str, credentials: OAuth2Credentials):+ assert (user_id, credentials.id) == (_USER, self.stored.id)+ self.stored = credentials.model_copy(deep=True)++ async def acquire(self, key: tuple[str, ...], *, blocking_timeout=None):+ mutex = self.locks.setdefault(key, asyncio.Lock())+ await asyncio.wait_for(mutex.acquire(), timeout=blocking_timeout)+ lock = AsyncMock(spec=Lock)+ lock.locked.return_value = True+ lock.owned.return_value = True+ lock.release.side_effect = mutex.release+ return lock++ async def exchange(self, _url: str, *, data: dict[str, str], **_kwargs):+ self.attempts.append(data["refresh_token"])+ self.refresh_started.set()+ await self.finish_refresh.wait()+ if data["refresh_token"] != self.provider_refresh_token:+ raise ValueError("invalid_grant: refresh token already consumed")+ self.provider_refresh_token = "refresh-new"+ return MagicMock(+ ok=True,+ json=MagicMock(+ return_value={+ "access_token": "access-new",+ "refresh_token": "refresh-new",+ "expires_in": 3600,+ "refresh_expires_in": 60 * 86400,+ "refresh_token_expires_in": 180 * 86400,+ "token_type": "Bearer",+ "scope": "read",+ }+ ),+ )++ def assert_one_refresh(self, token: str | None):+ assert token == "access-new"+ assert self.attempts == ["refresh-old"]+ self.writes.assert_awaited_once()+ assert self.stored.refresh_token is not None+ assert self.stored.refresh_token.get_secret_value() == "refresh-new"+ assert self.stored.access_token_expires_at is not None+ assert self.stored.access_token_expires_at > time.time()+ assert all(not lock.locked() for lock in self.locks.values())diff --git a/autogpt_platform/backend/backend/integrations/creds_manager.py b/autogpt_platform/backend/backend/integrations/creds_manager.pyindex 7a6604117a..10f699722e 100644--- a/autogpt_platform/backend/backend/integrations/creds_manager.py+++ b/autogpt_platform/backend/backend/integrations/creds_manager.py@@ -32,6 +32,7 @@ if TYPE_CHECKING:logger = logging.getLogger(__name__)settings = Settings()+_REFRESH_LOCK_WAIT_SECONDS = 5.0_on_creds_changed: Callable[[str, str], None] | None = None@@ -261,26 +262,27 @@ class IntegrationCredentialsManager:if credentials.refresh_strategy == "provider_runtime":return credentials- # When lock=False, skip ALL Redis locking (both the outer "refresh" scope- # lock and the inner credential lock). This is used by the copilot's- # integration_creds module which runs across multiple threads with separate- # event loops; acquiring a Redis lock whose asyncio.Lock() was created on- # a different loop raises "Future attached to a different loop".- # Resolve once: `_refresh_locked` needs the handler anyway, and- # building it twice per refresh just to read a class flag is wasteful.- # A construction failure is left to the refresh path to raise, rather- # than being turned into "take the lock" — `lock=False` exists because- # the copilot runs across threads with separate event loops, where- # entering a Redis lock raises "Future attached to a different loop".+ # Copilot skips refresh locks for reusable tokens. Rotating tokens must+ # serialize refreshes and reload storage, even for a lock-free caller.handler = await self._get_oauth_handler(credentials)- if lock or getattr(handler, "ROTATES_REFRESH_TOKEN", False):+ if lock:+ return await self._refresh_locked(user_id, credentials, handler=handler)+ # Lock-free callers pay for the lock only when a refresh will happen.+ if not handler.needs_refresh(credentials):+ return credentials+ if handler.ROTATES_REFRESH_TOKEN:# A provider that rotates its refresh token cannot take the# unlocked path: "last writer wins" is safe when the refresh token# is static, but two workers replaying the same rotated token can# have the grant revoked by a provider that treats reuse as# compromise, and the loser can persist a token already# invalidated upstream.- return await self._refresh_locked(user_id, credentials, handler=handler)+ return await self._refresh_locked(+ user_id,+ credentials,+ handler=handler,+ lock_timeout=_REFRESH_LOCK_WAIT_SECONDS,+ )return await self._refresh_unlocked(user_id, credentials, handler=handler)async def _get_oauth_handler(@@ -307,8 +309,13 @@ class IntegrationCredentialsManager:user_id: str,credentials: OAuth2Credentials,handler: "BaseOAuthHandler | BaseDeviceAuthHandler | None" = None,+ *,+ lock_timeout: float | None = None,) -> OAuth2Credentials:- async with self._locked(user_id, credentials.id, "refresh"):+ # Bound acquisition only: cancelling a refresh can lose a rotated token.+ async with self._locked(+ user_id, credentials.id, "refresh", blocking_timeout=lock_timeout+ ):credentials = await self._reload_oauth_credentials(user_id, credentials)if credentials.refresh_strategy == "provider_runtime":return credentials@@ -320,7 +327,9 @@ class IntegrationCredentialsManager:credentials.id,)# Wait until the credentials are no longer in use anywhere- _lock = await self._acquire_lock(user_id, credentials.id)+ _lock = await self._acquire_lock(+ user_id, credentials.id, blocking_timeout=lock_timeout+ )try:credentials = await self._reload_oauth_credentials(user_id, credentials@@ -373,11 +382,8 @@ class IntegrationCredentialsManager:) -> OAuth2Credentials:"""Best-effort token refresh without any Redis locking.- Safe for use from multi-threaded contexts (e.g. copilot workers) where- each thread has its own event loop and sharing Redis-backed asyncio locks- is not possible. Concurrent refreshes are tolerated: the last writer- wins, and stale tokens are overwritten — which holds only for a static- refresh token, so a rotating provider never reaches this path.+ Concurrent refreshes are tolerated only for reusable refresh tokens;+ rotating providers must use the locked path and reload stored credentials."""oauth_handler = handler or await self._get_oauth_handler(credentials)if oauth_handler.needs_refresh(credentials):@@ -509,11 +515,15 @@ class IntegrationCredentialsManager:# -- Locking utilities -- #async def _acquire_lock(- self, user_id: str, credentials_id: str, *args: str+ self,+ user_id: str,+ credentials_id: str,+ *args: str,+ blocking_timeout: float | None = None,) -> AsyncRedisLock:key = self._credentials_lock_key(user_id, credentials_id, *args)locks = await self.locks()- return await locks.acquire(key)+ return await locks.acquire(key, blocking_timeout=blocking_timeout)@staticmethoddef _credentials_lock_key(@@ -533,8 +543,16 @@ class IntegrationCredentialsManager:logger.warning("Failed to release credentials lock", exc_info=True)@asynccontextmanager- async def _locked(self, user_id: str, credentials_id: str, *args: str):- lock = await self._acquire_lock(user_id, credentials_id, *args)+ async def _locked(+ self,+ user_id: str,+ credentials_id: str,+ *args: str,+ blocking_timeout: float | None = None,+ ):+ lock = await self._acquire_lock(+ user_id, credentials_id, *args, blocking_timeout=blocking_timeout+ )try:yieldfinally:diff --git a/autogpt_platform/backend/backend/integrations/creds_manager_lock_test.py b/autogpt_platform/backend/backend/integrations/creds_manager_lock_test.pynew file mode 100644index 0000000000..07c44ff6db--- /dev/null+++ b/autogpt_platform/backend/backend/integrations/creds_manager_lock_test.py@@ -0,0 +1,253 @@+"""Rotating Copilot refreshes must not wait out a long-lived credential lease."""++import asyncio+import socket+import time+from unittest.mock import AsyncMock+from uuid import uuid4++import pytest+from autogpt_libs.utils.synchronize import AsyncRedisKeyedMutex+from redis.asyncio.cluster import RedisCluster+from redis.asyncio.lock import Lock++from backend.copilot import integration_creds+from backend.data import redis_client+from backend.data.model import OAuth2Credentials+from backend.integrations.creds_manager import IntegrationCredentialsManager+from backend.integrations.oauth.github import GitHubOAuthHandler+++async def scenario(mocker):+ try:+ socket.create_connection((redis_client.HOST, redis_client.PORT), 1).close()+ except OSError:+ pytest.skip("local redis cluster not reachable")+ redis = RedisCluster(+ host=redis_client.HOST,+ port=redis_client.PORT,+ password=redis_client.PASSWORD,+ address_remap=redis_client._address_remap,+ socket_connect_timeout=1,+ socket_timeout=1,+ )+ test = _RefreshScenario(mocker, redis)+ try:+ yield test+ finally:+ await test.mutex.release_all_locks()+ await redis.delete(*test.lock_keys)+ await redis.aclose()++[email protected]("sweep_waiting", [False, True])+async def test_copilot_times_out_without_releasing_the_lease_holder(+ scenario, sweep_waiting+):+ lease = await scenario.manager.acquire_lease(scenario.user, scenario.credentials.id)+ scenario.expire()+ sweep = None+ try:+ if sweep_waiting:+ scenario.read_started.clear()+ sweep = asyncio.create_task(+ scenario.manager.refresh_if_needed(scenario.user, scenario.credentials)+ )+ await asyncio.wait_for(scenario.read_started.wait(), timeout=1)+ token = await asyncio.wait_for(scenario.get_token(), timeout=1)+ assert token is None+ await lease.validate()+ scenario.refresh.assert_not_awaited()+ scenario.write.assert_not_awaited()+ finally:+ if sweep is not None:+ sweep.cancel()+ await asyncio.gather(sweep, return_exceptions=True)+ await lease.release()++ assert await scenario.redis.exists(*scenario.lock_keys) == 0+ assert await asyncio.wait_for(scenario.get_token(), timeout=1) == "new-access"+++async def test_default_refresh_still_waits_for_the_lease(scenario):+ lease = await scenario.manager.acquire_lease(scenario.user, scenario.credentials.id)+ scenario.expire()+ refresh = asyncio.create_task(+ scenario.manager.refresh_if_needed(scenario.user, scenario.credentials)+ )+ try:+ await asyncio.sleep(0.1)+ assert not refresh.done()+ await lease.validate()+ await lease.release()+ fresh = await asyncio.wait_for(refresh, timeout=1)+ assert fresh.access_token.get_secret_value() == "new-access"+ finally:+ refresh.cancel()+ await asyncio.gather(refresh, return_exceptions=True)+ await lease.release()+ assert await scenario.redis.exists(*scenario.lock_keys) == 0+++async def test_lock_wait_timeout_does_not_cancel_the_provider_refresh(scenario):+ scenario.expire()+ started, finish = asyncio.Event(), asyncio.Event()++ async def refresh(_credentials):+ started.set()+ await finish.wait()+ return scenario.fresh()++ scenario.refresh.side_effect = refresh+ lookup = asyncio.create_task(scenario.get_token())+ try:+ await asyncio.wait_for(started.wait(), timeout=1)+ await asyncio.sleep(0.1)+ assert not lookup.done()+ finish.set()+ assert await asyncio.wait_for(lookup, timeout=1) == "new-access"+ finally:+ finish.set()+ lookup.cancel()+ await asyncio.gather(lookup, return_exceptions=True)+ assert await scenario.redis.exists(*scenario.lock_keys) == 0++[email protected]("credential_lock_acquired", [False, True])+async def test_cancelled_copilot_lookup_releases_only_its_own_locks(+ scenario, credential_lock_acquired, mocker+):+ mocker.patch("backend.integrations.creds_manager._REFRESH_LOCK_WAIT_SECONDS", 5.0)+ lease = None+ if not credential_lock_acquired:+ lease = await scenario.manager.acquire_lease(+ scenario.user, scenario.credentials.id+ )+ scenario.expire()+ blocked = _pause_lookup(scenario, mocker, credential_lock_acquired)+ lookup = asyncio.create_task(scenario.get_token())+ try:+ await asyncio.wait_for(blocked.wait(), timeout=1)+ lookup.cancel()+ with pytest.raises(asyncio.CancelledError):+ await lookup+ assert await scenario.redis.exists(scenario.lock_keys[1]) == 0+ if lease is not None:+ await lease.validate()+ else:+ assert await scenario.redis.exists(scenario.lock_keys[0]) == 0+ scenario.refresh.assert_not_awaited()+ finally:+ lookup.cancel()+ await asyncio.gather(lookup, return_exceptions=True)+ if lease is not None:+ await lease.release()+++def _pause_lookup(scenario, mocker, after_acquisition: bool) -> asyncio.Event:+ blocked = asyncio.Event()+ reads = 0+ acquire = Lock.do_acquire++ async def read(*_args):+ nonlocal reads+ reads += 1+ if after_acquisition and reads == 2:+ blocked.set()+ await asyncio.Event().wait()+ return scenario.credentials++ async def acquire_then_pause(lock: Lock, token):+ acquired = await acquire(lock, token)+ if not after_acquisition and not acquired:+ blocked.set()+ await asyncio.Event().wait()+ return acquired++ scenario.manager.store.get_creds_by_id.side_effect = read+ mocker.patch.object(Lock, "do_acquire", acquire_then_pause)+ return blocked+++class _RefreshScenario:+ def __init__(self, mocker, redis: RedisCluster):+ self.user = f"refresh-lock-test-{uuid4()}"+ self.redis = redis+ self.mutex = AsyncRedisKeyedMutex(redis)+ self.manager = IntegrationCredentialsManager()+ self.credentials = OAuth2Credentials(+ id="credential-id",+ provider="github",+ access_token="old-access",+ refresh_token="old-refresh",+ access_token_expires_at=int(time.time()) + 3600,+ scopes=["read"],+ )+ self.read_started = asyncio.Event()+ self.lock_keys = [+ str(+ self.manager._credentials_lock_key(+ self.user, self.credentials.id, *args+ )+ )+ for args in [(), ("refresh",), ("!time_sensitive",)]+ ]+ self._mock_storage(mocker)+ self._mock_runtime(mocker)+ self.clock = mocker.patch("time.time", return_value=time.time())++ def _mock_storage(self, mocker):+ mocker.patch.object(self.manager, "locks", AsyncMock(return_value=self.mutex))+ mocker.patch.object(+ self.manager.store, "get_creds_by_id", AsyncMock(side_effect=self.read)+ )+ mocker.patch.object(+ self.manager.store,+ "get_creds_by_provider",+ AsyncMock(side_effect=lambda *_args: [self.credentials]),+ )+ self.write = mocker.patch.object(+ self.manager.store, "update_creds", AsyncMock()+ )++ def _mock_runtime(self, mocker):+ handler = GitHubOAuthHandler("client", "secret", "https://example.com")+ self.refresh = mocker.patch.object(+ handler,+ "refresh_tokens",+ AsyncMock(side_effect=lambda _creds: self.fresh()),+ )+ mocker.patch.object(+ self.manager, "_get_oauth_handler", AsyncMock(return_value=handler)+ )+ mocker.patch(+ "backend.integrations.creds_manager._REFRESH_LOCK_WAIT_SECONDS",+ 0.02,+ )+ mocker.patch("backend.integrations.creds_manager._invoke_creds_changed_hook")+ mocker.patch.object(integration_creds, "_manager", self.manager)+ mocker.patch.object(integration_creds, "_ensure_cache_invalidation_listener")+ mocker.patch.object(integration_creds, "_token_cache", {})+ mocker.patch.object(integration_creds, "_null_cache", {})++ async def read(self, user_id, credential_id):+ assert (user_id, credential_id) == (self.user, self.credentials.id)+ self.read_started.set()+ return self.credentials++ def expire(self):+ self.clock.return_value = self.credentials.access_token_expires_at + 1++ def fresh(self):+ return OAuth2Credentials(+ id=self.credentials.id,+ provider="github",+ access_token="new-access",+ refresh_token="new-refresh",+ access_token_expires_at=int(time.time()) + 3600,+ scopes=["read"],+ )++ async def get_token(self):+ return await integration_creds.get_provider_token(self.user, "github")diff --git a/autogpt_platform/backend/backend/integrations/creds_manager_test.py b/autogpt_platform/backend/backend/integrations/creds_manager_test.pyindex 64fbe59346..c876e7cf16 100644--- a/autogpt_platform/backend/backend/integrations/creds_manager_test.py+++ b/autogpt_platform/backend/backend/integrations/creds_manager_test.py@@ -1,10 +1,15 @@"""Tests for credential hooks and provider-runtime credential leases."""+import time+from functools import partialfrom unittest.mock import AsyncMock, MagicMock, patchimport pytestfrom pydantic import SecretStr+from backend.blocks.airtable._oauth import AirtableOAuthHandler+from backend.blocks.linear._oauth import LinearOAuthHandler+from backend.blocks.mcp.oauth import MCPOAuthHandlerfrom backend.data.model import APIKeyCredentials, OAuth2Credentialsfrom backend.data.user import get_near_expiry_oauth_credentialsfrom backend.integrations.creds_manager import (@@ -14,6 +19,10 @@ from backend.integrations.creds_manager import (unregister_creds_changed_hook,)from backend.integrations.oauth.github import GitHubOAuthHandler+from backend.integrations.oauth.google import GoogleOAuthHandler+from backend.integrations.oauth.notion import NotionOAuthHandler+from backend.integrations.oauth.todoist import TodoistOAuthHandler+from backend.integrations.oauth.twitter import TwitterOAuthHandlerfrom backend.util.encryption import JSONCryptor@@ -354,28 +363,82 @@ async def test_locked_refresh_reloads_after_waiting_for_a_lease(lock.release.assert_awaited_once()+_HANDLER_ARGS = ("client", "secret", "redirect")++@pytest.mark.asyncio-async def test_linear_refresh_uses_locked_path_even_when_copilot_requests_unlocked(- mocker,+ "make_handler,rotates",+ [+ pytest.param(partial(LinearOAuthHandler, *_HANDLER_ARGS), True, id="linear"),+ pytest.param(+ partial(AirtableOAuthHandler, *_HANDLER_ARGS), True, id="airtable"+ ),+ pytest.param(+ partial(+ MCPOAuthHandler,+ *_HANDLER_ARGS,+ authorize_url="https://auth.example.com/authorize",+ token_url="https://auth.example.com/token",+ ),+ True,+ id="mcp",+ ),+ pytest.param(partial(GitHubOAuthHandler, *_HANDLER_ARGS), True, id="github"),+ pytest.param(partial(TwitterOAuthHandler, *_HANDLER_ARGS), True, id="twitter"),+ pytest.param(partial(NotionOAuthHandler, *_HANDLER_ARGS), True, id="notion"),+ pytest.param(partial(TodoistOAuthHandler, *_HANDLER_ARGS), True, id="todoist"),+ pytest.param(partial(GoogleOAuthHandler, *_HANDLER_ARGS), False, id="google"),+ ],+)+async def test_copilot_refresh_takes_the_lock_only_for_rotating_providers(+ mocker, make_handler, rotates):- from backend.blocks.linear._oauth import LinearOAuthHandler-manager = IntegrationCredentialsManager()credentials = _provider_runtime_credentials().model_copy(- update={"provider": "linear", "refresh_strategy": "oauth_handler"}+ update={"refresh_strategy": "oauth_handler"})- handler = LinearOAuthHandler("client", "secret", "redirect")+ handler = make_handler()mocker.patch.object(manager, "_get_oauth_handler", AsyncMock(return_value=handler))locked = mocker.patch.object(manager, "_refresh_locked", AsyncMock(return_value=credentials))- unlocked = mocker.patch.object(manager, "_refresh_unlocked", AsyncMock())+ unlocked = mocker.patch.object(+ manager, "_refresh_unlocked", AsyncMock(return_value=credentials)+ )++ result = await manager.refresh_if_needed("user-a", credentials, lock=False)++ assert result is credentials+ taken, skipped = (locked, unlocked) if rotates else (unlocked, locked)+ if rotates:+ taken.assert_awaited_once_with(+ "user-a", credentials, handler=handler, lock_timeout=5.0+ )+ else:+ taken.assert_awaited_once_with("user-a", credentials, handler=handler)+ skipped.assert_not_awaited()+++async def test_copilot_read_of_an_unexpired_rotating_credential_takes_no_lock(+ mocker,+):+ manager = IntegrationCredentialsManager()+ credentials = _provider_runtime_credentials().model_copy(+ update={+ "refresh_strategy": "oauth_handler",+ "access_token_expires_at": int(time.time()) + 3600,+ }+ )+ handler = GitHubOAuthHandler(*_HANDLER_ARGS)+ mocker.patch.object(manager, "_get_oauth_handler", AsyncMock(return_value=handler))+ locked = mocker.patch.object(manager, "_refresh_locked", AsyncMock())result = await manager.refresh_if_needed("user-a", credentials, lock=False)assert result is credentials- locked.assert_awaited_once_with("user-a", credentials, handler=handler)- unlocked.assert_not_awaited()+ locked.assert_not_awaited()@pytest.mark.asynciodiff --git a/autogpt_platform/backend/backend/integrations/oauth/base.py b/autogpt_platform/backend/backend/integrations/oauth/base.pyindex e7e8b0bdf8..91b2a92886 100644--- a/autogpt_platform/backend/backend/integrations/oauth/base.py+++ b/autogpt_platform/backend/backend/integrations/oauth/base.py@@ -15,6 +15,9 @@ class BaseOAuthHandler(ABC):PROVIDER_NAME: ClassVar[ProviderName | str]DEFAULT_SCOPES: ClassVar[list[str]] = []# --8<-- [end:BaseOAuthHandler1]+ # True when a refresh makes the old refresh token worthless; such refreshes+ # always run under the credential lock, even for lock-free callers.+ ROTATES_REFRESH_TOKEN: ClassVar[bool] = False@abstractmethod# --8<-- [start:BaseOAuthHandler2]diff --git a/autogpt_platform/backend/backend/integrations/oauth/github.py b/autogpt_platform/backend/backend/integrations/oauth/github.pyindex d6e6a9f7d7..e9b3b120e5 100644--- a/autogpt_platform/backend/backend/integrations/oauth/github.py+++ b/autogpt_platform/backend/backend/integrations/oauth/github.py@@ -1,5 +1,5 @@import time-from typing import Optional+from typing import ClassVar, Optionalfrom urllib.parse import urlencodefrom backend.data.model import OAuth2Credentials@@ -25,6 +25,8 @@ class GitHubOAuthHandler(BaseOAuthHandler):""" # noqaPROVIDER_NAME = ProviderName.GITHUB+ # GitHub App refresh tokens are single-use; OAuth App tokens never refresh.+ ROTATES_REFRESH_TOKEN: ClassVar[bool] = Truedef __init__(self, client_id: str, client_secret: str, redirect_uri: str):self.client_id = client_iddiff --git a/autogpt_platform/backend/backend/integrations/oauth/notion.py b/autogpt_platform/backend/backend/integrations/oauth/notion.pyindex 67fc5bcc25..8691833f6f 100644--- a/autogpt_platform/backend/backend/integrations/oauth/notion.py+++ b/autogpt_platform/backend/backend/integrations/oauth/notion.py@@ -1,5 +1,6 @@+import timefrom base64 import b64encode-from typing import Optional+from typing import ClassVar, Optionalfrom urllib.parse import urlencodefrom backend.data.model import OAuth2Credentials@@ -14,11 +15,13 @@ class NotionOAuthHandler(BaseOAuthHandler):Based on the documentation at https://developers.notion.com/docs/authorizationNotes:- - Notion uses non-expiring access tokens and therefore doesn't have a refresh flow+ - Notion documents no access-token expiry, but returns a refresh token, and each+ refresh replaces both tokens- Notion doesn't use scopes"""PROVIDER_NAME = ProviderName.NOTION+ ROTATES_REFRESH_TOKEN: ClassVar[bool] = Truedef __init__(self, client_id: str, client_secret: str, redirect_uri: str):self.client_id = client_id@@ -42,11 +45,36 @@ class NotionOAuthHandler(BaseOAuthHandler):async def exchange_code_for_tokens(self, code: str, scopes: list[str], code_verifier: Optional[str]) -> OAuth2Credentials:- request_body = {- "grant_type": "authorization_code",- "code": code,- "redirect_uri": self.redirect_uri,- }+ return await self._request_tokens(+ {+ "grant_type": "authorization_code",+ "code": code,+ "redirect_uri": self.redirect_uri,+ }+ )++ async def revoke_tokens(self, credentials: OAuth2Credentials) -> bool:+ # Notion doesn't support token revocation+ return False++ async def _refresh_tokens(+ self, credentials: OAuth2Credentials+ ) -> OAuth2Credentials:+ if not credentials.refresh_token:+ return credentials+ return await self._request_tokens(+ {+ "grant_type": "refresh_token",+ "refresh_token": credentials.refresh_token.get_secret_value(),+ },+ current_credentials=credentials,+ )++ async def _request_tokens(+ self,+ request_body: dict[str, str],+ current_credentials: Optional[OAuth2Credentials] = None,+ ) -> OAuth2Credentials:auth_str = b64encode(f"{self.client_id}:{self.client_secret}".encode()).decode()headers = {"Authorization": f"Basic {auth_str}",@@ -64,13 +92,21 @@ class NotionOAuthHandler(BaseOAuthHandler):else None)- return OAuth2Credentials(+ expires_in = token_data.get("expires_in")+ new_credentials = OAuth2Credentials(provider=self.PROVIDER_NAME,- title=token_data.get("workspace_name"),+ title=(+ current_credentials.title+ if current_credentials+ else token_data.get("workspace_name")+ ),username=email,access_token=token_data["access_token"],- refresh_token=None,- access_token_expires_at=None, # Notion tokens don't expire+ refresh_token=token_data.get("refresh_token")+ or (current_credentials.refresh_token if current_credentials else None),+ access_token_expires_at=(+ int(time.time()) + expires_in if expires_in else None+ ),refresh_token_expires_at=None,scopes=[],metadata={@@ -81,17 +117,6 @@ class NotionOAuthHandler(BaseOAuthHandler):"workspace_icon": token_data.get("workspace_icon"),},)-- async def revoke_tokens(self, credentials: OAuth2Credentials) -> bool:- # Notion doesn't support token revocation- return False-- async def _refresh_tokens(- self, credentials: OAuth2Credentials- ) -> OAuth2Credentials:- # Notion doesn't support token refresh- return credentials-- def needs_refresh(self, credentials: OAuth2Credentials) -> bool:- # Notion access tokens don't expire- return False+ if current_credentials:+ new_credentials.id = current_credentials.id+ return new_credentialsdiff --git a/autogpt_platform/backend/backend/integrations/oauth/notion_test.py b/autogpt_platform/backend/backend/integrations/oauth/notion_test.pynew file mode 100644index 0000000000..cace8538c4--- /dev/null+++ b/autogpt_platform/backend/backend/integrations/oauth/notion_test.py@@ -0,0 +1,76 @@+from unittest.mock import AsyncMock, MagicMock++from pydantic import SecretStr+from pytest_mock import MockerFixture++from backend.data.model import OAuth2Credentials+from backend.integrations.oauth.notion import NotionOAuthHandler+from backend.integrations.providers import ProviderName+++async def test_exchange_keeps_the_refresh_token_notion_returns(mocker: MockerFixture):+ _mock_token_endpoint(mocker, _token_response())++ credentials = await _handler().exchange_code_for_tokens("code", [], None)++ assert credentials.refresh_token is not None+ assert credentials.refresh_token.get_secret_value() == "refresh-1"+++async def test_refresh_sends_the_refresh_token_and_stores_the_rotated_pair(+ mocker: MockerFixture,+):+ post = _mock_token_endpoint(+ mocker, _token_response(access_token="access-2", refresh_token="refresh-2")+ )+ current = OAuth2Credentials(+ id="credential-id",+ provider=ProviderName.NOTION,+ title="Renamed by the user",+ access_token=SecretStr("access-1"),+ refresh_token=SecretStr("refresh-1"),+ scopes=[],+ )++ fresh = await _handler().refresh_tokens(current)++ assert post.await_args.kwargs["json"] == {+ "grant_type": "refresh_token",+ "refresh_token": "refresh-1",+ }+ assert (fresh.id, fresh.title) == ("credential-id", "Renamed by the user")+ assert fresh.access_token.get_secret_value() == "access-2"+ assert fresh.refresh_token is not None+ assert fresh.refresh_token.get_secret_value() == "refresh-2"+++def _handler() -> NotionOAuthHandler:+ return NotionOAuthHandler(+ client_id="client-id",+ client_secret="client-secret",+ redirect_uri="https://example.com/callback",+ )+++def _token_response(**overrides: str) -> dict:+ return {+ "access_token": "access-1",+ "token_type": "bearer",+ "refresh_token": "refresh-1",+ "bot_id": "bot-id",+ "workspace_id": "workspace-id",+ "workspace_name": "Acme",+ "workspace_icon": None,+ "owner": {"type": "workspace", "workspace": True},+ "duplicated_template_id": None,+ **overrides,+ }+++def _mock_token_endpoint(mocker: MockerFixture, payload: dict) -> AsyncMock:+ post = AsyncMock(return_value=MagicMock(json=MagicMock(return_value=payload)))+ mocker.patch(+ "backend.integrations.oauth.notion.Requests",+ return_value=MagicMock(post=post),+ )+ return postdiff --git a/autogpt_platform/backend/backend/integrations/oauth/todoist.py b/autogpt_platform/backend/backend/integrations/oauth/todoist.pyindex ff879cc40d..22f628feba 100644--- a/autogpt_platform/backend/backend/integrations/oauth/todoist.py+++ b/autogpt_platform/backend/backend/integrations/oauth/todoist.py@@ -1,13 +1,23 @@+import timeimport urllib.parsefrom typing import ClassVar, Optional+from pydantic import SecretStr+from backend.data.model import OAuth2Credentials, ProviderNamefrom backend.integrations.oauth.base import BaseOAuthHandler, parse_granted_scopesfrom backend.util.request import Requestsclass TodoistOAuthHandler(BaseOAuthHandler):+ """+ Apps with refresh tokens enabled get 1-hour access tokens and rotating refresh+ tokens; legacy apps get a long-lived access token and no refresh token.+ https://developer.todoist.com/api/v1/#tag/Authorization+ """+PROVIDER_NAME = ProviderName.TODOIST+ ROTATES_REFRESH_TOKEN: ClassVar[bool] = TrueDEFAULT_SCOPES: ClassVar[list[str]] = ["task:add","data:read",@@ -63,8 +73,8 @@ class TodoistOAuthHandler(BaseOAuthHandler):title=None,username=user_email,access_token=tokens["access_token"],- refresh_token=None,- access_token_expires_at=None,+ refresh_token=tokens.get("refresh_token"),+ access_token_expires_at=_access_token_expires_at(tokens),refresh_token_expires_at=None,# Todoist's token response documents no `scope`; the request is then# all we know about the grant.@@ -74,8 +84,38 @@ class TodoistOAuthHandler(BaseOAuthHandler):async def _refresh_tokens(self, credentials: OAuth2Credentials) -> OAuth2Credentials:- # Todoist does not support token refresh- return credentials+ if not credentials.refresh_token:+ raise ValueError("No refresh token available")+ response = await Requests().post(+ self.TOKEN_URL,+ data={+ "client_id": self.client_id,+ "client_secret": self.client_secret,+ "grant_type": "refresh_token",+ "refresh_token": credentials.refresh_token.get_secret_value(),+ },+ )+ tokens = response.json()+ # A replayed refresh omits the new token, and the one we sent is spent:+ # sending it again revokes every token the user gave this app.+ refresh_token = tokens.get("refresh_token")+ return credentials.model_copy(+ update={+ "access_token": SecretStr(tokens["access_token"]),+ "refresh_token": SecretStr(refresh_token) if refresh_token else None,+ "access_token_expires_at": int(time.time()) + int(tokens["expires_in"]),+ "scopes": parse_granted_scopes(+ tokens.get("scope"), fallback=credentials.scopes+ ),+ }+ )async def revoke_tokens(self, credentials: OAuth2Credentials) -> bool:return False+++def _access_token_expires_at(tokens: dict) -> int | None:+ # Legacy apps send a 10-year placeholder expiry and no refresh token.+ if not tokens.get("refresh_token") or not tokens.get("expires_in"):+ return None+ return int(time.time()) + int(tokens["expires_in"])diff --git a/autogpt_platform/backend/backend/integrations/oauth/todoist_test.py b/autogpt_platform/backend/backend/integrations/oauth/todoist_test.pynew file mode 100644index 0000000000..385362315d--- /dev/null+++ b/autogpt_platform/backend/backend/integrations/oauth/todoist_test.py@@ -0,0 +1,123 @@+import time+from unittest.mock import AsyncMock, MagicMock++import pytest+from pydantic import SecretStr+from pytest_mock import MockerFixture++from backend.data.model import OAuth2Credentials+from backend.integrations.oauth.todoist import TodoistOAuthHandler+from backend.integrations.providers import ProviderName+++ "token_response,refresh_token,expires",+ [+ pytest.param(+ {"access_token": "a", "expires_in": 3600, "refresh_token": "r"},+ "r",+ True,+ id="refresh-enabled app",+ ),+ pytest.param(+ {"access_token": "a", "expires_in": 315360000},+ None,+ False,+ id="legacy app",+ ),+ ],+)+async def test_exchange_stores_the_refresh_token_and_expiry_only_when_issued(+ mocker: MockerFixture, token_response, refresh_token, expires+):+ _mock_token_endpoint(mocker, token_response)++ credentials = await _handler().exchange_code_for_tokens("code", [], None)++ stored = credentials.refresh_token+ assert (stored.get_secret_value() if stored else None) == refresh_token+ assert (credentials.access_token_expires_at is not None) == expires+++async def test_refresh_sends_the_refresh_token_and_stores_the_rotated_one(+ mocker: MockerFixture,+):+ post = _mock_token_endpoint(+ mocker, {"access_token": "a2", "expires_in": 3600, "refresh_token": "r2"}+ )++ fresh = await _handler().refresh_tokens(_credentials())++ assert post.await_args.kwargs["data"]["grant_type"] == "refresh_token"+ assert post.await_args.kwargs["data"]["refresh_token"] == "r1"+ assert fresh.id == "credential-id"+ assert fresh.access_token.get_secret_value() == "a2"+ assert fresh.refresh_token is not None+ assert fresh.refresh_token.get_secret_value() == "r2"+ assert fresh.access_token_expires_at is not None+ assert fresh.access_token_expires_at > time.time()+++async def test_replayed_refresh_drops_the_spent_token_and_fails_after_expiry(+ mocker: MockerFixture,+):+ # Todoist answers a reused refresh token inside its 60 s window without one.+ post = _mock_token_endpoint(mocker, {"access_token": "a2", "expires_in": 3600})+ clock = mocker.patch("time.time", return_value=1_750_000_000)+ handler = _handler()++ fresh = await handler.refresh_tokens(_credentials())++ assert fresh.refresh_token is None+ assert fresh.access_token_expires_at == 1_750_003_600+ assert await handler.get_access_token(fresh) == "a2"++ clock.return_value = 1_750_003_601+ with pytest.raises(ValueError, match="No refresh token available"):+ await handler.get_access_token(fresh)+ post.assert_awaited_once()+++async def test_legacy_access_token_remains_usable_without_a_refresh_token(+ mocker: MockerFixture,+):+ post = _mock_token_endpoint(mocker, {})+ credentials = _credentials().model_copy(+ update={"refresh_token": None, "access_token_expires_at": None}+ )++ assert await _handler().get_access_token(credentials) == "a1"+ post.assert_not_awaited()+++def _handler() -> TodoistOAuthHandler:+ return TodoistOAuthHandler(+ client_id="client-id",+ client_secret="client-secret",+ redirect_uri="https://example.com/callback",+ )+++def _credentials() -> OAuth2Credentials:+ return OAuth2Credentials(+ id="credential-id",+ provider=ProviderName.TODOIST,+ access_token=SecretStr("a1"),+ refresh_token=SecretStr("r1"),+ access_token_expires_at=1,+ scopes=["data:read"],+ )+++def _mock_token_endpoint(mocker: MockerFixture, payload: dict) -> AsyncMock:+ def respond(url: str, **_kwargs) -> MagicMock:+ if url == TodoistOAuthHandler.TOKEN_URL:+ return MagicMock(json=MagicMock(return_value=payload))+ return MagicMock(json=MagicMock(return_value={"user": {"email": "[email protected]"}}))++ post = AsyncMock(side_effect=respond)+ mocker.patch(+ "backend.integrations.oauth.todoist.Requests",+ return_value=MagicMock(post=post),+ )+ return postdiff --git a/autogpt_platform/backend/backend/integrations/oauth/twitter.py b/autogpt_platform/backend/backend/integrations/oauth/twitter.pyindex 2e73674b4e..182f0a5035 100644--- a/autogpt_platform/backend/backend/integrations/oauth/twitter.py+++ b/autogpt_platform/backend/backend/integrations/oauth/twitter.py@@ -9,6 +9,7 @@ from backend.util.request import Requestsclass TwitterOAuthHandler(BaseOAuthHandler):PROVIDER_NAME = ProviderName.TWITTER+ ROTATES_REFRESH_TOKEN: ClassVar[bool] = TrueDEFAULT_SCOPES: ClassVar[list[str]] = ["tweet.read","tweet.write",