MS IZ DG moar salmon refactoring

This commit is contained in:
Maxwell Salzberg 2011-09-08 19:22:23 -07:00
parent e0429ee823
commit 34ed07260f
15 changed files with 59 additions and 33 deletions

View file

@ -19,7 +19,7 @@ module Job
people = Person.where(:id => person_ids) people = Person.where(:id => person_ids)
salmon = Salmon::EncryptedSalmonSlap.create(user, Base64.decode64(enc_object_xml)) salmon = Salmon::EncryptedSlap.create(user, Base64.decode64(enc_object_xml))
failed_request_people = [] failed_request_people = []

View file

@ -215,7 +215,7 @@ class User < ActiveRecord::Base
end end
def salmon(post) def salmon(post)
Salmon::EncryptedSalmonSlap.create(self, post.to_diaspora_xml) Salmon::EncryptedSlap.create(self, post.to_diaspora_xml)
end end
def build_relayable(model, options = {}) def build_relayable(model, options = {})

View file

@ -18,7 +18,7 @@ class Postzord::Dispatch
end end
def salmon def salmon
@salmon_factory ||= Salmon::EncryptedSalmonSlap.create(@sender, @xml) @salmon_factory ||= Salmon::EncryptedSlap.create(@sender, @xml)
end end
def post(opts = {}) def post(opts = {})

View file

@ -45,7 +45,7 @@ module Postzord
protected protected
def salmon def salmon
@salmon ||= Salmon::EncryptedSalmonSlap.parse(@salmon_xml, @user) @salmon ||= Salmon::EncryptedSlap.parse(@salmon_xml, @user)
end end
def xml_author def xml_author

View file

@ -3,7 +3,7 @@
# the COPYRIGHT file. # the COPYRIGHT file.
module Salmon module Salmon
class EncryptedSalmonSlap < SalmonSlap class EncryptedSlap < Slap
def header(person) def header(person)
<<XML <<XML
<encrypted_header> <encrypted_header>
@ -21,5 +21,10 @@ XML
header = user.decrypt(doc.search('encrypted_header').text) header = user.decrypt(doc.search('encrypted_header').text)
Nokogiri::XML(header) Nokogiri::XML(header)
end end
# @return [String]
def self.payload(activity, user, aes_key_hash)
user.person.aes_encrypt(activity, aes_key_hash)
end
end end
end end

View file

@ -16,13 +16,14 @@ module Salmon
env.data = doc.search('//me:env/me:data', ns).text env.data = doc.search('//me:env/me:data', ns).text
env.alg = doc.search('//me:env/me:alg', ns).text.strip env.alg = doc.search('//me:env/me:alg', ns).text.strip
env.sig = doc.search('//me:env/me:sig', ns).text
env.data_type = doc.search('//me:env/me:data', ns).first['type'].strip
unless 'RSA-SHA256' == env.alg unless 'RSA-SHA256' == env.alg
raise ArgumentError, "Magic Signature data must be signed with RSA-SHA256, was #{env.alg}" raise ArgumentError, "Magic Signature data must be signed with RSA-SHA256, was #{env.alg}"
end end
env.sig = doc.search('//me:env/me:sig', ns).text
env.data_type = doc.search('//me:env/me:data', ns).first['type'].strip
env env
end end
@ -34,6 +35,7 @@ module Salmon
env.encoding = env.get_encoding env.encoding = env.get_encoding
env.alg = env.get_alg env.alg = env.get_alg
#TODO: WHY DO WE DOUBLE ENCODE
env.sig = Base64.urlsafe_encode64( env.sig = Base64.urlsafe_encode64(
user.encryption_key.sign OpenSSL::Digest::SHA256.new, env.signable_string ) user.encryption_key.sign OpenSSL::Digest::SHA256.new, env.signable_string )

View file

@ -39,7 +39,7 @@ end
# Verify documents secured with Magic Signatures # Verify documents secured with Magic Signatures
module Salmon module Salmon
autoload :SalmonSlap, File.join(Rails.root, "lib", "salmon", "salmon_slap") autoload :Slap, File.join(Rails.root, "lib", "salmon", "slap")
autoload :EncryptedSalmonSlap, File.join(Rails.root, "lib", "salmon", "encrypted_salmon_slap") autoload :EncryptedSlap, File.join(Rails.root, "lib", "salmon", "encrypted_slap")
autoload :MagicSigEnvelope, File.join(Rails.root, "lib", "salmon", "magic_sig_envelope") autoload :MagicSigEnvelope, File.join(Rails.root, "lib", "salmon", "magic_sig_envelope")
end end

View file

@ -3,7 +3,7 @@
# the COPYRIGHT file. # the COPYRIGHT file.
module Salmon module Salmon
class SalmonSlap class Slap
attr_accessor :magic_sig, :author, :author_email, :aes_key, :iv, :parsed_data, attr_accessor :magic_sig, :author, :author_email, :aes_key, :iv, :parsed_data,
:data_type, :sig :data_type, :sig
@ -11,9 +11,12 @@ module Salmon
salmon = self.new salmon = self.new
salmon.author = user.person salmon.author = user.person
aes_key_hash = user.person.gen_aes_key aes_key_hash = user.person.gen_aes_key
#additional headers
salmon.aes_key = aes_key_hash['key'] salmon.aes_key = aes_key_hash['key']
salmon.iv = aes_key_hash['iv'] salmon.iv = aes_key_hash['iv']
salmon.magic_sig = MagicSigEnvelope.create(user , user.person.aes_encrypt(activity, aes_key_hash))
salmon.magic_sig = MagicSigEnvelope.create(user, self.payload(activity, user, aes_key_hash))
salmon salmon
end end
@ -39,9 +42,14 @@ module Salmon
slap slap
end end
# @return [String]
def self.payload(activity, user=nil, aes_key_hash=nil)
activity
end
# @return [String] # @return [String]
def parse_data(key_hash, user=nil) def parse_data(key_hash, user=nil)
SalmonSlap.decode64url(self.magic_sig.data) Slap.decode64url(self.magic_sig.data)
end end
# @return [Nokogiri::Doc] # @return [Nokogiri::Doc]

View file

@ -57,7 +57,7 @@ describe PublicsController do
xml2 = post1.to_diaspora_xml xml2 = post1.to_diaspora_xml
user2 = Factory(:user) user2 = Factory(:user)
salmon_factory = Salmon::SalmonSlap.create(@user, xml2) salmon_factory = Salmon::EncryptedSlap.create(@user, xml2)
enc_xml = salmon_factory.xml_for(user2.person) enc_xml = salmon_factory.xml_for(user2.person)
Resque.should_receive(:enqueue).with(Job::ReceiveSalmon, @user.id, enc_xml).once Resque.should_receive(:enqueue).with(Job::ReceiveSalmon, @user.id, enc_xml).once

View file

@ -231,7 +231,7 @@ describe Postzord::Dispatch do
it 'calls salmon_for each remote person' do it 'calls salmon_for each remote person' do
salmon = @mailman.salmon salmon = @mailman.salmon
Salmon::SalmonSlap.stub(:create).and_return(salmon) Salmon::EncryptedSlap.stub(:create).and_return(salmon)
salmon.should_receive(:xml_for).with(alice.person).and_return('what') salmon.should_receive(:xml_for).with(alice.person).and_return('what')
@hydra.stub!(:queue) @hydra.stub!(:queue)
@hydra.stub!(:run) @hydra.stub!(:run)

View file

@ -24,7 +24,7 @@ describe Postzord::Receiver do
describe '.initialize' do describe '.initialize' do
it 'valid for local' do it 'valid for local' do
Webfinger.should_not_receive(:new) Webfinger.should_not_receive(:new)
Salmon::SalmonSlap.should_not_receive(:parse) Salmon::EncryptedSlap.should_not_receive(:parse)
zord = Postzord::Receiver.new(@user, :person => @person2, :object => @original_post) zord = Postzord::Receiver.new(@user, :person => @person2, :object => @original_post)
zord.instance_variable_get(:@user).should_not be_nil zord.instance_variable_get(:@user).should_not be_nil
@ -37,7 +37,7 @@ describe Postzord::Receiver do
web_mock = mock() web_mock = mock()
web_mock.should_receive(:fetch).and_return true web_mock.should_receive(:fetch).and_return true
salmon_mock.should_receive(:author_email).and_return(true) salmon_mock.should_receive(:author_email).and_return(true)
Salmon::SalmonSlap.should_receive(:parse).with(@salmon_xml, @user).and_return(salmon_mock) Salmon::EncryptedSlap.should_receive(:parse).with(@salmon_xml, @user).and_return(salmon_mock)
Webfinger.should_receive(:new).and_return(web_mock) Webfinger.should_receive(:new).and_return(web_mock)
zord = Postzord::Receiver.new(@user, :salmon_xml => @salmon_xml) zord = Postzord::Receiver.new(@user, :salmon_xml => @salmon_xml)

View file

@ -4,10 +4,10 @@
require 'spec_helper' require 'spec_helper'
describe Salmon::EncryptedSalmonSlap do describe Salmon::EncryptedSlap do
let(:post){ alice.post :status_message, :text => "hi", :to => alice.aspects.create(:name => "sdg").id } let(:post){ alice.post :status_message, :text => "hi", :to => alice.aspects.create(:name => "sdg").id }
let!(:created_salmon) {Salmon::EncryptedSalmonSlap.create(alice, post.to_diaspora_xml)} let!(:created_salmon) {Salmon::EncryptedSlap.create(alice, post.to_diaspora_xml)}
describe '#create' do describe '#create' do
@ -26,7 +26,7 @@ describe Salmon::EncryptedSalmonSlap do
it 'makes 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::EncryptedSalmonSlap.decode64url(created_salmon.magic_sig.data) decoded_string = Salmon::EncryptedSlap.decode64url(created_salmon.magic_sig.data)
alice.aes_decrypt(decoded_string, key_hash).should == post.to_diaspora_xml alice.aes_decrypt(decoded_string, key_hash).should == post.to_diaspora_xml
end end
end end
@ -47,7 +47,7 @@ describe Salmon::EncryptedSalmonSlap do
context 'marshaling' do context 'marshaling' do
let(:xml) {created_salmon.xml_for eve.person} let(:xml) {created_salmon.xml_for eve.person}
let(:parsed_salmon) { Salmon::EncryptedSalmonSlap.parse(xml, eve)} let(:parsed_salmon) { Salmon::EncryptedSlap.parse(xml, eve)}
it 'should parse out the aes key' do it 'should parse out the aes key' do
parsed_salmon.aes_key.should == created_salmon.aes_key parsed_salmon.aes_key.should == created_salmon.aes_key

View file

@ -1,5 +0,0 @@
require 'spec_helper'
describe Salmon::SalmonSlap do
end

View file

@ -0,0 +1,16 @@
require 'spec_helper'
describe Salmon::Slap do
before do
@post = alice.post(:status_message, :text => "hi", :to => alice.aspects.create(:name => "abcd").id)
@created_salmon = Salmon::Slap.create(alice, @post.to_diaspora_xml)
end
it 'works' do
salmon_string = @created_salmon.xml_for(nil)
salmon = Salmon::Slap.parse(salmon_string)
salmon.author.should == alice.person
salmon.parsed_data.should == @post.to_diaspora_xml
end
end

View file

@ -60,8 +60,8 @@ describe Job::HttpMulti do
Typhoeus::Hydra.stub!(:new).and_return(@hydra) Typhoeus::Hydra.stub!(:new).and_return(@hydra)
salmon = Salmon::SalmonSlap.create(bob, Base64.decode64(@post_xml)) salmon = Salmon::EncryptedSlap.create(bob, Base64.decode64(@post_xml))
Salmon::SalmonSlap.stub(:create).and_return(salmon) Salmon::EncryptedSlap.stub(:create).and_return(salmon)
salmon.should_receive(:xml_for).and_return("encrypted things") salmon.should_receive(:xml_for).and_return("encrypted things")
Job::HttpMulti.perform(bob.id, @post_xml, [person.id]) Job::HttpMulti.perform(bob.id, @post_xml, [person.id])