|
| 1 | +require File.expand_path("test/test_helper") |
| 2 | + |
| 3 | +describe Samlr do |
| 4 | + describe "invalid multiple saml responses" do |
| 5 | + let(:shared_id) { Samlr::Tools.uuid } |
| 6 | + let(:xml_response_doc) do |
| 7 | + options = { |
| 8 | + :destination => "https://example.org/saml/endpoint", |
| 9 | + :in_response_to => Samlr::Tools.uuid, |
| 10 | + |
| 11 | + :audience => "example.org", |
| 12 | + :not_on_or_after => Samlr::Tools::Timestamp.stamp(Time.now + 60), |
| 13 | + :not_before => Samlr::Tools::Timestamp.stamp(Time.now - 60), |
| 14 | + :response_id => Samlr::Tools.uuid, |
| 15 | + skip_conditions: true, |
| 16 | + sign_response: false, |
| 17 | + sign_assertion: false, |
| 18 | + assertion_id: shared_id, |
| 19 | + certificate: TEST_CERTIFICATE |
| 20 | + } |
| 21 | + Samlr::Tools::ResponseBuilder.build(options) |
| 22 | + end |
| 23 | + let(:xml_metadata_doc) do |
| 24 | + options = { |
| 25 | + :entity_id => "https://sp.example.com/saml2", |
| 26 | + :name_identity_format => "identity_format", |
| 27 | + :consumer_service_url => "https://support.sp.example.com/", |
| 28 | + :sign_metadata => true, |
| 29 | + metadata_id: shared_id, |
| 30 | + certificate: TEST_CERTIFICATE |
| 31 | + } |
| 32 | + Samlr::Tools::MetadataBuilder.build(options) |
| 33 | + end |
| 34 | + |
| 35 | + let(:fingerprint) { Samlr::Certificate.new(TEST_CERTIFICATE.x509).fingerprint.value } |
| 36 | + let(:saml_response) { Samlr::Response.new(Base64.encode64(xml_response_doc), fingerprint: fingerprint) } |
| 37 | + |
| 38 | + it "succeeds" do |
| 39 | + metadata_doc = Nokogiri::XML(xml_metadata_doc) |
| 40 | + response_doc = Nokogiri::XML(xml_response_doc) |
| 41 | + |
| 42 | + metadata_signature_doc = metadata_doc.xpath("md:EntityDescriptor/ds:Signature", Samlr::NS_MAP).first |
| 43 | + metadata_entity_descriptor_doc = metadata_doc.xpath("md:EntityDescriptor", Samlr::NS_MAP).first |
| 44 | + |
| 45 | + assertion_doc = response_doc.xpath("/samlp:Response/saml:Assertion", Samlr::NS_MAP).first |
| 46 | + assertion_doc.xpath("saml:Subject/saml:NameID").first.content = "[email protected]" |
| 47 | + assertion_doc.xpath("saml:Subject/saml:SubjectConfirmation/saml:SubjectConfirmationData", Samlr::NS_MAP).first.add_child(metadata_entity_descriptor_doc.dup) |
| 48 | + response_doc.at("/samlp:Response/saml:Issuer", Samlr::NS_MAP).add_next_sibling(metadata_signature_doc.dup) |
| 49 | + |
| 50 | + crafted_saml_response = Samlr::Response.new(Base64.encode64(response_doc.to_xml), fingerprint: fingerprint) |
| 51 | + error = assert_raises(Samlr::SignatureError) { crafted_saml_response.verify! } |
| 52 | + assert_equal "Expected 1 element with id #{shared_id}, found 2", error.details |
| 53 | + end |
| 54 | + end |
| 55 | +end |
0 commit comments