diff --git a/lib/diaspora_federation/entities/relayable_retraction.rb b/lib/diaspora_federation/entities/relayable_retraction.rb index 98ae6c2..fcf3b29 100644 --- a/lib/diaspora_federation/entities/relayable_retraction.rb +++ b/lib/diaspora_federation/entities/relayable_retraction.rb @@ -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 diff --git a/lib/diaspora_federation/entities/signed_retraction.rb b/lib/diaspora_federation/entities/signed_retraction.rb index a6d89c3..9e16495 100644 --- a/lib/diaspora_federation/entities/signed_retraction.rb +++ b/lib/diaspora_federation/entities/signed_retraction.rb @@ -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 diff --git a/spec/lib/diaspora_federation/entities/relayable_retraction_spec.rb b/spec/lib/diaspora_federation/entities/relayable_retraction_spec.rb index edb03b7..18aba1b 100644 --- a/spec/lib/diaspora_federation/entities/relayable_retraction_spec.rb +++ b/spec/lib/diaspora_federation/entities/relayable_retraction_spec.rb @@ -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 diff --git a/spec/lib/diaspora_federation/entities/signed_retraction_spec.rb b/spec/lib/diaspora_federation/entities/signed_retraction_spec.rb index a0fc78c..f33da7c 100644 --- a/spec/lib/diaspora_federation/entities/signed_retraction_spec.rb +++ b/spec/lib/diaspora_federation/entities/signed_retraction_spec.rb @@ -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