: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(oauth): expose token impersonation in introspection (#103666)
Paul D'Ambraee6ab643b818
diff --git a/docs/published/handbook/engineering/oauth-development-guide.md b/docs/published/handbook/engineering/oauth-development-guide.mdindex d20daee90e84..206075373436 100644--- a/docs/published/handbook/engineering/oauth-development-guide.md+++ b/docs/published/handbook/engineering/oauth-development-guide.md@@ -406,6 +406,7 @@ You only see tokens that belong to your own application, whichever method you us```json{"active": true,+ "is_impersonated": false,"scope": "openid experiment:read query:read","client_id": "DC5uRLVbGI02YQ82grxgnK6Qn12SXWpCqdPb60oZ","scoped_teams": [1, 2],@@ -414,6 +415,9 @@ You only see tokens that belong to your own application, whichever method you us}```+`is_impersonated` is `true` when a staff member issued the access token during impersonation.+This field describes the token, not the browser session.+**Inactive/Invalid Token Response**:```jsondiff --git a/posthog/api/oauth/test_views.py b/posthog/api/oauth/test_views.pyindex a91a27343bee..e02d137c5ff6 100644--- a/posthog/api/oauth/test_views.py+++ b/posthog/api/oauth/test_views.py@@ -4209,9 +4209,11 @@ class TestOAuthAPI(APIBaseTest):data = response.json()self.assertFalse(data["active"])- def test_self_introspection_succeeds_without_introspection_scope(self):- """A token can introspect itself without requiring the introspection scope."""+ @parameterized.expand([True, False])+ def test_self_introspection_succeeds_without_introspection_scope(self, impersonated: bool) -> None:access_token, _ = self._create_access_and_refresh_tokens(scopes="openid user:read")+ access_token.impersonated_by = self.user if impersonated else None+ access_token.save(update_fields=["impersonated_by"])response = self.post("/oauth/introspect/",@@ -4223,6 +4225,7 @@ class TestOAuthAPI(APIBaseTest):data = response.json()self.assertTrue(data["active"])self.assertEqual(data["scope"], "openid user:read")+ self.assertEqual(data["is_impersonated"], impersonated)def test_self_introspection_via_get_succeeds_without_introspection_scope(self):"""Self-introspection also works via GET method."""diff --git a/posthog/api/oauth/views.py b/posthog/api/oauth/views.pyindex e8e857270cd8..9776c3bd1a6b 100644--- a/posthog/api/oauth/views.py+++ b/posthog/api/oauth/views.py@@ -2377,6 +2377,7 @@ class OAuthIntrospectTokenView(ClientProtectedScopedResourceView):"active": True,"token_type": "access_token","scope": access_token.scope,+ "is_impersonated": access_token.impersonated_by_id is not None,"scoped_teams": access_token.scoped_teams or [],"scoped_organizations": access_token.scoped_organizations or [],"exp": int(calendar.timegm(access_token.expires.timetuple())),diff --git a/posthog/api/test/test_user.py b/posthog/api/test/test_user.pyindex 2e1d2ee8044d..fdf9e66e8d84 100644--- a/posthog/api/test/test_user.py+++ b/posthog/api/test/test_user.py@@ -392,6 +392,7 @@ class TestUserAPI(APIBaseTest):response = self.client.get("/api/users/@me/", headers={"authorization": f"Bearer {token.token}"})assert response.status_code == 200assert response.json()["requires_credential_review"] is False+ assert response.json()["is_impersonated"] is Falsedef test_requires_credential_review_unverified_passkey(self):# Unverified passkeys are the realistic pre-claim attack artifact - a partner