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
[#43473] Support valid Nextcloud installations using index.php paths
Wieland Lindenthal4242c6b5287d
diff --git a/app/services/oauth_clients/connection_manager.rb b/app/services/oauth_clients/connection_manager.rbindex f17cf87d18e..c62de6bc40d 100644--- a/app/services/oauth_clients/connection_manager.rb+++ b/app/services/oauth_clients/connection_manager.rb@@ -231,8 +231,8 @@ module OAuthClientsscheme: oauth_client_scheme,host: oauth_client_host,port: oauth_client_port,- authorization_endpoint: File.join(oauth_client_path, "/apps/oauth2/authorize"),- token_endpoint: File.join(oauth_client_path, "/apps/oauth2/api/v1/token")+ authorization_endpoint: File.join(oauth_client_path, "/index.php/apps/oauth2/authorize"),+ token_endpoint: File.join(oauth_client_path, "/index.php/apps/oauth2/api/v1/token"))enddiff --git a/modules/storages/app/services/storages/oauth_applications/create_service.rb b/modules/storages/app/services/storages/oauth_applications/create_service.rbindex 9bbc6afe5f4..f12d85732a6 100644--- a/modules/storages/app/services/storages/oauth_applications/create_service.rb+++ b/modules/storages/app/services/storages/oauth_applications/create_service.rb@@ -47,7 +47,7 @@ module Storages::OAuthApplications.new(::Doorkeeper::Application.new, user:).call({name: "#{storage.name} (#{I18n.t("storages.provider_types.#{storage.provider_type}.name")})",- redirect_uri: File.join(storage.host, "apps/integration_openproject/oauth-redirect"),+ redirect_uri: File.join(storage.host, "index.php/apps/integration_openproject/oauth-redirect"),scopes: 'api_v3',confidential: true,owner: storage.creator,diff --git a/modules/storages/app/services/storages/storages/update_service.rb b/modules/storages/app/services/storages/storages/update_service.rbindex 31311b44c96..c3358b5cb18 100644--- a/modules/storages/app/services/storages/storages/update_service.rb+++ b/modules/storages/app/services/storages/storages/update_service.rb@@ -41,7 +41,7 @@ module Storages::Storages.new(application, user:).call({name: "#{storage.name} (#{I18n.t("storages.provider_types.#{storage.provider_type}.name")})",- redirect_uri: File.join(storage.host, "apps/integration_openproject/oauth-redirect")+ redirect_uri: File.join(storage.host, "index.php/apps/integration_openproject/oauth-redirect")})service_call.add_dependent!(persist_service_result)enddiff --git a/modules/storages/app/validator/nextcloud_compatible_host_validator.rb b/modules/storages/app/validator/nextcloud_compatible_host_validator.rbindex aaff9c87869..c2ed3920772 100644--- a/modules/storages/app/validator/nextcloud_compatible_host_validator.rb+++ b/modules/storages/app/validator/nextcloud_compatible_host_validator.rb@@ -27,6 +27,7 @@#++class NextcloudCompatibleHostValidator < ActiveModel::EachValidatorMINIMAL_NEXTCLOUD_VERSION = 22+ AUTHORIZATION_HEADER = "Bearer TESTBEARERTOKEN".freezedef validate_each(contract, attribute, value)return unless contract.model.changed_attributes.include?(attribute)@@ -53,8 +54,8 @@ class NextcloudCompatibleHostValidator < ActiveModel::EachValidator# Apache strips that part of the request header by default.# https://docs.nextcloud.com/server/latest/admin_manual/configuration_server/oauth2.htmldef validate_setup_completeness(contract, attribute, value)- response = request_host(value)- error_type = check_host_response(response)+ response = request_config_check(value)+ error_type = check_config_check_response(response)if error_typecontract.errors.add(attribute, error_type)@@ -65,12 +66,19 @@ class NextcloudCompatibleHostValidator < ActiveModel::EachValidatordef check_capabilities_response(response)return :cannot_be_connected_to if response.is_a? StandardErrorreturn :cannot_be_connected_to unless response.is_a? Net::HTTPSuccess- return :not_nextcloud_server unless json_response?(response)+ return :not_nextcloud_server unless json_response_with_version?(response)return :minimal_nextcloud_version_unmet unless major_version_sufficient?(response)++ nilend- def check_host_response(response)- :setup_incomplete if response.code == '302' && response.header['location']&.include?("/index.php")+ def check_config_check_response(response)+ return :cannot_be_connected_to if response.is_a? StandardError+ return :op_application_not_installed if response.is_a? Net::HTTPRedirection+ return :cannot_be_connected_to unless response.is_a? Net::HTTPSuccess+ return :authorization_header_missing if read_authorization_header(response) != AUTHORIZATION_HEADER++ nilenddef message(host, response_or_exception, error_type)@@ -133,14 +141,15 @@ class NextcloudCompatibleHostValidator < ActiveModel::EachValidatormake_request(request)end- def request_host(host)- uri = URI.parse(host)+ def request_config_check(host)+ uri = URI.parse(File.join(host, 'index.php/apps/integration_openproject/check-config'))request = Net::HTTP::Get.new(uri)+ request["Authorization"] = AUTHORIZATION_HEADERmake_request(request)end- def json_response?(response)+ def json_response_with_version?(response)(response['content-type'].split(';').first.strip.downcase == 'application/json' \&& read_version(response)@@ -152,4 +161,10 @@ class NextcloudCompatibleHostValidator < ActiveModel::EachValidatordef read_version(response)JSON.parse(response.body).dig('ocs', 'data', 'version', 'major')end++ def read_authorization_header(response)+ JSON.parse(response.body)['authorization_header']+ rescue JSON::ParserError+ nil+ endenddiff --git a/modules/storages/config/locales/en.yml b/modules/storages/config/locales/en.ymlindex 2adbb48267b..9635dda72f3 100644--- a/modules/storages/config/locales/en.yml+++ b/modules/storages/config/locales/en.yml@@ -26,10 +26,13 @@ en:cannot_be_connected_to: "can not be connected to."minimal_nextcloud_version_unmet: "does not meet minimal version requirements (must be Nextcloud 23 or higher)"not_nextcloud_server: "is not a Nextcloud server"- setup_incomplete: >- is not fully set up. That Nextcloud instance still redirects to a URL containing 'index.php'. That is a- strong indicator that the server does not have mod_rewrite, mod_headers and/or mod_env fully configured,- which is necessary for a Bearer token based authorization of API requests.+ op_application_not_installed: >+ appears to not have the app "OpenProject integration" installed. Please install it first and then try+ again.+ authorization_header_missing: >+ is not fully set up. The Nextcloud instance does not receive the "Authorization" header, which is+ necessary for a Bearer token based authorization of API requests. Please double check your HTTP+ server configuration.storages/file_link:attributes:origin_id:diff --git a/modules/storages/lib/api/v3/file_links/storage_url_helper.rb b/modules/storages/lib/api/v3/file_links/storage_url_helper.rbindex e0fde803ef5..df2ceeaf8d6 100644--- a/modules/storages/lib/api/v3/file_links/storage_url_helper.rb+++ b/modules/storages/lib/api/v3/file_links/storage_url_helper.rb@@ -31,11 +31,11 @@ module API::V3::FileLinks::StorageUrlHelperdef storage_url_open_file(file_link, open_location: false)location_flag = ActiveModel::Type::Boolean.new.cast(open_location) ? 0 : 1- "#{file_link.storage.host}/f/#{file_link.origin_id}?openfile=#{location_flag}"+ "#{file_link.storage.host}/index.php/f/#{file_link.origin_id}?openfile=#{location_flag}"enddef storage_url_open(storage)- "#{storage.host}/apps/files"+ "#{storage.host}/index.php/apps/files"end# rubocop:disable Metrics/AbcSize@@ -54,7 +54,7 @@ module API::V3::FileLinks::StorageUrlHelperdownload_token = direct_download_token(body: direct_download_response.result)return download_token if download_token.failure?- url = "#{storage.host}/apps/integration_openproject/direct/#{download_token.result}/#{file_link.origin_name}"+ url = "#{storage.host}/index.php/apps/integration_openproject/direct/#{download_token.result}/#{file_link.origin_name}"ServiceResult.success(result: url)enddiff --git a/modules/storages/spec/contracts/storages/storages/base_contract_spec.rb b/modules/storages/spec/contracts/storages/storages/base_contract_spec.rbindex 7c165db41cd..4054df03efb 100644--- a/modules/storages/spec/contracts/storages/storages/base_contract_spec.rb+++ b/modules/storages/spec/contracts/storages/storages/base_contract_spec.rb@@ -36,7 +36,7 @@ describe Storages::Storages::BaseContract, :storage_server_helpers, webmock: truit 'checks the storage url only when changed' docapabilities_request = mock_server_capabilities_response(storage_host)- host_request = mock_server_host_response(storage_host)+ host_request = mock_server_config_check_response(storage_host)contract.valid?expect(capabilities_request).to have_been_made.onceexpect(host_request).to have_been_made.oncediff --git a/modules/storages/spec/contracts/storages/storages/shared_contract_examples.rb b/modules/storages/spec/contracts/storages/storages/shared_contract_examples.rbindex 39e560321fd..41ad3853645 100644--- a/modules/storages/spec/contracts/storages/storages/shared_contract_examples.rb+++ b/modules/storages/spec/contracts/storages/storages/shared_contract_examples.rb@@ -40,7 +40,7 @@ shared_examples_for 'storage contract', :storage_server_helpers, webmock: true dbefore doif storage_host.present?mock_server_capabilities_response(storage_host)- mock_server_host_response(storage_host)+ mock_server_config_check_response(storage_host)endend@@ -133,16 +133,25 @@ shared_examples_for 'storage contract', :storage_server_helpers, webmock: true dlet(:capabilities_response_code) { nil } # use defaultlet(:capabilities_response_headers) { nil } # use defaultlet(:capabilities_response_major_version) { 22 }+ let(:check_config_response_body) { nil } # use default+ let(:check_config_response_code) { nil } # use default+ let(:check_config_response_headers) { nil } # use defaultbefore do# simulate host value changed to have GET request sent to check host URL validitystorage.host_will_change!+# simulate http response returned upon GET requestmock_server_capabilities_response(storage_host,response_code: capabilities_response_code,response_headers: capabilities_response_headers,response_body: capabilities_response_body,response_nextcloud_major_version: capabilities_response_major_version)++ mock_server_config_check_response(storage_host,+ response_code: check_config_response_code,+ response_headers: check_config_response_headers,+ response_body: check_config_response_body)endcontext 'when connection fails' do@@ -193,17 +202,16 @@ shared_examples_for 'storage contract', :storage_server_helpers, webmock: true dinclude_examples 'contract is invalid', host: :minimal_nextcloud_version_unmetend- context 'when Nextcloud instance is not fully set up' do- let(:host_response_code) { 302 }- let(:host_response_header) { { Location: File.join(storage_host, '/index.php/login') } }+ context 'when Nextcloud instance is missing the "OpenProject integration" app' do+ let(:check_config_response_code) { 302 }- before do- mock_server_host_response(storage_host,- response_code: host_response_code,- response_headers: host_response_header)- end+ include_examples 'contract is invalid', host: :op_application_not_installed+ end++ context 'when Nextcloud instance is misconfigured and strips AUTHORIZATION header from HTTP request' do+ let(:check_config_response_body) { { authorization_header: '' }.to_json }- include_examples 'contract is invalid', host: :setup_incomplete+ include_examples 'contract is invalid', host: :authorization_header_missingendendenddiff --git a/modules/storages/spec/controller/storages_controller_spec.rb b/modules/storages/spec/controller/storages_controller_spec.rbindex 0b112aa5b5c..5d5ebea700d 100644--- a/modules/storages/spec/controller/storages_controller_spec.rb+++ b/modules/storages/spec/controller/storages_controller_spec.rb@@ -43,7 +43,7 @@ describe ::Storages::Admin::StoragesController, webmock: true, type: :controllerbefore dologin_as adminmock_server_capabilities_response(host)- mock_server_host_response(host)+ mock_server_config_check_response(host)post :create, params:enddiff --git a/modules/storages/spec/features/admin_storages_spec.rb b/modules/storages/spec/features/admin_storages_spec.rbindex cb16af25ec4..c2169d893b0 100644--- a/modules/storages/spec/features/admin_storages_spec.rb+++ b/modules/storages/spec/features/admin_storages_spec.rb@@ -53,7 +53,7 @@ describe 'Admin storages', :storage_server_helpers, type: :feature, js: true do# Test the happy path for a valid storage server (host).# Mock a valid response (=200) for example.com, so the host validation should succeedmock_server_capabilities_response("https://example.com")- mock_server_host_response("https://example.com")+ mock_server_config_check_response("https://example.com")page.find('#storages_storage_name').set("NC 1")page.find('#storages_storage_host').set("https://example.com")page.find('button[type=submit]', text: "Save and continue setup").clickdiff --git a/modules/storages/spec/lib/api/v3/file_links/file_link_representer_rendering_spec.rb b/modules/storages/spec/lib/api/v3/file_links/file_link_representer_rendering_spec.rbindex 31315f1e75f..f5946551972 100644--- a/modules/storages/spec/lib/api/v3/file_links/file_link_representer_rendering_spec.rb+++ b/modules/storages/spec/lib/api/v3/file_links/file_link_representer_rendering_spec.rb@@ -125,7 +125,7 @@ describe ::API::V3::FileLinks::FileLinkRepresenter, 'rendering' dodescribe 'originOpen' doit_behaves_like 'has an untitled link' dolet(:link) { 'originOpen' }- let(:href) { "#{storage.host}/f/#{file_link.origin_id}?openfile=1" }+ let(:href) { "#{storage.host}/index.php/f/#{file_link.origin_id}?openfile=1" }endend@@ -139,7 +139,7 @@ describe ::API::V3::FileLinks::FileLinkRepresenter, 'rendering' dodescribe 'originOpenLocation' doit_behaves_like 'has an untitled link' dolet(:link) { 'originOpenLocation' }- let(:href) { "#{storage.host}/f/#{file_link.origin_id}?openfile=0" }+ let(:href) { "#{storage.host}/index.php/f/#{file_link.origin_id}?openfile=0" }endenddiff --git a/modules/storages/spec/support/storage_server_helpers.rb b/modules/storages/spec/support/storage_server_helpers.rbindex 3cbdfa27e56..ff932783176 100644--- a/modules/storages/spec/support/storage_server_helpers.rb+++ b/modules/storages/spec/support/storage_server_helpers.rb@@ -64,18 +64,30 @@ module StorageServerHelpers)end- def mock_server_host_response(nextcloud_host,- response_code: nil,- response_headers: nil)+ def mock_server_config_check_response(nextcloud_host,+ response_code: nil,+ response_headers: nil,+ response_body: nil)response_code ||= 200- response_headers ||= {}+ response_headers ||= {+ 'Content-Type' => 'application/json; charset=utf-8'+ }++ response_body ||=+ %{+ {+ "user_id": "",+ "authorization_header": "Bearer TESTBEARERTOKEN"+ }+ }stub_request(:get,- nextcloud_host+ File.join(nextcloud_host, 'index.php/apps/integration_openproject/check-config')).to_return(status: response_code,- headers: response_headers+ headers: response_headers,+ body: response_body)endenddiff --git a/spec/services/oauth_clients/connection_manager_spec.rb b/spec/services/oauth_clients/connection_manager_spec.rbindex 54b4b1d5f10..8b9cce6a063 100644--- a/spec/services/oauth_clients/connection_manager_spec.rb+++ b/spec/services/oauth_clients/connection_manager_spec.rb@@ -157,7 +157,7 @@ describe ::OAuthClients::ConnectionManager, type: :model dorefresh_token: "UwFp...1FROJ",user_id: "admin"}.to_json- stub_request(:any, File.join(host, '/apps/oauth2/api/v1/token'))+ stub_request(:any, File.join(host, '/index.php/apps/oauth2/api/v1/token')).to_return(status: 200, body: response_body)end@@ -169,7 +169,7 @@ describe ::OAuthClients::ConnectionManager, type: :model docontext 'with known error', webmock: true dobefore do- stub_request(:post, File.join(host, '/apps/oauth2/api/v1/token'))+ stub_request(:post, File.join(host, '/index.php/apps/oauth2/api/v1/token')).to_return(status: 400, body: { error: error_message }.to_json)end@@ -197,7 +197,7 @@ describe ::OAuthClients::ConnectionManager, type: :model docontext 'with known reply invalid_grant', webmock: true dobefore do- stub_request(:post, File.join(host, '/apps/oauth2/api/v1/token'))+ stub_request(:post, File.join(host, '/index.php/apps/oauth2/api/v1/token')).to_return(status: 400, body: { error: "invalid_grant" }.to_json)end@@ -211,7 +211,7 @@ describe ::OAuthClients::ConnectionManager, type: :model docontext 'with unknown reply', webmock: true dobefore do- stub_request(:post, File.join(host, '/apps/oauth2/api/v1/token'))+ stub_request(:post, File.join(host, '/index.php/apps/oauth2/api/v1/token')).to_return(status: 400, body: { error: "invalid_requesttt" }.to_json)end@@ -225,7 +225,7 @@ describe ::OAuthClients::ConnectionManager, type: :model docontext 'with reply including JSON syntax error', webmock: true dobefore do- stub_request(:post, File.join(host, '/apps/oauth2/api/v1/token'))+ stub_request(:post, File.join(host, '/index.php/apps/oauth2/api/v1/token')).to_return(status: 400,headers: { 'Content-Type' => 'application/json; charset=utf-8' },@@ -243,7 +243,7 @@ describe ::OAuthClients::ConnectionManager, type: :model docontext 'with 500 reply without body', webmock: true dobefore do- stub_request(:post, File.join(host, '/apps/oauth2/api/v1/token'))+ stub_request(:post, File.join(host, '/index.php/apps/oauth2/api/v1/token')).to_return(status: 500)end@@ -257,7 +257,7 @@ describe ::OAuthClients::ConnectionManager, type: :model docontext 'with bad HTTP response', webmock: true dobefore do- stub_request(:post, File.join(host, '/apps/oauth2/api/v1/token')).to_raise(Net::HTTPBadResponse)+ stub_request(:post, File.join(host, '/index.php/apps/oauth2/api/v1/token')).to_raise(Net::HTTPBadResponse)endit 'returns an unspecific error message' do@@ -270,7 +270,7 @@ describe ::OAuthClients::ConnectionManager, type: :model docontext 'with timeout returns internal error', webmock: true dobefore do- stub_request(:post, File.join(host, '/apps/oauth2/api/v1/token')).to_timeout+ stub_request(:post, File.join(host, '/index.php/apps/oauth2/api/v1/token')).to_timeoutendit 'returns an unspecific error message' do@@ -303,7 +303,7 @@ describe ::OAuthClients::ConnectionManager, type: :model dorefresh_token: "xUwFp...1FROJ",user_id: "admin"}.to_json- stub_request(:any, File.join(host, '/apps/oauth2/api/v1/token'))+ stub_request(:any, File.join(host, '/index.php/apps/oauth2/api/v1/token')).to_return(status: 200, body: response_body)oauth_client_tokenend@@ -327,7 +327,7 @@ describe ::OAuthClients::ConnectionManager, type: :model dorefresh_token: "xUwFp...1FROJ",user_id: "admin"}.to_json- stub_request(:any, File.join(host, '/apps/oauth2/api/v1/token'))+ stub_request(:any, File.join(host, '/index.php/apps/oauth2/api/v1/token')).to_return(status: 200, body: response_body)oauth_client_token@@ -343,7 +343,7 @@ describe ::OAuthClients::ConnectionManager, type: :model docontext 'with server error from OAuth2 provider' dobefore do- stub_request(:any, File.join(host, '/apps/oauth2/api/v1/token'))+ stub_request(:any, File.join(host, '/index.php/apps/oauth2/api/v1/token')).to_return(status: 400, body: { error: "invalid_request" }.to_json)oauth_client_tokenend@@ -358,7 +358,7 @@ describe ::OAuthClients::ConnectionManager, type: :model docontext 'with successful response but invalid data' dobefore do# Simulate timeout- stub_request(:any, File.join(host, '/apps/oauth2/api/v1/token'))+ stub_request(:any, File.join(host, '/index.php/apps/oauth2/api/v1/token')).to_timeoutoauth_client_tokenend