|
5 | 5 | class MetadataTest < Minitest::Test |
6 | 6 |
|
7 | 7 | describe 'Metadata' do |
8 | | - def setup |
9 | | - @settings = OneLogin::RubySaml::Settings.new |
10 | | - @settings.issuer = "https://example.com" |
11 | | - @settings.name_identifier_format = "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress" |
12 | | - @settings.assertion_consumer_service_url = "https://foo.example/saml/consume" |
13 | | - @settings.security[:authn_requests_signed] = false |
| 8 | + let(:settings) { OneLogin::RubySaml::Settings.new } |
| 9 | + let(:xml_text) { OneLogin::RubySaml::Metadata.new.generate(settings, false) } |
| 10 | + let(:xml_doc) { REXML::Document.new(xml_text) } |
| 11 | + let(:spsso_descriptor) { REXML::XPath.first(xml_doc, "//md:SPSSODescriptor") } |
| 12 | + let(:acs) { REXML::XPath.first(xml_doc, "//md:AssertionConsumerService") } |
| 13 | + |
| 14 | + before do |
| 15 | + settings.issuer = "https://example.com" |
| 16 | + settings.name_identifier_format = "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress" |
| 17 | + settings.assertion_consumer_service_url = "https://foo.example/saml/consume" |
14 | 18 | end |
15 | 19 |
|
16 | | - it "generates Service Provider Metadata with X509Certificate" do |
17 | | - @settings.security[:authn_requests_signed] = true |
18 | | - @settings.certificate = ruby_saml_cert_text |
| 20 | + it "generates Pretty Print Service Provider Metadata" do |
| 21 | + xml_text = OneLogin::RubySaml::Metadata.new.generate(settings, true) |
| 22 | + # assert correct xml declaration |
| 23 | + start = "<?xml version='1.0' encoding='UTF-8'?>\n<md:EntityDescriptor" |
| 24 | + assert_equal xml_text[0..start.length-1],start |
19 | 25 |
|
20 | | - xml_text = OneLogin::RubySaml::Metadata.new.generate(@settings) |
| 26 | + assert_equal "https://example.com", REXML::XPath.first(xml_doc, "//md:EntityDescriptor").attribute("entityID").value |
21 | 27 |
|
22 | | - # assert xml_text can be parsed into an xml doc |
23 | | - xml_doc = REXML::Document.new(xml_text) |
| 28 | + assert_equal "urn:oasis:names:tc:SAML:2.0:protocol", spsso_descriptor.attribute("protocolSupportEnumeration").value |
| 29 | + assert_equal "false", spsso_descriptor.attribute("AuthnRequestsSigned").value |
| 30 | + assert_equal "false", spsso_descriptor.attribute("WantAssertionsSigned").value |
24 | 31 |
|
25 | | - spsso_descriptor = REXML::XPath.first(xml_doc, "//md:SPSSODescriptor") |
26 | | - assert_equal "true", spsso_descriptor.attribute("AuthnRequestsSigned").value |
| 32 | + assert_equal "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress", REXML::XPath.first(xml_doc, "//md:NameIDFormat").text.strip |
27 | 33 |
|
28 | | - cert_node = REXML::XPath.first(xml_doc, "//md:KeyDescriptor/ds:KeyInfo/ds:X509Data/ds:X509Certificate", { |
29 | | - "md" => "urn:oasis:names:tc:SAML:2.0:metadata", |
30 | | - "ds" => "http://www.w3.org/2000/09/xmldsig#" |
31 | | - }) |
32 | | - cert_text = cert_node.text |
33 | | - cert = OpenSSL::X509::Certificate.new(Base64.decode64(cert_text)) |
34 | | - assert_equal ruby_saml_cert.to_der, cert.to_der |
| 34 | + assert_equal "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST", acs.attribute("Binding").value |
| 35 | + assert_equal "https://foo.example/saml/consume", acs.attribute("Location").value |
35 | 36 | end |
36 | 37 |
|
37 | 38 | it "generates Service Provider Metadata" do |
38 | | - settings = OneLogin::RubySaml::Settings.new |
39 | | - settings.issuer = "https://example.com" |
40 | | - settings.name_identifier_format = "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress" |
41 | | - settings.assertion_consumer_service_url = "https://foo.example/saml/consume" |
42 | | - settings.security[:authn_requests_signed] = false |
43 | | - |
44 | | - xml_text = OneLogin::RubySaml::Metadata.new.generate(settings) |
45 | | - |
46 | 39 | # assert correct xml declaration |
47 | | - start = "<?xml version='1.0' encoding='UTF-8'?>\n<md:EntityDescriptor" |
48 | | - assert xml_text[0..start.length-1] == start |
49 | | - |
50 | | - # assert xml_text can be parsed into an xml doc |
51 | | - xml_doc = REXML::Document.new(xml_text) |
| 40 | + start = "<?xml version='1.0' encoding='UTF-8'?><md:EntityDescriptor" |
| 41 | + assert_equal xml_text[0..start.length-1], start |
52 | 42 |
|
53 | 43 | assert_equal "https://example.com", REXML::XPath.first(xml_doc, "//md:EntityDescriptor").attribute("entityID").value |
54 | 44 |
|
55 | | - spsso_descriptor = REXML::XPath.first(xml_doc, "//md:SPSSODescriptor") |
56 | 45 | assert_equal "urn:oasis:names:tc:SAML:2.0:protocol", spsso_descriptor.attribute("protocolSupportEnumeration").value |
57 | 46 | assert_equal "false", spsso_descriptor.attribute("AuthnRequestsSigned").value |
58 | 47 | assert_equal "false", spsso_descriptor.attribute("WantAssertionsSigned").value |
59 | 48 |
|
60 | 49 | assert_equal "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress", REXML::XPath.first(xml_doc, "//md:NameIDFormat").text.strip |
61 | 50 |
|
62 | | - acs = REXML::XPath.first(xml_doc, "//md:AssertionConsumerService") |
63 | 51 | assert_equal "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST", acs.attribute("Binding").value |
64 | 52 | assert_equal "https://foo.example/saml/consume", acs.attribute("Location").value |
65 | 53 | end |
66 | 54 |
|
67 | | - it "generates attribute service if configured" do |
68 | | - settings = OneLogin::RubySaml::Settings.new |
69 | | - settings.issuer = "https://example.com" |
70 | | - settings.name_identifier_format = "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress" |
71 | | - settings.assertion_consumer_service_url = "https://foo.example/saml/consume" |
72 | | - settings.attribute_consuming_service.configure do |
73 | | - service_name "Test Service" |
74 | | - add_attribute(:name => "Name", :name_format => "Name Format", :friendly_name => "Friendly Name", :attribute_value => "Attribute Value") |
| 55 | + describe "when auth requests are signed" do |
| 56 | + let(:cert_node) do |
| 57 | + REXML::XPath.first( |
| 58 | + xml_doc, |
| 59 | + "//md:KeyDescriptor/ds:KeyInfo/ds:X509Data/ds:X509Certificate", |
| 60 | + "md" => "urn:oasis:names:tc:SAML:2.0:metadata", |
| 61 | + "ds" => "http://www.w3.org/2000/09/xmldsig#" |
| 62 | + ) |
| 63 | + end |
| 64 | + let(:cert) { OpenSSL::X509::Certificate.new(Base64.decode64(cert_node.text)) } |
| 65 | + |
| 66 | + before do |
| 67 | + settings.security[:authn_requests_signed] = true |
| 68 | + settings.certificate = ruby_saml_cert_text |
75 | 69 | end |
76 | 70 |
|
77 | | - xml_text = OneLogin::RubySaml::Metadata.new.generate(settings) |
78 | | - xml_doc = REXML::Document.new(xml_text) |
79 | | - acs = REXML::XPath.first(xml_doc, "//md:AttributeConsumingService") |
80 | | - assert_equal "true", acs.attribute("isDefault").value |
81 | | - assert_equal "1", acs.attribute("index").value |
82 | | - assert_equal REXML::XPath.first(xml_doc, "//md:ServiceName").text.strip, "Test Service" |
83 | | - req_attr = REXML::XPath.first(xml_doc, "//md:RequestedAttribute") |
84 | | - assert_equal "Name", req_attr.attribute("Name").value |
85 | | - assert_equal "Name Format", req_attr.attribute("NameFormat").value |
86 | | - assert_equal "Friendly Name", req_attr.attribute("FriendlyName").value |
87 | | - assert_equal "Attribute Value", REXML::XPath.first(xml_doc, "//md:AttributeValue").text.strip |
| 71 | + it "generates Service Provider Metadata with X509Certificate" do |
| 72 | + assert_equal "true", spsso_descriptor.attribute("AuthnRequestsSigned").value |
| 73 | + assert_equal ruby_saml_cert.to_der, cert.to_der |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + describe "when attribute service is configured" do |
| 78 | + let(:attr_svc) { REXML::XPath.first(xml_doc, "//md:AttributeConsumingService") } |
| 79 | + let(:req_attr) { REXML::XPath.first(xml_doc, "//md:RequestedAttribute") } |
| 80 | + |
| 81 | + before do |
| 82 | + settings.attribute_consuming_service.configure do |
| 83 | + service_name "Test Service" |
| 84 | + add_attribute(:name => "Name", :name_format => "Name Format", :friendly_name => "Friendly Name", :attribute_value => "Attribute Value") |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + it "generates attribute service" do |
| 89 | + assert_equal "true", attr_svc.attribute("isDefault").value |
| 90 | + assert_equal "1", attr_svc.attribute("index").value |
| 91 | + assert_equal REXML::XPath.first(xml_doc, "//md:ServiceName").text.strip, "Test Service" |
| 92 | + |
| 93 | + assert_equal "Name", req_attr.attribute("Name").value |
| 94 | + assert_equal "Name Format", req_attr.attribute("NameFormat").value |
| 95 | + assert_equal "Friendly Name", req_attr.attribute("FriendlyName").value |
| 96 | + assert_equal "Attribute Value", REXML::XPath.first(xml_doc, "//md:AttributeValue").text.strip |
| 97 | + end |
| 98 | + end |
| 99 | + |
| 100 | + describe "when the settings indicate to sign (embedded) metadata" do |
| 101 | + before do |
| 102 | + settings.security[:metadata_signed] = true |
| 103 | + settings.certificate = ruby_saml_cert_text |
| 104 | + settings.private_key = ruby_saml_key_text |
| 105 | + end |
| 106 | + |
| 107 | + it "creates a signed metadata" do |
| 108 | + assert_match %r[<ds:SignatureValue>([a-zA-Z0-9/+=]+)</ds:SignatureValue>]m, xml_text |
| 109 | + assert_match %r[<ds:SignatureMethod Algorithm='http://www.w3.org/2000/09/xmldsig#rsa-sha1'/>], xml_text |
| 110 | + assert_match %r[<ds:DigestMethod Algorithm='http://www.w3.org/2000/09/xmldsig#sha1'/>], xml_text |
| 111 | + signed_metadata = XMLSecurity::SignedDocument.new(xml_text) |
| 112 | + assert signed_metadata.validate_document(ruby_saml_cert_fingerprint, false) |
| 113 | + end |
| 114 | + |
| 115 | + describe "when digest and signature methods are specified" do |
| 116 | + before do |
| 117 | + settings.security[:signature_method] = XMLSecurity::Document::RSA_SHA256 |
| 118 | + settings.security[:digest_method] = XMLSecurity::Document::SHA512 |
| 119 | + end |
| 120 | + |
| 121 | + it "creates a signed metadata with specified digest and signature methods" do |
| 122 | + assert_match %r[<ds:SignatureValue>([a-zA-Z0-9/+=]+)</ds:SignatureValue>]m, xml_text |
| 123 | + assert_match %r[<ds:SignatureMethod Algorithm='http://www.w3.org/2001/04/xmldsig-more#rsa-sha256'/>], xml_text |
| 124 | + assert_match %r[<ds:DigestMethod Algorithm='http://www.w3.org/2001/04/xmldsig-more#sha512'/>], xml_text |
| 125 | + |
| 126 | + signed_metadata_2 = XMLSecurity::SignedDocument.new(xml_text) |
| 127 | + |
| 128 | + assert signed_metadata_2.validate_document(ruby_saml_cert_fingerprint, false) |
| 129 | + end |
| 130 | + end |
88 | 131 | end |
89 | 132 | end |
90 | 133 | end |
0 commit comments