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:
Benjamin Neff 2016-01-30 23:53:48 +01:00
parent 1428c09da7
commit 714f6d8273
4 changed files with 45 additions and 15 deletions

View file

@ -78,6 +78,12 @@ module DiasporaFederation
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
# @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
def fill_required_signature(target_author, privkey, hash)
if target_author == diaspora_id && target_author_signature.nil?
hash[:target_author_signature] =
Signing.sign_with_key(SignedRetraction.apply_signable_exceptions(hash), privkey)
hash[:target_author_signature] = SignedRetraction.sign_with_key(privkey, self)
elsif target_author != diaspora_id && parent_author_signature.nil?
hash[:parent_author_signature] =
Signing.sign_with_key(SignedRetraction.apply_signable_exceptions(hash), privkey)
hash[:parent_author_signature] = SignedRetraction.sign_with_key(privkey, self)
end
end
end

View file

@ -48,21 +48,23 @@ module DiasporaFederation
super.tap do |hash|
if target_author_signature.nil?
privkey = DiasporaFederation.callbacks.trigger(:fetch_private_key_by_diaspora_id, diaspora_id)
unless privkey.nil?
hash[:target_author_signature] =
Signing.sign_with_key(SignedRetraction.apply_signable_exceptions(hash), privkey)
end
hash[:target_author_signature] = SignedRetraction.sign_with_key(privkey, self) unless privkey.nil?
end
end
end
# Deletes :diaspora_id (xml_name: sender_handle) from the hash in order to compute
# a signature since it is included from signable_string for SignedRetraction and RelayableRetraction
#
# @param [Hash] data hash of the retraction properties
# @return [Hash] hash copy without :diaspora_id member
def self.apply_signable_exceptions(data)
data.dup.tap {|data| data.delete(:diaspora_id) }
# 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
# 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

View file

@ -75,5 +75,17 @@ XML
expect(signed_hash[:target_author_signature]).to eq(nil)
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

View file

@ -50,5 +50,17 @@ XML
expect(signed_hash[:author_signature]).to eq(nil)
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