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

Add an interface of sorts to enforce the required methods and offer a unified way to handle the authorization check

Marcello Rocha652c66a0483e
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
new file mode 100644
index 00000000000..0ddaf34b75c
--- /dev/null
+++ b/modules/storages/app/common/storages/peripherals/oauth_configurations/configuration_interface.rb
@@ -0,0 +1,56 @@
+# 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 Storages
+ module Peripherals
+ module OAuthConfigurations
+ class ConfigurationInterface
+ def authorization_state_check(_) = raise ::Storages::Errors::SubclassResponsibility
+
+ def compute_scopes(_) = raise ::Storages::Errors::SubclassResponsibility
+
+ def basic_rack_oauth_client = raise ::Storages::Errors::SubclassResponsibility
+
+ private
+
+ def authorization_check_wrapper
+ case yield
+ when Net::HTTPSuccess
+ :success
+ when Net::HTTPForbidden, Net::HTTPUnauthorized
+ :refresh_needed
+ else
+ :error
+ end
+ end
+ end
+ 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 df6345b8745..af255a9743a 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
@@ -31,7 +31,7 @@
module Storages
module Peripherals
module OAuthConfigurations
- class NextcloudConfiguration
+ class NextcloudConfiguration < ConfigurationInterface
attr_reader :oauth_client
def initialize(storage)
@@ -41,24 +41,18 @@ module Storages
def authorization_state_check(token)
util = StorageInteraction::Nextcloud::Util
- 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'),
- {
- 'Authorization' => "Bearer #{token}",
- 'OCS-APIRequest' => 'true',
- 'Accept' => 'application/json'
- }
- )
- end
- case response
- when Net::HTTPSuccess
- :success
- when Net::HTTPForbidden, Net::HTTPUnauthorized
- :refresh_needed
- else
- :error
+ authorization_check_wrapper do
+ Net::HTTP.start(@uri.host, @uri.port, use_ssl: true) do |http|
+ http.get(
+ util.join_uri_path(@uri, '/ocs/v1.php/cloud/user'),
+ {
+ 'Authorization' => "Bearer #{token}",
+ 'OCS-APIRequest' => 'true',
+ 'Accept' => 'application/json'
+ }
+ )
+ end
end
end
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 7994160026a..c794ab764f2 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
@@ -31,7 +31,7 @@
module Storages
module Peripherals
module OAuthConfigurations
- class OneDriveConfiguration
+ class OneDriveConfiguration < ConfigurationInterface
DEFAULT_SCOPES = %w[offline_access files.readwrite.all user.read sites.readwrite.all].freeze
attr_reader :oauth_client
@@ -44,17 +44,10 @@ 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}", 'Accept' => 'application/json' })
- end
-
- case response
- when Net::HTTPSuccess
- :success
- when Net::HTTPForbidden, Net::HTTPUnauthorized
- :refresh_needed
- else
- :error
+ authorization_check_wrapper do
+ 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
end
end