create own sign-logic for SignedRetraction and RelayableRetraction
this is a small code-duplication, but will be removed when SignedRetraction and RelayableRetraction will be removed.
This commit is contained in:
parent
1428c09da7
commit
714f6d8273
4 changed files with 45 additions and 15 deletions
|
|
@ -78,6 +78,12 @@ module DiasporaFederation
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# use only {Retraction} for receive
|
||||||
|
# @return [Retraction] instance as normal retraction
|
||||||
|
def to_retraction
|
||||||
|
Retraction.new(diaspora_id: diaspora_id, target_guid: target_guid, target_type: target_type)
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
# @param [String] target_author the author of the entity to retract
|
# @param [String] target_author the author of the entity to retract
|
||||||
|
|
@ -85,11 +91,9 @@ module DiasporaFederation
|
||||||
# @param [Hash] hash hash given for a signing
|
# @param [Hash] hash hash given for a signing
|
||||||
def fill_required_signature(target_author, privkey, hash)
|
def fill_required_signature(target_author, privkey, hash)
|
||||||
if target_author == diaspora_id && target_author_signature.nil?
|
if target_author == diaspora_id && target_author_signature.nil?
|
||||||
hash[:target_author_signature] =
|
hash[:target_author_signature] = SignedRetraction.sign_with_key(privkey, self)
|
||||||
Signing.sign_with_key(SignedRetraction.apply_signable_exceptions(hash), privkey)
|
|
||||||
elsif target_author != diaspora_id && parent_author_signature.nil?
|
elsif target_author != diaspora_id && parent_author_signature.nil?
|
||||||
hash[:parent_author_signature] =
|
hash[:parent_author_signature] = SignedRetraction.sign_with_key(privkey, self)
|
||||||
Signing.sign_with_key(SignedRetraction.apply_signable_exceptions(hash), privkey)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -48,21 +48,23 @@ module DiasporaFederation
|
||||||
super.tap do |hash|
|
super.tap do |hash|
|
||||||
if target_author_signature.nil?
|
if target_author_signature.nil?
|
||||||
privkey = DiasporaFederation.callbacks.trigger(:fetch_private_key_by_diaspora_id, diaspora_id)
|
privkey = DiasporaFederation.callbacks.trigger(:fetch_private_key_by_diaspora_id, diaspora_id)
|
||||||
unless privkey.nil?
|
hash[:target_author_signature] = SignedRetraction.sign_with_key(privkey, self) unless privkey.nil?
|
||||||
hash[:target_author_signature] =
|
|
||||||
Signing.sign_with_key(SignedRetraction.apply_signable_exceptions(hash), privkey)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Deletes :diaspora_id (xml_name: sender_handle) from the hash in order to compute
|
# use only {Retraction} for receive
|
||||||
# a signature since it is included from signable_string for SignedRetraction and RelayableRetraction
|
# @return [Retraction] instance as normal retraction
|
||||||
#
|
def to_retraction
|
||||||
# @param [Hash] data hash of the retraction properties
|
Retraction.new(diaspora_id: diaspora_id, target_guid: target_guid, target_type: target_type)
|
||||||
# @return [Hash] hash copy without :diaspora_id member
|
end
|
||||||
def self.apply_signable_exceptions(data)
|
|
||||||
data.dup.tap {|data| data.delete(:diaspora_id) }
|
# Create signature for a retraction
|
||||||
|
# @param [OpenSSL::PKey::RSA] privkey private key of sender
|
||||||
|
# @param [SignedRetraction, RelayableRetraction] ret the retraction to sign
|
||||||
|
# @return [String] a Base64 encoded signature of the retraction with the key
|
||||||
|
def self.sign_with_key(privkey, ret)
|
||||||
|
Base64.strict_encode64(privkey.sign(OpenSSL::Digest::SHA256.new, [ret.target_guid, ret.target_type].join(";")))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -75,5 +75,17 @@ XML
|
||||||
expect(signed_hash[:target_author_signature]).to eq(nil)
|
expect(signed_hash[:target_author_signature]).to eq(nil)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "#to_retraction" do
|
||||||
|
it "copies the attributes to a Retraction" do
|
||||||
|
relayable_retraction = FactoryGirl.build(:relayable_retraction_entity)
|
||||||
|
retraction = relayable_retraction.to_retraction
|
||||||
|
|
||||||
|
expect(retraction).to be_a(Entities::Retraction)
|
||||||
|
expect(retraction.diaspora_id).to eq(relayable_retraction.diaspora_id)
|
||||||
|
expect(retraction.target_guid).to eq(relayable_retraction.target_guid)
|
||||||
|
expect(retraction.target_type).to eq(relayable_retraction.target_type)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -50,5 +50,17 @@ XML
|
||||||
expect(signed_hash[:author_signature]).to eq(nil)
|
expect(signed_hash[:author_signature]).to eq(nil)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "#to_retraction" do
|
||||||
|
it "copies the attributes to a Retraction" do
|
||||||
|
signed_retraction = FactoryGirl.build(:signed_retraction_entity)
|
||||||
|
retraction = signed_retraction.to_retraction
|
||||||
|
|
||||||
|
expect(retraction).to be_a(Entities::Retraction)
|
||||||
|
expect(retraction.diaspora_id).to eq(signed_retraction.diaspora_id)
|
||||||
|
expect(retraction.target_guid).to eq(signed_retraction.target_guid)
|
||||||
|
expect(retraction.target_type).to eq(signed_retraction.target_type)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue