Class: Hkp

Inherits:
Object
  • Object
show all
Defined in:
lib/hkp.rb

Overview

simple HKP client for public key search and retrieval

Defined Under Namespace

Classes: Client, InvalidResponse, TooManyRedirects

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Hkp

Returns a new instance of Hkp.



73
74
75
76
77
78
79
# File 'lib/hkp.rb', line 73

def initialize(options = {})
  if String === options
    options = { keyserver: options }
  end
  @keyserver = options.delete(:keyserver) || lookup_keyserver || 'hkp://keyserver.ubuntu.com'
  @options = { raise_errors: true }.merge options
end

Instance Method Details

#fetch(id) ⇒ Object

returns the key data as returned from the server as a string



112
113
114
115
116
117
118
119
# File 'lib/hkp.rb', line 112

def fetch(id)
  result = hkp_client.get "/pks/lookup?op=get&options=mr&search=0x#{URI.encode_www_form_component(id)}"
  return clean_key(result) if result

rescue Exception
  raise $! if raise_errors?
  nil
end

#fetch_and_import(id) ⇒ Object

fetches key data by id and imports the found key(s) into GPG, returning the full hex fingerprints of the imported key(s) as an array. Given there are no collisions with the id given / the server has returned exactly one key this will be a one element array.



125
126
127
128
129
130
131
# File 'lib/hkp.rb', line 125

def fetch_and_import(id)
  if key = fetch(id)
    GPGME::Key.import(key).imports.map(&:fpr)
  end
rescue Exception
  raise $! if raise_errors?
end

#raise_errors?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/hkp.rb', line 81

def raise_errors?
  !!@options[:raise_errors]
end

#search(name) ⇒ Object

hkp.search 'user@host.com' will return an array of arrays, one for each matching key found, containing the key id as the first elment and any further info returned by the key server in the following elements. see http://tools.ietf.org/html/draft-shaw-openpgp-hkp-00#section-5.2 for what that might be. unfortunately key servers seem to differ in how much and what info they return besides the key id



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/hkp.rb', line 93

def search(name)
  [].tap do |results|
    result = hkp_client.get "/pks/lookup?op=index&options=mr&search=#{URI.encode_www_form_component(name)}"

    result.each_line do |l|
      components = l.strip.split(':')
      if components.shift == 'pub'
        results << components
      end
    end if result
  end

rescue
  raise $! if raise_errors?
  nil
end