@@ -501,16 +501,31 @@ def _add_info(self, msg, **kwargs):
501
501
else :
502
502
msg .extension_elements = extensions
503
503
504
- def fix_cert_str (self , tmp_cert_str ):
505
- tmp_cert_str = "%s" % self .sec .my_cert
506
- tmp_cert_str = tmp_cert_str .replace ("-----BEGIN CERTIFICATE-----\n " , "" )
507
- tmp_cert_str = tmp_cert_str .replace ("\n -----END CERTIFICATE-----\n " , "" )
508
- return tmp_cert_str
504
+ def _encrypt_assertion (self , encrypt_cert , sp_entity_id , response , node_xpath = None ):
505
+ _certs = []
506
+ cbxs = CryptoBackendXmlSec1 (self .config .xmlsec_binary )
507
+ if encrypt_cert :
508
+ _certs = []
509
+ _certs .append (encrypt_cert )
510
+ elif sp_entity_id is not None :
511
+ _certs = self .metadata .certs (sp_entity_id , "any" , "encrypt" )
512
+ exception = None
513
+ for _cert in _certs :
514
+ try :
515
+ _ , cert_file = make_temp (_cert , decode = False )
516
+ response = cbxs .encrypt_assertion (response , self .sec .cert_file ,
517
+ pre_encryption_part (), node_xpath = node_xpath )
518
+ return response
519
+ except Exception as ex :
520
+ exception = ex
521
+ pass
522
+ if exception :
523
+ raise exception
509
524
510
525
def _response (self , in_response_to , consumer_url = None , status = None ,
511
- issuer = None , sign = False , to_sign = None ,
526
+ issuer = None , sign = False , to_sign = None , sp_entity_id = None ,
512
527
encrypt_assertion = False , encrypt_assertion_self_contained = False , encrypted_advice_attributes = False ,
513
- encrypt_cert = None ,sign_assertion = None , ** kwargs ):
528
+ encrypt_cert = None , encrypt_cert_assertion = None , sign_assertion = None , ** kwargs ):
514
529
""" Create a Response.
515
530
Encryption:
516
531
encrypt_assertion must be true for encryption to be performed. If encrypted_advice_attributes also is
@@ -530,6 +545,7 @@ def _response(self, in_response_to, consumer_url=None, status=None,
530
545
:return: A Response instance
531
546
"""
532
547
548
+
533
549
if not status :
534
550
status = success_status_factory ()
535
551
@@ -582,16 +598,11 @@ def _response(self, in_response_to, consumer_url=None, status=None,
582
598
583
599
if to_sign_advice :
584
600
response = signed_instance_factory (response , self .sec , to_sign_advice )
585
- tmp_cert_str = self .fix_cert_str ("%s" % encrypt_cert )
586
- _ , cert_file = make_temp ("%s" % encrypt_cert , decode = False )
587
- response = cbxs .encrypt_assertion (response , cert_file ,
588
- pre_encryption_part (), node_xpath = node_xpath )
589
- encrypt_advice = True
601
+ response = self ._encrypt_assertion (encrypt_cert , sp_entity_id , response , node_xpath = node_xpath )
590
602
if encrypt_assertion :
591
603
response = response_from_string (response )
592
604
if encrypt_assertion :
593
605
if encrypt_assertion_self_contained :
594
- assertion_tag = None
595
606
try :
596
607
assertion_tag = response .assertion ._to_element_tree ().tag
597
608
except :
@@ -607,15 +618,7 @@ def _response(self, in_response_to, consumer_url=None, status=None,
607
618
to_sign_assertion .append ((class_name (response .assertion ), response .assertion .id ))
608
619
if to_sign_assertion :
609
620
response = signed_instance_factory (response , self .sec , to_sign_assertion )
610
- if encrypt_cert is not None and not encrypt_advice :
611
- _ , cert_file = make_temp ("%s" % encrypt_cert , decode = False )
612
- else :
613
- tmp_cert_str = self .fix_cert_str ("%s" % self .sec .my_cert )
614
- _ , cert_file = make_temp (tmp_cert_str , decode = False )
615
-
616
- response = cbxs .encrypt_assertion (response , cert_file ,
617
- pre_encryption_part ())
618
- # template(response.assertion.id))
621
+ response = self ._encrypt_assertion (encrypt_cert_assertion , sp_entity_id , response )
619
622
if sign :
620
623
return signed_instance_factory (response , self .sec , sign_class )
621
624
else :
0 commit comments