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

Base tests passing

Marcello Rochab7ebeb721d8a
diff --git a/app/services/oauth_clients/connection_manager.rb b/app/services/oauth_clients/connection_manager.rb
index 62c4837038e..51c8d412dbe 100644
--- a/app/services/oauth_clients/connection_manager.rb
+++ b/app/services/oauth_clients/connection_manager.rb
@@ -37,11 +37,9 @@ module OAuthClients
AUTHORIZATION_CHECK_PATH = '/ocs/v1.php/cloud/user'
TOKEN_IS_FRESH_DURATION = 10.seconds.freeze
- attr_reader :user, :oauth_client
-
- def initialize(user:, oauth_client: nil, configuration: nil)
+ def initialize(user:, configuration:, oauth_client: nil)
@user = user
- @oauth_client = configuration ? configuration.oauth_client : oauth_client
+ @oauth_client = oauth_client || configuration.oauth_client
@config = configuration
end
@@ -58,12 +56,9 @@ module OAuthClients
# Return the Nextcloud OAuth authorization URI that a user needs to open to grant access and eventually obtain
# a token.
- full_scope = if @config
- @config.compute_scopes(scope)
- else
- scope
- end
+ full_scope = @config.compute_scopes(scope)
@redirect_url = get_authorization_uri(scope: full_scope, state:)
+
ServiceResult.failure(result: @redirect_url)
end
@@ -146,42 +141,20 @@ module OAuthClients
oauth_client_token = get_existing_token
return :failed_authorization unless oauth_client_token
- # Check for user information. This is the cheapest Nextcloud call that requires
- # valid authentication, so we use it for testing the validity of the Bearer token.
- # curl -H "Authorization: Bearer MY_TOKEN" -X GET 'https://my.nextcloud.org/ocs/v1.php/cloud/user' \
- # -H "OCS-APIRequest: true" -H "Accept: application/json"
- util = Storages::Peripherals::StorageInteraction::Nextcloud::Util
- response = if @config
- @config.authorization_state_check(oauth_client_token.access_token)
- else
- uri = URI(oauth_client.integration.host).normalize
- util
- .http(uri)
- .get(util.join_uri_path(uri, AUTHORIZATION_CHECK_PATH),
- {
- 'Authorization' => "Bearer #{oauth_client_token.access_token}",
- 'OCS-APIRequest' => 'true',
- 'Accept' => 'application/json'
- })
- end
-
- case response
- when Net::HTTPSuccess
+ case @config.authorization_state_check(oauth_client_token.access_token)
+ when :success
:connected
- when Net::HTTPForbidden, Net::HTTPUnauthorized
- service_result = refresh_token # `refresh_token` already has exception handling
+ when :refresh_needed
+ service_result = refresh_token
if service_result.success?
:connected
elsif service_result.result == 'invalid_request'
- # This can happen if the Authorization Server invalidated all tokens.
- # Then the user would ideally be asked to reauthorize.
:failed_authorization
else
- # It could also be that some other error happened, i.e. firewall badly configured.
- # Then the user needs to know that something is technically off. The user could try
- # to reload the page or contact an admin.
:error
end
+ else
+ response
end
rescue StandardError
:error
@@ -226,10 +199,7 @@ module OAuthClients
ServiceResult.success(result: rack_access_token)
rescue Rack::OAuth2::Client::Error => e
service_result_with_error(i18n_rack_oauth2_error_message(e), e.message)
- rescue Faraday::TimeoutError,
- Faraday::ConnectionFailed,
- Faraday::ParsingError,
- Faraday::SSLError => e
+ rescue Faraday::TimeoutError, Faraday::ConnectionFailed, Faraday::ParsingError, Faraday::SSLError => e
service_result_with_error(
"#{I18n.t('oauth_client.errors.oauth_returned_http_error')}: #{e.class}: #{e.message.to_html}",
e.message
@@ -254,7 +224,7 @@ module OAuthClients
# Return a fully configured RackOAuth2Client.
# This client does all the heavy lifting with the OAuth2 protocol.
def rack_oauth_client(options = {})
- rack_oauth_client = @config ? @config.basic_rack_oauth_client : build_basic_rack_oauth_client
+ rack_oauth_client = @config.basic_rack_oauth_client
# Write options, for example authorization_code and refresh_token
rack_oauth_client.refresh_token = options[:refresh_token] if options[:refresh_token]
@@ -263,24 +233,6 @@ module OAuthClients
rack_oauth_client
end
- def build_basic_rack_oauth_client
- oauth_client_uri = URI.parse(@oauth_client.integration.host)
- oauth_client_scheme = oauth_client_uri.scheme
- oauth_client_host = oauth_client_uri.host
- oauth_client_port = oauth_client_uri.port
- oauth_client_path = oauth_client_uri.path
-
- Rack::OAuth2::Client.new(
- identifier: @oauth_client.client_id,
- secret: @oauth_client.client_secret,
- scheme: oauth_client_scheme,
- host: oauth_client_host,
- port: oauth_client_port,
- 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")
- )
- end
-
# Update an OpenProject token based on updated values from a
# Rack::OAuth2::AccessToken::Bearer after a OAuth2 refresh operation
def update_oauth_client_token(oauth_client_token, rack_oauth2_access_token)
diff --git a/app/services/oauth_clients/one_drive_connection_manager.rb b/app/services/oauth_clients/one_drive_connection_manager.rb
deleted file mode 100644
index 975761554fc..00000000000
--- a/app/services/oauth_clients/one_drive_connection_manager.rb
+++ /dev/null
@@ -1,89 +0,0 @@
-# frozen_string_literal: true
-
-#-- copyright
-# OpenProject is an open source project management software.
-# Copyright (C) 2012-2023 the OpenProject GmbH
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License version 3.
-#
-# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
-# Copyright (C) 2006-2013 Jean-Philippe Lang
-# Copyright (C) 2010-2013 the ChiliProject Team
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; either version 2
-# of the License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-#
-# See COPYRIGHT and LICENSE files for more details.
-#++
-
-module OAuthClients
- class OneDriveConnectionManager < ConnectionManager
- OAUTH_URI = URI('https://login.microsoftonline.com/')
- DEFAULT_SCOPES = %w[offline_access files.readwrite.all user.read sites.readwrite.all].freeze
-
- GRAPH_API_URI = URI('https://graph.microsoft.com')
-
- def initialize(user:, oauth_client:, tenant_id:)
- super(user:, oauth_client:)
- @tenant_id = tenant_id
- end
-
- def authorization_state
- current_token = get_existing_token
- return :failed_authorization unless current_token
-
- response = Net::HTTP.start(GRAPH_API_URI.host, GRAPH_API_URI.port, use_ssl: true) do |http|
- http.get('/v1.0/me', { 'Authorization' => "Bearer #{current_token.access_token}" })
- end
-
- case response
- when Net::HTTPSuccess
- :connected
- when Net::HTTPForbidden, Net::HTTPUnauthorized
- service_result = refresh_token
- if service_result.success?
- :connected
- elsif service_result.result == 'invalid_request'
- :failed_authorization
- else
- :error
- end
- else
- raise StandardError, 'not sure what to do'
- end
- rescue StandardError
- :error
- end
-
- def get_authorization_uri(scope: [], state: nil)
- joined_scope = Array(scope) | DEFAULT_SCOPES
- super(scope: joined_scope, state:)
- end
-
- private
-
- def build_basic_rack_oauth_client
- Rack::OAuth2::Client.new(
- identifier: @oauth_client.client_id,
- secret: @oauth_client.client_secret,
- scheme: OAUTH_URI.scheme,
- host: OAUTH_URI.host,
- port: OAUTH_URI.port,
- authorization_endpoint: File.join('/', @tenant_id, "/oauth2/v2.0/authorize"),
- token_endpoint: File.join('/', @tenant_id, "/oauth2/v2.0/token")
- )
- end
- end
-end
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 64b3d4b1c78..2463197effa 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
@@ -16,8 +16,7 @@ module Storages
def authorization_state_check(token)
util = StorageInteraction::Nextcloud::Util
- # response =
- Net::HTTP.start(@uri.host, @uri.port, use_ssl: true) do |http|
+ response = Net::HTTP.start(@uri.host, @uri.port, use_ssl: true) do |http|
http.get(
util.join_uri_path(@uri, '/ocs/v1.php/cloud/user'),
{
@@ -27,24 +26,15 @@ module Storages
}
)
end
- # case response
- # when Net::HTTPSuccess
- # :connected
- # when Net::HTTPForbidden, Net::HTTPUnauthorized
- # service_result = refresh_token # `refresh_token` already has exception handling
- # if service_result.success?
- # :connected
- # elsif service_result.result == 'invalid_request'
- # # This can happen if the Authorization Server invalidated all tokens.
- # # Then the user would ideally be asked to reauthorize.
- # :failed_authorization
- # else
- # # It could also be that some other error happened, i.e. firewall badly configured.
- # # Then the user needs to know that something is technically off. The user could try
- # # to reload the page or contact an admin.
- # :error
- # end
- # end
+
+ case response
+ when Net::HTTPSuccess
+ :success
+ when Net::HTTPForbidden, Net::HTTPUnauthorized
+ :refresh_needed
+ else
+ :error
+ end
end
def compute_scopes(scopes)
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 3f21031ba8f..63e9ced15a6 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
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
#-- copyright
#++
@@ -17,28 +19,18 @@ module Storages
end
def authorization_state_check(access_token)
- # response =
- Net::HTTP.start(@uri.host, @uri.port, use_ssl: true) do |http|
- http.get('/v1.0/me', { 'Authorization' => "Bearer #{access_token}" })
+ response = Net::HTTP.start(@uri.host, @uri.port, use_ssl: true) do |http|
+ http.get('/v1.0/me', { 'Authorization' => "Bearer #{access_token}", 'Accept' => 'application/json' })
+ end
+
+ case response
+ when Net::HTTPSuccess
+ :success
+ when Net::HTTPForbidden, Net::HTTPUnauthorized
+ :refresh_needed
+ else
+ :error
end
- #
- # case response
- # when Net::HTTPSuccess
- # :connected
- # when Net::HTTPForbidden, Net::HTTPUnauthorized
- # service_result = refresh_token
- # if service_result.success?
- # :connected
- # elsif service_result.result == 'invalid_request'
- # :failed_authorization
- # else
- # :error
- # end
- # else
- # raise StandardError, 'not sure what to do'
- # end
- # rescue StandardError
- # :error
end
def compute_scopes(scopes)
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/files_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/files_query.rb
index c92def88938..c19934884fa 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/files_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/files_query.rb
@@ -101,8 +101,8 @@ module Storages
end
def using_user_token(user, &block)
- connection_manager = ::OAuthClients::OneDriveConnectionManager
- .new(user:, oauth_client: @storage.oauth_client, tenant_id: @storage.tenant_id)
+ connection_manager = ::OAuthClients::ConnectionManager
+ .new(user:, configuration: @storage.oauth_configuration)
connection_manager
.get_access_token
diff --git a/spec/services/oauth_clients/connection_manager_spec.rb b/spec/services/oauth_clients/connection_manager_spec.rb
index 52998321274..ac96b749369 100644
--- a/spec/services/oauth_clients/connection_manager_spec.rb
+++ b/spec/services/oauth_clients/connection_manager_spec.rb
@@ -40,7 +40,7 @@ RSpec.describe OAuthClients::ConnectionManager, type: :model, webmock: true do
let(:scope) { [:all] } # OAuth2 resources to access, specific to provider
let(:oauth_client_token) { create(:oauth_client_token, oauth_client:, user:) }
- let(:instance) { described_class.new(user:, oauth_client:, configuration: storage.oauth_configuration) }
+ let(:instance) { described_class.new(user:, configuration: storage.oauth_configuration) }
# The get_authorization_uri method returns the OAuth2 authorization URI as a string. That URI is the starting point for
# a user to grant OpenProject access to Nextcloud.
@@ -384,10 +384,7 @@ RSpec.describe OAuthClients::ConnectionManager, type: :model, webmock: true do
end
end
- context 'with parrallel requests for refresh',
- :aggregate_failures,
- use_transactional_fixtures: false,
- webmock: true do
+ context 'with parrallel requests for refresh', :aggregate_failures do
after do
Storages::Storage.destroy_all
User.destroy_all
@@ -415,12 +412,12 @@ RSpec.describe OAuthClients::ConnectionManager, type: :model, webmock: true do
result2 = nil
thread1 = Thread.new do
ApplicationRecord.connection_pool.with_connection do
- result1 = described_class.new(user:, oauth_client:).refresh_token.result
+ result1 = described_class.new(user:, configuration: storage.oauth_configuration).refresh_token.result
end
end
thread2 = Thread.new do
ApplicationRecord.connection_pool.with_connection do
- result2 = described_class.new(user:, oauth_client:).refresh_token.result
+ result2 = described_class.new(user:, configuration: storage.oauth_configuration).refresh_token.result
end
end
thread1.join
@@ -452,12 +449,12 @@ RSpec.describe OAuthClients::ConnectionManager, type: :model, webmock: true do
thread1 = Thread.new do
ApplicationRecord.connection_pool.with_connection do
sleep(3)
- result1 = described_class.new(user:, oauth_client:).refresh_token.result
+ result1 = described_class.new(user:, configuration: storage.oauth_configuration).refresh_token.result
end
end
thread2 = Thread.new do
ApplicationRecord.connection_pool.with_connection do
- result2 = described_class.new(user:, oauth_client:).refresh_token.result
+ result2 = described_class.new(user:, configuration: storage.oauth_configuration).refresh_token.result
end
end
thread1.join
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 df4c612dd99..930960dd0fd 100644
--- a/spec/services/oauth_clients/one_drive_connection_manager_spec.rb
+++ b/spec/services/oauth_clients/one_drive_connection_manager_spec.rb
@@ -30,7 +30,7 @@
require 'spec_helper'
-RSpec.describe OAuthClients::OneDriveConnectionManager, type: :model, webmock: true do
+RSpec.describe OAuthClients::ConnectionManager, type: :model, webmock: true do
let(:user) { create(:user) }
let(:storage) { create(:one_drive_storage, :with_oauth_client, tenant_id: 'consumers') }
let(:token) { create(:oauth_client_token, oauth_client: storage.oauth_client, user:) }
@@ -43,7 +43,7 @@ RSpec.describe OAuthClients::OneDriveConnectionManager, type: :model, webmock: t
it 'always add the necessary scopes' do
uri = connection_manager.get_authorization_uri(scope: nil, state: nil)
- expect(uri).to include OAuthClients::OneDriveConnectionManager::DEFAULT_SCOPES.join('%20')
+ expect(uri).to include storage.oauth_configuration.compute_scopes(nil).join('%20')
end
it 'adds the state if present' do