Compare commits

...

1 Commits

Author SHA1 Message Date
Eric Schubert 7a3c2033d0
[chore] added error handling for broken oauth data 2 years ago
  1. 7
      app/services/oauth_clients/connection_manager.rb
  2. 26
      modules/storages/lib/api/v3/file_links/work_packages_file_links_api.rb

@ -37,6 +37,9 @@ module OAuthClients
attr_reader :user, :oauth_client attr_reader :user, :oauth_client
def initialize(user:, oauth_client:) def initialize(user:, oauth_client:)
raise ArgumentError, 'Missing user' if user.blank?
raise ArgumentError, 'Missing oauth_client' if oauth_client.blank?
@user = user @user = user
@oauth_client = oauth_client @oauth_client = oauth_client
end end
@ -112,7 +115,7 @@ module OAuthClients
# other options. # other options.
def authorization_state def authorization_state
oauth_client_token = get_existing_token oauth_client_token = get_existing_token
return :failed_authorization unless oauth_client_token return :failed_authorization if oauth_client_token.blank?
# Check for user information. This is the cheapest Nextcloud call that requires # Check for user information. This is the cheapest Nextcloud call that requires
# valid authentication, so we use it for testing the validity of the Bearer token. # valid authentication, so we use it for testing the validity of the Bearer token.
@ -172,7 +175,7 @@ module OAuthClients
# Don't handle the case of an expired token. # Don't handle the case of an expired token.
def get_existing_token def get_existing_token
# Check if we've got a token in the database and return nil otherwise. # Check if we've got a token in the database and return nil otherwise.
OAuthClientToken.find_by(user_id: @user, oauth_client_id: @oauth_client.id) OAuthClientToken.find_by(user: @user, oauth_client: @oauth_client)
end end
# Calls client.access_token! # Calls client.access_token!

@ -65,23 +65,17 @@ module API
.where(container_id: @work_package.id, container_type: 'WorkPackage')) .where(container_id: @work_package.id, container_type: 'WorkPackage'))
.all .all
begin # Synchronize with Nextcloud. StorageAPI has handled OAuth2 for us before.
# Synchronize with Nextcloud. StorageAPI has handled OAuth2 for us before. # We ignore the result, because partial errors (storage network issues) are written to each FileLink
# We ignore the result, because partial errors (storage network issues) are written to each FileLink service_result = ::Storages::FileLinkSyncService
service_result = ::Storages::FileLinkSyncService .new(user: current_user)
.new(user: current_user) .call(file_links)
.call(file_links)
::API::V3::FileLinks::FileLinkCollectionRepresenter.new( ::API::V3::FileLinks::FileLinkCollectionRepresenter.new(
service_result.result, service_result.result,
self_link: api_v3_paths.file_links(@work_package.id), self_link: api_v3_paths.file_links(@work_package.id),
current_user: current_user:
) )
rescue StandardError => e
# Example case: invalid oauth_client for storage raises invalid operation => 500
message = "#{I18n.t('api_v3.errors.code_500')}: #{e.message}"
raise ::API::Errors::InternalError.new(message)
end
end end
post &CreateEndpoint post &CreateEndpoint

Loading…
Cancel
Save