summaryrefslogtreecommitdiff
path: root/lib/nickserver/hkp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/nickserver/hkp')
-rw-r--r--lib/nickserver/hkp/client.rb31
-rw-r--r--lib/nickserver/hkp/key_info.rb13
-rw-r--r--lib/nickserver/hkp/parse_key_info.rb19
-rw-r--r--lib/nickserver/hkp/response.rb2
-rw-r--r--lib/nickserver/hkp/source.rb29
-rw-r--r--lib/nickserver/hkp/v_index_response.rb15
6 files changed, 47 insertions, 62 deletions
diff --git a/lib/nickserver/hkp/client.rb b/lib/nickserver/hkp/client.rb
index d53daad..3dbb1de 100644
--- a/lib/nickserver/hkp/client.rb
+++ b/lib/nickserver/hkp/client.rb
@@ -1,18 +1,15 @@
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
+module 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.
class Client
-
def initialize(adapter)
@adapter = adapter
end
@@ -20,7 +17,7 @@ module Nickserver; module Hkp
#
# used to fetch an array of KeyInfo objects that match the given email
#
- def get_key_infos_by_email(email, &block)
+ def get_key_infos_by_email(email)
get op: 'vindex', search: email.to_s, fingerprint: 'on'
end
@@ -28,7 +25,7 @@ module Nickserver; module Hkp
# fetches ascii armored OpenPGP public key from the keyserver
#
def get_key_by_fingerprint(fingerprint)
- get op: 'get', search: "0x" + fingerprint
+ get op: 'get', search: '0x' + fingerprint
end
protected
@@ -37,9 +34,9 @@ module Nickserver; module Hkp
def get(query)
# in practice, exact=on seems to have no effect
- query = {exact: 'on', options: 'mr'}.merge query
+ query = { exact: 'on', options: 'mr' }.merge query
response = adapter.get Config.hkp_url, query: query
- return response
+ response
end
end
-end; end
+end
diff --git a/lib/nickserver/hkp/key_info.rb b/lib/nickserver/hkp/key_info.rb
index d4ecf10..ed38643 100644
--- a/lib/nickserver/hkp/key_info.rb
+++ b/lib/nickserver/hkp/key_info.rb
@@ -32,26 +32,22 @@ module Nickserver::Hkp
def creationdate
@creationdate ||= begin
- if @creationdate_s
- Time.at(@creationdate_s.to_i)
- end
+ Time.at(@creationdate_s.to_i) if @creationdate_s
end
end
def expirationdate
@expirationdate ||= begin
- if @expirationdate_s
- Time.at(@expirationdate_s.to_i)
- end
+ Time.at(@expirationdate_s.to_i) if @expirationdate_s
end
end
def rsa?
- @algo == "1"
+ @algo == '1'
end
def dsa?
- @algo == "17"
+ @algo == '17'
end
def revoked?
@@ -66,5 +62,4 @@ module Nickserver::Hkp
@flags =~ /e/
end
end
-
end
diff --git a/lib/nickserver/hkp/parse_key_info.rb b/lib/nickserver/hkp/parse_key_info.rb
index 2f928a0..09dc69e 100644
--- a/lib/nickserver/hkp/parse_key_info.rb
+++ b/lib/nickserver/hkp/parse_key_info.rb
@@ -5,9 +5,8 @@
# Parsing a response with 12 keys and validating them takes 2ms.
# So no need for memoization and making things more complex.
#
-module Nickserver; module Hkp
+module Nickserver::Hkp
class ParseKeyInfo
-
# for this regexp to work, the source text must end in a trailing "\n",
# which the output of sks does.
MATCH_PUB_KEY = /(^pub:.+?\n(^uid:.+?\n)+)/m
@@ -47,7 +46,7 @@ module Nickserver; module Hkp
if errors(uid).any?
error_messages(uid).join "\n"
else
- "Could not fetch keyinfo."
+ 'Could not fetch keyinfo.'
end
end
@@ -63,7 +62,7 @@ module Nickserver; module Hkp
end
def errors(uid)
- key_infos(uid).map{|key| error_for_key(key) }.compact
+ key_infos(uid).map { |key| error_for_key(key) }.compact
end
def error_messages(uid)
@@ -97,16 +96,16 @@ module Nickserver; module Hkp
def error_for_key(key)
if key.keylen < 2048
- "key length is too short."
+ 'key length is too short.'
elsif key.expired?
- "key expired."
+ 'key expired.'
elsif key.revoked?
- "key revoked."
+ 'key revoked.'
elsif key.disabled?
- "key disabled."
+ 'key disabled.'
elsif key.expirationdate && key.expirationdate < Time.now
- "key expired"
+ 'key expired'
end
end
end
-end; end
+end
diff --git a/lib/nickserver/hkp/response.rb b/lib/nickserver/hkp/response.rb
index c52e25f..2cc69d3 100644
--- a/lib/nickserver/hkp/response.rb
+++ b/lib/nickserver/hkp/response.rb
@@ -1,6 +1,5 @@
module Nickserver::Hkp
class Response
-
attr_reader :status, :content
def initialize(uid, key)
@@ -13,6 +12,5 @@ module Nickserver::Hkp
def format_response(map)
map.to_json
end
-
end
end
diff --git a/lib/nickserver/hkp/source.rb b/lib/nickserver/hkp/source.rb
index 82c94a0..d7c86a3 100644
--- a/lib/nickserver/hkp/source.rb
+++ b/lib/nickserver/hkp/source.rb
@@ -2,24 +2,21 @@ require 'nickserver/source'
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
+require 'nickserver/hkp/parse_key_info'
+require 'nickserver/hkp/key_info'
+
+module Nickserver::Hkp
+ #
+ # Fetch keys via HKP
+ # http://tools.ietf.org/html/draft-shaw-openpgp-hkp-00
+ #
class Source < Nickserver::Source
-
def query(nick)
status, response = search(nick)
if status == 200
best = pick_best_key(response)
get_key_by_fingerprint(best.keyid, nick)
- elsif status != 404 # 404 means no key found and we proceed
+ elsif status != 404 # 404 means no key found and we proceed
Nickserver::Response.new(status, response)
end
end
@@ -27,7 +24,7 @@ module Nickserver; module Hkp
def search(nick)
status, response = client.get_key_infos_by_email(nick)
parser = ParseKeyInfo.new status, response
- return parser.status_for(nick), parser.response_for(nick)
+ [parser.status_for(nick), parser.response_for(nick)]
end
def get_key_by_fingerprint(fingerprint, nick = nil)
@@ -35,7 +32,7 @@ module Nickserver; module Hkp
if status == 200
Response.new nick, response
else
- Nickserver::Response.new status, "HKP Request failed"
+ Nickserver::Response.new status, 'HKP Request failed'
end
end
@@ -48,11 +45,11 @@ module Nickserver; module Hkp
# 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
+ key_info_list.sort_by(&:creationdate).last
end
def client
@client ||= Client.new(adapter)
end
end
-end; end
+end
diff --git a/lib/nickserver/hkp/v_index_response.rb b/lib/nickserver/hkp/v_index_response.rb
index 865d476..a2a7b0d 100644
--- a/lib/nickserver/hkp/v_index_response.rb
+++ b/lib/nickserver/hkp/v_index_response.rb
@@ -9,7 +9,6 @@ require 'nickserver/hkp/key_info'
# So no need for memoization and making things more complex.
module Nickserver::Hkp
class VIndexResponse
-
# for this regexp to work, the source text must end in a trailing "\n",
# which the output of sks does.
MATCH_PUB_KEY = /(^pub:.+?\n(^uid:.+?\n)+)/m
@@ -36,7 +35,7 @@ module Nickserver::Hkp
if errors.any?
error_messages.join "\n"
else
- "Could not fetch keyinfo."
+ 'Could not fetch keyinfo.'
end
end
@@ -53,7 +52,7 @@ module Nickserver::Hkp
end
def errors
- key_infos.map{|key| error_for_key(key) }.compact
+ key_infos.map { |key| error_for_key(key) }.compact
end
def error_messages
@@ -81,15 +80,15 @@ module Nickserver::Hkp
def error_for_key(key)
if key.keylen < 2048
- "key length is too short."
+ 'key length is too short.'
elsif key.expired?
- "key expired."
+ 'key expired.'
elsif key.revoked?
- "key revoked."
+ 'key revoked.'
elsif key.disabled?
- "key disabled."
+ 'key disabled.'
elsif key.expirationdate && key.expirationdate < Time.now
- "key expired"
+ 'key expired'
end
end
end