summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorelijah <elijah@riseup.net>2013-05-15 22:03:08 -0700
committerelijah <elijah@riseup.net>2013-05-15 22:03:08 -0700
commit51dc03481a9be5148f16e8022a1b00b658739ff3 (patch)
treee370f488b6ce4b79c09778621d9f8fc2d95ee88d /lib
parentb078659defeead5b68ca2f387d0e308fa7511eb4 (diff)
add CouchDB support.
Diffstat (limited to 'lib')
-rw-r--r--lib/nickserver.rb2
-rw-r--r--lib/nickserver/config.rb2
-rw-r--r--lib/nickserver/couch/fetch_key.rb62
-rw-r--r--lib/nickserver/hkp/fetch_key.rb8
-rw-r--r--lib/nickserver/hkp/fetch_key_info.rb4
-rw-r--r--lib/nickserver/server.rb23
6 files changed, 87 insertions, 14 deletions
diff --git a/lib/nickserver.rb b/lib/nickserver.rb
index 9ddc839..f97a95f 100644
--- a/lib/nickserver.rb
+++ b/lib/nickserver.rb
@@ -2,6 +2,8 @@
require "nickserver/version"
require "nickserver/config"
+require "nickserver/couch/fetch_key"
+
require "nickserver/hkp/key_info"
require "nickserver/hkp/fetch_key_info"
require "nickserver/hkp/fetch_key"
diff --git a/lib/nickserver/config.rb b/lib/nickserver/config.rb
index 779f0a8..3f92186 100644
--- a/lib/nickserver/config.rb
+++ b/lib/nickserver/config.rb
@@ -12,6 +12,8 @@ module Nickserver
attr_accessor :couch_port
attr_accessor :couch_host
attr_accessor :couch_database
+ attr_accessor :couch_user
+ attr_accessor :couch_password
attr_accessor :port
attr_accessor :loaded
end
diff --git a/lib/nickserver/couch/fetch_key.rb b/lib/nickserver/couch/fetch_key.rb
new file mode 100644
index 0000000..d729ebc
--- /dev/null
+++ b/lib/nickserver/couch/fetch_key.rb
@@ -0,0 +1,62 @@
+require 'em-http'
+require 'json'
+
+module Nickserver; module Couch
+ class FetchKey
+ include EM::Deferrable
+
+ def initialize(options={})
+ @timeout = 5
+ end
+
+ def get(uid)
+ uid = uid.split('@').first # TEMPORARY HACK FOR NOW. in the future
+ # the database should be able to be searchable by full address
+ couch_request(uid)
+ self
+ end
+
+ protected
+
+ #
+ # curl http://localhost:5984/users/_design/User/_view/pgp_key_by_handle?key=%22bla%22\&reduce=false
+ #
+ def couch_request(uid)
+ query = {"reduce" => "false", "key" => "\"#{uid}\""}
+ request = EventMachine::HttpRequest.new("#{FetchKey.couch_url}/#{FetchKey.couch_view}").get(:timeout => @timeout, :query => query)
+ request.callback {|http|
+ if http.response_header.status != 200
+ self.fail http.response_header.status, 'Unknown Error'
+ else
+ self.succeed parse_key_from_response(uid, http.response)
+ end
+ }.errback {|http|
+ self.fail 0, http.error
+ }
+ end
+
+ def parse_key_from_response(uid, response)
+ json = JSON.load(response)
+ if json["offset"] == 0
+ self.fail 404, "Not Found"
+ else
+ return json["rows"].first["value"]
+ end
+ rescue Exception
+ self.fail 0, "Error parsing CouchDB reply"
+ end
+
+ def self.couch_view
+ "_design/User/_view/pgp_key_by_handle"
+ end
+
+ def self.couch_url
+ if Config.couch_user
+ ['http://', Config.couch_user, ':', Config.couch_password, '@', Config.couch_host, ':', Config.couch_port, '/', Config.couch_database].join
+ else
+ ['http://', Config.couch_host, ':', Config.couch_port, '/', Config.couch_database].join
+ end
+ end
+
+ end
+end; end \ No newline at end of file
diff --git a/lib/nickserver/hkp/fetch_key.rb b/lib/nickserver/hkp/fetch_key.rb
index 211ae92..6f91d8c 100644
--- a/lib/nickserver/hkp/fetch_key.rb
+++ b/lib/nickserver/hkp/fetch_key.rb
@@ -16,8 +16,8 @@ module Nickserver; module HKP
get_key_by_fingerprint(best.keyid) {|key|
self.succeed key
}
- }.errback {|msg|
- self.fail msg
+ }.errback {|status, msg|
+ self.fail status, msg
}
self
end
@@ -30,13 +30,13 @@ module Nickserver; module HKP
http = EventMachine::HttpRequest.new(Config.hkp_url).get(:query => params)
http.callback {
if http.response_header.status != 200
- self.fail http.response_header.status #"Request failed with #{http.response_header.status}: #{http.response}"
+ self.fail http.response_header.status, "HKP Request failed"
else
yield http.response
end
}
http.errback {
- self.fail http.error
+ self.fail 0, http.error
}
end
diff --git a/lib/nickserver/hkp/fetch_key_info.rb b/lib/nickserver/hkp/fetch_key_info.rb
index dce0326..b341aad 100644
--- a/lib/nickserver/hkp/fetch_key_info.rb
+++ b/lib/nickserver/hkp/fetch_key_info.rb
@@ -17,12 +17,12 @@ module Nickserver; module HKP
params = {:op => 'vindex', :search => uid, :exact => 'on', :options => 'mr', :fingerprint => 'on'}
EventMachine::HttpRequest.new(Config.hkp_url).get(:query => params).callback {|http|
if http.response_header.status != 200
- self.fail http.response_header.status
+ self.fail http.response_header.status, "Could net fetch keyinfo"
else
self.succeed parse(uid, http.response)
end
}.errback {|http|
- self.fail http.error
+ self.fail 0, http.error
}
self
end
diff --git a/lib/nickserver/server.rb b/lib/nickserver/server.rb
index 7e79b98..0bda4f1 100644
--- a/lib/nickserver/server.rb
+++ b/lib/nickserver/server.rb
@@ -5,6 +5,8 @@ require 'json'
#
# This is the main HTTP server that clients connect to in order to fetch keys
#
+# For info on EM::HttpServer, see https://github.com/eventmachine/evma_httpserver
+#
module Nickserver
class Server < EM::Connection
include EM::HttpServer
@@ -43,7 +45,7 @@ module Nickserver
send_response(:status => 500, :content => msg)
end
- def send_not_found(msg = "not found")
+ def send_not_found(msg = "404 Not Found")
send_response(:status => 404, :content => msg)
end
@@ -74,15 +76,20 @@ module Nickserver
end
def get_key_from_uid(uid)
- if local_address?(uid)
- send_not_found
+ fetcher = if local_address?(uid)
+ Nickserver::Couch::FetchKey.new
else
- Nickserver::HKP::FetchKey.new.get(uid).callback {|key|
- yield key
- }.errback {|status|
- send_response(:status => status, :content => 'could not fetch key')
- }
+ Nickserver::HKP::FetchKey.new
end
+ fetcher.get(uid).callback {|key|
+ yield key
+ }.errback {|status, msg|
+ if status == 404
+ send_not_found
+ else
+ send_response(:status => status, :content => msg)
+ end
+ }
end
def format_response(map)