logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

fetch_remote_account_service.rb (1848B)


  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. SUPPORTED_TYPES.include?(@json['type'])
  44. end
  45. end