override to_h to sign relayables

This commit is contained in:
Benjamin Neff 2016-01-21 23:40:36 +01:00
parent 1aead0ac5b
commit 8bd9c28ce0
13 changed files with 34 additions and 38 deletions

View file

@ -37,9 +37,10 @@ module DiasporaFederation
# Adds signatures to the hash with the keys of the author and the parent # 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. # 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 # @return [Hash] entity data hash with updated signatures
def to_signed_h def to_h
to_h.tap do |hash| super.tap do |hash|
if author_signature.nil? if 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)
hash[:author_signature] = Signing.sign_with_key(hash, privkey) unless privkey.nil? 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 # @return [Nokogiri::XML::Element] root element containing properties as child elements
def to_xml def to_xml
entity_xml.tap do |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("author_signature").content = hash[:author_signature]
xml.at_xpath("parent_author_signature").content = hash[:parent_author_signature] xml.at_xpath("parent_author_signature").content = hash[:parent_author_signature]
end end

View file

@ -58,7 +58,7 @@ module DiasporaFederation
# @return [Nokogiri::XML::Element] root element containing properties as child elements # @return [Nokogiri::XML::Element] root element containing properties as child elements
def to_xml def to_xml
entity_xml.tap do |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("target_author_signature").content = hash[:target_author_signature]
xml.at_xpath("parent_author_signature").content = hash[:parent_author_signature] xml.at_xpath("parent_author_signature").content = hash[:parent_author_signature]
end end
@ -67,12 +67,13 @@ module DiasporaFederation
# Adds signatures to the hash with the keys of the author and the parent # 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. # 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 # @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) 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) 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? fill_required_signature(target_author, privkey, hash) unless privkey.nil?
end end
end end

View file

@ -35,16 +35,17 @@ module DiasporaFederation
# @return [Nokogiri::XML::Element] root element containing properties as child elements # @return [Nokogiri::XML::Element] root element containing properties as child elements
def to_xml def to_xml
entity_xml.tap do |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
end end
# Adds signature to the hash with the key of the author # 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. # 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 # @return [Hash] entity data hash with updated signatures
def to_signed_h def to_h
to_h.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? unless privkey.nil?

View file

@ -3,12 +3,5 @@ require "diaspora_federation/test/factories"
module DiasporaFederation module DiasporaFederation
# This module encapsulates helper functions maybe wanted by a testsuite of a diaspora_federation gem user application # This module encapsulates helper functions maybe wanted by a testsuite of a diaspora_federation gem user application
module Test 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
end end

View file

@ -1,6 +1,6 @@
module DiasporaFederation module DiasporaFederation
describe Entities::Comment do describe Entities::Comment do
let(:data) { Test.attributes_with_signatures(:comment_entity) } let(:data) { FactoryGirl.build(:comment_entity).to_h }
let(:xml) { let(:xml) {
<<-XML <<-XML

View file

@ -1,7 +1,7 @@
module DiasporaFederation module DiasporaFederation
describe Entities::Conversation do describe Entities::Conversation do
let(:msg1_data) { Test.attributes_with_signatures(:message_entity) } let(:msg1_data) { FactoryGirl.build(:message_entity).to_h }
let(:msg2_data) { Test.attributes_with_signatures(:message_entity) } let(:msg2_data) { FactoryGirl.build(:message_entity).to_h }
let(:msg1) { FactoryGirl.build(:message_entity, msg1_data) } let(:msg1) { FactoryGirl.build(:message_entity, msg1_data) }
let(:msg2) { FactoryGirl.build(:message_entity, msg2_data) } let(:msg2) { FactoryGirl.build(:message_entity, msg2_data) }
let(:data) { let(:data) {

View file

@ -1,6 +1,6 @@
module DiasporaFederation module DiasporaFederation
describe Entities::Like do describe Entities::Like do
let(:data) { Test.attributes_with_signatures(:like_entity) } let(:data) { FactoryGirl.build(:like_entity).to_h }
let(:xml) { let(:xml) {
<<-XML <<-XML

View file

@ -1,6 +1,6 @@
module DiasporaFederation module DiasporaFederation
describe Entities::Message do describe Entities::Message do
let(:data) { Test.attributes_with_signatures(:message_entity) } let(:data) { FactoryGirl.build(:message_entity).to_h }
let(:xml) { let(:xml) {
<<-XML <<-XML

View file

@ -1,6 +1,6 @@
module DiasporaFederation module DiasporaFederation
describe Entities::Participation do describe Entities::Participation do
let(:data) { Test.attributes_with_signatures(:participation_entity) } let(:data) { FactoryGirl.build(:participation_entity).to_h }
let(:xml) { let(:xml) {
<<-XML <<-XML

View file

@ -1,6 +1,6 @@
module DiasporaFederation module DiasporaFederation
describe Entities::PollParticipation do describe Entities::PollParticipation do
let(:data) { Test.attributes_with_signatures(:poll_participation_entity) } let(:data) { FactoryGirl.build(:poll_participation_entity).to_h }
let(:xml) { let(:xml) {
<<-XML <<-XML

View file

@ -1,6 +1,6 @@
module DiasporaFederation module DiasporaFederation
describe Entities::RelayableRetraction do describe Entities::RelayableRetraction do
let(:data) { Test.attributes_with_signatures(:relayable_retraction_entity) } let(:data) { FactoryGirl.build(:relayable_retraction_entity).to_h }
let(:xml) { let(:xml) {
<<-XML <<-XML
@ -18,7 +18,7 @@ XML
it_behaves_like "an XML Entity" it_behaves_like "an XML Entity"
describe "#to_signed_h" do describe "#to_h" do
let(:author_pkey) { OpenSSL::PKey::RSA.generate(1024) } let(:author_pkey) { OpenSSL::PKey::RSA.generate(1024) }
let(:hash) { FactoryGirl.attributes_for(:relayable_retraction_entity) } let(:hash) { FactoryGirl.attributes_for(:relayable_retraction_entity) }
@ -31,7 +31,7 @@ XML
:fetch_private_key_by_diaspora_id, hash[:diaspora_id] :fetch_private_key_by_diaspora_id, hash[:diaspora_id]
).and_return(author_pkey) ).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, _| signable_hash = hash.select do |key, _|
%i(target_guid target_type).include?(key) %i(target_guid target_type).include?(key)
@ -48,7 +48,7 @@ XML
:fetch_private_key_by_diaspora_id, hash[:diaspora_id] :fetch_private_key_by_diaspora_id, hash[:diaspora_id]
).and_return(author_pkey) ).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, _| signable_hash = hash.select do |key, _|
%i(target_guid target_type).include?(key) %i(target_guid target_type).include?(key)
@ -59,7 +59,7 @@ XML
it "doesn't change signatures if they are already set" do it "doesn't change signatures if they are already set" do
hash.merge!(target_author_signature: "aa", parent_author_signature: "bb") 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 end
it "doesn't change signatures if keys weren't supplied" do 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] :fetch_entity_author_id_by_guid, "Comment", hash[:target_guid]
).and_return(hash[:diaspora_id]) ).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) expect(signed_hash[:target_author_signature]).to eq(nil)
end end
end end

View file

@ -119,7 +119,7 @@ module DiasporaFederation
end end
end end
describe "#to_signed_h" do describe "#to_h" do
it "updates signatures when they were nil and keys were supplied" do it "updates signatures when they were nil and keys were supplied" do
expect(DiasporaFederation.callbacks).to receive(:trigger).with( expect(DiasporaFederation.callbacks).to receive(:trigger).with(
:fetch_private_key_by_diaspora_id, hash[:diaspora_id] :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] :fetch_author_private_key_by_entity_guid, "Target", hash[:parent_guid]
).and_return(parent_pkey) ).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[:author_signature], author_pkey)).to be_truthy
expect(Signing.verify_signature(signed_hash, signed_hash[:parent_author_signature], parent_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 it "doesn't change signatures if they are already set" do
hash.merge!(author_signature: "aa", parent_author_signature: "bb").delete(:some_other_data) 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 end
it "doesn't change signatures if keys weren't supplied" do 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] :fetch_author_private_key_by_entity_guid, "Target", hash[:parent_guid]
).and_return(nil) ).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[:author_signature]).to eq(nil)
expect(signed_hash[:parent_author_signature]).to eq(nil) expect(signed_hash[:parent_author_signature]).to eq(nil)

View file

@ -1,6 +1,6 @@
module DiasporaFederation module DiasporaFederation
describe Entities::SignedRetraction do describe Entities::SignedRetraction do
let(:data) { Test.attributes_with_signatures(:signed_retraction_entity) } let(:data) { FactoryGirl.build(:signed_retraction_entity).to_h }
let(:xml) { let(:xml) {
<<-XML <<-XML
@ -17,7 +17,7 @@ XML
it_behaves_like "an XML Entity" it_behaves_like "an XML Entity"
describe "#to_signed_h" do describe "#to_h" do
let(:author_pkey) { OpenSSL::PKey::RSA.generate(1024) } let(:author_pkey) { OpenSSL::PKey::RSA.generate(1024) }
let(:hash) { FactoryGirl.attributes_for(:signed_retraction_entity) } let(:hash) { FactoryGirl.attributes_for(:signed_retraction_entity) }
@ -30,7 +30,7 @@ XML
%i(target_guid target_type).include?(key) %i(target_guid target_type).include?(key)
end 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 expect(Signing.verify_signature(signable_hash, signed_hash[:target_author_signature], author_pkey)).to be_truthy
end end
@ -38,7 +38,7 @@ XML
it "doesn't change signature if it is already set" do it "doesn't change signature if it is already set" do
hash[:target_author_signature] = "aa" 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 end
it "doesn't change signature if a key wasn't supplied" do 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] :fetch_private_key_by_diaspora_id, hash[:diaspora_id]
).and_return(nil) ).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) expect(signed_hash[:author_signature]).to eq(nil)
end end
end end