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

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