The code powering m.abunchtell.com https://m.abunchtell.com
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

164 linhas
4.6 KiB

  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 webfinger_update_due?
  15. Rails.logger.debug "Looking up webfinger for #{uri}"
  16. @webfinger = Goldfinger.finger("acct:#{uri}")
  17. raise Goldfinger::Error, 'Missing resource links' if links_missing?
  18. confirmed_username, confirmed_domain = @webfinger.subject.gsub(/\Aacct:/, '').split('@')
  19. if confirmed_username.casecmp(@username).zero? && confirmed_domain.casecmp(@domain).zero?
  20. @username = confirmed_username
  21. @domain = confirmed_domain
  22. else
  23. return call("#{confirmed_username}@#{confirmed_domain}", update_profile, true) if redirected.nil?
  24. raise Goldfinger::Error, 'Requested and returned acct URIs do not match'
  25. end
  26. return Account.find_local(@username) if TagManager.instance.local_domain?(@domain)
  27. RedisLock.acquire(lock_options) do |lock|
  28. if lock.acquired?
  29. @account = Account.find_remote(@username, @domain)
  30. create_account if @account.nil?
  31. update_account
  32. update_account_profile if update_profile
  33. end
  34. end
  35. @account
  36. end
  37. private
  38. def links_missing?
  39. @webfinger.link('http://schemas.google.com/g/2010#updates-from').nil? ||
  40. @webfinger.link('salmon').nil? ||
  41. @webfinger.link('http://webfinger.net/rel/profile-page').nil? ||
  42. @webfinger.link('magic-public-key').nil?
  43. end
  44. def webfinger_update_due?
  45. @account.nil? || @account.last_webfingered_at.nil? || @account.last_webfingered_at <= 1.day.ago
  46. end
  47. def create_account
  48. Rails.logger.debug "Creating new remote account for #{@username}@#{@domain}"
  49. @account = Account.new(username: @username, domain: @domain)
  50. @account.suspended = true if auto_suspend?
  51. @account.silenced = true if auto_silence?
  52. @account.private_key = nil
  53. end
  54. def update_account
  55. @account.last_webfingered_at = Time.now.utc
  56. @account.remote_url = atom_url
  57. @account.salmon_url = salmon_url
  58. @account.url = url
  59. @account.public_key = public_key
  60. @account.uri = canonical_uri
  61. @account.hub_url = hub_url
  62. @account.save!
  63. end
  64. def auto_suspend?
  65. domain_block && domain_block.suspend?
  66. end
  67. def auto_silence?
  68. domain_block && domain_block.silence?
  69. end
  70. def domain_block
  71. return @domain_block if defined?(@domain_block)
  72. @domain_block = DomainBlock.find_by(domain: @domain)
  73. end
  74. def atom_url
  75. @atom_url ||= @webfinger.link('http://schemas.google.com/g/2010#updates-from').href
  76. end
  77. def salmon_url
  78. @salmon_url ||= @webfinger.link('salmon').href
  79. end
  80. def url
  81. @url ||= @webfinger.link('http://webfinger.net/rel/profile-page').href
  82. end
  83. def public_key
  84. @public_key ||= magic_key_to_pem(@webfinger.link('magic-public-key').href)
  85. end
  86. def canonical_uri
  87. return @canonical_uri if defined?(@canonical_uri)
  88. author_uri = atom.at_xpath('/xmlns:feed/xmlns:author/xmlns:uri')
  89. if author_uri.nil?
  90. owner = atom.at_xpath('/xmlns:feed').at_xpath('./dfrn:owner', dfrn: DFRN_NS)
  91. author_uri = owner.at_xpath('./xmlns:uri') unless owner.nil?
  92. end
  93. raise Goldfinger::Error, 'Author URI could not be found' if author_uri.nil?
  94. @canonical_uri = author_uri.content
  95. end
  96. def hub_url
  97. return @hub_url if defined?(@hub_url)
  98. hubs = atom.xpath('//xmlns:link[@rel="hub"]')
  99. raise Goldfinger::Error, 'No PubSubHubbub hubs found' if hubs.empty? || hubs.first['href'].nil?
  100. @hub_url = hubs.first['href']
  101. end
  102. def atom_body
  103. return @atom_body if defined?(@atom_body)
  104. response = Request.new(:get, atom_url).perform
  105. raise Goldfinger::Error, "Feed attempt failed for #{atom_url}: HTTP #{response.code}" unless response.code == 200
  106. @atom_body = response.to_s
  107. end
  108. def atom
  109. return @atom if defined?(@atom)
  110. @atom = Nokogiri::XML(atom_body)
  111. end
  112. def update_account_profile
  113. RemoteProfileUpdateWorker.perform_async(@account.id, atom_body.force_encoding('UTF-8'), false)
  114. end
  115. def lock_options
  116. { redis: Redis.current, key: "resolve:#{@username}@#{@domain}" }
  117. end
  118. end