create MagicEnvelope instance after unenvelop

This commit is contained in:
Benjamin Neff 2016-03-20 20:18:50 +01:00
parent 0990eff69c
commit e4b5e79842
5 changed files with 46 additions and 24 deletions

View file

@ -11,9 +11,9 @@ module DiasporaFederation
response = HttpClient.get(url) response = HttpClient.get(url)
raise "Failed to fetch #{url}: #{response.status}" unless response.success? raise "Failed to fetch #{url}: #{response.status}" unless response.success?
magic_env = Nokogiri::XML::Document.parse(response.body).root magic_env_xml = Nokogiri::XML::Document.parse(response.body).root
entity = Salmon::MagicEnvelope.unenvelop(magic_env) magic_env = Salmon::MagicEnvelope.unenvelop(magic_env_xml)
DiasporaFederation.callbacks.trigger(:receive_entity, entity) DiasporaFederation.callbacks.trigger(:receive_entity, magic_env.payload)
rescue => e rescue => e
raise NotFetchable, "Failed to fetch #{entity_type}:#{guid} from #{author}: #{e.class}: #{e.message}" raise NotFetchable, "Failed to fetch #{entity_type}:#{guid} from #{author}: #{e.class}: #{e.message}"
end end

View file

@ -10,7 +10,7 @@ module DiasporaFederation
Salmon::Slap.from_xml(data).entity Salmon::Slap.from_xml(data).entity
else else
magic_env_xml = Nokogiri::XML::Document.parse(data).root magic_env_xml = Nokogiri::XML::Document.parse(data).root
Salmon::MagicEnvelope.unenvelop(magic_env_xml) Salmon::MagicEnvelope.unenvelop(magic_env_xml).payload
end end
receive(received_message) receive(received_message)
end end
@ -27,7 +27,7 @@ module DiasporaFederation
Salmon::EncryptedSlap.from_xml(data, recipient_private_key).entity Salmon::EncryptedSlap.from_xml(data, recipient_private_key).entity
else else
magic_env_xml = Salmon::EncryptedMagicEnvelope.decrypt(data, recipient_private_key) magic_env_xml = Salmon::EncryptedMagicEnvelope.decrypt(data, recipient_private_key)
Salmon::MagicEnvelope.unenvelop(magic_env_xml) Salmon::MagicEnvelope.unenvelop(magic_env_xml).payload
end end
receive(received_message, recipient_id) receive(received_message, recipient_id)
end end

View file

@ -114,6 +114,8 @@ module DiasporaFederation
raise ArgumentError unless magic_env.instance_of?(Nokogiri::XML::Element) raise ArgumentError unless magic_env.instance_of?(Nokogiri::XML::Element)
raise InvalidEnvelope unless envelope_valid?(magic_env) raise InvalidEnvelope unless envelope_valid?(magic_env)
sender ||= sender(magic_env)
raise InvalidSignature unless signature_valid?(magic_env, sender) raise InvalidSignature unless signature_valid?(magic_env, sender)
raise InvalidEncoding unless encoding_valid?(magic_env) raise InvalidEncoding unless encoding_valid?(magic_env)
@ -121,7 +123,7 @@ module DiasporaFederation
data = read_and_decrypt_data(magic_env, cipher_params) data = read_and_decrypt_data(magic_env, cipher_params)
XmlPayload.unpack(Nokogiri::XML::Document.parse(data).root) new(XmlPayload.unpack(Nokogiri::XML::Document.parse(data).root), sender)
end end
private private
@ -169,8 +171,6 @@ module DiasporaFederation
# @param [String] sender diaspora-ID of the sender or nil # @param [String] sender diaspora-ID of the sender or nil
# @return [Boolean] # @return [Boolean]
def self.signature_valid?(env, sender) def self.signature_valid?(env, sender)
sender ||= sender(env)
subject = sig_subject([Base64.urlsafe_decode64(env.at_xpath("me:data").content), subject = sig_subject([Base64.urlsafe_decode64(env.at_xpath("me:data").content),
env.at_xpath("me:data")["type"], env.at_xpath("me:data")["type"],
env.at_xpath("me:encoding").content, env.at_xpath("me:encoding").content,

View file

@ -20,13 +20,15 @@ module DiasporaFederation
:fetch_public_key_by_diaspora_id, alice.diaspora_id :fetch_public_key_by_diaspora_id, alice.diaspora_id
).and_return(alice.public_key) ).and_return(alice.public_key)
magic_env = Nokogiri::XML::Document.parse(response.body).root magic_env_xml = Nokogiri::XML::Document.parse(response.body).root
entity = Salmon::MagicEnvelope.unenvelop(magic_env) magic_env = Salmon::MagicEnvelope.unenvelop(magic_env_xml)
entity = magic_env.payload
expect(entity).to be_a(Entities::StatusMessage) expect(entity).to be_a(Entities::StatusMessage)
expect(entity.guid).to eq(guid) expect(entity.guid).to eq(guid)
expect(entity.author).to eq(alice.diaspora_id) expect(entity.author).to eq(alice.diaspora_id)
expect(entity.raw_message).to eq(post.raw_message) expect(entity.raw_message).to eq(post.raw_message)
expect(magic_env.sender).to eq(alice.diaspora_id)
end end
it "works with type 'post'" do it "works with type 'post'" do
@ -43,13 +45,15 @@ module DiasporaFederation
:fetch_public_key_by_diaspora_id, alice.diaspora_id :fetch_public_key_by_diaspora_id, alice.diaspora_id
).and_return(alice.public_key) ).and_return(alice.public_key)
magic_env = Nokogiri::XML::Document.parse(response.body).root magic_env_xml = Nokogiri::XML::Document.parse(response.body).root
entity = Salmon::MagicEnvelope.unenvelop(magic_env) magic_env = Salmon::MagicEnvelope.unenvelop(magic_env_xml)
entity = magic_env.payload
expect(entity).to be_a(Entities::StatusMessage) expect(entity).to be_a(Entities::StatusMessage)
expect(entity.guid).to eq(guid) expect(entity.guid).to eq(guid)
expect(entity.author).to eq(alice.diaspora_id) expect(entity.author).to eq(alice.diaspora_id)
expect(entity.raw_message).to eq(post.raw_message) expect(entity.raw_message).to eq(post.raw_message)
expect(magic_env.sender).to eq(alice.diaspora_id)
end end
it "redirects when the entity is from another pod" do it "redirects when the entity is from another pod" do

View file

@ -172,9 +172,18 @@ module DiasporaFederation
:fetch_public_key_by_diaspora_id, sender :fetch_public_key_by_diaspora_id, sender
).and_return(privkey.public_key) ).and_return(privkey.public_key)
entity = Salmon::MagicEnvelope.unenvelop(envelope.envelop(privkey), sender) magic_env = Salmon::MagicEnvelope.unenvelop(envelope.envelop(privkey), sender)
expect(entity).to be_an_instance_of Entities::TestEntity expect(magic_env.payload).to be_an_instance_of Entities::TestEntity
expect(entity.test).to eq("asdf") expect(magic_env.payload.test).to eq("asdf")
end
it "returns the original sender" do
allow(DiasporaFederation.callbacks).to receive(:trigger).with(
:fetch_public_key_by_diaspora_id, sender
).and_return(privkey.public_key)
magic_env = Salmon::MagicEnvelope.unenvelop(envelope.envelop(privkey), sender)
expect(magic_env.sender).to eq(sender)
end end
it "decrypts on the fly, when cipher params are present" do it "decrypts on the fly, when cipher params are present" do
@ -182,13 +191,13 @@ module DiasporaFederation
:fetch_public_key_by_diaspora_id, sender :fetch_public_key_by_diaspora_id, sender
).and_return(privkey.public_key) ).and_return(privkey.public_key)
params = envelope.encrypt! env = Salmon::MagicEnvelope.new(payload)
params = env.encrypt!
env_xml = env.envelop(privkey)
env_xml = envelope.envelop(privkey) magic_env = Salmon::MagicEnvelope.unenvelop(env_xml, sender, params)
expect(magic_env.payload).to be_an_instance_of Entities::TestEntity
entity = Salmon::MagicEnvelope.unenvelop(env_xml, sender, params) expect(magic_env.payload.test).to eq("asdf")
expect(entity).to be_an_instance_of Entities::TestEntity
expect(entity.test).to eq("asdf")
end end
context "use key_id from magic envelope" do context "use key_id from magic envelope" do
@ -197,9 +206,18 @@ module DiasporaFederation
:fetch_public_key_by_diaspora_id, sender :fetch_public_key_by_diaspora_id, sender
).and_return(privkey.public_key) ).and_return(privkey.public_key)
entity = Salmon::MagicEnvelope.unenvelop(envelope.envelop(privkey)) magic_env = Salmon::MagicEnvelope.unenvelop(envelope.envelop(privkey))
expect(entity).to be_an_instance_of Entities::TestEntity expect(magic_env.payload).to be_an_instance_of Entities::TestEntity
expect(entity.test).to eq("asdf") expect(magic_env.payload.test).to eq("asdf")
end
it "returns the original sender" do
expect(DiasporaFederation.callbacks).to receive(:trigger).with(
:fetch_public_key_by_diaspora_id, sender
).and_return(privkey.public_key)
magic_env = Salmon::MagicEnvelope.unenvelop(envelope.envelop(privkey))
expect(magic_env.sender).to eq(sender)
end end
it "raises if the magic envelope has no key_id" do it "raises if the magic envelope has no key_id" do