resolve_account_service.rb 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # frozen_string_literal: true
  2. class ResolveAccountService < BaseService
  3. include JsonLdHelper
  4. include DomainControlHelper
  5. class WebfingerRedirectError < StandardError; end
  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 to refresh account data
  12. # @return [Account]
  13. def call(uri, options = {})
  14. return if uri.blank?
  15. process_options!(uri, options)
  16. # First of all we want to check if we've got the account
  17. # record with the URI already, and if so, we can exit early
  18. return if domain_not_allowed?(@domain)
  19. @account ||= Account.find_remote(@username, @domain)
  20. return @account if @account&.local? || !webfinger_update_due?
  21. # At this point we are in need of a Webfinger query, which may
  22. # yield us a different username/domain through a redirect
  23. process_webfinger!(@uri)
  24. # Because the username/domain pair may be different than what
  25. # we already checked, we need to check if we've already got
  26. # the record with that URI, again
  27. return if domain_not_allowed?(@domain)
  28. @account ||= Account.find_remote(@username, @domain)
  29. return @account if @account&.local? || !webfinger_update_due?
  30. # Now it is certain, it is definitely a remote account, and it
  31. # either needs to be created, or updated from fresh data
  32. process_account!
  33. rescue Goldfinger::Error, WebfingerRedirectError, Oj::ParseError => e
  34. Rails.logger.debug "Webfinger query for #{@uri} failed: #{e}"
  35. nil
  36. end
  37. private
  38. def process_options!(uri, options)
  39. @options = options
  40. if uri.is_a?(Account)
  41. @account = uri
  42. @username = @account.username
  43. @domain = @account.domain
  44. @uri = [@username, @domain].compact.join('@')
  45. else
  46. @uri = uri
  47. @username, @domain = uri.split('@')
  48. end
  49. @domain = nil if TagManager.instance.local_domain?(@domain)
  50. end
  51. def process_webfinger!(uri, redirected = false)
  52. @webfinger = Goldfinger.finger("acct:#{@uri}")
  53. confirmed_username, confirmed_domain = @webfinger.subject.gsub(/\Aacct:/, '').split('@')
  54. if confirmed_username.casecmp(@username).zero? && confirmed_domain.casecmp(@domain).zero?
  55. @username = confirmed_username
  56. @domain = confirmed_domain
  57. @uri = uri
  58. elsif !redirected
  59. return process_webfinger!("#{confirmed_username}@#{confirmed_domain}", true)
  60. else
  61. raise WebfingerRedirectError, "The URI #{uri} tries to hijack #{@username}@#{@domain}"
  62. end
  63. @domain = nil if TagManager.instance.local_domain?(@domain)
  64. end
  65. def process_account!
  66. return unless activitypub_ready?
  67. RedisLock.acquire(lock_options) do |lock|
  68. if lock.acquired?
  69. @account = Account.find_remote(@username, @domain)
  70. next if (@account.present? && !@account.activitypub?) || actor_json.nil?
  71. @account = ActivityPub::ProcessAccountService.new.call(@username, @domain, actor_json)
  72. else
  73. raise Mastodon::RaceConditionError
  74. end
  75. end
  76. @account
  77. end
  78. def webfinger_update_due?
  79. @account.nil? || ((!@options[:skip_webfinger] || @account.ostatus?) && @account.possibly_stale?)
  80. end
  81. def activitypub_ready?
  82. !@webfinger.link('self').nil? && ['application/activity+json', 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'].include?(@webfinger.link('self').type)
  83. end
  84. def actor_url
  85. @actor_url ||= @webfinger.link('self').href
  86. end
  87. def actor_json
  88. return @actor_json if defined?(@actor_json)
  89. json = fetch_resource(actor_url, false)
  90. @actor_json = supported_context?(json) && equals_or_includes_any?(json['type'], ActivityPub::FetchRemoteAccountService::SUPPORTED_TYPES) ? json : nil
  91. end
  92. def lock_options
  93. { redis: Redis.current, key: "resolve:#{@username}@#{@domain}" }
  94. end
  95. end