resolve_account_service.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # frozen_string_literal: true
  2. class ResolveAccountService < BaseService
  3. include DomainControlHelper
  4. include Redisable
  5. include Lockable
  6. # Find or create an account record for a remote user. When creating,
  7. # look up the user's webfinger and fetch ActivityPub data
  8. # @param [String, Account] uri URI in the username@domain format or account record
  9. # @param [Hash] options
  10. # @option options [Boolean] :redirected Do not follow further Webfinger redirects
  11. # @option options [Boolean] :skip_webfinger Do not attempt any webfinger query or refreshing account data
  12. # @option options [Boolean] :skip_cache Get the latest data from origin even if cache is not due to update yet
  13. # @option options [Boolean] :suppress_errors When failing, return nil instead of raising an error
  14. # @return [Account]
  15. def call(uri, options = {})
  16. return if uri.blank?
  17. process_options!(uri, options)
  18. # First of all we want to check if we've got the account
  19. # record with the URI already, and if so, we can exit early
  20. return if domain_not_allowed?(@domain)
  21. @account ||= Account.find_remote(@username, @domain)
  22. return @account if @account&.local? || @domain.nil? || !webfinger_update_due?
  23. # At this point we are in need of a Webfinger query, which may
  24. # yield us a different username/domain through a redirect
  25. process_webfinger!(@uri)
  26. @domain = nil if TagManager.instance.local_domain?(@domain)
  27. # Because the username/domain pair may be different than what
  28. # we already checked, we need to check if we've already got
  29. # the record with that URI, again
  30. return if domain_not_allowed?(@domain)
  31. @account ||= Account.find_remote(@username, @domain)
  32. if gone_from_origin? && not_yet_deleted?
  33. queue_deletion!
  34. return
  35. end
  36. return @account if @account&.local? || gone_from_origin? || !webfinger_update_due?
  37. # Now it is certain, it is definitely a remote account, and it
  38. # either needs to be created, or updated from fresh data
  39. fetch_account!
  40. rescue Webfinger::Error => e
  41. Rails.logger.debug { "Webfinger query for #{@uri} failed: #{e}" }
  42. raise unless @options[:suppress_errors]
  43. end
  44. private
  45. def process_options!(uri, options)
  46. @options = { suppress_errors: true }.merge(options)
  47. if uri.is_a?(Account)
  48. @account = uri
  49. @username = @account.username
  50. @domain = @account.domain
  51. else
  52. @username, @domain = uri.strip.gsub(/\A@/, '').split('@')
  53. end
  54. @domain = if TagManager.instance.local_domain?(@domain)
  55. nil
  56. else
  57. TagManager.instance.normalize_domain(@domain)
  58. end
  59. @uri = [@username, @domain].compact.join('@')
  60. end
  61. def process_webfinger!(uri)
  62. @webfinger = Webfinger.new("acct:#{uri}").perform
  63. confirmed_username, confirmed_domain = split_acct(@webfinger.subject)
  64. if confirmed_username.casecmp(@username).zero? && confirmed_domain.casecmp(@domain).zero?
  65. @username = confirmed_username
  66. @domain = confirmed_domain
  67. return
  68. end
  69. # Account doesn't match, so it may have been redirected
  70. @webfinger = Webfinger.new("acct:#{confirmed_username}@#{confirmed_domain}").perform
  71. @username, @domain = split_acct(@webfinger.subject)
  72. raise Webfinger::RedirectError, "Too many webfinger redirects for URI #{uri} (stopped at #{@username}@#{@domain})" unless confirmed_username.casecmp(@username).zero? && confirmed_domain.casecmp(@domain).zero?
  73. rescue Webfinger::GoneError
  74. @gone = true
  75. end
  76. def split_acct(acct)
  77. acct.delete_prefix('acct:').split('@').tap do |parts|
  78. raise Webfinger::Error, 'Webfinger response is missing user or host value' unless parts.size == 2
  79. end
  80. end
  81. def fetch_account!
  82. with_redis_lock("resolve:#{@username}@#{@domain}") do
  83. @account = ActivityPub::FetchRemoteAccountService.new.call(actor_url, suppress_errors: @options[:suppress_errors])
  84. end
  85. @account
  86. end
  87. def webfinger_update_due?
  88. return false if @options[:check_delivery_availability] && !DeliveryFailureTracker.available?(@domain)
  89. return false if @options[:skip_webfinger]
  90. @options[:skip_cache] || @account.nil? || @account.possibly_stale?
  91. end
  92. def actor_url
  93. @actor_url ||= @webfinger.self_link_href
  94. end
  95. def gone_from_origin?
  96. @gone
  97. end
  98. def not_yet_deleted?
  99. @account.present? && !@account.local?
  100. end
  101. def queue_deletion!
  102. @account.suspend!(origin: :remote)
  103. AccountDeletionWorker.perform_async(@account.id, { 'reserve_username' => false, 'skip_activitypub' => true })
  104. end
  105. end