2017-08-22 05:57:34 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ActivityPub::FetchRemoteKeyService < BaseService
|
|
|
|
include JsonLdHelper
|
|
|
|
|
2022-09-21 06:30:26 +09:00
|
|
|
class Error < StandardError; end
|
|
|
|
|
2017-08-22 05:57:34 +09:00
|
|
|
# Returns account that owns the key
|
2022-09-21 06:30:26 +09:00
|
|
|
def call(uri, id: true, prefetched_body: nil, suppress_errors: true)
|
|
|
|
raise Error, 'No key URI given' if uri.blank?
|
2021-05-02 06:18:59 +09:00
|
|
|
|
2017-10-04 08:13:48 +09:00
|
|
|
if prefetched_body.nil?
|
|
|
|
if id
|
|
|
|
@json = fetch_resource_without_id_validation(uri)
|
|
|
|
if person?
|
|
|
|
@json = fetch_resource(@json['id'], true)
|
|
|
|
elsif uri != @json['id']
|
2022-09-21 06:30:26 +09:00
|
|
|
raise Error, "Fetched URI #{uri} has wrong id #{@json['id']}"
|
2017-10-04 08:13:48 +09:00
|
|
|
end
|
|
|
|
else
|
|
|
|
@json = fetch_resource(uri, id)
|
|
|
|
end
|
|
|
|
else
|
2018-08-23 03:55:14 +09:00
|
|
|
@json = body_to_json(prefetched_body, compare_id: id ? uri : nil)
|
2017-10-04 08:13:48 +09:00
|
|
|
end
|
2017-08-22 05:57:34 +09:00
|
|
|
|
2022-09-21 06:30:26 +09: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?
|
|
|
|
return find_account(@json['id'], @json, suppress_errors) if person?
|
2017-08-22 05:57:34 +09:00
|
|
|
|
2017-10-04 08:13:48 +09:00
|
|
|
@owner = fetch_resource(owner_uri, true)
|
2017-08-22 05:57:34 +09:00
|
|
|
|
2022-09-21 06:30:26 +09: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-22 05:57:34 +09:00
|
|
|
|
2022-09-21 06:30:26 +09:00
|
|
|
find_account(owner_uri, @owner, suppress_errors)
|
|
|
|
rescue Error => e
|
|
|
|
Rails.logger.debug "Fetching key #{uri} failed: #{e.message}"
|
|
|
|
raise unless suppress_errors
|
2017-08-22 05:57:34 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-09-21 06:30:26 +09:00
|
|
|
def find_account(uri, prefetched_body, suppress_errors)
|
2017-08-22 05:57:34 +09:00
|
|
|
account = ActivityPub::TagManager.instance.uri_to_resource(uri, Account)
|
2022-09-21 06:30:26 +09:00
|
|
|
account ||= ActivityPub::FetchRemoteAccountService.new.call(uri, prefetched_body: prefetched_body, suppress_errors: suppress_errors)
|
2017-08-22 05:57:34 +09:00
|
|
|
account
|
|
|
|
end
|
|
|
|
|
|
|
|
def expected_type?
|
|
|
|
person? || public_key?
|
|
|
|
end
|
|
|
|
|
|
|
|
def person?
|
2018-05-02 19:40:24 +09:00
|
|
|
equals_or_includes_any?(@json['type'], ActivityPub::FetchRemoteAccountService::SUPPORTED_TYPES)
|
2017-08-22 05:57:34 +09: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-21 06:30:26 +09:00
|
|
|
def expected_owner_type?
|
|
|
|
equals_or_includes_any?(@owner['type'], ActivityPub::FetchRemoteAccountService::SUPPORTED_TYPES)
|
|
|
|
end
|
|
|
|
|
2017-08-22 05:57:34 +09:00
|
|
|
def confirmed_owner?
|
2022-09-21 06:30:26 +09:00
|
|
|
value_or_id(@owner['publicKey']) == @json['id']
|
2017-08-22 05:57:34 +09:00
|
|
|
end
|
|
|
|
end
|