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/linear): split the refreshed token's space-separated scopes (#14738)
Reinier van der Leere5b97f7501e2
diff --git a/autogpt_platform/backend/backend/blocks/linear/_oauth.py b/autogpt_platform/backend/backend/blocks/linear/_oauth.pyindex 66dc6456f8..89f6aa8fd5 100644--- a/autogpt_platform/backend/backend/blocks/linear/_oauth.py+++ b/autogpt_platform/backend/backend/blocks/linear/_oauth.py@@ -15,6 +15,7 @@ from backend.sdk import (ProviderName,Requests,SecretStr,+ parse_granted_scopes,)@@ -239,10 +240,9 @@ class LinearOAuthHandler(BaseOAuthHandler):title=current_credentials.title if current_credentials else None,username=username or "Unknown User",access_token=token_data["access_token"],- scopes=(- token_data["scope"].split(",")- if "scope" in token_data- else (current_credentials.scopes if current_credentials else [])+ scopes=parse_granted_scopes(+ token_data.get("scope"),+ fallback=current_credentials.scopes if current_credentials else [],),refresh_token=token_data.get("refresh_token"),access_token_expires_at=access_token_expires_at,diff --git a/autogpt_platform/backend/backend/integrations/oauth/granted_scopes_test.py b/autogpt_platform/backend/backend/integrations/oauth/granted_scopes_test.pyindex 3023bf338d..25c55a0633 100644--- a/autogpt_platform/backend/backend/integrations/oauth/granted_scopes_test.py+++ b/autogpt_platform/backend/backend/integrations/oauth/granted_scopes_test.py@@ -13,6 +13,7 @@ from pydantic import SecretStrfrom backend.blocks.airtable._api import OAuthTokenResponse as AirtableTokenResponsefrom backend.blocks.airtable._oauth import AirtableOAuthHandler+from backend.blocks.linear._oauth import LinearOAuthHandlerfrom backend.blocks.mcp.oauth import MCPOAuthHandlerfrom backend.blocks.wordpress._api import OAuthTokenResponse as WordPressTokenResponsefrom backend.blocks.wordpress._oauth import WordPressOAuthHandler@@ -217,6 +218,69 @@ async def test_refresh_that_narrows_the_scopes_is_refused_by_the_store(mocker):await store.update_creds("user-a", oauth_credentials(["identity", "read"]))+async def test_linear_refresh_splits_the_space_separated_scope():+ """Linear is asked with commas and answers with spaces; `.split(",")` kept one."""+ with mock_requests(+ "backend.blocks.linear._oauth",+ post=[+ {+ "access_token": "new",+ "refresh_token": "rt2",+ "expires_in": 3600,+ "scope": "issues:create read",+ }+ ],+ ):+ refreshed = await LinearOAuthHandler(*CLIENT)._refresh_tokens(+ linear_credentials(["issues:create", "read"])+ )++ assert refreshed.scopes == ["issues:create", "read"]+ assert refreshed.id == "cred-1"+++async def test_linear_refresh_still_splits_a_comma_separated_scope():+ with mock_requests(+ "backend.blocks.linear._oauth",+ post=[{"access_token": "new", "scope": "issues:create,read"}],+ ):+ refreshed = await LinearOAuthHandler(*CLIENT)._refresh_tokens(+ linear_credentials(["issues:create", "read"])+ )++ assert refreshed.scopes == ["issues:create", "read"]+++async def test_linear_refresh_falls_back_when_the_response_omits_the_scope():+ with mock_requests("backend.blocks.linear._oauth", post=[{"access_token": "new"}]):+ refreshed = await LinearOAuthHandler(*CLIENT)._refresh_tokens(+ linear_credentials(["issues:create", "read"])+ )++ assert refreshed.scopes == ["issues:create", "read"]+++async def test_linear_refreshed_credentials_pass_the_stores_superset_check(mocker):+ """The production failure: the refresh succeeded and `update_creds` refused it."""+ stored = linear_credentials(["issues:create", "read"])+ store = IntegrationCredentialsStore()+ persist = patch_store(mocker, store, persisted=[stored])++ with mock_requests(+ "backend.blocks.linear._oauth",+ post=[{"access_token": "new", "scope": "issues:create read"}],+ ):+ refreshed = await LinearOAuthHandler(*CLIENT)._refresh_tokens(stored)++ await store.update_creds("user-a", refreshed)++ assert persist.await_args.args[1][0].scopes == ["issues:create", "read"]++def mock_requests(module: str, *, post: list[dict] | None = None, get: list[dict] | None = None):@@ -263,3 +327,14 @@ def patch_store(mocker, store: IntegrationCredentialsStore, persisted: list):return mocker.patch.object(store, "_set_user_integration_creds", new_callable=AsyncMock)+++def linear_credentials(scopes: list[str]) -> OAuth2Credentials:+ return OAuth2Credentials(+ id="cred-1",+ provider="linear",+ access_token=SecretStr("at"),+ refresh_token=SecretStr("rt"),+ scopes=scopes,+ username="alice",+ )