diff options
-rw-r--r-- | lib/nickserver.rb | 1 | ||||
-rw-r--r-- | lib/nickserver/hkp/fetch_key.rb | 59 | ||||
-rw-r--r-- | lib/nickserver/hkp/source.rb | 51 | ||||
-rw-r--r-- | test/integration/hkp_test.rb | 3 |
4 files changed, 46 insertions, 68 deletions
diff --git a/lib/nickserver.rb b/lib/nickserver.rb index 22d4b43..16f7e85 100644 --- a/lib/nickserver.rb +++ b/lib/nickserver.rb @@ -3,7 +3,6 @@ require "nickserver/version" require "nickserver/config" require "nickserver/email_address" -require "nickserver/hkp/fetch_key" require "nickserver/hkp/fetch_key_info" require "nickserver/hkp/parse_key_info" require "nickserver/hkp/key_info" diff --git a/lib/nickserver/hkp/fetch_key.rb b/lib/nickserver/hkp/fetch_key.rb deleted file mode 100644 index 10c4926..0000000 --- a/lib/nickserver/hkp/fetch_key.rb +++ /dev/null @@ -1,59 +0,0 @@ -require 'em-http' -require 'nickserver/response' -require 'nickserver/hkp/response' - -# -# Fetch keys via HKP -# http://tools.ietf.org/html/draft-shaw-openpgp-hkp-00 -# - -module Nickserver; module Hkp - - class FetchKey - - def initialize(adapter) - @adapter = adapter - end - - def get(nick, &block) - FetchKeyInfo.new.search(nick).callback {|key_info_list| - best = pick_best_key(key_info_list) - get_key_by_fingerprint(nick, best.keyid, &block) - }.errback {|status, msg| - yield Nickserver::Response.new(status, msg) - } - end - - # - # fetches ascii armored OpenPGP public key from the keyserver - # - def get_key_by_fingerprint(nick, key_id) - params = {op: 'get', search: "0x" + key_id, exact: 'on', options: 'mr'} - http = EventMachine::HttpRequest.new(Config.hkp_url).get(query: params) - http.callback { - status = http.response_header.status - if status != 200 - yield Nickserver::Response.new status, "HKP Request failed" - else - yield Response.new nick, http.response - end - } - http.errback { - yield Nickserver::Response.new 500, http.error - } - end - - protected - - # - # for now, just pick the newest key. - # - # in the future, we should perhaps pick the newest key - # that is signed by the oldest key. - # - def pick_best_key(key_info_list) - key_info_list.sort {|a,b| a.creationdate <=> b.creationdate}.last - end - end - -end; end diff --git a/lib/nickserver/hkp/source.rb b/lib/nickserver/hkp/source.rb index f06a931..1dd386d 100644 --- a/lib/nickserver/hkp/source.rb +++ b/lib/nickserver/hkp/source.rb @@ -1,4 +1,12 @@ -module Nickserver::Hkp +require 'nickserver/response' +require 'nickserver/hkp/response' + +# +# Fetch keys via HKP +# http://tools.ietf.org/html/draft-shaw-openpgp-hkp-00 +# + +module Nickserver; module Hkp class Source def initialize(adapter) @@ -6,15 +14,44 @@ module Nickserver::Hkp end def query(nick, &block) - fetcher.get(nick, &block) + FetchKeyInfo.new.search(nick).callback {|key_info_list| + best = pick_best_key(key_info_list) + get_key_by_fingerprint(nick, best.keyid, &block) + }.errback {|status, msg| + yield Nickserver::Response.new(status, msg) + } end - protected + # + # fetches ascii armored OpenPGP public key from the keyserver + # + def get_key_by_fingerprint(nick, key_id) + params = {op: 'get', search: "0x" + key_id, exact: 'on', options: 'mr'} + http = EventMachine::HttpRequest.new(Config.hkp_url).get(query: params) + http.callback { + status = http.response_header.status + if status != 200 + yield Nickserver::Response.new status, "HKP Request failed" + else + yield Response.new nick, http.response + end + } + http.errback { + yield Nickserver::Response.new 500, http.error + } + end - attr_reader :adapter + protected - def fetcher - Nickserver::Hkp::FetchKey.new(adapter) + # + # for now, just pick the newest key. + # + # in the future, we should perhaps pick the newest key + # that is signed by the oldest key. + # + def pick_best_key(key_info_list) + key_info_list.sort {|a,b| a.creationdate <=> b.creationdate}.last end end -end + +end; end diff --git a/test/integration/hkp_test.rb b/test/integration/hkp_test.rb index 6ef52fe..0410c4a 100644 --- a/test/integration/hkp_test.rb +++ b/test/integration/hkp_test.rb @@ -1,4 +1,5 @@ require 'test_helper' +require 'nickserver/hkp/source' class HkpTest < Minitest::Test @@ -110,7 +111,7 @@ class HkpTest < Minitest::Test def assert_response_for_uid(uid, &block) EM.run do - Nickserver::Hkp::FetchKey.new(nil).get(uid, &block) + Nickserver::Hkp::Source.new(nil).query(uid, &block) EM.stop end end |