Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/xml_security.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions test/response_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions test/responses/test_sign_saml2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<saml2p:Response xmlns:saml2p="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:xs="http://www.w3.org/2001/XMLSchema" Destination="https://example.hello.com/access/saml" ID="jVFQbyEpSfUwqhZtJtarIaGoshwuAQMDwLoiMhzJXsv" InResponseTo="cfeooghajnhofcmogakmlhpkohnmikicnfhdnjlc" IssueInstant="2011-06-21T13:54:38.661Z" Version="2.0">
<saml2:Issuer xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion">https://idm.orademo.com</saml2:Issuer>
<saml2p:Status>
<saml2p:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"/>
</saml2p:Status>
<saml2:Assertion xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion" ID="emmCjammnYdAbMWDuMAJeZvQIMBayeeYqqwvQoDclKE" IssueInstant="2011-06-21T13:54:38.676Z" Version="2.0">
<saml2:Issuer>https://idm.orademo.com</saml2:Issuer>
<saml2:Subject>
<saml2:NameID Format="urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress" NameQualifier="idp.example.org">someone@example.org</saml2:NameID>
<saml2:SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer">
<saml2:SubjectConfirmationData InResponseTo="cfeooghajnhofcmogakmlhpkohnmikicnfhdnjlc" NotOnOrAfter="2011-06-21T14:09:38.676Z" Recipient="https://example.hello.com/access/saml"/>
</saml2:SubjectConfirmation>
</saml2:Subject>
<saml2:Conditions NotBefore="2011-06-21T13:54:38.683Z" NotOnOrAfter="2011-06-21T14:09:38.683Z">
<saml2:AudienceRestriction>
<saml2:Audience>hello.com</saml2:Audience>
</saml2:AudienceRestriction>
</saml2:Conditions>
<saml2:AuthnStatement AuthnInstant="2011-06-21T13:54:38.685Z" SessionIndex="perdkjfskdjfksdiertusfsdfsddeurtherukjdfgkdffg">
<saml2:AuthnContext>
<saml2:AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport</saml2:AuthnContextClassRef>
</saml2:AuthnContext>
</saml2:AuthnStatement>
<saml2:AttributeStatement>
<saml2:Attribute Name="FirstName">
<saml2:AttributeValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">Someone</saml2:AttributeValue>
</saml2:Attribute>
<saml2:Attribute Name="LastName">
<saml2:AttributeValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">Special</saml2:AttributeValue>
</saml2:Attribute>
</saml2:AttributeStatement>
</saml2:Assertion>
</saml2p:Response>
Loading