summaryrefslogtreecommitdiff
path: root/lib/nickserver/hkp/source.rb
diff options
context:
space:
mode:
authorazul <azul@riseup.net>2016-06-15 14:22:08 +0200
committerGitHub <noreply@github.com>2016-06-15 14:22:08 +0200
commit3c6dc3d7b902b46b82a3d8cd38bd3fa896024beb (patch)
tree559b3998465248138b6f24a76b6b3e55e60f3986 /lib/nickserver/hkp/source.rb
parent5cad637a4a2a3de6b95ff1204fc29174e18b3124 (diff)
parent93258bd6fe6247e7af67f423243eba9808e920ee (diff)
Merge pull request #3 from azul/refactor/transport-adapters
Refactor em specifics into http adapter
Diffstat (limited to 'lib/nickserver/hkp/source.rb')
-rw-r--r--lib/nickserver/hkp/source.rb66
1 files changed, 66 insertions, 0 deletions
diff --git a/lib/nickserver/hkp/source.rb b/lib/nickserver/hkp/source.rb
new file mode 100644
index 0000000..cae3e01
--- /dev/null
+++ b/lib/nickserver/hkp/source.rb
@@ -0,0 +1,66 @@
+require 'nickserver/response'
+require 'nickserver/hkp/response'
+require 'nickserver/hkp/client'
+require "nickserver/hkp/parse_key_info"
+require "nickserver/hkp/key_info"
+
+
+#
+# Fetch keys via HKP
+# http://tools.ietf.org/html/draft-shaw-openpgp-hkp-00
+#
+
+module Nickserver; module Hkp
+ class Source
+
+ def initialize(adapter)
+ @adapter = adapter
+ end
+
+ def query(nick, &block)
+ search(nick) do |status, response|
+ if status == 200
+ best = pick_best_key(response)
+ get_key_by_fingerprint(nick, best.keyid, &block)
+ else
+ yield Nickserver::Response.new(status, response)
+ end
+ end
+ end
+
+ def search(nick, &block)
+ client.get_key_infos_by_email(nick) do |status, response|
+ parser = ParseKeyInfo.new status, response
+ yield parser.status_for(nick), parser.response_for(nick)
+ end
+ end
+
+ protected
+
+ attr_reader :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
+
+ def get_key_by_fingerprint(nick, fingerprint)
+ client.get_key_by_fingerprint fingerprint do |status, response|
+ if status == 200
+ yield Response.new nick, response
+ else
+ yield Nickserver::Response.new status, "HKP Request failed"
+ end
+ end
+ end
+
+ def client
+ @client ||= Client.new(adapter)
+ end
+ end
+end; end