move parse logic to receive methods

This commit is contained in:
Benjamin Neff 2016-03-20 17:39:41 +01:00
parent 3a83dc97ac
commit 3811cd0c03
10 changed files with 16 additions and 248 deletions

View file

@ -6,8 +6,13 @@ module DiasporaFederation
# @param [String] data message to receive
# @param [Boolean] legacy use old slap parser
def self.receive_public(data, legacy=false)
receiver = legacy ? PublicSlapReceiver.new(data) : MagicEnvelopeReceiver.new(data)
receive(receiver)
received_message = if legacy
Salmon::Slap.from_xml(data).entity
else
magic_env_xml = Nokogiri::XML::Document.parse(data).root
Salmon::MagicEnvelope.unenvelop(magic_env_xml)
end
receive(received_message)
end
# receive a private message
@ -18,25 +23,19 @@ module DiasporaFederation
# @param [Boolean] legacy use old slap parser
def self.receive_private(data, recipient_private_key, recipient_id, legacy=false)
raise ArgumentError, "no recipient key provided" unless recipient_private_key.instance_of?(OpenSSL::PKey::RSA)
receiver = if legacy
PrivateSlapReceiver.new(data, recipient_private_key)
else
EncryptedMagicEnvelopeReceiver.new(data, recipient_private_key)
end
receive(receiver, recipient_id)
received_message = if legacy
Salmon::EncryptedSlap.from_xml(data, recipient_private_key).entity
else
magic_env_xml = Salmon::EncryptedMagicEnvelope.decrypt(data, recipient_private_key)
Salmon::MagicEnvelope.unenvelop(magic_env_xml)
end
receive(received_message, recipient_id)
end
def self.receive(receiver, recipient_id=nil)
entity = receiver.parse
DiasporaFederation.callbacks.trigger(:receive_entity, entity, recipient_id)
def self.receive(received_message, recipient_id=nil)
DiasporaFederation.callbacks.trigger(:receive_entity, received_message, recipient_id)
end
private_class_method :receive
end
end
end
require "diaspora_federation/federation/receiver/slap_receiver"
require "diaspora_federation/federation/receiver/private_slap_receiver"
require "diaspora_federation/federation/receiver/public_slap_receiver"
require "diaspora_federation/federation/receiver/magic_envelope_receiver"
require "diaspora_federation/federation/receiver/encrypted_magic_envelope_receiver"

View file

@ -1,24 +0,0 @@
module DiasporaFederation
module Federation
module Receiver
# Receiver for an encrypted magic envelope
#
# @see Salmon::EncryptedMagicEnvelope
class EncryptedMagicEnvelopeReceiver < MagicEnvelopeReceiver
# create a new receiver for an encrypted magic envelope
# @param [String] data the encrypted json with magic envelope xml
# @param [OpenSSL::PKey::RSA] recipient_private_key recipient private key to decrypt the message
def initialize(data, recipient_private_key)
super(data)
@recipient_private_key = recipient_private_key
end
protected
def magic_env_xml
Salmon::EncryptedMagicEnvelope.decrypt(@data, @recipient_private_key)
end
end
end
end
end

View file

@ -1,28 +0,0 @@
module DiasporaFederation
module Federation
module Receiver
# Receiver for a magic envelope
#
# @see Salmon::MagicEnvelope
class MagicEnvelopeReceiver
# create a new receiver for a magic envelope
# @param [String] data the message magic envelope xml
def initialize(data)
@data = data
end
# parse the magic envelope and create the entity
# @return [Entity] the parsed entity
def parse
Salmon::MagicEnvelope.unenvelop(magic_env_xml)
end
protected
def magic_env_xml
Nokogiri::XML::Document.parse(@data).root
end
end
end
end
end

View file

@ -1,26 +0,0 @@
module DiasporaFederation
module Federation
module Receiver
# This is used to receive private messages, which are addressed to a specific user,
# encrypted with his public key and packed using {Salmon::EncryptedSlap}.
# @deprecated
class PrivateSlapReceiver < SlapReceiver
# initializes a new Private Receiver for a salmon slap XML
# @param [String] slap_xml the message salmon slap xml
# @param [OpenSSL::PKey::RSA] recipient_private_key recipient private key to decrypt the message
def initialize(slap_xml, recipient_private_key)
super(slap_xml)
@recipient_private_key = recipient_private_key
end
protected
# parses the encrypted slap xml
# @return [Salmon::EncryptedSlap] slap instance
def slap
@slap ||= Salmon::EncryptedSlap.from_xml(@slap_xml, @recipient_private_key)
end
end
end
end
end

View file

@ -1,18 +0,0 @@
module DiasporaFederation
module Federation
module Receiver
# This is used to receive public messages, which are not addressed to
# a specific user, unencrypted and packed using {Salmon::Slap}.
# @deprecated
class PublicSlapReceiver < SlapReceiver
protected
# parses the public slap xml
# @return [Salmon::Slap] slap instance
def slap
@slap ||= Salmon::Slap.from_xml(@slap_xml)
end
end
end
end
end

View file

@ -1,22 +0,0 @@
module DiasporaFederation
module Federation
module Receiver
# Common base for Private and Public receivers
# @see PublicSlapReceiver
# @see PrivateSlapReceiver
# @deprecated
class SlapReceiver
# initializes a new SlapReceiver for a salmon slap XML
# @param [String] slap_xml the message salmon xml
def initialize(slap_xml)
@slap_xml = slap_xml
end
# Parse the salmon xml
def parse
slap.entity
end
end
end
end
end

View file

@ -1,21 +0,0 @@
module DiasporaFederation
describe Federation::Receiver::EncryptedMagicEnvelopeReceiver do
let(:sender_id) { FactoryGirl.generate(:diaspora_id) }
let(:sender_key) { OpenSSL::PKey::RSA.generate(1024) }
let(:recipient_key) { OpenSSL::PKey::RSA.generate(1024) }
let(:entity) { FactoryGirl.build(:status_message_entity, public: false) }
let(:magic_env) { Salmon::MagicEnvelope.new(entity, sender_id).envelop(sender_key) }
let(:data) { Salmon::EncryptedMagicEnvelope.encrypt(magic_env, recipient_key.public_key) }
it "parses the entity if everything is fine" do
expect(DiasporaFederation.callbacks).to receive(:trigger).with(
:fetch_public_key_by_diaspora_id, sender_id
).and_return(sender_key)
parsed_entity = described_class.new(data, recipient_key).parse
expect(parsed_entity).to be_a(Entities::StatusMessage)
expect(parsed_entity.guid).to eq(entity.guid)
expect(parsed_entity.public).to eq("false")
end
end
end

View file

@ -1,19 +0,0 @@
module DiasporaFederation
describe Federation::Receiver::MagicEnvelopeReceiver do
let(:sender_id) { FactoryGirl.generate(:diaspora_id) }
let(:sender_key) { OpenSSL::PKey::RSA.generate(1024) }
let(:entity) { FactoryGirl.build(:status_message_entity) }
let(:data) { Salmon::MagicEnvelope.new(entity, sender_id).envelop(sender_key).to_xml }
it "parses the entity if everything is fine" do
expect(DiasporaFederation.callbacks).to receive(:trigger).with(
:fetch_public_key_by_diaspora_id, sender_id
).and_return(sender_key)
parsed_entity = described_class.new(data).parse
expect(parsed_entity).to be_a(Entities::StatusMessage)
expect(parsed_entity.guid).to eq(entity.guid)
expect(parsed_entity.public).to eq("true")
end
end
end

View file

@ -1,38 +0,0 @@
module DiasporaFederation
describe Federation::Receiver::PrivateSlapReceiver do
let(:sender_id) { FactoryGirl.generate(:diaspora_id) }
let(:sender_key) { OpenSSL::PKey::RSA.generate(1024) }
let(:recipient_key) { OpenSSL::PKey::RSA.generate(1024) }
let(:entity) { FactoryGirl.build(:status_message_entity, public: false) }
let(:xml) {
DiasporaFederation::Salmon::EncryptedSlap.prepare(sender_id, sender_key, entity).generate_xml(recipient_key)
}
it "parses the entity if everything is fine" do
expect(DiasporaFederation.callbacks).to receive(:trigger).with(
:fetch_public_key_by_diaspora_id, sender_id
).and_return(sender_key)
parsed_entity = described_class.new(xml, recipient_key).parse
expect(parsed_entity).to be_a(Entities::StatusMessage)
expect(parsed_entity.guid).to eq(entity.guid)
expect(parsed_entity.public).to eq("false")
end
it "raises when sender public key is not available" do
expect(DiasporaFederation.callbacks).to receive(:trigger).with(
:fetch_public_key_by_diaspora_id, sender_id
).and_return(nil)
expect {
described_class.new(xml, recipient_key).parse
}.to raise_error Salmon::SenderKeyNotFound
end
it "raises when bad xml was supplied" do
expect {
described_class.new("<XML/>", recipient_key).parse
}.to raise_error Salmon::MissingHeader
end
end
end

View file

@ -1,35 +0,0 @@
module DiasporaFederation
describe Federation::Receiver::PublicSlapReceiver do
let(:sender_id) { FactoryGirl.generate(:diaspora_id) }
let(:sender_key) { OpenSSL::PKey::RSA.generate(1024) }
let(:entity) { FactoryGirl.build(:status_message_entity) }
let(:xml) { DiasporaFederation::Salmon::Slap.generate_xml(sender_id, sender_key, entity) }
it "parses the entity if everything is fine" do
expect(DiasporaFederation.callbacks).to receive(:trigger).with(
:fetch_public_key_by_diaspora_id, sender_id
).and_return(sender_key)
parsed_entity = described_class.new(xml).parse
expect(parsed_entity).to be_a(Entities::StatusMessage)
expect(parsed_entity.guid).to eq(entity.guid)
expect(parsed_entity.public).to eq("true")
end
it "raises when sender public key is not available" do
expect(DiasporaFederation.callbacks).to receive(:trigger).with(
:fetch_public_key_by_diaspora_id, sender_id
).and_return(nil)
expect {
described_class.new(xml).parse
}.to raise_error Salmon::SenderKeyNotFound
end
it "raises when bad xml was supplied" do
expect {
described_class.new("<XML/>").parse
}.to raise_error Salmon::MissingAuthor
end
end
end