: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(demo): only create first-party demo OAuth app in local dev (#62209)
Matt Brooker7196afc08944
diff --git a/posthog/demo/products/hedgebox/matrix.py b/posthog/demo/products/hedgebox/matrix.pyindex 87641376b5dd..8717fec4c6a3 100644--- a/posthog/demo/products/hedgebox/matrix.py+++ b/posthog/demo/products/hedgebox/matrix.py@@ -45,6 +45,7 @@ from posthog.schema import (TrendsQuery,)+from posthog.cloud_utils import is_cloudfrom posthog.constants import PAGEVIEW_EVENTfrom posthog.demo.matrix.matrix import Cluster, Matrixfrom posthog.demo.matrix.models import SimEvent@@ -1777,30 +1778,38 @@ class HedgeboxMatrix(Matrix):self._set_up_error_tracking_demo_data(team)- if settings.OIDC_RSA_PRIVATE_KEY:- try:- OAuthApplication.objects.create(- name="Demo OAuth Application",- client_id="DC5uRLVbGI02YQ82grxgnK6Qn12SXWpCqdPb60oZ",- client_secret="GQItUP4GqE6t5kjcWIRfWO9c0GXPCY8QDV4eszH4PnxXwCVxIMVSil4Agit7yay249jasnzHEkkVqHnFMxI1YTXSrh8Bj1sl1IDfNi1S95sv208NOc0eoUBP3TdA7vf0",- redirect_uris="http://localhost:3000/callback https://example.com/callback http://localhost:8237/callback http://localhost:8239/callback",- user=user,- organization=team.organization,- client_type=OAuthApplication.CLIENT_PUBLIC,- authorization_grant_type=OAuthApplication.GRANT_AUTHORIZATION_CODE,- algorithm="RS256",- is_first_party=True,- # An empty ceiling resolves to UNPRIVILEGED_SCOPES at /authorize, which- # excludes the privileged/hidden scopes the onboarding wizard requests- # (llm_gateway:read, wizard_session:*) — so it failed with invalid_scope.- # Reproduce the broad default and add those so the wizard works locally.- scopes=sorted(- UNPRIVILEGED_SCOPES- | {"llm_gateway:read", "llm_gateway:write", "wizard_session:read", "wizard_session:write"}- ),- )- except (IntegrityError, ValidationError):- pass+ self._set_up_demo_oauth_application(team, user)++ def _set_up_demo_oauth_application(self, team: "Team", user: "User") -> None:+ # This app is first-party (skips OAuth consent, issues tokens scoped to all of the user's orgs) and+ # ships with committed public credentials, so it must only ever exist in local dev — never in cloud+ # production. Demo-data generation is reachable by any authenticated user, so the cloud guard matters.+ if not (settings.OIDC_RSA_PRIVATE_KEY and settings.DEBUG and not is_cloud()):+ return++ try:+ OAuthApplication.objects.create(+ name="Demo OAuth Application",+ client_id="DC5uRLVbGI02YQ82grxgnK6Qn12SXWpCqdPb60oZ",+ client_secret="GQItUP4GqE6t5kjcWIRfWO9c0GXPCY8QDV4eszH4PnxXwCVxIMVSil4Agit7yay249jasnzHEkkVqHnFMxI1YTXSrh8Bj1sl1IDfNi1S95sv208NOc0eoUBP3TdA7vf0",+ redirect_uris="http://localhost:3000/callback http://localhost:8237/callback http://localhost:8239/callback",+ user=user,+ organization=team.organization,+ client_type=OAuthApplication.CLIENT_PUBLIC,+ authorization_grant_type=OAuthApplication.GRANT_AUTHORIZATION_CODE,+ algorithm="RS256",+ is_first_party=True,+ # An empty ceiling resolves to UNPRIVILEGED_SCOPES at /authorize, which+ # excludes the privileged/hidden scopes the onboarding wizard requests+ # (llm_gateway:read, wizard_session:*) — so it failed with invalid_scope.+ # Reproduce the broad default and add those so the wizard works locally.+ scopes=sorted(+ UNPRIVILEGED_SCOPES+ | {"llm_gateway:read", "llm_gateway:write", "wizard_session:read", "wizard_session:write"}+ ),+ )+ except (IntegrityError, ValidationError):+ passdef _set_up_error_tracking_demo_data(self, team: "Team") -> None:issue_specs: list[ErrorTrackingDemoIssueSpec] = [diff --git a/posthog/demo/test/test_hedgebox_matrix.py b/posthog/demo/test/test_hedgebox_matrix.pyindex 9f66adb1c6bd..0b66f0ea716e 100644--- a/posthog/demo/test/test_hedgebox_matrix.py+++ b/posthog/demo/test/test_hedgebox_matrix.py@@ -5,7 +5,9 @@ from typing import Any, castfrom unittest.mock import MagicMock, patch-from django.test import SimpleTestCase+from django.test import SimpleTestCase, override_settings++from parameterized import parameterizedfrom posthog.demo.matrix.models import SimEventfrom posthog.demo.products.hedgebox.matrix import HedgeboxMatrix@@ -243,3 +245,40 @@ class TestHedgeboxMatrixDemoWarehouseTables(SimpleTestCase):person_properties={},person_created_at=timestamp,)+++class TestHedgeboxMatrixDemoOAuthApplication(SimpleTestCase):+ @parameterized.expand(+ [+ ("local_dev", "dummy-key", True, False, False),+ ("cloud_prod", "dummy-key", False, True, True),+ ("self_hosted_prod", "dummy-key", False, False, True),+ ("debug_but_cloud", "dummy-key", True, True, True),+ ("no_oidc_key", "", True, False, True),+ ]+ )+ @patch("posthog.demo.products.hedgebox.matrix.OAuthApplication.objects.create")+ def test_demo_oauth_app_only_created_in_local_dev(+ self,+ _name: str,+ oidc_key: str,+ debug: bool,+ cloud: bool,+ should_skip: bool,+ mock_create: MagicMock,+ ) -> None:+ matrix = HedgeboxMatrix(seed="oauth-test", n_clusters=0)+ team = cast(Any, SimpleNamespace(organization=SimpleNamespace()))+ user = cast(Any, SimpleNamespace())++ with override_settings(OIDC_RSA_PRIVATE_KEY=oidc_key, DEBUG=debug):+ with patch("posthog.demo.products.hedgebox.matrix.is_cloud", return_value=cloud):+ matrix._set_up_demo_oauth_application(team, user)++ if should_skip:+ mock_create.assert_not_called()+ else:+ mock_create.assert_called_once()+ kwargs = mock_create.call_args.kwargs+ assert kwargs["is_first_party"] is True+ assert "example.com" not in kwargs["redirect_uris"]