diff --git a/lib/xml_security.rb b/lib/xml_security.rb index db54ff34..bfe547dc 100644 --- a/lib/xml_security.rb +++ b/lib/xml_security.rb @@ -185,8 +185,9 @@ def sign_document(private_key, certificate, signature_method = RSA_SHA1, digest_ x509_cert_element.text = Base64.encode64(certificate.to_der).gsub(/\n/, "") # add the signature - issuer_element = elements["//saml:Issuer"] - if issuer_element + if issuer_element = elements["//saml:Issuer"] + root.insert_after(issuer_element, signature_element) + elsif issuer_element = elements["//saml2:Issuer"] root.insert_after(issuer_element, signature_element) elsif first_child = root.children[0] root.insert_before(first_child, signature_element) diff --git a/test/response_test.rb b/test/response_test.rb index 8acf90a4..48994e38 100644 --- a/test/response_test.rb +++ b/test/response_test.rb @@ -1411,6 +1411,28 @@ def generate_audience_error(expected, actual) end assert_empty signed_response.errors end + + it 'sign an unsigned SAML2 Response XML and initiate the SAML object with it' do + xml = Base64.decode64(fixture("test_sign_saml2.xml")) + + document = XMLSecurity::Document.new(xml) + + formatted_cert = OneLogin::RubySaml::Utils.format_cert(ruby_saml_cert_text) + cert = OpenSSL::X509::Certificate.new(formatted_cert) + + formatted_private_key = OneLogin::RubySaml::Utils.format_private_key(ruby_saml_key_text) + private_key = OpenSSL::PKey::RSA.new(formatted_private_key) + document.sign_document(private_key, cert) + + signed_response = OneLogin::RubySaml::Response.new(document.to_s) + settings.assertion_consumer_service_url = "https://example.hello.com/access/saml" + settings.idp_cert = ruby_saml_cert_text + signed_response.settings = settings + Timecop.freeze(Time.parse("2011-06-21 14:09:38Z")) do + signed_response.is_valid? + end + assert_empty signed_response.errors + end end describe '#want_assertion_signed' do diff --git a/test/responses/test_sign_saml2.xml b/test/responses/test_sign_saml2.xml new file mode 100644 index 00000000..e29a8d4d --- /dev/null +++ b/test/responses/test_sign_saml2.xml @@ -0,0 +1,34 @@ + + + https://idm.orademo.com + + + + + https://idm.orademo.com + + someone@example.org + + + + + + + hello.com + + + + + urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport + + + + + Someone + + + Special + + + +