Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(oauth): return 400 invalid_grant on OAuthAccessToken.DoesNotExist (#59154)

posthog[bot]8cf5ee4e1b90
diff --git a/posthog/api/oauth/test_views.py b/posthog/api/oauth/test_views.py
index 983604b89640..4405f7673b09 100644
--- a/posthog/api/oauth/test_views.py
+++ b/posthog/api/oauth/test_views.py
@@ -7,6 +7,7 @@ from urllib.parse import parse_qs, quote, urlencode, urlparse, urlunparse
import pytest
from freezegun import freeze_time
from posthog.test.base import APIBaseTest
+from unittest.mock import patch
from django.conf import settings
from django.test import override_settings
@@ -1048,6 +1049,20 @@ class TestOAuthAPI(APIBaseTest):
self.assertEqual(response2.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response2.json()["error"], "invalid_grant")
+ def test_token_endpoint_returns_invalid_grant_when_access_token_lookup_misses(self):
+ token_data = {**self.base_token_body, "code": "does_not_matter"}
+
+ with patch(
+ "oauth2_provider.views.base.TokenView.post",
+ side_effect=OAuthAccessToken.DoesNotExist,
+ ):
+ response = self.post("/oauth/token/", token_data)
+
+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
+ body = response.json()
+ self.assertEqual(body["error"], "invalid_grant")
+ self.assertIn("error_description", body)
+
def test_pkce_code_verifier_validation(self):
response = self.client.post("/oauth/authorize/", self.base_authorization_post_body)
self.assertEqual(response.status_code, status.HTTP_200_OK)
diff --git a/posthog/api/oauth/views.py b/posthog/api/oauth/views.py
index f5b966b55055..1597116fbea9 100644
--- a/posthog/api/oauth/views.py
+++ b/posthog/api/oauth/views.py
@@ -680,18 +680,39 @@ class OAuthTokenView(TokenView):
grant_type = request.POST.get("grant_type", "unknown")
client_id = request.POST.get("client_id", "")
client_id_prefix = client_id[:8] if client_id else "unknown"
+ redirect_uri = request.POST.get("redirect_uri", "")
logger.info(
"oauth_token_request",
grant_type=grant_type,
client_id_prefix=client_id_prefix,
+ redirect_uri=redirect_uri,
)
- response = super().post(request, *args, **kwargs)
+ try:
+ response = super().post(request, *args, **kwargs)
+ except OAuthAccessToken.DoesNotExist:
+ # django-oauth-toolkit's token response path re-reads the access token it
+ # just issued; concurrent requests racing on the same authorization code
+ # can surface this as DoesNotExist. Map to the standard 400 invalid_grant.
+ logger.warning(
+ "oauth_token_access_token_missing",
+ grant_type=grant_type,
+ client_id_prefix=client_id_prefix,
+ redirect_uri=redirect_uri,
+ )
+ return JsonResponse(
+ {
+ "error": "invalid_grant",
+ "error_description": "Authorization code is invalid or has already been used",
+ },
+ status=400,
+ )
logger.info(
"oauth_token_response",
grant_type=grant_type,
client_id_prefix=client_id_prefix,
+ redirect_uri=redirect_uri,
status=response.status_code,
)