@@ -160,6 +160,14 @@ def login_and_registration_form(request, initial_mode="login"):
160160 # Retrieve the form descriptions from the user API
161161 form_descriptions = _get_form_descriptions (request )
162162
163+ # Detect a running TPA pipeline early so it can guard against redirect loops below.
164+ saml_provider = False
165+ running_pipeline = pipeline .get (request )
166+ if running_pipeline :
167+ saml_provider , __ = third_party_auth .utils .is_saml_provider (
168+ running_pipeline .get ('backend' ), running_pipeline .get ('kwargs' )
169+ )
170+
163171 # Our ?next= URL may itself contain a parameter 'tpa_hint=x' that we need to check.
164172 # If present, we display a login page focused on third-party auth with that provider.
165173 third_party_auth_hint = None
@@ -171,9 +179,10 @@ def login_and_registration_form(request, initial_mode="login"):
171179 provider_id = next_args ['tpa_hint' ][0 ]
172180 tpa_hint_provider = third_party_auth .provider .Registry .get (provider_id = provider_id )
173181 if tpa_hint_provider :
174- if tpa_hint_provider .skip_hinted_login_dialog :
182+ if tpa_hint_provider .skip_hinted_login_dialog and not running_pipeline :
175183 # Forward the user directly to the provider's login URL when the provider is configured
176- # to skip the dialog.
184+ # to skip the dialog. Do not redirect if a TPA pipeline is already running, as that
185+ # would cause an infinite loop (e.g. new SAML users dispatched back to /login).
177186 if initial_mode == "register" :
178187 auth_entry = pipeline .AUTH_ENTRY_REGISTER
179188 else :
@@ -193,12 +202,6 @@ def login_and_registration_form(request, initial_mode="login"):
193202 # tpa_hint_provider is not available
194203 # AND
195204 # user is not coming from a SAML IDP.
196- saml_provider = False
197- running_pipeline = pipeline .get (request )
198- if running_pipeline :
199- saml_provider , __ = third_party_auth .utils .is_saml_provider (
200- running_pipeline .get ('backend' ), running_pipeline .get ('kwargs' )
201- )
202205
203206 enterprise_customer = enterprise_customer_for_request (request )
204207
0 commit comments