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.

33.0 KB
Raw Blame
import json

from unittest.mock import MagicMock, patch

from django.core.cache import cache as real_cache

from parameterized import parameterized
from rest_framework.parsers import JSONParser
from rest_framework.request import Request
from rest_framework.test import APIRequestFactory

from posthog.api.oauth.cimd import _cache_key, fetch_and_upsert_cimd_application
from posthog.api.oauth.client_assertion import PRIVATE_KEY_REQUIRED_MESSAGE
from posthog.models.activity_logging.utils import ActivityCredential, activity_storage
from posthog.models.oauth import OAuthApplication
from posthog.models.personal_api_key import PersonalAPIKey
from posthog.models.user import User
from posthog.test.anti_ssrf import mock_anti_ssrf_dns_lookup

from ee.api.agentic_provisioning.authentication import ProvisioningAuthentication
from ee.api.agentic_provisioning.test.base import (
    TEST_PARTNER_CLIENT_SECRET,
    TEST_PARTNER_JWKS_URI,
    ProvisioningTestBase,
    provisioning_config,
)

WIZARD_CLIENT_ID = "test-wizard-client"


class TestProvisioningAuthentication(ProvisioningTestBase):
    def setUp(self):
        super().setUp()

        OAuthApplication.objects.filter(client_id=WIZARD_CLIENT_ID).delete()
        self.wizard_app = OAuthApplication.objects.create(
            client_id=WIZARD_CLIENT_ID,
            name="PostHog Wizard",
            client_secret="",
            client_type=OAuthApplication.CLIENT_CONFIDENTIAL,
            jwks_uri=TEST_PARTNER_JWKS_URI,
            authorization_grant_type=OAuthApplication.GRANT_AUTHORIZATION_CODE,
            redirect_uris="http://localhost:8239/callback",
            algorithm="RS256",
            is_first_party=True,
            is_provisioning_partner=True,
            _provisioning_config=provisioning_config(
                active=True, can_create_accounts=True, can_provision_resources=True
            ),
        )
        # A client secret is enough only for a partner that neither creates accounts nor pays.
        self.secret_partner = OAuthApplication.objects.create(
            client_id="test-client-secret-partner",
            name="Client Secret Partner",
            client_secret=TEST_PARTNER_CLIENT_SECRET,
            client_type=OAuthApplication.CLIENT_CONFIDENTIAL,
            authorization_grant_type=OAuthApplication.GRANT_AUTHORIZATION_CODE,
            redirect_uris="https://partner.example.com/callback",
            algorithm="RS256",
            is_provisioning_partner=True,
            _provisioning_config=provisioning_config(active=True, can_create_accounts=False),
        )

    def _wizard_account_request(self, request_id: str, email: str, challenge: str):
        return self._post_api(
            "/api/agentic/provisioning/account_requests",
            {
                "id": request_id,
                "email": email,
                "code_challenge": challenge,
                "code_challenge_method": "S256",
                **self._client_auth(self.wizard_app),
            },
        )

    def _exchange_code(self, code: str, verifier: str, partner=None):
        return self.client.post(
            "/api/agentic/oauth/token",
            data={
                "grant_type": "authorization_code",
                "code": code,
                "code_verifier": verifier,
                **self._client_auth(partner or self.wizard_app),
            },
        )

    # --- client_secret identification ---

    @parameterized.expand(["body", "basic_header"])
    def test_client_secret_partner_identified(self, transport):
        payload = {"id": f"req_secret_{transport}", "email": f"{transport}[email protected]"}
        if transport == "body":
            res = self._post_as_partner("/api/agentic/provisioning/account_requests", payload, self.secret_partner)
        else:
            res = self._post_api(
                "/api/agentic/provisioning/account_requests",
                {**payload, "client_id": self.secret_partner.client_id},
                HTTP_AUTHORIZATION=self._basic_auth_header(self.secret_partner),
            )

        # Past authentication to the capability check, which a partner that cannot create accounts fails.
        assert res.status_code == 403, res.json()
        assert res.json()["error"]["code"] == "forbidden"

    @parameterized.expand([("proven by its secret", True), ("public client id only", False)])
    def test_partner_is_recorded_by_id_only_when_it_proved_itself(self, _name, confidential):
        if confidential:
            partner = self.secret_partner
            data = self._client_auth(partner)
        else:
            partner = OAuthApplication.objects.create(
                client_id="activity-log-public-partner",
                name="Public partner",
                client_type=OAuthApplication.CLIENT_PUBLIC,
                authorization_grant_type=OAuthApplication.GRANT_AUTHORIZATION_CODE,
                redirect_uris="https://partner.example.com/callback",
                algorithm="RS256",
                is_provisioning_partner=True,
                _provisioning_config=provisioning_config(can_create_accounts=False),
            )
            data = {"client_id": partner.client_id}
        request = Request(APIRequestFactory().post("/", data, format="json"), parsers=[JSONParser()])

        activity_storage.mark_request_scoped()
        try:
            assert ProvisioningAuthentication().authenticate(request) == (None, partner)
            credential = activity_storage.get_credential()
        finally:
            activity_storage.clear_all()

        assert credential == ActivityCredential(type="partner", id=str(partner.id) if confidential else None)

    @parameterized.expand(
        [
            ("wrong_secret", "not-the-real-secret"),
            ("blank_secret", ""),
        ]
    )
    def test_client_secret_partner_rejected_without_the_right_secret(self, _name, secret):
        res = self._post_api(
            "/api/agentic/provisioning/account_requests",
            {
                "id": "req_bad_secret",
                "email": "[email protected]",
                "client_id": self.secret_partner.client_id,
                "client_secret": secret,
            },
        )
        assert res.status_code == 401
        assert res.json()["error"]["code"] == "unauthorized"

    def test_client_secret_partner_cannot_downgrade_to_public_client_id(self):
        # A confidential partner's client_id is public. Accepting it on its own would let
        # anyone act as the partner just by leaving the secret out.
        res = self._post_api(
            "/api/agentic/provisioning/account_requests",
            {
                "id": "req_no_secret",
                "email": "[email protected]",
                "client_id": self.secret_partner.client_id,
                "code_challenge": "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM",
                "code_challenge_method": "S256",
            },
        )
        assert res.status_code == 401
        assert res.json()["error"]["code"] == "unauthorized"

    def test_access_token_no_longer_identifies_a_partner(self):
        # Partners used to prove themselves by presenting an access token they had been
        # issued. Only client credentials do that now; a token identifies a user, not a client.
        res = self._post_with_bearer(
            "/api/agentic/provisioning/account_requests",
            {"id": "req_token_only", "email": "[email protected]"},
            token=self._get_bearer_token(),
        )
        assert res.status_code == 401
        assert res.json()["error"]["code"] == "unauthorized"

    def test_inactive_client_secret_partner_rejected(self):
        self.secret_partner.update_provisioning(active=False)

        res = self._post_as_partner(
            "/api/agentic/provisioning/account_requests",
            {"id": "req_inactive_secret", "email": "[email protected]"},
            self.secret_partner,
        )
        assert res.status_code == 401
        assert res.json()["error"]["code"] == "unauthorized"

    # --- apps that aren't flagged as partners are fail-closed ---

    @parameterized.expand(
        [
            ("confidential", OAuthApplication.CLIENT_CONFIDENTIAL),
            ("public", OAuthApplication.CLIENT_PUBLIC),
        ]
    )
    def test_app_not_flagged_as_partner_cannot_authenticate(self, name, client_type):
        # The legacy HMAC partners land here after the backfill: an app can carry every other
        # provisioning flag and still be refused, of either client type.
        OAuthApplication.objects.create(
            client_id=f"legacy-partner-{name}",
            name=f"Legacy Partner {name}",
            client_secret="",
            client_type=client_type,
            authorization_grant_type=OAuthApplication.GRANT_AUTHORIZATION_CODE,
            redirect_uris="https://legacy.example.com/callback",
            algorithm="RS256",
            is_provisioning_partner=False,
            _provisioning_config=provisioning_config(
                active=True, can_create_accounts=True, can_provision_resources=True
            ),
        )

        res = self._post_api(
            "/api/agentic/provisioning/account_requests",
            {"id": "req_legacy", "email": f"legacy-{name}@example.com", "client_id": f"legacy-partner-{name}"},
        )
        assert res.status_code == 401
        assert res.json()["error"]["code"] == "unauthorized"

    # --- PKCE flow ---

    def test_pkce_wizard_new_user_full_flow(self):
        verifier, challenge = self._pkce_pair()

        res = self._wizard_account_request("req_wizard_1", "[email protected]", challenge)
        assert res.status_code == 200
        data = res.json()
        assert data["type"] == "oauth"
        code = data["oauth"]["code"]

        res = self._exchange_code(code, verifier)
        assert res.status_code == 200
        tokens = res.json()
        assert "access_token" in tokens
        assert "refresh_token" in tokens
        assert tokens["expires_in"] == 3600

        res = self._post_with_bearer("/api/agentic/provisioning/resources", {}, token=tokens["access_token"])
        assert res.status_code == 200
        assert res.json()["status"] == "complete"
        assert "api_key" in res.json()["complete"]["access_configuration"]

    @parameterized.expand(
        [
            ("wrong_verifier", "wrong_verifier_value", 400),
            ("missing_verifier", "", 401),
        ]
    )
    def test_pkce_code_exchange_requires_matching_verifier(self, _name, verifier, expected_status):
        _, challenge = self._pkce_pair()

        res = self._wizard_account_request("req_bad_pkce", "[email protected]", challenge)
        code = res.json()["oauth"]["code"]

        res = self._exchange_code(code, verifier)
        assert res.status_code == expected_status
        if expected_status == 400:
            assert res.json()["error"] == "invalid_grant"

    # --- can_create_accounts enforcement ---

    def test_partner_without_can_create_accounts_rejected(self):
        OAuthApplication.objects.create(
            name="Disabled Partner",
            client_id="disabled-partner",
            client_secret=TEST_PARTNER_CLIENT_SECRET,
            client_type=OAuthApplication.CLIENT_CONFIDENTIAL,
            authorization_grant_type=OAuthApplication.GRANT_AUTHORIZATION_CODE,
            redirect_uris="https://localhost",
            algorithm="RS256",
            is_provisioning_partner=True,
            _provisioning_config=provisioning_config(active=True, can_create_accounts=False),
        )

        res = self._post_api(
            "/api/agentic/provisioning/account_requests",
            {
                "id": "req_disabled",
                "email": "[email protected]",
                "client_id": "disabled-partner",
                "client_secret": TEST_PARTNER_CLIENT_SECRET,
            },
        )
        assert res.status_code == 403
        assert res.json()["error"]["code"] == "forbidden"

    # --- Org naming ---

    def test_org_named_after_the_partner_app(self):
        _, challenge = self._pkce_pair()
        email = "[email protected]"

        self._wizard_account_request("req_org_name", email, challenge)

        user = User.objects.get(email=email)
        membership = user.organization_memberships.first()
        assert membership is not None
        org = membership.organization
        assert org.name == f"PostHog Wizard ({email})"

    # --- PAT scopes ---

    def test_default_off_app_mints_no_provisioned_pat(self):
        verifier, challenge = self._pkce_pair()
        email = "[email protected]"

        res = self._wizard_account_request("req_pat", email, challenge)
        code = res.json()["oauth"]["code"]
        token = self._exchange_code(code, verifier).json()["access_token"]

        self._post_with_bearer("/api/agentic/provisioning/resources", {}, token=token)

        user = User.objects.get(email=email)
        # The wizard app does not set issues_personal_api_key, so no PAT is minted.
        pat = PersonalAPIKey.objects.filter(user=user).first()
        assert pat is None

    # --- is_active kill switch ---

    def test_inactive_pkce_partner_rejected(self):
        self.wizard_app.update_provisioning(active=False)

        _, challenge = self._pkce_pair()
        res = self._wizard_account_request("req_inactive_pkce", "[email protected]", challenge)
        assert res.status_code == 401

    def test_deactivated_user_bearer_token_rejected(self):
        token = self._get_bearer_token()

        self.user.is_active = False
        self.user.save()

        res = self._post_with_bearer("/api/agentic/provisioning/resources", {}, token=token)
        assert res.status_code == 401

    # --- partner config changes apply to tokens minted before them ---

    @parameterized.expand(
        [
            ("cannot_provision_resources", {"can_provision_resources": False}, False, 403),
            ("dropped_its_private_key", {}, True, 401),
            # The Wizard's production shape: a client secret, and no account creation.
            ("dropped_its_private_key_but_cannot_create_accounts", {"can_create_accounts": False}, True, 200),
        ]
    )
    def test_bearer_after_partner_changes(
        self, _name: str, change: dict[str, bool], drop_key: bool, expected_status: int
    ) -> None:
        token = self._get_bearer_token()

        if change:
            self.partner.update_provisioning(**change)
        if drop_key:
            self.partner.jwks_uri = None
            self.partner.save(update_fields=["jwks_uri"])

        res = self._post_with_bearer("/api/agentic/provisioning/resources", {}, token=token)
        assert res.status_code == expected_status

    # --- CIMD URL-based PKCE identification ---

    @patch("posthog.api.oauth.cimd.refresh_cimd_metadata_task")
    def test_pkce_partner_identified_by_cimd_url(self, mock_refresh):
        cimd_url = "https://example.com/api/oauth/wizard/client-metadata"
        OAuthApplication.objects.create(
            name="CIMD Wizard",
            client_secret="",
            client_type=OAuthApplication.CLIENT_PUBLIC,
            authorization_grant_type=OAuthApplication.GRANT_AUTHORIZATION_CODE,
            redirect_uris="http://localhost:8239/callback",
            algorithm="RS256",
            client_id=cimd_url,
            is_cimd_client=True,
            is_provisioning_partner=True,
            _provisioning_config=provisioning_config(
                active=True, can_create_accounts=False, can_provision_resources=True
            ),
        )

        _, challenge = self._pkce_pair()
        res = self._post_api(
            "/api/agentic/provisioning/account_requests",
            {
                "id": "req_cimd_pkce",
                "email": "[email protected]",
                "client_id": cimd_url,
                "code_challenge": challenge,
                "code_challenge_method": "S256",
            },
        )
        # Past authentication to the capability check: an unknown client_id would be a 401.
        assert res.status_code == 403, res.json()
        assert res.json()["error"]["code"] == "forbidden"

    @patch("posthog.api.oauth.cimd.refresh_cimd_metadata_task")
    def test_cimd_url_inactive_partner_rejected(self, mock_refresh):
        cimd_url = "https://example.com/api/oauth/wizard/client-metadata-inactive"
        OAuthApplication.objects.create(
            name="Inactive CIMD Wizard",
            client_secret="",
            client_type=OAuthApplication.CLIENT_PUBLIC,
            authorization_grant_type=OAuthApplication.GRANT_AUTHORIZATION_CODE,
            redirect_uris="http://localhost:8239/callback",
            algorithm="RS256",
            client_id=cimd_url,
            is_cimd_client=True,
            is_provisioning_partner=True,
            _provisioning_config=provisioning_config(active=False, can_create_accounts=True),
        )

        _, challenge = self._pkce_pair()
        res = self._post_api(
            "/api/agentic/provisioning/account_requests",
            {
                "id": "req_cimd_inactive",
                "email": "[email protected]",
                "client_id": cimd_url,
                "code_challenge": challenge,
                "code_challenge_method": "S256",
            },
        )
        assert res.status_code == 401

    @parameterized.expand(
        [
            ("stale_cache_fires_refresh", False, 1),
            ("fresh_cache_skips_refresh", True, 0),
        ]
    )
    @patch("posthog.api.oauth.cimd.refresh_cimd_metadata_task")
    def test_cimd_provisioning_partner_refreshes_metadata_only_when_stale(
        self, _name, cache_is_fresh, expected_delay_calls, mock_refresh
    ):
        cimd_url = "https://example.com/api/oauth/wizard/refresh-on-auth"
        cimd_app = OAuthApplication.objects.create(
            name="CIMD Wizard Refresh",
            client_secret="",
            client_type=OAuthApplication.CLIENT_PUBLIC,
            authorization_grant_type=OAuthApplication.GRANT_AUTHORIZATION_CODE,
            redirect_uris="http://localhost:8239/callback",
            algorithm="RS256",
            client_id=cimd_url,
            is_cimd_client=True,
            is_provisioning_partner=True,
            _provisioning_config=provisioning_config(
                active=True, can_create_accounts=True, can_provision_resources=True
            ),
        )
        self.addCleanup(real_cache.delete, _cache_key(cimd_url))

        if cache_is_fresh:
            real_cache.set(_cache_key(cimd_url), True, timeout=300)
        else:
            real_cache.delete(_cache_key(cimd_url))

        partner = ProvisioningAuthentication()._identify_pkce_partner(cimd_url)

        assert partner == cimd_app
        assert mock_refresh.delay.call_count == expected_delay_calls
        if expected_delay_calls:
            mock_refresh.delay.assert_called_once_with(cimd_url)

    # --- PKCE code_challenge_method validation ---

    def test_plain_code_challenge_method_rejected(self):
        res = self._post_api(
            "/api/agentic/provisioning/account_requests",
            {
                "id": "req_plain",
                "email": "[email protected]",
                "code_challenge": "some_challenge",
                "code_challenge_method": "plain",
                **self._client_auth(self.wizard_app),
            },
        )
        assert res.status_code == 400
        assert "S256" in res.json()["error"]["message"]


class TestUnsignedProvisioning(ProvisioningTestBase):
    def _public_partner(self, *, capabilities: dict[str, bool] | None = None) -> OAuthApplication:
        return OAuthApplication.objects.create(
            client_id="unsigned-public-partner",
            name="Unsigned partner",
            client_type=OAuthApplication.CLIENT_PUBLIC,
            authorization_grant_type=OAuthApplication.GRANT_AUTHORIZATION_CODE,
            redirect_uris="https://partner.example.com/callback",
            algorithm="RS256",
            is_provisioning_partner=True,
            _provisioning_config=provisioning_config(**(capabilities or {})),
        )

    def _account_request(self, partner: OAuthApplication, email: str):
        _, challenge = self._pkce_pair()
        with patch("ee.api.agentic_provisioning.authentication.capture_auth_event") as capture_auth_event:
            res = self._post_api(
                "/api/agentic/provisioning/account_requests",
                {
                    "id": f"req_{email}",
                    "email": email,
                    "code_challenge": challenge,
                    "code_challenge_method": "S256",
                    "client_id": partner.client_id,
                },
            )
        signing = capture_auth_event.call_args.kwargs.get("provisioning_signing") if capture_auth_event.called else None
        return res, signing

    def _assert_refused(self, res, email: str) -> None:
        assert res.status_code == 401, res.json()
        assert res.json()["error"] == {"code": "unauthorized", "message": PRIVATE_KEY_REQUIRED_MESSAGE}
        assert not User.objects.filter(email=email).exists()

    @parameterized.expand(
        [
            ("creates_accounts", {}, 401, "refused"),
            ("pays_for_customers", {"can_create_accounts": False, "pays_for_customers": True}, 401, "refused"),
            # Not asked to sign, so it gets past authentication to the capability check.
            ("neither", {"can_create_accounts": False}, 403, "unsigned_exempt"),
        ]
    )
    def test_partner_that_must_sign_is_refused_unsigned(
        self, name: str, capabilities: dict[str, bool], expected_status: int, expected_signing: str
    ) -> None:
        email = f"new-partner-{name}@example.com"
        partner = self._public_partner(capabilities=capabilities)
        res, signing = self._account_request(partner, email)

        assert signing == expected_signing
        if expected_status == 401:
            self._assert_refused(res, email)
        else:
            assert res.status_code == expected_status, res.json()
            assert res.json()["error"]["code"] == "forbidden"


CIMD_PROV_URL = "https://partner.example.com/.well-known/oauth-client-metadata.json"


def _make_cimd_metadata(url: str = CIMD_PROV_URL, **overrides) -> dict:
    metadata = {
        "client_id": url,
        "client_name": "Partner App",
        "redirect_uris": ["http://127.0.0.1:3000/callback"],
        "grant_types": ["authorization_code"],
        "response_types": ["code"],
        "token_endpoint_auth_method": "private_key_jwt",
        "jwks_uri": TEST_PARTNER_JWKS_URI,
        "com.posthog": {"provisioning": True},
    }
    metadata.update(overrides)
    return metadata


def _cimd_mock_response(metadata: dict | None, status_code: int = 200):
    resp = MagicMock()
    resp.status_code = status_code
    resp.headers = {}
    resp.is_redirect = False
    resp.is_permanent_redirect = False
    resp.close = MagicMock()
    body = json.dumps(metadata).encode() if metadata is not None else b""
    resp.iter_content = MagicMock(return_value=iter([body]))
    return resp


@mock_anti_ssrf_dns_lookup()
class TestCimdProvisioningRegistration(ProvisioningTestBase):
    def setUp(self):
        super().setUp()
        OAuthApplication.objects.filter(client_id=CIMD_PROV_URL).delete()
        real_cache.clear()

    def _register(self) -> OAuthApplication:
        """Register the partner the way client_registration does, so the promotion depends on
        the mocked document declaring the opt-in."""
        app = fetch_and_upsert_cimd_application(CIMD_PROV_URL, register_provisioning=True)
        assert app is not None
        return app

    @patch("posthog.api.oauth.cimd.requests.Session.get")
    def test_registered_cimd_partner_can_create_accounts(self, mock_get, _url_mock):
        mock_get.return_value = _cimd_mock_response(_make_cimd_metadata())

        app = self._register()
        assert app.is_cimd_client
        assert app.is_provisioning_partner
        assert app.provisioning.active
        assert app.provisioning.can_create_accounts
        assert app.provisioning.can_provision_resources

        _, challenge = self._pkce_pair()
        res = self._post_as_partner(
            "/api/agentic/provisioning/account_requests",
            {
                "id": "req_cimd_auto",
                "email": "[email protected]",
                "code_challenge": challenge,
                "code_challenge_method": "S256",
            },
            partner=app,
        )
        assert res.status_code == 200, res.json()
        assert res.json()["type"] == "oauth"

    @patch("posthog.api.oauth.cimd.requests.Session.get")
    def test_cimd_scope_ceiling_refreshes_on_agentic_auth_after_metadata_edit(self, mock_get, _url_mock):
        initial = _make_cimd_metadata()
        initial["com.posthog"] = {"provisioning": True, "scopes": ["insight:read"]}
        mock_get.return_value = _cimd_mock_response(initial)
        self._register()

        app = OAuthApplication.objects.get(client_id=CIMD_PROV_URL)
        assert app.scopes == ["insight:read"]

        # Partner edits the live metadata to widen the ceiling; the cached doc goes stale.
        real_cache.delete(_cache_key(CIMD_PROV_URL))
        widened = _make_cimd_metadata()
        widened["com.posthog"] = {"provisioning": True, "scopes": ["insight:read", "dashboard:read"]}
        mock_get.return_value = _cimd_mock_response(widened)

        # A later agentic provisioning auth request must propagate the edit — the bug was that
        # this raw-lookup path never refreshed, so the ceiling stayed frozen at registration.
        _, challenge = self._pkce_pair()
        res = self._post_as_partner(
            "/api/agentic/provisioning/account_requests",
            {
                "id": "req_cimd_refresh_e2e",
                "email": "[email protected]",
                "code_challenge": challenge,
                "code_challenge_method": "S256",
            },
            partner=app,
        )
        assert res.status_code == 200, res.json()

        app.refresh_from_db()
        assert app.scopes == ["insight:read", "dashboard:read"]

    @patch("posthog.api.oauth.cimd.refresh_cimd_metadata_task")
    def test_partner_rate_limit_enforced_after_threshold(self, mock_refresh, _url_mock):
        partner = OAuthApplication.objects.create(
            name="Rate Limit Test CIMD",
            client_secret="",
            client_type=OAuthApplication.CLIENT_CONFIDENTIAL,
            jwks_uri=TEST_PARTNER_JWKS_URI,
            authorization_grant_type=OAuthApplication.GRANT_AUTHORIZATION_CODE,
            redirect_uris="http://127.0.0.1:3000/callback",
            algorithm="RS256",
            client_id=CIMD_PROV_URL,
            is_cimd_client=True,
            is_provisioning_partner=True,
            _provisioning_config=provisioning_config(
                active=True,
                can_create_accounts=True,
                can_provision_resources=True,
                rate_limits={"account_requests": 10},
            ),
        )

        _, challenge = self._pkce_pair()

        def post_account_request(email: str):
            return self._post_as_partner(
                "/api/agentic/provisioning/account_requests",
                {
                    "id": f"req_{email}",
                    "email": email,
                    "code_challenge": challenge,
                    "code_challenge_method": "S256",
                },
                partner=partner,
            )

        assert post_account_request("[email protected]").status_code == 200

        partner.update_provisioning_rate_limits(account_requests=2)

        assert post_account_request("[email protected]").status_code == 200
        res = post_account_request("[email protected]")
        assert res.status_code == 429
        assert res.json()["error"]["code"] == "rate_limited"

    @patch("posthog.api.oauth.cimd.CIMD_THROTTLE_CLASSES", new=[])
    def test_cimd_domain_rate_limit_blocks_excessive_registrations(self, _url_mock):
        from ee.api.agentic_provisioning.constants import CIMD_DOMAIN_RATE_LIMIT_MAX

        base_domain = "evil.example.com"
        _, challenge = self._pkce_pair()

        for i in range(CIMD_DOMAIN_RATE_LIMIT_MAX):
            url = f"https://{base_domain}/path-{i}/metadata.json"
            res = self.client.post(
                "/api/agentic/provisioning/account_requests",
                data={
                    "id": f"req_domain_rl_{i}",
                    "email": f"domain-rl-{i}@example.com",
                    "client_id": url,
                    "code_challenge": challenge,
                    "code_challenge_method": "S256",
                },
                content_type="application/json",
            )
            assert res.status_code == 401, f"Request {i} failed: {res.json()}"

        url = f"https://{base_domain}/path-blocked/metadata.json"
        res = self.client.post(
            "/api/agentic/provisioning/account_requests",
            data={
                "id": "req_domain_rl_blocked",
                "email": "[email protected]",
                "client_id": url,
                "code_challenge": challenge,
                "code_challenge_method": "S256",
            },
            content_type="application/json",
        )
        assert res.status_code == 429
        assert res.json()["error"]["code"] == "rate_limited"

    @patch("posthog.api.oauth.cimd.CIMD_THROTTLE_CLASSES", new=[])
    def test_cimd_domain_rate_limit_does_not_block_different_domains(self, _url_mock):
        from ee.api.agentic_provisioning.constants import CIMD_DOMAIN_RATE_LIMIT_MAX

        _, challenge = self._pkce_pair()

        for i in range(CIMD_DOMAIN_RATE_LIMIT_MAX + 2):
            url = f"https://domain-{i}.example.com/.well-known/metadata.json"
            res = self.client.post(
                "/api/agentic/provisioning/account_requests",
                data={
                    "id": f"req_diff_domain_{i}",
                    "email": f"diff-domain-{i}@example.com",
                    "client_id": url,
                    "code_challenge": challenge,
                    "code_challenge_method": "S256",
                },
                content_type="application/json",
            )
            assert res.status_code == 401, f"Request {i} for domain-{i} failed: {res.json()}"

    @patch("posthog.api.oauth.cimd.CIMD_THROTTLE_CLASSES", new=[])
    @patch("posthog.api.oauth.cimd.refresh_cimd_metadata_task")
    def test_cimd_domain_rate_limit_skipped_for_existing_apps(self, mock_refresh, _url_mock):
        from ee.api.agentic_provisioning.constants import CIMD_DOMAIN_RATE_LIMIT_MAX

        base_domain = "existing.example.com"
        _, challenge = self._pkce_pair()

        for i in range(CIMD_DOMAIN_RATE_LIMIT_MAX + 1):
            url = f"https://{base_domain}/path-{i}/metadata.json"
            OAuthApplication.objects.create(
                name=f"Existing CIMD {i}",
                client_secret="",
                client_type=OAuthApplication.CLIENT_CONFIDENTIAL,
                jwks_uri=TEST_PARTNER_JWKS_URI,
                authorization_grant_type=OAuthApplication.GRANT_AUTHORIZATION_CODE,
                redirect_uris="http://127.0.0.1:3000/callback",
                algorithm="RS256",
                client_id=url,
                is_cimd_client=True,
                is_provisioning_partner=True,
                _provisioning_config=provisioning_config(
                    active=True, can_create_accounts=True, can_provision_resources=True
                ),
            )

        url = f"https://{base_domain}/path-0/metadata.json"
        res = self._post_as_partner(
            "/api/agentic/provisioning/account_requests",
            {
                "id": "req_existing_domain",
                "email": "[email protected]",
                "code_challenge": challenge,
                "code_challenge_method": "S256",
            },
            partner=OAuthApplication.objects.get(client_id=url),
        )
        assert res.status_code == 200, res.json()

    @patch("posthog.api.oauth.cimd.refresh_cimd_metadata_task")
    def test_self_serve_org_named_after_client_name(self, mock_refresh, _url_mock):
        partner = OAuthApplication.objects.create(
            name="Partner App",
            client_secret="",
            client_type=OAuthApplication.CLIENT_CONFIDENTIAL,
            jwks_uri=TEST_PARTNER_JWKS_URI,
            authorization_grant_type=OAuthApplication.GRANT_AUTHORIZATION_CODE,
            redirect_uris="http://127.0.0.1:3000/callback",
            algorithm="RS256",
            client_id=CIMD_PROV_URL,
            is_cimd_client=True,
            is_provisioning_partner=True,
            _provisioning_config=provisioning_config(
                active=True, can_create_accounts=True, can_provision_resources=True
            ),
        )

        email = "[email protected]"
        _, challenge = self._pkce_pair()
        self._post_as_partner(
            "/api/agentic/provisioning/account_requests",
            {
                "id": "req_cimd_org",
                "email": email,
                "code_challenge": challenge,
                "code_challenge_method": "S256",
            },
            partner=partner,
        )

        user = User.objects.get(email=email)
        org = user.organization_memberships.first().organization
        assert org.name == f"Partner App ({email})"