roundtrip done, just need to clean and move to the new api

This commit is contained in:
ilya 2010-09-28 15:11:01 -07:00
parent c7d29b29c8
commit 46bf0cfd99
2 changed files with 79 additions and 6 deletions

View file

@ -54,15 +54,26 @@ module Salmon
salmon salmon
end end
def self.parse(xml) def self.parse(xml, user)
slap = self.new slap = self.new
doc = Nokogiri::XML(xml) doc = Nokogiri::XML(xml)
sig_doc = doc.search('entry') 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 slap.magic_sig = MagicSigEnvelope.parse sig_doc
if 'base64url' == slap.magic_sig.encoding 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 slap.sig = slap.magic_sig.sig
else else
raise ArgumentError, "Magic Signature data must be encoded with base64url, was #{slap.magic_sig.encoding}" raise ArgumentError, "Magic Signature data must be encoded with base64url, was #{slap.magic_sig.encoding}"
@ -91,6 +102,34 @@ ENTRY
end end
def xml_for person
xml =<<ENTRY
<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns='http://www.w3.org/2005/Atom'>
<encrypted_header>#{person.encrypt(decrypted_header.rstrip)}</encrypted_header>
<author>
<name>#{@author.real_name}</name>
<uri>acct:#{@author.diaspora_handle}</uri>
</author>
#{@magic_sig.to_xml}
</entry>
ENTRY
end
def decrypted_header
header =<<HEADER
<header>
<iv>#{iv}</iv>
<aes_key>#{aes_key}</aes_key>
<author>
<name>#{@author.real_name}</name>
<uri>acct:#{@author.diaspora_handle}</uri>
</author>
</header>
HEADER
end
def author def author
if @author if @author
@author @author

View file

@ -6,10 +6,12 @@ require 'spec_helper'
describe Salmon do describe Salmon do
let(:user){Factory.create :user} 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(: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 describe '#create' do
let!(:created_salmon) {Salmon::SalmonSlap.create(user, post.to_diaspora_xml)}
it 'has data in the magic envelope' do it 'has data in the magic envelope' do
created_salmon.magic_sig.data.should_not be nil created_salmon.magic_sig.data.should_not be nil
@ -24,17 +26,49 @@ describe Salmon do
created_salmon.iv.should_not be nil created_salmon.iv.should_not be nil
end 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} key_hash = {'key' => created_salmon.aes_key, 'iv' => created_salmon.iv}
decoded_string = Salmon::SalmonSlap.decode64url(created_salmon.magic_sig.data) 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
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 context 'round trip' do
before do before do
@sent_salmon = Salmon::SalmonSlap.create(user, post.to_diaspora_xml) @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") stub_success("tom@tom.joindiaspora.com")
end end