fetch_remote_key_service.rb 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # frozen_string_literal: true
  2. class ActivityPub::FetchRemoteKeyService < BaseService
  3. include JsonLdHelper
  4. # Returns account that owns the key
  5. def call(uri)
  6. return if uri.blank?
  7. @json = fetch_resource(uri, false)
  8. return unless supported_context?(@json) && expected_type?
  9. return find_account(@json['id'], @json) if person?
  10. @owner = fetch_resource(owner_uri, true)
  11. return unless supported_context?(@owner) && confirmed_owner?
  12. find_account(owner_uri, @owner)
  13. end
  14. private
  15. def find_account(uri, prefetched_body)
  16. account = ActivityPub::TagManager.instance.uri_to_resource(uri, Account)
  17. account ||= ActivityPub::FetchRemoteAccountService.new.call(uri, prefetched_body: prefetched_body)
  18. account
  19. end
  20. def expected_type?
  21. person? || public_key?
  22. end
  23. def person?
  24. equals_or_includes_any?(@json['type'], ActivityPub::FetchRemoteAccountService::SUPPORTED_TYPES)
  25. end
  26. def public_key?
  27. @json['publicKeyPem'].present? && @json['owner'].present?
  28. end
  29. def owner_uri
  30. @owner_uri ||= value_or_id(@json['owner'])
  31. end
  32. def confirmed_owner?
  33. equals_or_includes_any?(@owner['type'], ActivityPub::FetchRemoteAccountService::SUPPORTED_TYPES) && value_or_id(@owner['publicKey']) == @json['id']
  34. end
  35. end