The code powering m.abunchtell.com https://m.abunchtell.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

136 lines
4.1 KiB

  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. else
  45. @username, @domain = uri.split('@')
  46. end
  47. @domain = begin
  48. if TagManager.instance.local_domain?(@domain)
  49. nil
  50. else
  51. TagManager.instance.normalize_domain(@domain)
  52. end
  53. end
  54. @uri = [@username, @domain].compact.join('@')
  55. end
  56. def process_webfinger!(uri, redirected = false)
  57. @webfinger = Goldfinger.finger("acct:#{uri}")
  58. confirmed_username, confirmed_domain = @webfinger.subject.gsub(/\Aacct:/, '').split('@')
  59. if confirmed_username.casecmp(@username).zero? && confirmed_domain.casecmp(@domain).zero?
  60. @username = confirmed_username
  61. @domain = confirmed_domain
  62. @uri = uri
  63. elsif !redirected
  64. return process_webfinger!("#{confirmed_username}@#{confirmed_domain}", true)
  65. else
  66. raise WebfingerRedirectError, "The URI #{uri} tries to hijack #{@username}@#{@domain}"
  67. end
  68. @domain = nil if TagManager.instance.local_domain?(@domain)
  69. end
  70. def process_account!
  71. return unless activitypub_ready?
  72. RedisLock.acquire(lock_options) do |lock|
  73. if lock.acquired?
  74. @account = Account.find_remote(@username, @domain)
  75. next if actor_json.nil?
  76. @account = ActivityPub::ProcessAccountService.new.call(@username, @domain, actor_json)
  77. else
  78. raise Mastodon::RaceConditionError
  79. end
  80. end
  81. @account
  82. end
  83. def webfinger_update_due?
  84. @account.nil? || ((!@options[:skip_webfinger] || @account.ostatus?) && @account.possibly_stale?)
  85. end
  86. def activitypub_ready?
  87. !@webfinger.link('self').nil? && ['application/activity+json', 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'].include?(@webfinger.link('self').type)
  88. end
  89. def actor_url
  90. @actor_url ||= @webfinger.link('self').href
  91. end
  92. def actor_json
  93. return @actor_json if defined?(@actor_json)
  94. json = fetch_resource(actor_url, false)
  95. @actor_json = supported_context?(json) && equals_or_includes_any?(json['type'], ActivityPub::FetchRemoteAccountService::SUPPORTED_TYPES) ? json : nil
  96. end
  97. def lock_options
  98. { redis: Redis.current, key: "resolve:#{@username}@#{@domain}" }
  99. end
  100. end