From 8bd9c28ce09b420b92934cd5d1a662aa981a5f41 Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Thu, 21 Jan 2016 23:40:36 +0100 Subject: [PATCH] override to_h to sign relayables --- lib/diaspora_federation/entities/relayable.rb | 7 ++++--- .../entities/relayable_retraction.rb | 7 ++++--- .../entities/signed_retraction.rb | 7 ++++--- lib/diaspora_federation/test.rb | 7 ------- .../lib/diaspora_federation/entities/comment_spec.rb | 2 +- .../entities/conversation_spec.rb | 4 ++-- spec/lib/diaspora_federation/entities/like_spec.rb | 2 +- .../lib/diaspora_federation/entities/message_spec.rb | 2 +- .../entities/participation_spec.rb | 2 +- .../entities/poll_participation_spec.rb | 2 +- .../entities/relayable_retraction_spec.rb | 12 ++++++------ .../diaspora_federation/entities/relayable_spec.rb | 8 ++++---- .../entities/signed_retraction_spec.rb | 10 +++++----- 13 files changed, 34 insertions(+), 38 deletions(-) diff --git a/lib/diaspora_federation/entities/relayable.rb b/lib/diaspora_federation/entities/relayable.rb index 50f47fb..39215c5 100644 --- a/lib/diaspora_federation/entities/relayable.rb +++ b/lib/diaspora_federation/entities/relayable.rb @@ -37,9 +37,10 @@ module DiasporaFederation # Adds signatures to the hash with the keys of the author and the parent # if the signatures are not in the hash yet and if the keys are available. # + # @see Entity#to_h # @return [Hash] entity data hash with updated signatures - def to_signed_h - to_h.tap do |hash| + def to_h + super.tap do |hash| if author_signature.nil? privkey = DiasporaFederation.callbacks.trigger(:fetch_private_key_by_diaspora_id, diaspora_id) hash[:author_signature] = Signing.sign_with_key(hash, privkey) unless privkey.nil? @@ -59,7 +60,7 @@ module DiasporaFederation # @return [Nokogiri::XML::Element] root element containing properties as child elements def to_xml entity_xml.tap do |xml| - hash = to_signed_h + hash = to_h xml.at_xpath("author_signature").content = hash[:author_signature] xml.at_xpath("parent_author_signature").content = hash[:parent_author_signature] end diff --git a/lib/diaspora_federation/entities/relayable_retraction.rb b/lib/diaspora_federation/entities/relayable_retraction.rb index 894066f..98ae6c2 100644 --- a/lib/diaspora_federation/entities/relayable_retraction.rb +++ b/lib/diaspora_federation/entities/relayable_retraction.rb @@ -58,7 +58,7 @@ module DiasporaFederation # @return [Nokogiri::XML::Element] root element containing properties as child elements def to_xml entity_xml.tap do |xml| - hash = to_signed_h + hash = to_h xml.at_xpath("target_author_signature").content = hash[:target_author_signature] xml.at_xpath("parent_author_signature").content = hash[:parent_author_signature] end @@ -67,12 +67,13 @@ module DiasporaFederation # Adds signatures to the hash with the keys of the author and the parent # if the signatures are not in the hash yet and if the keys are available. # + # @see Entity#to_h # @return [Hash] entity data hash with updated signatures - def to_signed_h + def to_h target_author = DiasporaFederation.callbacks.trigger(:fetch_entity_author_id_by_guid, target_type, target_guid) privkey = DiasporaFederation.callbacks.trigger(:fetch_private_key_by_diaspora_id, diaspora_id) - to_h.tap do |hash| + super.tap do |hash| fill_required_signature(target_author, privkey, hash) unless privkey.nil? end end diff --git a/lib/diaspora_federation/entities/signed_retraction.rb b/lib/diaspora_federation/entities/signed_retraction.rb index 171bb6f..a6d89c3 100644 --- a/lib/diaspora_federation/entities/signed_retraction.rb +++ b/lib/diaspora_federation/entities/signed_retraction.rb @@ -35,16 +35,17 @@ module DiasporaFederation # @return [Nokogiri::XML::Element] root element containing properties as child elements def to_xml entity_xml.tap do |xml| - xml.at_xpath("target_author_signature").content = to_signed_h[:target_author_signature] + xml.at_xpath("target_author_signature").content = to_h[:target_author_signature] end end # Adds signature to the hash with the key of the author # if the signature is not in the hash yet and if the key is available. # + # @see Entity#to_h # @return [Hash] entity data hash with updated signatures - def to_signed_h - to_h.tap do |hash| + def to_h + super.tap do |hash| if target_author_signature.nil? privkey = DiasporaFederation.callbacks.trigger(:fetch_private_key_by_diaspora_id, diaspora_id) unless privkey.nil? diff --git a/lib/diaspora_federation/test.rb b/lib/diaspora_federation/test.rb index 6bdcd41..2bb9d5a 100644 --- a/lib/diaspora_federation/test.rb +++ b/lib/diaspora_federation/test.rb @@ -3,12 +3,5 @@ require "diaspora_federation/test/factories" module DiasporaFederation # This module encapsulates helper functions maybe wanted by a testsuite of a diaspora_federation gem user application module Test - # Generates attributes for entity constructor with correct signatures in it - # - # @param [Symbol] factory_name the factory to generate attributes for (normally entity name) - # @return [Hash] hash with correct signatures - def self.attributes_with_signatures(factory_name) - FactoryGirl.build(factory_name).to_signed_h - end end end diff --git a/spec/lib/diaspora_federation/entities/comment_spec.rb b/spec/lib/diaspora_federation/entities/comment_spec.rb index 59d189d..ff39430 100644 --- a/spec/lib/diaspora_federation/entities/comment_spec.rb +++ b/spec/lib/diaspora_federation/entities/comment_spec.rb @@ -1,6 +1,6 @@ module DiasporaFederation describe Entities::Comment do - let(:data) { Test.attributes_with_signatures(:comment_entity) } + let(:data) { FactoryGirl.build(:comment_entity).to_h } let(:xml) { <<-XML diff --git a/spec/lib/diaspora_federation/entities/conversation_spec.rb b/spec/lib/diaspora_federation/entities/conversation_spec.rb index 1839ca5..fd15d6f 100644 --- a/spec/lib/diaspora_federation/entities/conversation_spec.rb +++ b/spec/lib/diaspora_federation/entities/conversation_spec.rb @@ -1,7 +1,7 @@ module DiasporaFederation describe Entities::Conversation do - let(:msg1_data) { Test.attributes_with_signatures(:message_entity) } - let(:msg2_data) { Test.attributes_with_signatures(:message_entity) } + let(:msg1_data) { FactoryGirl.build(:message_entity).to_h } + let(:msg2_data) { FactoryGirl.build(:message_entity).to_h } let(:msg1) { FactoryGirl.build(:message_entity, msg1_data) } let(:msg2) { FactoryGirl.build(:message_entity, msg2_data) } let(:data) { diff --git a/spec/lib/diaspora_federation/entities/like_spec.rb b/spec/lib/diaspora_federation/entities/like_spec.rb index a74d889..5821f7e 100644 --- a/spec/lib/diaspora_federation/entities/like_spec.rb +++ b/spec/lib/diaspora_federation/entities/like_spec.rb @@ -1,6 +1,6 @@ module DiasporaFederation describe Entities::Like do - let(:data) { Test.attributes_with_signatures(:like_entity) } + let(:data) { FactoryGirl.build(:like_entity).to_h } let(:xml) { <<-XML diff --git a/spec/lib/diaspora_federation/entities/message_spec.rb b/spec/lib/diaspora_federation/entities/message_spec.rb index bf1501c..e181920 100644 --- a/spec/lib/diaspora_federation/entities/message_spec.rb +++ b/spec/lib/diaspora_federation/entities/message_spec.rb @@ -1,6 +1,6 @@ module DiasporaFederation describe Entities::Message do - let(:data) { Test.attributes_with_signatures(:message_entity) } + let(:data) { FactoryGirl.build(:message_entity).to_h } let(:xml) { <<-XML diff --git a/spec/lib/diaspora_federation/entities/participation_spec.rb b/spec/lib/diaspora_federation/entities/participation_spec.rb index ad53d44..cf41587 100644 --- a/spec/lib/diaspora_federation/entities/participation_spec.rb +++ b/spec/lib/diaspora_federation/entities/participation_spec.rb @@ -1,6 +1,6 @@ module DiasporaFederation describe Entities::Participation do - let(:data) { Test.attributes_with_signatures(:participation_entity) } + let(:data) { FactoryGirl.build(:participation_entity).to_h } let(:xml) { <<-XML diff --git a/spec/lib/diaspora_federation/entities/poll_participation_spec.rb b/spec/lib/diaspora_federation/entities/poll_participation_spec.rb index 3e30811..73ccc7f 100644 --- a/spec/lib/diaspora_federation/entities/poll_participation_spec.rb +++ b/spec/lib/diaspora_federation/entities/poll_participation_spec.rb @@ -1,6 +1,6 @@ module DiasporaFederation describe Entities::PollParticipation do - let(:data) { Test.attributes_with_signatures(:poll_participation_entity) } + let(:data) { FactoryGirl.build(:poll_participation_entity).to_h } let(:xml) { <<-XML diff --git a/spec/lib/diaspora_federation/entities/relayable_retraction_spec.rb b/spec/lib/diaspora_federation/entities/relayable_retraction_spec.rb index f35dfa5..edb03b7 100644 --- a/spec/lib/diaspora_federation/entities/relayable_retraction_spec.rb +++ b/spec/lib/diaspora_federation/entities/relayable_retraction_spec.rb @@ -1,6 +1,6 @@ module DiasporaFederation describe Entities::RelayableRetraction do - let(:data) { Test.attributes_with_signatures(:relayable_retraction_entity) } + let(:data) { FactoryGirl.build(:relayable_retraction_entity).to_h } let(:xml) { <<-XML @@ -18,7 +18,7 @@ XML it_behaves_like "an XML Entity" - describe "#to_signed_h" do + describe "#to_h" do let(:author_pkey) { OpenSSL::PKey::RSA.generate(1024) } let(:hash) { FactoryGirl.attributes_for(:relayable_retraction_entity) } @@ -31,7 +31,7 @@ XML :fetch_private_key_by_diaspora_id, hash[:diaspora_id] ).and_return(author_pkey) - signed_hash = Entities::RelayableRetraction.new(hash).to_signed_h + signed_hash = Entities::RelayableRetraction.new(hash).to_h signable_hash = hash.select do |key, _| %i(target_guid target_type).include?(key) @@ -48,7 +48,7 @@ XML :fetch_private_key_by_diaspora_id, hash[:diaspora_id] ).and_return(author_pkey) - signed_hash = Entities::RelayableRetraction.new(hash).to_signed_h + signed_hash = Entities::RelayableRetraction.new(hash).to_h signable_hash = hash.select do |key, _| %i(target_guid target_type).include?(key) @@ -59,7 +59,7 @@ XML it "doesn't change signatures if they are already set" do hash.merge!(target_author_signature: "aa", parent_author_signature: "bb") - expect(Entities::RelayableRetraction.new(hash).to_signed_h).to eq(hash) + expect(Entities::RelayableRetraction.new(hash).to_h).to eq(hash) end it "doesn't change signatures if keys weren't supplied" do @@ -71,7 +71,7 @@ XML :fetch_entity_author_id_by_guid, "Comment", hash[:target_guid] ).and_return(hash[:diaspora_id]) - signed_hash = Entities::RelayableRetraction.new(hash).to_signed_h + signed_hash = Entities::RelayableRetraction.new(hash).to_h expect(signed_hash[:target_author_signature]).to eq(nil) end end diff --git a/spec/lib/diaspora_federation/entities/relayable_spec.rb b/spec/lib/diaspora_federation/entities/relayable_spec.rb index 57f98ca..16cfbbd 100644 --- a/spec/lib/diaspora_federation/entities/relayable_spec.rb +++ b/spec/lib/diaspora_federation/entities/relayable_spec.rb @@ -119,7 +119,7 @@ module DiasporaFederation end end - describe "#to_signed_h" do + describe "#to_h" do it "updates signatures when they were nil and keys were supplied" do expect(DiasporaFederation.callbacks).to receive(:trigger).with( :fetch_private_key_by_diaspora_id, hash[:diaspora_id] @@ -129,7 +129,7 @@ module DiasporaFederation :fetch_author_private_key_by_entity_guid, "Target", hash[:parent_guid] ).and_return(parent_pkey) - signed_hash = SomeRelayable.new(hash).to_signed_h + signed_hash = SomeRelayable.new(hash).to_h expect(Signing.verify_signature(signed_hash, signed_hash[:author_signature], author_pkey)).to be_truthy expect(Signing.verify_signature(signed_hash, signed_hash[:parent_author_signature], parent_pkey)).to be_truthy @@ -138,7 +138,7 @@ module DiasporaFederation it "doesn't change signatures if they are already set" do hash.merge!(author_signature: "aa", parent_author_signature: "bb").delete(:some_other_data) - expect(SomeRelayable.new(hash).to_signed_h).to eq(hash) + expect(SomeRelayable.new(hash).to_h).to eq(hash) end it "doesn't change signatures if keys weren't supplied" do @@ -150,7 +150,7 @@ module DiasporaFederation :fetch_author_private_key_by_entity_guid, "Target", hash[:parent_guid] ).and_return(nil) - signed_hash = SomeRelayable.new(hash).to_signed_h + signed_hash = SomeRelayable.new(hash).to_h expect(signed_hash[:author_signature]).to eq(nil) expect(signed_hash[:parent_author_signature]).to eq(nil) diff --git a/spec/lib/diaspora_federation/entities/signed_retraction_spec.rb b/spec/lib/diaspora_federation/entities/signed_retraction_spec.rb index 78fdd81..a0fc78c 100644 --- a/spec/lib/diaspora_federation/entities/signed_retraction_spec.rb +++ b/spec/lib/diaspora_federation/entities/signed_retraction_spec.rb @@ -1,6 +1,6 @@ module DiasporaFederation describe Entities::SignedRetraction do - let(:data) { Test.attributes_with_signatures(:signed_retraction_entity) } + let(:data) { FactoryGirl.build(:signed_retraction_entity).to_h } let(:xml) { <<-XML @@ -17,7 +17,7 @@ XML it_behaves_like "an XML Entity" - describe "#to_signed_h" do + describe "#to_h" do let(:author_pkey) { OpenSSL::PKey::RSA.generate(1024) } let(:hash) { FactoryGirl.attributes_for(:signed_retraction_entity) } @@ -30,7 +30,7 @@ XML %i(target_guid target_type).include?(key) end - signed_hash = Entities::SignedRetraction.new(hash).to_signed_h + signed_hash = Entities::SignedRetraction.new(hash).to_h expect(Signing.verify_signature(signable_hash, signed_hash[:target_author_signature], author_pkey)).to be_truthy end @@ -38,7 +38,7 @@ XML it "doesn't change signature if it is already set" do hash[:target_author_signature] = "aa" - expect(Entities::SignedRetraction.new(hash).to_signed_h).to eq(hash) + expect(Entities::SignedRetraction.new(hash).to_h).to eq(hash) end it "doesn't change signature if a key wasn't supplied" do @@ -46,7 +46,7 @@ XML :fetch_private_key_by_diaspora_id, hash[:diaspora_id] ).and_return(nil) - signed_hash = Entities::SignedRetraction.new(hash).to_signed_h + signed_hash = Entities::SignedRetraction.new(hash).to_h expect(signed_hash[:author_signature]).to eq(nil) end end