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

Hash builder with mapping

Oliver Günthera1dd5278f93b
diff --git a/modules/openid_connect/app/components/openid_connect/providers/attribute_mapping_form.rb b/modules/openid_connect/app/components/openid_connect/providers/attribute_mapping_form.rb
index 2c9717ba860..1ed8eb37544 100644
--- a/modules/openid_connect/app/components/openid_connect/providers/attribute_mapping_form.rb
+++ b/modules/openid_connect/app/components/openid_connect/providers/attribute_mapping_form.rb
@@ -34,7 +34,7 @@ module OpenIDConnect
attribute_name = User.human_attribute_name(attr == :email ? :mail : attr)
f.text_field(
- name: "#{attr}_mapping",
+ name: :"mapping_#{attr}",
label: I18n.t("label_mapping_for", attribute: attribute_name),
caption: I18n.t("openid_connect.instructions.mapping_#{attr}"),
disabled: provider.seeded_from_env?,
diff --git a/modules/openid_connect/app/models/openid_connect/provider.rb b/modules/openid_connect/app/models/openid_connect/provider.rb
index 9d6ac05bd26..692eb681682 100644
--- a/modules/openid_connect/app/models/openid_connect/provider.rb
+++ b/modules/openid_connect/app/models/openid_connect/provider.rb
@@ -1,5 +1,7 @@
module OpenIDConnect
class Provider < AuthProvider
+ include HashBuilder
+
OIDC_PROVIDERS = %w[google microsoft_entra custom].freeze
DISCOVERABLE_ATTRIBUTES_ALL = %i[authorization_endpoint
userinfo_endpoint
@@ -19,7 +21,7 @@ module OpenIDConnect
store_attribute :options, attribute, :string
end
MAPPABLE_ATTRIBUTES.each do |attribute|
- store_attribute :options, "#{attribute}_mapping", :string
+ store_attribute :options, "mapping_#{attribute}", :string
end
store_attribute :options, :client_id, :string
@@ -50,32 +52,7 @@ module OpenIDConnect
basic_details_configured? && advanced_details_configured? && metadata_configured?
end
- def to_h
- h = {
- name: slug,
- icon:,
- display_name:,
- userinfo_endpoint:,
- authorization_endpoint:,
- jwks_uri:,
- host: URI(issuer).host,
- issuer:,
- identifier: client_id,
- secret: client_secret,
- token_endpoint:,
- limit_self_registration:,
- end_session_endpoint:
- }.to_h
- if oidc_provider == "google"
- h.merge!({
- client_auth_method: :not_basic,
- send_nonce: false, # use state instead of nonce
- state: lambda { SecureRandom.hex(42) }
- })
- end
- h
- end
def icon
case oidc_provider
diff --git a/modules/openid_connect/app/models/openid_connect/provider/hash_builder.rb b/modules/openid_connect/app/models/openid_connect/provider/hash_builder.rb
new file mode 100644
index 00000000000..417d22eac48
--- /dev/null
+++ b/modules/openid_connect/app/models/openid_connect/provider/hash_builder.rb
@@ -0,0 +1,42 @@
+module OpenIDConnect
+ module Provider::HashBuilder
+ def attribute_map
+ OpenIDConnect::Provider::MAPPABLE_ATTRIBUTES
+ .index_with { |attr| public_send(:"mapping_#{attr}") }
+ .compact_blank
+ end
+
+ def to_h # rubocop:disable Metrics/AbcSize
+ h = {
+ name: slug,
+ icon:,
+ display_name:,
+ userinfo_endpoint:,
+ authorization_endpoint:,
+ jwks_uri:,
+ host: URI(issuer).host,
+ issuer:,
+ identifier: client_id,
+ secret: client_secret,
+ token_endpoint:,
+ limit_self_registration:,
+ end_session_endpoint:,
+ attribute_map:
+ }
+ .merge(attribute_map)
+ .compact_blank
+
+ if oidc_provider == "google"
+ h.merge!(
+ {
+ client_auth_method: :not_basic,
+ send_nonce: false, # use state instead of nonce
+ state: lambda { SecureRandom.hex(42) }
+ }
+ )
+ end
+
+ h
+ end
+ end
+end
diff --git a/modules/openid_connect/app/services/openid_connect/configuration_mapper.rb b/modules/openid_connect/app/services/openid_connect/configuration_mapper.rb
new file mode 100644
index 00000000000..74874554698
--- /dev/null
+++ b/modules/openid_connect/app/services/openid_connect/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