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): keep dcr and cimd refresh tokens alive while in use (#105394)

Rafael Audibert8ee7825e64e7
diff --git a/posthog/api/oauth/test_views.py b/posthog/api/oauth/test_views.py
index 6d687eea401b..ec279a32e06d 100644
--- a/posthog/api/oauth/test_views.py
+++ b/posthog/api/oauth/test_views.py
@@ -4503,6 +4503,8 @@ class TestOAuthAPI(APIBaseTest):
# Verify the refresh token was never revoked
db_refresh_token = OAuthRefreshToken.objects.get(token=original_refresh_token)
self.assertIsNone(db_refresh_token.revoked)
+ assert db_refresh_token.access_token is not None
+ self.assertEqual(db_refresh_token.access_token.token, original_access_token)
@time_machine.travel("2025-01-01 00:00:00", tick=False)
def test_dcr_refresh_does_not_invalidate_previously_issued_access_tokens(self):
@@ -4719,6 +4721,7 @@ class TestOAuthAPI(APIBaseTest):
},
)
refresh_token = token_response.json()["refresh_token"]
+ unlinked_access_token = token_response.json()["access_token"]
refresh_response = self.post(
"/oauth/token/",
@@ -4728,8 +4731,7 @@ class TestOAuthAPI(APIBaseTest):
"client_id": self.public_application.client_id,
},
)
- refresh_issued_access_token = refresh_response.json()["access_token"]
- self.assertTrue(OAuthAccessToken.objects.filter(token=refresh_issued_access_token).exists())
+ self.assertEqual(refresh_response.status_code, status.HTTP_200_OK)
revoke_response = self.post(
"/oauth/revoke/",
@@ -4741,7 +4743,7 @@ class TestOAuthAPI(APIBaseTest):
self.assertEqual(revoke_response.status_code, status.HTTP_200_OK)
self.assertTrue(
- OAuthAccessToken.objects.filter(token=refresh_issued_access_token).exists(),
+ OAuthAccessToken.objects.filter(token=unlinked_access_token).exists(),
"a dynamic client presenting another app's refresh token must not sweep "
"that app's (user, application) access-token family",
)
diff --git a/posthog/api/oauth/views.py b/posthog/api/oauth/views.py
index 588419340eb8..65d38bcfc28f 100644
--- a/posthog/api/oauth/views.py
+++ b/posthog/api/oauth/views.py
@@ -940,6 +940,13 @@ class OAuthValidator(OAuth2Validator):
original ``authorization_code``-issued AT keeps the back-reference;
refresh-issued rows pass ``source_refresh_token=None`` and stay
addressable by token / token_checksum.
+
+ The RT's own ``access_token`` link moves to the new row. The daily
+ cleanup job deletes an RT by the expiry of its linked AT, and DOT reads
+ the next refresh's scopes from that AT, so a link left on the
+ authorization-code AT would delete a refresh token that is still in use.
+ The previous AT stays valid until it expires, and the cleanup job then
+ deletes it as a standalone token.
"""
refresh_token_code = token.get("refresh_token")
refresh_token_instance = getattr(request, "refresh_token_instance", None)
@@ -961,13 +968,14 @@ class OAuthValidator(OAuth2Validator):
seconds=token.get("expires_in", oauth2_settings.ACCESS_TOKEN_EXPIRE_SECONDS),
)
- self._create_access_token(
+ access_token = self._create_access_token(
expires,
request,
token,
source_refresh_token=None,
scope_source_refresh_token=refresh_token_instance,
)
+ OAuthRefreshToken.objects.filter(pk=refresh_token_instance.pk).update(access_token=access_token)
logger.info(
"oauth_non_rotating_refresh_inserted",
client_id_prefix=str(getattr(request.client, "client_id", "")[:8]),