Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Add metadata spec

Oliver Günther7727641d14a0
diff --git a/modules/auth_saml/app/models/saml/provider/hash_builder.rb b/modules/auth_saml/app/models/saml/provider/hash_builder.rb
index 574556356a8..926bdcee4ee 100644
--- a/modules/auth_saml/app/models/saml/provider/hash_builder.rb
+++ b/modules/auth_saml/app/models/saml/provider/hash_builder.rb
@@ -18,10 +18,10 @@ module Saml
def formatted_request_attributes
[
- { name: requested_login_attribute, format: requested_login_format, friendly_name: "Login" },
- { name: requested_mail_attribute, format: requested_mail_format, friendly_name: "Email" },
- { name: requested_firstname_attribute, format: requested_firstname_format, friendly_name: "First Name" },
- { name: requested_lastname_attribute, format: requested_lastname_format, friendly_name: "Last Name" }
+ { name: requested_login_attribute, name_format: requested_login_format, friendly_name: "Login" },
+ { name: requested_mail_attribute, name_format: requested_mail_format, friendly_name: "Email" },
+ { name: requested_firstname_attribute, name_format: requested_firstname_format, friendly_name: "First Name" },
+ { name: requested_lastname_attribute, name_format: requested_lastname_format, friendly_name: "Last Name" }
]
end
diff --git a/modules/auth_saml/spec/factories/saml_provider_factory.rb b/modules/auth_saml/spec/factories/saml_provider_factory.rb
index 4b9e5d4be6c..a53c2c6857c 100644
--- a/modules/auth_saml/spec/factories/saml_provider_factory.rb
+++ b/modules/auth_saml/spec/factories/saml_provider_factory.rb
@@ -47,5 +47,26 @@ FactoryBot.define do
mapping_mail { Saml::Defaults::MAIL_MAPPING }
mapping_firstname { Saml::Defaults::FIRSTNAME_MAPPING }
mapping_lastname { Saml::Defaults::LASTNAME_MAPPING }
+
+ trait :with_requested_attributes do
+ requested_mail_attribute { Saml::Defaults::MAIL_MAPPING.split("\n").first.strip }
+ requested_login_attribute { Saml::Defaults::MAIL_MAPPING.split("\n").first.strip }
+ requested_firstname_attribute { Saml::Defaults::FIRSTNAME_MAPPING.split("\n").first.strip }
+ requested_lastname_attribute { Saml::Defaults::LASTNAME_MAPPING.split("\n").first.strip }
+ requested_login_format { Saml::Defaults::ATTRIBUTE_FORMATS.first }
+ requested_mail_format { Saml::Defaults::ATTRIBUTE_FORMATS.first }
+ requested_firstname_format { Saml::Defaults::ATTRIBUTE_FORMATS.first }
+ requested_lastname_format { Saml::Defaults::ATTRIBUTE_FORMATS.first }
+ end
+
+ trait :with_encryption do
+ certificate { CertificateHelper.valid_certificate.to_pem }
+ private_key { CertificateHelper.private_key.to_pem }
+ authn_requests_signed { true }
+ want_assertions_signed { true }
+ want_assertions_encrypted { true }
+ digest_method { Saml::Defaults::DIGEST_METHODS["SHA-256"] }
+ signature_method { Saml::Defaults::SIGNATURE_METHODS["RSA SHA-256"] }
+ end
end
end
diff --git a/modules/auth_saml/spec/requests/saml_metadata_endpoint_spec.rb b/modules/auth_saml/spec/requests/saml_metadata_endpoint_spec.rb
new file mode 100644
index 00000000000..eeab93f846e
--- /dev/null
+++ b/modules/auth_saml/spec/requests/saml_metadata_endpoint_spec.rb
@@ -0,0 +1,116 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require "spec_helper"
+require "rack/test"
+
+RSpec.describe "SAML metadata endpoint", with_ee: %i[sso_auth_providers] do
+ include Rack::Test::Methods
+ include API::V3::Utilities::PathHelper
+
+ subject do
+ temp = Nokogiri::XML(last_response.body)
+
+ # The ds prefix is not defined on root,
+ # which Nokogiri complains about
+ temp.root["xmlns:ds"] = "http://www.w3.org/2000/09/xmldsig#"
+
+ Nokogiri::XML(temp.to_xml(save_with: Nokogiri::XML::Node::SaveOptions::AS_XML))
+ end
+
+ before do
+ provider
+ get "/auth/saml/metadata"
+ end
+
+ context "with basic provider" do
+ let(:provider) do
+ create(:saml_provider, slug: "saml")
+ end
+
+ it "returns the metadata" do
+ expect(last_response).to be_successful
+ expect(subject.at_xpath("//md:EntityDescriptor")["entityID"]).to eq "http://test.host"
+
+ sso = subject.at_xpath("//md:SPSSODescriptor")
+ expect(sso["AuthnRequestsSigned"]).to eq "false"
+ expect(sso["WantAssertionsSigned"]).to eq "false"
+
+ consumer = sso.at_xpath("//md:AssertionConsumerService")
+ expect(consumer["Binding"]).to eq "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"
+ expect(consumer["Location"]).to eq "http://test.host/auth/saml/callback"
+ end
+ end
+
+ context "with elaborate provider" do
+ let(:provider) do
+ create(:saml_provider,
+ :with_encryption,
+ :with_requested_attributes,
+ slug: "saml")
+ end
+
+ it "returns the metadata", :aggregate_failures do # rubocop:disable RSpec/ExampleLength
+ expect(last_response).to be_successful
+ expect(subject.at_xpath("//md:EntityDescriptor")["entityID"]).to eq "http://test.host"
+
+ sso = subject.at_xpath("//md:SPSSODescriptor")
+ expect(sso["AuthnRequestsSigned"]).to eq "true"
+ expect(sso["WantAssertionsSigned"]).to eq "true"
+
+ # Expect signature present
+ signature = subject.at_xpath("//ds:Signature")
+ expect(signature.at_xpath("//ds:SignatureMethod")["Algorithm"]).to eq "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"
+ expect(signature.at_xpath("//ds:DigestMethod")["Algorithm"]).to eq "http://www.w3.org/2001/04/xmlenc#sha256"
+
+ expect(signature.at_xpath("//ds:DigestValue")).to be_present
+
+ signing = signature.at_xpath("//md:KeyDescriptor[@use='signing']/ds:KeyInfo/ds:X509Data/ds:X509Certificate").text
+ expect(signing).to eq CertificateHelper.non_padded_string(:valid_certificate)
+
+ encryption = signature.at_xpath("//md:KeyDescriptor[@use='encryption']/ds:KeyInfo/ds:X509Data/ds:X509Certificate").text
+ expect(encryption).to eq CertificateHelper.non_padded_string(:valid_certificate)
+
+ consumer = sso.at_xpath("//md:AssertionConsumerService")
+ expect(consumer["Binding"]).to eq "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"
+ expect(consumer["Location"]).to eq "http://test.host/auth/saml/callback"
+
+ consuming = consumer.at_xpath("//md:AttributeConsumingService")
+ requested = consuming.xpath("md:RequestedAttribute")
+ attributes = requested.map { |x| [x["FriendlyName"], x["Name"]] }
+ expect(attributes).to contain_exactly ["Login", "mail"],
+ ["First Name", "givenName"],
+ ["Last Name", "sn"],
+ ["Email", "mail"]
+
+ requested.each do |attr|
+ expect(attr["NameFormat"]).to eq "urn:oasis:names:tc:SAML:2.0:attrname-format:basic"
+ end
+ end
+ end
+end
diff --git a/modules/auth_saml/spec/support/certificate_helper.rb b/modules/auth_saml/spec/support/certificate_helper.rb
index 19d0ce58634..85bf1efe6f2 100644
--- a/modules/auth_saml/spec/support/certificate_helper.rb
+++ b/modules/auth_saml/spec/support/certificate_helper.rb
@@ -10,6 +10,7 @@ module CertificateHelper
.to_pem
.gsub("-----BEGIN CERTIFICATE-----", "")
.gsub("-----END CERTIFICATE-----", "")
+ .delete("\n")
.strip
end