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

[#43548] Refreshing OAuthClientToken transparently fails

Wieland Lindenthal26080cfdb647
diff --git a/app/services/oauth_clients/connection_manager.rb b/app/services/oauth_clients/connection_manager.rb
index f17cf87d18e..9c51c00835e 100644
--- a/app/services/oauth_clients/connection_manager.rb
+++ b/app/services/oauth_clients/connection_manager.rb
@@ -147,7 +147,7 @@ module OAuthClients
end
# @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 OAuthClients
return failed_service_result
end
+ oauth_client_token.reload
yield_service_result = yield # Should contain result=<data> in case of success
end
diff --git a/modules/storages/app/services/storages/file_link_sync_service.rb b/modules/storages/app/services/storages/file_link_sync_service.rb
index 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::FileLinkSyncService
return
end
- nextcloud_request_result = connection_manager.request_with_token_refresh do
+ nextcloud_request_result = connection_manager.request_with_token_refresh(oauth_client_token) do
response = 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.rb
index 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 do
let(: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 }
end
before 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' do
expect(subject.success).to be_truthy
expect(instance).not_to have_received(:refresh_token)
+ expect(oauth_client_token).not_to have_received(:reload)
end
end
- context 'with yield=:error and refresh not called' do
+ context 'with yield returning :error' do
let(: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' do
expect(subject.success).to be_falsey
expect(subject.result).to be :error
expect(instance).not_to have_received(:refresh_token)
+ expect(oauth_client_token).not_to have_received(:reload)
end
end
- context 'with yield=:not_authorized and refresh=:success' do
+ context 'with yield returning :not_authorized and the refresh returning a with a success' do
let(:yield_service_result) { ServiceResult.failure(result: :not_authorized) }
it 'returns a ServiceResult with success, without refresh' do
expect(subject.success).to be_falsey
expect(subject.result).to be :not_authorized
expect(instance).to have_received(:refresh_token)
+ expect(oauth_client_token).to have_received(:reload)
end
end
- context 'with yield=:not_authorized and refresh=:failure' do
+ context 'with yield returning :not_authorized and the refresh returning with a :failure' do
let(:yield_service_result) { ServiceResult.failure(result: :not_authorized) }
let(:refresh_service_result) { ServiceResult.failure }
@@ -500,17 +504,18 @@ describe ::OAuthClients::ConnectionManager, type: :model do
expect(subject.success).to be_falsey
expect(subject.result).to be :error
expect(instance).to have_received(:refresh_token)
+ expect(oauth_client_token).not_to have_received(:reload)
end
end
- 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' do
let(: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 }
end
before do
@@ -527,6 +532,7 @@ describe ::OAuthClients::ConnectionManager, type: :model do
expect(subject.success).to be_truthy
expect(subject).to be yield_service_result2
expect(instance).to have_received(:refresh_token)
+ expect(oauth_client_token).to have_received(:reload)
expect(yield_double_object).to have_received(:yield_twice_method).twice
end
end