fetch_remote_account_service.rb 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # frozen_string_literal: true
  2. class ActivityPub::FetchRemoteAccountService < BaseService
  3. include JsonLdHelper
  4. # Should be called when uri has already been checked for locality
  5. # Does a WebFinger roundtrip on each call
  6. def call(uri, prefetched_json = nil)
  7. @json = body_to_json(prefetched_json) || fetch_resource(uri)
  8. return unless supported_context? && expected_type?
  9. @uri = @json['id']
  10. @username = @json['preferredUsername']
  11. @domain = Addressable::URI.parse(uri).normalized_host
  12. return unless verified_webfinger?
  13. ActivityPub::ProcessAccountService.new.call(@username, @domain, @json)
  14. rescue Oj::ParseError
  15. nil
  16. end
  17. private
  18. def verified_webfinger?
  19. webfinger = Goldfinger.finger("acct:#{@username}@#{@domain}")
  20. confirmed_username, confirmed_domain = split_acct(webfinger.subject)
  21. return true if @username.casecmp(confirmed_username).zero? && @domain.casecmp(confirmed_domain).zero?
  22. webfinger = Goldfinger.finger("acct:#{confirmed_username}@#{confirmed_domain}")
  23. confirmed_username, confirmed_domain = split_acct(webfinger.subject)
  24. self_reference = webfinger.link('self')
  25. return false if self_reference&.href != @uri
  26. @username = confirmed_username
  27. @domain = confirmed_domain
  28. true
  29. rescue Goldfinger::Error
  30. false
  31. end
  32. def split_acct(acct)
  33. acct.gsub(/\Aacct:/, '').split('@')
  34. end
  35. def supported_context?
  36. super(@json)
  37. end
  38. def expected_type?
  39. @json['type'] == 'Person'
  40. end
  41. end