: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(auth): redirect gracefully when SSO provider is unreachable (#67235)
posthog[bot]5629338611fe
diff --git a/ee/api/test/test_authentication.py b/ee/api/test/test_authentication.pyindex 0b89a6ee4ea1..ec8055b731b7 100644--- a/ee/api/test/test_authentication.py+++ b/ee/api/test/test_authentication.py@@ -2,7 +2,7 @@ import osimport jsonimport uuidimport datetime-from typing import cast+from typing import Any, castfrom urllib.parse import parse_qs, urlparseimport pytest@@ -16,7 +16,7 @@ from django.utils import timezonefrom parameterized import parameterizedfrom rest_framework import status-from social_core.exceptions import AuthFailed+from social_core.exceptions import AuthConnectionError, AuthFailed, AuthMissingParameterfrom social_django.models import UserSocialAuthfrom posthog.constants import AvailableFeature@@ -245,6 +245,21 @@ class TestEEAuthenticationAPI(APILicensedTest):second_key = self.client.session.session_keyself.assertNotEqual(first_key, second_key)+ @parameterized.expand(+ [+ ("auth_failed", AuthFailed(cast(Any, "google-oauth2"), "bad")),+ ("missing_parameter", AuthMissingParameter(cast(Any, "google-oauth2"), "code")),+ ("connection_error", AuthConnectionError(cast(Any, "google-oauth2"), "cert verify failed")),+ ]+ )+ def test_unreachable_or_misconfigured_sso_provider_redirects(self, _name, exception):+ # An unreachable IdP or one whose TLS cert fails during OIDC discovery raises AuthConnectionError,+ # a sibling of AuthFailed - it must redirect gracefully rather than surface as an unhandled 500.+ self.client.logout()+ with self.settings(**GOOGLE_MOCK_SETTINGS), patch("posthog.api.authentication.auth", side_effect=exception):+ response = self.client.get("/login/google-oauth2/")+ self.assertRedirects(response, "/login?error_code=improperly_configured_sso", fetch_redirect_response=False)+def test_existing_session_remains_valid_when_sso_enforced(self):"""Test that existing password-authenticated sessions remain valid after SSO is enforced"""self.client.logout()diff --git a/posthog/api/authentication.py b/posthog/api/authentication.pyindex a378bec210cc..247dbab2c72e 100644--- a/posthog/api/authentication.py+++ b/posthog/api/authentication.py@@ -34,7 +34,7 @@ from rest_framework.decorators import actionfrom rest_framework.exceptions import APIExceptionfrom rest_framework.request import Requestfrom rest_framework.response import Response-from social_core.exceptions import AuthFailed, AuthMissingParameter+from social_core.exceptions import AuthConnectionError, AuthFailed, AuthMissingParameterfrom social_django.strategy import DjangoStrategyfrom social_django.views import authfrom two_factor.utils import default_device@@ -143,7 +143,9 @@ def sso_login(request: HttpRequest, backend: str) -> HttpResponse:try:return auth(request, backend)- except (AuthFailed, AuthMissingParameter) as e:+ except (AuthFailed, AuthMissingParameter, AuthConnectionError) as e:+ # AuthConnectionError covers an unreachable IdP or a TLS cert that fails during OIDC discovery -+ # it's a sibling of AuthFailed (not a subclass), so it would otherwise surface as an unhandled 500.logger.warning("SSO login failed, redirecting to login page", exc_info=e)return redirect("/login?error_code=improperly_configured_sso")