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

[#60151] enable nextcloud storages with sso strategy

Eric Schubert52a670c48712
diff --git a/docker/dev/keycloak/docker-compose.yml b/docker/dev/keycloak/docker-compose.yml
index 587e93744ab..65437087c79 100644
--- a/docker/dev/keycloak/docker-compose.yml
+++ b/docker/dev/keycloak/docker-compose.yml
@@ -1,5 +1,3 @@
-version: "3.9"
-
services:
db-keycloak:
image: postgres:13
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication.rb
index 28617553f9c..19494f7c514 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication.rb
@@ -40,6 +40,8 @@ module Storages
AuthenticationStrategies::Noop.new
when :basic_auth
AuthenticationStrategies::BasicAuth.new
+ when :sso_user_token
+ AuthenticationStrategies::SsoUserToken.new(strategy.user)
when :oauth_user_token
AuthenticationStrategies::OAuthUserToken.new(strategy.user)
when :oauth_client_credentials
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/nextcloud_strategies.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/nextcloud_strategies.rb
index 131a4b762de..69ea9acd007 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/nextcloud_strategies.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/nextcloud_strategies.rb
@@ -37,10 +37,20 @@ module Storages
::Storages::Peripherals::StorageInteraction::AuthenticationStrategies::BasicAuth.strategy
end
- UserBound = ->(user:) do
- ::Storages::Peripherals::StorageInteraction::AuthenticationStrategies::OAuthUserToken
- .strategy
- .with_user(user)
+ UserBound = ->(user:, storage:) do
+ sso_preferred = storage.audience.present? && user.authentication_provider.present?
+
+ if sso_preferred
+ ::Storages::Peripherals::StorageInteraction::AuthenticationStrategies::SsoUserToken
+ .strategy
+ .with_user(user)
+ elsif storage.oauth_configuration.present?
+ ::Storages::Peripherals::StorageInteraction::AuthenticationStrategies::OAuthUserToken
+ .strategy
+ .with_user(user)
+ else
+ ::Storages::Peripherals::StorageInteraction::AuthenticationStrategies::Noop.strategy
+ end
end
end
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/one_drive_strategies.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/one_drive_strategies.rb
index df69b3d4685..7833c61c6c1 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/one_drive_strategies.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/one_drive_strategies.rb
@@ -37,11 +37,13 @@ module Storages
::Storages::Peripherals::StorageInteraction::AuthenticationStrategies::OAuthClientCredentials.strategy
end
- UserBound = ->(user:) do
+ # rubocop:disable Lint/UnusedBlockArgument
+ UserBound = ->(user:, storage:) do
::Storages::Peripherals::StorageInteraction::AuthenticationStrategies::OAuthUserToken
.strategy
.with_user(user)
end
+ # rubocop:enable Lint/UnusedBlockArgument
end
end
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/sso_user_token.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/sso_user_token.rb
new file mode 100644
index 00000000000..6e817e5ddc7
--- /dev/null
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/sso_user_token.rb
@@ -0,0 +1,62 @@
+# 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 Storages
+ module Peripherals
+ module StorageInteraction
+ module AuthenticationStrategies
+ class SsoUserToken
+ def self.strategy
+ Strategy.new(:sso_user_token)
+ end
+
+ def initialize(user)
+ @user = user
+ end
+
+ def call(storage:, http_options: {}, &)
+ OpenIDConnect::UserTokens::FetchService
+ .new(user: @user)
+ .access_token_for(audience: storage.audience)
+ .either(
+ ->(token) do
+ opts = http_options.deep_merge({ headers: { "Authorization" => "Bearer #{token}" } })
+ yield OpenProject.httpx.with(opts)
+ end,
+ ->(error) do
+ Failures::Builder.call(code: :unauthorized, log_message: error.message)
+ end
+ )
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rb
index 4ea4c380974..e04c5c1ef23 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rb
@@ -70,7 +70,7 @@ module Storages
case auth_strategy.key
when :basic_auth
ServiceResult.success(result: storage.username)
- when :oauth_user_token
+ when :oauth_user_token || :sso_user_token
origin_user_id = RemoteIdentity.where(user_id: auth_strategy.user, oauth_client: storage.oauth_client)
.pick(:origin_user_id)
if origin_user_id.present?
diff --git a/modules/storages/app/models/storages/nextcloud_storage.rb b/modules/storages/app/models/storages/nextcloud_storage.rb
index 51ddfb1f1f5..6fd5cbc2a52 100644
--- a/modules/storages/app/models/storages/nextcloud_storage.rb
+++ b/modules/storages/app/models/storages/nextcloud_storage.rb
@@ -66,6 +66,10 @@ module Storages
end
end
+ def audience
+ nextcloud_audience
+ end
+
def authenticate_via_idp?
authentication_method == "oauth2_sso"
end
diff --git a/modules/storages/app/models/storages/one_drive_storage.rb b/modules/storages/app/models/storages/one_drive_storage.rb
index 3c4f9cfc0ff..aff7f7c877f 100644
--- a/modules/storages/app/models/storages/one_drive_storage.rb
+++ b/modules/storages/app/models/storages/one_drive_storage.rb
@@ -65,6 +65,10 @@ module Storages
end
end
+ def audience
+ nil
+ end
+
def oauth_configuration
Peripherals::OAuthConfigurations::OneDriveConfiguration.new(self)
end
diff --git a/modules/storages/app/models/storages/storage.rb b/modules/storages/app/models/storages/storage.rb
index b223c8ec3cb..ae6530096f5 100644
--- a/modules/storages/app/models/storages/storage.rb
+++ b/modules/storages/app/models/storages/storage.rb
@@ -147,6 +147,13 @@ module Storages
raise Errors::SubclassResponsibility
end
+ # Returns a value of an audience, if configured for this storage.
+ # The presence of an audience signals that this storage prioritizes
+ # remote authentication over token exchange if possible.
+ def audience
+ raise Errors::SubclassResponsibility
+ end
+
def configured?
configuration_checks.values.all?
end
diff --git a/modules/storages/app/services/storages/create_folder_service.rb b/modules/storages/app/services/storages/create_folder_service.rb
index 8c9f2ed1d35..7e7aa7815e2 100644
--- a/modules/storages/app/services/storages/create_folder_service.rb
+++ b/modules/storages/app/services/storages/create_folder_service.rb
@@ -37,7 +37,7 @@ module Storages
end
def call(storage:, user:, name:, parent_id:)
- auth_strategy = Peripherals::Registry.resolve("#{storage}.authentication.user_bound").call(user: user)
+ auth_strategy = Peripherals::Registry.resolve("#{storage}.authentication.user_bound").call(user:, storage:)
Peripherals::Registry
.resolve("#{storage}.commands.create_folder")
diff --git a/modules/storages/app/services/storages/file_link_sync_service.rb b/modules/storages/app/services/storages/file_link_sync_service.rb
index 1b7982786e4..d3fcad46f77 100644
--- a/modules/storages/app/services/storages/file_link_sync_service.rb
+++ b/modules/storages/app/services/storages/file_link_sync_service.rb
@@ -79,7 +79,7 @@ module Storages
end
def strategy(storage)
- Peripherals::Registry.resolve("#{storage}.authentication.user_bound").call(user: @user)
+ Peripherals::Registry.resolve("#{storage}.authentication.user_bound").call(user: @user, storage:)
end
def to_hash(file_infos)
diff --git a/modules/storages/app/services/storages/storage_file_service.rb b/modules/storages/app/services/storages/storage_file_service.rb
index 6b664d92f1a..ed8429fdd7f 100644
--- a/modules/storages/app/services/storages/storage_file_service.rb
+++ b/modules/storages/app/services/storages/storage_file_service.rb
@@ -44,7 +44,7 @@ module Storages
private
def strategy(storage, user)
- Peripherals::Registry.resolve("#{storage}.authentication.user_bound").call(user:)
+ Peripherals::Registry.resolve("#{storage}.authentication.user_bound").call(user:, storage:)
end
end
end
diff --git a/modules/storages/app/services/storages/storage_files_service.rb b/modules/storages/app/services/storages/storage_files_service.rb
index ce4ab8f9a4b..be0e9ba9f26 100644
--- a/modules/storages/app/services/storages/storage_files_service.rb
+++ b/modules/storages/app/services/storages/storage_files_service.rb
@@ -44,7 +44,7 @@ module Storages
private
def strategy(storage, user)
- Peripherals::Registry.resolve("#{storage}.authentication.user_bound").call(user:)
+ Peripherals::Registry.resolve("#{storage}.authentication.user_bound").call(user:, storage:)
end
end
end
diff --git a/modules/storages/app/services/storages/upload_link_service.rb b/modules/storages/app/services/storages/upload_link_service.rb
index dfc4b7017e3..864921ea389 100644
--- a/modules/storages/app/services/storages/upload_link_service.rb
+++ b/modules/storages/app/services/storages/upload_link_service.rb
@@ -74,7 +74,7 @@ module Storages
end
def auth_strategy(user)
- Peripherals::Registry.resolve("#{@storage}.authentication.user_bound").call(user:)
+ Peripherals::Registry.resolve("#{@storage}.authentication.user_bound").call(user:, storage: @storage)
end
end
end
diff --git a/modules/storages/lib/api/v3/storage_files/storage_files_api.rb b/modules/storages/lib/api/v3/storage_files/storage_files_api.rb
index e5749975862..5c92deabb1a 100644
--- a/modules/storages/lib/api/v3/storage_files/storage_files_api.rb
+++ b/modules/storages/lib/api/v3/storage_files/storage_files_api.rb
@@ -57,7 +57,8 @@ module API::V3::StorageFiles
end
def auth_strategy
- Storages::Peripherals::Registry.resolve("#{@storage}.authentication.user_bound").call(user: current_user)
+ Storages::Peripherals::Registry.resolve("#{@storage}.authentication.user_bound")
+ .call(user: current_user, storage: @storage)
end
end
diff --git a/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/rename_file_command_spec.rb b/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/rename_file_command_spec.rb
index 41077cd988b..22ecd3f447b 100644
--- a/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/rename_file_command_spec.rb
+++ b/modules/storages/spec/common/storages/peripherals/storage_interaction/nextcloud/rename_file_command_spec.rb
@@ -36,7 +36,9 @@ RSpec.describe Storages::Peripherals::StorageInteraction::Nextcloud::RenameFileC
let(:storage) do
create(:nextcloud_storage_with_local_connection, :as_not_automatically_managed, oauth_client_token_user: user)
end
- let(:auth_strategy) { Storages::Peripherals::Registry.resolve("nextcloud.authentication.user_bound").call(user:) }
+ let(:auth_strategy) do
+ Storages::Peripherals::Registry.resolve("nextcloud.authentication.user_bound").call(user:, storage:)
+ end
it_behaves_like "rename_file_command: basic command setup"
diff --git a/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/file_info_query_spec.rb b/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/file_info_query_spec.rb
index c7ab116c6df..c320fbde53d 100644
--- a/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/file_info_query_spec.rb
+++ b/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/file_info_query_spec.rb
@@ -36,7 +36,7 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::FileInfoQuer
let(:storage) { create(:sharepoint_dev_drive_storage, oauth_client_token_user: user) }
let(:auth_strategy) do
- Storages::Peripherals::Registry["one_drive.authentication.user_bound"].call(user:)
+ Storages::Peripherals::Registry["one_drive.authentication.user_bound"].call(user:, storage:)
end
it_behaves_like "file_info_query: basic query setup"
diff --git a/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/open_file_link_query_spec.rb b/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/open_file_link_query_spec.rb
index c34f467162c..2cd74a9ec51 100644
--- a/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/open_file_link_query_spec.rb
+++ b/modules/storages/spec/common/storages/peripherals/storage_interaction/one_drive/open_file_link_query_spec.rb
@@ -38,7 +38,7 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::OpenFileLink
let(:storage) { create(:sharepoint_dev_drive_storage, oauth_client_token_user: user) }
let(:file_id) { "01AZJL5PJTICED3C5YSVAY6NWTBNA2XERU" }
let(:auth_strategy) do
- Storages::Peripherals::Registry.resolve("one_drive.authentication.user_bound").call(user:)
+ Storages::Peripherals::Registry.resolve("one_drive.authentication.user_bound").call(user:, storage:)
end
subject { described_class.new(storage) }
diff --git a/modules/storages/spec/services/storages/create_folder_service_spec.rb b/modules/storages/spec/services/storages/create_folder_service_spec.rb
index d4c6a96ce2d..84ea8b999bb 100644
--- a/modules/storages/spec/services/storages/create_folder_service_spec.rb
+++ b/modules/storages/spec/services/storages/create_folder_service_spec.rb
@@ -38,7 +38,7 @@ RSpec.describe Storages::CreateFolderService do
let(:name) { "TestFolderName" }
context "when storage is nextcloud" do
- let(:storage) { create(:nextcloud_storage) }
+ let(:storage) { create(:nextcloud_storage_configured) }
let(:parent_id) { file_info.id }
let(:file_info) do
diff --git a/modules/storages/spec/services/storages/file_links/file_link_sync_service_spec.rb b/modules/storages/spec/services/storages/file_links/file_link_sync_service_spec.rb
index 43f2268f1f7..e4c607ae993 100644
--- a/modules/storages/spec/services/storages/file_links/file_link_sync_service_spec.rb
+++ b/modules/storages/spec/services/storages/file_links/file_link_sync_service_spec.rb
@@ -37,8 +37,8 @@ RSpec.describe Storages::FileLinkSyncService, type: :model do
let(:project) { create(:project, members: { user => role }) }
let(:work_package) { create(:work_package, project:) }
- let(:storage_one) { create(:nextcloud_storage, host: "https://host-1.example.org") }
- let(:storage_two) { create(:nextcloud_storage, host: "https://host-2.example.org") }
+ let(:storage_one) { create(:nextcloud_storage_configured, host: "https://host-1.example.org") }
+ let(:storage_two) { create(:nextcloud_storage_configured, host: "https://host-2.example.org") }
let(:file_link_one) { create(:file_link, storage: storage_one, container: work_package) }
let(:file_link_two) { create(:file_link, storage: storage_two, container: work_package) }