summaryrefslogtreecommitdiff
path: root/lib/nickserver/wkd
diff options
context:
space:
mode:
Diffstat (limited to 'lib/nickserver/wkd')
-rw-r--r--lib/nickserver/wkd/source.rb35
-rw-r--r--lib/nickserver/wkd/url.rb37
2 files changed, 44 insertions, 28 deletions
diff --git a/lib/nickserver/wkd/source.rb b/lib/nickserver/wkd/source.rb
index 01f376e..43f0b2e 100644
--- a/lib/nickserver/wkd/source.rb
+++ b/lib/nickserver/wkd/source.rb
@@ -1,16 +1,35 @@
require 'nickserver/source'
require 'nickserver/response'
+require 'nickserver/wkd/url'
+require 'nickserver/hkp/response'
-module Nickserver
- module Wkd
- class Source < Nickserver::Source
-
- def query(email)
- url = Url.new(email)
- status, body = adapter.get url
- return Nickserver::Response.new(status, body)
+module Nickserver::Wkd
+ # Query the web key directory for a given email address
+ class Source < Nickserver::Source
+ def query(email)
+ url = Url.new(email)
+ status, blob = adapter.get url
+ if status == 200
+ Nickserver::Hkp::Response.new(email.to_s, armor_key(blob))
end
+ end
+
+ protected
+
+ def armor_key(blob)
+ header + encode(blob) + footer
+ end
+
+ def encode(blob)
+ Base64.strict_encode64(blob).scan(/.{1,64}/).join "\n"
+ end
+
+ def header
+ "-----BEGIN PGP PUBLIC KEY BLOCK-----\n\n"
+ end
+ def footer
+ "\n-----END PGP PUBLIC KEY BLOCK-----\n"
end
end
end
diff --git a/lib/nickserver/wkd/url.rb b/lib/nickserver/wkd/url.rb
index 965e7ec..0ccff38 100644
--- a/lib/nickserver/wkd/url.rb
+++ b/lib/nickserver/wkd/url.rb
@@ -1,31 +1,28 @@
require 'digest/sha1'
require 'zbase32'
-module Nickserver
- module Wkd
- class Url
-
- def initialize(email)
- @domain = email.domain.downcase
- @local_part = email.local_part.downcase
- end
+module Nickserver::Wkd
+ # The url to lookup the given email address in the web key directory.
+ class Url
+ def initialize(email)
+ @domain = email.domain.downcase
+ @local_part = email.local_part.downcase
+ end
- def to_s
- "https://#{domain}/.well-known/openpgpkey" +
- "/hu/#{domain}/#{encoded_digest}"
- end
+ def to_s
+ "https://#{domain}/.well-known/openpgpkey/hu/#{encoded_digest}"
+ end
- protected
+ protected
- attr_reader :domain, :local_part
+ attr_reader :domain, :local_part
- def encoded_digest
- ZBase32.encode32(digest.to_i(16).to_s(2))
- end
+ def encoded_digest
+ ZBase32.encode32(digest.to_i(16).to_s(2))
+ end
- def digest
- Digest::SHA1.hexdigest local_part
- end
+ def digest
+ Digest::SHA1.hexdigest local_part
end
end
end