about summary refs log tree commit diff
path: root/app/services/activitypub/fetch_remote_key_service.rb
diff options
context:
space:
mode:
authorbeatrix <beatrix.bitrot@gmail.com>2017-09-09 20:11:48 -0400
committerGitHub <noreply@github.com>2017-09-09 20:11:48 -0400
commit3dff74eecf5387b92b862893248710d2efb90eec (patch)
tree0d29d8c952a0c62e7de4348a1d63963fd5eca237 /app/services/activitypub/fetch_remote_key_service.rb
parente18ed4bbc7ab4e258d05a3e2a5db0790f67a8f37 (diff)
parent14e1fb8d36763e5255e7b8e440ecaf02208db004 (diff)
Merge pull request #141 from yipdw/sync/upstream
Sync with upstream @ v1.6.0rc3

ohhhhhhhhhhh heck here we go
Diffstat (limited to 'app/services/activitypub/fetch_remote_key_service.rb')
-rw-r--r--app/services/activitypub/fetch_remote_key_service.rb47
1 files changed, 47 insertions, 0 deletions
diff --git a/app/services/activitypub/fetch_remote_key_service.rb b/app/services/activitypub/fetch_remote_key_service.rb
new file mode 100644
index 000000000..ebd64071e
--- /dev/null
+++ b/app/services/activitypub/fetch_remote_key_service.rb
@@ -0,0 +1,47 @@
+# frozen_string_literal: true
+
+class ActivityPub::FetchRemoteKeyService < BaseService
+  include JsonLdHelper
+
+  # Returns account that owns the key
+  def call(uri, prefetched_json = nil)
+    @json = body_to_json(prefetched_json) || fetch_resource(uri)
+
+    return unless supported_context?(@json) && expected_type?
+    return find_account(uri, @json) if person?
+
+    @owner = fetch_resource(owner_uri)
+
+    return unless supported_context?(@owner) && confirmed_owner?
+
+    find_account(owner_uri, @owner)
+  end
+
+  private
+
+  def find_account(uri, prefetched_json)
+    account   = ActivityPub::TagManager.instance.uri_to_resource(uri, Account)
+    account ||= ActivityPub::FetchRemoteAccountService.new.call(uri, prefetched_json)
+    account
+  end
+
+  def expected_type?
+    person? || public_key?
+  end
+
+  def person?
+    @json['type'] == 'Person'
+  end
+
+  def public_key?
+    @json['publicKeyPem'].present? && @json['owner'].present?
+  end
+
+  def owner_uri
+    @owner_uri ||= value_or_id(@json['owner'])
+  end
+
+  def confirmed_owner?
+    @owner['type'] == 'Person' && value_or_id(@owner['publicKey']) == @json['id']
+  end
+end