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

Adds the rest of the authentication_state method

Marcello Rocha3c84dbeb154e
diff --git a/app/services/oauth_clients/one_drive_connection_manager.rb b/app/services/oauth_clients/one_drive_connection_manager.rb
index 4638f7f3aba..73edb94b18d 100644
--- a/app/services/oauth_clients/one_drive_connection_manager.rb
+++ b/app/services/oauth_clients/one_drive_connection_manager.rb
@@ -30,10 +30,11 @@
module OAuthClients
class OneDriveConnectionManager < ConnectionManager
- OAUTH_URI = URI.parse('https://login.microsoftonline.com/')
- AUTHORIZATION_CHECK_PATH = "https://graph.microsoft.com/v1.0/me"
+ 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
@@ -41,11 +42,34 @@ module OAuthClients
def authorization_state
current_token = get_existing_token
- :failed_authorization unless current_token
+ return :failed_authorization unless current_token
+
+ response = Net::HTTP.start(GRAPH_API_URI.host.host, GRAPH_API_URI.host.port, use_ssl: true) do |http|
+ http.get('/v1.0/me', { 'Authorization' => "Bearer #{oauth_client_token.access_token}" })
+ end
- # Call AUTHORIZATION_CHECK_PATH
- # And check for the response
- # In case of error failN
+ 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
+ else
+ raise StandardError, 'not sure what to do'
+ end
+ rescue StandardError
+ :error
end
def get_authorization_uri(scope: [], state: nil)
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 1398c760012..966f92327f3 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
@@ -54,7 +54,7 @@ module Storages
handle_response(response)
end
- result.result[:value].map { |file| storage_file(file) }
+ result.map { |json_files| storage_files(json_files) }
end
private
@@ -62,7 +62,7 @@ module Storages
def handle_response(response)
case response
when Net::HTTPSuccess
- ServiceResult.success(result: MultiJson.load(response.body, symbolize_keys: true))
+ ServiceResult.success(result: MultiJson.load(response.body, symbolize_keys: true)[:value])
when Net::HTTPNotFound
ServiceResult.failure(result: :not_found, errors: ::Storages::StorageError.new(code: :not_found))
when Net::HTTPUnauthorized
@@ -78,19 +78,21 @@ module Storages
"/v1.0/drives/#{@storage.drive_id}/items/#{folder}/children"
end
- def storage_file(json)
- StorageFile.new(
- id: json[:id],
- name: json[:name],
- size: json[:size],
- mime_type: json.dig(:file, :mimeType) || 'application/x-op-directory',
- created_at: DateTime.parse(json.dig(:fileSystemInfo, :createdDateTime)),
- last_modified_at: DateTime.parse(json.dig(:fileSystemInfo, :lastModifiedDateTime)),
- created_by_name: json.dig(:createdBy, :user, :displayName),
- last_modified_by_name: json.dig(:lastModifiedBy, :user, :displayName),
- location: json[:webUrl],
- permissions: nil
- )
+ def storage_files(json_files)
+ json_files.map do |json|
+ StorageFile.new(
+ id: json[:id],
+ name: json[:name],
+ size: json[:size],
+ mime_type: json.dig(:file, :mimeType) || 'application/x-op-directory',
+ created_at: DateTime.parse(json.dig(:fileSystemInfo, :createdDateTime)),
+ last_modified_at: DateTime.parse(json.dig(:fileSystemInfo, :lastModifiedDateTime)),
+ created_by_name: json.dig(:createdBy, :user, :displayName),
+ last_modified_by_name: json.dig(:lastModifiedBy, :user, :displayName),
+ location: json[:webUrl],
+ permissions: nil
+ )
+ end
end
def mime_type(json)
diff --git a/modules/storages/spec/common/peripherals/storage_interaction/one_drive/files_query_spec.rb b/modules/storages/spec/common/peripherals/storage_interaction/one_drive/files_query_spec.rb
index 8b09eed32ab..e8d0ebb5cf4 100644
--- a/modules/storages/spec/common/peripherals/storage_interaction/one_drive/files_query_spec.rb
+++ b/modules/storages/spec/common/peripherals/storage_interaction/one_drive/files_query_spec.rb
@@ -49,7 +49,7 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::FilesQuery,
.with(headers: { 'Authorization' => "Bearer #{token.access_token}" })
.to_return(status: 200, body: json, headers: {})
- storage_files = described_class.call(storage:, user:, folder: nil)
+ storage_files = described_class.call(storage:, user:, folder: nil).result
expect(storage_files).to all(be_a(Storages::StorageFile))
one_file = storage_files[10]
@@ -73,7 +73,7 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::FilesQuery,
.with(headers: { 'Authorization' => "Bearer #{token.access_token}" })
.to_return(status: 200, body: json, headers: {})
- storage_files = described_class.call(storage:, user:, folder: nil)
+ storage_files = described_class.call(storage:, user:, folder: nil).result
expect(storage_files.size).to eq(38)
end
@@ -92,7 +92,7 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::FilesQuery,
.with(headers: { 'Authorization' => "Bearer #{token.access_token}" })
.to_return(status: 200, body: json, headers: {})
- storage_files = described_class.call(storage:, user:, folder: '01BYE5RZYJ43UXGBP23BBIFPISHHMCDTOY')
+ storage_files = described_class.call(storage:, user:, folder: '01BYE5RZYJ43UXGBP23BBIFPISHHMCDTOY').result
expect(storage_files.size).to eq(22)
end
@@ -100,22 +100,27 @@ RSpec.describe Storages::Peripherals::StorageInteraction::OneDrive::FilesQuery,
describe 'error handling' do
it 'returns a notfound error if the API call returns a 404' do
- skip("Seems that our error handling isn't handling errors")
-
stub_request(:get, "https://graph.microsoft.com/v1.0/me/drive/root/children?$select=id,name,size,webUrl,lastModifiedBy,createdBy,fileSystemInfo,file,folder")
.with(headers: { 'Authorization' => "Bearer #{token.access_token}" })
.to_return(status: 404, body: '', headers: {})
storage_files = described_class.call(storage:, user:, folder: nil)
+
expect(storage_files).to be_failure
+ expect(storage_files.result).to eq(:not_found)
+ expect(storage_files.errors.to_s).to eq(Storages::StorageError.new(code: :not_found).to_s)
end
it 'retries authentication when it returns a 401' do
- skip("Seems that our error handling isn't handling errors")
-
stub_request(:get, "https://graph.microsoft.com/v1.0/me/drive/root/children?$select=id,name,size,webUrl,lastModifiedBy,createdBy,fileSystemInfo,file,folder")
.with(headers: { 'Authorization' => "Bearer #{token.access_token}" })
.to_return(status: 401, body: '', headers: {})
+
+ storage_files = described_class.call(storage:, user:, folder: nil)
+
+ expect(storage_files).to be_failure
+ expect(storage_files.result).to eq(:not_authorized)
+ expect(storage_files.errors.to_s).to eq(Storages::StorageError.new(code: :not_authorized).to_s)
end
end