MS IZ DG moar salmon refactoring
This commit is contained in:
parent
e0429ee823
commit
34ed07260f
15 changed files with 59 additions and 33 deletions
|
|
@ -19,7 +19,7 @@ module Job
|
|||
|
||||
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 = []
|
||||
|
||||
|
|
|
|||
|
|
@ -215,7 +215,7 @@ class User < ActiveRecord::Base
|
|||
end
|
||||
|
||||
def salmon(post)
|
||||
Salmon::EncryptedSalmonSlap.create(self, post.to_diaspora_xml)
|
||||
Salmon::EncryptedSlap.create(self, post.to_diaspora_xml)
|
||||
end
|
||||
|
||||
def build_relayable(model, options = {})
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ class Postzord::Dispatch
|
|||
end
|
||||
|
||||
def salmon
|
||||
@salmon_factory ||= Salmon::EncryptedSalmonSlap.create(@sender, @xml)
|
||||
@salmon_factory ||= Salmon::EncryptedSlap.create(@sender, @xml)
|
||||
end
|
||||
|
||||
def post(opts = {})
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ module Postzord
|
|||
|
||||
protected
|
||||
def salmon
|
||||
@salmon ||= Salmon::EncryptedSalmonSlap.parse(@salmon_xml, @user)
|
||||
@salmon ||= Salmon::EncryptedSlap.parse(@salmon_xml, @user)
|
||||
end
|
||||
|
||||
def xml_author
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
# the COPYRIGHT file.
|
||||
|
||||
module Salmon
|
||||
class EncryptedSalmonSlap < SalmonSlap
|
||||
class EncryptedSlap < Slap
|
||||
def header(person)
|
||||
<<XML
|
||||
<encrypted_header>
|
||||
|
|
@ -21,5 +21,10 @@ XML
|
|||
header = user.decrypt(doc.search('encrypted_header').text)
|
||||
Nokogiri::XML(header)
|
||||
end
|
||||
|
||||
# @return [String]
|
||||
def self.payload(activity, user, aes_key_hash)
|
||||
user.person.aes_encrypt(activity, aes_key_hash)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -16,13 +16,14 @@ module Salmon
|
|||
|
||||
env.data = doc.search('//me:env/me:data', ns).text
|
||||
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
|
||||
raise ArgumentError, "Magic Signature data must be signed with RSA-SHA256, was #{env.alg}"
|
||||
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
|
||||
end
|
||||
|
||||
|
|
@ -34,6 +35,7 @@ module Salmon
|
|||
env.encoding = env.get_encoding
|
||||
env.alg = env.get_alg
|
||||
|
||||
#TODO: WHY DO WE DOUBLE ENCODE
|
||||
env.sig = Base64.urlsafe_encode64(
|
||||
user.encryption_key.sign OpenSSL::Digest::SHA256.new, env.signable_string )
|
||||
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ end
|
|||
|
||||
# Verify documents secured with Magic Signatures
|
||||
module Salmon
|
||||
autoload :SalmonSlap, File.join(Rails.root, "lib", "salmon", "salmon_slap")
|
||||
autoload :EncryptedSalmonSlap, File.join(Rails.root, "lib", "salmon", "encrypted_salmon_slap")
|
||||
autoload :MagicSigEnvelope, File.join(Rails.root, "lib", "salmon", "magic_sig_envelope")
|
||||
autoload :Slap, File.join(Rails.root, "lib", "salmon", "slap")
|
||||
autoload :EncryptedSlap, File.join(Rails.root, "lib", "salmon", "encrypted_slap")
|
||||
autoload :MagicSigEnvelope, File.join(Rails.root, "lib", "salmon", "magic_sig_envelope")
|
||||
end
|
||||
|
|
|
|||
|
|
@ -3,17 +3,20 @@
|
|||
# the COPYRIGHT file.
|
||||
|
||||
module Salmon
|
||||
class SalmonSlap
|
||||
class Slap
|
||||
attr_accessor :magic_sig, :author, :author_email, :aes_key, :iv, :parsed_data,
|
||||
:data_type, :sig
|
||||
|
||||
def self.create(user, activity)
|
||||
salmon = self.new
|
||||
salmon.author = user.person
|
||||
aes_key_hash = user.person.gen_aes_key
|
||||
salmon.aes_key = aes_key_hash['key']
|
||||
salmon.iv = aes_key_hash['iv']
|
||||
salmon.magic_sig = MagicSigEnvelope.create(user , user.person.aes_encrypt(activity, aes_key_hash))
|
||||
salmon.author = user.person
|
||||
aes_key_hash = user.person.gen_aes_key
|
||||
|
||||
#additional headers
|
||||
salmon.aes_key = aes_key_hash['key']
|
||||
salmon.iv = aes_key_hash['iv']
|
||||
|
||||
salmon.magic_sig = MagicSigEnvelope.create(user, self.payload(activity, user, aes_key_hash))
|
||||
salmon
|
||||
end
|
||||
|
||||
|
|
@ -39,9 +42,14 @@ module Salmon
|
|||
slap
|
||||
end
|
||||
|
||||
# @return [String]
|
||||
def self.payload(activity, user=nil, aes_key_hash=nil)
|
||||
activity
|
||||
end
|
||||
|
||||
# @return [String]
|
||||
def parse_data(key_hash, user=nil)
|
||||
SalmonSlap.decode64url(self.magic_sig.data)
|
||||
Slap.decode64url(self.magic_sig.data)
|
||||
end
|
||||
|
||||
# @return [Nokogiri::Doc]
|
||||
|
|
@ -55,7 +63,7 @@ module Salmon
|
|||
<entry xmlns='http://www.w3.org/2005/Atom'>
|
||||
#{header(person)}
|
||||
#{@magic_sig.to_xml}
|
||||
</entry>
|
||||
</entry>
|
||||
ENTRY
|
||||
end
|
||||
|
||||
|
|
@ -57,7 +57,7 @@ describe PublicsController do
|
|||
xml2 = post1.to_diaspora_xml
|
||||
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)
|
||||
|
||||
Resque.should_receive(:enqueue).with(Job::ReceiveSalmon, @user.id, enc_xml).once
|
||||
|
|
|
|||
|
|
@ -231,7 +231,7 @@ describe Postzord::Dispatch do
|
|||
|
||||
it 'calls salmon_for each remote person' do
|
||||
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')
|
||||
@hydra.stub!(:queue)
|
||||
@hydra.stub!(:run)
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ describe Postzord::Receiver do
|
|||
describe '.initialize' do
|
||||
it 'valid for local' do
|
||||
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.instance_variable_get(:@user).should_not be_nil
|
||||
|
|
@ -37,7 +37,7 @@ describe Postzord::Receiver do
|
|||
web_mock = mock()
|
||||
web_mock.should_receive(:fetch).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)
|
||||
|
||||
zord = Postzord::Receiver.new(@user, :salmon_xml => @salmon_xml)
|
||||
|
|
|
|||
|
|
@ -4,10 +4,10 @@
|
|||
|
||||
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!(:created_salmon) {Salmon::EncryptedSalmonSlap.create(alice, post.to_diaspora_xml)}
|
||||
let!(:created_salmon) {Salmon::EncryptedSlap.create(alice, post.to_diaspora_xml)}
|
||||
|
||||
describe '#create' do
|
||||
|
||||
|
|
@ -26,7 +26,7 @@ describe Salmon::EncryptedSalmonSlap 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::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
|
||||
end
|
||||
end
|
||||
|
|
@ -47,7 +47,7 @@ describe Salmon::EncryptedSalmonSlap do
|
|||
|
||||
context 'marshaling' do
|
||||
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
|
||||
parsed_salmon.aes_key.should == created_salmon.aes_key
|
||||
|
|
@ -1,5 +0,0 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Salmon::SalmonSlap do
|
||||
|
||||
end
|
||||
16
spec/lib/salmon/slap_spec.rb
Normal file
16
spec/lib/salmon/slap_spec.rb
Normal 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
|
||||
|
|
@ -60,8 +60,8 @@ describe Job::HttpMulti do
|
|||
|
||||
Typhoeus::Hydra.stub!(:new).and_return(@hydra)
|
||||
|
||||
salmon = Salmon::SalmonSlap.create(bob, Base64.decode64(@post_xml))
|
||||
Salmon::SalmonSlap.stub(:create).and_return(salmon)
|
||||
salmon = Salmon::EncryptedSlap.create(bob, Base64.decode64(@post_xml))
|
||||
Salmon::EncryptedSlap.stub(:create).and_return(salmon)
|
||||
salmon.should_receive(:xml_for).and_return("encrypted things")
|
||||
|
||||
Job::HttpMulti.perform(bob.id, @post_xml, [person.id])
|
||||
|
|
|
|||
Loading…
Reference in a new issue