refactor receive classes
This commit is contained in:
parent
4dc34b54e3
commit
c7446f7220
6 changed files with 38 additions and 44 deletions
|
|
@ -23,7 +23,6 @@ module DiasporaFederation
|
|||
fetch_person_for_hcard
|
||||
save_person_after_webfinger
|
||||
fetch_private_key_by_diaspora_id
|
||||
fetch_private_key_by_user_guid
|
||||
fetch_author_private_key_by_entity_guid
|
||||
fetch_public_key_by_diaspora_id
|
||||
fetch_author_public_key_by_entity_guid
|
||||
|
|
@ -31,7 +30,7 @@ module DiasporaFederation
|
|||
fetch_entity_author_id_by_guid
|
||||
queue_public_receive
|
||||
queue_private_receive
|
||||
entity_persist
|
||||
save_entity_after_receive
|
||||
)
|
||||
|
||||
class << self
|
||||
|
|
@ -136,6 +135,10 @@ module DiasporaFederation
|
|||
# @param [String] xml salmon xml
|
||||
# @return [Boolean] true if successful, false if the user was not found
|
||||
#
|
||||
# save_entity_after_receive
|
||||
# After the xml was parsed and processed the gem calls this callback to persist the entity
|
||||
# @param [DiasporaFederation::Entity] entity the received entity after processing
|
||||
#
|
||||
# @see Callbacks#on
|
||||
#
|
||||
# @example
|
||||
|
|
|
|||
|
|
@ -1,6 +1,10 @@
|
|||
module DiasporaFederation
|
||||
# SenderNotFound is raised when failed to fetch a public key of the sender of the received message
|
||||
class SenderNotFound < Exception
|
||||
# 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
|
||||
|
|
@ -13,9 +17,9 @@ module DiasporaFederation
|
|||
|
||||
def receive!
|
||||
sender_id = slap.author_id
|
||||
pkey = DiasporaFederation.callbacks.trigger(:fetch_public_key_by_diaspora_id, sender_id)
|
||||
raise SenderNotFound if pkey.nil?
|
||||
DiasporaFederation.callbacks.trigger(:entity_persist, slap.entity(pkey), @recipient_guid, sender_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
|
||||
|
|
|
|||
|
|
@ -1,17 +1,13 @@
|
|||
module DiasporaFederation
|
||||
# RecipientNotFound is raised when failed to fetch a private key of the recipient of the received message
|
||||
class RecipientNotFound < Exception
|
||||
end
|
||||
|
||||
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(recipient_guid, salmon_xml)
|
||||
def initialize(salmon_xml, recipient_private_key)
|
||||
super(salmon_xml)
|
||||
@recipient_guid = recipient_guid
|
||||
@recipient_private_key = DiasporaFederation.callbacks.trigger(:fetch_private_key_by_user_guid, recipient_guid)
|
||||
raise RecipientNotFound if @recipient_private_key.nil?
|
||||
raise RecipientKeyNotFound if recipient_private_key.nil?
|
||||
@recipient_private_key = recipient_private_key
|
||||
end
|
||||
|
||||
protected
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ module DiasporaFederation
|
|||
let(:sender_id) { FactoryGirl.generate(:diaspora_id) }
|
||||
let(:sender_key) { OpenSSL::PKey::RSA.generate(1024) }
|
||||
let(:recipient_key) { OpenSSL::PKey::RSA.generate(1024) }
|
||||
let(:recipient_guid) { FactoryGirl.generate(:guid) }
|
||||
let(:xml) {
|
||||
DiasporaFederation::Salmon::EncryptedSlap.generate_xml(
|
||||
sender_id,
|
||||
|
|
@ -13,45 +12,37 @@ module DiasporaFederation
|
|||
)
|
||||
}
|
||||
|
||||
it "calls entity_persist if everyting is fine" do
|
||||
it "calls save_entity_after_receive if everything is fine" do
|
||||
expect(DiasporaFederation.callbacks).to receive(:trigger)
|
||||
.with(:fetch_public_key_by_diaspora_id, sender_id)
|
||||
.and_return(sender_key)
|
||||
expect(DiasporaFederation.callbacks).to receive(:trigger)
|
||||
.with(:fetch_private_key_by_user_guid, recipient_guid)
|
||||
.and_return(recipient_key)
|
||||
|
||||
expect(DiasporaFederation.callbacks).to receive(:trigger)
|
||||
.with(:entity_persist, kind_of(Entity), recipient_guid, sender_id)
|
||||
.with(:save_entity_after_receive, kind_of(Entity))
|
||||
|
||||
described_class.new(recipient_guid, xml).receive!
|
||||
described_class.new(xml, recipient_key).receive!
|
||||
end
|
||||
|
||||
it "raises when sender public key is not available" do
|
||||
expect(DiasporaFederation.callbacks).to receive(:trigger)
|
||||
.with(:fetch_public_key_by_diaspora_id, sender_id)
|
||||
.and_return(nil)
|
||||
expect(DiasporaFederation.callbacks).to receive(:trigger)
|
||||
.with(:fetch_private_key_by_user_guid, recipient_guid)
|
||||
.and_return(recipient_key)
|
||||
|
||||
expect { described_class.new(recipient_guid, xml).receive! }.to raise_error SenderNotFound
|
||||
expect {
|
||||
described_class.new(xml, recipient_key).receive!
|
||||
}.to raise_error SenderKeyNotFound
|
||||
end
|
||||
|
||||
it "raises when recipient private key is not available" do
|
||||
expect(DiasporaFederation.callbacks).to receive(:trigger)
|
||||
.with(:fetch_private_key_by_user_guid, recipient_guid)
|
||||
.and_return(nil)
|
||||
|
||||
expect { described_class.new(recipient_guid, xml).receive! }.to raise_error RecipientNotFound
|
||||
expect {
|
||||
described_class.new(xml, nil).receive!
|
||||
}.to raise_error RecipientKeyNotFound
|
||||
end
|
||||
|
||||
it "raises when bad xml was supplied" do
|
||||
expect(DiasporaFederation.callbacks).to receive(:trigger)
|
||||
.with(:fetch_private_key_by_user_guid, recipient_guid)
|
||||
.and_return(recipient_key)
|
||||
|
||||
expect { described_class.new(recipient_guid, "<XML/>").receive! }.to raise_error Salmon::MissingHeader
|
||||
expect {
|
||||
described_class.new("<XML/>", recipient_key).receive!
|
||||
}.to raise_error Salmon::MissingHeader
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -10,12 +10,12 @@ module DiasporaFederation
|
|||
)
|
||||
}
|
||||
|
||||
it "calls entity_persist if everyting is fine" do
|
||||
it "calls save_entity_after_receive if everything is fine" do
|
||||
expect(DiasporaFederation.callbacks).to receive(:trigger)
|
||||
.with(:fetch_public_key_by_diaspora_id, sender_id)
|
||||
.and_return(sender_key)
|
||||
expect(DiasporaFederation.callbacks).to receive(:trigger)
|
||||
.with(:entity_persist, kind_of(Entity), nil, sender_id)
|
||||
.with(:save_entity_after_receive, kind_of(Entity))
|
||||
|
||||
described_class.new(xml).receive!
|
||||
end
|
||||
|
|
@ -25,11 +25,15 @@ module DiasporaFederation
|
|||
.with(:fetch_public_key_by_diaspora_id, sender_id)
|
||||
.and_return(nil)
|
||||
|
||||
expect { described_class.new(xml).receive! }.to raise_error SenderNotFound
|
||||
expect {
|
||||
described_class.new(xml).receive!
|
||||
}.to raise_error SenderKeyNotFound
|
||||
end
|
||||
|
||||
it "raises when bad xml was supplied" do
|
||||
expect { described_class.new("<XML/>").receive! }.to raise_error Salmon::MissingAuthor
|
||||
expect {
|
||||
described_class.new("<XML/>").receive!
|
||||
}.to raise_error Salmon::MissingAuthor
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -87,10 +87,6 @@ DiasporaFederation.configure do |config|
|
|||
nil
|
||||
end
|
||||
|
||||
on :fetch_private_key_by_user_guid do
|
||||
nil
|
||||
end
|
||||
|
||||
on :queue_public_receive do
|
||||
end
|
||||
|
||||
|
|
@ -98,7 +94,7 @@ DiasporaFederation.configure do |config|
|
|||
true
|
||||
end
|
||||
|
||||
on :entity_persist do
|
||||
on :save_entity_after_receive do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue