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.

92 lines
3.1 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. class FollowRemoteAccountService < BaseService
  2. include OStatus2::MagicKey
  3. DFRN_NS = 'http://purl.org/macgirvin/dfrn/1.0'.freeze
  4. # Find or create a local account for a remote user.
  5. # When creating, look up the user's webfinger and fetch all
  6. # important information from their feed
  7. # @param [String] uri User URI in the form of username@domain
  8. # @return [Account]
  9. def call(uri)
  10. username, domain = uri.split('@')
  11. return Account.find_local(username) if TagManager.instance.local_domain?(domain)
  12. return nil if DomainBlock.blocked?(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. account.remote_url = data.link('http://schemas.google.com/g/2010#updates-from').href
  25. account.salmon_url = data.link('salmon').href
  26. account.url = data.link('http://webfinger.net/rel/profile-page').href
  27. account.public_key = magic_key_to_pem(data.link('magic-public-key').href)
  28. account.private_key = nil
  29. xml = get_feed(account.remote_url)
  30. hubs = get_hubs(xml)
  31. account.uri = get_account_uri(xml)
  32. account.hub_url = hubs.first.attribute('href').value
  33. get_profile(xml, account)
  34. account.save!
  35. return account
  36. end
  37. private
  38. def get_feed(url)
  39. response = http_client.get(Addressable::URI.parse(url))
  40. Nokogiri::XML(response)
  41. end
  42. def get_hubs(xml)
  43. hubs = xml.xpath('//xmlns:link[@rel="hub"]')
  44. raise Goldfinger::Error, 'No PubSubHubbub hubs found' if hubs.empty? || hubs.first.attribute('href').nil?
  45. hubs
  46. end
  47. def get_account_uri(xml)
  48. author_uri = xml.at_xpath('/xmlns:feed/xmlns:author/xmlns:uri')
  49. if author_uri.nil?
  50. owner = xml.at_xpath('/xmlns:feed').at_xpath('./dfrn:owner', dfrn: DFRN_NS)
  51. author_uri = owner.at_xpath('./xmlns:uri') unless owner.nil?
  52. end
  53. raise Goldfinger::Error, 'Author URI could not be found' if author_uri.nil?
  54. author_uri.content
  55. end
  56. def get_profile(xml, account)
  57. author = xml.at_xpath('/xmlns:feed/xmlns:author') || xml.at_xpath('/xmlns:feed').at_xpath('./dfrn:owner', dfrn: DFRN_NS)
  58. update_remote_profile_service.call(author, account)
  59. end
  60. def update_remote_profile_service
  61. @update_remote_profile_service ||= UpdateRemoteProfileService.new
  62. end
  63. def http_client
  64. HTTP.timeout(:per_operation, write: 20, connect: 20, read: 50)
  65. end
  66. end