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.

96 lines
3.2 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. 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)
  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.nil?
  15. Rails.logger.debug "Looking up webfinger for #{uri}"
  16. account = Account.new(username: username, domain: domain)
  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. confirmed_username, confirmed_domain = data.subject.gsub(/\Aacct:/, '').split('@')
  20. return Account.find_local(confirmed_username) if TagManager.instance.local_domain?(confirmed_domain)
  21. confirmed_account = Account.find_remote(confirmed_username, confirmed_domain)
  22. return confirmed_account unless confirmed_account.nil?
  23. Rails.logger.debug "Creating new remote account for #{uri}"
  24. domain_block = DomainBlock.find_by(domain: domain)
  25. account.remote_url = data.link('http://schemas.google.com/g/2010#updates-from').href
  26. account.salmon_url = data.link('salmon').href
  27. account.url = data.link('http://webfinger.net/rel/profile-page').href
  28. account.public_key = magic_key_to_pem(data.link('magic-public-key').href)
  29. account.private_key = nil
  30. account.suspended = true if domain_block && domain_block.suspend?
  31. account.silenced = true if domain_block && domain_block.silence?
  32. xml = get_feed(account.remote_url)
  33. hubs = get_hubs(xml)
  34. account.uri = get_account_uri(xml)
  35. account.hub_url = hubs.first.attribute('href').value
  36. get_profile(xml, account)
  37. account.save!
  38. account
  39. end
  40. private
  41. def get_feed(url)
  42. response = http_client.get(Addressable::URI.parse(url))
  43. Nokogiri::XML(response)
  44. end
  45. def get_hubs(xml)
  46. hubs = xml.xpath('//xmlns:link[@rel="hub"]')
  47. raise Goldfinger::Error, 'No PubSubHubbub hubs found' if hubs.empty? || hubs.first.attribute('href').nil?
  48. hubs
  49. end
  50. def get_account_uri(xml)
  51. author_uri = xml.at_xpath('/xmlns:feed/xmlns:author/xmlns:uri')
  52. if author_uri.nil?
  53. owner = xml.at_xpath('/xmlns:feed').at_xpath('./dfrn:owner', dfrn: DFRN_NS)
  54. author_uri = owner.at_xpath('./xmlns:uri') unless owner.nil?
  55. end
  56. raise Goldfinger::Error, 'Author URI could not be found' if author_uri.nil?
  57. author_uri.content
  58. end
  59. def get_profile(xml, account)
  60. update_remote_profile_service.call(xml.at_xpath('/xmlns:feed'), account)
  61. end
  62. def update_remote_profile_service
  63. @update_remote_profile_service ||= UpdateRemoteProfileService.new
  64. end
  65. def http_client
  66. HTTP.timeout(:per_operation, write: 20, connect: 20, read: 50)
  67. end
  68. end