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

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.rb
index 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 Saml
end
def 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
+ end
rescue OpenSSL::X509::CertificateError => e
errors.add :idp_cert, :invalid_certificate, additional_message: e.message
end
@@ -75,6 +80,10 @@ module Saml
if model.loaded_certificate.blank?
errors.add :certificate, :blank
end
+
+ if OneLogin::RubySaml::Utils.is_cert_expired(model.loaded_certificate)
+ errors.add :certificate, :certificate_expired
+ end
rescue OpenSSL::OpenSSLError => e
errors.add :certificate, :invalid_certificate, additional_message: e.message
end
diff --git a/modules/auth_saml/app/controllers/saml/providers_controller.rb b/modules/auth_saml/app/controllers/saml/providers_controller.rb
index 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 Saml
before_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)
end
def edit
@@ -155,15 +155,7 @@ module Saml
def update_params
params
.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])
end
def find_provider
diff --git a/modules/auth_saml/app/models/saml/provider.rb b/modules/auth_saml/app/models/saml/provider.rb
index 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 Saml
class Provider < ApplicationRecord
+ include HashBuilder
+
self.table_name = "saml_providers"
belongs_to :creator, class_name: "User"
@@ -13,6 +15,8 @@ module Saml
store_attribute :options, :idp_slo_service_url, :string
store_attribute :options, :idp_cert, :string
+ # Allow fallbcak to fingerprint from previous versions,
+ # but we do not offer this in the UI
store_attribute :options, :idp_cert_fingerprint, :string
store_attribute :options, :certificate, :string
@@ -20,6 +24,8 @@ module Saml
store_attribute :options, :authn_requests_signed, :boolean
store_attribute :options, :want_assertions_signed, :boolean
store_attribute :options, :want_assertions_encrypted, :boolean
+ store_attribute :options, :digest_method, :string
+ store_attribute :options, :signature_method, :string
store_attribute :options, :mapping_login, :string
store_attribute :options, :mapping_mail, :string
@@ -72,19 +78,19 @@ module Saml
def loaded_certificate
return nil if certificate.blank?
- OpenSSL::X509::Certificate.new(certificate)
+ @loaded_certificate ||= OpenSSL::X509::Certificate.new(certificate)
end
def loaded_private_key
return nil if private_key.blank?
- OpenSSL::PKey::RSA.new(private_key)
+ @loaded_private_key ||= OpenSSL::PKey::RSA.new(private_key)
end
def loaded_idp_certificates
return nil if idp_cert.blank?
- OpenSSL::X509::Certificate.load(idp_cert)
+ @loaded_idp_certificates ||= OpenSSL::X509::Certificate.load(idp_cert)
end
def certificate_configured?
@@ -106,18 +112,5 @@ module Saml
root_url = OpenProject::StaticRouting::StaticUrlHelpers.new.root_url
URI.join(root_url, "/auth/#{slug}/callback").to_s
end
-
- 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
- end
end
end
diff --git a/modules/auth_saml/app/models/saml/provider/hash_builder.rb b/modules/auth_saml/app/models/saml/provider/hash_builder.rb
new file mode 100644
index 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
+end
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 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 Saml
set_default_requested_attributes
set_issuer
set_name_identifier_format
+ set_default_digest
end
end
+ 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_format
model.name_identifier_format ||= Saml::Defaults::NAME_IDENTIFIER_FORMAT
end
diff --git a/modules/auth_saml/config/locales/en.yml b/modules/auth_saml/config/locales/en.yml
index 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