/
auth_test.py
1168 lines (1024 loc) · 54.5 KB
/
auth_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
# Copyright (c) 2010-2018 OneLogin, Inc.
# MIT License
from base64 import b64decode, b64encode
import json
from os.path import dirname, join, exists
import unittest
from urlparse import urlparse, parse_qs
from onelogin.saml2.auth import OneLogin_Saml2_Auth
from onelogin.saml2.constants import OneLogin_Saml2_Constants
from onelogin.saml2.settings import OneLogin_Saml2_Settings
from onelogin.saml2.utils import OneLogin_Saml2_Utils
from onelogin.saml2.logout_request import OneLogin_Saml2_Logout_Request
from onelogin.saml2.errors import OneLogin_Saml2_Error
class OneLogin_Saml2_Auth_Test(unittest.TestCase):
data_path = join(dirname(dirname(dirname(dirname(__file__)))), 'data')
settings_path = join(dirname(dirname(dirname(dirname(__file__)))), 'settings')
def loadSettingsJSON(self, name='settings1.json'):
filename = join(self.settings_path, name)
if exists(filename):
stream = open(filename, 'r')
settings = json.load(stream)
stream.close()
return settings
else:
raise Exception('Settings json file does not exist')
def file_contents(self, filename):
f = open(filename, 'r')
content = f.read()
f.close()
return content
def get_request(self):
return {
'http_host': 'example.com',
'script_name': '/index.html',
'get_data': {}
}
def testGetSettings(self):
"""
Tests the get_settings method of the OneLogin_Saml2_Auth class
Build a OneLogin_Saml2_Settings object with a setting array
and compare the value returned from the method of the
auth object
"""
settings_info = self.loadSettingsJSON()
settings = OneLogin_Saml2_Settings(settings_info)
auth = OneLogin_Saml2_Auth(self.get_request(), old_settings=settings_info)
auth_settings = auth.get_settings()
self.assertEqual(settings.get_sp_data(), auth_settings.get_sp_data())
def testGetSSOurl(self):
"""
Tests the get_sso_url method of the OneLogin_Saml2_Auth class
"""
settings_info = self.loadSettingsJSON()
auth = OneLogin_Saml2_Auth(self.get_request(), old_settings=settings_info)
sso_url = settings_info['idp']['singleSignOnService']['url']
self.assertEqual(auth.get_sso_url(), sso_url)
def testGetSLOurl(self):
"""
Tests the get_slo_url method of the OneLogin_Saml2_Auth class
"""
settings_info = self.loadSettingsJSON()
auth = OneLogin_Saml2_Auth(self.get_request(), old_settings=settings_info)
slo_url = settings_info['idp']['singleLogoutService']['url']
self.assertEqual(auth.get_slo_url(), slo_url)
def testGetSessionIndex(self):
"""
Tests the get_session_index method of the OneLogin_Saml2_Auth class
"""
settings_info = self.loadSettingsJSON()
auth = OneLogin_Saml2_Auth(self.get_request(), old_settings=settings_info)
self.assertIsNone(auth.get_session_index())
request_data = self.get_request()
message = self.file_contents(join(self.data_path, 'responses', 'valid_response.xml.base64'))
del request_data['get_data']
request_data['post_data'] = {
'SAMLResponse': message
}
auth2 = OneLogin_Saml2_Auth(request_data, old_settings=self.loadSettingsJSON())
self.assertIsNone(auth2.get_session_index())
auth2.process_response()
self.assertEqual('_6273d77b8cde0c333ec79d22a9fa0003b9fe2d75cb', auth2.get_session_index())
def testGetSessionExpiration(self):
"""
Tests the get_session_expiration method of the OneLogin_Saml2_Auth class
"""
settings_info = self.loadSettingsJSON()
auth = OneLogin_Saml2_Auth(self.get_request(), old_settings=settings_info)
self.assertIsNone(auth.get_session_expiration())
request_data = self.get_request()
message = self.file_contents(join(self.data_path, 'responses', 'valid_response.xml.base64'))
del request_data['get_data']
request_data['post_data'] = {
'SAMLResponse': message
}
auth2 = OneLogin_Saml2_Auth(request_data, old_settings=self.loadSettingsJSON())
self.assertIsNone(auth2.get_session_expiration())
auth2.process_response()
self.assertEqual(2655106621, auth2.get_session_expiration())
def testGetLastErrorReason(self):
"""
Tests the get_last_error_reason method of the OneLogin_Saml2_Auth class
Case Invalid Response
"""
request_data = self.get_request()
message = self.file_contents(join(self.data_path, 'responses', 'response1.xml.base64'))
del request_data['get_data']
request_data['post_data'] = {
'SAMLResponse': message
}
auth = OneLogin_Saml2_Auth(request_data, old_settings=self.loadSettingsJSON())
auth.process_response()
self.assertEqual(auth.get_last_error_reason(), 'Signature validation failed. SAML Response rejected')
def testProcessNoResponse(self):
"""
Tests the process_response method of the OneLogin_Saml2_Auth class
Case No Response, An exception is throw
"""
auth = OneLogin_Saml2_Auth(self.get_request(), old_settings=self.loadSettingsJSON())
with self.assertRaisesRegexp(OneLogin_Saml2_Error, 'SAML Response not found'):
auth.process_response()
self.assertEqual(auth.get_errors(), ['invalid_binding'])
def testProcessResponseInvalid(self):
"""
Tests the process_response method of the OneLogin_Saml2_Auth class
Case Invalid Response, After processing the response the user
is not authenticated, attributes are notreturned, no nameID and
the error array is not empty, contains 'invalid_response
"""
request_data = self.get_request()
message = self.file_contents(join(self.data_path, 'responses', 'response1.xml.base64'))
del request_data['get_data']
request_data['post_data'] = {
'SAMLResponse': message
}
auth = OneLogin_Saml2_Auth(request_data, old_settings=self.loadSettingsJSON())
auth.process_response()
self.assertFalse(auth.is_authenticated())
self.assertEqual(len(auth.get_attributes()), 0)
self.assertEqual(auth.get_nameid(), None)
self.assertEqual(auth.get_attribute('uid'), None)
self.assertEqual(auth.get_errors(), ['invalid_response'])
def testProcessResponseInvalidRequestId(self):
"""
Tests the process_response method of the OneLogin_Saml2_Auth class
Case Invalid Response, Invalid requestID
"""
request_data = self.get_request()
message = self.file_contents(join(self.data_path, 'responses', 'unsigned_response.xml.base64'))
plain_message = b64decode(message)
current_url = OneLogin_Saml2_Utils.get_self_url_no_query(request_data)
plain_message = plain_message.replace('http://stuff.com/endpoints/endpoints/acs.php', current_url)
del request_data['get_data']
request_data['post_data'] = {
'SAMLResponse': b64encode(plain_message)
}
auth = OneLogin_Saml2_Auth(request_data, old_settings=self.loadSettingsJSON())
request_id = 'invalid'
auth.process_response(request_id)
self.assertEqual('No Signature found. SAML Response rejected', auth.get_last_error_reason())
auth.set_strict(True)
auth.process_response(request_id)
self.assertEqual(auth.get_errors(), ['invalid_response'])
self.assertEqual('The InResponseTo of the Response: _57bcbf70-7b1f-012e-c821-782bcb13bb38, does not match the ID of the AuthNRequest sent by the SP: invalid', auth.get_last_error_reason())
valid_request_id = '_57bcbf70-7b1f-012e-c821-782bcb13bb38'
auth.process_response(valid_request_id)
self.assertEqual('No Signature found. SAML Response rejected', auth.get_last_error_reason())
def testProcessResponseValid(self):
"""
Tests the process_response method of the OneLogin_Saml2_Auth class
Case Valid Response, After processing the response the user
is authenticated, attributes are returned, also has a nameID and
the error array is empty
"""
request_data = self.get_request()
message = self.file_contents(join(self.data_path, 'responses', 'valid_response.xml.base64'))
del request_data['get_data']
request_data['post_data'] = {
'SAMLResponse': message
}
auth = OneLogin_Saml2_Auth(request_data, old_settings=self.loadSettingsJSON())
auth.process_response()
self.assertTrue(auth.is_authenticated())
self.assertEqual(len(auth.get_errors()), 0)
self.assertEqual('492882615acf31c8096b627245d76ae53036c090', auth.get_nameid())
attributes = auth.get_attributes()
self.assertNotEqual(len(attributes), 0)
self.assertEqual(auth.get_attribute('mail'), attributes['mail'])
session_index = auth.get_session_index()
self.assertEqual('_6273d77b8cde0c333ec79d22a9fa0003b9fe2d75cb', session_index)
def testRedirectTo(self):
"""
Tests the redirect_to method of the OneLogin_Saml2_Auth class
(phpunit raises an exception when a redirect is executed, the
exception is catched and we check that the targetURL is correct)
Case redirect without url parameter
"""
request_data = self.get_request()
relay_state = 'http://sp.example.com'
request_data['get_data']['RelayState'] = relay_state
auth = OneLogin_Saml2_Auth(request_data, old_settings=self.loadSettingsJSON())
target_url = auth.redirect_to()
self.assertEqual(target_url, relay_state)
def testRedirectTowithUrl(self):
"""
Tests the redirect_to method of the OneLogin_Saml2_Auth class
(phpunit raises an exception when a redirect is executed, the
exception is catched and we check that the targetURL is correct)
Case redirect with url parameter
"""
request_data = self.get_request()
relay_state = 'http://sp.example.com'
url_2 = 'http://sp2.example.com'
request_data['get_data']['RelayState'] = relay_state
auth = OneLogin_Saml2_Auth(request_data, old_settings=self.loadSettingsJSON())
target_url = auth.redirect_to(url_2)
self.assertEqual(target_url, url_2)
def testProcessNoSLO(self):
"""
Tests the process_slo method of the OneLogin_Saml2_Auth class
Case No Message, An exception is throw
"""
auth = OneLogin_Saml2_Auth(self.get_request(), old_settings=self.loadSettingsJSON())
with self.assertRaisesRegexp(OneLogin_Saml2_Error, 'SAML LogoutRequest/LogoutResponse not found'):
auth.process_slo(True)
def testProcessSLOResponseInvalid(self):
"""
Tests the process_slo method of the OneLogin_Saml2_Auth class
Case Invalid Logout Response
"""
request_data = self.get_request()
message = self.file_contents(join(self.data_path, 'logout_responses', 'logout_response_deflated.xml.base64'))
request_data['get_data']['SAMLResponse'] = message
auth = OneLogin_Saml2_Auth(request_data, old_settings=self.loadSettingsJSON())
auth.process_slo(True)
self.assertEqual(len(auth.get_errors()), 0)
auth.set_strict(True)
auth.process_slo(True)
# The Destination fails
self.assertEqual(auth.get_errors(), ['invalid_logout_response'])
auth.set_strict(False)
auth.process_slo(True)
self.assertEqual(len(auth.get_errors()), 0)
def testProcessSLOResponseNoSucess(self):
"""
Tests the process_slo method of the OneLogin_Saml2_Auth class
Case Logout Response not sucess
"""
request_data = self.get_request()
message = self.file_contents(join(self.data_path, 'logout_responses', 'invalids', 'status_code_responder.xml.base64'))
# In order to avoid the destination problem
plain_message = OneLogin_Saml2_Utils.decode_base64_and_inflate(message)
current_url = OneLogin_Saml2_Utils.get_self_url_no_query(request_data)
plain_message = plain_message.replace('http://stuff.com/endpoints/endpoints/sls.php', current_url)
message = OneLogin_Saml2_Utils.deflate_and_base64_encode(plain_message)
request_data['get_data']['SAMLResponse'] = message
auth = OneLogin_Saml2_Auth(request_data, old_settings=self.loadSettingsJSON())
auth.set_strict(True)
auth.process_slo(True)
self.assertEqual(auth.get_errors(), ['logout_not_success'])
def testProcessSLOResponseRequestId(self):
"""
Tests the process_slo method of the OneLogin_Saml2_Auth class
Case Logout Response with valid and invalid Request ID
"""
request_data = self.get_request()
message = self.file_contents(join(self.data_path, 'logout_responses', 'logout_response_deflated.xml.base64'))
# In order to avoid the destination problem
plain_message = OneLogin_Saml2_Utils.decode_base64_and_inflate(message)
current_url = OneLogin_Saml2_Utils.get_self_url_no_query(request_data)
plain_message = plain_message.replace('http://stuff.com/endpoints/endpoints/sls.php', current_url)
message = OneLogin_Saml2_Utils.deflate_and_base64_encode(plain_message)
request_data['get_data']['SAMLResponse'] = message
auth = OneLogin_Saml2_Auth(request_data, old_settings=self.loadSettingsJSON())
request_id = 'wrongID'
auth.set_strict(True)
auth.process_slo(True, request_id)
self.assertEqual(auth.get_errors(), ['invalid_logout_response'])
request_id = 'ONELOGIN_21584ccdfaca36a145ae990442dcd96bfe60151e'
auth.process_slo(True, request_id)
self.assertEqual(len(auth.get_errors()), 0)
def testProcessSLOResponseValid(self):
"""
Tests the process_slo method of the OneLogin_Saml2_Auth class
Case Valid Logout Response
"""
request_data = self.get_request()
message = self.file_contents(join(self.data_path, 'logout_responses', 'logout_response_deflated.xml.base64'))
# In order to avoid the destination problem
plain_message = OneLogin_Saml2_Utils.decode_base64_and_inflate(message)
current_url = OneLogin_Saml2_Utils.get_self_url_no_query(request_data)
plain_message = plain_message.replace('http://stuff.com/endpoints/endpoints/sls.php', current_url)
message = OneLogin_Saml2_Utils.deflate_and_base64_encode(plain_message)
request_data['get_data']['SAMLResponse'] = message
auth = OneLogin_Saml2_Auth(request_data, old_settings=self.loadSettingsJSON())
# FIXME
# if (!isset($_SESSION)) {
# $_SESSION = array();
# }
# $_SESSION['samltest'] = true;
auth.set_strict(True)
auth.process_slo(True)
self.assertEqual(len(auth.get_errors()), 0)
# FIXME
# // Session keep alive
# $this->assertTrue(isset($_SESSION['samltest']));
# $this->assertTrue($_SESSION['samltest']);
def testProcessSLOResponseValidDeletingSession(self):
"""
Tests the process_slo method of the OneLogin_Saml2_Auth class
Case Valid Logout Response, validating deleting the local session
"""
request_data = self.get_request()
message = self.file_contents(join(self.data_path, 'logout_responses', 'logout_response_deflated.xml.base64'))
# FIXME
# if (!isset($_SESSION)) {
# $_SESSION = array();
# }
# $_SESSION['samltest'] = true;
# In order to avoid the destination problem
plain_message = OneLogin_Saml2_Utils.decode_base64_and_inflate(message)
current_url = OneLogin_Saml2_Utils.get_self_url_no_query(request_data)
plain_message = plain_message.replace('http://stuff.com/endpoints/endpoints/sls.php', current_url)
message = OneLogin_Saml2_Utils.deflate_and_base64_encode(plain_message)
request_data['get_data']['SAMLResponse'] = message
auth = OneLogin_Saml2_Auth(request_data, old_settings=self.loadSettingsJSON())
auth.set_strict(True)
auth.process_slo(False)
self.assertEqual(len(auth.get_errors()), 0)
# FIXME
# $this->assertFalse(isset($_SESSION['samltest']));
def testProcessSLORequestInvalidValid(self):
"""
Tests the process_slo method of the OneLogin_Saml2_Auth class
Case Invalid Logout Request
"""
settings_info = self.loadSettingsJSON()
request_data = self.get_request()
message = self.file_contents(join(self.data_path, 'logout_requests', 'logout_request_deflated.xml.base64'))
request_data['get_data']['SAMLRequest'] = message
auth = OneLogin_Saml2_Auth(request_data, old_settings=settings_info)
target_url = auth.process_slo(True)
parsed_query = parse_qs(urlparse(target_url)[4])
self.assertEqual(len(auth.get_errors()), 0)
slo_url = settings_info['idp']['singleLogoutService']['url']
self.assertIn(slo_url, target_url)
self.assertIn('SAMLResponse', parsed_query)
# self.assertNotIn('RelayState', parsed_query)
auth.set_strict(True)
auth.process_slo(True)
# Fail due destination missmatch
self.assertEqual(auth.get_errors(), ['invalid_logout_request'])
auth.set_strict(False)
target_url_2 = auth.process_slo(True)
parsed_query_2 = parse_qs(urlparse(target_url_2)[4])
self.assertEqual(len(auth.get_errors()), 0)
slo_url = settings_info['idp']['singleLogoutService']['url']
self.assertIn(slo_url, target_url_2)
self.assertIn('SAMLResponse', parsed_query_2)
# self.assertNotIn('RelayState', parsed_query_2)
def testProcessSLORequestNotOnOrAfterFailed(self):
"""
Tests the process_slo method of the OneLogin_Saml2_Auth class
Case Logout Request NotOnOrAfter failed
"""
request_data = self.get_request()
message = self.file_contents(join(self.data_path, 'logout_requests', 'invalids', 'not_after_failed.xml.base64'))
# In order to avoid the destination problem
plain_message = OneLogin_Saml2_Utils.decode_base64_and_inflate(message)
current_url = OneLogin_Saml2_Utils.get_self_url_no_query(request_data)
plain_message = plain_message.replace('http://stuff.com/endpoints/endpoints/sls.php', current_url)
message = OneLogin_Saml2_Utils.deflate_and_base64_encode(plain_message)
request_data['get_data']['SAMLRequest'] = message
auth = OneLogin_Saml2_Auth(request_data, old_settings=self.loadSettingsJSON())
auth.set_strict(True)
auth.process_slo(True)
self.assertEqual(auth.get_errors(), ['invalid_logout_request'])
def testProcessSLORequestDeletingSession(self):
"""
Tests the process_slo method of the OneLogin_Saml2_Auth class
Case Valid Logout Request, validating that the local session is deleted,
a LogoutResponse is created and a redirection executed
"""
settings_info = self.loadSettingsJSON()
request_data = self.get_request()
message = self.file_contents(join(self.data_path, 'logout_requests', 'logout_request_deflated.xml.base64'))
# In order to avoid the destination problem
plain_message = OneLogin_Saml2_Utils.decode_base64_and_inflate(message)
current_url = OneLogin_Saml2_Utils.get_self_url_no_query(request_data)
plain_message = plain_message.replace('http://stuff.com/endpoints/endpoints/sls.php', current_url)
message = OneLogin_Saml2_Utils.deflate_and_base64_encode(plain_message)
request_data['get_data']['SAMLRequest'] = message
# FIXME
# if (!isset($_SESSION)) {
# $_SESSION = array();
# }
# $_SESSION['samltest'] = true;
auth = OneLogin_Saml2_Auth(request_data, old_settings=settings_info)
auth.set_strict(True)
target_url = auth.process_slo(True)
parsed_query = parse_qs(urlparse(target_url)[4])
slo_url = settings_info['idp']['singleLogoutService']['url']
self.assertIn(slo_url, target_url)
self.assertIn('SAMLResponse', parsed_query)
# self.assertNotIn('RelayState', parsed_query)
# FIXME // Session is not alive
# $this->assertFalse(isset($_SESSION['samltest']));
# $_SESSION['samltest'] = true;
auth.set_strict(True)
target_url_2 = auth.process_slo(True)
target_url_2 = auth.process_slo(True)
parsed_query_2 = parse_qs(urlparse(target_url_2)[4])
slo_url = settings_info['idp']['singleLogoutService']['url']
self.assertIn(slo_url, target_url_2)
self.assertIn('SAMLResponse', parsed_query_2)
# self.assertNotIn('RelayState', parsed_query_2)
# FIXME // Session is alive
# $this->assertTrue(isset($_SESSION['samltest']));
# $this->assertTrue($_SESSION['samltest']);
def testProcessSLORequestRelayState(self):
"""
Tests the process_slo method of the OneLogin_Saml2_Auth class
Case Valid Logout Request, validating the relayState,
a LogoutResponse is created and a redirection executed
"""
settings_info = self.loadSettingsJSON()
request_data = self.get_request()
message = self.file_contents(join(self.data_path, 'logout_requests', 'logout_request_deflated.xml.base64'))
# In order to avoid the destination problem
plain_message = OneLogin_Saml2_Utils.decode_base64_and_inflate(message)
current_url = OneLogin_Saml2_Utils.get_self_url_no_query(request_data)
plain_message = plain_message.replace('http://stuff.com/endpoints/endpoints/sls.php', current_url)
message = OneLogin_Saml2_Utils.deflate_and_base64_encode(plain_message)
request_data['get_data']['SAMLRequest'] = message
request_data['get_data']['RelayState'] = 'http://relaystate.com'
auth = OneLogin_Saml2_Auth(request_data, old_settings=settings_info)
auth.set_strict(True)
target_url = auth.process_slo(False)
parsed_query = parse_qs(urlparse(target_url)[4])
slo_url = settings_info['idp']['singleLogoutService']['url']
self.assertIn(slo_url, target_url)
self.assertIn('SAMLResponse', parsed_query)
self.assertIn('RelayState', parsed_query)
self.assertIn('http://relaystate.com', parsed_query['RelayState'])
def testProcessSLORequestSignedResponse(self):
"""
Tests the process_slo method of the OneLogin_Saml2_Auth class
Case Valid Logout Request, validating the relayState,
a signed LogoutResponse is created and a redirection executed
"""
settings_info = self.loadSettingsJSON()
settings_info['security']['logoutResponseSigned'] = True
request_data = self.get_request()
message = self.file_contents(join(self.data_path, 'logout_requests', 'logout_request_deflated.xml.base64'))
# In order to avoid the destination problem
plain_message = OneLogin_Saml2_Utils.decode_base64_and_inflate(message)
current_url = OneLogin_Saml2_Utils.get_self_url_no_query(request_data)
plain_message = plain_message.replace('http://stuff.com/endpoints/endpoints/sls.php', current_url)
message = OneLogin_Saml2_Utils.deflate_and_base64_encode(plain_message)
request_data['get_data']['SAMLRequest'] = message
request_data['get_data']['RelayState'] = 'http://relaystate.com'
auth = OneLogin_Saml2_Auth(request_data, old_settings=settings_info)
auth.set_strict(True)
target_url = auth.process_slo(False)
parsed_query = parse_qs(urlparse(target_url)[4])
slo_url = settings_info['idp']['singleLogoutService']['url']
self.assertIn(slo_url, target_url)
self.assertIn('SAMLResponse', parsed_query)
self.assertIn('RelayState', parsed_query)
self.assertIn('SigAlg', parsed_query)
self.assertIn('Signature', parsed_query)
self.assertIn('http://relaystate.com', parsed_query['RelayState'])
self.assertIn(OneLogin_Saml2_Constants.RSA_SHA1, parsed_query['SigAlg'])
def testLogin(self):
"""
Tests the login method of the OneLogin_Saml2_Auth class
Case Login with no parameters. An AuthnRequest is built an redirect executed
"""
settings_info = self.loadSettingsJSON()
request_data = self.get_request()
auth = OneLogin_Saml2_Auth(request_data, old_settings=settings_info)
target_url = auth.login()
parsed_query = parse_qs(urlparse(target_url)[4])
sso_url = settings_info['idp']['singleSignOnService']['url']
self.assertIn(sso_url, target_url)
self.assertIn('SAMLRequest', parsed_query)
self.assertIn('RelayState', parsed_query)
hostname = OneLogin_Saml2_Utils.get_self_host(request_data)
self.assertIn(u'http://%s/index.html' % hostname, parsed_query['RelayState'])
def testLoginWithUnicodeSettings(self):
"""
Tests the login method of the OneLogin_Saml2_Auth class
Case Login with unicode settings. An AuthnRequest is built an redirect executed
"""
settings_info = self.loadSettingsJSON('settings6.json')
request_data = self.get_request()
auth = OneLogin_Saml2_Auth(request_data, old_settings=settings_info)
target_url = auth.login()
parsed_query = parse_qs(urlparse(target_url)[4])
hostname = OneLogin_Saml2_Utils.get_self_host(request_data)
self.assertIn(u'http://%s/index.html' % hostname, parsed_query['RelayState'])
def testLoginWithRelayState(self):
"""
Tests the login method of the OneLogin_Saml2_Auth class
Case Login with relayState. An AuthnRequest is built with a the
RelayState in the assertion is built and redirect executed
"""
settings_info = self.loadSettingsJSON()
auth = OneLogin_Saml2_Auth(self.get_request(), old_settings=settings_info)
relay_state = 'http://sp.example.com'
target_url = auth.login(relay_state)
parsed_query = parse_qs(urlparse(target_url)[4])
sso_url = settings_info['idp']['singleSignOnService']['url']
self.assertIn(sso_url, target_url)
self.assertIn('SAMLRequest', parsed_query)
self.assertIn('RelayState', parsed_query)
self.assertIn(relay_state, parsed_query['RelayState'])
def testLoginSigned(self):
"""
Tests the login method of the OneLogin_Saml2_Auth class
Case Login signed. An AuthnRequest signed is built an redirect executed
"""
settings_info = self.loadSettingsJSON()
settings_info['security']['authnRequestsSigned'] = True
auth = OneLogin_Saml2_Auth(self.get_request(), old_settings=settings_info)
return_to = u'http://example.com/returnto'
target_url = auth.login(return_to)
parsed_query = parse_qs(urlparse(target_url)[4])
sso_url = settings_info['idp']['singleSignOnService']['url']
self.assertIn(sso_url, target_url)
self.assertIn('SAMLRequest', parsed_query)
self.assertIn('RelayState', parsed_query)
self.assertIn('SigAlg', parsed_query)
self.assertIn('Signature', parsed_query)
self.assertIn(return_to, parsed_query['RelayState'])
self.assertIn(OneLogin_Saml2_Constants.RSA_SHA1, parsed_query['SigAlg'])
def testLoginForceAuthN(self):
"""
Tests the login method of the OneLogin_Saml2_Auth class
Case Login with no parameters. A AuthN Request is built with ForceAuthn and redirect executed
"""
settings_info = self.loadSettingsJSON()
return_to = u'http://example.com/returnto'
sso_url = settings_info['idp']['singleSignOnService']['url']
auth = OneLogin_Saml2_Auth(self.get_request(), old_settings=settings_info)
target_url = auth.login(return_to)
parsed_query = parse_qs(urlparse(target_url)[4])
sso_url = settings_info['idp']['singleSignOnService']['url']
self.assertIn(sso_url, target_url)
self.assertIn('SAMLRequest', parsed_query)
request = OneLogin_Saml2_Utils.decode_base64_and_inflate(parsed_query['SAMLRequest'][0])
self.assertNotIn('ForceAuthn="true"', request)
auth_2 = OneLogin_Saml2_Auth(self.get_request(), old_settings=settings_info)
target_url_2 = auth_2.login(return_to, False, False)
parsed_query_2 = parse_qs(urlparse(target_url_2)[4])
self.assertIn(sso_url, target_url_2)
self.assertIn('SAMLRequest', parsed_query_2)
request_2 = OneLogin_Saml2_Utils.decode_base64_and_inflate(parsed_query_2['SAMLRequest'][0])
self.assertNotIn('ForceAuthn="true"', request_2)
auth_3 = OneLogin_Saml2_Auth(self.get_request(), old_settings=settings_info)
target_url_3 = auth_3.login(return_to, True, False)
parsed_query_3 = parse_qs(urlparse(target_url_3)[4])
self.assertIn(sso_url, target_url_3)
self.assertIn('SAMLRequest', parsed_query_3)
request_3 = OneLogin_Saml2_Utils.decode_base64_and_inflate(parsed_query_3['SAMLRequest'][0])
self.assertIn('ForceAuthn="true"', request_3)
def testLoginIsPassive(self):
"""
Tests the login method of the OneLogin_Saml2_Auth class
Case Login with no parameters. A AuthN Request is built with IsPassive and redirect executed
"""
settings_info = self.loadSettingsJSON()
return_to = u'http://example.com/returnto'
sso_url = settings_info['idp']['singleSignOnService']['url']
auth = OneLogin_Saml2_Auth(self.get_request(), old_settings=settings_info)
target_url = auth.login(return_to)
parsed_query = parse_qs(urlparse(target_url)[4])
sso_url = settings_info['idp']['singleSignOnService']['url']
self.assertIn(sso_url, target_url)
self.assertIn('SAMLRequest', parsed_query)
request = OneLogin_Saml2_Utils.decode_base64_and_inflate(parsed_query['SAMLRequest'][0])
self.assertNotIn('IsPassive="true"', request)
auth_2 = OneLogin_Saml2_Auth(self.get_request(), old_settings=settings_info)
target_url_2 = auth_2.login(return_to, False, False)
parsed_query_2 = parse_qs(urlparse(target_url_2)[4])
self.assertIn(sso_url, target_url_2)
self.assertIn('SAMLRequest', parsed_query_2)
request_2 = OneLogin_Saml2_Utils.decode_base64_and_inflate(parsed_query_2['SAMLRequest'][0])
self.assertNotIn('IsPassive="true"', request_2)
auth_3 = OneLogin_Saml2_Auth(self.get_request(), old_settings=settings_info)
target_url_3 = auth_3.login(return_to, False, True)
parsed_query_3 = parse_qs(urlparse(target_url_3)[4])
self.assertIn(sso_url, target_url_3)
self.assertIn('SAMLRequest', parsed_query_3)
request_3 = OneLogin_Saml2_Utils.decode_base64_and_inflate(parsed_query_3['SAMLRequest'][0])
self.assertIn('IsPassive="true"', request_3)
def testLoginSetNameIDPolicy(self):
"""
Tests the login method of the OneLogin_Saml2_Auth class
Case Logout with no parameters. A AuthN Request is built with and without NameIDPolicy
"""
settings_info = self.loadSettingsJSON()
return_to = u'http://example.com/returnto'
sso_url = settings_info['idp']['singleSignOnService']['url']
auth = OneLogin_Saml2_Auth(self.get_request(), old_settings=settings_info)
target_url = auth.login(return_to)
parsed_query = parse_qs(urlparse(target_url)[4])
sso_url = settings_info['idp']['singleSignOnService']['url']
self.assertIn(sso_url, target_url)
self.assertIn('SAMLRequest', parsed_query)
request = OneLogin_Saml2_Utils.decode_base64_and_inflate(parsed_query['SAMLRequest'][0])
self.assertIn('<samlp:NameIDPolicy', request)
auth_2 = OneLogin_Saml2_Auth(self.get_request(), old_settings=settings_info)
target_url_2 = auth_2.login(return_to, False, False, True)
parsed_query_2 = parse_qs(urlparse(target_url_2)[4])
self.assertIn(sso_url, target_url_2)
self.assertIn('SAMLRequest', parsed_query_2)
request_2 = OneLogin_Saml2_Utils.decode_base64_and_inflate(parsed_query_2['SAMLRequest'][0])
self.assertIn('<samlp:NameIDPolicy', request_2)
auth_3 = OneLogin_Saml2_Auth(self.get_request(), old_settings=settings_info)
target_url_3 = auth_3.login(return_to, False, False, False)
parsed_query_3 = parse_qs(urlparse(target_url_3)[4])
self.assertIn(sso_url, target_url_3)
self.assertIn('SAMLRequest', parsed_query_3)
request_3 = OneLogin_Saml2_Utils.decode_base64_and_inflate(parsed_query_3['SAMLRequest'][0])
self.assertNotIn('<samlp:NameIDPolicy', request_3)
def testLogout(self):
"""
Tests the logout method of the OneLogin_Saml2_Auth class
Case Logout with no parameters. A logout Request is built and redirect
executed
"""
settings_info = self.loadSettingsJSON()
request_data = self.get_request()
auth = OneLogin_Saml2_Auth(request_data, old_settings=settings_info)
target_url = auth.logout()
parsed_query = parse_qs(urlparse(target_url)[4])
slo_url = settings_info['idp']['singleLogoutService']['url']
self.assertIn(slo_url, target_url)
self.assertIn('SAMLRequest', parsed_query)
self.assertIn('RelayState', parsed_query)
hostname = OneLogin_Saml2_Utils.get_self_host(request_data)
self.assertIn(u'http://%s/index.html' % hostname, parsed_query['RelayState'])
def testLogoutWithRelayState(self):
"""
Tests the logout method of the OneLogin_Saml2_Auth class
Case Logout with relayState. A logout Request with a the RelayState in
the assertion is built and redirect executed
"""
settings_info = self.loadSettingsJSON()
auth = OneLogin_Saml2_Auth(self.get_request(), old_settings=settings_info)
relay_state = 'http://sp.example.com'
target_url = auth.logout(relay_state)
parsed_query = parse_qs(urlparse(target_url)[4])
slo_url = settings_info['idp']['singleLogoutService']['url']
self.assertIn(slo_url, target_url)
self.assertIn('SAMLRequest', parsed_query)
self.assertIn('RelayState', parsed_query)
self.assertIn(relay_state, parsed_query['RelayState'])
def testLogoutSigned(self):
"""
Tests the logout method of the OneLogin_Saml2_Auth class
Case Logout signed. A logout Request signed in
the assertion is built and redirect executed
"""
settings_info = self.loadSettingsJSON()
settings_info['security']['logoutRequestSigned'] = True
auth = OneLogin_Saml2_Auth(self.get_request(), old_settings=settings_info)
return_to = u'http://example.com/returnto'
target_url = auth.logout(return_to)
parsed_query = parse_qs(urlparse(target_url)[4])
slo_url = settings_info['idp']['singleLogoutService']['url']
self.assertIn(slo_url, target_url)
self.assertIn('SAMLRequest', parsed_query)
self.assertIn('RelayState', parsed_query)
self.assertIn('SigAlg', parsed_query)
self.assertIn('Signature', parsed_query)
self.assertIn(return_to, parsed_query['RelayState'])
self.assertIn(OneLogin_Saml2_Constants.RSA_SHA1, parsed_query['SigAlg'])
def testLogoutNoSLO(self):
"""
Tests the logout method of the OneLogin_Saml2_Auth class
Case IdP no SLO endpoint.
"""
settings_info = self.loadSettingsJSON()
del settings_info['idp']['singleLogoutService']
auth = OneLogin_Saml2_Auth(self.get_request(), old_settings=settings_info)
with self.assertRaisesRegexp(OneLogin_Saml2_Error, 'The IdP does not support Single Log Out'):
# The Header of the redirect produces an Exception
auth.logout('http://example.com/returnto')
def testLogoutNameIDandSessionIndex(self):
"""
Tests the logout method of the OneLogin_Saml2_Auth class
Case nameID and sessionIndex as parameters.
"""
settings_info = self.loadSettingsJSON()
request_data = self.get_request()
auth = OneLogin_Saml2_Auth(request_data, old_settings=settings_info)
name_id = 'name_id_example'
session_index = 'session_index_example'
target_url = auth.logout(name_id=name_id, session_index=session_index)
parsed_query = parse_qs(urlparse(target_url)[4])
slo_url = settings_info['idp']['singleLogoutService']['url']
self.assertIn(slo_url, target_url)
self.assertIn('SAMLRequest', parsed_query)
logout_request = OneLogin_Saml2_Utils.decode_base64_and_inflate(parsed_query['SAMLRequest'][0])
name_id_from_request = OneLogin_Saml2_Logout_Request.get_nameid(logout_request)
sessions_index_in_request = OneLogin_Saml2_Logout_Request.get_session_indexes(logout_request)
self.assertIn(session_index, sessions_index_in_request)
self.assertEqual(name_id, name_id_from_request)
def testLogoutNameID(self):
"""
Tests the logout method of the OneLogin_Saml2_Auth class
Case nameID loaded after process SAML Response
"""
request_data = self.get_request()
message = self.file_contents(join(self.data_path, 'responses', 'valid_response.xml.base64'))
del request_data['get_data']
request_data['post_data'] = {
'SAMLResponse': message
}
auth = OneLogin_Saml2_Auth(request_data, old_settings=self.loadSettingsJSON())
auth.process_response()
name_id_from_response = auth.get_nameid()
name_id_format_from_response = auth.get_nameid_format()
target_url = auth.logout()
parsed_query = parse_qs(urlparse(target_url)[4])
self.assertIn('SAMLRequest', parsed_query)
logout_request = OneLogin_Saml2_Utils.decode_base64_and_inflate(parsed_query['SAMLRequest'][0])
name_id_from_request = OneLogin_Saml2_Logout_Request.get_nameid(logout_request)
name_id_format_from_request = OneLogin_Saml2_Logout_Request.get_nameid_format(logout_request)
self.assertEqual(name_id_from_response, name_id_from_request)
self.assertEqual(name_id_format_from_response, name_id_format_from_request)
new_name_id = "new_name_id"
new_name_id_format = "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress"
target_url_2 = auth.logout(name_id=new_name_id, name_id_format=new_name_id_format)
parsed_query = parse_qs(urlparse(target_url_2)[4])
self.assertIn('SAMLRequest', parsed_query)
logout_request = OneLogin_Saml2_Utils.decode_base64_and_inflate(parsed_query['SAMLRequest'][0])
name_id_from_request = OneLogin_Saml2_Logout_Request.get_nameid(logout_request)
name_id_format_from_request = OneLogin_Saml2_Logout_Request.get_nameid_format(logout_request)
self.assertEqual(new_name_id, name_id_from_request)
self.assertEqual(new_name_id_format, name_id_format_from_request)
def testSetStrict(self):
"""
Tests the set_strict method of the OneLogin_Saml2_Auth
"""
settings_info = self.loadSettingsJSON()
settings_info['strict'] = False
auth = OneLogin_Saml2_Auth(self.get_request(), old_settings=settings_info)
settings = auth.get_settings()
self.assertFalse(settings.is_strict())
auth.set_strict(True)
settings = auth.get_settings()
self.assertTrue(settings.is_strict())
auth.set_strict(False)
settings = auth.get_settings()
self.assertFalse(settings.is_strict())
with self.assertRaises(AssertionError):
auth.set_strict('42')
def testIsAuthenticated(self):
"""
Tests the is_authenticated method of the OneLogin_Saml2_Auth
"""
request_data = self.get_request()
del request_data['get_data']
message = self.file_contents(join(self.data_path, 'responses', 'response1.xml.base64'))
request_data['post_data'] = {
'SAMLResponse': message
}
auth = OneLogin_Saml2_Auth(request_data, old_settings=self.loadSettingsJSON())
auth.process_response()
self.assertFalse(auth.is_authenticated())
message = self.file_contents(join(self.data_path, 'responses', 'valid_response.xml.base64'))
request_data['post_data'] = {
'SAMLResponse': message
}
auth = OneLogin_Saml2_Auth(request_data, old_settings=self.loadSettingsJSON())
auth.process_response()
self.assertTrue(auth.is_authenticated())
def testGetNameId(self):
"""
Tests the get_nameid method of the OneLogin_Saml2_Auth
"""
settings = self.loadSettingsJSON()
request_data = self.get_request()
del request_data['get_data']
message = self.file_contents(join(self.data_path, 'responses', 'response1.xml.base64'))
request_data['post_data'] = {
'SAMLResponse': message
}
auth = OneLogin_Saml2_Auth(request_data, old_settings=settings)
auth.process_response()
self.assertFalse(auth.is_authenticated())
self.assertEqual(auth.get_nameid(), None)
message = self.file_contents(join(self.data_path, 'responses', 'valid_response.xml.base64'))
request_data['post_data'] = {
'SAMLResponse': message
}
auth = OneLogin_Saml2_Auth(request_data, old_settings=settings)
auth.process_response()
self.assertTrue(auth.is_authenticated())
self.assertEqual("492882615acf31c8096b627245d76ae53036c090", auth.get_nameid())
settings_2 = self.loadSettingsJSON('settings2.json')
message = self.file_contents(join(self.data_path, 'responses', 'signed_message_encrypted_assertion2.xml.base64'))
request_data['post_data'] = {
'SAMLResponse': message
}
auth = OneLogin_Saml2_Auth(request_data, old_settings=settings_2)
auth.process_response()
self.assertTrue(auth.is_authenticated())
self.assertEqual("25ddd7d34a7d79db69167625cda56a320adf2876", auth.get_nameid())
def testGetNameIdFormat(self):
"""
Tests the get_nameid_format method of the OneLogin_Saml2_Auth
"""
settings = self.loadSettingsJSON()
request_data = self.get_request()
del request_data['get_data']
message = self.file_contents(join(self.data_path, 'responses', 'response1.xml.base64'))
request_data['post_data'] = {
'SAMLResponse': message
}
auth = OneLogin_Saml2_Auth(request_data, old_settings=settings)
auth.process_response()
self.assertFalse(auth.is_authenticated())
self.assertEqual(auth.get_nameid_format(), None)
message = self.file_contents(join(self.data_path, 'responses', 'valid_response.xml.base64'))
request_data['post_data'] = {
'SAMLResponse': message
}
auth = OneLogin_Saml2_Auth(request_data, old_settings=settings)
auth.process_response()
self.assertTrue(auth.is_authenticated())
self.assertEqual("urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress", auth.get_nameid_format())
settings_2 = self.loadSettingsJSON('settings2.json')
message = self.file_contents(join(self.data_path, 'responses', 'signed_message_encrypted_assertion2.xml.base64'))
request_data['post_data'] = {
'SAMLResponse': message
}
auth = OneLogin_Saml2_Auth(request_data, old_settings=settings_2)
auth.process_response()
self.assertTrue(auth.is_authenticated())
self.assertEqual("urn:oasis:names:tc:SAML:2.0:nameid-format:unspecified", auth.get_nameid_format())
def testBuildRequestSignature(self):
"""
Tests the build_request_signature method of the OneLogin_Saml2_Auth
"""
settings = self.loadSettingsJSON()
message = self.file_contents(join(self.data_path, 'logout_requests', 'logout_request_deflated.xml.base64'))
relay_state = 'http://relaystate.com'
auth = OneLogin_Saml2_Auth(self.get_request(), old_settings=settings)
signature = auth.build_request_signature(message, relay_state)
valid_signature = 'Pb1EXAX5TyipSJ1SndEKZstLQTsT+1D00IZAhEepBM+OkAZQSToivu3njgJu47HZiZAqgXZFgloBuuWE/+GdcSsRYEMkEkiSDWTpUr25zKYLJDSg6GNo6iAHsKSuFt46Z54Xe/keYxYP03Hdy97EwuuSjBzzgRc5tmpV+KC7+a0='
self.assertEqual(signature, valid_signature)
settings['sp']['privatekey'] = ''
settings['custom_base_path'] = u'invalid/path/'
auth2 = OneLogin_Saml2_Auth(self.get_request(), old_settings=settings)
with self.assertRaisesRegexp(OneLogin_Saml2_Error, "Trying to sign the SAMLRequest but can't load the SP private key"):
auth2.build_request_signature(message, relay_state)
def testBuildResponseSignature(self):
"""
Tests the build_response_signature method of the OneLogin_Saml2_Auth
"""
settings = self.loadSettingsJSON()
message = self.file_contents(join(self.data_path, 'logout_responses', 'logout_response_deflated.xml.base64'))
relay_state = 'http://relaystate.com'
auth = OneLogin_Saml2_Auth(self.get_request(), old_settings=settings)
signature = auth.build_response_signature(message, relay_state)
valid_signature = 'IcyWLRX6Dz3wHBfpcUaNLVDMGM3uo6z2Z11Gjq0/APPJaHboKGljffsgMVAGBml497yckq+eYKmmz+jpURV9yTj2sF9qfD6CwX2dEzSzMdRzB40X7pWyHgEJGIhs6BhaOt5oXEk4T+h3AczERqpVYFpL00yo7FNtyQkhZFpHFhM='
self.assertEqual(signature, valid_signature)
settings['sp']['privatekey'] = ''
settings['custom_base_path'] = u'invalid/path/'
auth2 = OneLogin_Saml2_Auth(self.get_request(), old_settings=settings)
with self.assertRaisesRegexp(OneLogin_Saml2_Error, "Trying to sign the SAMLResponse but can't load the SP private key"):