Skip to content

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): proactively refresh OAuth credentials before expiry (#15619)

AutoGPT8eaa8bf661b9
diff --git a/autogpt_platform/backend/backend/data/credential_refresh_test.py b/autogpt_platform/backend/backend/data/credential_refresh_test.py
new file mode 100644
index 0000000000..3cb6b3b0a7
--- /dev/null
+++ b/autogpt_platform/backend/backend/data/credential_refresh_test.py
@@ -0,0 +1,66 @@
+from unittest.mock import AsyncMock, MagicMock
+
+import pytest
+from pydantic import SecretStr
+
+from backend.data.model import OAuth2Credentials
+from backend.data.user import get_near_expiry_oauth_credentials
+from backend.util.encryption import JSONCryptor
+
+
+ "access_expiry,refresh_expiry,eligible",
+ [
+ (None, None, False),
+ (999, None, False),
+ (1_000, None, False),
+ (1_001, None, True),
+ (1_299, None, True),
+ (1_300, None, False),
+ (1_301, None, False),
+ (1_100, 999, False),
+ (1_100, 1_000, False),
+ (1_100, 1_001, True),
+ ],
+)
+async def test_refresh_candidate_expiry_boundaries(
+ mocker, access_expiry, refresh_expiry, eligible
+):
+ credentials = OAuth2Credentials(
+ id="credential",
+ provider="github",
+ access_token=SecretStr("access"),
+ refresh_token=SecretStr("refresh"),
+ access_token_expires_at=access_expiry,
+ refresh_token_expires_at=refresh_expiry,
+ scopes=[],
+ )
+ row = MagicMock(
+ id=credentials.id,
+ ownerId="owner",
+ encryptedPayload=JSONCryptor().encrypt(credentials.model_dump()),
+ )
+ database = mocker.patch("backend.data.user.prisma")
+ database.integrationcredential.find_many = AsyncMock(return_value=[row])
+ mocker.patch("time.time", return_value=1_000)
+
+ result = await get_near_expiry_oauth_credentials(1_300)
+
+ assert result == ([("owner", credentials)] if eligible else [])
+
+
+async def test_corrupt_candidate_does_not_log_decrypted_secrets(mocker, caplog):
+ secret = "do-not-log-this-access-token"
+ row = MagicMock(
+ id="corrupt",
+ ownerId="owner",
+ encryptedPayload=JSONCryptor().encrypt(
+ {"access_token": secret, "type": "unsupported"}
+ ),
+ )
+ database = mocker.patch("backend.data.user.prisma")
+ database.integrationcredential.find_many = AsyncMock(return_value=[row])
+
+ assert await get_near_expiry_oauth_credentials(1_300) == []
+ assert "Corrupt credential row corrupt" in caplog.text
+ assert secret not in caplog.text
diff --git a/autogpt_platform/backend/backend/data/db_manager.py b/autogpt_platform/backend/backend/data/db_manager.py
index e5dbf6aed1..ebbf9a8675 100644
--- a/autogpt_platform/backend/backend/data/db_manager.py
+++ b/autogpt_platform/backend/backend/data/db_manager.py
@@ -187,6 +187,7 @@ from backend.data.user import (
get_billing_email_recipient,
get_briefing_candidate,
get_briefing_candidates,
+ get_near_expiry_oauth_credentials,
get_user_by_id,
get_user_credentials,
get_user_default_chat_route,
@@ -411,6 +412,7 @@ class DatabaseManager(AppService):
heal_orphaned_auth_identities = _(heal_orphaned_auth_identities)
get_user_integrations = _(get_user_integrations)
update_user_integrations = _(update_user_integrations)
+ get_near_expiry_oauth_credentials = _(get_near_expiry_oauth_credentials)
get_user_credentials = _(get_user_credentials)
set_user_credentials = _(set_user_credentials)
@@ -908,6 +910,7 @@ class DatabaseManagerAsyncClient(AppServiceClient):
heal_orphaned_auth_identities = d.heal_orphaned_auth_identities
get_user_integrations = d.get_user_integrations
update_user_integrations = d.update_user_integrations
+ get_near_expiry_oauth_credentials = d.get_near_expiry_oauth_credentials
get_user_credentials = d.get_user_credentials
set_user_credentials = d.set_user_credentials
diff --git a/autogpt_platform/backend/backend/data/user.py b/autogpt_platform/backend/backend/data/user.py
index b055e381a3..8439a53bc6 100644
--- a/autogpt_platform/backend/backend/data/user.py
+++ b/autogpt_platform/backend/backend/data/user.py
@@ -3,6 +3,7 @@ import base64
import hashlib
import hmac
import logging
+import time
from datetime import datetime, timedelta, timezone
from typing import TYPE_CHECKING, Optional, cast
from urllib.parse import quote_plus
@@ -21,6 +22,7 @@ from backend.data.db import prisma, query_raw_with_schema, transaction
from backend.data.model import (
CREDENTIALS_ADAPTER,
Credentials,
+ OAuth2Credentials,
User,
UserIntegrations,
UserMetadata,
@@ -462,6 +464,50 @@ async def get_user_credentials(user_id: str) -> list[Credentials]:
return credentials
+async def get_near_expiry_oauth_credentials(
+ refresh_before: int,
+) -> list[tuple[str, OAuth2Credentials]]:
+ """Return still-valid user OAuth credentials approaching expiry.
+
+ This cross-user query is for trusted system callers such as the scheduler,
+ never user-scoped requests. Expired grants stay on the on-demand path.
+ """
+ rows = await prisma.integrationcredential.find_many(
+ where={"ownerType": "USER", "status": "active", "credentialType": "oauth2"}
+ )
+ now = int(time.time())
+ cryptor = JSONCryptor()
+ candidates: list[tuple[str, OAuth2Credentials]] = []
+
+ for row in rows:
+ try:
+ credential = CREDENTIALS_ADAPTER.validate_python(
+ cryptor.decrypt(row.encryptedPayload)
+ )
+ except Exception:
+ logger.error(
+ f"Corrupt credential row {row.id} for user {row.ownerId}; skipping"
+ )
+ continue
+
+ if credential.type != "oauth2":
+ continue
+ if (
+ credential.refresh_strategy == "oauth_handler"
+ and not credential.is_managed
+ and credential.refresh_token is not None
+ and credential.access_token_expires_at is not None
+ and now < credential.access_token_expires_at < refresh_before
+ and (
+ credential.refresh_token_expires_at is None
+ or credential.refresh_token_expires_at > now
+ )
+ ):
+ candidates.append((row.ownerId, credential))
+
+ return candidates
+
+
async def set_user_credentials(user_id: str, credentials: list[Credentials]) -> None:
"""Full-list replace of the user's USER-scoped credential rows.
diff --git a/autogpt_platform/backend/backend/data/user_test.py b/autogpt_platform/backend/backend/data/user_test.py
index 5270c326c8..49504873e6 100644
--- a/autogpt_platform/backend/backend/data/user_test.py
+++ b/autogpt_platform/backend/backend/data/user_test.py
@@ -1055,6 +1055,83 @@ class TestTableBackedCredentials:
where = mock_prisma.integrationcredential.find_many.call_args.kwargs["where"]
assert where == {"ownerType": "USER", "ownerId": "u1", "status": "active"}
+ @pytest.mark.asyncio
+ async def test_get_near_expiry_oauth_credentials_returns_only_refreshable_due_rows(
+ self, mocker
+ ):
+ from pydantic import SecretStr
+
+ from backend.data.model import APIKeyCredentials, OAuth2Credentials
+ from backend.data.user import get_near_expiry_oauth_credentials
+ from backend.util.encryption import JSONCryptor
+
+ due = OAuth2Credentials(
+ id="due",
+ provider="github",
+ access_token=SecretStr("access"),
+ access_token_expires_at=1_299,
+ refresh_token=SecretStr("refresh"),
+ scopes=[],
+ )
+ future = due.model_copy(
+ update={"id": "future", "access_token_expires_at": 1_301}
+ )
+ provider_runtime = due.model_copy(
+ update={"id": "runtime", "refresh_strategy": "provider_runtime"}
+ )
+ managed = due.model_copy(update={"id": "managed", "is_managed": True})
+ without_refresh = due.model_copy(
+ update={"id": "without-refresh", "refresh_token": None}
+ )
+ expired = due.model_copy(
+ update={"id": "expired", "access_token_expires_at": 999}
+ )
+ expired_refresh = due.model_copy(
+ update={"id": "expired-refresh", "refresh_token_expires_at": 999}
+ )
+ without_expiry = due.model_copy(
+ update={"id": "without-expiry", "access_token_expires_at": None}
+ )
+ api_key = APIKeyCredentials(
+ id="api-key", provider="github", api_key=SecretStr("key")
+ )
+ cryptor = JSONCryptor()
+
+ def row(owner_id, credentials):
+ return MagicMock(
+ id=credentials.id,
+ ownerId=owner_id,
+ encryptedPayload=cryptor.encrypt(credentials.model_dump()),
+ )
+
+ corrupt = MagicMock(
+ id="corrupt", ownerId="user-corrupt", encryptedPayload="bad"
+ )
+ mock_prisma = MagicMock()
+ mock_prisma.integrationcredential.find_many = AsyncMock(
+ return_value=[
+ row("user-due", due),
+ row("user-future", future),
+ row("user-runtime", provider_runtime),
+ row("user-managed", managed),
+ row("user-without-refresh", without_refresh),
+ row("user-expired", expired),
+ row("user-expired-refresh", expired_refresh),
+ row("user-without-expiry", without_expiry),
+ row("user-api", api_key),
+ corrupt,
+ ]
+ )
+ mocker.patch("backend.data.user.prisma", mock_prisma)
+ mocker.patch("time.time", return_value=1_000)
+
+ result = await get_near_expiry_oauth_credentials(1_300)
+
+ assert result == [("user-due", due)]
+ mock_prisma.integrationcredential.find_many.assert_awaited_once_with(
+ where={"ownerType": "USER", "status": "active", "credentialType": "oauth2"}
+ )
+
@pytest.mark.asyncio
async def test_set_updates_existing_creates_new_revokes_missing(self, mocker):
from unittest.mock import AsyncMock, MagicMock
diff --git a/autogpt_platform/backend/backend/executor/scheduler.py b/autogpt_platform/backend/backend/executor/scheduler.py
index d85494c65f..a8adbf1530 100644
--- a/autogpt_platform/backend/backend/executor/scheduler.py
+++ b/autogpt_platform/backend/backend/executor/scheduler.py
@@ -65,6 +65,7 @@ from backend.executor import schedule_events
from backend.executor import utils as execution_utils
from backend.executor.jobstore import ResilientSQLAlchemyJobStore
from backend.executor.schedule_index import ScheduleIndex, ScheduleIndexEntry
+from backend.integrations.creds_manager import IntegrationCredentialsManager
from backend.monitoring import (
flush_matured_alerts,
report_block_error_rates,
@@ -200,7 +201,7 @@ def get_event_loop():
return _event_loop
-def run_async(coro, timeout: float = SCHEDULER_OPERATION_TIMEOUT_SECONDS):
+def run_async(coro, timeout: float | None = SCHEDULER_OPERATION_TIMEOUT_SECONDS):
"""Run a coroutine in the shared event loop and wait for completion."""
loop = get_event_loop()
future = asyncio.run_coroutine_threadsafe(coro, loop)
@@ -1852,6 +1853,13 @@ def _clear_dream_registration_marker(user_id: str, key_prefix: str) -> None:
)
+def refresh_near_expiry_credentials() -> None:
+ """Keep max_instances guarding the sweep until every refresh finishes."""
+ run_async(
+ IntegrationCredentialsManager().refresh_near_expiry_credentials(), timeout=None
+ )
+
+
def cleanup_oauth_tokens():
"""Clean up expired OAuth tokens from the database."""
@@ -2575,6 +2583,17 @@ class Scheduler(AppService):
jobstore=Jobstores.EXECUTION.value,
)
+ # Credential Refresh - keep third-party OAuth credentials valid before use.
+ self.scheduler.add_job(
+ refresh_near_expiry_credentials,
+ id="refresh_near_expiry_credentials",
+ trigger="interval",
+ replace_existing=True,
+ max_instances=1,
+ seconds=60,
+ jobstore=Jobstores.EXECUTION.value,
+ )
+
# OAuth Token Cleanup - configurable interval
self.scheduler.add_job(
cleanup_oauth_tokens,
diff --git a/autogpt_platform/backend/backend/executor/scheduler_unit_test.py b/autogpt_platform/backend/backend/executor/scheduler_unit_test.py
index 9e90e3501a..61834f9907 100644
--- a/autogpt_platform/backend/backend/executor/scheduler_unit_test.py
+++ b/autogpt_platform/backend/backend/executor/scheduler_unit_test.py
@@ -9,6 +9,7 @@ import asyncio
import logging
import threading
import time
+from concurrent.futures import ThreadPoolExecutor
from datetime import datetime, timedelta, timezone
from typing import NamedTuple
from unittest.mock import AsyncMock, MagicMock, patch
@@ -25,6 +26,7 @@ from backend.copilot.permissions import (
ROUTINE_SELF_ESCALATION_TOOLS,
)
from backend.copilot.rate_limit import RateLimitExceeded, RateLimitUnavailable
+from backend.executor import scheduler as scheduler_module
from backend.executor.scheduler import (
_MAX_CAP_RETRIES,
_MAX_EXPERT_LOOKUP_RETRIES,
@@ -51,6 +53,7 @@ from backend.executor.scheduler import (
_self_delete_copilot_turn_schedule,
_self_delete_morning_briefing_schedule,
reconcile_stripe_tiers,
+ refresh_near_expiry_credentials,
)
from backend.util.exceptions import (
ExpertNotFoundError,
@@ -1740,6 +1743,64 @@ def test_reconcile_stripe_tiers_job_registered_with_interval_and_single_instance
assert call.kwargs["seconds"] == 6 * 3600
+def test_refresh_near_expiry_credentials_job_is_registered_every_minute(monkeypatch):
+ calls = _registered_jobs(monkeypatch, interval_hours=6).add_job_calls
+
+ matches = [
+ call
+ for call in calls
+ if call.args and call.args[0] is refresh_near_expiry_credentials
+ ]
+
+ assert len(matches) == 1
+ call = matches[0]
+ assert call.kwargs["id"] == "refresh_near_expiry_credentials"
+ assert call.kwargs["trigger"] == "interval"
+ assert call.kwargs["seconds"] == 60
+ assert call.kwargs["replace_existing"] is True
+ assert call.kwargs["max_instances"] == 1
+ assert call.kwargs["jobstore"] == "execution"
+
+
+def test_refresh_job_stays_active_until_the_coroutine_finishes(monkeypatch):
+ loop = asyncio.new_event_loop()
+ thread = threading.Thread(target=loop.run_forever, daemon=True)
+ started, release, completed = (threading.Event() for _ in range(3))
+ run_async = scheduler_module.run_async
+
+ async def refresh():
+ started.set()
+ await asyncio.to_thread(release.wait)
+ completed.set()
+
+ def short_timeout(coro, timeout=0.01):
+ return run_async(coro, timeout=timeout)
+
+ monkeypatch.setattr(scheduler_module, "_event_loop", loop)
+ monkeypatch.setattr(scheduler_module, "run_async", short_timeout)
+ monkeypatch.setattr(
+ scheduler_module,
+ "IntegrationCredentialsManager",
+ lambda: MagicMock(refresh_near_expiry_credentials=refresh),
+ )
+ thread.start()
+ try:
+ with ThreadPoolExecutor(max_workers=1) as pool:
+ job = pool.submit(refresh_near_expiry_credentials)
+ try:
+ assert started.wait(timeout=2)
+ assert not completed.wait(timeout=0.1)
+ assert not job.done()
+ finally:
+ release.set()
+ assert completed.wait(timeout=2)
+ job.result(timeout=2)
+ finally:
+ loop.call_soon_threadsafe(loop.stop)
+ thread.join(timeout=2)
+ loop.close()
+
+
def test_reconcile_stripe_tiers_interval_follows_config_setting(monkeypatch):
"""Changing the configured interval changes the registered ``seconds``."""
calls = _registered_jobs(monkeypatch, interval_hours=12).add_job_calls
diff --git a/autogpt_platform/backend/backend/integrations/creds_manager.py b/autogpt_platform/backend/backend/integrations/creds_manager.py
index e94769418a..19182443bf 100644
--- a/autogpt_platform/backend/backend/integrations/creds_manager.py
+++ b/autogpt_platform/backend/backend/integrations/creds_manager.py
@@ -1,5 +1,6 @@
import logging
import os
+import time
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
from datetime import datetime
@@ -238,6 +239,22 @@ class IntegrationCredentialsManager:
return get_credentials
+ async def refresh_near_expiry_credentials(self) -> None:
+ """Refresh eligible OAuth credentials that enter the five-minute window."""
+ refresh_before = int(time.time()) + 300
+ candidates = await self.store.db_manager.get_near_expiry_oauth_credentials(
+ refresh_before
+ )
+
+ for user_id, credentials in candidates:
+ try:
+ await self.refresh_if_needed(user_id, credentials)
+ except Exception as error:
+ logger.warning(
+ f"Proactive refresh failed for credential #{credentials.id} "
+ f"belonging to user {user_id[:12]} ({type(error).__name__})"
+ )
+
async def refresh_if_needed(
self, user_id: str, credentials: OAuth2Credentials, lock: bool = True
) -> OAuth2Credentials:
@@ -292,22 +309,9 @@ class IntegrationCredentialsManager:
handler: "BaseOAuthHandler | BaseDeviceAuthHandler | None" = None,
) -> OAuth2Credentials:
async with self._locked(user_id, credentials.id, "refresh"):
- # A different worker may have completed the refresh while this one
- # waited for the refresh mutex. Re-read before deciding, otherwise
- # rotating providers can receive the same old refresh token twice.
- current = await self.store.get_creds_by_id(user_id, credentials.id)
- if current is None:
- raise ValueError(
- f"Credentials #{credentials.id} for user #{user_id} not found"
- )
- if current.type != "oauth2":
- raise TypeError(
- f"Credentials #{credentials.id} for user #{user_id} changed "
- f"type to {current.type!r} during OAuth refresh"
- )
- if current.provider != credentials.provider:
- raise RuntimeError("Credential provider changed during refresh")
- credentials = current
+ credentials = await self._reload_oauth_credentials(user_id, credentials)
+ if credentials.refresh_strategy == "provider_runtime":
+ return credentials
oauth_handler = handler or await self._get_oauth_handler(credentials)
if oauth_handler.needs_refresh(credentials):
logger.debug(
@@ -318,6 +322,14 @@ class IntegrationCredentialsManager:
# Wait until the credentials are no longer in use anywhere
_lock = await self._acquire_lock(user_id, credentials.id)
try:
+ credentials = await self._reload_oauth_credentials(
+ user_id, credentials
+ )
+ if (
+ credentials.refresh_strategy == "provider_runtime"
+ or not oauth_handler.needs_refresh(credentials)
+ ):
+ return credentials
fresh_credentials = await oauth_handler.refresh_tokens(credentials)
await self.store.update_creds(user_id, fresh_credentials)
await _invoke_creds_changed_hook(
@@ -335,6 +347,24 @@ class IntegrationCredentialsManager:
)
return credentials
+ async def _reload_oauth_credentials(
+ self, user_id: str, credentials: OAuth2Credentials
+ ) -> OAuth2Credentials:
+ """Re-read after either lock wait; a lease holder may replace or delete it."""
+ current = await self.store.get_creds_by_id(user_id, credentials.id)
+ if current is None:
+ raise ValueError(
+ f"Credentials #{credentials.id} for user #{user_id} not found"
+ )
+ if current.type != "oauth2":
+ raise TypeError(
+ f"Credentials #{credentials.id} for user #{user_id} changed "
+ f"type to {current.type!r} during OAuth refresh"
+ )
+ if current.provider != credentials.provider:
+ raise RuntimeError("Credential provider changed during refresh")
+ return current
+
async def _refresh_unlocked(
self,
user_id: str,
diff --git a/autogpt_platform/backend/backend/integrations/creds_manager_test.py b/autogpt_platform/backend/backend/integrations/creds_manager_test.py
index 47d5bca97d..d01697d2d5 100644
--- a/autogpt_platform/backend/backend/integrations/creds_manager_test.py
+++ b/autogpt_platform/backend/backend/integrations/creds_manager_test.py
@@ -12,6 +12,7 @@ from backend.integrations.creds_manager import (
register_creds_changed_hook,
unregister_creds_changed_hook,
)
+from backend.integrations.oauth.github import GitHubOAuthHandler
@pytest.fixture(autouse=True)
@@ -86,6 +87,114 @@ class TestInvokeCredsChangedHook:
assert calls == [("user-a", "github"), ("user-b", "slack")]
+async def test_refresh_near_expiry_credentials_refreshes_each_candidate(mocker):
+ manager = IntegrationCredentialsManager()
+ first = _provider_runtime_credentials().model_copy(
+ update={"id": "cred-1", "refresh_strategy": "oauth_handler"}
+ )
+ second = first.model_copy(update={"id": "cred-2"})
+ get_candidates = mocker.patch.object(
+ manager.store.db_manager,
+ "get_near_expiry_oauth_credentials",
+ AsyncMock(return_value=[("user-1", first), ("user-2", second)]),
+ )
+ refresh = mocker.patch.object(manager, "refresh_if_needed", AsyncMock())
+
+ with patch("backend.integrations.creds_manager.time.time", return_value=1_000):
+ await manager.refresh_near_expiry_credentials()
+
+ get_candidates.assert_awaited_once_with(1_300)
+ assert refresh.await_args_list == [
+ (("user-1", first), {}),
+ (("user-2", second), {}),
+ ]
+
+
+async def test_refresh_near_expiry_credentials_continues_after_a_failure(
+ mocker, caplog
+):
+ manager = IntegrationCredentialsManager()
+ first = _provider_runtime_credentials().model_copy(
+ update={"id": "cred-1", "refresh_strategy": "oauth_handler"}
+ )
+ second = first.model_copy(update={"id": "cred-2"})
+ mocker.patch.object(
+ manager.store.db_manager,
+ "get_near_expiry_oauth_credentials",
+ AsyncMock(return_value=[("user-1", first), ("user-2", second)]),
+ )
+ refresh = mocker.patch.object(
+ manager,
+ "refresh_if_needed",
+ AsyncMock(side_effect=[RuntimeError("provider rejected secret-token"), None]),
+ )
+
+ await manager.refresh_near_expiry_credentials()
+
+ assert refresh.await_args_list == [
+ (("user-1", first), {}),
+ (("user-2", second), {}),
+ ]
+ assert "secret-token" not in caplog.text
+
+
+async def test_sweep_persists_rotated_tokens_and_does_not_refresh_them_twice(mocker):
+ manager = IntegrationCredentialsManager()
+ original = _provider_runtime_credentials().model_copy(
+ update={
+ "provider": "github",
+ "refresh_strategy": "oauth_handler",
+ "access_token_expires_at": 1_100,
+ }
+ )
+ stored = {original.id: original}
+ handler = GitHubOAuthHandler("client", "secret", "https://example.com/callback")
+ mocker.patch("time.time", return_value=1_000)
+ mocker.patch.object(
+ manager.store.db_manager,
+ "get_near_expiry_oauth_credentials",
+ AsyncMock(return_value=[("user-a", original)]),
+ )
+ mocker.patch.object(manager, "_get_oauth_handler", AsyncMock(return_value=handler))
+ mocker.patch.object(manager, "_locked", return_value=_noop_lock_context())
+ mocker.patch.object(manager, "_acquire_lock", AsyncMock(return_value=_owned_lock()))
+ mocker.patch.object(
+ manager.store,
+ "get_creds_by_id",
+ AsyncMock(side_effect=lambda _user, credential_id: stored[credential_id]),
+ )
+
+ async def persist(_user, credentials):
+ stored[credentials.id] = credentials
+
+ mocker.patch.object(manager.store, "update_creds", AsyncMock(side_effect=persist))
+ response = MagicMock()
+ response.json.return_value = {
+ "access_token": "rotated-access",
+ "refresh_token": "rotated-refresh",
+ "expires_in": 3_600,
+ "refresh_token_expires_in": 7_200,
+ "scope": "repo",
+ }
+ post = AsyncMock(return_value=response)
+ mocker.patch(
+ "backend.integrations.oauth.github.Requests", return_value=MagicMock(post=post)
+ )
+ mocker.patch.object(handler, "_request_username", AsyncMock(return_value="user"))
+
+ await manager.refresh_near_expiry_credentials()
+ await manager.refresh_near_expiry_credentials()
+
+ post.assert_awaited_once()
+ assert post.call_args.kwargs["data"]["refresh_token"] == "refresh"
+ assert stored[original.id].access_token.get_secret_value() == "rotated-access"
+ assert stored[original.id].refresh_token == SecretStr("rotated-refresh")
+ assert stored[original.id].access_token_expires_at == 4_600
+
+
@pytest.mark.asyncio
async def test_provider_runtime_refresh_never_resolves_generic_handler(mocker):
manager = IntegrationCredentialsManager()
@@ -177,6 +286,40 @@ async def test_locked_refresh_reloads_after_waiting_for_rotating_provider(mocker
acquire_lock.assert_not_awaited()
[email protected]("replacement_exists", [True, False])
+async def test_locked_refresh_reloads_after_waiting_for_a_lease(
+ mocker, replacement_exists
+):
+ manager = IntegrationCredentialsManager()
+ stale = _provider_runtime_credentials().model_copy(
+ update={"refresh_strategy": "oauth_handler", "access_token_expires_at": 1_100}
+ )
+ fresh = stale.model_copy(update={"access_token_expires_at": 2_000})
+ lock = _owned_lock()
+ mocker.patch.object(manager, "_locked", return_value=_noop_lock_context())
+ mocker.patch.object(manager, "_acquire_lock", AsyncMock(return_value=lock))
+ mocker.patch.object(
+ manager.store,
+ "get_creds_by_id",
+ AsyncMock(side_effect=[stale, fresh if replacement_exists else None]),
+ )
+ update = mocker.patch.object(manager.store, "update_creds", AsyncMock())
+ handler = MagicMock()
+ handler.needs_refresh.side_effect = lambda c: c.access_token_expires_at < 1_300
+ handler.refresh_tokens = AsyncMock(return_value=fresh)
+
+ if replacement_exists:
+ assert await manager._refresh_locked("user-a", stale, handler) is fresh
+ else:
+ with pytest.raises(ValueError, match="not found"):
+ await manager._refresh_locked("user-a", stale, handler)
+
+ handler.refresh_tokens.assert_not_awaited()
+ update.assert_not_awaited()
+ lock.release.assert_awaited_once()
+
+
@pytest.mark.asyncio
async def test_linear_refresh_uses_locked_path_even_when_copilot_requests_unlocked(
mocker,
diff --git a/autogpt_platform/backend/backend/integrations/oauth/google.py b/autogpt_platform/backend/backend/integrations/oauth/google.py
index afe710f862..60eaae1bae 100644
--- a/autogpt_platform/backend/backend/integrations/oauth/google.py
+++ b/autogpt_platform/backend/backend/integrations/oauth/google.py
@@ -1,3 +1,4 @@
+import asyncio
import logging
from typing import Optional
@@ -177,7 +178,7 @@ class GoogleOAuthHandler(BaseOAuthHandler):
assert google_creds.refresh_token
assert google_creds.scopes
- google_creds.refresh(Request())
+ await asyncio.to_thread(google_creds.refresh, Request())
assert google_creds.expiry
return OAuth2Credentials(
diff --git a/autogpt_platform/backend/backend/integrations/oauth/google_test.py b/autogpt_platform/backend/backend/integrations/oauth/google_test.py
index 9e69d80048..59b5be88c4 100644
--- a/autogpt_platform/backend/backend/integrations/oauth/google_test.py
+++ b/autogpt_platform/backend/backend/integrations/oauth/google_test.py
@@ -8,6 +8,9 @@ google-auth's AuthorizedSession, which expected a google-auth
Credentials and then called .before_request() on it.
"""
+import asyncio
+from datetime import datetime, timezone
+from threading import Event
from unittest.mock import AsyncMock, MagicMock
import pytest
@@ -40,6 +43,36 @@ def _creds() -> OAuth2Credentials:
)
+async def test_refresh_keeps_the_event_loop_responsive(mocker: MockerFixture):
+ heartbeat = Event()
+ progressed = []
+ google_creds = MagicMock(
+ token="new-access",
+ refresh_token="refresh-token-value",
+ expiry=datetime(2030, 1, 1, tzinfo=timezone.utc),
+ scopes=_creds().scopes,
+ )
+
+ def refresh(_request):
+ progressed.append(heartbeat.wait(timeout=1))
+
+ google_creds.refresh.side_effect = refresh
+ mocker.patch(
+ "backend.integrations.oauth.google.Credentials", return_value=google_creds
+ )
+ asyncio.get_running_loop().call_soon(heartbeat.set)
+ credentials = _creds()
+
+ result = await _handler().refresh_tokens(credentials)
+
+ assert progressed == [True]
+ assert result.id == credentials.id
+ assert result.access_token.get_secret_value() == "new-access"
+ assert result.refresh_token == credentials.refresh_token
+ assert result.access_token_expires_at == 1_893_456_000
+
+
@pytest.mark.asyncio
async def test_revoke_tokens_does_not_call_before_request(mocker: MockerFixture):
"""revoke_tokens must not pass our Pydantic credentials into