Refactor all ActivityPub deliveries to be serialized and signed through one concern (#10966)

lolsob-rspec
Eugen Rochko 2019-06-04 23:11:18 +02:00 committed by GitHub
parent f3a02e70a8
commit 57098a1b8d
24 changed files with 79 additions and 153 deletions

View File

@ -1,6 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
class ActivityPub::Activity::Follow < ActivityPub::Activity class ActivityPub::Activity::Follow < ActivityPub::Activity
include Payloadable
def perform def perform
target_account = account_from_uri(object_uri) target_account = account_from_uri(object_uri)
@ -28,7 +30,7 @@ class ActivityPub::Activity::Follow < ActivityPub::Activity
end end
def reject_follow_request!(target_account) def reject_follow_request!(target_account)
json = ActiveModelSerializers::SerializableResource.new(FollowRequest.new(account: @account, target_account: target_account, uri: @json['id']), serializer: ActivityPub::RejectFollowSerializer, adapter: ActivityPub::Adapter).to_json json = Oj.dump(serialize_payload(FollowRequest.new(account: @account, target_account: target_account, uri: @json['id']), ActivityPub::RejectFollowSerializer))
ActivityPub::DeliveryWorker.perform_async(json, target_account.id, @account.inbox_url) ActivityPub::DeliveryWorker.perform_async(json, target_account.id, @account.inbox_url)
end end
end end

View File

@ -204,6 +204,10 @@ class Account < ApplicationRecord
end end
end end
def sign?
true
end
def keypair def keypair
@keypair ||= OpenSSL::PKey::RSA.new(private_key || public_key) @keypair ||= OpenSSL::PKey::RSA.new(private_key || public_key)
end end

View File

@ -3,6 +3,7 @@
class Form::AccountBatch class Form::AccountBatch
include ActiveModel::Model include ActiveModel::Model
include Authorization include Authorization
include Payloadable
attr_accessor :account_ids, :action, :current_account attr_accessor :account_ids, :action, :current_account
@ -54,13 +55,7 @@ class Form::AccountBatch
return unless follow.account.activitypub? return unless follow.account.activitypub?
json = ActiveModelSerializers::SerializableResource.new( ActivityPub::DeliveryWorker.perform_async(Oj.dump(serialize_payload(follow, ActivityPub::RejectFollowSerializer)), current_account.id, follow.account.inbox_url)
follow,
serializer: ActivityPub::RejectFollowSerializer,
adapter: ActivityPub::Adapter
).to_json
ActivityPub::DeliveryWorker.perform_async(json, current_account.id, follow.account.inbox_url)
end end
def approve! def approve!

View File

@ -204,6 +204,8 @@ class Status < ApplicationRecord
public_visibility? || unlisted_visibility? public_visibility? || unlisted_visibility?
end end
alias sign? distributable?
def with_media? def with_media?
media_attachments.any? media_attachments.any?
end end

View File

@ -1,6 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
class AfterBlockDomainFromAccountService < BaseService class AfterBlockDomainFromAccountService < BaseService
include Payloadable
# This service does not create an AccountDomainBlock record, # This service does not create an AccountDomainBlock record,
# it's meant to be called after such a record has been created # it's meant to be called after such a record has been created
# synchronously, to "clean up" # synchronously, to "clean up"
@ -31,12 +33,6 @@ class AfterBlockDomainFromAccountService < BaseService
return unless follow.account.activitypub? return unless follow.account.activitypub?
json = ActiveModelSerializers::SerializableResource.new( ActivityPub::DeliveryWorker.perform_async(Oj.dump(serialize_payload(follow, ActivityPub::RejectFollowSerializer)), @account.id, follow.account.inbox_url)
follow,
serializer: ActivityPub::RejectFollowSerializer,
adapter: ActivityPub::Adapter
).to_json
ActivityPub::DeliveryWorker.perform_async(json, @account.id, follow.account.inbox_url)
end end
end end

View File

@ -1,6 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
class AuthorizeFollowService < BaseService class AuthorizeFollowService < BaseService
include Payloadable
def call(source_account, target_account, **options) def call(source_account, target_account, **options)
if options[:skip_follow_request] if options[:skip_follow_request]
follow_request = FollowRequest.new(account: source_account, target_account: target_account, uri: options[:follow_request_uri]) follow_request = FollowRequest.new(account: source_account, target_account: target_account, uri: options[:follow_request_uri])
@ -24,11 +26,7 @@ class AuthorizeFollowService < BaseService
end end
def build_json(follow_request) def build_json(follow_request)
ActiveModelSerializers::SerializableResource.new( Oj.dump(serialize_payload(follow_request, ActivityPub::AcceptFollowSerializer))
follow_request,
serializer: ActivityPub::AcceptFollowSerializer,
adapter: ActivityPub::Adapter
).to_json
end end
def build_xml(follow_request) def build_xml(follow_request)

View File

@ -1,6 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
class BlockService < BaseService class BlockService < BaseService
include Payloadable
def call(account, target_account) def call(account, target_account)
return if account.id == target_account.id return if account.id == target_account.id
@ -26,11 +28,7 @@ class BlockService < BaseService
end end
def build_json(block) def build_json(block)
ActiveModelSerializers::SerializableResource.new( Oj.dump(serialize_payload(block, ActivityPub::BlockSerializer))
block,
serializer: ActivityPub::BlockSerializer,
adapter: ActivityPub::Adapter
).to_json
end end
def build_xml(block) def build_xml(block)

View File

@ -0,0 +1,19 @@
# frozen_string_literal: true
module Payloadable
def serialize_payload(record, serializer, options = {})
signer = options.delete(:signer)
sign_with = options.delete(:sign_with)
payload = ActiveModelSerializers::SerializableResource.new(record, options.merge(serializer: serializer, adapter: ActivityPub::Adapter)).as_json
if (record.respond_to?(:sign?) && record.sign?) && signer && signing_enabled?
ActivityPub::LinkedDataSignature.new(payload).sign!(signer, sign_with: sign_with)
else
payload
end
end
def signing_enabled?
true
end
end

View File

@ -2,6 +2,7 @@
class FavouriteService < BaseService class FavouriteService < BaseService
include Authorization include Authorization
include Payloadable
# Favourite a status and notify remote user # Favourite a status and notify remote user
# @param [Account] account # @param [Account] account
@ -43,11 +44,7 @@ class FavouriteService < BaseService
end end
def build_json(favourite) def build_json(favourite)
Oj.dump(ActivityPub::LinkedDataSignature.new(ActiveModelSerializers::SerializableResource.new( Oj.dump(serialize_payload(favourite, ActivityPub::LikeSerializer))
favourite,
serializer: ActivityPub::LikeSerializer,
adapter: ActivityPub::Adapter
).as_json).sign!(favourite.account))
end end
def build_xml(favourite) def build_xml(favourite)

View File

@ -2,6 +2,7 @@
class FollowService < BaseService class FollowService < BaseService
include Redisable include Redisable
include Payloadable
# Follow a remote user, notify remote user about the follow # Follow a remote user, notify remote user about the follow
# @param [Account] source_account From which to follow # @param [Account] source_account From which to follow
@ -78,10 +79,6 @@ class FollowService < BaseService
end end
def build_json(follow_request) def build_json(follow_request)
ActiveModelSerializers::SerializableResource.new( Oj.dump(serialize_payload(follow_request, ActivityPub::FollowSerializer))
follow_request,
serializer: ActivityPub::FollowSerializer,
adapter: ActivityPub::Adapter
).to_json
end end
end end

View File

@ -2,6 +2,7 @@
class ProcessMentionsService < BaseService class ProcessMentionsService < BaseService
include StreamEntryRenderer include StreamEntryRenderer
include Payloadable
# Scan status for mentions and fetch remote mentioned users, create # Scan status for mentions and fetch remote mentioned users, create
# local mention pointers, send Salmon notifications to mentioned # local mention pointers, send Salmon notifications to mentioned
@ -61,12 +62,7 @@ class ProcessMentionsService < BaseService
def activitypub_json def activitypub_json
return @activitypub_json if defined?(@activitypub_json) return @activitypub_json if defined?(@activitypub_json)
payload = ActiveModelSerializers::SerializableResource.new( @activitypub_json = Oj.dump(serialize_payload(@status, ActivityPub::ActivitySerializer, signer: @status.account))
@status,
serializer: ActivityPub::ActivitySerializer,
adapter: ActivityPub::Adapter
).as_json
@activitypub_json = Oj.dump(@status.distributable? ? ActivityPub::LinkedDataSignature.new(payload).sign!(@status.account) : payload)
end end
def resolve_account_service def resolve_account_service

View File

@ -3,6 +3,7 @@
class ReblogService < BaseService class ReblogService < BaseService
include Authorization include Authorization
include StreamEntryRenderer include StreamEntryRenderer
include Payloadable
# Reblog a status and notify its remote author # Reblog a status and notify its remote author
# @param [Account] account Account to reblog from # @param [Account] account Account to reblog from
@ -53,10 +54,6 @@ class ReblogService < BaseService
end end
def build_json(reblog) def build_json(reblog)
Oj.dump(ActivityPub::LinkedDataSignature.new(ActiveModelSerializers::SerializableResource.new( Oj.dump(serialize_payload(reblog, ActivityPub::ActivitySerializer, signer: reblog.account))
reblog,
serializer: ActivityPub::ActivitySerializer,
adapter: ActivityPub::Adapter
).as_json).sign!(reblog.account))
end end
end end

View File

@ -1,6 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
class RejectFollowService < BaseService class RejectFollowService < BaseService
include Payloadable
def call(source_account, target_account) def call(source_account, target_account)
follow_request = FollowRequest.find_by!(account: source_account, target_account: target_account) follow_request = FollowRequest.find_by!(account: source_account, target_account: target_account)
follow_request.reject! follow_request.reject!
@ -19,11 +21,7 @@ class RejectFollowService < BaseService
end end
def build_json(follow_request) def build_json(follow_request)
ActiveModelSerializers::SerializableResource.new( Oj.dump(serialize_payload(follow_request, ActivityPub::RejectFollowSerializer))
follow_request,
serializer: ActivityPub::RejectFollowSerializer,
adapter: ActivityPub::Adapter
).to_json
end end
def build_xml(follow_request) def build_xml(follow_request)

View File

@ -3,6 +3,7 @@
class RemoveStatusService < BaseService class RemoveStatusService < BaseService
include StreamEntryRenderer include StreamEntryRenderer
include Redisable include Redisable
include Payloadable
def call(status, **options) def call(status, **options)
@payload = Oj.dump(event: :delete, payload: status.id.to_s) @payload = Oj.dump(event: :delete, payload: status.id.to_s)
@ -115,15 +116,7 @@ class RemoveStatusService < BaseService
end end
def signed_activity_json def signed_activity_json
@signed_activity_json ||= Oj.dump(ActivityPub::LinkedDataSignature.new(activity_json).sign!(@account)) @signed_activity_json ||= Oj.dump(serialize_payload(@status, @status.reblog? ? ActivityPub::UndoAnnounceSerializer : ActivityPub::DeleteSerializer, signer: @account))
end
def activity_json
@activity_json ||= ActiveModelSerializers::SerializableResource.new(
@status,
serializer: @status.reblog? ? ActivityPub::UndoAnnounceSerializer : ActivityPub::DeleteSerializer,
adapter: ActivityPub::Adapter
).as_json
end end
def remove_reblogs def remove_reblogs

View File

@ -1,6 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
class ReportService < BaseService class ReportService < BaseService
include Payloadable
def call(source_account, target_account, options = {}) def call(source_account, target_account, options = {})
@source_account = source_account @source_account = source_account
@target_account = target_account @target_account = target_account
@ -44,12 +46,7 @@ class ReportService < BaseService
end end
def payload def payload
Oj.dump(ActiveModelSerializers::SerializableResource.new( Oj.dump(serialize_payload(@report, ActivityPub::FlagSerializer, account: some_local_account))
@report,
serializer: ActivityPub::FlagSerializer,
adapter: ActivityPub::Adapter,
account: some_local_account
).as_json)
end end
def some_local_account def some_local_account

View File

@ -1,6 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
class SuspendAccountService < BaseService class SuspendAccountService < BaseService
include Payloadable
ASSOCIATIONS_ON_SUSPEND = %w( ASSOCIATIONS_ON_SUSPEND = %w(
account_pins account_pins
active_relationships active_relationships
@ -118,23 +120,11 @@ class SuspendAccountService < BaseService
end end
def delete_actor_json def delete_actor_json
return @delete_actor_json if defined?(@delete_actor_json) @delete_actor_json ||= Oj.dump(serialize_payload(@account, ActivityPub::DeleteActorSerializer, signer: @account))
payload = ActiveModelSerializers::SerializableResource.new(
@account,
serializer: ActivityPub::DeleteActorSerializer,
adapter: ActivityPub::Adapter
).as_json
@delete_actor_json = Oj.dump(ActivityPub::LinkedDataSignature.new(payload).sign!(@account))
end end
def build_reject_json(follow) def build_reject_json(follow)
ActiveModelSerializers::SerializableResource.new( Oj.dump(serialize_payload(follow, ActivityPub::RejectFollowSerializer))
follow,
serializer: ActivityPub::RejectFollowSerializer,
adapter: ActivityPub::Adapter
).to_json
end end
def delivery_inboxes def delivery_inboxes

View File

@ -1,6 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
class UnblockService < BaseService class UnblockService < BaseService
include Payloadable
def call(account, target_account) def call(account, target_account)
return unless account.blocking?(target_account) return unless account.blocking?(target_account)
@ -20,11 +22,7 @@ class UnblockService < BaseService
end end
def build_json(unblock) def build_json(unblock)
ActiveModelSerializers::SerializableResource.new( Oj.dump(serialize_payload(unblock, ActivityPub::UndoBlockSerializer))
unblock,
serializer: ActivityPub::UndoBlockSerializer,
adapter: ActivityPub::Adapter
).to_json
end end
def build_xml(block) def build_xml(block)

View File

@ -1,6 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
class UnfavouriteService < BaseService class UnfavouriteService < BaseService
include Payloadable
def call(account, status) def call(account, status)
favourite = Favourite.find_by!(account: account, status: status) favourite = Favourite.find_by!(account: account, status: status)
favourite.destroy! favourite.destroy!
@ -21,11 +23,7 @@ class UnfavouriteService < BaseService
end end
def build_json(favourite) def build_json(favourite)
Oj.dump(ActivityPub::LinkedDataSignature.new(ActiveModelSerializers::SerializableResource.new( Oj.dump(serialize_payload(favourite, ActivityPub::UndoLikeSerializer))
favourite,
serializer: ActivityPub::UndoLikeSerializer,
adapter: ActivityPub::Adapter
).as_json).sign!(favourite.account))
end end
def build_xml(favourite) def build_xml(favourite)

View File

@ -1,6 +1,8 @@
# frozen_string_literal: true # frozen_string_literal: true
class UnfollowService < BaseService class UnfollowService < BaseService
include Payloadable
# Unfollow and notify the remote user # Unfollow and notify the remote user
# @param [Account] source_account Where to unfollow from # @param [Account] source_account Where to unfollow from
# @param [Account] target_account Which to unfollow # @param [Account] target_account Which to unfollow
@ -50,19 +52,11 @@ class UnfollowService < BaseService
end end
def build_json(follow) def build_json(follow)
ActiveModelSerializers::SerializableResource.new( Oj.dump(serialize_payload(follow, ActivityPub::UndoFollowSerializer))
follow,
serializer: ActivityPub::UndoFollowSerializer,
adapter: ActivityPub::Adapter
).to_json
end end
def build_reject_json(follow) def build_reject_json(follow)
ActiveModelSerializers::SerializableResource.new( Oj.dump(serialize_payload(follow, ActivityPub::RejectFollowSerializer))
follow,
serializer: ActivityPub::RejectFollowSerializer,
adapter: ActivityPub::Adapter
).to_json
end end
def build_xml(follow) def build_xml(follow)

View File

@ -2,6 +2,7 @@
class VoteService < BaseService class VoteService < BaseService
include Authorization include Authorization
include Payloadable
def call(account, poll, choices) def call(account, poll, choices)
authorize_with account, poll, :vote? authorize_with account, poll, :vote?
@ -50,10 +51,6 @@ class VoteService < BaseService
end end
def build_json(vote) def build_json(vote)
ActiveModelSerializers::SerializableResource.new( Oj.dump(serialize_payload(vote, ActivityPub::VoteSerializer))
vote,
serializer: ActivityPub::VoteSerializer,
adapter: ActivityPub::Adapter
).to_json
end end
end end

View File

@ -2,6 +2,7 @@
class ActivityPub::DistributePollUpdateWorker class ActivityPub::DistributePollUpdateWorker
include Sidekiq::Worker include Sidekiq::Worker
include Payloadable
sidekiq_options queue: 'push', unique: :until_executed, retry: 0 sidekiq_options queue: 'push', unique: :until_executed, retry: 0
@ -41,20 +42,8 @@ class ActivityPub::DistributePollUpdateWorker
@inboxes @inboxes
end end
def signed_payload
Oj.dump(ActivityPub::LinkedDataSignature.new(unsigned_payload).sign!(@account))
end
def unsigned_payload
ActiveModelSerializers::SerializableResource.new(
@status,
serializer: ActivityPub::UpdatePollSerializer,
adapter: ActivityPub::Adapter
).as_json
end
def payload def payload
@payload ||= @status.distributable? ? signed_payload : Oj.dump(unsigned_payload) @payload ||= Oj.dump(serialize_payload(@status, ActivityPub::UpdatePollSerializer, signer: @account))
end end
def relay! def relay!

View File

@ -2,6 +2,7 @@
class ActivityPub::DistributionWorker class ActivityPub::DistributionWorker
include Sidekiq::Worker include Sidekiq::Worker
include Payloadable
sidekiq_options queue: 'push' sidekiq_options queue: 'push'
@ -41,20 +42,8 @@ class ActivityPub::DistributionWorker
end end
end end
def signed_payload
Oj.dump(ActivityPub::LinkedDataSignature.new(unsigned_payload).sign!(@account))
end
def unsigned_payload
ActiveModelSerializers::SerializableResource.new(
@status,
serializer: ActivityPub::ActivitySerializer,
adapter: ActivityPub::Adapter
).as_json
end
def payload def payload
@payload ||= @status.distributable? ? signed_payload : Oj.dump(unsigned_payload) @payload ||= Oj.dump(serialize_payload(@status, ActivityPub::ActivitySerializer, signer: @account))
end end
def relay! def relay!

View File

@ -5,6 +5,7 @@
class ActivityPub::ReplyDistributionWorker class ActivityPub::ReplyDistributionWorker
include Sidekiq::Worker include Sidekiq::Worker
include Payloadable
sidekiq_options queue: 'push' sidekiq_options queue: 'push'
@ -27,19 +28,7 @@ class ActivityPub::ReplyDistributionWorker
@inboxes ||= @account.followers.inboxes @inboxes ||= @account.followers.inboxes
end end
def signed_payload
Oj.dump(ActivityPub::LinkedDataSignature.new(unsigned_payload).sign!(@status.account))
end
def unsigned_payload
ActiveModelSerializers::SerializableResource.new(
@status,
serializer: ActivityPub::ActivitySerializer,
adapter: ActivityPub::Adapter
).as_json
end
def payload def payload
@payload ||= @status.distributable? ? signed_payload : Oj.dump(unsigned_payload) @payload ||= Oj.dump(serialize_payload(@status, ActivityPub::ActivitySerializer, signer: @status.account))
end end
end end

View File

@ -2,6 +2,7 @@
class ActivityPub::UpdateDistributionWorker class ActivityPub::UpdateDistributionWorker
include Sidekiq::Worker include Sidekiq::Worker
include Payloadable
sidekiq_options queue: 'push' sidekiq_options queue: 'push'
@ -27,14 +28,6 @@ class ActivityPub::UpdateDistributionWorker
end end
def signed_payload def signed_payload
@signed_payload ||= Oj.dump(ActivityPub::LinkedDataSignature.new(payload).sign!(@account, sign_with: @options[:sign_with])) @signed_payload ||= Oj.dump(serialize_payload(@account, ActivityPub::UpdateSerializer, signer: @account, sign_with: @options[:sign_with]))
end
def payload
@payload ||= ActiveModelSerializers::SerializableResource.new(
@account,
serializer: ActivityPub::UpdateSerializer,
adapter: ActivityPub::Adapter
).as_json
end end
end end