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