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

Incorporates feedback from @Kharonus

Marcello Rochaf7a40e64672d
diff --git a/app/services/oauth_clients/connection_manager.rb b/app/services/oauth_clients/connection_manager.rb
index 2ed9b21d9d6..a523134e421 100644
--- a/app/services/oauth_clients/connection_manager.rb
+++ b/app/services/oauth_clients/connection_manager.rb
@@ -48,14 +48,14 @@ module OAuthClients
# @param state (OAuth2 RFC) encapsulates the state of the calling page (URL + params) to return
# @param scope (OAuth2 RFC) specifies the resources to access. Nextcloud only has one global scope.
# @return ServiceResult with ServiceResult.result being either an OAuthClientToken or a redirection URL
- def get_access_token(scope: [], state: nil)
+ def get_access_token(state: nil)
# Check for an already existing token from last call
token = get_existing_token
return ServiceResult.success(result: token) if token.present?
# Return the Nextcloud OAuth authorization URI that a user needs to open to grant access and eventually obtain
# a token.
- @redirect_url = get_authorization_uri(scope:, state:)
+ @redirect_url = get_authorization_uri(state:)
ServiceResult.failure(result: @redirect_url)
end
@@ -88,10 +88,9 @@ module OAuthClients
# @param state (OAuth2 RFC) is a nonce referencing a cookie containing the calling page (URL + params) to which to
# return to at the end of the whole flow.
# @param scope (OAuth2 RFC) specifies the resources to access. Nextcloud has only one global scope.
- def get_authorization_uri(scope: [], state: nil)
+ def get_authorization_uri(state: nil)
client = rack_oauth_client # Configure and start the rack-oauth2 client
- joined_scopes = @config.compute_scopes(scope)
- client.authorization_uri(scope: joined_scopes, state:)
+ client.authorization_uri(scope: @config.scope, state:)
end
# Called by callback_page with a cryptographic "code" that indicates
diff --git a/modules/storages/app/common/storages/peripherals/oauth_configurations/configuration_interface.rb b/modules/storages/app/common/storages/peripherals/oauth_configurations/configuration_interface.rb
index 0ddaf34b75c..be177b34aff 100644
--- a/modules/storages/app/common/storages/peripherals/oauth_configurations/configuration_interface.rb
+++ b/modules/storages/app/common/storages/peripherals/oauth_configurations/configuration_interface.rb
@@ -34,7 +34,7 @@ module Storages
class ConfigurationInterface
def authorization_state_check(_) = raise ::Storages::Errors::SubclassResponsibility
- def compute_scopes(_) = raise ::Storages::Errors::SubclassResponsibility
+ def scope = raise ::Storages::Errors::SubclassResponsibility
def basic_rack_oauth_client = raise ::Storages::Errors::SubclassResponsibility
diff --git a/modules/storages/app/common/storages/peripherals/oauth_configurations/nextcloud_configuration.rb b/modules/storages/app/common/storages/peripherals/oauth_configurations/nextcloud_configuration.rb
index af255a9743a..7e44f3888a7 100644
--- a/modules/storages/app/common/storages/peripherals/oauth_configurations/nextcloud_configuration.rb
+++ b/modules/storages/app/common/storages/peripherals/oauth_configurations/nextcloud_configuration.rb
@@ -56,8 +56,8 @@ module Storages
end
end
- def compute_scopes(scopes)
- scopes
+ def scope
+ []
end
def basic_rack_oauth_client
diff --git a/modules/storages/app/common/storages/peripherals/oauth_configurations/one_drive_configuration.rb b/modules/storages/app/common/storages/peripherals/oauth_configurations/one_drive_configuration.rb
index c794ab764f2..e812f896e39 100644
--- a/modules/storages/app/common/storages/peripherals/oauth_configurations/one_drive_configuration.rb
+++ b/modules/storages/app/common/storages/peripherals/oauth_configurations/one_drive_configuration.rb
@@ -51,8 +51,8 @@ module Storages
end
end
- def compute_scopes(scopes)
- Array(scopes) | DEFAULT_SCOPES
+ def scope
+ DEFAULT_SCOPES
end
def basic_rack_oauth_client
diff --git a/spec/services/oauth_clients/connection_manager_spec.rb b/spec/services/oauth_clients/connection_manager_spec.rb
index cc7014961da..b0ed6dcfdd8 100644
--- a/spec/services/oauth_clients/connection_manager_spec.rb
+++ b/spec/services/oauth_clients/connection_manager_spec.rb
@@ -49,7 +49,7 @@ RSpec.describe OAuthClients::ConnectionManager, type: :model, webmock: true do
let(:scope) { nil }
let(:state) { nil }
- subject { instance.get_authorization_uri(scope:, state:) }
+ subject { instance.get_authorization_uri(state:) }
context 'with empty state and scope' do
shared_examples_for 'returns the authorization URI relative to the host' do
@@ -87,6 +87,8 @@ RSpec.describe OAuthClients::ConnectionManager, type: :model, webmock: true do
let(:scope) { %i(email profile) }
it 'returns the redirect URL' do
+ allow(configuration).to receive(:scope).and_return(scope)
+
expect(subject).to be_a String
expect(subject).to include oauth_client.integration.host
expect(subject).not_to include "state"
@@ -110,9 +112,11 @@ RSpec.describe OAuthClients::ConnectionManager, type: :model, webmock: true do
end
context 'with no OAuthClientToken present and state parameters' do
- subject { instance.get_access_token(state: "some_state", scope: [:email]) }
+ subject { instance.get_access_token(state: "some_state") }
it 'returns the redirect URL' do
+ allow(configuration).to receive(:scope).and_return(%w[email])
+
expect(subject.success).to be_falsey
expect(subject.result).to be_a String
expect(subject.result).to include oauth_client.integration.host
diff --git a/spec/services/oauth_clients/one_drive_connection_manager_spec.rb b/spec/services/oauth_clients/one_drive_connection_manager_spec.rb
index 305d87c9df8..9a40f9f370b 100644
--- a/spec/services/oauth_clients/one_drive_connection_manager_spec.rb
+++ b/spec/services/oauth_clients/one_drive_connection_manager_spec.rb
@@ -41,13 +41,13 @@ RSpec.describe OAuthClients::ConnectionManager, type: :model, webmock: true do
describe '#get_authorization_uri' do
it 'always add the necessary scopes' do
- uri = connection_manager.get_authorization_uri(scope: nil, state: nil)
+ uri = connection_manager.get_authorization_uri(state: nil)
- expect(uri).to include storage.oauth_configuration.compute_scopes(nil).join('%20')
+ expect(uri).to include storage.oauth_configuration.scope.join('%20')
end
it 'adds the state if present' do
- uri = connection_manager.get_authorization_uri(scope: nil, state: 'https://some.site.com')
+ uri = connection_manager.get_authorization_uri(state: 'https://some.site.com')
expect(uri).to include "&state=https"
end
end