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.
 
 
 
 

63 lines
2.0 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. # Does a WebFinger roundtrip on each call
  6. def call(uri, id: true, prefetched_body: nil, break_on_redirect: false)
  7. return ActivityPub::TagManager.instance.uri_to_resource(uri, Account) if ActivityPub::TagManager.instance.local_uri?(uri)
  8. @json = if prefetched_body.nil?
  9. fetch_resource(uri, id)
  10. else
  11. body_to_json(prefetched_body, compare_id: id ? uri : nil)
  12. end
  13. return if !supported_context? || !expected_type? || (break_on_redirect && @json['movedTo'].present?)
  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