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.

107 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 ResolveRemoteAccountService < 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, update_profile = true, 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}", update_profile, 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. begin
  46. account.save!
  47. get_profile(body, account) if update_profile
  48. rescue ActiveRecord::RecordNotUnique
  49. # The account has been added by another worker!
  50. return Account.find_remote(confirmed_username, confirmed_domain)
  51. end
  52. account
  53. end
  54. private
  55. def account_needs_webfinger_update?(account)
  56. account&.last_webfingered_at.nil? || account.last_webfingered_at <= 1.day.ago
  57. end
  58. def get_feed(url)
  59. response = Request.new(:get, url).perform
  60. raise Goldfinger::Error, "Feed attempt failed for #{url}: HTTP #{response.code}" unless response.code == 200
  61. [response.to_s, Nokogiri::XML(response)]
  62. end
  63. def get_hubs(xml)
  64. hubs = xml.xpath('//xmlns:link[@rel="hub"]')
  65. raise Goldfinger::Error, 'No PubSubHubbub hubs found' if hubs.empty? || hubs.first.attribute('href').nil?
  66. hubs
  67. end
  68. def get_account_uri(xml)
  69. author_uri = xml.at_xpath('/xmlns:feed/xmlns:author/xmlns:uri')
  70. if author_uri.nil?
  71. owner = xml.at_xpath('/xmlns:feed').at_xpath('./dfrn:owner', dfrn: DFRN_NS)
  72. author_uri = owner.at_xpath('./xmlns:uri') unless owner.nil?
  73. end
  74. raise Goldfinger::Error, 'Author URI could not be found' if author_uri.nil?
  75. author_uri.content
  76. end
  77. def get_profile(body, account)
  78. RemoteProfileUpdateWorker.perform_async(account.id, body.force_encoding('UTF-8'), false)
  79. end
  80. end