Skip to content

Commit c2e52de

Browse files
committed
Related to #209. Fix sign_document method: inclusive_namespaces
1 parent 50ddb3f commit c2e52de

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

lib/xml_security.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ def uuid
107107
#</Signature>
108108
def sign_document(private_key, certificate, signature_method = RSA_SHA1, digest_method = SHA1)
109109
noko = Nokogiri.parse(self.to_s)
110-
canon_doc = noko.canonicalize(canon_algorithm(C14N))
111110

112111
signature_element = REXML::Element.new("ds:Signature").add_namespace('ds', DSIG)
113112
signed_info_element = signature_element.add_element("ds:SignedInfo")
@@ -120,10 +119,14 @@ def sign_document(private_key, certificate, signature_method = RSA_SHA1, digest_
120119
# Add Transforms
121120
transforms_element = reference_element.add_element("ds:Transforms")
122121
transforms_element.add_element("ds:Transform", {"Algorithm" => ENVELOPED_SIG})
123-
transforms_element.add_element("ds:Transform", {"Algorithm" => C14N})
124-
transforms_element.add_element("ds:InclusiveNamespaces", {"xmlns" => C14N, "PrefixList" => INC_PREFIX_LIST})
125-
122+
#transforms_element.add_element("ds:Transform", {"Algorithm" => C14N})
123+
#transforms_element.add_element("ds:InclusiveNamespaces", {"xmlns" => C14N, "PrefixList" => INC_PREFIX_LIST})
124+
c14element = transforms_element.add_element("ds:Transform", {"Algorithm" => C14N})
125+
c14element.add_element("ec:InclusiveNamespaces", {"xmlns:ec" => C14N, "PrefixList" => INC_PREFIX_LIST})
126+
126127
digest_method_element = reference_element.add_element("ds:DigestMethod", {"Algorithm" => digest_method})
128+
inclusive_namespaces = INC_PREFIX_LIST.split(" ")
129+
canon_doc = noko.canonicalize(canon_algorithm(C14N), inclusive_namespaces)
127130
reference_element.add_element("ds:DigestValue").text = compute_digest(canon_doc, algorithm(digest_method_element))
128131

129132
# add SignatureValue

test/response_test.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,5 +379,27 @@ class RubySamlTest < Minitest::Test
379379
end
380380
end
381381

382+
describe '#sign_document' do
383+
it 'Sign an unsigned SAML Response XML and initiate the SAML object with it' do
384+
xml = Base64.decode64(fixture("test_sign.xml"))
385+
386+
document = XMLSecurity::Document.new(xml)
387+
388+
formated_cert = OneLogin::RubySaml::Utils.format_cert(ruby_saml_cert_text)
389+
cert = OpenSSL::X509::Certificate.new(formated_cert)
390+
391+
formated_private_key = OneLogin::RubySaml::Utils.format_private_key(ruby_saml_key_text)
392+
private_key = OpenSSL::PKey::RSA.new(formated_private_key)
393+
document.sign_document(private_key, cert)
394+
395+
saml_response = OneLogin::RubySaml::Response.new(document.to_s)
396+
settings = OneLogin::RubySaml::Settings.new
397+
settings.idp_cert = ruby_saml_cert_text
398+
saml_response.settings = settings
399+
time = Time.parse("2015-03-18T04:50:24Z")
400+
Time.stubs(:now).returns(time)
401+
saml_response.validate!
402+
end
403+
end
382404
end
383405
end

0 commit comments

Comments
 (0)