闭社主体 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.

102 lines
3.7 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
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. account.save!
  47. get_profile(body, account)
  48. account
  49. end
  50. private
  51. def account_needs_webfinger_update?(account)
  52. account&.last_webfingered_at.nil? || account.last_webfingered_at <= 1.day.ago
  53. end
  54. def get_feed(url)
  55. response = http_client(write: 20, connect: 20, read: 50).get(Addressable::URI.parse(url).normalize)
  56. [response.to_s, Nokogiri::XML(response)]
  57. end
  58. def get_hubs(xml)
  59. hubs = xml.xpath('//xmlns:link[@rel="hub"]')
  60. raise Goldfinger::Error, 'No PubSubHubbub hubs found' if hubs.empty? || hubs.first.attribute('href').nil?
  61. hubs
  62. end
  63. def get_account_uri(xml)
  64. author_uri = xml.at_xpath('/xmlns:feed/xmlns:author/xmlns:uri')
  65. if author_uri.nil?
  66. owner = xml.at_xpath('/xmlns:feed').at_xpath('./dfrn:owner', dfrn: DFRN_NS)
  67. author_uri = owner.at_xpath('./xmlns:uri') unless owner.nil?
  68. end
  69. raise Goldfinger::Error, 'Author URI could not be found' if author_uri.nil?
  70. author_uri.content
  71. end
  72. def get_profile(body, account)
  73. RemoteProfileUpdateWorker.perform_async(account.id, body.force_encoding('UTF-8'), false)
  74. end
  75. end