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

Extract mapper, spec

Oliver Günther28c379530dc9
diff --git a/modules/auth_saml/app/seeders/env_data/saml/provider_seeder.rb b/modules/auth_saml/app/seeders/env_data/saml/provider_seeder.rb
index 08e0a116740..937a3747757 100644
--- a/modules/auth_saml/app/seeders/env_data/saml/provider_seeder.rb
+++ b/modules/auth_saml/app/seeders/env_data/saml/provider_seeder.rb
@@ -32,13 +32,8 @@ module EnvData
Setting.seed_saml_provider.each do |name, options|
print_status " ↳ Creating or Updating SAML provider #{name}" do
provider = ::Saml::Provider.find_by(slug: name)
- options = mapped_options(options)
- params = {
- slug: name,
- display_name: options.delete("display_name") || "SAML",
- available: true,
- options:
- }
+ params = ::Saml::ConfigurationMapper.new(options).call!
+ params["options"]["seeded_from_env"] = true
if provider
print_status " - Updating existing SAML provider '#{name}' from ENV"
@@ -72,54 +67,6 @@ module EnvData
.on_success { print_status " - Successfully updated SAML provider #{name}." }
.on_failure { |call| raise "Failed to update SAML provider: #{call.message}" }
end
-
- def mapped_options(options)
- options["seeded_from_env"] = true
- options["idp_sso_service_url"] ||= options.delete("idp_sso_target_url")
- options["idp_slo_service_url"] ||= options.delete("idp_slo_target_url")
- options["sp_entity_id"] ||= options.delete("issuer")
-
- build_idp_cert(options)
- extract_security_options(options)
- extract_mapping(options)
-
- options.compact
- end
-
- def extract_mapping(options)
- nil unless options["attribute_statements"]
-
- options["mapping_login"] = extract_mapping_attribute(options, "login")
- options["mapping_mail"] = extract_mapping_attribute(options, "email")
- options["mapping_firstname"] = extract_mapping_attribute(options, "first_name")
- options["mapping_lastname"] = extract_mapping_attribute(options, "last_name")
- options["mapping_uid"] = extract_mapping_attribute(options, "uid")
- end
-
- def extract_mapping_attribute(options, key)
- value = options["attribute_statements"][key]
-
- if value.present?
- Array(value).join("\n")
- end
- end
-
- def build_idp_cert(options)
- if options["idp_cert"]
- options["idp_cert"] = OneLogin::RubySaml::Utils.format_cert(options["idp_cert"])
- elsif options["idp_cert_multi"]
- options["idp_cert"] = options["idp_cert_multi"]["signing"]
- .map { |cert| OneLogin::RubySaml::Utils.format_cert(cert) }
- .join("\n")
- end
- end
-
- def extract_security_options(options)
- return unless options["security"]
-
- options.merge! options["security"].slice("authn_requests_signed", "want_assertions_signed",
- "want_assertions_encrypted", "digest_method", "signature_method")
- end
end
end
end
diff --git a/modules/auth_saml/app/services/saml/configuration_mapper.rb b/modules/auth_saml/app/services/saml/configuration_mapper.rb
new file mode 100644
index 00000000000..74874554698
--- /dev/null
+++ b/modules/auth_saml/app/services/saml/configuration_mapper.rb
@@ -0,0 +1,95 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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.
+#++
+
+module Saml
+ class ConfigurationMapper
+ attr_reader :configuration
+
+ def initialize(configuration)
+ @configuration = configuration
+ end
+
+ def call!
+ options = mapped_options(configuration.deep_stringify_keys)
+ {
+ "options" => options,
+ "slug" => options.delete("name"),
+ "display_name" => options.delete("display_name") || "SAML"
+ }
+ end
+
+ private
+
+ def mapped_options(options)
+ options["idp_sso_service_url"] ||= options.delete("idp_sso_target_url")
+ options["idp_slo_service_url"] ||= options.delete("idp_slo_target_url")
+ options["sp_entity_id"] ||= options.delete("issuer")
+
+ build_idp_cert(options)
+ extract_security_options(options)
+ extract_mapping(options)
+
+ options.compact
+ end
+
+ def extract_mapping(options)
+ return unless options["attribute_statements"]
+
+ options["mapping_login"] = extract_mapping_attribute(options, "login")
+ options["mapping_mail"] = extract_mapping_attribute(options, "email")
+ options["mapping_firstname"] = extract_mapping_attribute(options, "first_name")
+ options["mapping_lastname"] = extract_mapping_attribute(options, "last_name")
+ options["mapping_uid"] = extract_mapping_attribute(options, "uid")
+ end
+
+ def extract_mapping_attribute(options, key)
+ value = options["attribute_statements"][key]
+
+ if value.present?
+ Array(value).join("\n")
+ end
+ end
+
+ def build_idp_cert(options)
+ if options["idp_cert"]
+ options["idp_cert"] = OneLogin::RubySaml::Utils.format_cert(options["idp_cert"])
+ elsif options["idp_cert_multi"]
+ options["idp_cert"] = options["idp_cert_multi"]["signing"]
+ .map { |cert| OneLogin::RubySaml::Utils.format_cert(cert) }
+ .join("\n")
+ end
+ end
+
+ def extract_security_options(options)
+ return unless options["security"]
+
+ options.merge! options["security"].slice("authn_requests_signed", "want_assertions_signed",
+ "want_assertions_encrypted", "digest_method", "signature_method")
+ end
+ end
+end
diff --git a/modules/auth_saml/spec/fixtures/idp_cert_plain.txt b/modules/auth_saml/spec/fixtures/idp_cert_plain.txt
new file mode 100644
index 00000000000..bb5ce9674d6
--- /dev/null
+++ b/modules/auth_saml/spec/fixtures/idp_cert_plain.txt
@@ -0,0 +1 @@
+MIIDHTCCAgWgAwIBAgIJaPVOFuER4IyxMA0GCSqGSIb3DQEBCwUAMCwxKjAoBgNVBAMTIWRldi10eXQzaDZkNTZjeTB4aXA3LnVzLmF1dGgwLmNvbTAeFw0yMzA0MjUwODU0MThaFw0zNzAxMDEwODU0MThaMCwxKjAoBgNVBAMTIWRldi10eXQzaDZkNTZjeTB4aXA3LnVzLmF1dGgwLmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAK/2idoD9sJDzYukmOCVC2Qp4nLm2WGkIbdAudSDzb3hEDVTebAUXeDUqi9nQMtXjS5lvXYVQMIHdq1yRLGciSBviO+qT/FGNRm/VncpWYt0mVvf6t/9Y1PJ1krFlo/72FN3VBEClqAccVryPq+pjwswZMWbgBi3BsMdi+HYW2mgPRKeAemaEYcPwPyUpCBiVQo/6cCSlBB+Kigyp33LAJZVakwNA5pxEfnhUwifiyBzO+xH5m4ol8mcDznsmiBYddxqVQ7qaYds4HSpwSzArdxLnlFj1nVwusJyGsjZD4pCMZuoWXLGPs4ldBgzLH5Dq6UtsCKPZGhRzYA4Au7qVGcCAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUrh9OF0EmZv3h3BmcuCF05qLDYokwDgYDVR0PAQH/BAQDAgKEMA0GCSqGSIb3DQEBCwUAA4IBAQAoo/z1LTjEmGrm9ilObVDzPSWlvOqDq1lkP8nrROpYkghDPxXQl+8nJ4J0gKwDP6BgTjWzhMk8MyoeCn8wZGI7oBOeBPOeJCbc6WYW2HGvtc86nO3JHHdvDYMO3HbR9LFoxBXhF95nWwgmPd+9uxdHrdfQ1CIXasATWToQgud6rkCgMcNP25LFds36iMCzXDxKiYvn54JaXmGo567U1Rx6O4AwnCG1RHTdtx/CdiIuPAOBl/xUnnsVeCPQgqNl53/6Qtmk9kg15GeeFDsjJoHoZuLZANaGMUBLRMzLU4al3/SRgPJLaEq46BaRq/oX0WvWlGiVegHNWjpOJ5ZSEeS5
diff --git a/modules/auth_saml/spec/seeders/env_data/saml/provider_seeder_spec.rb b/modules/auth_saml/spec/seeders/env_data/saml/provider_seeder_spec.rb
new file mode 100644
index 00000000000..42e039df5de
--- /dev/null
+++ b/modules/auth_saml/spec/seeders/env_data/saml/provider_seeder_spec.rb
@@ -0,0 +1,149 @@
+# frozen_string_literal: true
+
+#-- 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"
+
+RSpec.describe EnvData::Saml::ProviderSeeder, :settings_reset do
+ let(:seed_data) { Source::SeedData.new({}) }
+
+ subject(:seeder) { described_class.new(seed_data) }
+
+ before do
+ reset(:seed_saml_provider,
+ description: "Provide a SAML provider and sync its settings through ENV",
+ env_alias: "OPENPROJECT_SAML",
+ writable: false,
+ default: nil,
+ format: :hash)
+ end
+
+ context "when not provided" do
+ it "does nothing" do
+ expect { seeder.seed! }.not_to change(Saml::Provider, :count)
+ end
+ end
+
+ context "when providing seed variables",
+ with_env: {
+ OPENPROJECT_SAML_SAML_NAME: "saml",
+ OPENPROJECT_SAML_SAML_DISPLAY__NAME: "Test SAML",
+ OPENPROJECT_SAML_SAML_ASSERTION__CONSUMER__SERVICE__URL: "some wrong value",
+ OPENPROJECT_SAML_SAML_ASSERTION__CONSUMER__SERVICE__BINDING: "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect",
+ OPENPROJECT_SAML_SAML_SP__ENTITY__ID: "http://localhost:3000",
+ OPENPROJECT_SAML_SAML_IDP__CERT: "MIIDHTCCAgWgAwIBAgIJaPVOFuER4IyxMA0GCSqGSIb3DQEBCwUAMCwxKjAoBgNVBAMTIWRldi10eXQzaDZkNTZjeTB4aXA3LnVzLmF1dGgwLmNvbTAeFw0yMzA0MjUwODU0MThaFw0zNzAxMDEwODU0MThaMCwxKjAoBgNVBAMTIWRldi10eXQzaDZkNTZjeTB4aXA3LnVzLmF1dGgwLmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAK/2idoD9sJDzYukmOCVC2Qp4nLm2WGkIbdAudSDzb3hEDVTebAUXeDUqi9nQMtXjS5lvXYVQMIHdq1yRLGciSBviO+qT/FGNRm/VncpWYt0mVvf6t/9Y1PJ1krFlo/72FN3VBEClqAccVryPq+pjwswZMWbgBi3BsMdi+HYW2mgPRKeAemaEYcPwPyUpCBiVQo/6cCSlBB+Kigyp33LAJZVakwNA5pxEfnhUwifiyBzO+xH5m4ol8mcDznsmiBYddxqVQ7qaYds4HSpwSzArdxLnlFj1nVwusJyGsjZD4pCMZuoWXLGPs4ldBgzLH5Dq6UtsCKPZGhRzYA4Au7qVGcCAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUrh9OF0EmZv3h3BmcuCF05qLDYokwDgYDVR0PAQH/BAQDAgKEMA0GCSqGSIb3DQEBCwUAA4IBAQAoo/z1LTjEmGrm9ilObVDzPSWlvOqDq1lkP8nrROpYkghDPxXQl+8nJ4J0gKwDP6BgTjWzhMk8MyoeCn8wZGI7oBOeBPOeJCbc6WYW2HGvtc86nO3JHHdvDYMO3HbR9LFoxBXhF95nWwgmPd+9uxdHrdfQ1CIXasATWToQgud6rkCgMcNP25LFds36iMCzXDxKiYvn54JaXmGo567U1Rx6O4AwnCG1RHTdtx/CdiIuPAOBl/xUnnsVeCPQgqNl53/6Qtmk9kg15GeeFDsjJoHoZuLZANaGMUBLRMzLU4al3/SRgPJLaEq46BaRq/oX0WvWlGiVegHNWjpOJ5ZSEeS5",
+ OPENPROJECT_SAML_SAML_IDP__SSO__TARGET__URL: "https://saml.example.com/samlp/tPLrXaiBZhLNtzBMN8oNFQAzhFdGlUPK",
+ OPENPROJECT_SAML_SAML_IDP__SLO__TARGET__URL: "https://saml.example.com/samlp/tPLrXaiBZhLNtzBMN8oNFQAzhFdGlUPK/logout",
+ OPENPROJECT_SAML_SAML_ATTRIBUTE__STATEMENTS_EMAIL: "['mail', 'urn:oid:2.5.4.42', 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress']",
+ OPENPROJECT_SAML_SAML_ATTRIBUTE__STATEMENTS_LOGIN: "['mail', 'urn:oid:2.5.4.42', 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress']",
+ OPENPROJECT_SAML_SAML_ATTRIBUTE__STATEMENTS_FIRST__NAME: "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname",
+ OPENPROJECT_SAML_SAML_ATTRIBUTE__STATEMENTS_LAST__NAME: "['urn:oid:2.5.4.4', 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname']"
+ } do
+ it "uses those variables" do
+ expect { seeder.seed! }.to change(Saml::Provider, :count).by(1)
+
+ provider = Saml::Provider.last
+ expect(provider.slug).to eq "saml"
+ expect(provider.display_name).to eq "Test SAML"
+ expect(provider.seeded_from_env).to be true
+
+ expect(provider.sp_entity_id).to eq "http://localhost:3000"
+ expect(provider.assertion_consumer_service_url).to eq "http://localhost:3000/auth/saml/callback"
+ expect(provider.idp_cert).to eq OneLogin::RubySaml::Utils.format_cert(ENV.fetch("OPENPROJECT_SAML_SAML_IDP__CERT"))
+
+ expect(provider.mapping_login).to eq "mail\nurn:oid:2.5.4.42\nhttp://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress"
+ expect(provider.mapping_mail).to eq "mail\nurn:oid:2.5.4.42\nhttp://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress"
+ expect(provider.mapping_firstname).to eq "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname"
+ expect(provider.mapping_lastname).to eq "urn:oid:2.5.4.4\nhttp://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname"
+ end
+
+ context "when provider already exists with that name" do
+ it "updates the provider" do
+ provider = Saml::Provider.create!(display_name: "Something", slug: "saml", mapping_mail: "old", creator: User.system)
+ expect(provider.seeded_from_env).to be_nil
+
+ expect { seeder.seed! }.not_to change(Saml::Provider, :count)
+
+ provider.reload
+
+ expect(provider.display_name).to eq "Test SAML"
+ expect(provider.seeded_from_env).to be true
+ expect(provider.mapping_mail).to eq "mail\nurn:oid:2.5.4.42\nhttp://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress"
+ end
+ end
+ end
+
+ context "when providing invalid variables",
+ with_env: {
+ OPENPROJECT_SAML_SAML_NAME: "saml",
+ OPENPROJECT_SAML_SAML_DISPLAY__NAME: "Test SAML",
+ OPENPROJECT_SAML_SAML_IDP__CERT: "invalid"
+ } do
+ it "raises an exception" do
+ expect { seeder.seed! }.to raise_error(/Idp cert is not a valid PEM-formatted certificate/)
+
+ expect(Saml::Provider.all).to be_empty
+ end
+ end
+
+ context "when providing multiple variables",
+ with_env: {
+ OPENPROJECT_SAML_SAML_NAME: "saml",
+ OPENPROJECT_SAML_SAML_DISPLAY__NAME: "Test SAML",
+ OPENPROJECT_SAML_SAML_ASSERTION__CONSUMER__SERVICE__URL: "some wrong value",
+ OPENPROJECT_SAML_SAML_ASSERTION__CONSUMER__SERVICE__BINDING: "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect",
+ OPENPROJECT_SAML_SAML_SP__ENTITY__ID: "http://localhost:3000",
+ OPENPROJECT_SAML_SAML_IDP__CERT: "MIIDHTCCAgWgAwIBAgIJaPVOFuER4IyxMA0GCSqGSIb3DQEBCwUAMCwxKjAoBgNVBAMTIWRldi10eXQzaDZkNTZjeTB4aXA3LnVzLmF1dGgwLmNvbTAeFw0yMzA0MjUwODU0MThaFw0zNzAxMDEwODU0MThaMCwxKjAoBgNVBAMTIWRldi10eXQzaDZkNTZjeTB4aXA3LnVzLmF1dGgwLmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAK/2idoD9sJDzYukmOCVC2Qp4nLm2WGkIbdAudSDzb3hEDVTebAUXeDUqi9nQMtXjS5lvXYVQMIHdq1yRLGciSBviO+qT/FGNRm/VncpWYt0mVvf6t/9Y1PJ1krFlo/72FN3VBEClqAccVryPq+pjwswZMWbgBi3BsMdi+HYW2mgPRKeAemaEYcPwPyUpCBiVQo/6cCSlBB+Kigyp33LAJZVakwNA5pxEfnhUwifiyBzO+xH5m4ol8mcDznsmiBYddxqVQ7qaYds4HSpwSzArdxLnlFj1nVwusJyGsjZD4pCMZuoWXLGPs4ldBgzLH5Dq6UtsCKPZGhRzYA4Au7qVGcCAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUrh9OF0EmZv3h3BmcuCF05qLDYokwDgYDVR0PAQH/BAQDAgKEMA0GCSqGSIb3DQEBCwUAA4IBAQAoo/z1LTjEmGrm9ilObVDzPSWlvOqDq1lkP8nrROpYkghDPxXQl+8nJ4J0gKwDP6BgTjWzhMk8MyoeCn8wZGI7oBOeBPOeJCbc6WYW2HGvtc86nO3JHHdvDYMO3HbR9LFoxBXhF95nWwgmPd+9uxdHrdfQ1CIXasATWToQgud6rkCgMcNP25LFds36iMCzXDxKiYvn54JaXmGo567U1Rx6O4AwnCG1RHTdtx/CdiIuPAOBl/xUnnsVeCPQgqNl53/6Qtmk9kg15GeeFDsjJoHoZuLZANaGMUBLRMzLU4al3/SRgPJLaEq46BaRq/oX0WvWlGiVegHNWjpOJ5ZSEeS5",
+ OPENPROJECT_SAML_SAML_IDP__SSO__TARGET__URL: "https://saml.example.com/samlp/tPLrXaiBZhLNtzBMN8oNFQAzhFdGlUPK",
+ OPENPROJECT_SAML_SAML_IDP__SLO__TARGET__URL: "https://saml.example.com/samlp/tPLrXaiBZhLNtzBMN8oNFQAzhFdGlUPK/logout",
+ OPENPROJECT_SAML_SAML_ATTRIBUTE__STATEMENTS_EMAIL: "['mail', 'urn:oid:2.5.4.42', 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress']",
+ OPENPROJECT_SAML_SAML_ATTRIBUTE__STATEMENTS_LOGIN: "['mail', 'urn:oid:2.5.4.42', 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress']",
+ OPENPROJECT_SAML_SAML_ATTRIBUTE__STATEMENTS_FIRST__NAME: "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname",
+ OPENPROJECT_SAML_SAML_ATTRIBUTE__STATEMENTS_LAST__NAME: "['urn:oid:2.5.4.4', 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname']",
+ OPENPROJECT_SAML_MYSAML_NAME: "mysaml",
+ OPENPROJECT_SAML_MYSAML_DISPLAY__NAME: "Another SAML",
+ OPENPROJECT_SAML_MYSAML_ASSERTION__CONSUMER__SERVICE__URL: "some wrong value",
+ OPENPROJECT_SAML_MYSAML_ASSERTION__CONSUMER__SERVICE__BINDING: "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect",
+ OPENPROJECT_SAML_MYSAML_SP__ENTITY__ID: "http://localhost:3000",
+ OPENPROJECT_SAML_MYSAML_IDP__CERT: "MIIDHTCCAgWgAwIBAgIJaPVOFuER4IyxMA0GCSqGSIb3DQEBCwUAMCwxKjAoBgNVBAMTIWRldi10eXQzaDZkNTZjeTB4aXA3LnVzLmF1dGgwLmNvbTAeFw0yMzA0MjUwODU0MThaFw0zNzAxMDEwODU0MThaMCwxKjAoBgNVBAMTIWRldi10eXQzaDZkNTZjeTB4aXA3LnVzLmF1dGgwLmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAK/2idoD9sJDzYukmOCVC2Qp4nLm2WGkIbdAudSDzb3hEDVTebAUXeDUqi9nQMtXjS5lvXYVQMIHdq1yRLGciSBviO+qT/FGNRm/VncpWYt0mVvf6t/9Y1PJ1krFlo/72FN3VBEClqAccVryPq+pjwswZMWbgBi3BsMdi+HYW2mgPRKeAemaEYcPwPyUpCBiVQo/6cCSlBB+Kigyp33LAJZVakwNA5pxEfnhUwifiyBzO+xH5m4ol8mcDznsmiBYddxqVQ7qaYds4HSpwSzArdxLnlFj1nVwusJyGsjZD4pCMZuoWXLGPs4ldBgzLH5Dq6UtsCKPZGhRzYA4Au7qVGcCAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUrh9OF0EmZv3h3BmcuCF05qLDYokwDgYDVR0PAQH/BAQDAgKEMA0GCSqGSIb3DQEBCwUAA4IBAQAoo/z1LTjEmGrm9ilObVDzPSWlvOqDq1lkP8nrROpYkghDPxXQl+8nJ4J0gKwDP6BgTjWzhMk8MyoeCn8wZGI7oBOeBPOeJCbc6WYW2HGvtc86nO3JHHdvDYMO3HbR9LFoxBXhF95nWwgmPd+9uxdHrdfQ1CIXasATWToQgud6rkCgMcNP25LFds36iMCzXDxKiYvn54JaXmGo567U1Rx6O4AwnCG1RHTdtx/CdiIuPAOBl/xUnnsVeCPQgqNl53/6Qtmk9kg15GeeFDsjJoHoZuLZANaGMUBLRMzLU4al3/SRgPJLaEq46BaRq/oX0WvWlGiVegHNWjpOJ5ZSEeS5",
+ OPENPROJECT_SAML_MYSAML_IDP__SSO__TARGET__URL: "https://saml.example.com/samlp/tPLrXaiBZhLNtzBMN8oNFQAzhFdGlUPK",
+ OPENPROJECT_SAML_MYSAML_IDP__SLO__TARGET__URL: "https://saml.example.com/samlp/tPLrXaiBZhLNtzBMN8oNFQAzhFdGlUPK/logout",
+ OPENPROJECT_SAML_MYSAML_ATTRIBUTE__STATEMENTS_EMAIL: "['mail', 'urn:oid:2.5.4.42', 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress']",
+ OPENPROJECT_SAML_MYSAML_ATTRIBUTE__STATEMENTS_LOGIN: "['mail', 'urn:oid:2.5.4.42', 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress']",
+ OPENPROJECT_SAML_MYSAML_ATTRIBUTE__STATEMENTS_FIRST__NAME: "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname",
+ OPENPROJECT_SAML_MYSAML_ATTRIBUTE__STATEMENTS_LAST__NAME: "['urn:oid:2.5.4.4', 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname']"
+ } do
+ it "creates both" do
+ expect { seeder.seed! }.to change(Saml::Provider, :count).by(2)
+
+ providers = Saml::Provider.pluck(:slug)
+ expect(providers).to contain_exactly("saml", "mysaml")
+ end
+ end
+end
diff --git a/modules/auth_saml/spec/services/saml/configuration_mapper_spec.rb b/modules/auth_saml/spec/services/saml/configuration_mapper_spec.rb
new file mode 100644
index 00000000000..6cf93f4af3f
--- /dev/null
+++ b/modules/auth_saml/spec/services/saml/configuration_mapper_spec.rb
@@ -0,0 +1,261 @@
+# frozen_string_literal: true
+
+#-- 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"
+
+RSpec.describe Saml::ConfigurationMapper, type: :model do
+ let(:instance) { described_class.new(configuration) }
+ let(:result) { instance.call! }
+
+ describe "display_name" do
+ subject { result["display_name"] }
+
+ context "when provided" do
+ let(:configuration) { { display_name: "My SAML Provider" } }
+
+ it { is_expected.to eq("My SAML Provider") }
+ end
+
+ context "when not provided" do
+ let(:configuration) { {} }
+
+ it { is_expected.to eq("SAML") }
+ end
+ end
+
+ describe "slug" do
+ subject { result["slug"] }
+
+ context "when provided from name" do
+ let(:configuration) { { name: "samlwat" } }
+
+ it { is_expected.to eq("samlwat") }
+ end
+
+ context "when not provided" do
+ let(:configuration) { {} }
+
+ it { is_expected.to be_nil }
+ end
+ end
+
+ describe "idp_sso_service_url" do
+ subject { result["options"] }
+
+ context "when provided" do
+ let(:configuration) { { idp_sso_service_url: "http://example.com" } }
+
+ it { is_expected.to include("idp_sso_service_url" => "http://example.com") }
+ end
+
+ context "when provided as legacy" do
+ let(:configuration) { { idp_sso_target_url: "http://example.com" } }
+
+ it { is_expected.to include("idp_sso_service_url" => "http://example.com") }
+ end
+ end
+
+ describe "idp_slo_service_url" do
+ subject { result["options"] }
+
+ context "when provided" do
+ let(:configuration) { { idp_slo_service_url: "http://example.com" } }
+
+ it { is_expected.to include("idp_slo_service_url" => "http://example.com") }
+ end
+
+ context "when provided as legacy" do
+ let(:configuration) { { idp_slo_target_url: "http://example.com" } }
+
+ it { is_expected.to include("idp_slo_service_url" => "http://example.com") }
+ end
+ end
+
+ describe "sp_entity_id" do
+ subject { result["options"] }
+
+ context "when provided" do
+ let(:configuration) { { sp_entity_id: "http://example.com" } }
+
+ it { is_expected.to include("sp_entity_id" => "http://example.com") }
+ end
+
+ context "when provided as legacy" do
+ let(:configuration) { { issuer: "http://example.com" } }
+
+ it { is_expected.to include("sp_entity_id" => "http://example.com") }
+ end
+ end
+
+ describe "idp_cert" do
+ let(:idp_cert) { File.read(Rails.root.join("modules/auth_saml/spec/fixtures/idp_cert_plain.txt").to_s) }
+
+ subject { result["options"] }
+
+ context "when provided as single" do
+ let(:configuration) do
+ { idp_cert: }
+ end
+
+ it "formats the certificate" do
+ expect(subject["idp_cert"]).to include("BEGIN CERTIFICATE")
+ expect(subject["idp_cert"]).to eq(OneLogin::RubySaml::Utils.format_cert(idp_cert))
+ end
+ end
+
+ context "when provided already formatted" do
+ let(:configuration) do
+ { idp_cert: OneLogin::RubySaml::Utils.format_cert(idp_cert) }
+ end
+
+ it "uses the certificate as is" do
+ expect(subject["idp_cert"]).to include("BEGIN CERTIFICATE")
+ expect(subject["idp_cert"]).to eq(OneLogin::RubySaml::Utils.format_cert(idp_cert))
+ end
+ end
+
+ context "when provided as multi" do
+ let(:configuration) do
+ {
+ idp_cert_multi: {
+ signing: [idp_cert, idp_cert]
+ }
+ }
+ end
+
+ it "formats the certificate" do
+ expect(subject["idp_cert"]).to include("BEGIN CERTIFICATE")
+ expect(subject["idp_cert"].scan("BEGIN CERTIFICATE").length).to eq(2)
+ formatted = OneLogin::RubySaml::Utils.format_cert(idp_cert)
+ expect(subject["idp_cert"]).to eq("#{formatted}\n#{formatted}")
+ end
+ end
+ end
+
+ describe "attribute mapping" do
+ let(:configuration) { { attribute_statements: } }
+
+ subject { result["options"] }
+
+ context "when provided" do
+ let(:attribute_statements) do
+ {
+ login: "uid",
+ email: %w[email mail],
+ first_name: "givenName",
+ last_name: "sn",
+ uid: "someInternalValue"
+ }
+ end
+
+ it "extracts the mappings" do
+ expect(subject["mapping_login"]).to eq "uid"
+ expect(subject["mapping_mail"]).to eq "email\nmail"
+ expect(subject["mapping_firstname"]).to eq "givenName"
+ expect(subject["mapping_lastname"]).to eq "sn"
+ expect(subject["mapping_uid"]).to eq "someInternalValue"
+ end
+ end
+
+ context "when partially provided" do
+ let(:attribute_statements) do
+ {
+ login: "uid",
+ email: "mail"
+ }
+ end
+
+ it "extracts the mappings" do
+ expect(subject["mapping_login"]).to eq "uid"
+ expect(subject["mapping_mail"]).to eq "mail"
+ expect(subject["mapping_firstname"]).to be_nil
+ expect(subject["mapping_lastname"]).to be_nil
+ expect(subject["mapping_uid"]).to be_nil
+ end
+ end
+
+ context "when not provided" do
+ let(:attribute_statements) { nil }
+
+ it "does not set any security options" do
+ expect(subject["mapping_login"]).to be_nil
+ expect(subject["mapping_mail"]).to be_nil
+ expect(subject["mapping_firstname"]).to be_nil
+ expect(subject["mapping_lastname"]).to be_nil
+ expect(subject["mapping_uid"]).to be_nil
+ end
+ end
+ end
+
+ describe "security" do
+ let(:configuration) { { security: } }
+
+ subject { result["options"] }
+
+ context "when provided" do
+ let(:security) do
+ {
+ authn_requests_signed: true,
+ want_assertions_signed: true,
+ want_assertions_encrypted: true,
+ digest_method: "http://www.w3.org/2001/04/xmlenc#sha256",
+ signature_method: "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256",
+ bogus_method: "wat"
+ }
+ end
+
+ it "extracts the security options" do
+ expect(subject).to include(security
+ .slice(:authn_requests_signed, :want_assertions_signed,
+ :want_assertions_encrypted, :digest_method, :signature_method)
+ .stringify_keys)
+
+ expect(subject["authn_requests_signed"]).to be true
+ expect(subject["want_assertions_signed"]).to be true
+ expect(subject["want_assertions_encrypted"]).to be true
+ expect(subject["digest_method"]).to eq("http://www.w3.org/2001/04/xmlenc#sha256")
+ expect(subject["signature_method"]).to eq("http://www.w3.org/2001/04/xmldsig-more#rsa-sha256")
+ expect(subject).not_to include("bogus_method")
+ end
+ end
+
+ context "when not provided" do
+ let(:security) { nil }
+
+ it "does not set any security options" do
+ expect(subject["authn_requests_signed"]).to be_nil
+ expect(subject["want_assertions_signed"]).to be_nil
+ expect(subject["want_assertions_encrypted"]).to be_nil
+ expect(subject["digest_method"]).to be_nil
+ expect(subject["signature_method"]).to be_nil
+ end
+ end
+ end
+end
diff --git a/spec/support/shared/with_settings.rb b/spec/support/shared/with_settings.rb
index a49c56bbcb2..6d516dbd799 100644
--- a/spec/support/shared/with_settings.rb
+++ b/spec/support/shared/with_settings.rb
@@ -41,9 +41,10 @@ end
RSpec.shared_context "with settings reset" do
shared_let(:definitions_before) { Settings::Definition.all.dup }
- def reset(setting)
+ def reset(setting, **definitions)
+ definitions ||= Settings::Definition::DEFINITIONS[setting]
Settings::Definition.all.delete(setting)
- Settings::Definition.add(setting, **Settings::Definition::DEFINITIONS[setting])
+ Settings::Definition.add(setting, **definitions)
end
def stub_configuration_yml