summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/nickserver/hkp/client.rb4
-rw-r--r--lib/nickserver/hkp/source.rb20
-rw-r--r--lib/nickserver/request_handler.rb32
-rw-r--r--test/integration/nickserver_test.rb14
-rw-r--r--test/unit/request_handler_test.rb35
5 files changed, 74 insertions, 31 deletions
diff --git a/lib/nickserver/hkp/client.rb b/lib/nickserver/hkp/client.rb
index 1fbe7a2..d53daad 100644
--- a/lib/nickserver/hkp/client.rb
+++ b/lib/nickserver/hkp/client.rb
@@ -27,8 +27,8 @@ module Nickserver; module Hkp
#
# fetches ascii armored OpenPGP public key from the keyserver
#
- def get_key_by_fingerprint(fingerprint, &block)
- get op: 'get', search: "0x" + fingerprint, &block
+ def get_key_by_fingerprint(fingerprint)
+ get op: 'get', search: "0x" + fingerprint
end
protected
diff --git a/lib/nickserver/hkp/source.rb b/lib/nickserver/hkp/source.rb
index 0d79856..e104aa8 100644
--- a/lib/nickserver/hkp/source.rb
+++ b/lib/nickserver/hkp/source.rb
@@ -18,7 +18,7 @@ module Nickserver; module Hkp
status, response = search(nick)
if status == 200
best = pick_best_key(response)
- get_key_by_fingerprint(nick, best.keyid)
+ get_key_by_fingerprint(best.keyid, nick)
else
Nickserver::Response.new(status, response)
end
@@ -30,6 +30,15 @@ module Nickserver; module Hkp
return parser.status_for(nick), parser.response_for(nick)
end
+ def get_key_by_fingerprint(fingerprint, nick = nil)
+ status, response = client.get_key_by_fingerprint fingerprint
+ if status == 200
+ Response.new nick, response
+ else
+ Nickserver::Response.new status, "HKP Request failed"
+ end
+ end
+
protected
#
@@ -42,15 +51,6 @@ module Nickserver; module Hkp
key_info_list.sort {|a,b| a.creationdate <=> b.creationdate}.last
end
- def get_key_by_fingerprint(nick, fingerprint)
- status, response = client.get_key_by_fingerprint fingerprint
- if status == 200
- Response.new nick, response
- else
- Nickserver::Response.new status, "HKP Request failed"
- end
- end
-
def client
@client ||= Client.new(adapter)
end
diff --git a/lib/nickserver/request_handler.rb b/lib/nickserver/request_handler.rb
index 608db83..856ec5d 100644
--- a/lib/nickserver/request_handler.rb
+++ b/lib/nickserver/request_handler.rb
@@ -10,14 +10,14 @@ module Nickserver
end
def respond_to(params, headers)
- email = get_email_from_params(params)
- if email.nil?
- send_not_found
- elsif email.invalid?
- send_error("Not a valid address")
+ if params && params["address"] && params["address"].any?
+ by_email(params, headers)
+ elsif params && params["fingerprint"] && params["fingerprint"].any?
+ by_fingerprint(params)
else
- send_key(email, headers)
+ send_not_found
end
+
rescue RuntimeError => exc
puts "Error: #{exc}"
puts exc.backtrace
@@ -26,9 +26,23 @@ module Nickserver
protected
- def get_email_from_params(params)
- if params && params["address"] && params["address"].any?
- EmailAddress.new(params["address"].first)
+ def by_email(params, headers)
+ email = EmailAddress.new(params["address"].first)
+ if email.invalid?
+ send_error("Not a valid address")
+ else
+ send_key(email, headers)
+ end
+ end
+
+ def by_fingerprint(params)
+ fingerprint = params["fingerprint"].first
+ if fingerprint.length == 40 && !fingerprint[/\H/]
+ source = Nickserver::Hkp::Source.new(adapter)
+ key_response = source.get_key_by_fingerprint(fingerprint)
+ send_response key_response.status, key_response.content
+ else
+ send_error('Fingerprint invalid: ' + fingerprint)
end
end
diff --git a/test/integration/nickserver_test.rb b/test/integration/nickserver_test.rb
index 710c3a1..9aac6b4 100644
--- a/test/integration/nickserver_test.rb
+++ b/test/integration/nickserver_test.rb
@@ -26,7 +26,7 @@ class NickserverTest < Minitest::Test
super
end
- def test_GET_served_via_SKS
+ def test_GET_key_by_email_address_served_via_SKS
uid = 'cloudadmin@leap.se'
key_id = 'E818C478D3141282F7590D29D041EB11B1647490'
stub_sks_vindex_reponse(uid, body: file_content(:leap_vindex_result))
@@ -40,15 +40,13 @@ class NickserverTest < Minitest::Test
end
end
- def test_POST_served_via_SKS
- uid = 'cloudadmin@leap.se'
- key_id = 'E818C478D3141282F7590D29D041EB11B1647490'
- stub_sks_vindex_reponse(uid, body: file_content(:leap_vindex_result))
- stub_sks_get_reponse(key_id, body: file_content(:leap_public_key))
+ def test_GET_key_by_fingerprint_served_via_SKS
+ fingerprint = 'E818C478D3141282F7590D29D041EB11B1647490'
+ stub_sks_get_reponse(fingerprint, body: file_content(:leap_public_key))
start do
- params = {body: {"address" => uid}}
- post(params) do |response|
+ params = {query: {"fingerprint" => fingerprint}}
+ get(params) do |response|
assert_equal file_content(:leap_public_key), JSON.parse(response.to_s)["openpgp"]
end
end
diff --git a/test/unit/request_handler_test.rb b/test/unit/request_handler_test.rb
index ef656f9..420e74b 100644
--- a/test/unit/request_handler_test.rb
+++ b/test/unit/request_handler_test.rb
@@ -18,11 +18,42 @@ class Nickserver::RequestHandlerTest < Minitest::Test
assert_response status: 500, content: "500 HTTP request must include a Host header.\n"
end
+ def test_email_from_hkp
+ handle address: ['valid@email.tld'], headers: { "Host" => "http://nickserver.me" }
+ source = Minitest::Mock.new
+ source.expect :query, Nickserver::Response.new(200, "fake content"), [Nickserver::EmailAddress]
+ Nickserver::Hkp::Source.stub :new, source do
+ assert_response status: 200, content: "200 fake content"
+ end
+ end
+
+ def test_fingerprint_to_short
+ handle fingerprint: ['44F2F455E28']
+ assert_response status: 500, content: "500 Fingerprint invalid: 44F2F455E28\n"
+ end
+
+ def test_fingerprint_is_not_hex
+ handle fingerprint: ['X36E738D69173C13Z709E44F2F455E2824D18DDX']
+ assert_response status: 500,
+ content: "500 Fingerprint invalid: X36E738D69173C13Z709E44F2F455E2824D18DDX\n"
+ end
+
+ def test_get_key_with_fingerprint_from_hkp
+ handle fingerprint: ['E36E738D69173C13D709E44F2F455E2824D18DDF']
+ source = Minitest::Mock.new
+ source.expect :get_key_by_fingerprint,
+ Nickserver::Response.new(200, "fake fingerprint"),
+ ['E36E738D69173C13D709E44F2F455E2824D18DDF']
+ Nickserver::Hkp::Source.stub :new, source do
+ assert_response status: 200, content: "200 fake fingerprint"
+ end
+ end
+
protected
- def handle(params = {}, headers = {})
+ def handle(params = {})
+ @headers = params.delete(:headers) || {}
@params = Hash[ params.map{ |k,v| [k.to_s, v] } ]
- @headers = headers
end
def assert_response(args)