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.

61 lines
1.8 KiB

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