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
Implements the handling of token acquiral and refresh results. (#18063)
Marcello Rochac6eaf03d030d
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.rbindex 1ed8eb37544..ad302929873 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@@ -1,6 +1,8 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.-# Copyright (C) 2012-2024 the OpenProject GmbH+# Copyright (C) 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.diff --git a/modules/openid_connect/app/components/openid_connect/providers/base_form.rb b/modules/openid_connect/app/components/openid_connect/providers/base_form.rbindex 6a727f4162b..1fc6b45e43b 100644--- a/modules/openid_connect/app/components/openid_connect/providers/base_form.rb+++ b/modules/openid_connect/app/components/openid_connect/providers/base_form.rb@@ -1,6 +1,8 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.-# Copyright (C) 2012-2024 the OpenProject GmbH+# Copyright (C) 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.diff --git a/modules/openid_connect/app/components/openid_connect/providers/claims_form.rb b/modules/openid_connect/app/components/openid_connect/providers/claims_form.rbindex 1a4fdee56f7..73e447404a6 100644--- a/modules/openid_connect/app/components/openid_connect/providers/claims_form.rb+++ b/modules/openid_connect/app/components/openid_connect/providers/claims_form.rb@@ -1,6 +1,8 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.-# Copyright (C) 2012-2024 the OpenProject GmbH+# Copyright (C) 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.diff --git a/modules/openid_connect/app/components/openid_connect/providers/client_details_form.rb b/modules/openid_connect/app/components/openid_connect/providers/client_details_form.rbindex fc25de4d1bc..eb1d23bf5fb 100644--- a/modules/openid_connect/app/components/openid_connect/providers/client_details_form.rb+++ b/modules/openid_connect/app/components/openid_connect/providers/client_details_form.rb@@ -1,6 +1,8 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.-# Copyright (C) 2012-2024 the OpenProject GmbH+# Copyright (C) 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.diff --git a/modules/openid_connect/app/components/openid_connect/providers/metadata_details_form.rb b/modules/openid_connect/app/components/openid_connect/providers/metadata_details_form.rbindex cdce864fa77..2f7b2ac56ff 100644--- a/modules/openid_connect/app/components/openid_connect/providers/metadata_details_form.rb+++ b/modules/openid_connect/app/components/openid_connect/providers/metadata_details_form.rb@@ -1,6 +1,8 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.-# Copyright (C) 2012-2024 the OpenProject GmbH+# Copyright (C) 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.diff --git a/modules/openid_connect/app/components/openid_connect/providers/metadata_options_form.rb b/modules/openid_connect/app/components/openid_connect/providers/metadata_options_form.rbindex 0e82e822386..bc4abd6494c 100644--- a/modules/openid_connect/app/components/openid_connect/providers/metadata_options_form.rb+++ b/modules/openid_connect/app/components/openid_connect/providers/metadata_options_form.rb@@ -1,6 +1,8 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.-# Copyright (C) 2012-2024 the OpenProject GmbH+# Copyright (C) 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.diff --git a/modules/openid_connect/app/components/openid_connect/providers/metadata_url_form.rb b/modules/openid_connect/app/components/openid_connect/providers/metadata_url_form.rbindex b18a185536c..0e4401887ec 100644--- a/modules/openid_connect/app/components/openid_connect/providers/metadata_url_form.rb+++ b/modules/openid_connect/app/components/openid_connect/providers/metadata_url_form.rb@@ -1,6 +1,8 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.-# Copyright (C) 2012-2024 the OpenProject GmbH+# Copyright (C) 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.diff --git a/modules/openid_connect/app/components/openid_connect/providers/name_input_and_tenant_form.rb b/modules/openid_connect/app/components/openid_connect/providers/name_input_and_tenant_form.rbindex 09ae6787158..cd967c9ab85 100644--- a/modules/openid_connect/app/components/openid_connect/providers/name_input_and_tenant_form.rb+++ b/modules/openid_connect/app/components/openid_connect/providers/name_input_and_tenant_form.rb@@ -1,6 +1,8 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.-# Copyright (C) 2012-2024 the OpenProject GmbH+# Copyright (C) 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.diff --git a/modules/openid_connect/app/components/openid_connect/providers/name_input_form.rb b/modules/openid_connect/app/components/openid_connect/providers/name_input_form.rbindex 441563939d3..8e28f76babf 100644--- a/modules/openid_connect/app/components/openid_connect/providers/name_input_form.rb+++ b/modules/openid_connect/app/components/openid_connect/providers/name_input_form.rb@@ -1,6 +1,8 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.-# Copyright (C) 2012-2024 the OpenProject GmbH+# Copyright (C) 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.diff --git a/modules/openid_connect/app/components/openid_connect/providers/sections/form_component.rb b/modules/openid_connect/app/components/openid_connect/providers/sections/form_component.rbindex 194e3012606..a1592a14226 100644--- a/modules/openid_connect/app/components/openid_connect/providers/sections/form_component.rb+++ b/modules/openid_connect/app/components/openid_connect/providers/sections/form_component.rb@@ -2,7 +2,7 @@#-- copyright# OpenProject is an open source project management software.-# Copyright (C) 2012-2024 the OpenProject GmbH+# Copyright (C) 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.diff --git a/modules/openid_connect/app/components/openid_connect/providers/sections/metadata_form_component.rb b/modules/openid_connect/app/components/openid_connect/providers/sections/metadata_form_component.rbindex b8ea2f88a11..da87fdddd2f 100644--- a/modules/openid_connect/app/components/openid_connect/providers/sections/metadata_form_component.rb+++ b/modules/openid_connect/app/components/openid_connect/providers/sections/metadata_form_component.rb@@ -2,7 +2,7 @@#-- copyright# OpenProject is an open source project management software.-# Copyright (C) 2012-2024 the OpenProject GmbH+# Copyright (C) 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.diff --git a/modules/openid_connect/app/components/openid_connect/providers/sections/show_component.rb b/modules/openid_connect/app/components/openid_connect/providers/sections/show_component.rbindex 630b15558b5..5e95139a726 100644--- a/modules/openid_connect/app/components/openid_connect/providers/sections/show_component.rb+++ b/modules/openid_connect/app/components/openid_connect/providers/sections/show_component.rb@@ -2,7 +2,7 @@#-- copyright# OpenProject is an open source project management software.-# Copyright (C) 2012-2024 the OpenProject GmbH+# Copyright (C) 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.diff --git a/modules/openid_connect/app/components/openid_connect/providers/side_panel/information_component.rb b/modules/openid_connect/app/components/openid_connect/providers/side_panel/information_component.rbindex 610e01b3e96..21cafac77c9 100644--- a/modules/openid_connect/app/components/openid_connect/providers/side_panel/information_component.rb+++ b/modules/openid_connect/app/components/openid_connect/providers/side_panel/information_component.rb@@ -1,6 +1,8 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.-# Copyright (C) 2012-2024 the OpenProject GmbH+# Copyright (C) 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.diff --git a/modules/openid_connect/app/components/openid_connect/providers/side_panel_component.rb b/modules/openid_connect/app/components/openid_connect/providers/side_panel_component.rbindex 5764ea920d5..346129f8c31 100644--- a/modules/openid_connect/app/components/openid_connect/providers/side_panel_component.rb+++ b/modules/openid_connect/app/components/openid_connect/providers/side_panel_component.rb@@ -1,6 +1,8 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.-# Copyright (C) 2012-2024 the OpenProject GmbH+# Copyright (C) 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.diff --git a/modules/openid_connect/app/components/openid_connect/providers/submit_or_cancel_form.rb b/modules/openid_connect/app/components/openid_connect/providers/submit_or_cancel_form.rbindex d04799752bb..9d10adea4fa 100644--- a/modules/openid_connect/app/components/openid_connect/providers/submit_or_cancel_form.rb+++ b/modules/openid_connect/app/components/openid_connect/providers/submit_or_cancel_form.rb@@ -1,6 +1,8 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.-# Copyright (C) 2012-2024 the OpenProject GmbH+# Copyright (C) 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.diff --git a/modules/openid_connect/app/components/openid_connect/providers/view_component.rb b/modules/openid_connect/app/components/openid_connect/providers/view_component.rbindex 400203eb270..c4cc698435c 100644--- a/modules/openid_connect/app/components/openid_connect/providers/view_component.rb+++ b/modules/openid_connect/app/components/openid_connect/providers/view_component.rb@@ -2,7 +2,7 @@#-- copyright# OpenProject is an open source project management software.-# Copyright (C) 2012-2024 the OpenProject GmbH+# Copyright (C) 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.diff --git a/modules/openid_connect/app/contracts/openid_connect/providers/base_contract.rb b/modules/openid_connect/app/contracts/openid_connect/providers/base_contract.rbindex e61b68fd0d4..8cd123138b4 100644--- a/modules/openid_connect/app/contracts/openid_connect/providers/base_contract.rb+++ b/modules/openid_connect/app/contracts/openid_connect/providers/base_contract.rb@@ -1,6 +1,8 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.-# Copyright (C) 2012-2024 the OpenProject GmbH+# Copyright (C) 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.diff --git a/modules/openid_connect/app/contracts/openid_connect/providers/create_contract.rb b/modules/openid_connect/app/contracts/openid_connect/providers/create_contract.rbindex 3f6e5232e93..f3cd270d28c 100644--- a/modules/openid_connect/app/contracts/openid_connect/providers/create_contract.rb+++ b/modules/openid_connect/app/contracts/openid_connect/providers/create_contract.rb@@ -1,6 +1,8 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.-# Copyright (C) 2012-2024 the OpenProject GmbH+# Copyright (C) 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.diff --git a/modules/openid_connect/app/contracts/openid_connect/providers/delete_contract.rb b/modules/openid_connect/app/contracts/openid_connect/providers/delete_contract.rbindex 89a44cf5904..e7346224f7c 100644--- a/modules/openid_connect/app/contracts/openid_connect/providers/delete_contract.rb+++ b/modules/openid_connect/app/contracts/openid_connect/providers/delete_contract.rb@@ -1,6 +1,8 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.-# Copyright (C) 2012-2024 the OpenProject GmbH+# Copyright (C) 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.diff --git a/modules/openid_connect/app/contracts/openid_connect/providers/update_contract.rb b/modules/openid_connect/app/contracts/openid_connect/providers/update_contract.rbindex 4b6ec6a47ba..96d06d21205 100644--- a/modules/openid_connect/app/contracts/openid_connect/providers/update_contract.rb+++ b/modules/openid_connect/app/contracts/openid_connect/providers/update_contract.rb@@ -1,6 +1,8 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.-# Copyright (C) 2012-2024 the OpenProject GmbH+# Copyright (C) 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.diff --git a/modules/openid_connect/app/models/openid_connect/provider.rb b/modules/openid_connect/app/models/openid_connect/provider.rbindex 7c51c9c2888..c770cb07b5a 100644--- a/modules/openid_connect/app/models/openid_connect/provider.rb+++ b/modules/openid_connect/app/models/openid_connect/provider.rb@@ -1,11 +1,41 @@# frozen_string_literal: true+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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 OpenIDConnectclass Provider < AuthProviderinclude HashBuilderhas_many :remote_identities, as: :auth_source, dependent: :destroy+ TOKEN_EXCHANGE_GRANT_TYPE = "urn:ietf:params:oauth:grant-type:token-exchange"+OIDC_PROVIDERS = %w[google microsoft_entra custom].freezeDISCOVERABLE_STRING_ATTRIBUTES_MANDATORY = %i[authorization_endpointuserinfo_endpoint@@ -23,6 +53,7 @@ module OpenIDConnectDISCOVERABLE_STRING_ATTRIBUTES_ALL.each do |attribute|store_attribute :options, attribute, :stringend+MAPPABLE_ATTRIBUTES.each do |attribute|store_attribute :options, "mapping_#{attribute}", :stringend@@ -87,7 +118,7 @@ module OpenIDConnectdef token_exchange_capable?return false if grant_types_supported.blank?- grant_types_supported.include?("urn:ietf:params:oauth:grant-type:token-exchange")+ grant_types_supported.include?(TOKEN_EXCHANGE_GRANT_TYPE)enddef icondiff --git a/modules/openid_connect/app/models/openid_connect/token_operation_error.rb b/modules/openid_connect/app/models/openid_connect/token_operation_error.rbnew file mode 100644index 00000000000..0597470f452--- /dev/null+++ b/modules/openid_connect/app/models/openid_connect/token_operation_error.rb@@ -0,0 +1,37 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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 OpenIDConnect+ TokenOperationError = Data.define(:code, :payload, :source) do+ def initialize(source:, payload: nil, code: nil)+ super+ end+ end+enddiff --git a/modules/openid_connect/app/models/openid_connect/user_session_link.rb b/modules/openid_connect/app/models/openid_connect/user_session_link.rbindex e15000a288a..6ac0fa9f01f 100644--- a/modules/openid_connect/app/models/openid_connect/user_session_link.rb+++ b/modules/openid_connect/app/models/openid_connect/user_session_link.rb@@ -2,7 +2,7 @@#-- copyright# OpenProject is an open source project management software.-# Copyright (C) 2012-2024 the OpenProject GmbH+# Copyright (C) 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.diff --git a/modules/openid_connect/app/models/openid_connect/user_token.rb b/modules/openid_connect/app/models/openid_connect/user_token.rbindex 8f4a0cbfaea..55f4bb970c5 100644--- a/modules/openid_connect/app/models/openid_connect/user_token.rb+++ b/modules/openid_connect/app/models/openid_connect/user_token.rb@@ -2,7 +2,7 @@#-- copyright# OpenProject is an open source project management software.-# Copyright (C) 2012-2024 the OpenProject GmbH+# Copyright (C) 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.diff --git a/modules/openid_connect/app/seeders/env_data/openid_connect/provider_seeder.rb b/modules/openid_connect/app/seeders/env_data/openid_connect/provider_seeder.rbindex 732ff3bc3e4..aad923113c1 100644--- a/modules/openid_connect/app/seeders/env_data/openid_connect/provider_seeder.rb+++ b/modules/openid_connect/app/seeders/env_data/openid_connect/provider_seeder.rb@@ -1,5 +1,6 @@-#-- copyright+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.# Copyright (C) the OpenProject GmbH#diff --git a/modules/openid_connect/app/services/openid_connect/configuration_mapper.rb b/modules/openid_connect/app/services/openid_connect/configuration_mapper.rbindex d2d16bc0af1..4f61d780020 100644--- a/modules/openid_connect/app/services/openid_connect/configuration_mapper.rb+++ b/modules/openid_connect/app/services/openid_connect/configuration_mapper.rb@@ -1,6 +1,8 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.-# Copyright (C) 2012-2024 the OpenProject GmbH+# Copyright (C) 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.diff --git a/modules/openid_connect/app/services/openid_connect/jwt_parser.rb b/modules/openid_connect/app/services/openid_connect/jwt_parser.rbindex 7de07e394ad..60f941b2c5f 100644--- a/modules/openid_connect/app/services/openid_connect/jwt_parser.rb+++ b/modules/openid_connect/app/services/openid_connect/jwt_parser.rb@@ -1,14 +1,14 @@# frozen_string_literal: true#-- copyright-# OpenProject is a project management system.+# OpenProject is an open source project management software.# Copyright (C) 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-2017 Jean-Philippe Lang+# 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@@ -26,7 +26,7 @@# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.## See COPYRIGHT and LICENSE files for more details.-# ++#++module OpenIDConnectclass JwtParserdiff --git a/modules/openid_connect/app/services/openid_connect/providers/create_service.rb b/modules/openid_connect/app/services/openid_connect/providers/create_service.rbindex 734fd537897..dcd7ec9488e 100644--- a/modules/openid_connect/app/services/openid_connect/providers/create_service.rb+++ b/modules/openid_connect/app/services/openid_connect/providers/create_service.rb@@ -1,6 +1,8 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.-# Copyright (C) 2012-2024 the OpenProject GmbH+# Copyright (C) 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.diff --git a/modules/openid_connect/app/services/openid_connect/providers/delete_service.rb b/modules/openid_connect/app/services/openid_connect/providers/delete_service.rbindex 11416b30fa3..d35692913d5 100644--- a/modules/openid_connect/app/services/openid_connect/providers/delete_service.rb+++ b/modules/openid_connect/app/services/openid_connect/providers/delete_service.rb@@ -1,6 +1,8 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.-# Copyright (C) 2012-2024 the OpenProject GmbH+# Copyright (C) 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.diff --git a/modules/openid_connect/app/services/openid_connect/providers/set_attributes_service.rb b/modules/openid_connect/app/services/openid_connect/providers/set_attributes_service.rbindex 8e4a9bbd64f..3147619fd0a 100644--- a/modules/openid_connect/app/services/openid_connect/providers/set_attributes_service.rb+++ b/modules/openid_connect/app/services/openid_connect/providers/set_attributes_service.rb@@ -1,6 +1,8 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.-# Copyright (C) 2012-2024 the OpenProject GmbH+# Copyright (C) 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.diff --git a/modules/openid_connect/app/services/openid_connect/providers/update_service.rb b/modules/openid_connect/app/services/openid_connect/providers/update_service.rbindex 4dbc0f5fb76..fb21afd9b7d 100644--- a/modules/openid_connect/app/services/openid_connect/providers/update_service.rb+++ b/modules/openid_connect/app/services/openid_connect/providers/update_service.rb@@ -1,6 +1,8 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.-# Copyright (C) 2012-2024 the OpenProject GmbH+# Copyright (C) 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.diff --git a/modules/openid_connect/app/services/openid_connect/sync_service.rb b/modules/openid_connect/app/services/openid_connect/sync_service.rbindex 94dee1aa65c..106e3fbfa48 100644--- a/modules/openid_connect/app/services/openid_connect/sync_service.rb+++ b/modules/openid_connect/app/services/openid_connect/sync_service.rb@@ -1,6 +1,8 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.-# Copyright (C) 2012-2024 the OpenProject GmbH+# Copyright (C) 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.diff --git a/modules/openid_connect/app/services/openid_connect/user_tokens/create_service.rb b/modules/openid_connect/app/services/openid_connect/user_tokens/create_service.rbindex 8d3013ede9c..e2bcf1085b3 100644--- a/modules/openid_connect/app/services/openid_connect/user_tokens/create_service.rb+++ b/modules/openid_connect/app/services/openid_connect/user_tokens/create_service.rb@@ -1,14 +1,14 @@# frozen_string_literal: true#-- copyright-# OpenProject is a project management system.+# OpenProject is an open source project management software.# Copyright (C) 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-2017 Jean-Philippe Lang+# 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@@ -26,7 +26,7 @@# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.## See COPYRIGHT and LICENSE files for more details.-# ++#++module OpenIDConnectmodule UserTokensdiff --git a/modules/openid_connect/app/services/openid_connect/user_tokens/exchange_service.rb b/modules/openid_connect/app/services/openid_connect/user_tokens/exchange_service.rbindex 2f803d6357f..d741955d97f 100644--- a/modules/openid_connect/app/services/openid_connect/user_tokens/exchange_service.rb+++ b/modules/openid_connect/app/services/openid_connect/user_tokens/exchange_service.rb@@ -2,7 +2,7 @@#-- copyright# OpenProject is an open source project management software.-# Copyright (C) 2012-2024 the OpenProject GmbH+# Copyright (C) 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.@@ -31,7 +31,7 @@module OpenIDConnectmodule UserTokensclass ExchangeService- include Dry::Monads[:result]+ include Dry::Monads::Result(TokenOperationError)include Dry::Monads::Do.for(:call)class Disabled@@ -51,16 +51,15 @@ module OpenIDConnectenddef call(audience)- return Failure("Provider does not support token exchange") unless supported?-- idp_token = yield FetchService.new(user:, token_exchange: Disabled)- .access_token_for(audience: UserToken::IDP_AUDIENCE)+ unless supported?+ return failure_with(code: :token_exchange_not_supported, payload: provider&.grant_types_supported)+ end- json = yield TokenRequest.new(provider:).exchange(idp_token, audience)+ idp_token = yield fetch_idp_token+ json = yield exchange_token_request(idp_token, audience)- access_token = json["access_token"]- expires_in = json["expires_in"]- return Failure("Token exchange response invalid") if access_token.blank?+ access_token, expires_in = json.values_at("access_token", "expires_in")+ return failure_with(code: :token_exchange_response_invalid, payload: json) if access_token.blank?# We are explicitly opting to not store the refresh token for exchanged tokens# For one there is no need to store one, we can simply exchange a new token once the old expired.@@ -77,17 +76,30 @@ module OpenIDConnectprivate+ def failure_with(**) = Failure(error.with(**))++ def error = TokenOperationError.new(source: self.class)++ def fetch_idp_token+ FetchService.new(user:, token_exchange: Disabled).access_token_for(audience: UserToken::IDP_AUDIENCE)+ end++ def exchange_token_request(idp_token, audience)+ TokenRequest.new(provider:).exchange(idp_token, audience).alt_map do+ it.with(code: :"token_exchange_#{it.code}", source: self.class)+ end+ end+def store_exchanged_token(audience:, access_token:, refresh_token:, expires_in:)token_data = { access_token:, refresh_token:, expires_at: expires_in&.seconds&.from_now }token = user.oidc_user_tokens.where("audiences ? :audience", audience:).first- if token- if token.audiences.size > 1- raise "Did not expect to update token with multiple audiences (#{token.audiences}) in-place."- end- token.update!(**token_data)- else+ if token.nil?token = user.oidc_user_tokens.create!(audiences: [audience], **token_data)+ elsif token.audiences.size > 1+ raise "Did not expect to update token with multiple audiences (#{token.audiences}) in-place."+ else+ token.update!(**token_data)endtokendiff --git a/modules/openid_connect/app/services/openid_connect/user_tokens/fetch_service.rb b/modules/openid_connect/app/services/openid_connect/user_tokens/fetch_service.rbindex bb3a2e54023..f731c3a7ab7 100644--- a/modules/openid_connect/app/services/openid_connect/user_tokens/fetch_service.rb+++ b/modules/openid_connect/app/services/openid_connect/user_tokens/fetch_service.rb@@ -2,7 +2,7 @@#-- copyright# OpenProject is an open source project management software.-# Copyright (C) 2012-2024 the OpenProject GmbH+# Copyright (C) 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.@@ -35,8 +35,8 @@ module OpenIDConnect# application for which we know the audience name, which is typically the application's# client_id at an identity provider that OpenProject and the application have in common.class FetchService- include Dry::Monads[:result]- include Dry::Monads::Do.for(:access_token_for, :refreshed_access_token_for)+ include Dry::Monads::Result(TokenOperationError)+ include Dry::Monads::Do.for(:access_token_for)TOKEN_OBTAINED_EVENT = "access_token_obtained"@@ -50,6 +50,7 @@ module OpenIDConnect@token_exchange = token_exchange@token_refresh = token_refresh@jwt_parser = jwt_parser+ @error = TokenOperationError.new(source: self.class)end##@@ -71,41 +72,21 @@ module OpenIDConnectSuccess(token.access_token)end- ##- # Obtains an access token that can be used to make requests in the user's name at the- # remote service identified by the +audience+ parameter.- #- # The access token will always be refreshed before being returned by this method.- # It is advised to use this method, after learning that a remote service rejected- # an access token, because it was expired.- #- # A token exchange is attempted, if the provider supports OAuth 2.0 Token Exchange and a token- # for the target audience either can't be found or it has expired, but has no available refresh token.- def refreshed_access_token_for(audience:)- token = yield token_with_audience(audience)- token = yield @token_refresh.call(token)-- emit_event(token, audience)- Success(token.access_token)- end-privatedef emit_event(token, audience)- OpenProject::Notifications.send(- TOKEN_OBTAINED_EVENT,- audience:,- token:- )+ OpenProject::Notifications.send(TOKEN_OBTAINED_EVENT, audience:, token:)enddef token_with_audience(aud)token = @user.oidc_user_tokens.with_audience(aud).firstreturn Success(token) if token- return @token_exchange.call(aud) if @token_exchange.supported?-- Failure("No token for audience '#{aud}'")+ if @token_exchange.supported?+ @token_exchange.call(aud)+ else+ Failure(@error.with(code: :no_token_for_audience, payload: "No token for audience '#{aud}'"))+ endenddef expired?(token)diff --git a/modules/openid_connect/app/services/openid_connect/user_tokens/refresh_service.rb b/modules/openid_connect/app/services/openid_connect/user_tokens/refresh_service.rbindex 60e3d6d49b7..cb4d139eb8b 100644--- a/modules/openid_connect/app/services/openid_connect/user_tokens/refresh_service.rb+++ b/modules/openid_connect/app/services/openid_connect/user_tokens/refresh_service.rb@@ -2,7 +2,7 @@#-- copyright# OpenProject is an open source project management software.-# Copyright (C) 2012-2024 the OpenProject GmbH+# Copyright (C) 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.@@ -31,7 +31,7 @@module OpenIDConnectmodule UserTokensclass RefreshService- include Dry::Monads[:result]+ include Dry::Monads::Result(TokenOperationError)include Dry::Monads::Do.for(:call)def initialize(user:, token_exchange:)@@ -40,37 +40,37 @@ module OpenIDConnectenddef call(token)- if token.refresh_token.blank?- return exchange_instead_of_refresh(token)- end+ return exchange_instead_of_refresh(token) if token.refresh_token.blank?json = yield refresh_token_request(token.refresh_token)-- access_token = json["access_token"]- refresh_token = json["refresh_token"]- expires_in = json["expires_in"]- return Failure("Refresh token response invalid") if access_token.blank?+ access_token, refresh_token, expires_in = json.values_at("access_token", "refresh_token", "expires_in")+ return failure_with(code: :token_refresh_response_invalid, payload: json) if access_token.blank?token.update!(access_token:, refresh_token:, expires_at: expires_in&.seconds&.from_now)-Success(token)endprivate+ def error = TokenOperationError.new(source: self.class)++ def failure_with(**) = Failure(error.with(**))+def exchange_instead_of_refresh(token)# We can attempt a token exchange instead of a refresh, if we previously exchanged the token.- # For simplicity we do not consider scenarios where the original token had a wider audience,+ # For simplicity, we do not consider scenarios where the original token had a wider audience,# because all tokens obtained through exchange in this service will have exactly one audience.if @token_exchange.supported? && token.audiences.size == 1- return @token_exchange.call(token.audiences.first)+ @token_exchange.call(token.audiences.first)+ else+ failure_with(code: :unable_to_exchange_token)end-- Failure("Can't refresh the access token")enddef refresh_token_request(refresh_token)- TokenRequest.new(provider:).refresh(refresh_token)+ TokenRequest.new(provider:).refresh(refresh_token).alt_map do+ it.with(code: :"token_refresh_#{it.code}", source: self.class)+ endenddef providerdiff --git a/modules/openid_connect/app/services/openid_connect/user_tokens/token_request.rb b/modules/openid_connect/app/services/openid_connect/user_tokens/token_request.rbindex a2ef1d46cb1..36be64a145b 100644--- a/modules/openid_connect/app/services/openid_connect/user_tokens/token_request.rb+++ b/modules/openid_connect/app/services/openid_connect/user_tokens/token_request.rb@@ -2,7 +2,7 @@#-- copyright# OpenProject is an open source project management software.-# Copyright (C) 2012-2024 the OpenProject GmbH+# Copyright (C) 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.@@ -31,7 +31,7 @@module OpenIDConnectmodule UserTokensclass TokenRequest- include Dry::Monads[:result]+ include Dry::Monads::Result(TokenOperationError)attr_reader :provider@@ -45,7 +45,7 @@ module OpenIDConnectdef exchange(access_token, audience)request_token(form: {- grant_type: "urn:ietf:params:oauth:grant-type:token-exchange",+ grant_type: OpenIDConnect::Provider::TOKEN_EXCHANGE_GRANT_TYPE,subject_token: access_token,audience:})@@ -55,11 +55,18 @@ module OpenIDConnectdef request_token(form:)response = authenticated_request.post(provider.token_endpoint, form:)- response.raise_for_status+ error = TokenOperationError.new(payload: response, source: self.class)- Success(response.json)- rescue HTTPX::Error => e- Failure(e)+ case response+ in status: 200+ Success(response.json)+ in status: 401+ Failure(error.with(code: :unauthorized))+ in status: 403+ Failure(error.with(code: :forbidden))+ else+ Failure(error.with(code: :error))+ endenddef authenticated_requestdiff --git a/modules/openid_connect/spec/factories/oidc_provider_factory.rb b/modules/openid_connect/spec/factories/oidc_provider_factory.rbindex ed28a8d6a5c..3f385241eb0 100644--- a/modules/openid_connect/spec/factories/oidc_provider_factory.rb+++ b/modules/openid_connect/spec/factories/oidc_provider_factory.rb@@ -1,3 +1,33 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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.+#+++FactoryBot.define dofactory :oidc_provider, class: "OpenIDConnect::Provider" dodisplay_name { "Foobar" }@@ -22,7 +52,7 @@ FactoryBot.define dotrait :token_exchange_capable docallback(:after_build) do |provider|provider.options["grant_types_supported"] ||= []- provider.options["grant_types_supported"] << "urn:ietf:params:oauth:grant-type:token-exchange"+ provider.options["grant_types_supported"] << OpenIDConnect::Provider::TOKEN_EXCHANGE_GRANT_TYPEendendenddiff --git a/modules/openid_connect/spec/factories/oidc_user_token_factory.rb b/modules/openid_connect/spec/factories/oidc_user_token_factory.rbindex 7c3ede38c5c..806aae374c6 100644--- a/modules/openid_connect/spec/factories/oidc_user_token_factory.rb+++ b/modules/openid_connect/spec/factories/oidc_user_token_factory.rb@@ -1,5 +1,33 @@# frozen_string_literal: true+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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.+#+++FactoryBot.define dofactory :oidc_user_token, class: "OpenIDConnect::UserToken" dotransient do@@ -8,7 +36,8 @@ FactoryBot.define douseraccess_token { "INVALID_TOKEN" }- refresh_token { "COOL_AID_TOKEN" }+ refresh_token { "REFRESH_TOKEN" }+ expires_at { 1.hour.from_now }audiences { [OpenIDConnect::UserToken::IDP_AUDIENCE] + Array(extra_audiences) }endenddiff --git a/modules/openid_connect/spec/services/openid_connect/user_tokens/exchange_service_spec.rb b/modules/openid_connect/spec/services/openid_connect/user_tokens/exchange_service_spec.rbindex 59f0e9f869a..34c0f23e077 100644--- a/modules/openid_connect/spec/services/openid_connect/user_tokens/exchange_service_spec.rb+++ b/modules/openid_connect/spec/services/openid_connect/user_tokens/exchange_service_spec.rb@@ -57,7 +57,7 @@ RSpec.describe OpenIDConnect::UserTokens::ExchangeService, :webmock douser.oidc_user_tokens.create!(access_token: idp_access_token, audiences: [OpenIDConnect::UserToken::IDP_AUDIENCE])user.oidc_user_tokens.create!(access_token:, refresh_token:, audiences: [existing_audience])stub_request(:post, provider.token_endpoint)- .with(body: hash_including(grant_type: "urn:ietf:params:oauth:grant-type:token-exchange"))+ .with(body: hash_including(grant_type: OpenIDConnect::Provider::TOKEN_EXCHANGE_GRANT_TYPE)).to_return(**exchange_response)enddiff --git a/modules/openid_connect/spec/services/openid_connect/user_tokens/fetch_service_spec.rb b/modules/openid_connect/spec/services/openid_connect/user_tokens/fetch_service_spec.rbindex 2cddefdf98f..dd2c7ac469b 100644--- a/modules/openid_connect/spec/services/openid_connect/user_tokens/fetch_service_spec.rb+++ b/modules/openid_connect/spec/services/openid_connect/user_tokens/fetch_service_spec.rb@@ -168,27 +168,4 @@ RSpec.describe OpenIDConnect::UserTokens::FetchService, :webmock doendendend-- describe "#refreshed_access_token_for" do- subject(:result) { service.refreshed_access_token_for(audience: queried_audience) }-- it { is_expected.to be_success }-- it "emits appropriate event" do- allow(OpenProject::Notifications)- .to receive(:send).with(described_class::TOKEN_OBTAINED_EVENT, token: instance_of(OpenIDConnect::UserToken),- audience: queried_audience).once- expect(result.value!).to eq("access-token-refreshed")- end-- it "returns the refreshed access token" do- expect(result.value!).to eq("access-token-refreshed")- end-- context "when audience can't be found" do- let(:queried_audience) { "wrong-audience" }-- it { is_expected.to be_failure }- end- endenddiff --git a/modules/openid_connect/spec/services/openid_connect/user_tokens/token_request_spec.rb b/modules/openid_connect/spec/services/openid_connect/user_tokens/token_request_spec.rbindex 9c0ed132ac1..9d1c6d70f44 100644--- a/modules/openid_connect/spec/services/openid_connect/user_tokens/token_request_spec.rb+++ b/modules/openid_connect/spec/services/openid_connect/user_tokens/token_request_spec.rb@@ -100,7 +100,8 @@ RSpec.describe OpenIDConnect::UserTokens::TokenRequest, :webmock doit { is_expected.not_to be_success }it "returns the error response" do- expect(subject.failure).to be_a(HTTPX::HTTPError)+ expect(subject.failure).to be_a(OpenIDConnect::TokenOperationError)+ expect(subject.failure.code).to eq(:unauthorized)endendend@@ -120,7 +121,7 @@ RSpec.describe OpenIDConnect::UserTokens::TokenRequest, :webmock doit "uses a properly formatted request body" dosubjectexpect(WebMock).to have_requested(:post, provider.token_endpoint)- .with(body: { grant_type: "urn:ietf:params:oauth:grant-type:token-exchange", subject_token: token, audience: })+ .with(body: { grant_type: OpenIDConnect::Provider::TOKEN_EXCHANGE_GRANT_TYPE, subject_token: token, audience: })endit "authenticates the request via HTTP Basic auth using Client ID and Client Secret" do@@ -159,7 +160,8 @@ RSpec.describe OpenIDConnect::UserTokens::TokenRequest, :webmock doit { is_expected.not_to be_success }it "returns the error response" do- expect(subject.failure).to be_a(HTTPX::HTTPError)+ expect(subject.failure).to be_a(OpenIDConnect::TokenOperationError)+ expect(subject.failure.code).to eq(:unauthorized)endendenddiff --git a/modules/storages/app/common/storages/peripherals/nextcloud_connection_validator.rb b/modules/storages/app/common/storages/peripherals/nextcloud_connection_validator.rbindex 43c82566e17..c08982b45bd 100644--- a/modules/storages/app/common/storages/peripherals/nextcloud_connection_validator.rb+++ b/modules/storages/app/common/storages/peripherals/nextcloud_connection_validator.rb@@ -58,7 +58,9 @@ module Storagesdef sso_misconfiguredreturn None() unless @storage.authenticate_via_idp?- non_provisioned_user.or { non_oidc_provisioned_user }.or { token_usability }+ non_provisioned_user+ .or { non_oidc_provisioned_user }+ .or { token_usability }enddef non_provisioned_user@@ -70,20 +72,6 @@ module Storagesdescription: I18n.t("storages.health.connection_validation.oidc_non_provisioned_user")))end- def token_usability- usable_token = @user.oidc_user_tokens.with_audience(@storage.audience).first- return None() if usable_token-- # Temporary error as I implement the next steps- Some(ConnectionValidation- .new(type: :error,- error_code: :oidc_no_token_with_required_audience,- timestamp: Time.current,- description: I18n.t(- "storages.health.connection_validation.oidc_no_token_with_required_audience", audience: @storage.audience- )))- end-def non_oidc_provisioned_userreturn None() if @user.authentication_provider.is_a?(OpenIDConnect::Provider)@@ -93,6 +81,29 @@ module Storagesdescription: I18n.t("storages.health.connection_validation.oidc_non_oidc_user")))end+ def token_usability+ service = OpenIDConnect::UserTokens::FetchService.new(user: @user)++ result = service.access_token_for(audience: @storage.audience)+ return None() if result.success?++ error_code = case result.failure+ in { code: /token_exchange/ | :unable_to_exchange_token }+ :oidc_cant_exchange_token+ in { code: /token_refresh/ }+ :oidc_cant_refresh_token+ in { code: :no_token_for_audience }+ :oidc_cant_acquire_token+ else+ :unknown_error+ end++ Some(ConnectionValidation.new(type: :error,+ error_code:,+ timestamp: Time.current,+ description: I18n.t("storages.health.connection_validation.#{error_code}")))+ end+def has_base_configuration_error?host_url_not_found.or { missing_dependencies }diff --git a/modules/storages/config/locales/en.yml b/modules/storages/config/locales/en.ymlindex e7018b365ab..3b6edbf272f 100644--- a/modules/storages/config/locales/en.yml+++ b/modules/storages/config/locales/en.yml@@ -243,7 +243,9 @@ en:host_not_found: No Nextcloud server found at the configured host url. Please check the configuration.missing_dependencies: 'A required dependency is missing on the file storage. Please add the following dependency: %{dependency}.'not_configured: The connection could not be validated. Please finish configuration first.- oidc_no_token_with_required_audience: There's no OpenID Connect token with the required %{audience} audience.+ oidc_cant_acquire_token: Your OpenID Connect setup doesn't provide the necessary audience, nor provides token exchange capabilities. Please check out our documentation for more information.+ oidc_cant_exchange_token: There seems to be a problem with the Token Exchange setup on your OpenID Connect Provider. Please check its configuration and try again.+ oidc_cant_refresh_token: There was an error while trying to check your access to the storage. Please check the server logs for further information.oidc_non_oidc_user: The current user, while provisioned, wasn't provisioned by an OpenID Connect (OIDC) Identity Provider. Please re-run the check with an OIDC provisioned user.oidc_non_provisioned_user: The current user isn't provided by an OpenID Connect Identity Provider. Please re-run the check with a provided user.placeholder: Check your connection against the server.diff --git a/modules/storages/spec/common/storages/peripherals/nextcloud_connection_validator_spec.rb b/modules/storages/spec/common/storages/peripherals/nextcloud_connection_validator_spec.rbindex 4a522b7e9d7..824fc026b26 100644--- a/modules/storages/spec/common/storages/peripherals/nextcloud_connection_validator_spec.rb+++ b/modules/storages/spec/common/storages/peripherals/nextcloud_connection_validator_spec.rb@@ -31,7 +31,7 @@require "spec_helper"require_module_spec_helper-RSpec.describe Storages::Peripherals::NextcloudConnectionValidator do+RSpec.describe Storages::Peripherals::NextcloudConnectionValidator, :webmock dobefore doStorages::Peripherals::Registry.stub("#{storage}.queries.capabilities", ->(_) { capabilities_response })Storages::Peripherals::Registry.stub("#{storage}.queries.files", ->(_) { files_response })@@ -242,34 +242,34 @@ RSpec.describe Storages::Peripherals::NextcloudConnectionValidator do))end- let(:user) { create(:user) }+ let(:user) { create(:user, identity_url: "#{oidc_provider.slug}:UNIVERSALLY-DUPLICATED-IDENTIFIER") }let!(:oidc_provider) { create(:oidc_provider) }- before { User.current = user }+ before do+ User.current = user++ xml_response = Rails.root.join("modules/storages/spec/support/payloads/nextcloud_user_query_success.xml")++ stub_request(:get, "#{storage.uri}ocs/v1.php/cloud/user")+ .and_return(status: 200, body: File.read(xml_response), headers: { content_type: "text/xml" })+ endit "returns a success" docreate(:oidc_user_token, user:, extra_audiences: storage.audience)- user.update!(identity_url: "#{oidc_provider.slug}:UNIVERSALLY-DUPLICATED-IDENTIFIER")expect(subject.type).to eq(:healthy)expect(subject.error_code).to eq(:none)end- it "returns a validation failure if storage audience isn't set" do- storage.update!(storage_audience: nil)-- expect(subject.type).to eq(:none)- expect(subject.error_code).to eq(:wrn_not_configured)- expect(subject.description).to eq(I18n.t("storages.health.connection_validation.not_configured"))- end+ it "returns a validation warning if the current user isn't provisioned" do+ user.update!(identity_url: nil)- it "returns a validation warning if the current user isn't provided" doexpect(subject.type).to eq(:warning)expect(subject.error_code).to eq(:oidc_non_provisioned_user)expect(subject.description).to eq(I18n.t("storages.health.connection_validation.oidc_non_provisioned_user"))end- it "returns a warning if the user is not provided by an oidc provider" do+ it "returns a warning if the user is not provisioned by an oidc provider" douser.update!(identity_url: "ldap-provider:this-will-trigger-a-warning")expect(subject.type).to eq(:warning)@@ -277,13 +277,92 @@ RSpec.describe Storages::Peripherals::NextcloudConnectionValidator doexpect(subject.description).to eq(I18n.t("storages.health.connection_validation.oidc_non_oidc_user"))end- it "returns an error if the user does not have a usable token" do- user.update!(identity_url: "#{oidc_provider.slug}:UNIVERSALLY-DUPLICATED-IDENTIFIER")+ describe "checks related to the token" do+ let(:user) { create(:user, identity_url: "#{oidc_provider.slug}:UNIVERSALLY-DUPLICATED-IDENTIFIER") }++ context "when the token doesn't have the necessary audiences" do+ it "returns a validation failure in case the server does not support token exchange" do+ create(:oidc_user_token, user:)++ expect(subject.type).to eq(:error)+ expect(subject.error_code).to eq(:oidc_cant_acquire_token)+ expect(subject.description).to eq(I18n.t("storages.health.connection_validation.oidc_cant_acquire_token"))+ end+ end++ describe "token refresh" do+ let(:expired_storage_token) do+ create(:oidc_user_token, user:, extra_audiences: storage.audience, expires_at: 10.hours.ago)+ end++ it "tries to refresh the token if it is expired" do+ refresh_request = stub_request(:post, oidc_provider.token_endpoint)+ .with(body: { grant_type: "refresh_token", refresh_token: expired_storage_token.refresh_token })+ .and_return_json(status: 200, body: { access_token: "NEW_TOKEN" })- expect(subject.type).to eq(:error)- expect(subject.error_code).to eq(:oidc_no_token_with_required_audience)- expect(subject.description)- .to eq(I18n.t("storages.health.connection_validation.oidc_no_token_with_required_audience", audience: storage.audience))+ expect(subject.type).to eq(:healthy)+ expect(refresh_request).to have_been_requested.once+ end++ it "returns a validation failure when the refresh response is invalid" do+ stub_request(:post, oidc_provider.token_endpoint)+ .with(body: { grant_type: "refresh_token", refresh_token: expired_storage_token.refresh_token })+ .and_return_json(status: 200, body: { error: "this is a broken endpoint" })++ expect(subject.type).to eq(:error)+ expect(subject.error_code).to eq(:oidc_cant_refresh_token)+ expect(subject.description)+ .to eq(I18n.t("storages.health.connection_validation.oidc_cant_refresh_token"))+ end++ it "returns validation failure about an unusable token if refresh fails" do+ stub_request(:post, oidc_provider.token_endpoint)+ .with(body: { grant_type: "refresh_token", refresh_token: expired_storage_token.refresh_token })+ .and_return(status: 401)++ expect(subject.type).to eq(:error)+ expect(subject.error_code).to eq(:oidc_cant_refresh_token)+ expect(subject.description)+ .to eq(I18n.t("storages.health.connection_validation.oidc_cant_refresh_token"))+ end++ context "when the server supports token exchange" do+ let(:oidc_provider) { create(:oidc_provider, :token_exchange_capable) }+ let(:exchangeable_token) { create(:oidc_user_token, user:, refresh_token: nil) }++ it "perform favors token exchange when refreshing" do+ exchange_request = stub_request(:post, oidc_provider.token_endpoint)+ .with(body: { audience: storage.audience, subject_token: exchangeable_token.access_token,+ grant_type: OpenIDConnect::Provider::TOKEN_EXCHANGE_GRANT_TYPE })+ .and_return_json(status: 200, body: { access_token: "NEW_TOKEN" })++ expect(subject.type).to eq(:healthy)+ expect(exchange_request).to have_been_requested.once+ end++ it "returns a validation failure if the exchange fails with an unexpected body" do+ exchange_request = stub_request(:post, oidc_provider.token_endpoint)+ .with(body: { audience: storage.audience, subject_token: exchangeable_token.access_token,+ grant_type: OpenIDConnect::Provider::TOKEN_EXCHANGE_GRANT_TYPE })+ .and_return_json(status: 200, body: { error: "failed " })++ expect(subject.type).to eq(:error)+ expect(subject.error_code).to eq(:oidc_cant_exchange_token)+ expect(exchange_request).to have_been_requested.once+ end++ it "returns a validation failure if the exchange fails" do+ exchange_request = stub_request(:post, oidc_provider.token_endpoint)+ .with(body: { audience: storage.audience, subject_token: exchangeable_token.access_token,+ grant_type: OpenIDConnect::Provider::TOKEN_EXCHANGE_GRANT_TYPE })+ .and_return(status: 401)++ expect(subject.type).to eq(:error)+ expect(subject.error_code).to eq(:oidc_cant_exchange_token)+ expect(exchange_request).to have_been_requested.once+ end+ end+ endendend