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

Merge remote-tracking branch 'origin/dev' into implementation/42843-add-authorization-state-to-storages-api-endpoint

Andreas Pfohl527e46768051
diff --cc app/services/oauth_clients/connection_manager.rb
index 29b0b5b0d52,7a9f3fbe0d5..0df0e6801ad
--- a/app/services/oauth_clients/connection_manager.rb
+++ b/app/services/oauth_clients/connection_manager.rb
@@@ -91,57 -88,10 +91,57 @@@ module OAuthClient
service_result = request_new_token(authorization_code: code)
return service_result unless service_result.success?
- # Create a new OAuthClientToken from Rack::OAuth::AccessToken::Bearer and return
- ServiceResult.success(
- result: create_new_oauth_client_token(service_result.result)
+ # Check for existing OAuthClientToken and update,
+ # or create a new one from Rack::OAuth::AccessToken::Bearer
+ oauth_client_token = get_existing_token
+ if oauth_client_token.present?
+ update_oauth_client_token(oauth_client_token, service_result.result)
+ else
+ oauth_client_token = create_new_oauth_client_token(service_result.result)
+ end
+
- ServiceResult.new(success: true, result: oauth_client_token)
++ ServiceResult.success(result: oauth_client_token)
+ end
+
+ # Called by StorageRepresenter to inquire about the status of the OAuth2
+ # authentication server.
+ # Returns :connected/:authorization_failed or :error for a general error.
+ # We have decided to distinguish between only these 3 cases, because the
+ # front-end (and a normal user) probably wouldn't know how to deal with
+ # other options.
+ def authorization_state
+ oauth_client_token = get_existing_token
+ return :failed_authorization unless oauth_client_token
+
+ # 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.
+ # curl -H "Authorization: Bearer MY_TOKEN" -X GET 'https://my.nextcloud.org/ocs/v1.php/cloud/user' \
+ # -H "OCS-APIRequest: true" -H "Accept: application/json"
+ RestClient.get(
+ File.join(oauth_client.integration.host, AUTHORIZATION_CHECK_PATH),
+ {
+ 'Authorization' => "Bearer #{oauth_client_token.access_token}",
+ 'OCS-APIRequest' => "true",
+ 'Accept' => "application/json"
+ }
)
+ :connected
+ rescue RestClient::Unauthorized, RestClient::Forbidden => _e
+ service_result = refresh_token # `refresh_token` already has exception handling
+ return :connected if service_result.success?
+
+ if service_result.result == 'invalid_grant'
+ # 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
+ rescue StandardError => _e
+ :error
end
private
@@@ -159,10 -109,9 +159,9 @@@
rack_access_token = rack_oauth_client(options)
.access_token!(:body) # Rack::OAuth2::AccessToken
- ServiceResult.new(success: true,
- result: rack_access_token)
+ ServiceResult.success(result: rack_access_token)
rescue Rack::OAuth2::Client::Error => e # Handle Rack::OAuth2 specific errors
- service_result_with_error(i18n_rack_oauth2_error_message(e))
+ service_result_with_error(i18n_rack_oauth2_error_message(e), e.message)
rescue Timeout::Error, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError,
Errno::EINVAL, Errno::ENETUNREACH, Errno::ECONNRESET, Errno::ECONNREFUSED, JSON::ParserError => e
service_result_with_error(
@@@ -249,9 -198,9 +248,9 @@@
# Shortcut method to convert an error message into an unsuccessful
# ServiceResult with that error message
- def service_result_with_error(message, res = nil)
- ServiceResult.new(success: false, result: res).tap do |service_result|
- service_result.errors.add(:base, message)
- def service_result_with_error(message)
- ServiceResult.failure.tap do |result|
- result.errors.add(:base, message)
++ def service_result_with_error(message, result = nil)
++ ServiceResult.failure(result:).tap do |r|
++ r.errors.add(:base, message)
end
end
end