From 46bf0cfd99e0504122e3caeb9065848119870b4b Mon Sep 17 00:00:00 2001 From: ilya Date: Tue, 28 Sep 2010 15:11:01 -0700 Subject: [PATCH] roundtrip done, just need to clean and move to the new api --- lib/salmon/salmon.rb | 43 ++++++++++++++++++++++++++++++++-- spec/lib/salmon_salmon_spec.rb | 42 +++++++++++++++++++++++++++++---- 2 files changed, 79 insertions(+), 6 deletions(-) diff --git a/lib/salmon/salmon.rb b/lib/salmon/salmon.rb index 82893a59d..bd2c0b1b9 100644 --- a/lib/salmon/salmon.rb +++ b/lib/salmon/salmon.rb @@ -54,15 +54,26 @@ module Salmon salmon end - def self.parse(xml) + def self.parse(xml, user) slap = self.new doc = Nokogiri::XML(xml) sig_doc = doc.search('entry') + + ### Header ## + decrypted_header = user.decrypt(doc.search('encrypted_header').text) + puts decrypted_header + header_doc = Nokogiri::XML(decrypted_header) + puts header_doc.inspect + slap.aes_key = header_doc.search('aes_key').text + slap.iv = header_doc.search('iv').text + slap.magic_sig = MagicSigEnvelope.parse sig_doc if 'base64url' == slap.magic_sig.encoding - slap.parsed_data = decode64url(slap.magic_sig.data) + + key_hash = {'key' => slap.aes_key, 'iv' => slap.iv} + slap.parsed_data = user.aes_decrypt(decode64url(slap.magic_sig.data), key_hash) slap.sig = slap.magic_sig.sig else raise ArgumentError, "Magic Signature data must be encoded with base64url, was #{slap.magic_sig.encoding}" @@ -91,6 +102,34 @@ ENTRY end + def xml_for person + xml =< + + #{person.encrypt(decrypted_header.rstrip)} + + #{@author.real_name} + acct:#{@author.diaspora_handle} + + #{@magic_sig.to_xml} + +ENTRY + + end + + def decrypted_header + header =<
+ #{iv} + #{aes_key} + + #{@author.real_name} + acct:#{@author.diaspora_handle} + +
+HEADER + end + def author if @author @author diff --git a/spec/lib/salmon_salmon_spec.rb b/spec/lib/salmon_salmon_spec.rb index ef24b5037..3ea98afe0 100644 --- a/spec/lib/salmon_salmon_spec.rb +++ b/spec/lib/salmon_salmon_spec.rb @@ -6,10 +6,12 @@ require 'spec_helper' describe Salmon do let(:user){Factory.create :user} + let(:user2) {Factory.create :user} let(:post){ user.post :status_message, :message => "hi", :to => user.aspect(:name => "sdg").id } + let!(:created_salmon) {Salmon::SalmonSlap.create(user, post.to_diaspora_xml)} + describe '#create' do - let!(:created_salmon) {Salmon::SalmonSlap.create(user, post.to_diaspora_xml)} it 'has data in the magic envelope' do created_salmon.magic_sig.data.should_not be nil @@ -24,17 +26,49 @@ describe Salmon do created_salmon.iv.should_not be nil end - it 'should make the data in the signature encrypted with that key' do + it 'makes the data in the signature encrypted with that key' do key_hash = {'key' => created_salmon.aes_key, 'iv' => created_salmon.iv} decoded_string = Salmon::SalmonSlap.decode64url(created_salmon.magic_sig.data) - user.aes_decrypt(decoded_string, key_hash).to_s.should == post.to_diaspora_xml.to_s + user.aes_decrypt(decoded_string, key_hash).should == post.to_diaspora_xml end end + + describe '#xml_for' do + let(:xml) {created_salmon.xml_for user2.person} + + it 'has a encrypted header field' do + xml.include?("encrypted_header").should be true + end + + it 'the encrypted_header field should contain the aes key' do + doc = Nokogiri::XML(xml) + decrypted_header = user2.decrypt(doc.search('encrypted_header').text) + decrypted_header.include?(created_salmon.aes_key).should be true + end + end + + context 'marshaling' do + let(:xml) {created_salmon.xml_for user2.person} + let(:parsed_salmon) { Salmon::SalmonSlap.parse(xml, user2)} + + it 'should parse out the aes key' do + parsed_salmon.aes_key.should == created_salmon.aes_key + end + + it 'should parse out the iv' do + parsed_salmon.iv.should == created_salmon.iv + end + + it 'contains the original data' do + parsed_salmon.parsed_data.should == post.to_diaspora_xml + end + + end context 'round trip' do before do @sent_salmon = Salmon::SalmonSlap.create(user, post.to_diaspora_xml) - @parsed_salmon = Salmon::SalmonSlap.parse @sent_salmon.to_xml + @parsed_salmon = stub_success("tom@tom.joindiaspora.com") end