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

[#66464] Try to simplify code a bit.

Pavel Balashoucac815d2ab31
diff --git a/modules/storages/app/common/storages/adapters/authentication_strategies/oauth_user_token.rb b/modules/storages/app/common/storages/adapters/authentication_strategies/oauth_user_token.rb
index be15d391543..650f4951fc6 100644
--- a/modules/storages/app/common/storages/adapters/authentication_strategies/oauth_user_token.rb
+++ b/modules/storages/app/common/storages/adapters/authentication_strategies/oauth_user_token.rb
@@ -44,13 +44,14 @@ module Storages
oauth_client = validate_oauth_client(storage).value_or { return Failure(it) }
token = current_token(oauth_client).value_or { return Failure(it) }
- options = http_options.deep_merge(headers: { "Authorization" => "Bearer #{token.access_token}" })
-
- original_response = yield OpenProject.httpx.with(options)
+ original_response = yield(httpx_with_auth(token.access_token, http_options))
case original_response
in Failure(code: :unauthorized)
- refresh_and_retry(storage, token, options, &)
+ updated_token = refresh_token!(storage.oauth_configuration.to_httpx_oauth_config.to_h,
+ http_options,
+ token).value_or { return Failure(it) }
+ yield(httpx_with_auth(updated_token.access_token, http_options))
else
original_response
end
@@ -65,28 +66,30 @@ module Storages
private
+ def httpx_with_auth(access_token, http_options)
+ OpenProject
+ .httpx
+ .with(http_options.deep_merge(headers: { "Authorization" => "Bearer #{access_token}" }))
+ end
+
def validate_oauth_client(storage)
return Success(storage.oauth_client) if storage.oauth_client
Failure(@error_data.with(code: :missing_oauth_client, payload: storage))
end
- def refresh_and_retry(storage, token, options, &)
- config = storage.oauth_configuration.to_httpx_oauth_config.to_h
- refreshed_session = refresh_token(config, options, token).value_or { return Failure(it) }
- update_token(token, refreshed_session)
- options = options.deep_merge(headers: { "Authorization" => "Bearer #{token.access_token}" })
- yield OpenProject.httpx.with(options)
- end
-
- def update_token(token, http_session)
- oauth_session = http_session.instance_variable_get(:@options).oauth_session
- token.update(access_token: oauth_session.access_token, refresh_token: oauth_session.refresh_token)
- end
-
- def refresh_token(oauth_config, options, token)
- httpx_config = oauth_config.merge(refresh_token: token.refresh_token, token_endpoint_auth_method: "client_secret_post")
- Success(OpenProject.httpx.oauth_auth(**httpx_config).with(options).with_access_token)
+ def refresh_token!(oauth_config, http_options, token)
+ oauth_session = OpenProject
+ .httpx
+ .oauth_auth(**oauth_config,
+ refresh_token: token.refresh_token,
+ token_endpoint_auth_method: "client_secret_post")
+ .with(http_options)
+ .with_access_token
+ .instance_variable_get(:@options)
+ .oauth_session
+ token.update!(access_token: oauth_session.access_token, refresh_token: oauth_session.refresh_token)
+ Success(token)
rescue HTTPX::HTTPError => e
handle_http_error(token, e)
rescue HTTPX::TimeoutError => e