@@ -8,6 +8,9 @@ module DocumentSigner
88
99 INC_PREFIX_LIST = '#default samlp saml ds xs xsi md'
1010
11+ # Returns a copy of the document with a signature added.
12+ #
13+ # @example The Signature is added following the Issuer node.
1114 # <Signature>
1215 # <SignedInfo>
1316 # <CanonicalizationMethod />
@@ -23,7 +26,6 @@ module DocumentSigner
2326 # <KeyInfo />
2427 # <Object />
2528 # </Signature>
26- # Returns a copy of the document with a signature added.
2729 def sign_document ( document , private_key , certificate , signature_method = RubySaml ::XML ::RSA_SHA256 , digest_method = RubySaml ::XML ::SHA256 )
2830 noko = RubySaml ::XML . safe_load_nokogiri ( document . to_s )
2931
@@ -32,23 +34,23 @@ def sign_document(document, private_key, certificate, signature_method = RubySam
3234
3335 # Modifies an existing Nokogiri document to add a signature.
3436 def sign_document! ( noko , private_key , certificate , signature_method = RubySaml ::XML ::RSA_SHA256 , digest_method = RubySaml ::XML ::SHA256 )
35- signature_element = build_signature_element ( noko , private_key , certificate , signature_method , digest_method )
36- issuer_element = noko . at_xpath ( '//saml:Issuer' , 'saml' => RubySaml :: XML :: NS_ASSERTION )
37- if issuer_element
38- issuer_element . after ( signature_element )
37+ signature_node = build_signature_node ( noko , private_key , certificate , signature_method , digest_method )
38+
39+ if ( issuer_node = noko . at_xpath ( '//saml:Issuer' , 'saml' => RubySaml :: XML :: NS_ASSERTION ) )
40+ issuer_node . after ( signature_node )
3941 elsif noko . root . children . any?
40- noko . root . children . first . before ( signature_element )
42+ noko . root . children . first . before ( signature_node )
4143 else
42- noko . root . add_child ( signature_element )
44+ noko . root . add_child ( signature_node )
4345 end
4446
4547 noko
4648 end
4749
4850 private
4951
50- def build_signature_element ( noko , private_key , certificate , signature_method , digest_method )
51- signature_element = Nokogiri ::XML ::Builder . new do |xml |
52+ def build_signature_node ( noko , private_key , certificate , signature_method , digest_method )
53+ signature_node = Nokogiri ::XML ::Builder . new do |xml |
5254 xml [ 'ds' ] . Signature ( 'xmlns:ds' => RubySaml ::XML ::DSIG ) do
5355 xml [ 'ds' ] . SignedInfo do
5456 xml [ 'ds' ] . CanonicalizationMethod ( Algorithm : RubySaml ::XML ::C14N )
@@ -77,11 +79,11 @@ def build_signature_element(noko, private_key, certificate, signature_method, di
7779 end . doc . root
7880
7981 # Set the signature value
80- signed_info_element = signature_element . at_xpath ( '//ds:SignedInfo' , 'ds' => RubySaml ::XML ::DSIG )
81- sig_value_element = signature_element . at_xpath ( '//ds:SignatureValue' , 'ds' => RubySaml ::XML ::DSIG )
82- sig_value_element . content = signature_value ( signed_info_element , private_key , signature_method )
82+ signed_info_node = signature_node . at_xpath ( '//ds:SignedInfo' , 'ds' => RubySaml ::XML ::DSIG )
83+ signature_value_node = signature_node . at_xpath ( '//ds:SignatureValue' , 'ds' => RubySaml ::XML ::DSIG )
84+ signature_value_node . content = signature_value ( signed_info_node , private_key , signature_method )
8385
84- signature_element
86+ signature_node
8587 end
8688
8789 def digest_value ( document , digest_method )
@@ -93,11 +95,11 @@ def digest_value(document, digest_method)
9395 Base64 . strict_encode64 ( hash_algorithm . digest ( canon_doc ) )
9496 end
9597
96- def signature_value ( signed_info_element , private_key , signature_method )
98+ def signature_value ( signed_info_node , private_key , signature_method )
9799 canon_algorithm = RubySaml ::XML . canon_algorithm ( RubySaml ::XML ::C14N )
98100 hash_algorithm = RubySaml ::XML . hash_algorithm ( signature_method ) . new
99101
100- canon_string = signed_info_element . canonicalize ( canon_algorithm )
102+ canon_string = signed_info_node . canonicalize ( canon_algorithm )
101103 Base64 . strict_encode64 ( private_key . sign ( hash_algorithm , canon_string ) )
102104 end
103105
0 commit comments