闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.9 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. # frozen_string_literal: true
  2. class FollowRemoteAccountService < BaseService
  3. include OStatus2::MagicKey
  4. include HttpHelper
  5. DFRN_NS = 'http://purl.org/macgirvin/dfrn/1.0'
  6. # Find or create a local account for a remote user.
  7. # When creating, look up the user's webfinger and fetch all
  8. # important information from their feed
  9. # @param [String] uri User URI in the form of username@domain
  10. # @return [Account]
  11. def call(uri, redirected = nil)
  12. username, domain = uri.split('@')
  13. return Account.find_local(username) if TagManager.instance.local_domain?(domain)
  14. account = Account.find_remote(username, domain)
  15. return account unless account_needs_webfinger_update?(account)
  16. Rails.logger.debug "Looking up webfinger for #{uri}"
  17. data = Goldfinger.finger("acct:#{uri}")
  18. 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?
  19. # Disallow account hijacking
  20. confirmed_username, confirmed_domain = data.subject.gsub(/\Aacct:/, '').split('@')
  21. unless confirmed_username.casecmp(username).zero? && confirmed_domain.casecmp(domain).zero?
  22. return call("#{confirmed_username}@#{confirmed_domain}", true) if redirected.nil?
  23. raise Goldfinger::Error, 'Requested and returned acct URI do not match'
  24. end
  25. return Account.find_local(confirmed_username) if TagManager.instance.local_domain?(confirmed_domain)
  26. confirmed_account = Account.find_remote(confirmed_username, confirmed_domain)
  27. if confirmed_account.nil?
  28. Rails.logger.debug "Creating new remote account for #{uri}"
  29. domain_block = DomainBlock.find_by(domain: domain)
  30. account = Account.new(username: confirmed_username, domain: confirmed_domain)
  31. account.suspended = true if domain_block && domain_block.suspend?
  32. account.silenced = true if domain_block && domain_block.silence?
  33. account.private_key = nil
  34. else
  35. account = confirmed_account
  36. end
  37. account.last_webfingered_at = Time.now.utc
  38. account.remote_url = data.link('http://schemas.google.com/g/2010#updates-from').href
  39. account.salmon_url = data.link('salmon').href
  40. account.url = data.link('http://webfinger.net/rel/profile-page').href
  41. account.public_key = magic_key_to_pem(data.link('magic-public-key').href)
  42. body, xml = get_feed(account.remote_url)
  43. hubs = get_hubs(xml)
  44. account.uri = get_account_uri(xml)
  45. account.hub_url = hubs.first.attribute('href').value
  46. begin
  47. account.save!
  48. get_profile(body, account)
  49. rescue ActiveRecord::RecordNotUnique
  50. # The account has been added by another worker!
  51. return Account.find_remote(confirmed_username, confirmed_domain)
  52. end
  53. account
  54. end
  55. private
  56. def account_needs_webfinger_update?(account)
  57. account&.last_webfingered_at.nil? || account.last_webfingered_at <= 1.day.ago
  58. end
  59. def get_feed(url)
  60. response = http_client(write: 20, connect: 20, read: 50).get(Addressable::URI.parse(url).normalize)
  61. raise Goldfinger::Error, "Feed attempt failed for #{url}: HTTP #{response.code}" unless response.code == 200
  62. [response.to_s, Nokogiri::XML(response)]
  63. end
  64. def get_hubs(xml)
  65. hubs = xml.xpath('//xmlns:link[@rel="hub"]')
  66. raise Goldfinger::Error, 'No PubSubHubbub hubs found' if hubs.empty? || hubs.first.attribute('href').nil?
  67. hubs
  68. end
  69. def get_account_uri(xml)
  70. author_uri = xml.at_xpath('/xmlns:feed/xmlns:author/xmlns:uri')
  71. if author_uri.nil?
  72. owner = xml.at_xpath('/xmlns:feed').at_xpath('./dfrn:owner', dfrn: DFRN_NS)
  73. author_uri = owner.at_xpath('./xmlns:uri') unless owner.nil?
  74. end
  75. raise Goldfinger::Error, 'Author URI could not be found' if author_uri.nil?
  76. author_uri.content
  77. end
  78. def get_profile(body, account)
  79. RemoteProfileUpdateWorker.perform_async(account.id, body.force_encoding('UTF-8'), false)
  80. end
  81. end