From 48502085e5aa7a1d8d430fc1dd4913f34e818afa Mon Sep 17 00:00:00 2001 From: Azul Date: Tue, 14 Jun 2016 09:36:29 +0200 Subject: introduce Hkp::Client wraps the hkp protocol --- lib/nickserver/hkp/client.rb | 44 ++++++++++++++++++++++++++++++++++++ lib/nickserver/hkp/fetch_key_info.rb | 10 +++++--- lib/nickserver/hkp/source.rb | 28 +++++++++++------------ 3 files changed, 64 insertions(+), 18 deletions(-) create mode 100644 lib/nickserver/hkp/client.rb (limited to 'lib') diff --git a/lib/nickserver/hkp/client.rb b/lib/nickserver/hkp/client.rb new file mode 100644 index 0000000..6bd239d --- /dev/null +++ b/lib/nickserver/hkp/client.rb @@ -0,0 +1,44 @@ +require 'nickserver/hkp' + +# +# Client for the HKP protocol. +# +# This is not a complete implementation - only the parts we need. +# Instantiate with an adapter that will take care of the http requests. +# +# For each request we yield http_status and the response content just +# like the adapter does. + + +module Nickserver; module Hkp + class Client + + def initialize(adapter) + @adapter = adapter + end + + # + # used to fetch an array of KeyInfo objects that match the given email + # + def get_key_infos_by_email(email, &block) + get op: 'vindex', search: email, fingerprint: 'on', &block + end + + # + # fetches ascii armored OpenPGP public key from the keyserver + # + def get_key_by_fingerprint(fingerprint, &block) + get op: 'get', search: "0x" + fingerprint, &block + end + + protected + + attr_reader :adapter + + def get(query, &block) + # in practice, exact=on seems to have no effect + query = {exact: 'on', options: 'mr'}.merge query + adapter.get Config.hkp_url, query: query, &block + end + end +end; end diff --git a/lib/nickserver/hkp/fetch_key_info.rb b/lib/nickserver/hkp/fetch_key_info.rb index b23af15..b8685bc 100644 --- a/lib/nickserver/hkp/fetch_key_info.rb +++ b/lib/nickserver/hkp/fetch_key_info.rb @@ -1,3 +1,5 @@ +require 'nickserver/hkp/client' + # # used to fetch an array of KeyInfo objects that match the given uid. # @@ -10,9 +12,7 @@ module Nickserver; module Hkp end def search(uid, &block) - # in practice, exact=on seems to have no effect - params = {op: 'vindex', search: uid, exact: 'on', options: 'mr', fingerprint: 'on'} - adapter.get(Config.hkp_url, query: params) do |status, response| + client.get_key_infos_by_email(uid) do |status, response| parser = ParseKeyInfo.new status, response yield parser.status_for(uid), parser.response_for(uid) end @@ -21,6 +21,10 @@ module Nickserver; module Hkp protected attr_reader :adapter + def client + @client ||= Client.new(adapter) + end + end end; end diff --git a/lib/nickserver/hkp/source.rb b/lib/nickserver/hkp/source.rb index 1af8ac9..cbff91a 100644 --- a/lib/nickserver/hkp/source.rb +++ b/lib/nickserver/hkp/source.rb @@ -1,5 +1,6 @@ require 'nickserver/response' require 'nickserver/hkp/response' +require 'nickserver/hkp/client' # # Fetch keys via HKP @@ -29,11 +30,17 @@ module Nickserver; module Hkp attr_reader :adapter # - # fetches ascii armored OpenPGP public key from the keyserver + # for now, just pick the newest key. # - def get_key_by_fingerprint(nick, key_id) - params = {op: 'get', search: "0x" + key_id, exact: 'on', options: 'mr'} - adapter.get Config.hkp_url, query: params do |status, response| + # 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 @@ -42,17 +49,8 @@ module Nickserver; module Hkp end 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 + def client + @client ||= Client.new(adapter) end end - end; end -- cgit v1.2.3