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

Accept non-formatted cert

Oliver Günther195db69d4bf8
diff --git a/modules/auth_saml/app/contracts/saml/providers/base_contract.rb b/modules/auth_saml/app/contracts/saml/providers/base_contract.rb
index 5ac5ab61f7b..738949677b7 100644
--- a/modules/auth_saml/app/contracts/saml/providers/base_contract.rb
+++ b/modules/auth_saml/app/contracts/saml/providers/base_contract.rb
@@ -62,50 +62,56 @@ module Saml
%i[mapping_mail mapping_login mapping_firstname mapping_lastname].each do |attr|
attribute attr
- validates_presence_of attr
+ validates_presence_of attr, if: -> { model.public_send(:"#{attr}_changed?") }
end
def idp_cert_is_valid
- return if model.idp_cert.blank?
-
- OpenSSL::X509::Certificate.load(model.idp_cert)
+ model.loaded_idp_certificates
rescue OpenSSL::X509::CertificateError => e
errors.add :idp_cert, :invalid_certificate, additional_message: e.message
end
def valid_certificate
- if model.certificate.blank?
+ if model.loaded_certificate.blank?
errors.add :certificate, :blank
- else
- OpenSSL::X509::Certificate.new(model.certificate)
end
- rescue OpenSSL::X509::CertificateError => e
+ rescue OpenSSL::OpenSSLError => e
errors.add :certificate, :invalid_certificate, additional_message: e.message
end
def valid_sp_key
- if model.private_key.blank?
+ if model.loaded_private_key.blank?
errors.add :private_key, :blank
- else
- OpenSSL::PKey::RSA.new(model.private_key)
end
- rescue OpenSSL::X509::CertificateError => e
+ rescue OpenSSL::OpenSSLError => e
errors.add :private_key, :invalid_private_key, additional_message: e.message
end
def authn_requests_signed_requires_cert
- return unless model.authn_requests_signed
- return unless model.authn_requests_signed_changed? || model.certificate_changed? || model.private_key_changed?
+ return unless should_test_certificate?
+ return if certificate_invalid?
- cert = valid_certificate
- key = valid_sp_key
+ cert = model.loaded_certificate
+ key = model.loaded_private_key
- return unless errors.empty?
-
- unless cert.public_key.public_to_pem == key.public_key.public_to_pem
+ if cert && key && cert.public_key.public_to_pem != key.public_key.public_to_pem
errors.add :private_key, :unmatched_private_key
end
end
+
+ def certificate_invalid?
+ valid_certificate
+ valid_sp_key
+
+ errors.any?
+ end
+
+ def should_test_certificate?
+ return false unless model.authn_requests_signed
+ return false unless model.authn_requests_signed_changed? || model.certificate_changed? || model.private_key_changed?
+
+ model.certificate.present? && model.private_key.present?
+ end
end
end
end
diff --git a/modules/auth_saml/app/models/saml/provider.rb b/modules/auth_saml/app/models/saml/provider.rb
index 2d64c587c7a..8b65ce198b2 100644
--- a/modules/auth_saml/app/models/saml/provider.rb
+++ b/modules/auth_saml/app/models/saml/provider.rb
@@ -60,10 +60,39 @@ module Saml
sp_entity_id.present? && idp_sso_service_url.present? && certificate_configured?
end
+ def loaded_certificate
+ return nil if certificate.blank?
+
+ OpenSSL::X509::Certificate.new(certificate)
+ end
+
+ def loaded_private_key
+ return nil if private_key.blank?
+
+ OpenSSL::PKey::RSA.new(private_key)
+ end
+
+ def loaded_idp_certificates
+ return nil if idp_cert.blank?
+
+ OpenSSL::X509::Certificate.load(idp_cert)
+ end
+
def certificate_configured?
idp_cert.present?
end
+ def idp_cert=(cert)
+ formatted =
+ if cert.include?("BEGIN CERTIFICATE")
+ cert
+ else
+ OneLogin::RubySaml::Utils.format_cert(cert)
+ end
+
+ super(formatted)
+ end
+
def assertion_consumer_service_url
root_url = OpenProject::StaticRouting::StaticUrlHelpers.new.root_url
URI.join(root_url, "/auth/#{slug}/callback").to_s
diff --git a/modules/auth_saml/app/services/saml/providers/set_attributes_service.rb b/modules/auth_saml/app/services/saml/providers/set_attributes_service.rb
index 9e08db9f2b8..9218a761676 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
@@ -33,10 +33,21 @@ module Saml
def set_attributes(params)
update_mapping(params)
+ update_options(params.delete(:options)) if params.key?(:options)
super
end
+ def update_options(options)
+ update_idp_cert(options.delete(:idp_cert)) if options.key?(:idp_cert)
+
+ options
+ .select { |key, _| Saml::Provider.stored_attributes[:options].include?(key.to_s) }
+ .each do |key, value|
+ model.public_send(:"#{key}=", value)
+ end
+ end
+
def set_default_attributes(*)
model.change_by_system do
set_default_creator
@@ -55,6 +66,15 @@ module Saml
model.creator = user
end
+ def update_idp_cert(cert)
+ model.idp_cert =
+ if cert.include?("BEGIN CERTIFICATE")
+ cert
+ else
+ OneLogin::RubySaml::Utils.format_cert(cert)
+ end
+ end
+
##
# Clean up provided mapping, reducing whitespace
def update_mapping(params)