Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(sso): let an existing member sign in with a stale invite present (#115228)

Eleftheria Trivyzaki04026729201f
diff --git a/posthog/api/signup.py b/posthog/api/signup.py
index e3412767c3d4..d7fc258537ec 100644
--- a/posthog/api/signup.py
+++ b/posthog/api/signup.py
@@ -37,7 +37,15 @@ from posthog.exceptions_capture import capture_exception
from posthog.helpers.email_utils import EmailValidationHelper, reject_plus_addressed_email, validate_display_name
from posthog.helpers.oauth_pending_connection import read_pending_oauth_connection
from posthog.helpers.verified_domain_enforcement import resolve_login_organization
-from posthog.models import InviteExpiredException, Organization, OrganizationDomain, OrganizationInvite, Team, User
+from posthog.models import (
+ InviteExpiredException,
+ Organization,
+ OrganizationDomain,
+ OrganizationInvite,
+ OrganizationMembership,
+ Team,
+ User,
+)
from posthog.models.identity_provider_config import ConfigScope, IdentityProviderConfig
from posthog.models.organization_invite import INVITE_DAYS_VALIDITY
from posthog.models.webauthn_credential import WebauthnCredential
@@ -885,6 +893,13 @@ def lookup_invite_for_saml(email: str, saml_relay_state: str) -> Optional[Organi
config = IdentityProviderConfig.objects.filter(saml_relay_state=saml_relay_state).first()
if config is None:
return None
+ # An existing member cannot use an invite, because `OrganizationInvite.validate` rejects one for
+ # them, either as expired or as a duplicate membership. The caller raises that rejection inside
+ # the social-auth pipeline, which answers the login with a 500, so report no invite instead.
+ if OrganizationMembership.objects.filter(
+ organization_id=config.organization_id, user__email__iexact=email
+ ).exists():
+ return None
return (
OrganizationInvite.objects.filter(target_email__iexact=email, organization_id=config.organization_id)
.order_by("-created_at")
diff --git a/posthog/api/test/test_signup.py b/posthog/api/test/test_signup.py
index aab4a926217d..61870aa3eb26 100644
--- a/posthog/api/test/test_signup.py
+++ b/posthog/api/test/test_signup.py
@@ -3584,3 +3584,16 @@ class TestSAMLInviteLookup(APIBaseTest):
def test_unknown_identifier_returns_nothing(self):
assert lookup_invite_for_saml("[email protected]", "not-an-identifier") is None
+
+ @parameterized.expand([("expired", INVITE_DAYS_VALIDITY + 5), ("still_valid", 0)])
+ def test_reports_no_invite_for_someone_who_is_already_a_member(self, _name, days_old):
+ identifier = self._saml_identifier_for("member.example.com")
+ member = User.objects.create_and_join(
+ organization=self.organization, email="[email protected]", password=None
+ )
+ invite = OrganizationInvite.objects.create(organization=self.organization, target_email=member.email)
+ if days_old:
+ invite.created_at = timezone.now() - timedelta(days=days_old)
+ invite.save(update_fields=["created_at"])
+
+ assert lookup_invite_for_saml(member.email, identifier) is None