move Receiver to Federation module
This commit is contained in:
parent
1cc8fac8a1
commit
aa84c4c40e
12 changed files with 83 additions and 67 deletions
|
|
@ -1,5 +1,4 @@
|
|||
require_dependency "diaspora_federation/application_controller"
|
||||
require "diaspora_federation/receiver"
|
||||
|
||||
module DiasporaFederation
|
||||
# this controller processes receiving messages
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ require "diaspora_federation/entities"
|
|||
|
||||
require "diaspora_federation/discovery"
|
||||
require "diaspora_federation/salmon"
|
||||
require "diaspora_federation/receiver"
|
||||
require "diaspora_federation/federation"
|
||||
|
||||
# diaspora* federation library
|
||||
module DiasporaFederation
|
||||
|
|
|
|||
8
lib/diaspora_federation/federation.rb
Normal file
8
lib/diaspora_federation/federation.rb
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
module DiasporaFederation
|
||||
# This module contains the federation logic
|
||||
module Federation
|
||||
end
|
||||
end
|
||||
|
||||
require "diaspora_federation/federation/exceptions"
|
||||
require "diaspora_federation/federation/receiver"
|
||||
11
lib/diaspora_federation/federation/exceptions.rb
Normal file
11
lib/diaspora_federation/federation/exceptions.rb
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
module DiasporaFederation
|
||||
module Federation
|
||||
# Raised if failed to fetch a public key of the sender of the received message
|
||||
class SenderKeyNotFound < Exception
|
||||
end
|
||||
|
||||
# Raised if recipient private key is missing for a private receive
|
||||
class RecipientKeyNotFound < Exception
|
||||
end
|
||||
end
|
||||
end
|
||||
22
lib/diaspora_federation/federation/receiver.rb
Normal file
22
lib/diaspora_federation/federation/receiver.rb
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
module DiasporaFederation
|
||||
module Federation
|
||||
# Common base for Private and Public receivers
|
||||
# @see Receiver::Public
|
||||
# @see Receiver::Private
|
||||
class Receiver
|
||||
def initialize(salmon_xml)
|
||||
@salmon_xml = salmon_xml
|
||||
end
|
||||
|
||||
def receive!
|
||||
sender_id = slap.author_id
|
||||
public_key = DiasporaFederation.callbacks.trigger(:fetch_public_key_by_diaspora_id, sender_id)
|
||||
raise SenderKeyNotFound if public_key.nil?
|
||||
DiasporaFederation.callbacks.trigger(:save_entity_after_receive, slap.entity(public_key))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
require "diaspora_federation/federation/receiver/private"
|
||||
require "diaspora_federation/federation/receiver/public"
|
||||
21
lib/diaspora_federation/federation/receiver/private.rb
Normal file
21
lib/diaspora_federation/federation/receiver/private.rb
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
module DiasporaFederation
|
||||
module Federation
|
||||
class Receiver
|
||||
# Receiver::Private is used to receive private messages, which are addressed
|
||||
# to a specific user, encrypted with his public key and packed using Salmon::EncryptedSlap
|
||||
class Private < Receiver
|
||||
def initialize(salmon_xml, recipient_private_key)
|
||||
super(salmon_xml)
|
||||
raise RecipientKeyNotFound if recipient_private_key.nil?
|
||||
@recipient_private_key = recipient_private_key
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def slap
|
||||
@salmon ||= Salmon::EncryptedSlap.from_xml(@salmon_xml, @recipient_private_key)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
15
lib/diaspora_federation/federation/receiver/public.rb
Normal file
15
lib/diaspora_federation/federation/receiver/public.rb
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
module DiasporaFederation
|
||||
module Federation
|
||||
class Receiver
|
||||
# Receiver::Public is used to receive public messages, which are not addressed to a specific user, unencrypted
|
||||
# and packed using Salmon::Slap
|
||||
class Public < Receiver
|
||||
protected
|
||||
|
||||
def slap
|
||||
@salmon ||= Salmon::Slap.from_xml(@salmon_xml)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,28 +0,0 @@
|
|||
module DiasporaFederation
|
||||
# Raised if failed to fetch a public key of the sender of the received message
|
||||
class SenderKeyNotFound < Exception
|
||||
end
|
||||
|
||||
# Raised if recipient private key is missing for a private receive
|
||||
class RecipientKeyNotFound < Exception
|
||||
end
|
||||
|
||||
# Common base for Private and Public receivers
|
||||
# @see Receiver::Public
|
||||
# @see Receiver::Private
|
||||
class Receiver
|
||||
def initialize(salmon_xml)
|
||||
@salmon_xml = salmon_xml
|
||||
end
|
||||
|
||||
def receive!
|
||||
sender_id = slap.author_id
|
||||
public_key = DiasporaFederation.callbacks.trigger(:fetch_public_key_by_diaspora_id, sender_id)
|
||||
raise SenderKeyNotFound if public_key.nil?
|
||||
DiasporaFederation.callbacks.trigger(:save_entity_after_receive, slap.entity(public_key))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
require "diaspora_federation/receiver/private"
|
||||
require "diaspora_federation/receiver/public"
|
||||
|
|
@ -1,19 +0,0 @@
|
|||
module DiasporaFederation
|
||||
class Receiver
|
||||
# Receiver::Private is used to receive private messages, which are addressed to a specific user, encrypted with his
|
||||
# public key and packed using Salmon::EncryptedSlap
|
||||
class Private < Receiver
|
||||
def initialize(salmon_xml, recipient_private_key)
|
||||
super(salmon_xml)
|
||||
raise RecipientKeyNotFound if recipient_private_key.nil?
|
||||
@recipient_private_key = recipient_private_key
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def slap
|
||||
@salmon ||= Salmon::EncryptedSlap.from_xml(@salmon_xml, @recipient_private_key)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,13 +0,0 @@
|
|||
module DiasporaFederation
|
||||
class Receiver
|
||||
# Receiver::Public is used to receive public messages, which are not addressed to a specific user, unencrypted
|
||||
# and packed using Salmon::Slap
|
||||
class Public < Receiver
|
||||
protected
|
||||
|
||||
def slap
|
||||
@salmon ||= Salmon::Slap.from_xml(@salmon_xml)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
module DiasporaFederation
|
||||
describe Receiver::Private do
|
||||
describe Federation::Receiver::Private do
|
||||
let(:sender_id) { FactoryGirl.generate(:diaspora_id) }
|
||||
let(:sender_key) { OpenSSL::PKey::RSA.generate(1024) }
|
||||
let(:recipient_key) { OpenSSL::PKey::RSA.generate(1024) }
|
||||
|
|
@ -30,13 +30,13 @@ module DiasporaFederation
|
|||
|
||||
expect {
|
||||
described_class.new(xml, recipient_key).receive!
|
||||
}.to raise_error SenderKeyNotFound
|
||||
}.to raise_error Federation::SenderKeyNotFound
|
||||
end
|
||||
|
||||
it "raises when recipient private key is not available" do
|
||||
expect {
|
||||
described_class.new(xml, nil).receive!
|
||||
}.to raise_error RecipientKeyNotFound
|
||||
}.to raise_error Federation::RecipientKeyNotFound
|
||||
end
|
||||
|
||||
it "raises when bad xml was supplied" do
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
module DiasporaFederation
|
||||
describe Receiver::Public do
|
||||
describe Federation::Receiver::Public do
|
||||
let(:sender_id) { FactoryGirl.generate(:diaspora_id) }
|
||||
let(:sender_key) { OpenSSL::PKey::RSA.generate(1024) }
|
||||
let(:xml) {
|
||||
|
|
@ -27,7 +27,7 @@ module DiasporaFederation
|
|||
|
||||
expect {
|
||||
described_class.new(xml).receive!
|
||||
}.to raise_error SenderKeyNotFound
|
||||
}.to raise_error Federation::SenderKeyNotFound
|
||||
end
|
||||
|
||||
it "raises when bad xml was supplied" do
|
||||
Loading…
Reference in a new issue