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
Initial mess
Marcello Rocha6017d0517750
diff --git a/app/models/oauth_client_token.rb b/app/models/oauth_client_token.rbindex b628a89e075..2ee4b12b449 100644--- a/app/models/oauth_client_token.rb+++ b/app/models/oauth_client_token.rb@@ -1,3 +1,5 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.# Copyright (C) 2012-2023 the OpenProject GmbH@@ -36,6 +38,6 @@ class OAuthClientToken < ApplicationRecordvalidates :user, uniqueness: { scope: :oauth_client }- validates :access_token, length: { minimum: 1, maximum: 255 }- validates :refresh_token, length: { minimum: 1, maximum: 255 }+ validates :access_token, length: { minimum: 1 }+ validates :refresh_token, length: { minimum: 1 }enddiff --git a/app/services/oauth_clients/connection_manager.rb b/app/services/oauth_clients/connection_manager.rbindex 8417800ad8b..0e9d877b5fa 100644--- a/app/services/oauth_clients/connection_manager.rb+++ b/app/services/oauth_clients/connection_manager.rb@@ -1,3 +1,5 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.# Copyright (C) 2012-2023 the OpenProject GmbH@@ -32,7 +34,7 @@ require "uri/http"module OAuthClientsclass ConnectionManager# Nextcloud API endpoint to check if Bearer token is valid- AUTHORIZATION_CHECK_PATH = '/ocs/v1.php/cloud/user'.freeze+ AUTHORIZATION_CHECK_PATH = '/ocs/v1.php/cloud/user'TOKEN_IS_FRESH_DURATION = 10.seconds.freezeattr_reader :user, :oauth_clientdiff --git a/app/services/oauth_clients/one_drive_connection_manager.rb b/app/services/oauth_clients/one_drive_connection_manager.rbnew file mode 100644index 00000000000..e85330e0eb5--- /dev/null+++ b/app/services/oauth_clients/one_drive_connection_manager.rb@@ -0,0 +1,70 @@+# 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.parse('https://login.microsoftonline.com/')+ AUTHORIZATION_CHECK_PATH = "https://graph.microsoft.com/v1.0/me"+ DEFAULT_SCOPES = %w[offline_access files.readwrite.all user.read sites.readwrite.all].freeze++ def initialize(user:, oauth_client:, tenant_id:)+ super(user:, oauth_client:)+ @tenant_id = tenant_id+ end++ def authorization_state+ current_token = get_existing_token+ :failed_authorization unless current_token++ # Call AUTHORIZATION_CHECK_PATH+ # And check for the response+ # In case of error fail+ end++ def get_authorization_uri(scope: [], state: nil)+ joined_scope = (scope | DEFAULT_SCOPES).join(' ')+ 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+enddiff --git a/modules/storages/app/common/storages/peripherals/registry.rb b/modules/storages/app/common/storages/peripherals/registry.rbindex 20c27cc7f64..60c808b8ecd 100644--- a/modules/storages/app/common/storages/peripherals/registry.rb+++ b/modules/storages/app/common/storages/peripherals/registry.rb@@ -54,6 +54,8 @@ module StoragesRegistry.import StorageInteraction::Nextcloud::QueriesRegistry.import StorageInteraction::Nextcloud::Commands+ Registry.import StorageInteraction::OneDrive::Queries+Registry.import Contractsendenddiff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive.rbnew file mode 100644index 00000000000..7e7e37fca23--- /dev/null+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive.rb@@ -0,0 +1,43 @@+# 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 StorageInteraction+ module OneDrive+ Queries = Dry::Container::Namespace.new('queries') do+ namespace('one_drive') do+ register(:files, FilesQuery)+ end+ end+ end+ end+ end+enddiff --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.rbnew file mode 100644index 00000000000..4bed0e780fe--- /dev/null+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/files_query.rb@@ -0,0 +1,114 @@+# 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 StorageInteraction+ module OneDrive+ class FilesQuery+ using ServiceResultRefinements+ def self.call(storage:, user:, folder:)+ new(storage).call(user:, folder:)+ end++ def initialize(storage)+ @storage = storage+ end++ def call(user:, folder:)+ # result = Util.token(user:, oauth_client: @oauth_client) do |token|+ # base_path = Util.join_uri_path(@uri.path, "remote.php/dav/files")+ # @location_prefix = Util.join_uri_path(base_path, token.origin_user_id.gsub(' ', '%20'))+ #+ # response = Util.http(@uri).propfind(+ # Util.join_uri_path(base_path, CGI.escapeURIComponent(token.origin_user_id), requested_folder(folder)),+ # requested_properties,+ # {+ # 'Depth' => '1',+ # 'Authorization' => "Bearer #{token.access_token}"+ # }+ # )+ #+ # case response+ # when Net::HTTPSuccess+ # ServiceResult.success(result: response.body)+ # when Net::HTTPNotFound+ # Util.error(:not_found)+ # when Net::HTTPUnauthorized+ # Util.error(:not_authorized)+ # else+ # Util.error(:error)+ # end+ # end+ #+ # storage_files(result)+ uri = URI('https://graph.microsoft.com').normalize++ using_user_token(user) do |token|+ # Make the Get Request to the necessary endpoints+ request = Net::HTTP.new(uri.host, uri.port)+ request.use_ssl = true+ p request.get(+ '/drives/root/items/children',+ {+ 'Authorization' => "Bearer #{token.access_token}"+ }+ )+ # grab the response+ # parse!+ # Do stuff!+ end++ # Using a token+ # Call the folder_path+ # grab the response+ # parse into Storages::Files+ end++ private++ def using_user_token(user, &block)+ connection_manager = ::OAuthClients::OneDriveConnectionManager+ .new(user:, oauth_client: @storage.oauth_client, tenant_id: @storage.provider_fields['tenant_id'])++ connection_manager+ .get_access_token+ .match(+ on_success: ->(token) do+ connection_manager.request_with_token_refresh(token) { block.call(token) }+ end,+ on_failure: ->(_) { error(:not_authorized, 'Query could not be created! No access token found!') }+ )+ end+ end+ end+ end+ end+enddiff --git a/modules/storages/app/models/storages/one_drive_storage.rb b/modules/storages/app/models/storages/one_drive_storage.rbindex 1a27320528f..9acc3b8167e 100644--- a/modules/storages/app/models/storages/one_drive_storage.rb+++ b/modules/storages/app/models/storages/one_drive_storage.rb@@ -30,5 +30,6 @@module Storagesclass OneDriveStorage < Storage+ store_attribute :provider_fields, :tenant_id, :stringendend