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