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.

87 lines
2.6 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. account = Account.where(username: username, domain: domain).first
  11. if account.nil?
  12. account = Account.new(username: username, domain: domain)
  13. elsif account.subscribed?
  14. return account
  15. end
  16. data = Goldfinger.finger("acct:#{uri}")
  17. account.remote_url = data.link('http://schemas.google.com/g/2010#updates-from').href
  18. account.salmon_url = data.link('salmon').href
  19. account.url = data.link('http://webfinger.net/rel/profile-page').href
  20. account.public_key = magic_key_to_pem(data.link('magic-public-key').href)
  21. account.private_key = nil
  22. feed = get_feed(account.remote_url)
  23. hubs = feed.xpath('//xmlns:link[@rel="hub"]')
  24. return nil if hubs.empty? || hubs.first.attribute('href').nil? || feed.at_xpath('/xmlns:feed/xmlns:author/xmlns:uri').nil?
  25. account.uri = feed.at_xpath('/xmlns:feed/xmlns:author/xmlns:uri').content
  26. account.hub_url = hubs.first.attribute('href').value
  27. get_profile(feed, account)
  28. account.save!
  29. if subscribe
  30. account.secret = SecureRandom.hex
  31. account.verify_token = SecureRandom.hex
  32. subscription = account.subscription(subscription_url(account))
  33. subscription.subscribe
  34. account.save!
  35. end
  36. return account
  37. rescue Goldfinger::Error, HTTP::Error => e
  38. nil
  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_profile(xml, account)
  46. author = xml.at_xpath('/xmlns:feed/xmlns:author')
  47. if author.at_xpath('./poco:displayName').nil?
  48. account.display_name = account.username
  49. else
  50. account.display_name = author.at_xpath('./poco:displayName').content
  51. end
  52. unless author.at_xpath('./poco:note').nil?
  53. account.note = author.at_xpath('./poco:note').content
  54. end
  55. end
  56. def magic_key_to_pem(magic_key)
  57. _, modulus, exponent = magic_key.split('.')
  58. modulus, exponent = [modulus, exponent].map { |n| Base64.urlsafe_decode64(n).bytes.inject(0) { |num, byte| (num << 8) | byte } }
  59. key = OpenSSL::PKey::RSA.new
  60. key.n = modulus
  61. key.e = exponent
  62. key.to_pem
  63. end
  64. def http_client
  65. HTTP
  66. end
  67. end