summaryrefslogtreecommitdiff
path: root/lib/nickserver/hkp/source.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/nickserver/hkp/source.rb')
-rw-r--r--lib/nickserver/hkp/source.rb51
1 files changed, 44 insertions, 7 deletions
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