fetch_remote_key_service.rb 1.6 KB

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