resolve_account_service.rb 4.6 KB

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