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

Add configuration for authentication method

Jan Sandbrink5ecc78e60d90
diff --git a/modules/storages/app/contracts/storages/storages/nextcloud_contract.rb b/modules/storages/app/contracts/storages/storages/nextcloud_contract.rb
index 7f718f7e9d5..d9d218132dd 100644
--- a/modules/storages/app/contracts/storages/storages/nextcloud_contract.rb
+++ b/modules/storages/app/contracts/storages/storages/nextcloud_contract.rb
@@ -36,6 +36,12 @@ module Storages::Storages
# But only do so if the validations above for URL were successful.
validates :host, secure_context_uri: true, nextcloud_compatible_host: true, unless: -> { errors.include?(:host) }
+ attribute :authentication_method
+ validates :authentication_method, presence: true, inclusion: { in: ::Storages::NextcloudStorage::AUTHENTICATION_METHODS }
+
+ attribute :nextcloud_audience
+ validates :nextcloud_audience, presence: true, if: :nextcloud_storage_authenticate_via_idp?
+
attribute :automatically_managed
attribute :username
@@ -68,6 +74,10 @@ module Storages::Storages
@model.username == @model.provider_fields_defaults[:username]
end
+ def nextcloud_storage_authenticate_via_idp?
+ nextcloud_storage? && @model.authenticate_via_idp?
+ end
+
def nextcloud_storage?
@model.is_a?(Storages::NextcloudStorage)
end
diff --git a/modules/storages/app/models/storages/nextcloud_storage.rb b/modules/storages/app/models/storages/nextcloud_storage.rb
index 29feae12367..51ddfb1f1f5 100644
--- a/modules/storages/app/models/storages/nextcloud_storage.rb
+++ b/modules/storages/app/models/storages/nextcloud_storage.rb
@@ -35,11 +35,15 @@ module Storages
username: "OpenProject"
}.freeze
+ AUTHENTICATION_METHODS = %w[two_way_oauth2 oauth2_sso].freeze
+
store_attribute :provider_fields, :automatically_managed, :boolean
store_attribute :provider_fields, :username, :string
store_attribute :provider_fields, :password, :string
store_attribute :provider_fields, :group, :string
store_attribute :provider_fields, :group_folder, :string
+ store_attribute :provider_fields, :authentication_method, :string, default: "two_way_oauth2"
+ store_attribute :provider_fields, :nextcloud_audience, :string
def oauth_configuration
Peripherals::OAuthConfigurations::NextcloudConfiguration.new(self)
@@ -62,6 +66,10 @@ module Storages
end
end
+ def authenticate_via_idp?
+ authentication_method == "oauth2_sso"
+ end
+
def configuration_checks
{
storage_oauth_client_configured: oauth_client.present?,
diff --git a/modules/storages/db/migrate/20250115100336_set_default_authentication_method.rb b/modules/storages/db/migrate/20250115100336_set_default_authentication_method.rb
new file mode 100644
index 00000000000..28a045c691d
--- /dev/null
+++ b/modules/storages/db/migrate/20250115100336_set_default_authentication_method.rb
@@ -0,0 +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.
+#++
+
+class SetDefaultAuthenticationMethod < ActiveRecord::Migration[7.1]
+ def change
+ update_sql = <<~SQL.squish
+ UPDATE storages
+ SET provider_fields = provider_fields || '{ "authentication_method": "two_way_oauth2" }'::jsonb
+ WHERE provider_type = 'Storages::NextcloudStorage' AND NOT provider_fields ? 'authentication_method'
+ SQL
+
+ ActiveRecord::Base.connection.execute(update_sql)
+ end
+end
diff --git a/modules/storages/spec/contracts/storages/storages/nextcloud_contract_spec.rb b/modules/storages/spec/contracts/storages/storages/nextcloud_contract_spec.rb
index 147626b4055..8499a238cba 100644
--- a/modules/storages/spec/contracts/storages/storages/nextcloud_contract_spec.rb
+++ b/modules/storages/spec/contracts/storages/storages/nextcloud_contract_spec.rb
@@ -33,16 +33,17 @@ require_module_spec_helper
RSpec.describe Storages::Storages::NextcloudContract, :storage_server_helpers, :webmock do
let(:current_user) { create(:admin) }
- let(:storage_host) { "https://host1.example.com" }
- let(:storage) { build(:nextcloud_storage, host: storage_host) }
+ let(:storage) { build(:nextcloud_storage) }
+ let(:mocked_host) { storage.host }
+
+ let!(:capabilities_request) { mock_server_capabilities_response(mocked_host) }
+ let!(:host_request) { mock_server_config_check_response(mocked_host) }
# As the NextcloudContract is selected by the BaseContract to make writable attributes available,
# the BaseContract needs to be instantiated here.
subject { Storages::Storages::BaseContract.new(storage, current_user) }
it "checks the storage url only when changed" do
- capabilities_request = mock_server_capabilities_response(storage_host)
- host_request = mock_server_config_check_response(storage_host)
subject.validate
expect(capabilities_request).to have_been_made.once
expect(host_request).to have_been_made.once
@@ -58,11 +59,6 @@ RSpec.describe Storages::Storages::NextcloudContract, :storage_server_helpers, :
context "with valid credentials" do
let(:storage) { build(:nextcloud_storage, :as_automatically_managed) }
- before do
- mock_server_capabilities_response(storage.host)
- mock_server_config_check_response(storage.host)
- end
-
it "passes validation" do
credentials_request = mock_nextcloud_application_credentials_validation(storage.host)
@@ -137,6 +133,7 @@ RSpec.describe Storages::Storages::NextcloudContract, :storage_server_helpers, :
context "when the storage host is nil" do
let(:storage) { build(:nextcloud_storage, :as_automatically_managed, host: nil) }
+ let(:mocked_host) { "https://example.com/unrelated" }
before do
allow(NextcloudApplicationCredentialsValidator).to receive(:new).and_call_original
@@ -149,4 +146,67 @@ RSpec.describe Storages::Storages::NextcloudContract, :storage_server_helpers, :
end
end
end
+
+ describe "authentication_method validation" do
+ let(:storage) { build(:nextcloud_storage, :as_not_automatically_managed, authentication_method:) }
+ let(:authentication_method) { "two_way_oauth2" }
+
+ it { is_expected.to be_valid }
+
+ context "when the authentication method is oauth2_sso" do
+ let(:authentication_method) { "oauth2_sso" }
+
+ it { is_expected.to be_valid }
+ end
+
+ context "when the authentication method is unknown" do
+ let(:authentication_method) { "magic_unicorns" }
+
+ it { is_expected.not_to be_valid }
+ end
+
+ context "when the authentication method is missing" do
+ let(:authentication_method) { nil }
+
+ it { is_expected.not_to be_valid }
+ end
+ end
+
+ describe "nextcloud_audience validation" do
+ let(:storage) do
+ build(:nextcloud_storage, :as_not_automatically_managed, authentication_method:, nextcloud_audience:)
+ end
+
+ context "when authentication happens through bidirectional OAuth 2.0" do
+ let(:authentication_method) { "two_way_oauth2" }
+
+ context "and there is no nextcloud_audience" do
+ let(:nextcloud_audience) { nil }
+
+ it { is_expected.to be_valid }
+ end
+
+ context "and there is a nextcloud_audience" do
+ let(:nextcloud_audience) { "nextcloud" }
+
+ it { is_expected.to be_valid }
+ end
+ end
+
+ context "when authentication happens through a common IDP" do
+ let(:authentication_method) { "oauth2_sso" }
+
+ context "and there is no nextcloud_audience" do
+ let(:nextcloud_audience) { nil }
+
+ it { is_expected.not_to be_valid }
+ end
+
+ context "and there is a nextcloud_audience" do
+ let(:nextcloud_audience) { "nextcloud" }
+
+ it { is_expected.to be_valid }
+ end
+ end
+ end
end
diff --git a/modules/storages/spec/contracts/storages/storages/shared_contract_examples.rb b/modules/storages/spec/contracts/storages/storages/shared_contract_examples.rb
index ee4f284a5d9..4ffb31a273f 100644
--- a/modules/storages/spec/contracts/storages/storages/shared_contract_examples.rb
+++ b/modules/storages/spec/contracts/storages/storages/shared_contract_examples.rb
@@ -294,8 +294,7 @@ RSpec.shared_examples_for "nextcloud storage contract", :storage_server_helpers,
context "when not automatically managed, no username or password" do
before do
- storage.provider_fields = {}
- storage.assign_attributes(automatic_management_enabled: false)
+ storage.assign_attributes(automatic_management_enabled: false, username: nil, password: nil)
end
it_behaves_like "contract is valid"
diff --git a/modules/storages/spec/factories/storage_factory.rb b/modules/storages/spec/factories/storage_factory.rb
index f31ed752dd2..8dae7ab0714 100644
--- a/modules/storages/spec/factories/storage_factory.rb
+++ b/modules/storages/spec/factories/storage_factory.rb
@@ -91,6 +91,8 @@ FactoryBot.define do
class: "::Storages::NextcloudStorage" do
provider_type { Storages::Storage::PROVIDER_TYPE_NEXTCLOUD }
sequence(:host) { |n| "https://host#{n}.example.com/" }
+ authentication_method { "two_way_oauth2" }
+ nextcloud_audience { "nextcloud" }
trait :as_automatically_managed do
automatic_management_enabled { true }