@@ -167,12 +167,16 @@ def test_login_evil_redirect(self):
167
167
idp_hosts = ['idp.example.com' ],
168
168
metadata_file = 'remote_metadata_one_idp.xml' ,
169
169
)
170
- response = self .client .get (
171
- reverse ('saml2_login' ) + '?next=http://evil.com' )
172
- url = urlparse (response ['Location' ])
173
- params = parse_qs (url .query )
174
170
175
- self .assertEqual (params ['RelayState' ], [settings .LOGIN_REDIRECT_URL , ])
171
+ for redirect_url in ['/dashboard/' , 'testprofiles:dashboard' ]:
172
+ with self .subTest (LOGIN_REDIRECT_URL = redirect_url ):
173
+ with override_settings (LOGIN_REDIRECT_URL = redirect_url ):
174
+ response = self .client .get (
175
+ reverse ('saml2_login' ) + '?next=http://evil.com' )
176
+ url = urlparse (response ['Location' ])
177
+ params = parse_qs (url .query )
178
+
179
+ self .assertEqual (params ['RelayState' ], ['/dashboard/' ])
176
180
177
181
def test_no_redirect (self ):
178
182
"""
@@ -186,11 +190,30 @@ def test_no_redirect(self):
186
190
idp_hosts = ['idp.example.com' ],
187
191
metadata_file = 'remote_metadata_one_idp.xml' ,
188
192
)
189
- response = self .client .get (reverse ('saml2_login' ) + '?next=' )
190
- url = urlparse (response ['Location' ])
191
- params = parse_qs (url .query )
192
193
193
- self .assertEqual (params ['RelayState' ], [settings .LOGIN_REDIRECT_URL , ])
194
+ for redirect_url in ['/dashboard/' , 'testprofiles:dashboard' ]:
195
+ with self .subTest (LOGIN_REDIRECT_URL = redirect_url ):
196
+ with override_settings (LOGIN_REDIRECT_URL = redirect_url ):
197
+ response = self .client .get (reverse ('saml2_login' ) + '?next=' )
198
+ url = urlparse (response ['Location' ])
199
+ params = parse_qs (url .query )
200
+
201
+ self .assertEqual (params ['RelayState' ], ['/dashboard/' ])
202
+
203
+ @override_settings (SAML_IGNORE_AUTHENTICATED_USERS_ON_LOGIN = True )
204
+ def test_login_already_logged (self ):
205
+ self .client .force_login (User .objects .create (username = 'user' , password = 'pass' ))
206
+
207
+ for redirect_url in ['/dashboard/' , 'testprofiles:dashboard' ]:
208
+ with self .subTest (LOGIN_REDIRECT_URL = redirect_url ):
209
+ with override_settings (LOGIN_REDIRECT_URL = redirect_url ):
210
+ with self .subTest ('no next url' ):
211
+ response = self .client .get (reverse ('saml2_login' ))
212
+ self .assertRedirects (response , '/dashboard/' )
213
+
214
+ with self .subTest ('evil next url' ):
215
+ response = self .client .get (reverse ('saml2_login' ) + '?next=http://evil.com' )
216
+ self .assertRedirects (response , '/dashboard/' )
194
217
195
218
def test_unknown_idp (self ):
196
219
# monkey patch SAML configuration
@@ -277,6 +300,7 @@ def test_login_several_idps(self):
277
300
self .assertIn ('AuthnRequest xmlns' , decode_base64_and_inflate (
278
301
saml_request ).decode ('utf-8' ))
279
302
303
+ @override_settings (LOGIN_REDIRECT_URL = 'testprofiles:dashboard' )
280
304
def test_assertion_consumer_service (self ):
281
305
# Get initial number of users
282
306
initial_user_count = User .objects .count ()
@@ -325,14 +349,36 @@ def test_assertion_consumer_service(self):
325
349
'SAMLResponse' : self .b64_for_post (saml_response ),
326
350
'RelayState' : came_from ,
327
351
})
328
- self .assertEqual (response .status_code , 302 )
329
- location = response ['Location' ]
330
352
331
- url = urlparse (location )
332
353
# as the RelayState is empty we have redirect to LOGIN_REDIRECT_URL
333
- self .assertEqual ( url . path , settings . LOGIN_REDIRECT_URL )
354
+ self .assertRedirects ( response , '/dashboard/' )
334
355
self .assertEqual (force_text (new_user .id ), client .session [SESSION_KEY ])
335
356
357
+ @override_settings (LOGIN_REDIRECT_URL = 'testprofiles:dashboard' )
358
+ def test_assertion_consumer_service_default_relay_state (self ):
359
+ settings .SAML_CONFIG = conf .create_conf (
360
+ sp_host = 'sp.example.com' ,
361
+ idp_hosts = ['idp.example.com' ],
362
+ metadata_file = 'remote_metadata_one_idp.xml' ,
363
+ )
364
+
365
+ new_user = User .objects .create (username = 'teacher' , password = 'not-used' )
366
+
367
+ response = self .client .get (reverse ('saml2_login' ))
368
+ saml2_req = saml2_from_httpredirect_request (response .url )
369
+ session_id = get_session_id_from_saml2 (saml2_req )
370
+
371
+ saml_response = auth_response (session_id , 'teacher' )
372
+ self .add_outstanding_query (session_id , '/' )
373
+ response = self .client .post (reverse ('saml2_acs' ), {
374
+ 'SAMLResponse' : self .b64_for_post (saml_response ),
375
+ })
376
+ self .assertEqual (response .status_code , 302 )
377
+
378
+ # The RelayState is missing, redirect to LOGIN_REDIRECT_URL
379
+ self .assertRedirects (response , '/dashboard/' )
380
+ self .assertEqual (force_text (new_user .id ), self .client .session [SESSION_KEY ])
381
+
336
382
def test_assertion_consumer_service_already_logged_in_allowed (self ):
337
383
self .client .force_login (User .objects .create (
338
384
username = 'user' , password = 'pass' ))
0 commit comments