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

[#56011] reset user tokens when refreshing token fails

Eric Schuberta8d5c77c620b
diff --git a/app/services/oauth_clients/connection_manager.rb b/app/services/oauth_clients/connection_manager.rb
index 4c9ab3921af..5209ca058c9 100644
--- a/app/services/oauth_clients/connection_manager.rb
+++ b/app/services/oauth_clients/connection_manager.rb
@@ -155,12 +155,19 @@ module OAuthClients
OAuthClientToken.find_by(user_id: @user, oauth_client_id: @oauth_client.id)
end
+ # rubocop:disable Metrics/AbcSize
def request_new_token(options = {})
rack_access_token = rack_oauth_client(options).access_token!(:body)
ServiceResult.success(result: rack_access_token)
rescue Rack::OAuth2::Client::Error => e
- service_result_with_error(:bad_request, e.response, i18n_rack_oauth2_error_message(e))
+ if e.status == 429
+ service_result_with_error(:too_many_requests,
+ e.response,
+ "#{I18n.t('oauth_client.errors.oauth_reported')}: too many requests")
+ else
+ service_result_with_error(:bad_request, e.response, i18n_rack_oauth2_error_message(e))
+ end
rescue Faraday::TimeoutError, Faraday::ConnectionFailed, Faraday::ParsingError, Faraday::SSLError => e
service_result_with_error(
:internal_server_error,
@@ -175,6 +182,8 @@ module OAuthClients
)
end
+ # rubocop:enable Metrics/AbcSize
+
def i18n_rack_oauth2_error_message(rack_oauth2_client_exception)
i18n_key = "oauth_client.errors.rack_oauth2.#{rack_oauth2_client_exception.message}"
if I18n.exists? i18n_key
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/failures.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/failures.rb
index ad2740f842b..f68fbab077b 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/failures.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/failures.rb
@@ -34,7 +34,7 @@ module Storages
module AuthenticationStrategies
module Failures
Builder = ->(code:, log_message:, data:) do
- storage_error = ::Storages::StorageError.new(code:, log_message:, data:)
+ storage_error = StorageError.new(code:, log_message:, data:)
ServiceResult.failure(result: code, errors: storage_error)
end
@@ -49,7 +49,11 @@ module Storages
response.body.to_s
end
- ::Storages::StorageErrorData.new(source:, payload:)
+ StorageErrorData.new(source:, payload:)
+ end
+
+ TimeoutErrorData = ->(error:, source:) do
+ StorageErrorData.new(source:, payload: error.to_s)
end
end
end
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 a1f791f1a99..35cba2f6c69 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
@@ -95,6 +95,10 @@ module Storages
Failures::Builder.call(code: :unauthorized,
log_message: "Error while fetching OAuth access token.",
data: Failures::ErrorData.call(response: e.response, source: self.class))
+ rescue HTTPX::TimeoutError => e
+ Failures::Builder.call(code: :unauthorized,
+ log_message: "Timeout while fetching OAuth token.",
+ data: Failures::TimeoutErrorData.call(error: e, source: self.class))
end
def build_failure(storage)
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/oauth_user_token.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/oauth_user_token.rb
index a88f64e36d7..31a0cb96988 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/oauth_user_token.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/authentication_strategies/oauth_user_token.rb
@@ -67,7 +67,7 @@ module Storages
if storage.oauth_client.blank?
log_message = "Authorization failed. Storage has no configured oauth client credentials."
- return Failures::Builder.call(code: :unauthorized, log_message:, data:)
+ return Failures::Builder.call(code: :error, log_message:, data:)
end
current_token = OAuthClientToken.find_by(user: @user,
@@ -94,9 +94,9 @@ module Storages
.with_access_token
.with(http_options)
rescue HTTPX::HTTPError => e
- return Failures::Builder.call(code: :unauthorized,
- log_message: "Error while refreshing OAuth token.",
- data: Failures::ErrorData.call(response: e.response, source: self.class))
+ return handle_http_error_on_refresh(token, e)
+ rescue HTTPX::TimeoutError => e
+ return handle_timeout_on_refresh(token, e)
end
response = yield http_session
@@ -124,6 +124,35 @@ module Storages
token.update(access_token:, refresh_token:)
end
+ def log_and_destroy_token(token, log_message)
+ Rails.logger.error(log_message)
+ token.destroy
+ end
+
+ def handle_http_error_on_refresh(token, exception)
+ log_message = "Error while refreshing OAuth token."
+ data = Failures::ErrorData.call(response: exception.response, source: self.class)
+
+ Rails.logger.error("#{log_message} - Payload: #{data.payload}")
+
+ # Delete token from database to enforce new user login
+ token.destroy
+
+ Failures::Builder.call(code: :unauthorized, log_message:, data:)
+ end
+
+ def handle_timeout_on_refresh(token, exception)
+ log_message = "Timeout while refreshing OAuth token."
+ data = Failures::TimeoutErrorData.call(error: exception, source: self.class)
+
+ Rails.logger.error("#{log_message} - Payload: #{data.payload}")
+
+ # Delete token from database to enforce new user login
+ token.destroy
+
+ Failures::Builder.call(code: :unauthorized, log_message:, data:)
+ end
+
def build_failure(storage)
log_message = "Cannot refresh user token for storage. Storage authentication credentials not configured."
data = ::Storages::StorageErrorData.new(source: self.class, payload: storage)