@@ -38,8 +38,8 @@ def __init__(self, settings, response):
38
38
:param response: The base64 encoded, XML string containing the samlp:Response
39
39
:type response: string
40
40
"""
41
- self .__settings = settings
42
- self .__error = None
41
+ self ._settings = settings
42
+ self ._error = None
43
43
self .response = self .__class__ .decode_response (response )
44
44
self .document = fromstring (self .response )
45
45
self .decrypted_document = None
@@ -68,8 +68,8 @@ def is_valid(self, request_data, response_id=None):
68
68
69
69
return self .validate_response (request_data , response_id )
70
70
except Exception as err :
71
- self .__error = err .__str__ ()
72
- debug = self .__settings .is_debug_active ()
71
+ self ._error = err .__str__ ()
72
+ debug = self ._settings .is_debug_active ()
73
73
if debug :
74
74
print err .__str__ ()
75
75
return False
@@ -103,7 +103,7 @@ def get_audiences(self):
103
103
"""
104
104
audiences = []
105
105
106
- audience_nodes = self .__query_assertion ('/saml:Conditions/saml:AudienceRestriction/saml:Audience' )
106
+ audience_nodes = self ._query_assertion ('/saml:Conditions/saml:AudienceRestriction/saml:Audience' )
107
107
for audience_node in audience_nodes :
108
108
audiences .append (audience_node .text )
109
109
return audiences
@@ -117,11 +117,11 @@ def get_issuers(self):
117
117
"""
118
118
issuers = []
119
119
120
- message_issuer_nodes = self .__query ('/samlp:Response/saml:Issuer' )
120
+ message_issuer_nodes = self ._query ('/samlp:Response/saml:Issuer' )
121
121
if message_issuer_nodes :
122
122
issuers .append (message_issuer_nodes [0 ].text )
123
123
124
- assertion_issuer_nodes = self .__query_assertion ('/saml:Issuer' )
124
+ assertion_issuer_nodes = self ._query_assertion ('/saml:Issuer' )
125
125
if assertion_issuer_nodes :
126
126
issuers .append (assertion_issuer_nodes [0 ].text )
127
127
@@ -135,13 +135,13 @@ def get_nameid_data(self):
135
135
:rtype: dict
136
136
"""
137
137
nameid = None
138
- encrypted_id_data_nodes = self .__query_assertion ('/saml:Subject/saml:EncryptedID/xenc:EncryptedData' )
138
+ encrypted_id_data_nodes = self ._query_assertion ('/saml:Subject/saml:EncryptedID/xenc:EncryptedData' )
139
139
if encrypted_id_data_nodes :
140
140
encrypted_data = encrypted_id_data_nodes [0 ]
141
- key = self .__settings .get_sp_key ()
141
+ key = self ._settings .get_sp_key ()
142
142
nameid = OneLogin_Saml2_Utils .decrypt_element (encrypted_data , key )
143
143
else :
144
- nameid_nodes = self .__query_assertion ('/saml:Subject/saml:NameID' )
144
+ nameid_nodes = self ._query_assertion ('/saml:Subject/saml:NameID' )
145
145
if nameid_nodes :
146
146
nameid = nameid_nodes [0 ]
147
147
if nameid is None :
@@ -173,7 +173,7 @@ def get_session_not_on_or_after(self):
173
173
:rtype: time|None
174
174
"""
175
175
not_on_or_after = None
176
- authn_statement_nodes = self .__query_assertion ('/saml:AuthnStatement[@SessionNotOnOrAfter]' )
176
+ authn_statement_nodes = self ._query_assertion ('/saml:AuthnStatement[@SessionNotOnOrAfter]' )
177
177
if authn_statement_nodes :
178
178
not_on_or_after = OneLogin_Saml2_Utils .parse_SAML_to_time (authn_statement_nodes [0 ].get ('SessionNotOnOrAfter' ))
179
179
return not_on_or_after
@@ -189,7 +189,7 @@ def get_session_index(self):
189
189
:rtype: string|None
190
190
"""
191
191
session_index = None
192
- authn_statement_nodes = self .__query_assertion ('/saml:AuthnStatement[@SessionIndex]' )
192
+ authn_statement_nodes = self ._query_assertion ('/saml:AuthnStatement[@SessionIndex]' )
193
193
if authn_statement_nodes :
194
194
session_index = authn_statement_nodes [0 ].get ('SessionIndex' )
195
195
return session_index
@@ -200,7 +200,7 @@ def get_attributes(self):
200
200
EncryptedAttributes are not supported
201
201
"""
202
202
attributes = {}
203
- attribute_nodes = self .__query_assertion ('/saml:AttributeStatement/saml:Attribute' )
203
+ attribute_nodes = self ._query_assertion ('/saml:AttributeStatement/saml:Attribute' )
204
204
for attribute_node in attribute_nodes :
205
205
attr_name = attribute_node .get ('Name' )
206
206
values = []
@@ -216,8 +216,8 @@ def validate_num_assertions(self):
216
216
:returns: True if only 1 assertion encrypted or not
217
217
:rtype: bool
218
218
"""
219
- encrypted_assertion_nodes = self .__query ('/samlp:Response/saml:EncryptedAssertion' )
220
- assertion_nodes = self .__query ('/samlp:Response/saml:Assertion' )
219
+ encrypted_assertion_nodes = self ._query ('/samlp:Response/saml:EncryptedAssertion' )
220
+ assertion_nodes = self ._query ('/samlp:Response/saml:Assertion' )
221
221
return (len (encrypted_assertion_nodes ) + len (assertion_nodes )) == 1
222
222
223
223
def validate_timestamps (self ):
@@ -227,7 +227,7 @@ def validate_timestamps(self):
227
227
:returns: True if the condition is valid, False otherwise
228
228
:rtype: bool
229
229
"""
230
- conditions_nodes = self .__query_assertion ('/saml:Conditions' )
230
+ conditions_nodes = self ._query_assertion ('/saml:Conditions' )
231
231
232
232
for conditions_node in conditions_nodes :
233
233
nb_attr = conditions_node .get ('NotBefore' )
@@ -238,7 +238,7 @@ def validate_timestamps(self):
238
238
return False
239
239
return True
240
240
241
- def __query_assertion (self , xpath_expr ):
241
+ def _query_assertion (self , xpath_expr ):
242
242
"""
243
243
Extracts nodes that match the query from the Assertion
244
244
@@ -254,12 +254,12 @@ def __query_assertion(self, xpath_expr):
254
254
assertion_expr = '/saml:Assertion'
255
255
signature_expr = '/ds:Signature/ds:SignedInfo/ds:Reference'
256
256
signed_assertion_query = '/samlp:Response' + assertion_expr + signature_expr
257
- assertion_reference_nodes = self .__query (signed_assertion_query )
257
+ assertion_reference_nodes = self ._query (signed_assertion_query )
258
258
259
259
if not assertion_reference_nodes :
260
260
# Check if the message is signed
261
261
signed_message_query = '/samlp:Response' + signature_expr
262
- message_reference_nodes = self .__query (signed_message_query )
262
+ message_reference_nodes = self ._query (signed_message_query )
263
263
if message_reference_nodes :
264
264
message_id = message_reference_nodes [0 ].get ('URI' )
265
265
final_query = "/samlp:Response[@ID='%s']/" % message_id [1 :]
@@ -270,9 +270,9 @@ def __query_assertion(self, xpath_expr):
270
270
assertion_id = assertion_reference_nodes [0 ].get ('URI' )
271
271
final_query = '/samlp:Response' + assertion_expr + "[@ID='%s']" % assertion_id [1 :]
272
272
final_query += xpath_expr
273
- return self .__query (final_query )
273
+ return self ._query (final_query )
274
274
275
- def __query (self , query ):
275
+ def _query (self , query ):
276
276
"""
277
277
Extracts nodes that match the query from the Response
278
278
@@ -288,7 +288,7 @@ def __query(self, query):
288
288
document = self .document
289
289
return OneLogin_Saml2_Utils .query (document , query )
290
290
291
- def __decrypt_assertion (self , dom ):
291
+ def _decrypt_assertion (self , dom ):
292
292
"""
293
293
Decrypts the Assertion
294
294
@@ -298,7 +298,7 @@ def __decrypt_assertion(self, dom):
298
298
:returns: Decrypted Assertion
299
299
:rtype: Element
300
300
"""
301
- key = self .__settings .get_sp_key ()
301
+ key = self ._settings .get_sp_key ()
302
302
303
303
if not key :
304
304
raise Exception ('No private key available, check settings' )
@@ -315,7 +315,7 @@ def get_error(self):
315
315
"""
316
316
After execute a validation process, if fails this method returns the cause
317
317
"""
318
- return self .__error
318
+ return self ._error
319
319
320
320
321
321
class OneLogin_Saml2_Response_Post (OneLogin_Saml2_Response ):
@@ -331,16 +331,13 @@ def __init__(self, settings, response):
331
331
:type response: string
332
332
"""
333
333
OneLogin_Saml2_Response .__init__ (self , settings , response )
334
- # Reset these given the meaning of double underscore in Python.
335
- self .__settings = settings
336
- self .__error = None
337
334
338
335
# Quick check for the presence of EncryptedAssertion
339
- encrypted_assertion_nodes = self .__query ('/samlp:Response/saml:EncryptedAssertion' )
336
+ encrypted_assertion_nodes = self ._query ('/samlp:Response/saml:EncryptedAssertion' )
340
337
if encrypted_assertion_nodes :
341
338
decrypted_document = deepcopy (self .document )
342
339
self .encrypted = True
343
- self .decrypted_document = self .__decrypt_assertion (decrypted_document )
340
+ self .decrypted_document = self ._decrypt_assertion (decrypted_document )
344
341
345
342
@staticmethod
346
343
def decode_response (response ):
@@ -366,24 +363,24 @@ def validate_response(self, request_data, request_id=None):
366
363
:returns: True if the SAML Response is valid, False if not
367
364
:rtype: bool
368
365
"""
369
- self .__error = None
370
- idp_data = self .__settings .get_idp_data ()
366
+ self ._error = None
367
+ idp_data = self ._settings .get_idp_data ()
371
368
idp_entity_id = idp_data .get ('entityId' , '' )
372
- sp_data = self .__settings .get_sp_data ()
369
+ sp_data = self ._settings .get_sp_data ()
373
370
sp_entity_id = sp_data .get ('entityId' , '' )
374
371
375
- sign_nodes = self .__query ('//ds:Signature' )
372
+ sign_nodes = self ._query ('//ds:Signature' )
376
373
377
374
signed_elements = []
378
375
for sign_node in sign_nodes :
379
376
signed_elements .append (sign_node .getparent ().tag )
380
377
381
- if self .__settings .is_strict ():
382
- res = OneLogin_Saml2_Utils .validate_xml (etree .tostring (self .document ), 'saml-schema-protocol-2.0.xsd' , self .__settings .is_debug_active ())
378
+ if self ._settings .is_strict ():
379
+ res = OneLogin_Saml2_Utils .validate_xml (etree .tostring (self .document ), 'saml-schema-protocol-2.0.xsd' , self ._settings .is_debug_active ())
383
380
if not isinstance (res , Document ):
384
381
raise Exception ('Invalid SAML Response. Not match the saml-schema-protocol-2.0.xsd' )
385
382
386
- security = self .__settings .get_security_data ()
383
+ security = self ._settings .get_security_data ()
387
384
current_url = OneLogin_Saml2_Utils .get_self_url_no_query (request_data )
388
385
389
386
# Check if the InResponseTo of the Response matchs the ID of the AuthNRequest (requestId) if provided
@@ -396,20 +393,20 @@ def validate_response(self, request_data, request_id=None):
396
393
raise Exception ('The assertion of the Response is not encrypted and the SP require it' )
397
394
398
395
if security .get ('wantNameIdEncrypted' , False ):
399
- encrypted_nameid_nodes = self .__query_assertion ('/saml:Subject/saml:EncryptedID/xenc:EncryptedData' )
396
+ encrypted_nameid_nodes = self ._query_assertion ('/saml:Subject/saml:EncryptedID/xenc:EncryptedData' )
400
397
if len (encrypted_nameid_nodes ) == 0 :
401
398
raise Exception ('The NameID of the Response is not encrypted and the SP require it' )
402
399
403
400
# Checks that there is at least one AttributeStatement
404
- attribute_statement_nodes = self .__query_assertion ('/saml:AttributeStatement' )
401
+ attribute_statement_nodes = self ._query_assertion ('/saml:AttributeStatement' )
405
402
if not attribute_statement_nodes :
406
403
raise Exception ('There is no AttributeStatement on the Response' )
407
404
408
405
# Validates Asserion timestamps
409
406
if not self .validate_timestamps ():
410
407
raise Exception ('Timing issues (please check your clock settings)' )
411
408
412
- encrypted_attributes_nodes = self .__query_assertion ('/saml:AttributeStatement/saml:EncryptedAttribute' )
409
+ encrypted_attributes_nodes = self ._query_assertion ('/saml:AttributeStatement/saml:EncryptedAttribute' )
413
410
if encrypted_attributes_nodes :
414
411
raise Exception ('There is an EncryptedAttribute in the Response and this SP not support them' )
415
412
@@ -441,7 +438,7 @@ def validate_response(self, request_data, request_id=None):
441
438
442
439
# Checks the SubjectConfirmation, at least one SubjectConfirmation must be valid
443
440
any_subject_confirmation = False
444
- subject_confirmation_nodes = self .__query_assertion ('/saml:Subject/saml:SubjectConfirmation' )
441
+ subject_confirmation_nodes = self ._query_assertion ('/saml:Subject/saml:SubjectConfirmation' )
445
442
446
443
for scn in subject_confirmation_nodes :
447
444
method = scn .get ('Method' , None )
@@ -513,9 +510,6 @@ def __init__(self, settings, response):
513
510
:type response: string
514
511
"""
515
512
OneLogin_Saml2_Response .__init__ (self , settings , response )
516
- # Reset these given the meaning of double underscore in Python.
517
- self .__settings = settings
518
- self .__error = None
519
513
520
514
@staticmethod
521
515
def decode_response (response ):
@@ -541,17 +535,17 @@ def validate_response(self, request_data, request_id=None):
541
535
:returns: True if the SAML Response is valid, False if not
542
536
:rtype: bool
543
537
"""
544
- self .__error = None
545
- idp_data = self .__settings .get_idp_data ()
538
+ self ._error = None
539
+ idp_data = self ._settings .get_idp_data ()
546
540
idp_entity_id = idp_data ['entityId' ]
547
541
get_data = request_data ['get_data' ]
548
542
549
- if self .__settings .is_strict ():
550
- res = OneLogin_Saml2_Utils .validate_xml (self .document , 'saml-schema-protocol-2.0.xsd' , self .__settings .is_debug_active ())
543
+ if self ._settings .is_strict ():
544
+ res = OneLogin_Saml2_Utils .validate_xml (self .document , 'saml-schema-protocol-2.0.xsd' , self ._settings .is_debug_active ())
551
545
if not isinstance (res , Document ):
552
546
raise Exception ('Invalid SAML Logout Request. Not match the saml-schema-protocol-2.0.xsd' )
553
547
554
- security = self .__settings .get_security_data ()
548
+ security = self ._settings .get_security_data ()
555
549
556
550
# Check if the InResponseTo of the Logout Response matchs the ID of the Logout Request (requestId) if provided
557
551
if request_id is not None and self .document .documentElement .hasAttribute ('InResponseTo' ):
@@ -595,6 +589,12 @@ def validate_response(self, request_data, request_id=None):
595
589
raise Exception ('In order to validate the sign on the Logout Response, the x509cert of the IdP is required' )
596
590
cert = idp_data ['x509cert' ]
597
591
592
+ print '////////////'
593
+ print cert
594
+ print get_data
595
+ print signed_query
596
+ print '////////////'
597
+
598
598
if not OneLogin_Saml2_Utils .validate_binary_sign (signed_query , b64decode (get_data ['Signature' ]), cert ):
599
599
raise Exception ('Signature validation failed. Logout Response rejected' )
600
600
0 commit comments