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): send the platform PKCE verifier on Google token exchange (prod replica of #14894) (#14897)

Reinier van der Leer1f1d17c8a4d9
diff --git a/autogpt_platform/backend/backend/integrations/oauth/google.py b/autogpt_platform/backend/backend/integrations/oauth/google.py
index a807a8b7d2..afe710f862 100644
--- a/autogpt_platform/backend/backend/integrations/oauth/google.py
+++ b/autogpt_platform/backend/backend/integrations/oauth/google.py
@@ -47,11 +47,20 @@ class GoogleOAuthHandler(BaseOAuthHandler):
logger.debug(f"Setting up OAuth flow with scopes: {all_scopes}")
flow = self._setup_oauth_flow(all_scopes)
flow.redirect_uri = self.redirect_uri
+ # The platform generated this challenge together with the verifier it
+ # stores on the OAuth state, so the challenge on the login URL is the
+ # one the verifier in exchange_code_for_tokens can answer for.
+ pkce_params = (
+ {"code_challenge": code_challenge, "code_challenge_method": "S256"}
+ if code_challenge
+ else {}
+ )
authorization_url, _ = flow.authorization_url(
access_type="offline",
include_granted_scopes="true",
state=state,
prompt="consent",
+ **pkce_params,
)
return authorization_url
@@ -68,7 +77,9 @@ class GoogleOAuthHandler(BaseOAuthHandler):
# Disable scope check in fetch_token
flow.oauth2session.scope = None
- token = flow.fetch_token(code=code)
+ # This is a different Flow than the one that built the login URL, so
+ # the verifier has to come from the platform's OAuth state.
+ token = flow.fetch_token(code=code, code_verifier=code_verifier)
logger.debug("Token fetched successfully")
# Get the actual scopes granted by Google
@@ -182,6 +193,13 @@ class GoogleOAuthHandler(BaseOAuthHandler):
)
def _setup_oauth_flow(self, scopes: list[str]) -> Flow:
+ # google-auth-oauthlib >= 1.3.0 generates its own PKCE verifier per
+ # Flow by default. A verifier minted here would only live on this
+ # Flow, which is discarded after the login URL is built, so the token
+ # exchange could never present it and Google would reject every
+ # exchange with "(invalid_grant) Missing code verifier". PKCE is
+ # handled with the platform's own pair instead (see get_login_url and
+ # exchange_code_for_tokens).
return Flow.from_client_config(
{
"web": {
@@ -192,4 +210,5 @@ class GoogleOAuthHandler(BaseOAuthHandler):
}
},
scopes=scopes,
+ autogenerate_code_verifier=False,
)
diff --git a/autogpt_platform/backend/backend/integrations/oauth/google_pkce_test.py b/autogpt_platform/backend/backend/integrations/oauth/google_pkce_test.py
new file mode 100644
index 0000000000..7f5e22717c
--- /dev/null
+++ b/autogpt_platform/backend/backend/integrations/oauth/google_pkce_test.py
@@ -0,0 +1,97 @@
+"""PKCE regression tests for GoogleOAuthHandler.
+
+The platform generates its own PKCE pair in ``store_state_token`` and hands
+the challenge to ``get_login_url`` and the verifier to
+``exchange_code_for_tokens``. The Google handler builds a fresh
+``google_auth_oauthlib.flow.Flow`` for each step, so a verifier the library
+generates for the login URL is thrown away before the exchange. Since
+google-auth-oauthlib 1.3.0 ``Flow.from_client_config`` auto-generates that
+verifier by default, which put a ``code_challenge`` on every Google login URL
+with nothing to back it at the token endpoint. Google then rejects every
+exchange with ``(invalid_grant) Missing code verifier.``
+
+These tests pin the handler to the platform's PKCE pair on both legs.
+"""
+
+import time
+from unittest.mock import MagicMock
+from urllib.parse import parse_qs, urlparse
+
+import pytest
+from pytest_mock import MockerFixture
+
+from backend.integrations.oauth.google import GoogleOAuthHandler
+
+CODE_CHALLENGE = "platform-generated-challenge"
+CODE_VERIFIER = "platform-generated-verifier"
+SCOPES = ["https://www.googleapis.com/auth/gmail.send"]
+
+
+def _handler() -> GoogleOAuthHandler:
+ return GoogleOAuthHandler(
+ client_id="test-client-id",
+ client_secret="test-client-secret",
+ redirect_uri="https://example.com/auth/integrations/oauth_callback",
+ )
+
+
+def _login_params(handler: GoogleOAuthHandler, code_challenge: str | None):
+ url = handler.get_login_url(SCOPES, "state-token", code_challenge=code_challenge)
+ return parse_qs(urlparse(url).query)
+
+
+def test_login_url_carries_the_platform_code_challenge():
+ params = _login_params(_handler(), CODE_CHALLENGE)
+
+ assert params["code_challenge"] == [CODE_CHALLENGE]
+ assert params["code_challenge_method"] == ["S256"]
+ assert params["state"] == ["state-token"]
+
+
+def test_login_url_without_platform_challenge_has_no_pkce():
+ """A caller that opts out of PKCE must not get a library-generated
+ challenge, because nothing would send the matching verifier later."""
+ params = _login_params(_handler(), None)
+
+ assert "code_challenge" not in params
+ assert "code_challenge_method" not in params
+
+
+async def test_exchange_sends_the_platform_code_verifier(mocker: MockerFixture):
+ handler = _handler()
+ captured: dict[str, object] = {}
+ token = {
+ "access_token": "access-token",
+ "refresh_token": "refresh-token",
+ "expires_in": 3600,
+ "expires_at": time.time() + 3600,
+ "scope": SCOPES,
+ "token_type": "Bearer",
+ }
+ real_setup = handler._setup_oauth_flow
+
+ def setup_with_stubbed_session(scopes):
+ flow = real_setup(scopes)
+
+ def fake_fetch_token(token_url, **kwargs):
+ captured["token_url"] = token_url
+ captured.update(kwargs)
+ flow.oauth2session.token = token
+ return token
+
+ flow.oauth2session.fetch_token = MagicMock(side_effect=fake_fetch_token)
+ return flow
+
+ mocker.patch.object(handler, "_setup_oauth_flow", setup_with_stubbed_session)
+ mocker.patch.object(handler, "_request_email", return_value="[email protected]")
+
+ credentials = await handler.exchange_code_for_tokens(
+ "auth-code", SCOPES, code_verifier=CODE_VERIFIER
+ )
+
+ assert captured["token_url"] == handler.token_uri
+ assert captured["code"] == "auth-code"
+ assert captured["code_verifier"] == CODE_VERIFIER
+ assert credentials.username == "[email protected]"
+ assert credentials.scopes == SCOPES