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

Update metadata correctly

Oliver Günther1a886a984e9a
diff --git a/modules/auth_saml/app/components/saml/providers/sections/form_component.html.erb b/modules/auth_saml/app/components/saml/providers/sections/form_component.html.erb
index 952ad7343cc..ba286d6bd05 100644
--- a/modules/auth_saml/app/components/saml/providers/sections/form_component.html.erb
+++ b/modules/auth_saml/app/components/saml/providers/sections/form_component.html.erb
@@ -15,7 +15,7 @@
end
if @banner
- flex.with_row do
+ flex.with_row(mb: 2) do
icon = @banner_scheme == :warning ? :alert : :info
render(Primer::Alpha::Banner.new(scheme: @banner_scheme, icon:)) do
@banner
diff --git a/modules/auth_saml/app/components/saml/providers/sections/form_component.rb b/modules/auth_saml/app/components/saml/providers/sections/form_component.rb
index b0b97c2ed33..7935602098d 100644
--- a/modules/auth_saml/app/components/saml/providers/sections/form_component.rb
+++ b/modules/auth_saml/app/components/saml/providers/sections/form_component.rb
@@ -33,7 +33,7 @@ module Saml::Providers::Sections
attr_reader :edit_state, :next_edit_state, :edit_mode
def initialize(provider, edit_state:, form_class:,
- heading:, banner: nil, banner_scheme: :info,
+ heading:, banner: nil, banner_scheme: :default,
next_edit_state: nil, edit_mode: nil)
super(provider)
diff --git a/modules/auth_saml/app/components/saml/providers/view_component.html.erb b/modules/auth_saml/app/components/saml/providers/view_component.html.erb
index c8eaf5dc8ab..931e2dfa3a0 100644
--- a/modules/auth_saml/app/components/saml/providers/view_component.html.erb
+++ b/modules/auth_saml/app/components/saml/providers/view_component.html.erb
@@ -68,7 +68,9 @@
edit_state:,
next_edit_state: :encryption,
edit_mode:,
- heading: t("saml.providers.section_texts.configuration_form")
+ banner: provider.last_metadata_update ? t("saml.providers.section_texts.configuration_metadata") : nil,
+ banner_scheme: :default,
+ heading: nil
))
else
render(Saml::Providers::Sections::ShowComponent.new(
diff --git a/modules/auth_saml/app/controllers/saml/providers_controller.rb b/modules/auth_saml/app/controllers/saml/providers_controller.rb
index ef4cfce3057..e49985cbcea 100644
--- a/modules/auth_saml/app/controllers/saml/providers_controller.rb
+++ b/modules/auth_saml/app/controllers/saml/providers_controller.rb
@@ -47,18 +47,23 @@ module Saml
end
def import_metadata
- if params[:saml_provider][:metadata] != "none"
- update_provider_metadata
- return if performed?
- end
+ call = update_provider_metadata_call
+ @provider = call.result
- if @edit_mode
- redirect_to edit_saml_provider_path(@provider,
- anchor: "saml-providers-edit-form",
- edit_mode: @edit_mode,
- edit_state: :configuration)
+ if call.success?
+ if @edit_mode || @provider.last_metadata_update.present?
+ redirect_to edit_saml_provider_path(@provider,
+ anchor: "saml-providers-edit-form",
+ edit_mode: @edit_mode,
+ edit_state: :configuration)
+ else
+ redirect_to saml_provider_path(@provider)
+ end
else
- redirect_to saml_provider_path(@provider)
+ @edit_state = :metadata
+
+ flash[:error] = call.message
+ render action: :edit
end
end
@@ -140,61 +145,24 @@ module Saml
end
end
- def update_provider_metadata
- call = Saml::Providers::UpdateService
+ def update_provider_metadata_call
+ Saml::Providers::UpdateService
.new(model: @provider, user: User.current)
.call(import_params)
-
- if call.success?
- load_and_apply_metadata
- else
- @provider = call.result
- @edit_state = :metadata
-
- flash[:error] = call.message
- render action: :edit
- end
- end
-
- def load_and_apply_metadata
- call = Saml::UpdateMetadataService
- .new(provider: @provider, user: User.current)
- .call
-
- if call.success?
- apply_metadata(call.result.compact_blank)
- else
- @edit_state = :metadata
-
- flash[:error] = call.message
- render action: :edit
- end
end
- def apply_metadata(params) # rubocop:disable Metrics/AbcSize
- new_options = @provider.options.merge(params)
- last_metadata_update = params.blank? ? nil : Time.current
- call = Saml::Providers::UpdateService
- .new(model: @provider, user: User.current)
- .call({ options: new_options, last_metadata_update: })
+ def import_params
+ options = params
+ .require(:saml_provider)
+ .permit(:metadata_url, :metadata_xml, :metadata)
- if call.success?
- flash[:notice] = I18n.t("saml.metadata_parser.success")
+ if options[:metadata] == "none"
+ { metadata_url: nil, metadata_xml: nil }
else
- @provider = call.result
- @edit_state = :configuration
-
- flash[:error] = call.message
- render action: :edit
+ options.slice(:metadata_url, :metadata_xml)
end
end
- def import_params
- params
- .require(:saml_provider)
- .permit(:metadata_url, :metadata_xml)
- end
-
def create_params
params.require(:saml_provider).permit(:display_name)
end
diff --git a/modules/auth_saml/app/forms/saml/providers/submit_or_cancel_form.rb b/modules/auth_saml/app/forms/saml/providers/submit_or_cancel_form.rb
index ce4610581c6..40f55fe34a6 100644
--- a/modules/auth_saml/app/forms/saml/providers/submit_or_cancel_form.rb
+++ b/modules/auth_saml/app/forms/saml/providers/submit_or_cancel_form.rb
@@ -40,7 +40,7 @@ module Saml
f.group(layout: :horizontal) do |button_group|
button_group.submit(**@submit_button_options) unless @provider.seeded_from_env?
- button_group.button(**@cancel_button_options)
+ button_group.button(**@cancel_button_options) unless @cancel_button_options[:hidden]
end
end
diff --git a/modules/auth_saml/app/models/saml/provider.rb b/modules/auth_saml/app/models/saml/provider.rb
index d56d36bffdd..7747e7cee62 100644
--- a/modules/auth_saml/app/models/saml/provider.rb
+++ b/modules/auth_saml/app/models/saml/provider.rb
@@ -56,6 +56,10 @@ module Saml
metadata_xml.present? || metadata_url.present?
end
+ def metadata_updated?
+ metadata_xml_changed? || metadata_url_changed?
+ end
+
def configured?
sp_entity_id.present? &&
idp_sso_service_url.present? &&
diff --git a/modules/auth_saml/app/services/saml/providers/create_service.rb b/modules/auth_saml/app/services/saml/providers/create_service.rb
index 45edbfdcf92..9f36558b94b 100644
--- a/modules/auth_saml/app/services/saml/providers/create_service.rb
+++ b/modules/auth_saml/app/services/saml/providers/create_service.rb
@@ -29,6 +29,7 @@
module Saml
module Providers
class CreateService < BaseServices::Create
+ include UpdateMetadata
end
end
end
diff --git a/modules/auth_saml/app/services/saml/providers/update_metadata.rb b/modules/auth_saml/app/services/saml/providers/update_metadata.rb
new file mode 100644
index 00000000000..6139150379a
--- /dev/null
+++ b/modules/auth_saml/app/services/saml/providers/update_metadata.rb
@@ -0,0 +1,47 @@
+#-- 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
+ module Providers
+ module UpdateMetadata
+ def after_validate(_params, call)
+ return call unless model.metadata_updated?
+
+ metadata_update_call(call.result)
+ end
+
+ private
+
+ def metadata_update_call(provider)
+ Saml::UpdateMetadataService
+ .new(provider:, user:)
+ .call
+ end
+ end
+ end
+end
diff --git a/modules/auth_saml/app/services/saml/providers/update_service.rb b/modules/auth_saml/app/services/saml/providers/update_service.rb
index 558bd3cb02a..97f5fc83564 100644
--- a/modules/auth_saml/app/services/saml/providers/update_service.rb
+++ b/modules/auth_saml/app/services/saml/providers/update_service.rb
@@ -29,6 +29,7 @@
module Saml
module Providers
class UpdateService < BaseServices::Update
+ include UpdateMetadata
end
end
end
diff --git a/modules/auth_saml/app/services/saml/update_metadata_service.rb b/modules/auth_saml/app/services/saml/update_metadata_service.rb
index a4416d11791..6f0976aef0b 100644
--- a/modules/auth_saml/app/services/saml/update_metadata_service.rb
+++ b/modules/auth_saml/app/services/saml/update_metadata_service.rb
@@ -36,34 +36,40 @@ module Saml
end
def call
- updated_metadata
+ apply_metadata(fetch_metadata)
rescue StandardError => e
OpenProject.logger.error(e)
- ServiceResult.failure(message: I18n.t("saml.metadata_parser.error", error: e.class.name))
+ ServiceResult.failure(result: provider,
+ message: I18n.t("saml.metadata_parser.error", error: e.class.name))
end
private
- def updated_metadata
+ def apply_metadata(metadata)
+ new_options = provider.options.merge(metadata)
+ last_metadata_update = metadata.blank? ? nil : Time.current
+
+ Saml::Providers::SetAttributesService
+ .new(model: @provider, user: User.current, contract_class: Saml::Providers::UpdateContract)
+ .call({ options: new_options, last_metadata_update: })
+ end
+
+ def fetch_metadata
if provider.metadata_url.present?
parse_url
elsif provider.metadata_xml.present?
parse_xml
else
- ServiceResult.success(result: {})
+ {}
end
end
def parse_xml
- result = parser_instance.parse_to_hash(provider.metadata_xml)
- ServiceResult.success(result:)
+ parser_instance.parse_to_hash(provider.metadata_xml)
end
def parse_url
- result = parser_instance.parse_remote_to_hash(provider.metadata_url)
- ServiceResult.success(result:)
- rescue OneLogin::RubySaml::HttpError => e
- ServiceResult.failure(message: I18n.t("saml.metadata_parser.error", error: e.message))
+ parser_instance.parse_remote_to_hash(provider.metadata_url)
end
def parser_instance
diff --git a/modules/auth_saml/config/locales/en.yml b/modules/auth_saml/config/locales/en.yml
index 6665fed787f..26b56974d90 100644
--- a/modules/auth_saml/config/locales/en.yml
+++ b/modules/auth_saml/config/locales/en.yml
@@ -37,7 +37,7 @@ en:
description: >
Use these parameters to configure your identity provider connection to OpenProject.
metadata_parser:
- success: "Successfully updated the configuration using the identity provider metadata. Please review and save the configuration."
+ success: "Successfully updated the configuration using the identity provider metadata."
invalid_url: "Provided metadata URL is invalid. Provide a HTTP(s) URL."
error: "Failed to retrieve the identity provider metadata: %{error}"
providers:
@@ -76,7 +76,9 @@ en:
display_name: "Configure the display name of the SAML provider."
metadata: "Pre-fill configuration using a metadata URL or by pasting metadata XML"
metadata_form: "If your identity provider has a metadata endpoint or XML download, add it below to pre-fill configuration."
+ metadata_form_banner: "Editing the metadata may override existing values in other sections. "
configuration: "Configure the endpoint URLs for the identity provider, certificates, and further SAML options."
+ configuration_metadata: "This information has been pre-filled using the supplied metadata. In most cases, they do not require editing."
encryption: "Configure assertion signatures and encryption for SAML requests and responses."
encryption_form: "You may optionally want to encrypt the assertion response, or have requests from OpenProject signed."
mapping: "Manually adjust the mapping between the SAML response and user attributes in OpenProject."