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

Implements the Authentication Configuration Checks (#18377)

Marcello Rocha1f57cdc487fb
diff --git a/modules/storages/app/common/storages/peripherals/connection_validators/check_result.rb b/modules/storages/app/common/storages/peripherals/connection_validators/check_result.rb
index 79d39939bb8..b489a816e6f 100644
--- a/modules/storages/app/common/storages/peripherals/connection_validators/check_result.rb
+++ b/modules/storages/app/common/storages/peripherals/connection_validators/check_result.rb
@@ -45,8 +45,14 @@ module Storages
new(key:, state: :success, message: nil, timestamp: Time.zone.now)
end
+ def self.warning(key, message)
+ new(key:, state: :warning, message: message, timestamp: Time.zone.now)
+ end
+
def success? = state == :success
def failure? = state == :failure
+ def warning? = state == :warning
+ def skipped? = state == :skipped
end
end
end
diff --git a/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/authentication_validator.rb b/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/authentication_validator.rb
new file mode 100644
index 00000000000..6289b27dac3
--- /dev/null
+++ b/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/authentication_validator.rb
@@ -0,0 +1,120 @@
+# 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 ConnectionValidators
+ module Nextcloud
+ class AuthenticationValidator < BaseValidator
+ def initialize(storage)
+ super
+ @user = User.current
+ end
+
+ private
+
+ def validate
+ @storage.authenticate_via_idp? ? validate_sso : validate_oauth
+ end
+
+ def validate_oauth
+ register_checks(:existing_token, :user_bound_request)
+
+ oauth_token
+ user_bound_request
+ end
+
+ def oauth_token
+ if OAuthClientToken.where(user: @user, oauth_client: @storage.oauth_client).any?
+ pass_check(:existing_token)
+ else
+ warn_check(:existing_token, message(:oauth_token_missing), halt_validation: true)
+ end
+ end
+
+ def user_bound_request
+ Registry["nextcloud.queries.user"].call(storage: @storage, auth_strategy:).on_failure do
+ fail_check(:user_bound_request, message("oauth_request_#{it.result}"))
+ end
+
+ pass_check(:user_bound_request)
+ end
+
+ def auth_strategy = Registry["nextcloud.authentication.user_bound"].call(storage: @storage, user: @user)
+
+ def validate_sso
+ register_checks(:non_provisioned_user, :provisioned_user_provider, :token_negotiable, :user_bound_request)
+
+ non_provisioned_user
+ non_oidc_provisioned_user
+ token_negotiable
+ user_bound_request
+ end
+
+ def non_provisioned_user
+ if @user.identity_url.present?
+ pass_check(:non_provisioned_user)
+ else
+ warn_check(:non_provisioned_user, message(:oidc_non_provisioned_user), halt_validation: true)
+ end
+ end
+
+ def non_oidc_provisioned_user
+ if @user.authentication_provider.is_a?(OpenIDConnect::Provider)
+ pass_check(:provisioned_user_provider)
+ else
+ warn_check(:provisioned_user_provider, message(:oidc_non_oidc_user), halt_validation: true)
+ end
+ end
+
+ def token_negotiable
+ service = OpenIDConnect::UserTokens::FetchService.new(user: @user)
+
+ result = service.access_token_for(audience: @storage.audience)
+ return pass_check(:token_negotiable) 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
+
+ fail_check(:token_negotiable, message(error_code))
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/base_configuration_validator.rb b/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/base_configuration_validator.rb
index 909a575439a..1d64b0b2982 100644
--- a/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/base_configuration_validator.rb
+++ b/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/base_configuration_validator.rb
@@ -32,30 +32,23 @@ module Storages
module Peripherals
module ConnectionValidators
module Nextcloud
- class BaseConfigurationValidator
- def initialize(storage)
- @storage = storage
- @results = build_result_list
- end
+ class BaseConfigurationValidator < BaseValidator
+ private
- def call
- catch :interrupted do
- capabilities_request_failed
- host_url_not_found
- missing_dependencies
- version_mismatch
- end
+ def validate
+ register_checks(:capabilities_request, :host_url_accessible, :dependencies_check, :dependencies_versions)
- @results
+ capabilities_request_status
+ host_url_not_found
+ missing_dependencies
+ version_mismatch
end
- private
-
- def capabilities_request_failed
+ def capabilities_request_status
if capabilities.failure? && capabilities.result != :not_found
- fail_check(__method__, message(:error))
+ fail_check(:capabilities_request, message(:error))
else
- pass_check(__method__)
+ pass_check(:capabilities_request)
end
end
@@ -64,9 +57,9 @@ module Storages
capabilities_result = capabilities.result
if capabilities_result.app_version < min_app_version
- fail_check(__method__, message(:app_version_mismatch))
+ fail_check(:dependencies_versions, message(:app_version_mismatch))
else
- pass_check(__method__)
+ pass_check(:dependencies_versions)
end
end
@@ -75,24 +68,20 @@ module Storages
app_name = I18n.t("storages.dependencies.nextcloud.integration_app")
if capabilities_result.app_disabled?
- fail_check(__method__, message(:missing_dependencies, dependency: app_name))
+ fail_check(:dependencies_check, message(:missing_dependencies, dependency: app_name))
else
- pass_check(__method__)
+ pass_check(:dependencies_check)
end
end
def host_url_not_found
if capabilities.result == :not_found
- fail_check(__method__, message(:host_not_found))
+ fail_check(:host_url_accessible, message(:host_not_found))
else
- pass_check(__method__)
+ pass_check(:host_url_accessible)
end
end
- def message(key, context = {})
- I18n.t("storages.health.connection_validation.#{key}", **context)
- end
-
def noop = StorageInteraction::AuthenticationStrategies::Noop.strategy
def capabilities
@@ -105,28 +94,6 @@ module Storages
end
def path_to_config = Rails.root.join("modules/storages/config/nextcloud_dependencies.yml")
-
- def fail_check(key, message)
- update_result(key, CheckResult.failure(key, message))
- throw :interrupted
- end
-
- def pass_check(key)
- update_result(key, CheckResult.success(key))
- end
-
- def update_result(method, value)
- @results[method.to_sym] = value
- end
-
- def build_result_list
- {
- capabilities_request_failed: CheckResult.skipped(:capabilities_request_failed),
- host_url_not_found: CheckResult.skipped(:host_url_not_found),
- missing_dependencies: CheckResult.skipped(:missing_dependencies),
- version_mismatch: CheckResult.skipped(:version_mismatch)
- }
- end
end
end
end
diff --git a/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/base_validator.rb b/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/base_validator.rb
new file mode 100644
index 00000000000..bd8c3be4d1e
--- /dev/null
+++ b/modules/storages/app/common/storages/peripherals/connection_validators/nextcloud/base_validator.rb
@@ -0,0 +1,85 @@
+# 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 ConnectionValidators
+ module Nextcloud
+ class BaseValidator
+ def initialize(storage)
+ @storage = storage
+ end
+
+ def call
+ catch :interrupted do
+ validate
+ end
+
+ @results
+ end
+
+ private
+
+ def validate = raise Errors::SubclassResponsibility
+
+ def register_checks(*keys)
+ @results = keys.to_h { [it, CheckResult.skipped(it)] }
+ end
+
+ def update_result(key, value)
+ if @results&.has_key?(key)
+ @results[key] = value
+ else
+ raise ArgumentError, "Check #{key} not registered."
+ end
+ end
+
+ def pass_check(key)
+ update_result(key, CheckResult.success(key))
+ end
+
+ def fail_check(key, message)
+ update_result(key, CheckResult.failure(key, message))
+ throw :interrupted
+ end
+
+ def warn_check(key, message, halt_validation: false)
+ update_result(key, CheckResult.warning(key, message))
+ throw :interrupted if halt_validation
+ end
+
+ def message(key, context = {})
+ I18n.t("storages.health.connection_validation.#{key}", **context)
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/modules/storages/config/locales/en.yml b/modules/storages/config/locales/en.yml
index 81d3cf9feef..b444f8424dc 100644
--- a/modules/storages/config/locales/en.yml
+++ b/modules/storages/config/locales/en.yml
@@ -243,6 +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.
+ oauth_request_not_found: OpenProject couldn't find the root folder of Nextcloud using the current logged-in user. Please check the server logs for further information.
+ oauth_request_unauthorized: The current user isn't authorized to see the main folder of Nextcloud or we couldn't acquire a token. Please check the server logs for further information.
+ oauth_token_missing: OpenProject cannot test the user level communication with Nextcloud as there's no token for the current user.
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.
diff --git a/modules/storages/spec/common/storages/peripherals/connection_validators/nextcloud/authentication_validator_spec.rb b/modules/storages/spec/common/storages/peripherals/connection_validators/nextcloud/authentication_validator_spec.rb
new file mode 100644
index 00000000000..97370e73b37
--- /dev/null
+++ b/modules/storages/spec/common/storages/peripherals/connection_validators/nextcloud/authentication_validator_spec.rb
@@ -0,0 +1,221 @@
+# 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.
+#++
+
+require "spec_helper"
+require_module_spec_helper
+
+module Storages
+ module Peripherals
+ module ConnectionValidators
+ module Nextcloud
+ RSpec.describe AuthenticationValidator, :webmock do
+ subject(:validator) { described_class.new(storage) }
+
+ context "when using OAuth2" do
+ let(:user) { create(:user) }
+ let(:storage) do
+ create(:nextcloud_storage_with_local_connection, :as_not_automatically_managed,
+ oauth_client_token_user: user, origin_user_id: "[email protected]")
+ end
+
+ before { User.current = user }
+
+ it "passes when the user has a token and the request works", vcr: "nextcloud/user_query_success" do
+ result = validator.call
+
+ expect(result.values).to all(be_success)
+ end
+
+ it "returns a warning when there's no token for the current user" do
+ User.current = create(:user)
+ result = validator.call
+
+ expect(result[:existing_token]).to be_a_warning
+ expect(result[:existing_token].message).to eq(I18n.t(i18n_key(:oauth_token_missing)))
+ expect(result[:user_bound_request]).to be_skipped
+ end
+
+ it "returns a failure if the remote call failed" do
+ Registry.stub("nextcloud.queries.user", ->(_) { ServiceResult.failure(result: :unauthorized) })
+
+ result = validator.call
+ expect(result[:user_bound_request]).to be_a_failure
+ expect(result[:user_bound_request].message).to eq(I18n.t(i18n_key(:oauth_request_unauthorized)))
+ end
+ end
+
+ context "when using OpenID Connect" do
+ let(:storage) { create(:nextcloud_storage_configured, :oidc_sso_enabled) }
+
+ let(:user) { create(:user, identity_url: "#{oidc_provider.slug}:UNIVERSALLY-DUPLICATED-IDENTIFIER") }
+ let!(:oidc_provider) { create(:oidc_provider) }
+
+ 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" })
+ end
+
+ it "succeeds give the user is provisioned and tokens can be acquired" do
+ create(:oidc_user_token, user:, extra_audiences: storage.audience)
+ result = validator.call
+
+ expect(result.values).to all(be_success)
+ end
+
+ describe "error and warning handling" do
+ it "returns a warning if the current user isn't provisioned" do
+ user.update!(identity_url: nil)
+ result = validator.call
+
+ expect(result[:non_provisioned_user]).to be_warning
+ expect(result[:non_provisioned_user].message).to eq(I18n.t(i18n_key(:oidc_non_provisioned_user)))
+
+ state_count = result.values.map { it.state }.tally
+ expect(state_count).to eq({ skipped: 3, warning: 1 })
+ end
+
+ it "returns a warning if the user is not provisioned by an oidc provider" do
+ user.update!(identity_url: "ldap-provider:this-will-trigger-a-warning")
+ result = validator.call
+
+ expect(result[:provisioned_user_provider]).to be_warning
+ expect(result[:provisioned_user_provider].message).to eq(I18n.t(i18n_key(:oidc_non_oidc_user)))
+
+ state_count = result.values.map { it.state }.tally
+ expect(state_count).to eq({ success: 1, skipped: 2, warning: 1 })
+ end
+ end
+
+ describe "checks related to the token" do
+ 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:)
+ result = validator.call
+
+ expect(result[:token_negotiable]).to be_failure
+ expect(result[:token_negotiable].message).to eq(I18n.t(i18n_key(:oidc_cant_acquire_token)))
+ end
+ end
+
+ context "when the existing token requires a 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" })
+
+ result = validator.call
+
+ expect(result.values).to all(be_success)
+ expect(refresh_request).to have_been_requested.once
+ end
+
+ it "fails 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" })
+
+ result = validator.call
+
+ expect(result[:token_negotiable]).to be_failure
+ expect(result[:token_negotiable].message).to eq(I18n.t(i18n_key(:oidc_cant_refresh_token)))
+ end
+
+ it "fails when 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)
+
+ result = validator.call
+
+ expect(result[:token_negotiable]).to be_failure
+ expect(result[:token_negotiable].message).to eq(I18n.t(i18n_key(: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 "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" })
+
+ result = validator.call
+ expect(result.values).to all(be_success)
+ expect(exchange_request).to have_been_requested.once
+ end
+
+ it "fails if the exchange is met 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 " })
+
+ result = validator.call
+
+ expect(result[:token_negotiable]).to be_failure
+ expect(result[:token_negotiable].message).to eq(I18n.t(i18n_key(:oidc_cant_exchange_token)))
+ expect(exchange_request).to have_been_requested.once
+ end
+
+ it "fails 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)
+
+ result = validator.call
+
+ expect(result[:token_negotiable]).to be_failure
+ expect(result[:token_negotiable].message).to eq(I18n.t(i18n_key(:oidc_cant_exchange_token)))
+ expect(exchange_request).to have_been_requested.once
+ end
+ end
+ end
+ end
+ end
+
+ private
+
+ def i18n_key(key) = "storages.health.connection_validation.#{key}"
+ end
+ end
+ end
+ end
+end
diff --git a/modules/storages/spec/common/storages/peripherals/connection_validators/nextcloud/base_configuration_validator_spec.rb b/modules/storages/spec/common/storages/peripherals/connection_validators/nextcloud/base_configuration_validator_spec.rb
index 3fbe341dd3d..a417e9a74b3 100644
--- a/modules/storages/spec/common/storages/peripherals/connection_validators/nextcloud/base_configuration_validator_spec.rb
+++ b/modules/storages/spec/common/storages/peripherals/connection_validators/nextcloud/base_configuration_validator_spec.rb
@@ -53,8 +53,8 @@ module Storages
.to_return(status: 404, body: "Not Found")
results = validator.call
- expect(results[:host_url_not_found]).to be_a_failure
- expect(results[:host_url_not_found].message).to eq(I18n.t(i18n_key(:host_not_found)))
+ expect(results[:host_url_accessible]).to be_a_failure
+ expect(results[:host_url_accessible].message).to eq(I18n.t(i18n_key(:host_not_found)))
end
it "integration app version mismatch", vcr: "nextcloud/capabilities_success" do
@@ -62,15 +62,15 @@ module Storages
allow(subject).to receive(:nextcloud_dependencies).and_return(absurd_version)
results = validator.call
- expect(results[:version_mismatch]).to be_a_failure
- expect(results[:version_mismatch].message).to eq(I18n.t(i18n_key(:app_version_mismatch)))
+ expect(results[:dependencies_versions]).to be_a_failure
+ expect(results[:dependencies_versions].message).to eq(I18n.t(i18n_key(:app_version_mismatch)))
end
it "integration app disabled / missing", vcr: "nextcloud/capabilities_success_app_disabled" do
results = validator.call
- expect(results[:missing_dependencies]).to be_a_failure
- expect(results[:missing_dependencies].message)
+ expect(results[:dependencies_check]).to be_a_failure
+ expect(results[:dependencies_check].message)
.to eq(I18n.t(i18n_key(:missing_dependencies), dependency: "Integration OpenProject"))
end
end