@@ -88,6 +88,7 @@ def testReturnNameId(self):
8888 Tests the get_nameid method of the OneLogin_Saml2_Response
8989 """
9090 json_settings = self .loadSettingsJSON ()
91+ json_settings ['strict' ] = False
9192 settings = OneLogin_Saml2_Settings (json_settings )
9293 xml = self .file_contents (join (self .data_path , 'responses' , 'response1.xml.base64' ))
9394 response = OneLogin_Saml2_Response (settings , xml )
@@ -101,50 +102,98 @@ def testReturnNameId(self):
101102 response_3 = OneLogin_Saml2_Response (settings , xml_3 )
102103 self .assertEqual ('_68392312d490db6d355555cfbbd8ec95d746516f60' , response_3 .get_nameid ())
103104
105+ json_settings ['strict' ] = True
106+ json_settings ['security' ]['wantNameId' ] = True
107+ settings = OneLogin_Saml2_Settings (json_settings )
108+
104109 xml_4 = self .file_contents (join (self .data_path , 'responses' , 'invalids' , 'no_nameid.xml.base64' ))
105110 response_4 = OneLogin_Saml2_Response (settings , xml_4 )
106111 with self .assertRaisesRegex (Exception , 'NameID not found in the assertion of the Response' ):
107112 response_4 .get_nameid ()
108113
109- json_settings ['security' ]['wantNameId' ] = True
114+ json_settings ['security' ]['wantNameId' ] = False
110115 settings = OneLogin_Saml2_Settings (json_settings )
111-
112116 response_5 = OneLogin_Saml2_Response (settings , xml_4 )
113- with self .assertRaisesRegex (Exception , 'NameID not found in the assertion of the Response' ):
114- response_5 .get_nameid ()
117+ self .assertIsNone (response_5 .get_nameid ())
115118
119+ json_settings ['strict' ] = False
116120 json_settings ['security' ]['wantNameId' ] = False
117121 settings = OneLogin_Saml2_Settings (json_settings )
118-
119122 response_6 = OneLogin_Saml2_Response (settings , xml_4 )
120- nameid_6 = response_6 .get_nameid ()
121- self .assertIsNone (nameid_6 )
123+ self .assertIsNone (response_6 .get_nameid ())
124+
125+ json_settings ['security' ]['wantNameId' ] = True
126+ settings = OneLogin_Saml2_Settings (json_settings )
127+ response_7 = OneLogin_Saml2_Response (settings , xml_4 )
128+ self .assertIsNone (response_7 .get_nameid ())
122129
123130 del json_settings ['security' ]['wantNameId' ]
124131 settings = OneLogin_Saml2_Settings (json_settings )
132+ response_8 = OneLogin_Saml2_Response (settings , xml_4 )
133+ self .assertIsNone (response_8 .get_nameid ())
125134
126- response_7 = OneLogin_Saml2_Response (settings , xml_4 )
135+ json_settings ['strict' ] = True
136+ settings = OneLogin_Saml2_Settings (json_settings )
137+ response_9 = OneLogin_Saml2_Response (settings , xml_4 )
127138 with self .assertRaisesRegex (Exception , 'NameID not found in the assertion of the Response' ):
128- response_7 .get_nameid ()
139+ response_9 .get_nameid ()
140+
141+ json_settings ['strict' ] = False
142+ settings = OneLogin_Saml2_Settings (json_settings )
143+ xml_5 = self .file_contents (join (self .data_path , 'responses' , 'invalids' , 'wrong_spnamequalifier.xml.base64' ))
144+ response_10 = OneLogin_Saml2_Response (settings , xml_5 )
145+ self .assertEqual ('test@example.com' , response_10 .get_nameid ())
129146
130147 json_settings ['strict' ] = True
131148 settings = OneLogin_Saml2_Settings (json_settings )
132149
133150 xml_5 = self .file_contents (join (self .data_path , 'responses' , 'invalids' , 'wrong_spnamequalifier.xml.base64' ))
134- response_8 = OneLogin_Saml2_Response (settings , xml_5 )
151+ response_11 = OneLogin_Saml2_Response (settings , xml_5 )
135152 with self .assertRaisesRegex (Exception , 'The SPNameQualifier value mistmatch the SP entityID value.' ):
136- response_8 .get_nameid ()
153+ response_11 .get_nameid ()
154+
155+ json_settings ['strict' ] = True
156+ json_settings ['security' ]['wantNameId' ] = True
157+ settings = OneLogin_Saml2_Settings (json_settings )
137158
138159 xml_6 = self .file_contents (join (self .data_path , 'responses' , 'invalids' , 'empty_nameid.xml.base64' ))
139- response_9 = OneLogin_Saml2_Response (settings , xml_6 )
160+ response_12 = OneLogin_Saml2_Response (settings , xml_6 )
140161 with self .assertRaisesRegex (Exception , 'An empty NameID value found' ):
141- response_9 .get_nameid ()
162+ response_12 .get_nameid ()
163+
164+ json_settings ['security' ]['wantNameId' ] = False
165+ settings = OneLogin_Saml2_Settings (json_settings )
166+ response_13 = OneLogin_Saml2_Response (settings , xml_6 )
167+ self .assertIsNone (response_13 .get_nameid ())
168+
169+ json_settings ['strict' ] = False
170+ json_settings ['security' ]['wantNameId' ] = False
171+ settings = OneLogin_Saml2_Settings (json_settings )
172+ response_14 = OneLogin_Saml2_Response (settings , xml_6 )
173+ self .assertIsNone (response_14 .get_nameid ())
174+
175+ json_settings ['security' ]['wantNameId' ] = True
176+ settings = OneLogin_Saml2_Settings (json_settings )
177+ response_15 = OneLogin_Saml2_Response (settings , xml_6 )
178+ self .assertIsNone (response_15 .get_nameid ())
179+
180+ del json_settings ['security' ]['wantNameId' ]
181+ settings = OneLogin_Saml2_Settings (json_settings )
182+ response_16 = OneLogin_Saml2_Response (settings , xml_6 )
183+ self .assertIsNone (response_16 .get_nameid ())
184+
185+ json_settings ['strict' ] = True
186+ settings = OneLogin_Saml2_Settings (json_settings )
187+ response_17 = OneLogin_Saml2_Response (settings , xml_6 )
188+ with self .assertRaisesRegex (Exception , 'An empty NameID value found' ):
189+ response_17 .get_nameid ()
142190
143191 def testReturnNameIdFormat (self ):
144192 """
145193 Tests the get_nameid_format method of the OneLogin_Saml2_Response
146194 """
147195 json_settings = self .loadSettingsJSON ()
196+ json_settings ['strict' ] = False
148197 settings = OneLogin_Saml2_Settings (json_settings )
149198 xml = self .file_contents (join (self .data_path , 'responses' , 'response1.xml.base64' ))
150199 response = OneLogin_Saml2_Response (settings , xml )
@@ -158,50 +207,98 @@ def testReturnNameIdFormat(self):
158207 response_3 = OneLogin_Saml2_Response (settings , xml_3 )
159208 self .assertEqual ('urn:oasis:names:tc:SAML:2.0:nameid-format:transient' , response_3 .get_nameid_format ())
160209
210+ json_settings ['strict' ] = True
211+ json_settings ['security' ]['wantNameId' ] = True
212+ settings = OneLogin_Saml2_Settings (json_settings )
213+
161214 xml_4 = self .file_contents (join (self .data_path , 'responses' , 'invalids' , 'no_nameid.xml.base64' ))
162215 response_4 = OneLogin_Saml2_Response (settings , xml_4 )
163216 with self .assertRaisesRegex (Exception , 'NameID not found in the assertion of the Response' ):
164- response_4 .get_nameid ()
217+ response_4 .get_nameid_format ()
165218
166- json_settings ['security' ]['wantNameId' ] = True
219+ json_settings ['security' ]['wantNameId' ] = False
167220 settings = OneLogin_Saml2_Settings (json_settings )
168-
169221 response_5 = OneLogin_Saml2_Response (settings , xml_4 )
170- with self .assertRaisesRegex (Exception , 'NameID not found in the assertion of the Response' ):
171- response_5 .get_nameid ()
222+ self .assertIsNone (response_5 .get_nameid_format ())
172223
224+ json_settings ['strict' ] = False
173225 json_settings ['security' ]['wantNameId' ] = False
174226 settings = OneLogin_Saml2_Settings (json_settings )
175-
176227 response_6 = OneLogin_Saml2_Response (settings , xml_4 )
177- nameid_6 = response_6 .get_nameid ()
178- self .assertIsNone (nameid_6 )
228+ self .assertIsNone (response_6 .get_nameid_format ())
229+
230+ json_settings ['security' ]['wantNameId' ] = True
231+ settings = OneLogin_Saml2_Settings (json_settings )
232+ response_7 = OneLogin_Saml2_Response (settings , xml_4 )
233+ self .assertIsNone (response_7 .get_nameid_format ())
179234
180235 del json_settings ['security' ]['wantNameId' ]
181236 settings = OneLogin_Saml2_Settings (json_settings )
237+ response_8 = OneLogin_Saml2_Response (settings , xml_4 )
238+ self .assertIsNone (response_8 .get_nameid_format ())
182239
183- response_7 = OneLogin_Saml2_Response (settings , xml_4 )
240+ json_settings ['strict' ] = True
241+ settings = OneLogin_Saml2_Settings (json_settings )
242+ response_9 = OneLogin_Saml2_Response (settings , xml_4 )
184243 with self .assertRaisesRegex (Exception , 'NameID not found in the assertion of the Response' ):
185- response_7 .get_nameid ()
244+ response_9 .get_nameid_format ()
245+
246+ json_settings ['strict' ] = False
247+ settings = OneLogin_Saml2_Settings (json_settings )
248+ xml_5 = self .file_contents (join (self .data_path , 'responses' , 'invalids' , 'wrong_spnamequalifier.xml.base64' ))
249+ response_10 = OneLogin_Saml2_Response (settings , xml_5 )
250+ self .assertEqual ('urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress' , response_10 .get_nameid_format ())
186251
187252 json_settings ['strict' ] = True
188253 settings = OneLogin_Saml2_Settings (json_settings )
189254
190255 xml_5 = self .file_contents (join (self .data_path , 'responses' , 'invalids' , 'wrong_spnamequalifier.xml.base64' ))
191- response_8 = OneLogin_Saml2_Response (settings , xml_5 )
256+ response_11 = OneLogin_Saml2_Response (settings , xml_5 )
192257 with self .assertRaisesRegex (Exception , 'The SPNameQualifier value mistmatch the SP entityID value.' ):
193- response_8 .get_nameid ()
258+ response_11 .get_nameid_format ()
259+
260+ json_settings ['strict' ] = True
261+ json_settings ['security' ]['wantNameId' ] = True
262+ settings = OneLogin_Saml2_Settings (json_settings )
194263
195264 xml_6 = self .file_contents (join (self .data_path , 'responses' , 'invalids' , 'empty_nameid.xml.base64' ))
196- response_9 = OneLogin_Saml2_Response (settings , xml_6 )
265+ response_12 = OneLogin_Saml2_Response (settings , xml_6 )
197266 with self .assertRaisesRegex (Exception , 'An empty NameID value found' ):
198- response_9 .get_nameid ()
267+ response_12 .get_nameid_format ()
268+
269+ json_settings ['security' ]['wantNameId' ] = False
270+ settings = OneLogin_Saml2_Settings (json_settings )
271+ response_13 = OneLogin_Saml2_Response (settings , xml_6 )
272+ self .assertEqual ('urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress' , response_13 .get_nameid_format ())
273+
274+ json_settings ['strict' ] = False
275+ json_settings ['security' ]['wantNameId' ] = False
276+ settings = OneLogin_Saml2_Settings (json_settings )
277+ response_14 = OneLogin_Saml2_Response (settings , xml_6 )
278+ self .assertEqual ('urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress' , response_14 .get_nameid_format ())
279+
280+ json_settings ['security' ]['wantNameId' ] = True
281+ settings = OneLogin_Saml2_Settings (json_settings )
282+ response_15 = OneLogin_Saml2_Response (settings , xml_6 )
283+ self .assertEqual ('urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress' , response_15 .get_nameid_format ())
284+
285+ del json_settings ['security' ]['wantNameId' ]
286+ settings = OneLogin_Saml2_Settings (json_settings )
287+ response_16 = OneLogin_Saml2_Response (settings , xml_6 )
288+ self .assertEqual ('urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress' , response_16 .get_nameid_format ())
289+
290+ json_settings ['strict' ] = True
291+ settings = OneLogin_Saml2_Settings (json_settings )
292+ response_17 = OneLogin_Saml2_Response (settings , xml_6 )
293+ with self .assertRaisesRegex (Exception , 'An empty NameID value found' ):
294+ response_17 .get_nameid_format ()
199295
200296 def testGetNameIdData (self ):
201297 """
202298 Tests the get_nameid_data method of the OneLogin_Saml2_Response
203299 """
204300 json_settings = self .loadSettingsJSON ()
301+ json_settings ['strict' ] = False
205302 settings = OneLogin_Saml2_Settings (json_settings )
206303 xml = self .file_contents (join (self .data_path , 'responses' , 'response1.xml.base64' ))
207304 response = OneLogin_Saml2_Response (settings , xml )
@@ -232,58 +329,110 @@ def testGetNameIdData(self):
232329 nameid_data_3 = response_3 .get_nameid_data ()
233330 self .assertEqual (expected_nameid_data_3 , nameid_data_3 )
234331
332+ json_settings ['strict' ] = True
333+ json_settings ['security' ]['wantNameId' ] = True
334+ settings = OneLogin_Saml2_Settings (json_settings )
335+
235336 xml_4 = self .file_contents (join (self .data_path , 'responses' , 'invalids' , 'no_nameid.xml.base64' ))
236337 response_4 = OneLogin_Saml2_Response (settings , xml_4 )
237338 with self .assertRaisesRegex (Exception , 'NameID not found in the assertion of the Response' ):
238339 response_4 .get_nameid_data ()
239340
240- json_settings ['security' ]['wantNameId' ] = True
341+ json_settings ['security' ]['wantNameId' ] = False
241342 settings = OneLogin_Saml2_Settings (json_settings )
242-
243343 response_5 = OneLogin_Saml2_Response (settings , xml_4 )
244- with self . assertRaisesRegex ( Exception , 'NameID not found in the assertion of the Response' ):
245- response_5 . get_nameid_data ( )
344+ nameid_data_5 = response_5 . get_nameid_data ()
345+ self . assertEqual ({}, nameid_data_5 )
246346
347+ json_settings ['strict' ] = False
247348 json_settings ['security' ]['wantNameId' ] = False
248349 settings = OneLogin_Saml2_Settings (json_settings )
249-
250350 response_6 = OneLogin_Saml2_Response (settings , xml_4 )
251351 nameid_data_6 = response_6 .get_nameid_data ()
252352 self .assertEqual ({}, nameid_data_6 )
253353
254- del json_settings ['security' ]['wantNameId' ]
354+ json_settings ['security' ]['wantNameId' ] = True
255355 settings = OneLogin_Saml2_Settings (json_settings )
256-
257356 response_7 = OneLogin_Saml2_Response (settings , xml_4 )
258- with self .assertRaisesRegex (Exception , 'NameID not found in the assertion of the Response' ):
259- response_7 .get_nameid_data ()
260-
261- json_settings ['security' ]['wantNameId' ] = False
262- settings = OneLogin_Saml2_Settings (json_settings )
263-
264- response_6 = OneLogin_Saml2_Response (settings , xml_4 )
265- nameid_data_6 = response_6 .get_nameid_data ()
266- self .assertEqual ({}, nameid_data_6 )
357+ nameid_data_7 = response_7 .get_nameid_data ()
358+ self .assertEqual ({}, nameid_data_7 )
267359
268360 del json_settings ['security' ]['wantNameId' ]
269361 settings = OneLogin_Saml2_Settings (json_settings )
362+ response_8 = OneLogin_Saml2_Response (settings , xml_4 )
363+ nameid_data_8 = response_8 .get_nameid_data ()
364+ self .assertEqual ({}, nameid_data_8 )
270365
271- response_7 = OneLogin_Saml2_Response (settings , xml_4 )
366+ json_settings ['strict' ] = True
367+ settings = OneLogin_Saml2_Settings (json_settings )
368+ response_9 = OneLogin_Saml2_Response (settings , xml_4 )
272369 with self .assertRaisesRegex (Exception , 'NameID not found in the assertion of the Response' ):
273- response_7 .get_nameid_data ()
370+ response_9 .get_nameid_data ()
371+
372+ expected_nameid_data_4 = {
373+ 'Format' : 'urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress' ,
374+ 'SPNameQualifier' : 'wrong-sp-entityid' ,
375+ 'Value' : 'test@example.com'
376+ }
377+ json_settings ['strict' ] = False
378+ settings = OneLogin_Saml2_Settings (json_settings )
379+ xml_5 = self .file_contents (join (self .data_path , 'responses' , 'invalids' , 'wrong_spnamequalifier.xml.base64' ))
380+ response_10 = OneLogin_Saml2_Response (settings , xml_5 )
381+ nameid_data_10 = response_10 .get_nameid_data ()
382+ self .assertEqual (expected_nameid_data_4 , nameid_data_10 )
274383
275384 json_settings ['strict' ] = True
276385 settings = OneLogin_Saml2_Settings (json_settings )
277386
278387 xml_5 = self .file_contents (join (self .data_path , 'responses' , 'invalids' , 'wrong_spnamequalifier.xml.base64' ))
279- response_8 = OneLogin_Saml2_Response (settings , xml_5 )
388+ response_11 = OneLogin_Saml2_Response (settings , xml_5 )
280389 with self .assertRaisesRegex (Exception , 'The SPNameQualifier value mistmatch the SP entityID value.' ):
281- response_8 .get_nameid_data ()
390+ response_11 .get_nameid_data ()
391+
392+ expected_nameid_data_5 = {
393+ 'Value' : None ,
394+ 'Format' : 'urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress' ,
395+ }
396+
397+ json_settings ['strict' ] = True
398+ json_settings ['security' ]['wantNameId' ] = True
399+ settings = OneLogin_Saml2_Settings (json_settings )
282400
283401 xml_6 = self .file_contents (join (self .data_path , 'responses' , 'invalids' , 'empty_nameid.xml.base64' ))
284- response_9 = OneLogin_Saml2_Response (settings , xml_6 )
402+ response_12 = OneLogin_Saml2_Response (settings , xml_6 )
285403 with self .assertRaisesRegex (Exception , 'An empty NameID value found' ):
286- response_9 .get_nameid_data ()
404+ response_12 .get_nameid_data ()
405+
406+ json_settings ['security' ]['wantNameId' ] = False
407+ settings = OneLogin_Saml2_Settings (json_settings )
408+ response_13 = OneLogin_Saml2_Response (settings , xml_6 )
409+ nameid_data_13 = response_13 .get_nameid_data ()
410+ nameid_data_13 = self .assertEqual (expected_nameid_data_5 , nameid_data_13 )
411+
412+ json_settings ['strict' ] = False
413+ json_settings ['security' ]['wantNameId' ] = False
414+ settings = OneLogin_Saml2_Settings (json_settings )
415+ response_14 = OneLogin_Saml2_Response (settings , xml_6 )
416+ nameid_data_14 = response_14 .get_nameid_data ()
417+ self .assertEqual (expected_nameid_data_5 , nameid_data_14 )
418+
419+ json_settings ['security' ]['wantNameId' ] = True
420+ settings = OneLogin_Saml2_Settings (json_settings )
421+ response_15 = OneLogin_Saml2_Response (settings , xml_6 )
422+ nameid_data_15 = response_15 .get_nameid_data ()
423+ self .assertEqual (expected_nameid_data_5 , nameid_data_15 )
424+
425+ del json_settings ['security' ]['wantNameId' ]
426+ settings = OneLogin_Saml2_Settings (json_settings )
427+ response_16 = OneLogin_Saml2_Response (settings , xml_6 )
428+ nameid_data_16 = response_16 .get_nameid_data ()
429+ self .assertEqual (expected_nameid_data_5 , nameid_data_16 )
430+
431+ json_settings ['strict' ] = True
432+ settings = OneLogin_Saml2_Settings (json_settings )
433+ response_17 = OneLogin_Saml2_Response (settings , xml_6 )
434+ with self .assertRaisesRegex (Exception , 'An empty NameID value found' ):
435+ response_17 .get_nameid_data ()
287436
288437 def testCheckStatus (self ):
289438 """
0 commit comments