2017-08-21 22:57:34 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ActivityPub::FetchRemoteKeyService < BaseService
|
|
|
|
include JsonLdHelper
|
|
|
|
|
2022-09-20 23:30:26 +02:00
|
|
|
class Error < StandardError; end
|
|
|
|
|
2022-09-21 22:45:57 +02:00
|
|
|
# Returns actor that owns the key
|
2024-02-01 15:56:46 +01:00
|
|
|
def call(uri, suppress_errors: true)
|
2022-09-20 23:30:26 +02:00
|
|
|
raise Error, 'No key URI given' if uri.blank?
|
2021-05-01 23:18:59 +02:00
|
|
|
|
2024-02-01 15:56:46 +01:00
|
|
|
@json = fetch_resource(uri, false)
|
2017-08-21 22:57:34 +02:00
|
|
|
|
2022-09-20 23:30:26 +02:00
|
|
|
raise Error, "Unable to fetch key JSON at #{uri}" if @json.nil?
|
|
|
|
raise Error, "Unsupported JSON-LD context for document #{uri}" unless supported_context?(@json)
|
|
|
|
raise Error, "Unexpected object type for key #{uri}" unless expected_type?
|
2022-09-24 07:41:01 +02:00
|
|
|
return find_actor(@json['id'], @json, suppress_errors) if actor_type?
|
2017-08-21 22:57:34 +02:00
|
|
|
|
2017-10-04 01:13:48 +02:00
|
|
|
@owner = fetch_resource(owner_uri, true)
|
2017-08-21 22:57:34 +02:00
|
|
|
|
2022-09-20 23:30:26 +02:00
|
|
|
raise Error, "Unable to fetch actor JSON #{owner_uri}" if @owner.nil?
|
|
|
|
raise Error, "Unsupported JSON-LD context for document #{owner_uri}" unless supported_context?(@owner)
|
|
|
|
raise Error, "Unexpected object type for actor #{owner_uri} (expected any of: #{SUPPORTED_TYPES})" unless expected_owner_type?
|
|
|
|
raise Error, "publicKey id for #{owner_uri} does not correspond to #{@json['id']}" unless confirmed_owner?
|
2017-08-21 22:57:34 +02:00
|
|
|
|
2022-09-21 22:45:57 +02:00
|
|
|
find_actor(owner_uri, @owner, suppress_errors)
|
2022-09-20 23:30:26 +02:00
|
|
|
rescue Error => e
|
2023-02-07 03:44:36 +01:00
|
|
|
Rails.logger.debug { "Fetching key #{uri} failed: #{e.message}" }
|
2022-09-20 23:30:26 +02:00
|
|
|
raise unless suppress_errors
|
2017-08-21 22:57:34 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-09-21 22:45:57 +02:00
|
|
|
def find_actor(uri, prefetched_body, suppress_errors)
|
|
|
|
actor = ActivityPub::TagManager.instance.uri_to_actor(uri)
|
|
|
|
actor ||= ActivityPub::FetchRemoteActorService.new.call(uri, prefetched_body: prefetched_body, suppress_errors: suppress_errors)
|
|
|
|
actor
|
2017-08-21 22:57:34 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def expected_type?
|
2022-09-24 07:41:01 +02:00
|
|
|
actor_type? || public_key?
|
2017-08-21 22:57:34 +02:00
|
|
|
end
|
|
|
|
|
2022-09-24 07:41:01 +02:00
|
|
|
def actor_type?
|
2022-09-21 22:45:57 +02:00
|
|
|
equals_or_includes_any?(@json['type'], ActivityPub::FetchRemoteActorService::SUPPORTED_TYPES)
|
2017-08-21 22:57:34 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def public_key?
|
|
|
|
@json['publicKeyPem'].present? && @json['owner'].present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def owner_uri
|
|
|
|
@owner_uri ||= value_or_id(@json['owner'])
|
|
|
|
end
|
|
|
|
|
2022-09-20 23:30:26 +02:00
|
|
|
def expected_owner_type?
|
2022-09-21 22:45:57 +02:00
|
|
|
equals_or_includes_any?(@owner['type'], ActivityPub::FetchRemoteActorService::SUPPORTED_TYPES)
|
2022-09-20 23:30:26 +02:00
|
|
|
end
|
|
|
|
|
2017-08-21 22:57:34 +02:00
|
|
|
def confirmed_owner?
|
2022-09-20 23:30:26 +02:00
|
|
|
value_or_id(@owner['publicKey']) == @json['id']
|
2017-08-21 22:57:34 +02:00
|
|
|
end
|
|
|
|
end
|