@@ -197,20 +197,12 @@ def __load_settings_from_dict(self, settings):
197197 self .__errors = []
198198 self .__sp = settings ['sp' ]
199199
200- if 'idp' in settings :
201- self .__idp = settings ['idp' ]
202- if 'strict' in settings :
203- self .__strict = settings ['strict' ]
204- if 'debug' in settings :
205- self .__debug = settings ['debug' ]
206- if 'security' in settings :
207- self .__security = settings ['security' ]
208- else :
209- self .__security = {}
210- if 'contactPerson' in settings :
211- self .__contacts = settings ['contactPerson' ]
212- if 'organization' in settings :
213- self .__organization = settings ['organization' ]
200+ self .__idp = settings .get ('idp' , {})
201+ self .__strict = settings .get ('strict' , False )
202+ self .__debug = settings .get ('debug' , False )
203+ self .__security = settings .get ('security' , {})
204+ self .__contacts = settings .get ('contactPerson' , {})
205+ self .__organization = settings .get ('organization' , {})
214206
215207 self .__add_default_values ()
216208 return True
@@ -252,79 +244,53 @@ def __add_default_values(self):
252244 """
253245 Add default values if the settings info is not complete
254246 """
255- if 'assertionConsumerService' not in self .__sp .keys ():
256- self .__sp ['assertionConsumerService' ] = {}
257- if 'binding' not in self .__sp ['assertionConsumerService' ].keys ():
258- self .__sp ['assertionConsumerService' ]['binding' ] = OneLogin_Saml2_Constants .BINDING_HTTP_POST
247+ self .__sp .setdefault ('assertionConsumerService' , {})
248+ self .__sp ['assertionConsumerService' ].setdefault ('binding' , OneLogin_Saml2_Constants .BINDING_HTTP_POST )
259249
260- if 'attributeConsumingService' not in self .__sp .keys ():
261- self .__sp ['attributeConsumingService' ] = {}
250+ self .__sp .setdefault ('attributeConsumingService' , {})
262251
263- if 'singleLogoutService' not in self .__sp .keys ():
264- self .__sp ['singleLogoutService' ] = {}
265- if 'binding' not in self .__sp ['singleLogoutService' ]:
266- self .__sp ['singleLogoutService' ]['binding' ] = OneLogin_Saml2_Constants .BINDING_HTTP_REDIRECT
252+ self .__sp .setdefault ('singleLogoutService' , {})
253+ self .__sp ['singleLogoutService' ].setdefault ('binding' , OneLogin_Saml2_Constants .BINDING_HTTP_REDIRECT )
267254
268255 # Related to nameID
269- if 'NameIDFormat' not in self .__sp :
270- self .__sp ['NameIDFormat' ] = OneLogin_Saml2_Constants .NAMEID_UNSPECIFIED
271- if 'nameIdEncrypted' not in self .__security :
272- self .__security ['nameIdEncrypted' ] = False
256+ self .__sp .setdefault ('NameIDFormat' , OneLogin_Saml2_Constants .NAMEID_UNSPECIFIED )
257+ self .__security .setdefault ('nameIdEncrypted' , False )
273258
274259 # Metadata format
275- if 'metadataValidUntil' not in self .__security .keys ():
276- self .__security ['metadataValidUntil' ] = None # None means use default
277- if 'metadataCacheDuration' not in self .__security .keys ():
278- self .__security ['metadataCacheDuration' ] = None # None means use default
260+ self .__security .setdefault ('metadataValidUntil' , None ) # None means use default
261+ self .__security .setdefault ('metadataCacheDuration' , None ) # None means use default
279262
280263 # Sign provided
281- if 'authnRequestsSigned' not in self .__security .keys ():
282- self .__security ['authnRequestsSigned' ] = False
283- if 'logoutRequestSigned' not in self .__security .keys ():
284- self .__security ['logoutRequestSigned' ] = False
285- if 'logoutResponseSigned' not in self .__security .keys ():
286- self .__security ['logoutResponseSigned' ] = False
287- if 'signMetadata' not in self .__security .keys ():
288- self .__security ['signMetadata' ] = False
264+ self .__security .setdefault ('authnRequestsSigned' , False )
265+ self .__security .setdefault ('logoutRequestSigned' , False )
266+ self .__security .setdefault ('logoutResponseSigned' , False )
267+ self .__security .setdefault ('signMetadata' , False )
289268
290269 # Sign expected
291- if 'wantMessagesSigned' not in self .__security .keys ():
292- self .__security ['wantMessagesSigned' ] = False
293- if 'wantAssertionsSigned' not in self .__security .keys ():
294- self .__security ['wantAssertionsSigned' ] = False
270+ self .__security .setdefault ('wantMessagesSigned' , False )
271+ self .__security .setdefault ('wantAssertionsSigned' , False )
295272
296273 # NameID element expected
297- if 'wantNameId' not in self .__security .keys ():
298- self .__security ['wantNameId' ] = True
274+ self .__security .setdefault ('wantNameId' , True )
299275
300276 # Encrypt expected
301- if 'wantAssertionsEncrypted' not in self .__security .keys ():
302- self .__security ['wantAssertionsEncrypted' ] = False
303- if 'wantNameIdEncrypted' not in self .__security .keys ():
304- self .__security ['wantNameIdEncrypted' ] = False
277+ self .__security .setdefault ('wantAssertionsEncrypted' , False )
278+ self .__security .setdefault ('wantNameIdEncrypted' , False )
305279
306280 # Signature Algorithm
307- if 'signatureAlgorithm' not in self .__security .keys ():
308- self .__security ['signatureAlgorithm' ] = OneLogin_Saml2_Constants .RSA_SHA1
281+ self .__security .setdefault ('signatureAlgorithm' , OneLogin_Saml2_Constants .RSA_SHA1 )
309282
310283 # AttributeStatement required by default
311- if 'wantAttributeStatement' not in self .__security .keys ():
312- self .__security ['wantAttributeStatement' ] = True
284+ self .__security .setdefault ('wantAttributeStatement' , True )
313285
314- if 'x509cert' not in self .__idp :
315- self .__idp ['x509cert' ] = ''
316- if 'certFingerprint' not in self .__idp :
317- self .__idp ['certFingerprint' ] = ''
318- if 'certFingerprintAlgorithm' not in self .__idp :
319- self .__idp ['certFingerprintAlgorithm' ] = 'sha1'
286+ self .__idp .setdefault ('x509cert' , '' )
287+ self .__idp .setdefault ('certFingerprint' , '' )
288+ self .__idp .setdefault ('certFingerprintAlgorithm' , 'sha1' )
320289
321- if 'x509cert' not in self .__sp :
322- self .__sp ['x509cert' ] = ''
323- if 'privateKey' not in self .__sp :
324- self .__sp ['privateKey' ] = ''
290+ self .__sp .setdefault ('x509cert' , '' )
291+ self .__sp .setdefault ('privateKey' , '' )
325292
326- if 'requestedAuthnContext' not in self .__security .keys ():
327- self .__security ['requestedAuthnContext' ] = True
293+ self .__security .setdefault ('requestedAuthnContext' , True )
328294
329295 def check_settings (self , settings ):
330296 """
@@ -365,37 +331,31 @@ def check_idp_settings(self, settings):
365331 if not isinstance (settings , dict ) or len (settings ) == 0 :
366332 errors .append ('invalid_syntax' )
367333 else :
368- if 'idp' not in settings or len ( settings [ 'idp' ]) == 0 :
334+ if not settings . get ( 'idp' ) :
369335 errors .append ('idp_not_found' )
370336 else :
371337 idp = settings ['idp' ]
372- if 'entityId' not in idp or len ( idp [ 'entityId' ]) == 0 :
338+ if not idp . get ( 'entityId' ) :
373339 errors .append ('idp_entityId_not_found' )
374340
375- if 'singleSignOnService' not in idp or \
376- 'url' not in idp ['singleSignOnService' ] or \
377- len (idp ['singleSignOnService' ]['url' ]) == 0 :
341+ if not idp .get ('singleSignOnService' , {}).get ('url' ):
378342 errors .append ('idp_sso_not_found' )
379343 elif not validate_url (idp ['singleSignOnService' ]['url' ]):
380344 errors .append ('idp_sso_url_invalid' )
381345
382- if 'singleLogoutService' in idp and \
383- 'url' in idp ['singleLogoutService' ] and \
384- len (idp ['singleLogoutService' ]['url' ]) > 0 and \
385- not validate_url (idp ['singleLogoutService' ]['url' ]):
346+ slo_url = idp .get ('singleLogoutService' , {}).get ('url' )
347+ if slo_url and not validate_url (slo_url ):
386348 errors .append ('idp_slo_url_invalid' )
387349
388350 if 'security' in settings :
389351 security = settings ['security' ]
390352
391- exists_x509 = ('x509cert' in idp and
392- len (idp ['x509cert' ]) > 0 )
393- exists_fingerprint = ('certFingerprint' in idp and
394- len (idp ['certFingerprint' ]) > 0 )
353+ exists_x509 = bool (idp .get ('x509cert' ))
354+ exists_fingerprint = bool (idp .get ('certFingerprint' ))
395355
396- want_assert_sign = 'wantAssertionsSigned' in security .keys () and security [ 'wantAssertionsSigned' ]
397- want_mes_signed = 'wantMessagesSigned' in security .keys () and security [ 'wantMessagesSigned' ]
398- nameid_enc = 'nameIdEncrypted' in security .keys () and security [ 'nameIdEncrypted' ]
356+ want_assert_sign = bool ( security .get ( 'wantAssertionsSigned' ))
357+ want_mes_signed = bool ( security .get ( 'wantMessagesSigned' ))
358+ nameid_enc = bool ( security .get ( 'nameIdEncrypted' ))
399359
400360 if (want_assert_sign or want_mes_signed ) and \
401361 not (exists_x509 or exists_fingerprint ):
@@ -418,32 +378,28 @@ def check_sp_settings(self, settings):
418378 assert isinstance (settings , dict )
419379
420380 errors = []
421- if not isinstance (settings , dict ) or len ( settings ) == 0 :
381+ if not isinstance (settings , dict ) or not settings :
422382 errors .append ('invalid_syntax' )
423383 else :
424- if 'sp' not in settings or len ( settings [ 'sp' ]) == 0 :
384+ if not settings . get ( 'sp' ) :
425385 errors .append ('sp_not_found' )
426386 else :
427387 # check_sp_certs uses self.__sp so I add it
428388 old_sp = self .__sp
429389 self .__sp = settings ['sp' ]
430390
431391 sp = settings ['sp' ]
432- security = {}
433- if 'security' in settings :
434- security = settings ['security' ]
392+ security = settings .get ('security' , {})
435393
436- if 'entityId' not in sp or len ( sp [ 'entityId' ]) == 0 :
394+ if not sp . get ( 'entityId' ) :
437395 errors .append ('sp_entityId_not_found' )
438396
439- if 'assertionConsumerService' not in sp or \
440- 'url' not in sp ['assertionConsumerService' ] or \
441- len (sp ['assertionConsumerService' ]['url' ]) == 0 :
397+ if not sp .get ('assertionConsumerService' , {}).get ('url' ):
442398 errors .append ('sp_acs_not_found' )
443399 elif not validate_url (sp ['assertionConsumerService' ]['url' ]):
444400 errors .append ('sp_acs_url_invalid' )
445401
446- if 'attributeConsumingService' in sp and len ( sp [ 'attributeConsumingService' ] ):
402+ if sp . get ( 'attributeConsumingService' ):
447403 attributeConsumingService = sp ['attributeConsumingService' ]
448404 if 'serviceName' not in attributeConsumingService :
449405 errors .append ('sp_attributeConsumingService_serviceName_not_found' )
@@ -468,22 +424,20 @@ def check_sp_settings(self, settings):
468424 if "serviceDescription" in attributeConsumingService and not isinstance (attributeConsumingService ['serviceDescription' ], basestring ):
469425 errors .append ('sp_attributeConsumingService_serviceDescription_type_invalid' )
470426
471- if 'singleLogoutService' in sp and \
472- 'url' in sp ['singleLogoutService' ] and \
473- len (sp ['singleLogoutService' ]['url' ]) > 0 and \
474- not validate_url (sp ['singleLogoutService' ]['url' ]):
427+ slo_url = sp .get ('singleLogoutService' , {}).get ('url' )
428+ if slo_url and not validate_url (slo_url ):
475429 errors .append ('sp_sls_url_invalid' )
476430
477431 if 'signMetadata' in security and isinstance (security ['signMetadata' ], dict ):
478432 if 'keyFileName' not in security ['signMetadata' ] or \
479433 'certFileName' not in security ['signMetadata' ]:
480434 errors .append ('sp_signMetadata_invalid' )
481435
482- authn_sign = 'authnRequestsSigned' in security .keys () and security [ 'authnRequestsSigned' ]
483- logout_req_sign = 'logoutRequestSigned' in security .keys () and security [ 'logoutRequestSigned' ]
484- logout_res_sign = 'logoutResponseSigned' in security .keys () and security [ 'logoutResponseSigned' ]
485- want_assert_enc = 'wantAssertionsEncrypted' in security .keys () and security [ 'wantAssertionsEncrypted' ]
486- want_nameid_enc = 'wantNameIdEncrypted' in security .keys () and security [ 'wantNameIdEncrypted' ]
436+ authn_sign = bool ( security .get ( 'authnRequestsSigned' ))
437+ logout_req_sign = bool ( security .get ( 'logoutRequestSigned' ))
438+ logout_res_sign = bool ( security .get ( 'logoutResponseSigned' ))
439+ want_assert_enc = bool ( security .get ( 'wantAssertionsEncrypted' ))
440+ want_nameid_enc = bool ( security .get ( 'wantNameIdEncrypted' ))
487441
488442 if not self .check_sp_certs ():
489443 if authn_sign or logout_req_sign or logout_res_sign or \
@@ -535,40 +489,32 @@ def get_sp_key(self):
535489 Returns the x509 private key of the SP.
536490
537491 :returns: SP private key
538- :rtype: string
492+ :rtype: string or None
539493 """
540- key = None
494+ key = self .__sp .get ('privateKey' )
495+ key_file_name = self .__paths ['cert' ] + 'sp.key'
541496
542- if 'privateKey' in self .__sp .keys () and self .__sp ['privateKey' ]:
543- key = self .__sp ['privateKey' ]
544- else :
545- key_file_name = self .__paths ['cert' ] + 'sp.key'
497+ if not key and exists (key_file_name ):
498+ with open (key_file_name ) as f :
499+ key = f .read ()
546500
547- if exists (key_file_name ):
548- f_key = open (key_file_name , 'r' )
549- key = f_key .read ()
550- f_key .close ()
551- return key
501+ return key or None
552502
553503 def get_sp_cert (self ):
554504 """
555505 Returns the x509 public cert of the SP.
556506
557507 :returns: SP public cert
558- :rtype: string
508+ :rtype: string or None
559509 """
560- cert = None
510+ cert = self .__sp .get ('x509cert' )
511+ cert_file_name = self .__paths ['cert' ] + 'sp.crt'
561512
562- if 'x509cert' in self .__sp .keys () and self .__sp ['x509cert' ]:
563- cert = self .__sp ['x509cert' ]
564- else :
565- cert_file_name = self .__paths ['cert' ] + 'sp.crt'
566- if exists (cert_file_name ):
567- f_cert = open (cert_file_name , 'r' )
568- cert = f_cert .read ()
569- f_cert .close ()
513+ if not cert and exists (cert_file_name ):
514+ with open (cert_file_name ) as f :
515+ cert = f .read ()
570516
571- return cert
517+ return cert or None
572518
573519 def get_idp_cert (self ):
574520 """
@@ -577,11 +523,7 @@ def get_idp_cert(self):
577523 :returns: IdP public cert
578524 :rtype: string
579525 """
580- cert = None
581-
582- if 'x509cert' in self .__idp .keys () and self .__idp ['x509cert' ]:
583- cert = self .__idp ['x509cert' ]
584- return cert
526+ return self .__idp .get ('x509cert' )
585527
586528 def get_idp_data (self ):
587529 """
0 commit comments