resolve_account_service.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 = if TagManager.instance.local_domain?(@domain)
  56. nil
  57. else
  58. TagManager.instance.normalize_domain(@domain)
  59. end
  60. @uri = [@username, @domain].compact.join('@')
  61. end
  62. def process_webfinger!(uri)
  63. @webfinger = webfinger!("acct:#{uri}")
  64. confirmed_username, confirmed_domain = split_acct(@webfinger.subject)
  65. if confirmed_username.casecmp(@username).zero? && confirmed_domain.casecmp(@domain).zero?
  66. @username = confirmed_username
  67. @domain = confirmed_domain
  68. return
  69. end
  70. # Account doesn't match, so it may have been redirected
  71. @webfinger = webfinger!("acct:#{confirmed_username}@#{confirmed_domain}")
  72. @username, @domain = split_acct(@webfinger.subject)
  73. 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?
  74. rescue Webfinger::GoneError
  75. @gone = true
  76. end
  77. def split_acct(acct)
  78. acct.delete_prefix('acct:').split('@')
  79. end
  80. def fetch_account!
  81. return unless activitypub_ready?
  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 activitypub_ready?
  93. ['application/activity+json', 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'].include?(@webfinger.link('self', 'type'))
  94. end
  95. def actor_url
  96. @actor_url ||= @webfinger.link('self', 'href')
  97. end
  98. def gone_from_origin?
  99. @gone
  100. end
  101. def not_yet_deleted?
  102. @account.present? && !@account.local?
  103. end
  104. def queue_deletion!
  105. @account.suspend!(origin: :remote)
  106. AccountDeletionWorker.perform_async(@account.id, { 'reserve_username' => false, 'skip_activitypub' => true })
  107. end
  108. end