From f3be6052867343ab15cc84bc91edcaf0c1d70115 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Mon, 19 Jun 2017 01:51:04 +0200 Subject: Rename FollowRemoteAccountService to ResolveRemoteAccountService (#3847) Rename Activitypub to ActivityPub --- .../api/activitypub/activities_controller.rb | 2 +- .../api/activitypub/notes_controller.rb | 2 +- .../api/activitypub/outbox_controller.rb | 2 +- app/controllers/authorize_follows_controller.rb | 2 +- app/services/account_search_service.rb | 2 +- app/services/concerns/author_extractor.rb | 2 +- app/services/follow_remote_account_service.rb | 108 --------------------- app/services/follow_service.rb | 2 +- app/services/process_mentions_service.rb | 2 +- app/services/resolve_remote_account_service.rb | 108 +++++++++++++++++++++ .../types/collection.activitystreams2.rabl | 2 +- .../ordered_collection_page.activitystreams2.rabl | 2 +- app/workers/import_worker.rb | 4 +- 13 files changed, 120 insertions(+), 120 deletions(-) delete mode 100644 app/services/follow_remote_account_service.rb create mode 100644 app/services/resolve_remote_account_service.rb (limited to 'app') diff --git a/app/controllers/api/activitypub/activities_controller.rb b/app/controllers/api/activitypub/activities_controller.rb index 740c8589a..a880ee92f 100644 --- a/app/controllers/api/activitypub/activities_controller.rb +++ b/app/controllers/api/activitypub/activities_controller.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -class Api::Activitypub::ActivitiesController < Api::BaseController +class Api::ActivityPub::ActivitiesController < Api::BaseController include Authorization # before_action :set_follow, only: [:show_follow] diff --git a/app/controllers/api/activitypub/notes_controller.rb b/app/controllers/api/activitypub/notes_controller.rb index 783c1c4ed..96652b879 100644 --- a/app/controllers/api/activitypub/notes_controller.rb +++ b/app/controllers/api/activitypub/notes_controller.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -class Api::Activitypub::NotesController < Api::BaseController +class Api::ActivityPub::NotesController < Api::BaseController include Authorization before_action :set_status diff --git a/app/controllers/api/activitypub/outbox_controller.rb b/app/controllers/api/activitypub/outbox_controller.rb index 0738d7dee..1af04cb54 100644 --- a/app/controllers/api/activitypub/outbox_controller.rb +++ b/app/controllers/api/activitypub/outbox_controller.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -class Api::Activitypub::OutboxController < Api::BaseController +class Api::ActivityPub::OutboxController < Api::BaseController before_action :set_account respond_to :activitystreams2 diff --git a/app/controllers/authorize_follows_controller.rb b/app/controllers/authorize_follows_controller.rb index b15883d58..da4ef022a 100644 --- a/app/controllers/authorize_follows_controller.rb +++ b/app/controllers/authorize_follows_controller.rb @@ -40,7 +40,7 @@ class AuthorizeFollowsController < ApplicationController end def account_from_remote_follow - FollowRemoteAccountService.new.call(acct_without_prefix) + ResolveRemoteAccountService.new.call(acct_without_prefix) end def acct_param_is_url? diff --git a/app/services/account_search_service.rb b/app/services/account_search_service.rb index 2b77ad7c6..c266494f0 100644 --- a/app/services/account_search_service.rb +++ b/app/services/account_search_service.rb @@ -18,7 +18,7 @@ class AccountSearchService < BaseService return [] if query_blank_or_hashtag? || limit < 1 if resolving_non_matching_remote_account? - [FollowRemoteAccountService.new.call("#{query_username}@#{query_domain}")] + [ResolveRemoteAccountService.new.call("#{query_username}@#{query_domain}")] else search_results_and_exact_match.compact.uniq.slice(0, limit) end diff --git a/app/services/concerns/author_extractor.rb b/app/services/concerns/author_extractor.rb index ae32eebbb..00fe1c663 100644 --- a/app/services/concerns/author_extractor.rb +++ b/app/services/concerns/author_extractor.rb @@ -18,6 +18,6 @@ module AuthorExtractor acct = "#{username}@#{domain}" end - FollowRemoteAccountService.new.call(acct, update_profile) + ResolveRemoteAccountService.new.call(acct, update_profile) end end diff --git a/app/services/follow_remote_account_service.rb b/app/services/follow_remote_account_service.rb deleted file mode 100644 index 30ba7bc75..000000000 --- a/app/services/follow_remote_account_service.rb +++ /dev/null @@ -1,108 +0,0 @@ -# frozen_string_literal: true - -class FollowRemoteAccountService < BaseService - include OStatus2::MagicKey - include HttpHelper - - DFRN_NS = 'http://purl.org/macgirvin/dfrn/1.0' - - # Find or create a local account for a remote user. - # When creating, look up the user's webfinger and fetch all - # important information from their feed - # @param [String] uri User URI in the form of username@domain - # @return [Account] - def call(uri, update_profile = true, redirected = nil) - username, domain = uri.split('@') - - return Account.find_local(username) if TagManager.instance.local_domain?(domain) - - account = Account.find_remote(username, domain) - return account unless account_needs_webfinger_update?(account) - - Rails.logger.debug "Looking up webfinger for #{uri}" - - data = Goldfinger.finger("acct:#{uri}") - - raise Goldfinger::Error, 'Missing resource links' if data.link('http://schemas.google.com/g/2010#updates-from').nil? || data.link('salmon').nil? || data.link('http://webfinger.net/rel/profile-page').nil? || data.link('magic-public-key').nil? - - # Disallow account hijacking - confirmed_username, confirmed_domain = data.subject.gsub(/\Aacct:/, '').split('@') - - unless confirmed_username.casecmp(username).zero? && confirmed_domain.casecmp(domain).zero? - return call("#{confirmed_username}@#{confirmed_domain}", update_profile, true) if redirected.nil? - raise Goldfinger::Error, 'Requested and returned acct URI do not match' - end - - return Account.find_local(confirmed_username) if TagManager.instance.local_domain?(confirmed_domain) - - confirmed_account = Account.find_remote(confirmed_username, confirmed_domain) - if confirmed_account.nil? - Rails.logger.debug "Creating new remote account for #{uri}" - - domain_block = DomainBlock.find_by(domain: domain) - account = Account.new(username: confirmed_username, domain: confirmed_domain) - account.suspended = true if domain_block && domain_block.suspend? - account.silenced = true if domain_block && domain_block.silence? - account.private_key = nil - else - account = confirmed_account - end - - account.last_webfingered_at = Time.now.utc - - account.remote_url = data.link('http://schemas.google.com/g/2010#updates-from').href - account.salmon_url = data.link('salmon').href - account.url = data.link('http://webfinger.net/rel/profile-page').href - account.public_key = magic_key_to_pem(data.link('magic-public-key').href) - - body, xml = get_feed(account.remote_url) - hubs = get_hubs(xml) - - account.uri = get_account_uri(xml) - account.hub_url = hubs.first.attribute('href').value - - begin - account.save! - get_profile(body, account) if update_profile - rescue ActiveRecord::RecordNotUnique - # The account has been added by another worker! - return Account.find_remote(confirmed_username, confirmed_domain) - end - - account - end - - private - - def account_needs_webfinger_update?(account) - account&.last_webfingered_at.nil? || account.last_webfingered_at <= 1.day.ago - end - - def get_feed(url) - response = http_client(write: 20, connect: 20, read: 50).get(Addressable::URI.parse(url).normalize) - raise Goldfinger::Error, "Feed attempt failed for #{url}: HTTP #{response.code}" unless response.code == 200 - [response.to_s, Nokogiri::XML(response)] - end - - def get_hubs(xml) - hubs = xml.xpath('//xmlns:link[@rel="hub"]') - raise Goldfinger::Error, 'No PubSubHubbub hubs found' if hubs.empty? || hubs.first.attribute('href').nil? - hubs - end - - def get_account_uri(xml) - author_uri = xml.at_xpath('/xmlns:feed/xmlns:author/xmlns:uri') - - if author_uri.nil? - owner = xml.at_xpath('/xmlns:feed').at_xpath('./dfrn:owner', dfrn: DFRN_NS) - author_uri = owner.at_xpath('./xmlns:uri') unless owner.nil? - end - - raise Goldfinger::Error, 'Author URI could not be found' if author_uri.nil? - author_uri.content - end - - def get_profile(body, account) - RemoteProfileUpdateWorker.perform_async(account.id, body.force_encoding('UTF-8'), false) - end -end diff --git a/app/services/follow_service.rb b/app/services/follow_service.rb index 4de75e98d..e54ff7d0f 100644 --- a/app/services/follow_service.rb +++ b/app/services/follow_service.rb @@ -7,7 +7,7 @@ class FollowService < BaseService # @param [Account] source_account From which to follow # @param [String] uri User URI to follow in the form of username@domain def call(source_account, uri) - target_account = FollowRemoteAccountService.new.call(uri) + target_account = ResolveRemoteAccountService.new.call(uri) raise ActiveRecord::RecordNotFound if target_account.nil? || target_account.id == source_account.id || target_account.suspended? raise Mastodon::NotPermittedError if target_account.blocking?(source_account) || source_account.blocking?(target_account) diff --git a/app/services/process_mentions_service.rb b/app/services/process_mentions_service.rb index aa0a4d71b..438033d22 100644 --- a/app/services/process_mentions_service.rb +++ b/app/services/process_mentions_service.rb @@ -41,6 +41,6 @@ class ProcessMentionsService < BaseService private def follow_remote_account_service - @follow_remote_account_service ||= FollowRemoteAccountService.new + @follow_remote_account_service ||= ResolveRemoteAccountService.new end end diff --git a/app/services/resolve_remote_account_service.rb b/app/services/resolve_remote_account_service.rb new file mode 100644 index 000000000..362d0df98 --- /dev/null +++ b/app/services/resolve_remote_account_service.rb @@ -0,0 +1,108 @@ +# frozen_string_literal: true + +class ResolveRemoteAccountService < BaseService + include OStatus2::MagicKey + include HttpHelper + + DFRN_NS = 'http://purl.org/macgirvin/dfrn/1.0' + + # Find or create a local account for a remote user. + # When creating, look up the user's webfinger and fetch all + # important information from their feed + # @param [String] uri User URI in the form of username@domain + # @return [Account] + def call(uri, update_profile = true, redirected = nil) + username, domain = uri.split('@') + + return Account.find_local(username) if TagManager.instance.local_domain?(domain) + + account = Account.find_remote(username, domain) + return account unless account_needs_webfinger_update?(account) + + Rails.logger.debug "Looking up webfinger for #{uri}" + + data = Goldfinger.finger("acct:#{uri}") + + raise Goldfinger::Error, 'Missing resource links' if data.link('http://schemas.google.com/g/2010#updates-from').nil? || data.link('salmon').nil? || data.link('http://webfinger.net/rel/profile-page').nil? || data.link('magic-public-key').nil? + + # Disallow account hijacking + confirmed_username, confirmed_domain = data.subject.gsub(/\Aacct:/, '').split('@') + + unless confirmed_username.casecmp(username).zero? && confirmed_domain.casecmp(domain).zero? + return call("#{confirmed_username}@#{confirmed_domain}", update_profile, true) if redirected.nil? + raise Goldfinger::Error, 'Requested and returned acct URI do not match' + end + + return Account.find_local(confirmed_username) if TagManager.instance.local_domain?(confirmed_domain) + + confirmed_account = Account.find_remote(confirmed_username, confirmed_domain) + if confirmed_account.nil? + Rails.logger.debug "Creating new remote account for #{uri}" + + domain_block = DomainBlock.find_by(domain: domain) + account = Account.new(username: confirmed_username, domain: confirmed_domain) + account.suspended = true if domain_block && domain_block.suspend? + account.silenced = true if domain_block && domain_block.silence? + account.private_key = nil + else + account = confirmed_account + end + + account.last_webfingered_at = Time.now.utc + + account.remote_url = data.link('http://schemas.google.com/g/2010#updates-from').href + account.salmon_url = data.link('salmon').href + account.url = data.link('http://webfinger.net/rel/profile-page').href + account.public_key = magic_key_to_pem(data.link('magic-public-key').href) + + body, xml = get_feed(account.remote_url) + hubs = get_hubs(xml) + + account.uri = get_account_uri(xml) + account.hub_url = hubs.first.attribute('href').value + + begin + account.save! + get_profile(body, account) if update_profile + rescue ActiveRecord::RecordNotUnique + # The account has been added by another worker! + return Account.find_remote(confirmed_username, confirmed_domain) + end + + account + end + + private + + def account_needs_webfinger_update?(account) + account&.last_webfingered_at.nil? || account.last_webfingered_at <= 1.day.ago + end + + def get_feed(url) + response = http_client(write: 20, connect: 20, read: 50).get(Addressable::URI.parse(url).normalize) + raise Goldfinger::Error, "Feed attempt failed for #{url}: HTTP #{response.code}" unless response.code == 200 + [response.to_s, Nokogiri::XML(response)] + end + + def get_hubs(xml) + hubs = xml.xpath('//xmlns:link[@rel="hub"]') + raise Goldfinger::Error, 'No PubSubHubbub hubs found' if hubs.empty? || hubs.first.attribute('href').nil? + hubs + end + + def get_account_uri(xml) + author_uri = xml.at_xpath('/xmlns:feed/xmlns:author/xmlns:uri') + + if author_uri.nil? + owner = xml.at_xpath('/xmlns:feed').at_xpath('./dfrn:owner', dfrn: DFRN_NS) + author_uri = owner.at_xpath('./xmlns:uri') unless owner.nil? + end + + raise Goldfinger::Error, 'Author URI could not be found' if author_uri.nil? + author_uri.content + end + + def get_profile(body, account) + RemoteProfileUpdateWorker.perform_async(account.id, body.force_encoding('UTF-8'), false) + end +end diff --git a/app/views/activitypub/types/collection.activitystreams2.rabl b/app/views/activitypub/types/collection.activitystreams2.rabl index d3f8ddcac..cc0e532b7 100644 --- a/app/views/activitypub/types/collection.activitystreams2.rabl +++ b/app/views/activitypub/types/collection.activitystreams2.rabl @@ -1,3 +1,3 @@ extends 'activitypub/intransient.activitystreams2.rabl' -node(:type) { 'Collection' } +node(:type) { 'Collection' } diff --git a/app/views/activitypub/types/ordered_collection_page.activitystreams2.rabl b/app/views/activitypub/types/ordered_collection_page.activitystreams2.rabl index c821fa928..9937d11e9 100644 --- a/app/views/activitypub/types/ordered_collection_page.activitystreams2.rabl +++ b/app/views/activitypub/types/ordered_collection_page.activitystreams2.rabl @@ -1,3 +1,3 @@ extends 'activitypub/types/ordered_collection.activitystreams2.rabl' -node(:type) { 'OrderedCollectionPage' } +node(:type) { 'OrderedCollectionPage' } diff --git a/app/workers/import_worker.rb b/app/workers/import_worker.rb index e93fa33cf..90a226206 100644 --- a/app/workers/import_worker.rb +++ b/app/workers/import_worker.rb @@ -41,7 +41,7 @@ class ImportWorker def process_mutes import_rows.each do |row| begin - target_account = FollowRemoteAccountService.new.call(row.first) + target_account = ResolveRemoteAccountService.new.call(row.first) next if target_account.nil? MuteService.new.call(from_account, target_account) rescue Goldfinger::Error, HTTP::Error, OpenSSL::SSL::SSLError @@ -53,7 +53,7 @@ class ImportWorker def process_blocks import_rows.each do |row| begin - target_account = FollowRemoteAccountService.new.call(row.first) + target_account = ResolveRemoteAccountService.new.call(row.first) next if target_account.nil? BlockService.new.call(from_account, target_account) rescue Goldfinger::Error, HTTP::Error, OpenSSL::SSL::SSLError -- cgit