Change code to use first usable keypairs record for local user for signing, if it exists (#39662)
This commit is contained in:
1 parent
68627adff4
commit
7fc19f81aa
14 files changed
+115
-33
No files matched your search
@@ -34,18 +34,19 @@ class ActivityPub::LinkedDataSignature
|
||||
end
|
||||
|
||||
def sign!(creator, sign_with: nil)
|
||||
keypair = sign_with.presence || creator.keypair(type: :rsa)
|
||||
|
||||
options = {
|
||||
'type' => 'RsaSignature2017',
|
||||
'creator' => ActivityPub::TagManager.instance.key_uri_for(creator),
|
||||
'creator' => keypair.full_uri,
|
||||
'created' => Time.now.utc.iso8601,
|
||||
}
|
||||
|
||||
options_hash = hash(options.without('type', 'id', 'signatureValue').merge('@context' => CONTEXT))
|
||||
document_hash = hash(@json.without('signature'))
|
||||
to_be_signed = options_hash + document_hash
|
||||
keypair = sign_with.present? ? OpenSSL::PKey::RSA.new(sign_with) : creator.keypair
|
||||
|
||||
signature = Base64.strict_encode64(keypair.sign(OpenSSL::Digest.new('SHA256'), to_be_signed))
|
||||
signature = Base64.strict_encode64(keypair.keypair.sign(OpenSSL::Digest.new('SHA256'), to_be_signed))
|
||||
|
||||
# Mastodon's context is either an array or a single URL
|
||||
context_with_security = Array(@json['@context'])
|
||||
|
||||
+2
-3
@@ -100,9 +100,8 @@ class Request
|
||||
def on_behalf_of(actor, sign_with: nil)
|
||||
raise ArgumentError, 'actor must not be nil' if actor.nil?
|
||||
|
||||
key_id = ActivityPub::TagManager.instance.key_uri_for(actor)
|
||||
keypair = sign_with.present? ? OpenSSL::PKey::RSA.new(sign_with) : actor.keypair
|
||||
@signing = HttpSignatureDraft.new(keypair, key_id)
|
||||
keypair = sign_with.presence || actor.keypair(type: :rsa)
|
||||
@signing = HttpSignatureDraft.new(keypair.keypair, keypair.full_uri)
|
||||
|
||||
self
|
||||
end
|
||||
|
||||
+16
-2
@@ -301,8 +301,22 @@ class Account < ApplicationRecord
|
||||
strikes.where(overruled_at: nil).count
|
||||
end
|
||||
|
||||
def keypair
|
||||
@keypair ||= OpenSSL::PKey::RSA.new(private_key || public_key)
|
||||
def keypair(type: nil)
|
||||
# Pick the first (oldest) keypair matching the expected type,
|
||||
# as we can expect our key rotation code to add stand-by keys with higher IDs
|
||||
# before pruning older keys with lower IDs after some time.
|
||||
|
||||
scope = keypairs.usable.order(id: :asc)
|
||||
scope = scope.where(type: type) if type.present?
|
||||
|
||||
case type
|
||||
when :rsa, nil
|
||||
# The legacy key is always RSA, so only fallback
|
||||
# when no other type is requested
|
||||
scope.first || Keypair.from_legacy_account(self)
|
||||
else
|
||||
scope.first
|
||||
end
|
||||
end
|
||||
|
||||
def tags_as_strings=(tag_names)
|
||||
|
||||
@@ -50,6 +50,12 @@ class Keypair < ApplicationRecord
|
||||
|
||||
alias actor account
|
||||
|
||||
def full_uri
|
||||
return ActivityPub::TagManager.instance.uri_for(account) + local_fragment if local_fragment.present?
|
||||
|
||||
uri
|
||||
end
|
||||
|
||||
def keypair
|
||||
@keypair ||= begin
|
||||
case type
|
||||
@@ -87,4 +93,22 @@ class Keypair < ApplicationRecord
|
||||
type: :rsa
|
||||
)
|
||||
end
|
||||
|
||||
def self.from_worker_arg(account, private_key_pem_or_hash)
|
||||
if private_key_pem_or_hash.is_a?(String)
|
||||
account.keypairs.build(
|
||||
private_key: private_key_pem_or_hash,
|
||||
local_fragment: '#main-key',
|
||||
type: :rsa
|
||||
)
|
||||
else
|
||||
account.keypairs.build(
|
||||
private_key: private_key_pem_or_hash['private_key'],
|
||||
public_key: private_key_pem_or_hash['public_key'],
|
||||
uri: private_key_pem_or_hash['uri'],
|
||||
local_fragment: private_key_pem_or_hash['local_fragment'],
|
||||
type: private_key_pem_or_hash['type']
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -24,7 +24,7 @@ class ActivityPub::ActorSerializer < ActivityPub::Serializer
|
||||
attribute :interaction_policy
|
||||
attribute :featured_collections
|
||||
|
||||
has_one :public_key, serializer: ActivityPub::PublicKeySerializer
|
||||
has_one :keypair, key: :public_key, serializer: ActivityPub::PublicKeySerializer
|
||||
|
||||
has_many :virtual_tags, key: :tag
|
||||
has_many :virtual_attachments, key: :attachment
|
||||
|
||||
@@ -6,11 +6,11 @@ class ActivityPub::PublicKeySerializer < ActivityPub::Serializer
|
||||
attributes :id, :owner, :public_key_pem
|
||||
|
||||
def id
|
||||
ActivityPub::TagManager.instance.key_uri_for(object)
|
||||
object.full_uri
|
||||
end
|
||||
|
||||
def owner
|
||||
ActivityPub::TagManager.instance.uri_for(object)
|
||||
ActivityPub::TagManager.instance.uri_for(object.actor)
|
||||
end
|
||||
|
||||
def public_key_pem
|
||||
|
||||
@@ -48,7 +48,7 @@ class ActivityPub::DeliveryWorker
|
||||
|
||||
def build_request(http_client)
|
||||
Request.new(:post, @inbox_url, body: @json, http_client: http_client).tap do |request|
|
||||
request.on_behalf_of(@source_account, sign_with: @options[:sign_with])
|
||||
request.on_behalf_of(@source_account, sign_with: sign_with)
|
||||
request.add_headers(HEADERS)
|
||||
request.add_headers({ 'Collection-Synchronization' => synchronization_header }) if ENV['DISABLE_FOLLOWERS_SYNCHRONIZATION'] != 'true' && @options[:synchronize_followers]
|
||||
end
|
||||
@@ -93,4 +93,8 @@ class ActivityPub::DeliveryWorker
|
||||
def request_pool
|
||||
RequestPool.current
|
||||
end
|
||||
|
||||
def sign_with
|
||||
@options[:sign_with].presence && Keypair.from_worker_arg(@source_account, @options[:sign_with])
|
||||
end
|
||||
end
|
||||
@@ -23,6 +23,10 @@ class ActivityPub::UpdateDistributionWorker < ActivityPub::RawDistributionWorker
|
||||
end
|
||||
|
||||
def payload
|
||||
@payload ||= serialize_payload(@account, ActivityPub::UpdateActorSerializer, signer: @account, sign_with: @options[:sign_with]).to_json
|
||||
@payload ||= serialize_payload(@account, ActivityPub::UpdateActorSerializer, signer: @account, sign_with: sign_with).to_json
|
||||
end
|
||||
|
||||
def sign_with
|
||||
@options[:sign_with].presence && Keypair.from_worker_arg(@account, @options[:sign_with])
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user