: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
chore(ee): move authentication helpers out of ee (#122122)
Reece Jones066d97ee052f
diff --git a/.github/CODEOWNERS b/.github/CODEOWNERSindex 77a93220f6ce..2bcc35108e7d 100644--- a/.github/CODEOWNERS+++ b/.github/CODEOWNERS@@ -54,6 +54,7 @@ rust/persons_migrations/** @PostHog/team-ingestion# Critical authentication logicee/api/authentication.py @PostHog/team-security+posthog/api/sso.py @PostHog/team-securityposthog/api/authentication.py @PostHog/team-securityposthog/auth.py @PostHog/team-securityposthog/api/scim/auth.py @PostHog/team-securitydiff --git a/.semgrep/rules/devex/user-email-lookups-use-the-shared-fold.yaml b/.semgrep/rules/devex/user-email-lookups-use-the-shared-fold.yamlindex 157bea024a4b..e52aedf50e28 100644--- a/.semgrep/rules/devex/user-email-lookups-use-the-shared-fold.yaml+++ b/.semgrep/rules/devex/user-email-lookups-use-the-shared-fold.yaml@@ -41,6 +41,7 @@ rules:- /posthog/api/authentication.py- /posthog/api/id_jag.py- /posthog/api/signup.py+ - /posthog/api/sso.py- /posthog/api/user.py- /posthog/api/webauthn.py- /posthog/models/user.pydiff --git a/ee/api/authentication.py b/ee/api/authentication.pyindex 99c0ad5514bf..17cb602687d3 100644--- a/ee/api/authentication.py+++ b/ee/api/authentication.py@@ -1,537 +1,48 @@import re-import json-import base64from typing import Any, Literal, TypedDict, cast-from uuid import UUID, uuid4--from django.core.exceptions import ValidationError as DjangoValidationError-from django.http.response import HttpResponse-from django.urls.base import reverseimport jwtimport structlog-import posthoganalytics-import redis.exceptions as redis_exceptionsfrom jwt.algorithms import RSAAlgorithm-from onelogin.saml2.xml_utils import OneLogin_Saml2_XMLfrom rest_framework import authentication-from rest_framework.decorators import api_view-from rest_framework.exceptions import AuthenticationFailed, PermissionDenied+from rest_framework.exceptions import AuthenticationFailedfrom rest_framework.request import Request-from social_core.backends.google import GoogleOAuth2-from social_core.backends.saml import (- OID_COMMON_NAME,- OID_GIVEN_NAME,- OID_MAIL,- OID_SURNAME,- OID_USERID,- SAMLAuth,- SAMLIdentityProvider,-)-from social_core.exceptions import AuthFailed, AuthMissingParameter-from social_django.models import UserSocialAuth-from social_django.strategy import DjangoStrategy-from social_django.utils import load_backend, load_strategy-from posthog.api.scim.utils import mask_email+from posthog.api.sso import (+ CustomGoogleOAuth2 as _CustomGoogleOAuth2,+ MultitenantSAMLAuth as _MultitenantSAMLAuth,+)from posthog.cloud_utils import get_cached_instance_license-from posthog.constants import AvailableFeaturefrom posthog.exceptions_capture import capture_exception-from posthog.helpers.email_utils import EmailLookupHandler-from posthog.helpers.sso import UNVERIFIED_SOCIAL_EMAIL_ERRORfrom posthog.models.activity_logging.utils import ActivityCredentialMixin-from posthog.models.identity_provider_config import IdentityProviderConfig, has_verified_organization_domain_q-from posthog.models.organization import OrganizationMembership-from posthog.models.organization_domain import OrganizationDomain-from posthog.redis import get_clientfrom ee import settingsfrom ee.api.vercel.types import VercelClaims, VercelSystemClaims, VercelUser, VercelUserClaimsfrom ee.api.vercel.utils import get_vercel_jwks-saml_logger = structlog.get_logger("posthog.auth.saml")---def _saml_log_context(email: str, organization_id: UUID | None = None) -> dict[str, Any]:-- ctx: dict[str, Any] = {- "masked_email": mask_email(email),- "email_domain": email.split("@")[-1].lower(),- }-- try:- user = EmailLookupHandler.get_user_by_email(email, is_active=None)- if user:- ctx["user_id"] = str(user.id)- if organization_id:- membership = user.organization_memberships.filter(organization_id=organization_id).first()- ctx["membership_level"] = membership.get_level_display() if membership else "none"- else:- ctx["user_id"] = "unknown"- except Exception:- pass-- return ctx---@api_view(["GET"])-def saml_metadata_view(request, *args, **kwargs):- if (- not request.user.organization_memberships.get(organization=request.user.organization).level- >= OrganizationMembership.Level.ADMIN- ):- raise PermissionDenied("You need to be an administrator or owner to access this resource.")-- complete_url = reverse("social:complete", args=("saml",))- saml_backend = load_backend(load_strategy(request), "saml", redirect_uri=complete_url)- metadata, errors = saml_backend.generate_metadata_xml()-- if not errors:- return HttpResponse(content=metadata, content_type="text/xml")-+logger = structlog.get_logger(__name__)-# OASIS SAML bindings cap RelayState at 80 bytes; the cache TTL only needs to outlast the IdP round trip.-SAML_RELAY_STATE_CACHE_TTL_SECONDS = 60 * 10+class MultitenantSAMLAuth(_MultitenantSAMLAuth):+ """Legacy alias for sessions created before SAML auth moved to posthog.api.sso.-def _saml_relay_state_cache_key(key: str) -> str:- return f"saml_relay_state:{key}"+ `django.contrib.auth.get_user()` only restores a session if its stored backend path is in+ `AUTHENTICATION_BACKENDS`. `authenticate()` returns None so no new session adopts this path.+ """+ name = "saml-legacy"-def _relay_state_from_json(raw: str | bytes) -> dict[str, Any] | None:- try:- state = json.loads(raw)- except ValueError:+ def authenticate(self, request, *args, **kwargs):return None- return state if isinstance(state, dict) else None-def _store_saml_relay_state(state: dict[str, Any]) -> str:- key = str(uuid4())- try:- get_client().setex(_saml_relay_state_cache_key(key), SAML_RELAY_STATE_CACHE_TTL_SECONDS, json.dumps(state))- except redis_exceptions.RedisError:- # Sessions are DB-backed, so a Redis outage must not block login. Send the pre-cache JSON- # instead: the IdP may refuse it above the RelayState cap, but failing the flow is worse.- return json.dumps(state)- return key+class CustomGoogleOAuth2(_CustomGoogleOAuth2):+ """Legacy alias for sessions created before Google auth moved to posthog.api.sso."""+ name = "google-oauth2-legacy"-def _load_saml_relay_state(key: str) -> dict[str, Any] | None:- try:- raw = get_client().get(_saml_relay_state_cache_key(key))- except redis_exceptions.RedisError:+ def authenticate(self, request, *args, **kwargs):return None- return _relay_state_from_json(raw) if raw else None---class MultitenantSAMLAuth(SAMLAuth):- """- Implements our own version of SAML auth that supports multitenancy. Instead of relying on instance-based config via env vars,- each organization can have multiple verified domains each with its own SAML configuration.- """-- def auth_complete(self, *args, **kwargs):- # attempt to recover from missing relay state in idp-initiated SAML- self._recover_idp_initiated_relay_state()-- try:- result = super().auth_complete(*args, **kwargs)- saml_logger.info("saml_auth_complete")- return result- except Exception as e:- posthoganalytics.tag("request_data", json.dumps(self.strategy.request_data()))- saml_logger.warning("saml_auth_complete_failed", error=str(e))- raise-- def _recover_idp_initiated_relay_state(self) -> None:- """- SP-initiated logins carry a RelayState holding the linked IdP config's identifier, which- is how we route an assertion to the right tenant. IdP-initiated logins don't: the IdP- either omits RelayState (Azure AD) or sends its own value (e.g. an app URL). When the- RelayState doesn't map to a config, recover the tenant from the Response's <Issuer> (the- IdP entity ID, stored on the `IdentityProviderConfig` as `saml_entity_id`) and inject a- RelayState so the rest of the flow proceeds unchanged.- """- data = self.strategy.request_data()- saml_response = data.get("SAMLResponse")- if not saml_response:- return-- if self._relay_state_points_to_config(data.get("RelayState")):- return # SP-initiated: RelayState already identifies the tenant-- issuer = self._extract_response_issuer(saml_response)- if not issuer:- return-- # `saml_entity_id` has no uniqueness constraint, so only act on an unambiguous config —- # never guess which tenant an unsolicited assertion belongs to. The config can be linked- # to multiple domains, so resolve it before choosing a RelayState.- configs = list(- # nosemgrep: idor-lookup-without-org (pre-auth SAML routing by IdP entity id; the assertion signature is verified afterwards)- IdentityProviderConfig.objects.filter(- has_verified_organization_domain_q(),- saml_entity_id=issuer,- ).distinct()- )- if len(configs) != 1:- if len(configs) > 1:- saml_logger.warning("saml_idp_initiated_ambiguous_issuer", issuer=issuer, count=len(configs))- return-- idp_config = configs[0]- relay_state = idp_config.saml_relay_state- if not idp_config.has_saml or not relay_state:- return-- saml_logger.info(- "saml_idp_initiated_relay_state_recovered",- identity_provider_config_id=str(idp_config.id),- organization_id=str(idp_config.organization_id),- )- # Inject the config identifier so the standard flow can route the response to the tenant.- strategy = cast(DjangoStrategy, self.strategy)- mutable_post = strategy.request.POST.copy()- mutable_post["RelayState"] = relay_state- # django-stubs types request.POST as immutable; reassigning it is valid at runtime.- strategy.request.POST = mutable_post # type: ignore[assignment] # ty: ignore[invalid-assignment]-- def parse_relay_state(self, relay_state_str: str) -> dict:- # A miss still routes the pre-cache JSON payload, an IdP-supplied value, or an expired key- # through the base parser, which sends the last two to the IdP-initiated recovery.- stored = _load_saml_relay_state(relay_state_str)- return stored if stored is not None else super().parse_relay_state(relay_state_str)-- def _relay_state_points_to_config(self, relay_state_str: str | None) -> bool:- if not relay_state_str:- return False- candidate = self.parse_relay_state(relay_state_str).get("idp")- if not candidate:- return False- try:- # nosemgrep: idor-lookup-without-org (pre-auth SAML routing check by IdP config identifier)- return (- IdentityProviderConfig.objects.filter(- has_verified_organization_domain_q(),- saml_relay_state=candidate,- )- .distinct()- .exists()- )- except (DjangoValidationError, ValueError):- return False # malformed or IdP-supplied value-- @staticmethod- def _extract_response_issuer(saml_response_b64: str) -> str | None:- # POST-binding SAMLResponse is base64-encoded (not deflated). Parsing is XXE-safe- # (onelogin forbids DTDs/entities); the signature is still verified afterwards.- try:- document = OneLogin_Saml2_XML.to_etree(base64.b64decode(saml_response_b64))- issuer_nodes = OneLogin_Saml2_XML.query(document, "/samlp:Response/saml:Issuer")- except Exception:- return None- if not issuer_nodes:- return None- return (OneLogin_Saml2_XML.element_text(issuer_nodes[0]) or "").strip() or None-- def get_idp(self, identity_provider_config_or_id: IdentityProviderConfig | str | None) -> SAMLIdentityProvider:- if identity_provider_config_or_id is None:- saml_logger.warning("saml_idp_lookup_failed", idp_id="None")- raise AuthFailed(self, "Authentication request is invalid. Invalid RelayState.")-- if isinstance(identity_provider_config_or_id, IdentityProviderConfig):- idp_config = identity_provider_config_or_id- else:- try:- # nosemgrep: idor-lookup-without-org (pre-auth SAML flow, lookup by config identifier on verified configs)- idp_config = (- IdentityProviderConfig.objects.filter(- has_verified_organization_domain_q(),- saml_relay_state=identity_provider_config_or_id,- )- .distinct()- .get()- )- except (- IdentityProviderConfig.DoesNotExist,- IdentityProviderConfig.MultipleObjectsReturned,- DjangoValidationError,- ):- saml_logger.warning("saml_idp_lookup_failed", idp_id=str(identity_provider_config_or_id))- raise AuthFailed(self, "Authentication request is invalid. Invalid RelayState.")-- if not idp_config.organization.is_feature_available(AvailableFeature.SAML):- saml_logger.warning(- "saml_license_missing",- organization_id=str(idp_config.organization_id),- )- raise AuthFailed(- self,- "Your organization does not have the required license to use SAML.",- )-- if not idp_config.saml_relay_state:- raise AuthFailed(self, "Authentication request is invalid. Invalid RelayState.")-- return SAMLIdentityProvider(- self,- idp_config.saml_relay_state,- entity_id=idp_config.saml_entity_id,- url=idp_config.saml_acs_url,- x509cert=idp_config.saml_x509_cert,- )-- def auth_url(self):- email = self.strategy.request_data().get("email")-- if not email:- raise AuthMissingParameter(self, "email")-- idp_configs = list(IdentityProviderConfig.objects.saml_for_email(email)[:2])- if len(idp_configs) == 0:- saml_logger.warning("saml_not_configured", **_saml_log_context(email))- raise AuthFailed(self, "SAML not configured for this user.")- if len(idp_configs) > 1:- saml_logger.warning("saml_multiple_configs", **_saml_log_context(email))- raise AuthFailed(self, "Multiple SAML configurations found for this user.")-- idp_config = idp_configs[0]- saml_logger.info(- "saml_auth_redirect",- organization_id=str(idp_config.organization_id),- **_saml_log_context(email, idp_config.organization_id),- )- identity_provider = self.get_idp(idp_config)- auth = self._create_saml_auth(idp=identity_provider)- # `return_to` sets the RelayState, a value the IdP echoes back in its POST to the- # (shared) auth_complete URL. The session cookie is SameSite=Lax and so is dropped on- # the IdP's cross-site POST, which would otherwise lose `next` and send the user to `/`;- # carrying it in RelayState lets the base `auth_complete` recover it for the post-login- # redirect. `idp` carries the linked config identifier since multiple IdPs share the URL.- # We deliberately omit the session key that upstream `SAMLAuth.auth_url` also packs into- # RelayState: it would be disclosed to the (potentially attacker-controlled) IdP in the- # redirect, and `next` alone is enough to recover the redirect without it.- relay_state = {"idp": identity_provider.name, "next": self.data.get("next")}- return auth.login(return_to=_store_saml_relay_state(relay_state))-- def _get_attr(- self,- response_attributes: dict[str, Any],- attribute_names: list[str],- optional: bool = False,- ) -> str:- """- Fetches a specific attribute from the SAML response, attempting with multiple different attribute names.- We attempt multiple attribute names to make it easier for admins to configure SAML (less configuration to set).- """- output: str | list[str] | None = None- for _attr in attribute_names:- if _attr in response_attributes:- output = response_attributes[_attr]- break-- if not output and not optional:- raise AuthMissingParameter(self, attribute_names[0])-- if isinstance(output, list):- output = output[0]-- return output or ""-- def get_user_details(self, response) -> dict[str, str | None]:- """- Overridden to find attributes across multiple possible names.- """- attributes = response["attributes"]- email = self._get_attr(- attributes,- [- "email",- "EMAIL",- "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress",- OID_MAIL,- ],- )-- self._validate_email_domain(response, email)-- return {- "fullname": self._get_attr(- attributes,- ["full_name", "FULL_NAME", "fullName", OID_COMMON_NAME],- optional=True,- ),- "first_name": self._get_attr(- attributes,- [- "first_name",- "FIRST_NAME",- "firstName",- "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname",- OID_GIVEN_NAME,- ],- optional=True,- ),- "last_name": self._get_attr(- attributes,- [- "last_name",- "LAST_NAME",- "lastName",- "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname",- OID_SURNAME,- ],- optional=True,- ),- "email": email,- }-- def _validate_email_domain(self, response: dict, email: str) -> None:- """- Ensures the email domain from the SAML assertion is consistent with- the OrganizationDomain that owns this IdP configuration.- """- idp_name = response.get("idp_name")- if not idp_name:- saml_logger.warning(- "saml_email_domain_validation_failed",- reason="missing_idp_name",- **_saml_log_context(email),- )- raise AuthFailed(self, "Authentication request is invalid. Missing IdP identifier.")-- try:- # nosemgrep: idor-lookup-without-org (pre-auth SAML validation, config identifier from IdP round-trip)- idp_config = IdentityProviderConfig.objects.get(saml_relay_state=idp_name)- except (IdentityProviderConfig.DoesNotExist, DjangoValidationError):- saml_logger.warning(- "saml_email_domain_validation_failed",- reason="invalid_idp",- idp_id=str(idp_name),- **_saml_log_context(email),- )- raise AuthFailed(self, "Authentication request is invalid. Invalid IdP identifier.")-- # A config can back several verified domains, and an assertion is valid for any of them.- configured_domains = {- domain.lower()- for domain in idp_config.organization_domains.filter(verified_at__isnull=False).values_list(- "domain", flat=True- )- }- if email.rsplit("@", 1)[-1].lower() in configured_domains:- return-- if not configured_domains:- # SAML stays gated behind domain verification, so a config that backs none can't- # authorize anyone, whatever its RelayState resolves to.- saml_logger.warning(- "saml_email_domain_validation_failed",- reason="no_verified_domain",- idp_id=str(idp_name),- **_saml_log_context(email),- )- raise AuthFailed(self, "Authentication request is invalid. Invalid IdP identifier.")-- saml_logger.warning(- "saml_email_domain_mismatch",- configured_domains=sorted(configured_domains),- organization_id=str(idp_config.organization_id),- **_saml_log_context(email, idp_config.organization_id),- )- raise AuthFailed(- self,- "Authentication failed. The email domain from your identity provider does not match the configured domain.",- )-- def get_user_id(self, details, response):- """- Overridden to find user ID across multiple attribute names.- Get the permanent ID for this user from the response.- """- USER_ID_ATTRIBUTES = ["name_id", "NAME_ID", "nameId", OID_USERID]- uid = self._get_attr(response["attributes"], USER_ID_ATTRIBUTES)- return f"{response['idp_name']}:{uid}"---class CustomGoogleOAuth2(GoogleOAuth2):- def auth_extra_arguments(self):- extra_args = super().auth_extra_arguments()- is_reauth = self.strategy.request_get().get("reauth") == "true"- if not is_reauth:- prompt_tokens = [token for token in str(extra_args.get("prompt", "")).split() if token]- if "select_account" not in prompt_tokens:- prompt_tokens.append("select_account")- extra_args["prompt"] = " ".join(prompt_tokens)-- email = self.strategy.request_get().get("email")-- if email:- extra_args["login_hint"] = email-- return extra_args-- def get_user_id(self, details, response):- """- Retrieve and migrate Google OAuth user identification.-- Note: While social-auth-core supports using Google's sub claim via- settings.USE_UNIQUE_USER_ID = True, this setting was not enabled historically- in our application. This led to emails being stored as uids instead of the- more stable Google sub identifier.-- 'sub' (subject identifier) is part of OpenID Connect and is guaranteed to be a- stable, unique identifier for the user within Google's system. It's designed- specifically for authentication purposes and won't change even if the user changes- their email or other profile details.-- This method handles two types of user identification:- 1. Legacy users: Originally stored with email as their uid (due to missing USE_UNIQUE_USER_ID)- 2. New users: Using Google's sub claim (unique identifier) as uid-- The method first checks if a user exists with the sub as uid. If not found,- it looks for a legacy user with email as uid and migrates them to use sub.- This ensures a smooth transition from email-based to sub-based identification- while maintaining backward compatibility.-- Args:- details: User details dictionary from OAuth response- response: Full OAuth response from Google-- Returns:- str: The Google sub claim to be used as uid- """- email = response.get("email")- sub = response.get("sub")-- if not sub:- raise ValueError("Google OAuth response missing 'sub' claim")-- try:- # First try: Find user by sub (preferred method)- social_auth = UserSocialAuth.objects.get(provider="google-oauth2", uid=sub)- return sub- except UserSocialAuth.DoesNotExist:- pass-- try:- # Second try: Find and migrate legacy user using email as uid- social_auth = UserSocialAuth.objects.get(provider="google-oauth2", uid=email)- # This lookup resolves the account by email address, so the email has to be verified,- # the same as for `associate_by_email`.- if response.get("email_verified") is not True:- raise AuthFailed(self, UNVERIFIED_SOCIAL_EMAIL_ERROR)- # Migrate user from email to sub- social_auth.uid = sub- social_auth.save()- return sub- except UserSocialAuth.DoesNotExist:- # No existing user found - use sub for new account- return sub---logger = structlog.get_logger(__name__)def _get_bearer_token(request: Request) -> str | None:@@ -765,30 +276,3 @@ class BillingServiceAuthentication(ActivityCredentialMixin, authentication.BaseA)return cast(BillingServiceJWTPayload, decoded_payload)---def social_auth_allowed(backend, details, response, *args, **kwargs) -> None:- email = details.get("email")- # Check if SSO enforcement is enabled for this email address- sso_enforcement = OrganizationDomain.objects.get_sso_enforcement_for_email_address(email)- if sso_enforcement is None or sso_enforcement == backend.name:- return-- saml_logger.warning(- "sso_enforcement_blocked_login",- attempted_backend=backend.name,- enforced_backend=sso_enforcement,- **(_saml_log_context(email) if email else {"masked_email": "unknown"}),- )-- if sso_enforcement == "saml":- raise AuthFailed(backend, "saml_sso_enforced")- elif sso_enforcement == "google-oauth2":- raise AuthFailed(backend, "google_sso_enforced")- elif sso_enforcement == "github":- raise AuthFailed(backend, "github_sso_enforced")- elif sso_enforcement == "gitlab":- raise AuthFailed(backend, "gitlab_sso_enforced")- else:- # catch-all in case we missed a case above- raise AuthFailed(backend, "sso_enforced", email)diff --git a/ee/api/test/test_admin_oauth2_middleware.py b/ee/api/test/test_admin_oauth2_middleware.pyindex 4f1c92bdffbb..fa168700c961 100644--- a/ee/api/test/test_admin_oauth2_middleware.py+++ b/ee/api/test/test_admin_oauth2_middleware.py@@ -17,11 +17,10 @@ from cryptography.hazmat.primitives import serializationfrom cryptography.hazmat.primitives.asymmetric import rsafrom parameterized import parameterized+from posthog.admin.oauth2 import AdminOAuth2Middleware, _get_email_from_id_token, admin_oauth2_callbackfrom posthog.models import Userfrom posthog.session.backend import SessionStore-from ee.middleware import AdminOAuth2Middleware, _get_email_from_id_token, admin_oauth2_callback-# Any path under the prefix exercises the middleware branch; no route has to exist.ADMIN_API_PATH = "/admin/api/example/"@@ -192,10 +191,10 @@ class TestNonceValidation(BaseTest):return requestdef _mock_token_exchange(self, id_token: str):- return patch("ee.middleware._exchange_code_for_token", return_value={"id_token": id_token})+ return patch("posthog.admin.oauth2._exchange_code_for_token", return_value={"id_token": id_token})def _mock_get_email(self, email: str, payload: dict):- return patch("ee.middleware._get_email_from_id_token", return_value=(email, payload))+ return patch("posthog.admin.oauth2._get_email_from_id_token", return_value=(email, payload))@staticmethoddef _assert_redirects_to_admin(response: HttpResponse) -> HttpResponseRedirect:diff --git a/ee/api/test/test_authentication.py b/ee/api/test/test_authentication.pyindex c25e1a86b13c..6ae29b127180 100644--- a/ee/api/test/test_authentication.py+++ b/ee/api/test/test_authentication.py@@ -32,6 +32,7 @@ from social_django.utils import load_strategyfrom posthog.api.authentication import social_identity_matches_sessionfrom posthog.api.oidc import MultitenantOIDCAuth+from posthog.api.sso import CustomGoogleOAuth2, MultitenantSAMLAuthfrom posthog.constants import AvailableFeaturefrom posthog.helpers.sso import GITHUB_EMAIL_LOOKUP_ERROR, UNVERIFIED_SOCIAL_EMAIL_ERRORfrom posthog.models import OrganizationMembership, User@@ -42,7 +43,6 @@ from posthog.models.organization_domain import OrganizationDomainfrom posthog.redis import get_clientfrom posthog.session.activity import session_public_id-from ee.api.authentication import CustomGoogleOAuth2, MultitenantSAMLAuthfrom ee.api.test.base import APILicensedTestfrom ee.models.license import License@@ -1518,7 +1518,7 @@ class TestEESAMLAuthenticationAPI(APILicensedTest):auth = object.__new__(MultitenantSAMLAuth)legacy = {"idp": str(self.organization_domain.id), "next": "/settings/organization/authentication"}- with patch("ee.api.authentication.get_client", side_effect=redis_exceptions.ConnectionError("down")):+ with patch("posthog.api.sso.get_client", side_effect=redis_exceptions.ConnectionError("down")):self.assertEqual(auth.parse_relay_state(json.dumps(legacy)), legacy)self.assertEqual(auth.parse_relay_state("https://idp.hogflix.io/launch"), {"idp": "https://idp.hogflix.io/launch"}@@ -2076,7 +2076,7 @@ class TestCustomGoogleOAuth2(APILicensedTest):"""Test that auth_extra_arguments appends select_account to existing prompt values."""self.google_oauth.strategy = self._mock_strategy({}) # type: ignore[assignment] # ty: ignore[invalid-assignment]- with patch("ee.api.authentication.GoogleOAuth2.auth_extra_arguments", return_value={"prompt": "consent"}):+ with patch("posthog.api.sso.GoogleOAuth2.auth_extra_arguments", return_value={"prompt": "consent"}):extra_args = self.google_oauth.auth_extra_arguments()self.assertEqual(extra_args["prompt"], "consent select_account")@@ -2162,7 +2162,7 @@ class TestSSOEnforcement(APILicensedTest):@override_settings(**SAML_MOCK_SETTINGS, **GOOGLE_MOCK_SETTINGS)def test_cannot_use_social_auth_when_saml_is_required(self):"""Test that Google OAuth2 fails when SAML is enforced"""- from ee.api.authentication import social_auth_allowed+ from posthog.api.sso import social_auth_allowed# Create domain with SAML enforcementdomain = OrganizationDomain.objects.create(@@ -2200,7 +2200,7 @@ class TestSSOEnforcement(APILicensedTest):@override_settings(**SAML_MOCK_SETTINGS, **GOOGLE_MOCK_SETTINGS)def test_other_social_auth_blocked_with_google_enforcement(self):"""Test other social auth methods are blocked, including saml, when Google is enforced"""- from ee.api.authentication import social_auth_allowed+ from posthog.api.sso import social_auth_allowedOrganizationDomain.objects.create(domain="testdomain.com",diff --git a/ee/settings.py b/ee/settings.pyindex 5c4d467e411e..0ef819ea63cc 100644--- a/ee/settings.py+++ b/ee/settings.py@@ -11,8 +11,12 @@ from posthog.utils import str_to_bool# SSOAUTHENTICATION_BACKENDS = [*AUTHENTICATION_BACKENDS,- "ee.api.authentication.MultitenantSAMLAuth",+ "posthog.api.sso.MultitenantSAMLAuth","posthog.api.oidc.MultitenantOIDCAuth",+ "posthog.api.sso.CustomGoogleOAuth2",+ # Legacy paths for sessions issued before the move. They resolve existing sessions but+ # refuse to authenticate, so new sessions only ever use the paths above.+ "ee.api.authentication.MultitenantSAMLAuth","ee.api.authentication.CustomGoogleOAuth2",]@@ -58,7 +62,7 @@ if ENFORCE_ADMIN_OAUTH2 and ADMIN_AUTH_GOOGLE_OAUTH2_KEY and ADMIN_AUTH_GOOGLE_O# middleware must be added after `AuthenticationMiddleware``MIDDLEWARE = MIDDLEWARE.copy()auth_middleware_index = MIDDLEWARE.index("django.contrib.auth.middleware.AuthenticationMiddleware")- MIDDLEWARE.insert(auth_middleware_index + 1, "ee.middleware.AdminOAuth2Middleware")+ MIDDLEWARE.insert(auth_middleware_index + 1, "posthog.admin.oauth2.AdminOAuth2Middleware")CUSTOMER_IO_API_KEY = get_from_env("CUSTOMER_IO_API_KEY", "", type_cast=str)CUSTOMER_IO_API_URL = get_from_env("CUSTOMER_IO_API_URL", "https://api-eu.customer.io", type_cast=str)diff --git a/ee/urls.py b/ee/urls.pyindex 12ab4efd6a6a..7f8fef04f746 100644--- a/ee/urls.py+++ b/ee/urls.py@@ -11,6 +11,9 @@ from django_otp.plugins.otp_static.models import StaticDevicefrom django_otp.plugins.otp_totp.models import TOTPDevicefrom posthog.admin import register_all_admin+from posthog.admin.oauth2 import admin_oauth2_callback+from posthog.admin.oauth_views import admin_auth_check, admin_oauth_success+from posthog.api.sso import saml_metadata_viewfrom posthog.ingress.vercel.provider import build_vercel_providerfrom posthog.ingress.views import build_webhook_viewfrom posthog.middleware import impersonated_session_logout@@ -19,13 +22,11 @@ from posthog.views import api_key_search_view, redis_edit_ttl_view, redis_valuesfrom products.cdp.backend.api import hooksfrom ee.admin.loginas_views import loginas_user, upgrade_impersonation-from ee.admin.oauth_views import admin_auth_check, admin_oauth_successfrom ee.api import integrationfrom ee.api.vercel import vercel_connect, vercel_sso-from ee.middleware import admin_oauth2_callbackfrom ee.support_sidebar_max.views import MaxChatViewSet-from .api import authentication, billing, conversation, core_memory, organization_billing, project_billing+from .api import billing, conversation, core_memory, organization_billing, project_billingdef extend_api_router() -> None:@@ -173,7 +174,7 @@ else:urlpatterns: list[Any] = [- path("api/saml/metadata/", authentication.saml_metadata_view),+ path("api/saml/metadata/", saml_metadata_view),path("max/chat/", csrf_exempt(MaxChatViewSet.as_view({"post": "create"})), name="max_chat"),re_path(r"^login/vercel/?$", vercel_sso.VercelSSOViewSet.as_view({"get": "sso_redirect"})),re_path(r"^login/vercel/continue/?$", vercel_sso.VercelSSOViewSet.as_view({"get": "sso_continue"})),diff --git a/ee/middleware.py b/posthog/admin/oauth2.pysimilarity index 100%rename from ee/middleware.pyrename to posthog/admin/oauth2.pydiff --git a/ee/admin/oauth_views.py b/posthog/admin/oauth_views.pysimilarity index 100%rename from ee/admin/oauth_views.pyrename to posthog/admin/oauth_views.pydiff --git a/posthog/api/sso.py b/posthog/api/sso.pynew file mode 100644index 000000000000..b66142b4656f--- /dev/null+++ b/posthog/api/sso.py@@ -0,0 +1,546 @@+import json+import base64+from typing import Any, cast+from uuid import UUID, uuid4++from django.core.exceptions import ValidationError as DjangoValidationError+from django.http.response import HttpResponse+from django.urls.base import reverse++import structlog+import posthoganalytics+import redis.exceptions as redis_exceptions+from onelogin.saml2.xml_utils import OneLogin_Saml2_XML+from rest_framework.decorators import api_view+from rest_framework.exceptions import PermissionDenied+from social_core.backends.google import GoogleOAuth2+from social_core.backends.saml import (+ OID_COMMON_NAME,+ OID_GIVEN_NAME,+ OID_MAIL,+ OID_SURNAME,+ OID_USERID,+ SAMLAuth,+ SAMLIdentityProvider,+)+from social_core.exceptions import AuthFailed, AuthMissingParameter+from social_django.models import UserSocialAuth+from social_django.strategy import DjangoStrategy+from social_django.utils import load_backend, load_strategy++from posthog.api.scim.utils import mask_email+from posthog.constants import AvailableFeature+from posthog.helpers.email_utils import EmailLookupHandler+from posthog.helpers.sso import UNVERIFIED_SOCIAL_EMAIL_ERROR+from posthog.models.identity_provider_config import IdentityProviderConfig, has_verified_organization_domain_q+from posthog.models.organization import OrganizationMembership+from posthog.models.organization_domain import OrganizationDomain+from posthog.redis import get_client++saml_logger = structlog.get_logger("posthog.auth.saml")+++def _saml_log_context(email: str, organization_id: UUID | None = None) -> dict[str, Any]:++ ctx: dict[str, Any] = {+ "masked_email": mask_email(email),+ "email_domain": email.split("@")[-1].lower(),+ }++ try:+ user = EmailLookupHandler.get_user_by_email(email, is_active=None)+ if user:+ ctx["user_id"] = str(user.id)+ if organization_id:+ membership = user.organization_memberships.filter(organization_id=organization_id).first()+ ctx["membership_level"] = membership.get_level_display() if membership else "none"+ else:+ ctx["user_id"] = "unknown"+ except Exception:+ pass++ return ctx+++@api_view(["GET"])+def saml_metadata_view(request, *args, **kwargs):+ if (+ not request.user.organization_memberships.get(organization=request.user.organization).level+ >= OrganizationMembership.Level.ADMIN+ ):+ raise PermissionDenied("You need to be an administrator or owner to access this resource.")++ complete_url = reverse("social:complete", args=("saml",))+ saml_backend = load_backend(load_strategy(request), "saml", redirect_uri=complete_url)+ metadata, errors = saml_backend.generate_metadata_xml()++ if not errors:+ return HttpResponse(content=metadata, content_type="text/xml")+++# OASIS SAML bindings cap RelayState at 80 bytes; the cache TTL only needs to outlast the IdP round trip.+SAML_RELAY_STATE_CACHE_TTL_SECONDS = 60 * 10+++def _saml_relay_state_cache_key(key: str) -> str:+ return f"saml_relay_state:{key}"+++def _relay_state_from_json(raw: str | bytes) -> dict[str, Any] | None:+ try:+ state = json.loads(raw)+ except ValueError:+ return None+ return state if isinstance(state, dict) else None+++def _store_saml_relay_state(state: dict[str, Any]) -> str:+ key = str(uuid4())+ try:+ get_client().setex(_saml_relay_state_cache_key(key), SAML_RELAY_STATE_CACHE_TTL_SECONDS, json.dumps(state))+ except redis_exceptions.RedisError:+ # Sessions are DB-backed, so a Redis outage must not block login. Send the pre-cache JSON+ # instead: the IdP may refuse it above the RelayState cap, but failing the flow is worse.+ return json.dumps(state)+ return key+++def _load_saml_relay_state(key: str) -> dict[str, Any] | None:+ try:+ raw = get_client().get(_saml_relay_state_cache_key(key))+ except redis_exceptions.RedisError:+ return None+ return _relay_state_from_json(raw) if raw else None+++class MultitenantSAMLAuth(SAMLAuth):+ """+ Implements our own version of SAML auth that supports multitenancy. Instead of relying on instance-based config via env vars,+ each organization can have multiple verified domains each with its own SAML configuration.+ """++ def auth_complete(self, *args, **kwargs):+ # attempt to recover from missing relay state in idp-initiated SAML+ self._recover_idp_initiated_relay_state()++ try:+ result = super().auth_complete(*args, **kwargs)+ saml_logger.info("saml_auth_complete")+ return result+ except Exception as e:+ posthoganalytics.tag("request_data", json.dumps(self.strategy.request_data()))+ saml_logger.warning("saml_auth_complete_failed", error=str(e))+ raise++ def _recover_idp_initiated_relay_state(self) -> None:+ """+ SP-initiated logins carry a RelayState holding the linked IdP config's identifier, which+ is how we route an assertion to the right tenant. IdP-initiated logins don't: the IdP+ either omits RelayState (Azure AD) or sends its own value (e.g. an app URL). When the+ RelayState doesn't map to a config, recover the tenant from the Response's <Issuer> (the+ IdP entity ID, stored on the `IdentityProviderConfig` as `saml_entity_id`) and inject a+ RelayState so the rest of the flow proceeds unchanged.+ """+ data = self.strategy.request_data()+ saml_response = data.get("SAMLResponse")+ if not saml_response:+ return++ if self._relay_state_points_to_config(data.get("RelayState")):+ return # SP-initiated: RelayState already identifies the tenant++ issuer = self._extract_response_issuer(saml_response)+ if not issuer:+ return++ # `saml_entity_id` has no uniqueness constraint, so only act on an unambiguous config —+ # never guess which tenant an unsolicited assertion belongs to. The config can be linked+ # to multiple domains, so resolve it before choosing a RelayState.+ configs = list(+ # nosemgrep: idor-lookup-without-org (pre-auth SAML routing by IdP entity id; the assertion signature is verified afterwards)+ IdentityProviderConfig.objects.filter(+ has_verified_organization_domain_q(),+ saml_entity_id=issuer,+ ).distinct()+ )+ if len(configs) != 1:+ if len(configs) > 1:+ saml_logger.warning("saml_idp_initiated_ambiguous_issuer", issuer=issuer, count=len(configs))+ return++ idp_config = configs[0]+ relay_state = idp_config.saml_relay_state+ if not idp_config.has_saml or not relay_state:+ return++ saml_logger.info(+ "saml_idp_initiated_relay_state_recovered",+ identity_provider_config_id=str(idp_config.id),+ organization_id=str(idp_config.organization_id),+ )+ # Inject the config identifier so the standard flow can route the response to the tenant.+ strategy = cast(DjangoStrategy, self.strategy)+ mutable_post = strategy.request.POST.copy()+ mutable_post["RelayState"] = relay_state+ # django-stubs types request.POST as immutable; reassigning it is valid at runtime.+ strategy.request.POST = mutable_post # type: ignore[assignment] # ty: ignore[invalid-assignment]++ def parse_relay_state(self, relay_state_str: str) -> dict:+ # A miss still routes the pre-cache JSON payload, an IdP-supplied value, or an expired key+ # through the base parser, which sends the last two to the IdP-initiated recovery.+ stored = _load_saml_relay_state(relay_state_str)+ return stored if stored is not None else super().parse_relay_state(relay_state_str)++ def _relay_state_points_to_config(self, relay_state_str: str | None) -> bool:+ if not relay_state_str:+ return False+ candidate = self.parse_relay_state(relay_state_str).get("idp")+ if not candidate:+ return False+ try:+ # nosemgrep: idor-lookup-without-org (pre-auth SAML routing check by IdP config identifier)+ return (+ IdentityProviderConfig.objects.filter(+ has_verified_organization_domain_q(),+ saml_relay_state=candidate,+ )+ .distinct()+ .exists()+ )+ except (DjangoValidationError, ValueError):+ return False # malformed or IdP-supplied value++ @staticmethod+ def _extract_response_issuer(saml_response_b64: str) -> str | None:+ # POST-binding SAMLResponse is base64-encoded (not deflated). Parsing is XXE-safe+ # (onelogin forbids DTDs/entities); the signature is still verified afterwards.+ try:+ document = OneLogin_Saml2_XML.to_etree(base64.b64decode(saml_response_b64))+ issuer_nodes = OneLogin_Saml2_XML.query(document, "/samlp:Response/saml:Issuer")+ except Exception:+ return None+ if not issuer_nodes:+ return None+ return (OneLogin_Saml2_XML.element_text(issuer_nodes[0]) or "").strip() or None++ def get_idp(self, identity_provider_config_or_id: IdentityProviderConfig | str | None) -> SAMLIdentityProvider:+ if identity_provider_config_or_id is None:+ saml_logger.warning("saml_idp_lookup_failed", idp_id="None")+ raise AuthFailed(self, "Authentication request is invalid. Invalid RelayState.")++ if isinstance(identity_provider_config_or_id, IdentityProviderConfig):+ idp_config = identity_provider_config_or_id+ else:+ try:+ # nosemgrep: idor-lookup-without-org (pre-auth SAML flow, lookup by config identifier on verified configs)+ idp_config = (+ IdentityProviderConfig.objects.filter(+ has_verified_organization_domain_q(),+ saml_relay_state=identity_provider_config_or_id,+ )+ .distinct()+ .get()+ )+ except (+ IdentityProviderConfig.DoesNotExist,+ IdentityProviderConfig.MultipleObjectsReturned,+ DjangoValidationError,+ ):+ saml_logger.warning("saml_idp_lookup_failed", idp_id=str(identity_provider_config_or_id))+ raise AuthFailed(self, "Authentication request is invalid. Invalid RelayState.")++ if not idp_config.organization.is_feature_available(AvailableFeature.SAML):+ saml_logger.warning(+ "saml_license_missing",+ organization_id=str(idp_config.organization_id),+ )+ raise AuthFailed(+ self,+ "Your organization does not have the required license to use SAML.",+ )++ if not idp_config.saml_relay_state:+ raise AuthFailed(self, "Authentication request is invalid. Invalid RelayState.")++ return SAMLIdentityProvider(+ self,+ idp_config.saml_relay_state,+ entity_id=idp_config.saml_entity_id,+ url=idp_config.saml_acs_url,+ x509cert=idp_config.saml_x509_cert,+ )++ def auth_url(self):+ email = self.strategy.request_data().get("email")++ if not email:+ raise AuthMissingParameter(self, "email")++ idp_configs = list(IdentityProviderConfig.objects.saml_for_email(email)[:2])+ if len(idp_configs) == 0:+ saml_logger.warning("saml_not_configured", **_saml_log_context(email))+ raise AuthFailed(self, "SAML not configured for this user.")+ if len(idp_configs) > 1:+ saml_logger.warning("saml_multiple_configs", **_saml_log_context(email))+ raise AuthFailed(self, "Multiple SAML configurations found for this user.")++ idp_config = idp_configs[0]+ saml_logger.info(+ "saml_auth_redirect",+ organization_id=str(idp_config.organization_id),+ **_saml_log_context(email, idp_config.organization_id),+ )+ identity_provider = self.get_idp(idp_config)+ auth = self._create_saml_auth(idp=identity_provider)+ # `return_to` sets the RelayState, a value the IdP echoes back in its POST to the+ # (shared) auth_complete URL. The session cookie is SameSite=Lax and so is dropped on+ # the IdP's cross-site POST, which would otherwise lose `next` and send the user to `/`;+ # carrying it in RelayState lets the base `auth_complete` recover it for the post-login+ # redirect. `idp` carries the linked config identifier since multiple IdPs share the URL.+ # We deliberately omit the session key that upstream `SAMLAuth.auth_url` also packs into+ # RelayState: it would be disclosed to the (potentially attacker-controlled) IdP in the+ # redirect, and `next` alone is enough to recover the redirect without it.+ relay_state = {"idp": identity_provider.name, "next": self.data.get("next")}+ return auth.login(return_to=_store_saml_relay_state(relay_state))++ def _get_attr(+ self,+ response_attributes: dict[str, Any],+ attribute_names: list[str],+ optional: bool = False,+ ) -> str:+ """+ Fetches a specific attribute from the SAML response, attempting with multiple different attribute names.+ We attempt multiple attribute names to make it easier for admins to configure SAML (less configuration to set).+ """+ output: str | list[str] | None = None+ for _attr in attribute_names:+ if _attr in response_attributes:+ output = response_attributes[_attr]+ break++ if not output and not optional:+ raise AuthMissingParameter(self, attribute_names[0])++ if isinstance(output, list):+ output = output[0]++ return output or ""++ def get_user_details(self, response) -> dict[str, str | None]:+ """+ Overridden to find attributes across multiple possible names.+ """+ attributes = response["attributes"]+ email = self._get_attr(+ attributes,+ [+ "email",+ "EMAIL",+ "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress",+ OID_MAIL,+ ],+ )++ self._validate_email_domain(response, email)++ return {+ "fullname": self._get_attr(+ attributes,+ ["full_name", "FULL_NAME", "fullName", OID_COMMON_NAME],+ optional=True,+ ),+ "first_name": self._get_attr(+ attributes,+ [+ "first_name",+ "FIRST_NAME",+ "firstName",+ "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname",+ OID_GIVEN_NAME,+ ],+ optional=True,+ ),+ "last_name": self._get_attr(+ attributes,+ [+ "last_name",+ "LAST_NAME",+ "lastName",+ "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname",+ OID_SURNAME,+ ],+ optional=True,+ ),+ "email": email,+ }++ def _validate_email_domain(self, response: dict, email: str) -> None:+ """+ Ensures the email domain from the SAML assertion is consistent with+ the OrganizationDomain that owns this IdP configuration.+ """+ idp_name = response.get("idp_name")+ if not idp_name:+ saml_logger.warning(+ "saml_email_domain_validation_failed",+ reason="missing_idp_name",+ **_saml_log_context(email),+ )+ raise AuthFailed(self, "Authentication request is invalid. Missing IdP identifier.")++ try:+ # nosemgrep: idor-lookup-without-org (pre-auth SAML validation, config identifier from IdP round-trip)+ idp_config = IdentityProviderConfig.objects.get(saml_relay_state=idp_name)+ except (IdentityProviderConfig.DoesNotExist, DjangoValidationError):+ saml_logger.warning(+ "saml_email_domain_validation_failed",+ reason="invalid_idp",+ idp_id=str(idp_name),+ **_saml_log_context(email),+ )+ raise AuthFailed(self, "Authentication request is invalid. Invalid IdP identifier.")++ # A config can back several verified domains, and an assertion is valid for any of them.+ configured_domains = {+ domain.lower()+ for domain in idp_config.organization_domains.filter(verified_at__isnull=False).values_list(+ "domain", flat=True+ )+ }+ if email.rsplit("@", 1)[-1].lower() in configured_domains:+ return++ if not configured_domains:+ # SAML stays gated behind domain verification, so a config that backs none can't+ # authorize anyone, whatever its RelayState resolves to.+ saml_logger.warning(+ "saml_email_domain_validation_failed",+ reason="no_verified_domain",+ idp_id=str(idp_name),+ **_saml_log_context(email),+ )+ raise AuthFailed(self, "Authentication request is invalid. Invalid IdP identifier.")++ saml_logger.warning(+ "saml_email_domain_mismatch",+ configured_domains=sorted(configured_domains),+ organization_id=str(idp_config.organization_id),+ **_saml_log_context(email, idp_config.organization_id),+ )+ raise AuthFailed(+ self,+ "Authentication failed. The email domain from your identity provider does not match the configured domain.",+ )++ def get_user_id(self, details, response):+ """+ Overridden to find user ID across multiple attribute names.+ Get the permanent ID for this user from the response.+ """+ USER_ID_ATTRIBUTES = ["name_id", "NAME_ID", "nameId", OID_USERID]+ uid = self._get_attr(response["attributes"], USER_ID_ATTRIBUTES)+ return f"{response['idp_name']}:{uid}"+++class CustomGoogleOAuth2(GoogleOAuth2):+ def auth_extra_arguments(self):+ extra_args = super().auth_extra_arguments()+ is_reauth = self.strategy.request_get().get("reauth") == "true"+ if not is_reauth:+ prompt_tokens = [token for token in str(extra_args.get("prompt", "")).split() if token]+ if "select_account" not in prompt_tokens:+ prompt_tokens.append("select_account")+ extra_args["prompt"] = " ".join(prompt_tokens)++ email = self.strategy.request_get().get("email")++ if email:+ extra_args["login_hint"] = email++ return extra_args++ def get_user_id(self, details, response):+ """+ Retrieve and migrate Google OAuth user identification.++ Note: While social-auth-core supports using Google's sub claim via+ settings.USE_UNIQUE_USER_ID = True, this setting was not enabled historically+ in our application. This led to emails being stored as uids instead of the+ more stable Google sub identifier.++ 'sub' (subject identifier) is part of OpenID Connect and is guaranteed to be a+ stable, unique identifier for the user within Google's system. It's designed+ specifically for authentication purposes and won't change even if the user changes+ their email or other profile details.++ This method handles two types of user identification:+ 1. Legacy users: Originally stored with email as their uid (due to missing USE_UNIQUE_USER_ID)+ 2. New users: Using Google's sub claim (unique identifier) as uid++ The method first checks if a user exists with the sub as uid. If not found,+ it looks for a legacy user with email as uid and migrates them to use sub.+ This ensures a smooth transition from email-based to sub-based identification+ while maintaining backward compatibility.++ Args:+ details: User details dictionary from OAuth response+ response: Full OAuth response from Google++ Returns:+ str: The Google sub claim to be used as uid+ """+ email = response.get("email")+ sub = response.get("sub")++ if not sub:+ raise ValueError("Google OAuth response missing 'sub' claim")++ try:+ # First try: Find user by sub (preferred method)+ social_auth = UserSocialAuth.objects.get(provider="google-oauth2", uid=sub)+ return sub+ except UserSocialAuth.DoesNotExist:+ pass++ try:+ # Second try: Find and migrate legacy user using email as uid+ social_auth = UserSocialAuth.objects.get(provider="google-oauth2", uid=email)+ # This lookup resolves the account by email address, so the email has to be verified,+ # the same as for `associate_by_email`.+ if response.get("email_verified") is not True:+ raise AuthFailed(self, UNVERIFIED_SOCIAL_EMAIL_ERROR)+ # Migrate user from email to sub+ social_auth.uid = sub+ social_auth.save()+ return sub+ except UserSocialAuth.DoesNotExist:+ # No existing user found - use sub for new account+ return sub+++def social_auth_allowed(backend, details, response, *args, **kwargs) -> None:+ email = details.get("email")+ # Check if SSO enforcement is enabled for this email address+ sso_enforcement = OrganizationDomain.objects.get_sso_enforcement_for_email_address(email)+ if sso_enforcement is None or sso_enforcement == backend.name:+ return++ saml_logger.warning(+ "sso_enforcement_blocked_login",+ attempted_backend=backend.name,+ enforced_backend=sso_enforcement,+ **(_saml_log_context(email) if email else {"masked_email": "unknown"}),+ )++ if sso_enforcement == "saml":+ raise AuthFailed(backend, "saml_sso_enforced")+ elif sso_enforcement == "google-oauth2":+ raise AuthFailed(backend, "google_sso_enforced")+ elif sso_enforcement == "github":+ raise AuthFailed(backend, "github_sso_enforced")+ elif sso_enforcement == "gitlab":+ raise AuthFailed(backend, "gitlab_sso_enforced")+ else:+ # catch-all in case we missed a case above+ raise AuthFailed(backend, "sso_enforced", email)diff --git a/posthog/api/test/test_authentication.py b/posthog/api/test/test_authentication.pyindex b1af001ee410..68c0ac4ebc62 100644--- a/posthog/api/test/test_authentication.py+++ b/posthog/api/test/test_authentication.py@@ -10,7 +10,7 @@ from posthog.test.base import APIBaseTestfrom unittest.mock import ANY, MagicMock, patchfrom django.conf import settings-from django.contrib.auth import BACKEND_SESSION_KEY, load_backend+from django.contrib.auth import BACKEND_SESSION_KEY, HASH_SESSION_KEY, SESSION_KEY, get_user, load_backendfrom django.contrib.sessions.middleware import SessionMiddlewarefrom django.core import mailfrom django.core.asgi import get_asgi_application@@ -903,6 +903,32 @@ class TestSessionBackendsRefuseInactiveUsers(APIBaseTest):assert backend.get_user(self.user.pk) is None+class TestLegacySessionBackends(APIBaseTest):+ @parameterized.expand(+ [+ ("saml", "ee.api.authentication.MultitenantSAMLAuth"),+ ("google", "ee.api.authentication.CustomGoogleOAuth2"),+ ]+ )+ def test_legacy_backend_restores_an_existing_session(self, _name: str, legacy_path: str) -> None:+ request = RequestFactory().get("/")+ SessionMiddleware(lambda r: HttpResponse()).process_request(request)+ request.session[SESSION_KEY] = str(self.user.pk)+ request.session[BACKEND_SESSION_KEY] = legacy_path+ request.session[HASH_SESSION_KEY] = self.user.get_session_auth_hash()++ assert get_user(request) == self.user++ @parameterized.expand(+ [+ ("saml", "ee.api.authentication.MultitenantSAMLAuth"),+ ("google", "ee.api.authentication.CustomGoogleOAuth2"),+ ]+ )+ def test_legacy_backend_refuses_new_authentication(self, _name: str, legacy_path: str) -> None:+ assert load_backend(legacy_path).authenticate(RequestFactory().get("/")) is None++class TestLogoutRedirect(APIBaseTest):"""Tests that /logout preserves a safe `next` param so users return to where they werediff --git a/posthog/constants.py b/posthog/constants.pyindex 9a938b06151d..105419c3b044 100644--- a/posthog/constants.py+++ b/posthog/constants.py@@ -301,7 +301,12 @@ LOGIN_METHODS = [{"key": "google-oauth2","display": "Google OAuth",- "backends": ["google-oauth2", "ee.api.authentication.CustomGoogleOAuth2"],+ # The `ee.` path is only carried by sessions issued before the move to `posthog.api.sso`.+ "backends": [+ "google-oauth2",+ "posthog.api.sso.CustomGoogleOAuth2",+ "ee.api.authentication.CustomGoogleOAuth2",+ ],},{"key": "github",@@ -316,7 +321,8 @@ LOGIN_METHODS = [{"key": "saml","display": "SAML",- "backends": ["saml", "ee.api.authentication.MultitenantSAMLAuth"],+ # The `ee.` path is only carried by sessions issued before the move to `posthog.api.sso`.+ "backends": ["saml", "posthog.api.sso.MultitenantSAMLAuth", "ee.api.authentication.MultitenantSAMLAuth"],},{"key": "oidc",diff --git a/posthog/settings/web.py b/posthog/settings/web.pyindex 8319a0009b4a..36a9c51c3495 100644--- a/posthog/settings/web.py+++ b/posthog/settings/web.py@@ -325,7 +325,7 @@ SOCIAL_AUTH_PIPELINE = ("social_core.pipeline.social_auth.social_details","social_core.pipeline.social_auth.social_uid","social_core.pipeline.social_auth.auth_allowed",- "ee.api.authentication.social_auth_allowed",+ "posthog.api.sso.social_auth_allowed","social_core.pipeline.social_auth.social_user",# Must stay ahead of association/provisioning so a mismatched authenticated identity is rejected first"posthog.api.authentication.social_identity_matches_session",diff --git a/posthog/test/repo_invariants/test_startup_import_budget.py b/posthog/test/repo_invariants/test_startup_import_budget.pyindex 5f12fda15708..cd9ab161db6f 100644--- a/posthog/test/repo_invariants/test_startup_import_budget.py+++ b/posthog/test/repo_invariants/test_startup_import_budget.py@@ -46,7 +46,7 @@ FORBIDDEN_AT_SETUP = ["posthog.api.services.query", # API query service — viewset-request-time only"products.signals.backend.tasks", # celery task module — workers load it by autodiscovery; at setup it drags the signals and tasks contracts in"products.signals.backend.scout_harness.suggestions", # reaches the tasks facade contracts (pydantic DTOs) — deferred in the signals receivers; the facade itself stays importable- "ee.vercel.integration", # reaches ee.api.authentication (@api_view -> DRF schema class) — receivers live in ee.vercel.receivers and import it at call time+ "ee.vercel.integration", # receiver modules import the integration when the receiver fires, not during Django setup"posthog.api.documentation", # drf_spectacular schema hooks — request-time only"django.test", # test client — was dragged in by drf_spectacular.plumbing via rest_framework.test"zxcvbn", # password strength, needed only by posthog.auth (deferred in posthog.helpers.impersonation and posthog.event_usage)