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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Fix hash building
Oliver Güntherd9a33c8bdc97
diff --git a/modules/auth_saml/app/contracts/saml/providers/base_contract.rb b/modules/auth_saml/app/contracts/saml/providers/base_contract.rbindex 738949677b7..6ac62cb2271 100644--- a/modules/auth_saml/app/contracts/saml/providers/base_contract.rb+++ b/modules/auth_saml/app/contracts/saml/providers/base_contract.rb@@ -66,7 +66,12 @@ module Samlenddef idp_cert_is_valid- model.loaded_idp_certificates+ model.loaded_idp_certificates.each do |cert|+ if OneLogin::RubySaml::Utils.is_cert_expired(cert)+ errors.add :certificate, :certificate_expired+ return+ end+ endrescue OpenSSL::X509::CertificateError => eerrors.add :idp_cert, :invalid_certificate, additional_message: e.messageend@@ -75,6 +80,10 @@ module Samlif model.loaded_certificate.blank?errors.add :certificate, :blankend++ if OneLogin::RubySaml::Utils.is_cert_expired(model.loaded_certificate)+ errors.add :certificate, :certificate_expired+ endrescue OpenSSL::OpenSSLError => eerrors.add :certificate, :invalid_certificate, additional_message: e.messageenddiff --git a/modules/auth_saml/app/controllers/saml/providers_controller.rb b/modules/auth_saml/app/controllers/saml/providers_controller.rbindex 69b0000c466..5206ed737f3 100644--- a/modules/auth_saml/app/controllers/saml/providers_controller.rb+++ b/modules/auth_saml/app/controllers/saml/providers_controller.rb@@ -8,7 +8,7 @@ module Samlbefore_action :find_provider, only: %i[show edit import_metadata update destroy]def index- @providers = Saml::Provider.all.order(display_name: "ASC")+ @providers = Saml::Provider.order(display_name: :asc)enddef edit@@ -155,15 +155,7 @@ module Samldef update_paramsparams.require(:saml_provider)- .permit(:display_name, :sp_entity_id, :idp_sso_service_url, :idp_slo_service_url, :idp_cert,- :name_identifier_format, :limit_self_registration,- :certificate, :private_key, :authn_requests_signed,- :want_assertions_signed, :want_assertions_encrypted,- :mapping_login, :mapping_mail, :mapping_firstname, :mapping_lastname, :mapping_uid,- :requested_login_attribute, :requested_mail_attribute, :requested_firstname_attribute,- :requested_lastname_attribute, :requested_uid_attribute,- :requested_login_format, :requested_mail_format, :requested_firstname_format,- :requested_lastname_format, :requested_uid_format)+ .permit(:display_name, *Saml::Provider.stored_attributes[:options])enddef find_providerdiff --git a/modules/auth_saml/app/models/saml/provider.rb b/modules/auth_saml/app/models/saml/provider.rbindex b908f89dbb0..1a59208a1a5 100644--- a/modules/auth_saml/app/models/saml/provider.rb+++ b/modules/auth_saml/app/models/saml/provider.rb@@ -1,5 +1,7 @@module Samlclass Provider < ApplicationRecord+ include HashBuilder+self.table_name = "saml_providers"belongs_to :creator, class_name: "User"@@ -13,6 +15,8 @@ module Samlstore_attribute :options, :idp_slo_service_url, :stringstore_attribute :options, :idp_cert, :string+ # Allow fallbcak to fingerprint from previous versions,+ # but we do not offer this in the UIstore_attribute :options, :idp_cert_fingerprint, :stringstore_attribute :options, :certificate, :string@@ -20,6 +24,8 @@ module Samlstore_attribute :options, :authn_requests_signed, :booleanstore_attribute :options, :want_assertions_signed, :booleanstore_attribute :options, :want_assertions_encrypted, :boolean+ store_attribute :options, :digest_method, :string+ store_attribute :options, :signature_method, :stringstore_attribute :options, :mapping_login, :stringstore_attribute :options, :mapping_mail, :string@@ -72,19 +78,19 @@ module Samldef loaded_certificatereturn nil if certificate.blank?- OpenSSL::X509::Certificate.new(certificate)+ @loaded_certificate ||= OpenSSL::X509::Certificate.new(certificate)enddef loaded_private_keyreturn nil if private_key.blank?- OpenSSL::PKey::RSA.new(private_key)+ @loaded_private_key ||= OpenSSL::PKey::RSA.new(private_key)enddef loaded_idp_certificatesreturn nil if idp_cert.blank?- OpenSSL::X509::Certificate.load(idp_cert)+ @loaded_idp_certificates ||= OpenSSL::X509::Certificate.load(idp_cert)enddef certificate_configured?@@ -106,18 +112,5 @@ module Samlroot_url = OpenProject::StaticRouting::StaticUrlHelpers.new.root_urlURI.join(root_url, "/auth/#{slug}/callback").to_send-- def to_h- options- .merge(- name: slug,- display_name:,- assertion_consumer_service_url:,- check_idp_cert_expiration: true,- check_sp_cert_expiration: true,- metadata_signed: certificate.present? && private_key.present?- )- .symbolize_keys- endendenddiff --git a/modules/auth_saml/app/models/saml/provider/hash_builder.rb b/modules/auth_saml/app/models/saml/provider/hash_builder.rbnew file mode 100644index 00000000000..c208b94e53f--- /dev/null+++ b/modules/auth_saml/app/models/saml/provider/hash_builder.rb@@ -0,0 +1,72 @@+module Saml+ module Provider::HashBuilder+ def formatted_attribute_statements+ {+ email: mapping_mail&.split("[\r\n]+"),+ login: mapping_login&.split("[\r\n]+"),+ first_name: mapping_firstname&.split("[\r\n]+"),+ last_name: mapping_lastname&.split("[\r\n]+")+ }.compact+ end++ 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" }+ ]+ end++ def idp_cert_options_hash+ if idp_cert_fingerprint.present?+ return { idp_cert_fingerprint: }+ end++ certificates = loaded_idp_certificates.map(&:to_pem)+ if certificates.count > 1+ {+ idp_cert_multi: {+ signing: certificates,+ encryption: certificates+ }+ }+ else+ { idp_cert: certificates.first }+ end+ end++ def security_options_hash+ {+ check_idp_cert_expiration: false, # done in contract+ check_sp_cert_expiration: false, # done in contract+ metadata_signed: certificate.present? && private_key.present?,+ authn_requests_signed:,+ want_assertions_signed:,+ want_assertions_encrypted:,+ digest_method:,+ signature_method:+ }.compact+ end++ def to_h+ {+ name: slug,+ display_name:,+ assertion_consumer_service_url:,+ sp_entity_id:,+ idp_sso_service_url:,+ idp_slo_service_url:,+ name_identifier_format:,+ certificate:,+ private_key:,+ attribute_statements: formatted_attribute_statements,+ request_attributes: formatted_request_attributes,+ uid_attribute: mapping_uid+ }+ .merge(idp_cert_options_hash)+ .merge(security: security_options_hash)+ .compact+ end+ end+enddiff --git a/modules/auth_saml/app/services/saml/providers/set_attributes_service.rb b/modules/auth_saml/app/services/saml/providers/set_attributes_service.rbindex 0386a8b5b05..d4e12a00927 100644--- a/modules/auth_saml/app/services/saml/providers/set_attributes_service.rb+++ b/modules/auth_saml/app/services/saml/providers/set_attributes_service.rb@@ -63,9 +63,15 @@ module Samlset_default_requested_attributesset_issuerset_name_identifier_format+ set_default_digestendend+ def set_default_digest+ model.signature_method ||= Saml::Defaults::SIGNATURE_METHODS["RSA SHA-1"]+ model.digest_method ||= Saml::Defaults::DIGEST_METHODS["SHA-1"]+ end+def set_name_identifier_formatmodel.name_identifier_format ||= Saml::Defaults::NAME_IDENTIFIER_FORMATenddiff --git a/modules/auth_saml/config/locales/en.yml b/modules/auth_saml/config/locales/en.ymlindex 88742931aa3..ed7b17260da 100644--- a/modules/auth_saml/config/locales/en.yml+++ b/modules/auth_saml/config/locales/en.yml@@ -27,6 +27,7 @@ en:saml/provider:invalid_certificate: "is not a valid PEM-formatted certificate: %{additional_message}"invalid_private_key: "is not a valid PEM-formatted private key: %{additional_message}"+ certificate_expired: "is expired and can no longer be used."unmatched_private_key: "does not belong to the given certificate"saml:menu_title: SAML providers