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.

60 lines
1.4 KiB

  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)
  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. @json['type'] == 'Person'
  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. @owner['type'] == 'Person' && value_or_id(@owner['publicKey']) == @json['id']
  46. end
  47. end