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
[#43548] Refreshing OAuthClientToken transparently fails
Wieland Lindenthal26080cfdb647
diff --git a/app/services/oauth_clients/connection_manager.rb b/app/services/oauth_clients/connection_manager.rbindex f17cf87d18e..9c51c00835e 100644--- a/app/services/oauth_clients/connection_manager.rb+++ b/app/services/oauth_clients/connection_manager.rb@@ -147,7 +147,7 @@ module OAuthClientsend# @returns ServiceResult with result to be :error or any type of object with data- def request_with_token_refresh+ def request_with_token_refresh(oauth_client_token)# `yield` needs to returns a ServiceResult:# success: result= any object with data# failure: result= :error or :not_authorized@@ -161,6 +161,7 @@ module OAuthClientsreturn failed_service_resultend+ oauth_client_token.reloadyield_service_result = yield # Should contain result=<data> in case of successenddiff --git a/modules/storages/app/services/storages/file_link_sync_service.rb b/modules/storages/app/services/storages/file_link_sync_service.rbindex c4dab5c9e1a..c37c416f76d 100644--- a/modules/storages/app/services/storages/file_link_sync_service.rb+++ b/modules/storages/app/services/storages/file_link_sync_service.rb@@ -78,7 +78,7 @@ class Storages::FileLinkSyncServicereturnend- nextcloud_request_result = connection_manager.request_with_token_refresh do+ nextcloud_request_result = connection_manager.request_with_token_refresh(oauth_client_token) doresponse = request_files_info(oauth_client_token, file_links.map(&:origin_id))Rails.logger.debug { "Got nextcloud filesinfo response: #{response.inspect}" }# Parse HTTP response an return a ServiceResult with:diff --git a/spec/services/oauth_clients/connection_manager_spec.rb b/spec/services/oauth_clients/connection_manager_spec.rbindex 54b4b1d5f10..03ee0c67618 100644--- a/spec/services/oauth_clients/connection_manager_spec.rb+++ b/spec/services/oauth_clients/connection_manager_spec.rb@@ -458,41 +458,45 @@ describe ::OAuthClients::ConnectionManager, type: :model dolet(:refresh_service_result) { ServiceResult.success }subject do- instance.request_with_token_refresh { yield_service_result }+ instance.request_with_token_refresh(oauth_client_token) { yield_service_result }endbefore do- allow(instance).to receive(:refresh_token).and_return refresh_service_result+ allow(instance).to receive(:refresh_token).and_return(refresh_service_result)+ allow(oauth_client_token).to receive(:reload)end- context 'with yield=:success and refresh not called' do- it 'returns a ServiceResult with success, without refresh' do+ context 'with yield returning :success' do+ it 'returns a ServiceResult with success, without refreshing the token' doexpect(subject.success).to be_truthyexpect(instance).not_to have_received(:refresh_token)+ expect(oauth_client_token).not_to have_received(:reload)endend- context 'with yield=:error and refresh not called' do+ context 'with yield returning :error' dolet(:yield_service_result) { ServiceResult.failure(result: :error) }- it 'returns a ServiceResult with success, without refresh' do+ it 'returns a ServiceResult with success, without refreshing the token' doexpect(subject.success).to be_falseyexpect(subject.result).to be :errorexpect(instance).not_to have_received(:refresh_token)+ expect(oauth_client_token).not_to have_received(:reload)endend- context 'with yield=:not_authorized and refresh=:success' do+ context 'with yield returning :not_authorized and the refresh returning a with a success' dolet(:yield_service_result) { ServiceResult.failure(result: :not_authorized) }it 'returns a ServiceResult with success, without refresh' doexpect(subject.success).to be_falseyexpect(subject.result).to be :not_authorizedexpect(instance).to have_received(:refresh_token)+ expect(oauth_client_token).to have_received(:reload)endend- context 'with yield=:not_authorized and refresh=:failure' do+ context 'with yield returning :not_authorized and the refresh returning with a :failure' dolet(:yield_service_result) { ServiceResult.failure(result: :not_authorized) }let(:refresh_service_result) { ServiceResult.failure }@@ -500,17 +504,18 @@ describe ::OAuthClients::ConnectionManager, type: :model doexpect(subject.success).to be_falseyexpect(subject.result).to be :errorexpect(instance).to have_received(:refresh_token)+ expect(oauth_client_token).not_to have_received(:reload)endend- context 'with yield=:not_authorized first time and success the second time' do+ context 'with yield returning :not_authorized first time and :success the second time' dolet(:yield_double_object) { Object.new }let(:yield_service_result1) { ServiceResult.failure(result: :not_authorized) }let(:yield_service_result2) { ServiceResult.success }let(:refresh_service_result) { ServiceResult.success }subject do- instance.request_with_token_refresh { yield_double_object.yield_twice_method }+ instance.request_with_token_refresh(oauth_client_token) { yield_double_object.yield_twice_method }endbefore do@@ -527,6 +532,7 @@ describe ::OAuthClients::ConnectionManager, type: :model doexpect(subject.success).to be_truthyexpect(subject).to be yield_service_result2expect(instance).to have_received(:refresh_token)+ expect(oauth_client_token).to have_received(:reload)expect(yield_double_object).to have_received(:yield_twice_method).twiceendend