diff --git a/lib/diaspora_federation/federation/sender.rb b/lib/diaspora_federation/federation/sender.rb index 63283e7..ab15662 100644 --- a/lib/diaspora_federation/federation/sender.rb +++ b/lib/diaspora_federation/federation/sender.rb @@ -11,7 +11,7 @@ module DiasporaFederation # @return [Array] url to retry def self.public(sender_id, obj_str, urls, xml) hydra = HydraWrapper.new(sender_id, obj_str) - urls.each {|url| hydra.insert_job(url, xml) } + urls.each {|url| hydra.insert_magic_env_request(url, xml) } hydra.send end @@ -23,7 +23,7 @@ module DiasporaFederation # @return [Hash] targets to retry def self.private(sender_id, obj_str, targets) hydra = HydraWrapper.new(sender_id, obj_str) - targets.each {|url, xml| hydra.insert_job(url, xml) } + targets.each {|url, json| hydra.insert_enc_magic_env_request(url, json) } hydra.send.map {|url| [url, targets[url]] }.to_h end end diff --git a/lib/diaspora_federation/federation/sender/hydra_wrapper.rb b/lib/diaspora_federation/federation/sender/hydra_wrapper.rb index ea25c82..be6dbae 100644 --- a/lib/diaspora_federation/federation/sender/hydra_wrapper.rb +++ b/lib/diaspora_federation/federation/sender/hydra_wrapper.rb @@ -17,12 +17,21 @@ module DiasporaFederation method: :post, verbose: DiasporaFederation.http_verbose, cainfo: DiasporaFederation.certificate_authorities, - forbid_reuse: true, - headers: { - "Expect" => "", - "Transfer-Encoding" => "", - "User-Agent" => DiasporaFederation.http_user_agent - } + forbid_reuse: true + } + end + + def self.xml_headers + @xml_headers ||= { + "Content-Type" => "application/magic-envelope+xml", + "User-Agent" => DiasporaFederation.http_user_agent + } + end + + def self.json_headers + @json_headers ||= { + "Content-Type" => "application/json", + "User-Agent" => DiasporaFederation.http_user_agent } end @@ -36,13 +45,18 @@ module DiasporaFederation @urls_to_retry = [] end - # Prepares and inserts job into the hydra queue + # Prepares and inserts a public MagicEnvelope job into the hydra queue # @param [String] url the receive-url for the xml - # @param [String] xml xml salmon message - def insert_job(url, xml) - request = Typhoeus::Request.new(url, HydraWrapper.hydra_opts.merge(body: {xml: xml})) - prepare_request(request) - hydra.queue(request) + # @param [String] xml MagicEnvelope xml + def insert_magic_env_request(url, xml) + insert_job(url, HydraWrapper.hydra_opts.merge(body: xml, headers: HydraWrapper.xml_headers)) + end + + # Prepares and inserts a private encrypted MagicEnvelope job into the hydra queue + # @param [String] url the receive-url for the message + # @param [String] json encrypted MagicEnvelope json + def insert_enc_magic_env_request(url, json) + insert_job(url, HydraWrapper.hydra_opts.merge(body: json, headers: HydraWrapper.json_headers)) end # Sends all queued messages @@ -54,6 +68,15 @@ module DiasporaFederation private + # Prepares and inserts job into the hydra queue + # @param [String] url the receive-url for the message + # @param [Hash] options request options + def insert_job(url, options) + request = Typhoeus::Request.new(url, options) + prepare_request(request) + hydra.queue(request) + end + # @return [Typhoeus::Hydra] hydra def hydra @hydra ||= Typhoeus::Hydra.new(max_concurrency: DiasporaFederation.http_concurrency) diff --git a/spec/lib/diaspora_federation/federation/sender/hydra_wrapper_spec.rb b/spec/lib/diaspora_federation/federation/sender/hydra_wrapper_spec.rb index ed5203a..66f9698 100644 --- a/spec/lib/diaspora_federation/federation/sender/hydra_wrapper_spec.rb +++ b/spec/lib/diaspora_federation/federation/sender/hydra_wrapper_spec.rb @@ -3,6 +3,7 @@ module DiasporaFederation let(:sender_id) { Fabricate.sequence(:diaspora_id) } let(:obj_str) { "status_message@guid" } let(:xml) { "post" } + let(:json) { "{\"aes_key\": \"...\", \"encrypted_magic_envelope\": \"...\"}" } let(:url) { "http://example.org/receive/public" } let(:url2) { "http://example.com/receive/public" } @@ -13,21 +14,45 @@ module DiasporaFederation allow(Typhoeus::Hydra).to receive(:new).and_return(hydra) end - describe "#insert_job" do + describe "#insert_magic_env_request" do it "queues a request to hydra" do expect(hydra).to receive(:queue).with(kind_of(Typhoeus::Request)) expect(Typhoeus::Request).to receive(:new).with( - url, Federation::Sender::HydraWrapper.hydra_opts.merge(body: {xml: xml}) + url, + Federation::Sender::HydraWrapper.hydra_opts.merge( + body: xml, headers: Federation::Sender::HydraWrapper.xml_headers + ) ).and_call_original - hydra_wrapper.insert_job(url, xml) + hydra_wrapper.insert_magic_env_request(url, xml) end it "queues multiple requests to hydra" do expect(hydra).to receive(:queue).twice.with(kind_of(Typhoeus::Request)) - hydra_wrapper.insert_job(url, xml) - hydra_wrapper.insert_job(url2, xml) + hydra_wrapper.insert_magic_env_request(url, xml) + hydra_wrapper.insert_magic_env_request(url2, xml) + end + end + + describe "#insert_enc_magic_env_request" do + it "queues a request to hydra" do + expect(hydra).to receive(:queue).with(kind_of(Typhoeus::Request)) + expect(Typhoeus::Request).to receive(:new).with( + url, + Federation::Sender::HydraWrapper.hydra_opts.merge( + body: json, headers: Federation::Sender::HydraWrapper.json_headers + ) + ).and_call_original + + hydra_wrapper.insert_enc_magic_env_request(url, json) + end + + it "queues multiple requests to hydra" do + expect(hydra).to receive(:queue).twice.with(kind_of(Typhoeus::Request)) + + hydra_wrapper.insert_enc_magic_env_request(url, json) + hydra_wrapper.insert_enc_magic_env_request(url2, json) end end @@ -54,8 +79,8 @@ module DiasporaFederation before do Typhoeus.stub(url).and_return(response) Typhoeus.stub(url2).and_return(error_response) - hydra_wrapper.insert_job(url, xml) - hydra_wrapper.insert_job(url2, xml) + hydra_wrapper.insert_magic_env_request(url, xml) + hydra_wrapper.insert_magic_env_request(url2, xml) end before :all do WebMock::HttpLibAdapters::TyphoeusAdapter.disable! @@ -88,7 +113,7 @@ module DiasporaFederation return_code: :ok ) Typhoeus.stub("http://example.net/receive/not_found").and_return(not_found) - hydra_wrapper.insert_job("http://example.net/receive/not_found", xml) + hydra_wrapper.insert_magic_env_request("http://example.net/receive/not_found", xml) hydra_wrapper.send end @@ -100,7 +125,7 @@ module DiasporaFederation url3 = "http://example.net/receive/public" Typhoeus.stub(url3).and_return(response) - hydra_wrapper.insert_job(url3, xml) + hydra_wrapper.insert_magic_env_request(url3, xml) expect(hydra_wrapper.send).to eq([url2, url3]) end diff --git a/spec/lib/diaspora_federation/federation/sender_spec.rb b/spec/lib/diaspora_federation/federation/sender_spec.rb index 29a6a61..8f1dce9 100644 --- a/spec/lib/diaspora_federation/federation/sender_spec.rb +++ b/spec/lib/diaspora_federation/federation/sender_spec.rb @@ -13,8 +13,8 @@ module DiasporaFederation let(:urls) { ["https://example.org/receive/public", "https://example.com/receive/public"] } before do - expect(hydra_wrapper).to receive(:insert_job).with(urls.at(0), xml) - expect(hydra_wrapper).to receive(:insert_job).with(urls.at(1), xml) + expect(hydra_wrapper).to receive(:insert_magic_env_request).with(urls.at(0), xml) + expect(hydra_wrapper).to receive(:insert_magic_env_request).with(urls.at(1), xml) end it "returns empty array if send was successful" do @@ -34,14 +34,14 @@ module DiasporaFederation describe ".private" do let(:targets) { { - "https://example.org/receive/user/guid" => "post", - "https://example.com/receive/user/guid" => "post2" + "https://example.org/receive/user/guid" => "{\"aes_key\": \"key1\", \"encrypted_magic_envelope\": \"...\"}", + "https://example.com/receive/user/guid" => "{\"aes_key\": \"key2\", \"encrypted_magic_envelope\": \"...\"}" } } before do - targets.each do |url, xml| - expect(hydra_wrapper).to receive(:insert_job).with(url, xml) + targets.each do |url, json| + expect(hydra_wrapper).to receive(:insert_enc_magic_env_request).with(url, json) end end @@ -55,7 +55,9 @@ module DiasporaFederation expect(hydra_wrapper).to receive(:send).and_return(["https://example.com/receive/user/guid"]) expect(Federation::Sender.private(sender_id, obj_str, targets)) - .to eq("https://example.com/receive/user/guid" => "post2") + .to eq( + "https://example.com/receive/user/guid" => "{\"aes_key\": \"key2\", \"encrypted_magic_envelope\": \"...\"}" + ) end end end