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.

97 lines
2.8 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. class FollowRemoteAccountService < BaseService
  2. # Find or create a local account for a remote user.
  3. # When creating, look up the user's webfinger and fetch all
  4. # important information from their feed
  5. # @param [String] uri User URI in the form of username@domain
  6. # @param [Boolean] subscribe Whether to initiate a PubSubHubbub subscription
  7. # @return [Account]
  8. def call(uri, subscribe = true)
  9. username, domain = uri.split('@')
  10. return Account.find_local(username) if domain == Rails.configuration.x.local_domain || domain.nil?
  11. account = Account.find_remote(username, domain)
  12. if account.nil?
  13. Rails.logger.debug "Creating new remote account for #{uri}"
  14. account = Account.new(username: username, domain: domain)
  15. elsif account.subscribed?
  16. Rails.logger.debug "Already subscribed to remote account #{uri}"
  17. return account
  18. end
  19. data = Goldfinger.finger("acct:#{uri}")
  20. account.remote_url = data.link('http://schemas.google.com/g/2010#updates-from').href
  21. account.salmon_url = data.link('salmon').href
  22. account.url = data.link('http://webfinger.net/rel/profile-page').href
  23. account.public_key = magic_key_to_pem(data.link('magic-public-key').href)
  24. account.private_key = nil
  25. feed = get_feed(account.remote_url)
  26. hubs = feed.xpath('//xmlns:link[@rel="hub"]')
  27. if hubs.empty? || hubs.first.attribute('href').nil?
  28. raise Goldfinger::Error, "No PubSubHubbub hubs found"
  29. end
  30. if feed.at_xpath('/xmlns:feed/xmlns:author/xmlns:uri').nil?
  31. raise Goldfinger::Error, "No author URI found"
  32. end
  33. account.uri = feed.at_xpath('/xmlns:feed/xmlns:author/xmlns:uri').content
  34. account.hub_url = hubs.first.attribute('href').value
  35. get_profile(feed, account)
  36. account.save!
  37. if subscribe
  38. account.secret = SecureRandom.hex
  39. account.verify_token = SecureRandom.hex
  40. subscription = account.subscription(api_subscription_url(account.id))
  41. subscription.subscribe
  42. account.save!
  43. end
  44. return account
  45. end
  46. private
  47. def get_feed(url)
  48. response = http_client.get(Addressable::URI.parse(url))
  49. Nokogiri::XML(response)
  50. end
  51. def get_profile(xml, account)
  52. author = xml.at_xpath('/xmlns:feed/xmlns:author')
  53. update_remote_profile_service.(author, account)
  54. end
  55. def magic_key_to_pem(magic_key)
  56. _, modulus, exponent = magic_key.split('.')
  57. modulus, exponent = [modulus, exponent].map { |n| Base64.urlsafe_decode64(n).bytes.inject(0) { |num, byte| (num << 8) | byte } }
  58. key = OpenSSL::PKey::RSA.new
  59. key.n = modulus
  60. key.e = exponent
  61. key.to_pem
  62. end
  63. def update_remote_profile_service
  64. @update_remote_profile_service ||= UpdateRemoteProfileService.new
  65. end
  66. def http_client
  67. HTTP
  68. end
  69. end
  70. class NoAuthorFeedError < StandardError
  71. end
  72. class NoHubError < StandardError
  73. end