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

Addresses feedback, adds pattern matching to ServiceResult

Marcello Rocha8209dbdd5164
diff --git a/app/services/service_result.rb b/app/services/service_result.rb
index 265d8f6c2d0..660103e5d39 100644
--- a/app/services/service_result.rb
+++ b/app/services/service_result.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2024 the OpenProject GmbH
@@ -190,6 +192,24 @@ class ServiceResult
end
end
+ def deconstruct_keys(keys)
+ if keys
+ value = {}
+ keys.each do |key|
+ case key
+ when :success then value[key] = success?
+ when :failure then value[key] = failure?
+ when :result then value[key] = result
+ when :errors then value[key] = errors
+ end
+ end
+
+ value
+ else
+ { success: success?, failure: failure?, result:, errors: }
+ end
+ end
+
def message
if @message
@message
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/oauth_client_credentials.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/oauth_client_credentials.rb
index 9d041f1c315..a1f791f1a99 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/oauth_client_credentials.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/oauth_client_credentials.rb
@@ -37,56 +37,57 @@ module Storages
Strategy.new(:oauth_client_credentials)
end
- # rubocop:disable Metrics/AbcSize
def call(storage:, http_options: {})
config = storage.oauth_configuration.to_httpx_oauth_config
-
return build_failure(storage) unless config.valid?
- access_token = Rails.cache.read("storage.#{storage.id}.httpx_access_token")
-
- http_result = if access_token.present?
- http_with_current_token(access_token:, http_options:)
- else
- http_with_new_token(issuer: config.issuer,
- client_id: config.client_id,
- client_secret: config.client_secret,
- scope: config.scope,
- http_options:)
- end
-
- return http_result if http_result.failure?
+ token_cache_key = cache_key(storage)
+ access_token = Rails.cache.read(token_cache_key)
- http = http_result.result
+ # In ruby 3.4 this can become `return it`
+ http = build_http_session(access_token, config, http_options).on_failure { return _1 }.result
operation_result = yield http
- if access_token.nil? && operation_result.success?
- token = http.instance_variable_get(:@options).oauth_session.access_token
- Rails.cache.write("storage.#{storage.id}.httpx_access_token", token, expires_in: 50.minutes)
- elsif operation_result.failure? && operation_result.result == :forbidden
- Rails.cache.delete("storage.#{storage.id}.httpx_access_token")
+ case operation_result
+ in success: true
+ write_cache(token_cache_key, http) if access_token.blank?
+ in failure: true, result: :forbidden
+ clear_cache(token_cache_key)
+ else
+ return operation_result
end
operation_result
end
- # rubocop:enable Metrics/AbcSize
-
private
+ def write_cache(key, httpx_session)
+ access_token = httpx_session.instance_variable_get(:@options).oauth_session.access_token
+ Rails.cache.write(key, access_token, expires_in: 50.minutes)
+ end
+
+ def clear_cache(key) = Rails.cache.delete(key)
+
+ def build_http_session(access_token, config, http_options)
+ if access_token.present?
+ http_with_current_token(access_token:, http_options:)
+ else
+ http_with_new_token(config:, http_options:)
+ end
+ end
+
+ def cache_key(storage) = "storage.#{storage.id}.httpx_access_token"
+
def http_with_current_token(access_token:, http_options:)
opts = http_options.deep_merge({ headers: { "Authorization" => "Bearer #{access_token}" } })
ServiceResult.success(result: OpenProject.httpx.with(opts))
end
- def http_with_new_token(issuer:, client_id:, client_secret:, scope:, http_options:)
+ def http_with_new_token(config:, http_options:)
http = OpenProject.httpx
- .oauth_auth(issuer:,
- client_id:,
- client_secret:,
- scope:,
- token_endpoint_auth_method: "client_secret_post")
+ .oauth_auth(**config.to_h, token_endpoint_auth_method: "client_secret_post")
.with_access_token
.with(http_options)
ServiceResult.success(result: http)
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/oauth_configuration.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/oauth_configuration.rb
index 263bcd29189..c5d6cba15f6 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/oauth_configuration.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/oauth_configuration.rb
@@ -48,6 +48,10 @@ module Storages
@issuer = issuer
@scope = scope
end
+
+ def to_h
+ { issuer:, client_id:, client_secret:, scope: }
+ end
end
end
end
diff --git a/modules/storages/spec/common/storages/peripherals/storage_interaction/authentication_strategies/oauth_client_credentials_spec.rb b/modules/storages/spec/common/storages/peripherals/storage_interaction/authentication_strategies/oauth_client_credentials_spec.rb
index eb18533212f..92bac4c68ac 100644
--- a/modules/storages/spec/common/storages/peripherals/storage_interaction/authentication_strategies/oauth_client_credentials_spec.rb
+++ b/modules/storages/spec/common/storages/peripherals/storage_interaction/authentication_strategies/oauth_client_credentials_spec.rb
@@ -33,6 +33,7 @@ require_module_spec_helper
RSpec.describe Storages::Peripherals::StorageInteraction::AuthenticationStrategies::OAuthClientCredentials, :webmock do
let(:storage) { create(:sharepoint_dev_drive_storage) }
+ let(:cache_key) { "storage.#{storage.id}.httpx_access_token" }
subject(:strategy) { described_class.new }
@@ -46,7 +47,7 @@ RSpec.describe Storages::Peripherals::StorageInteraction::AuthenticationStrategi
end
it "does not cache the result" do
- expect(Rails.cache.fetch("storage.#{storage.id}.httpx_access_token")).to be_nil
+ expect(Rails.cache.fetch(cache_key)).to be_nil
strategy.call(storage:) do |http|
http.get("https://graph.microsoft.com/v1.0/drives/#{storage.drive_id}/root").raise_for_status
@@ -54,7 +55,7 @@ RSpec.describe Storages::Peripherals::StorageInteraction::AuthenticationStrategi
ServiceResult.failure
end
- expect(Rails.cache.fetch("storage.#{storage.id}.httpx_access_token")).to be_nil
+ expect(Rails.cache.fetch(cache_key)).to be_nil
end
it "returns the result of the operation" do
@@ -69,7 +70,7 @@ RSpec.describe Storages::Peripherals::StorageInteraction::AuthenticationStrategi
end
it "clears an already existing token" do
- Rails.cache.write("storage.#{storage.id}.httpx_access_token", "BORKED_TOKEN")
+ Rails.cache.write(cache_key, "BORKED_TOKEN")
strategy.call(storage:) do |http|
http.get("https://graph.microsoft.com/v1.0/drives/#{storage.drive_id}/root").raise_for_status
@@ -77,7 +78,7 @@ RSpec.describe Storages::Peripherals::StorageInteraction::AuthenticationStrategi
ServiceResult.failure(result: :forbidden)
end
- expect(Rails.cache.fetch("storage.#{storage.id}.httpx_access_token")).to be_nil
+ expect(Rails.cache.fetch(cache_key)).to be_nil
end
end
@@ -93,14 +94,14 @@ RSpec.describe Storages::Peripherals::StorageInteraction::AuthenticationStrategi
end
it "caches the generated token" do
- expect(Rails.cache.fetch("storage.#{storage.id}.httpx_access_token")).to be_nil
+ expect(Rails.cache.fetch(cache_key)).to be_nil
strategy.call(storage:) do |http|
http.get("https://graph.microsoft.com/v1.0/drives/#{storage.drive_id}/root").raise_for_status
ServiceResult.success
end
- expect(Rails.cache.fetch("storage.#{storage.id}.httpx_access_token")).to eq("TOTALLY_VALID_TOKEN")
+ expect(Rails.cache.fetch(cache_key)).to eq("TOTALLY_VALID_TOKEN")
end
it "returns the result of the operation" do