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

[#53622] fixed error handling for user token authentication

Eric Schubert7a128a2cf44d
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/o_auth_user_token.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/o_auth_user_token.rb
index 22bdc4994b7..5f5e2943c5e 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/o_auth_user_token.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/o_auth_user_token.rb
@@ -52,7 +52,7 @@ module Storages
data:)
end
- opts = http_options.merge({ headers: { "Authorization" => "Bearer #{current_token.access_token}" } })
+ opts = http_options.deep_merge({ headers: { "Authorization" => "Bearer #{current_token.access_token}" } })
response_with_current_token = yield OpenProject.httpx.with(opts)
if response_with_current_token.success? || response_with_current_token.result != :unauthorized
@@ -82,10 +82,9 @@ module Storages
.with_access_token
.with(http_options)
rescue HTTPX::HTTPError => e
- data = ::Storages::StorageErrorData.new(source: self.class, payload: e.response.json)
return Failures::Builder.call(code: :unauthorized,
log_message: "Error while refreshing OAuth token.",
- data:)
+ data: error_data_from_response(e.response))
end
response = yield http_session
@@ -105,6 +104,20 @@ module Storages
# rubocop:enable Metrics/AbcSize
+ def error_data_from_response(response)
+ payload =
+ case response
+ in { content_type: { mime_type: "application/json" } }
+ response.json
+ in { content_type: { mime_type: "text/xml" } }
+ response.xml
+ else
+ response.body.to_s
+ end
+
+ ::Storages::StorageErrorData.new(source: self.class, payload:)
+ end
+
def update_refreshed_token(token, http_session)
oauth = http_session.instance_variable_get(:@options).oauth_session
access_token = oauth.access_token
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/file_info_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/file_info_query.rb
index 2c65be434ee..5a0d3c3cc3f 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/file_info_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/file_info_query.rb
@@ -44,7 +44,8 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
end
def call(auth_strategy:, file_id:)
- Auth[auth_strategy].call(storage: @storage, http_options: Util.ocs_api_request) do |http|
+ http_options = Util.ocs_api_request.deep_merge(Util.accept_json)
+ Auth[auth_strategy].call(storage: @storage, http_options:) do |http|
file_info(http, file_id).map(&parse_json) >> handle_failure >> create_storage_file_info
end
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_info_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_info_query.rb
index b5bba8ab6f4..51866801a84 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_info_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_info_query.rb
@@ -52,7 +52,8 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
return ServiceResult.success(result: [])
end
- Auth[auth_strategy].call(storage: @storage, http_options: Util.ocs_api_request) do |http|
+ http_options = Util.ocs_api_request.deep_merge(Util.accept_json)
+ Auth[auth_strategy].call(storage: @storage, http_options:) do |http|
files_info(http, file_ids).map(&parse_json) >> handle_failure >> create_storage_file_infos
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 e707eee5c6b..f8e091d393c 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
@@ -33,17 +33,21 @@ module Storages::Peripherals::StorageInteraction::Nextcloud::Util
class << self
def escape_path(path)
- escaped_path = path.split('/').map { |i| CGI.escapeURIComponent(i) }.join('/')
- escaped_path << '/' if path[-1] == '/'
+ escaped_path = path.split("/").map { |i| CGI.escapeURIComponent(i) }.join("/")
+ escaped_path << "/" if path[-1] == "/"
escaped_path
end
def ocs_api_request
- { headers: { 'OCS-APIRequest' => 'true' } }
+ { headers: { "OCS-APIRequest" => "true" } }
+ end
+
+ def accept_json
+ { headers: { "Accept" => "application/json" } }
end
def webdav_request_with_depth(number)
- { headers: { 'Depth' => number } }
+ { headers: { "Depth" => number } }
end
def error(code, log_message = nil, data = nil)
@@ -69,7 +73,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud::Util
end,
on_failure: ->(_) do
error(:unauthorized,
- 'Query could not be created! No access token found!',
+ "Query could not be created! No access token found!",
Storages::StorageErrorData.new(source: connection_manager))
end
)
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/files_info_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/files_info_query.rb
index f32ddc46187..59e95c45720 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/files_info_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/files_info_query.rb
@@ -54,8 +54,8 @@ module Storages
result = file_ids.map do |file_id|
file_info_result = FileInfoQuery.call(storage: @storage, auth_strategy:, file_id:)
if file_info_result.failure? &&
- file_info_result.error_source.is_a?(::OAuthClients::ConnectionManager)
- # errors in the connection manager must short circuit the query and return the error
+ file_info_result.error_source.module_parent == AuthenticationStrategies
+ # errors in the authentication strategies must short circuit the query and return the error
return file_info_result
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/internal/drive_item_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/internal/drive_item_query.rb
index 9ddf3b51624..7179d09597b 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/internal/drive_item_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/internal/drive_item_query.rb
@@ -63,13 +63,13 @@ module Storages
ServiceResult.success(result: response.json(symbolize_keys: true))
in { status: 404 }
ServiceResult.failure(result: :not_found,
- errors: Util.storage_error(response:, code: :not_found, source: self))
+ errors: Util.storage_error(response:, code: :not_found, source: self.class))
in { status: 403 }
ServiceResult.failure(result: :forbidden,
- errors: Util.storage_error(response:, code: :forbidden, source: self))
+ errors: Util.storage_error(response:, code: :forbidden, source: self.class))
in { status: 401 }
ServiceResult.failure(result: :unauthorized,
- errors: Util.storage_error(response:, code: :unauthorized, source: self))
+ errors: Util.storage_error(response:, code: :unauthorized, source: self.class))
else
data = ::Storages::StorageErrorData.new(source: self.class, payload: response)
ServiceResult.failure(result: :error, errors: ::Storages::StorageError.new(code: :error, data:))