summaryrefslogtreecommitdiff
path: root/lib/nickserver
diff options
context:
space:
mode:
Diffstat (limited to 'lib/nickserver')
-rw-r--r--lib/nickserver/dispatcher.rb4
-rw-r--r--lib/nickserver/request_handlers/email_handler.rb40
-rw-r--r--lib/nickserver/request_handlers/invalid_email_handler.rb15
-rw-r--r--lib/nickserver/request_handlers/local_email_handler.rb32
4 files changed, 55 insertions, 36 deletions
diff --git a/lib/nickserver/dispatcher.rb b/lib/nickserver/dispatcher.rb
index 833a2ad..8bcfd05 100644
--- a/lib/nickserver/dispatcher.rb
+++ b/lib/nickserver/dispatcher.rb
@@ -14,6 +14,8 @@
#
require 'nickserver/request'
+require 'nickserver/request_handlers/invalid_email_handler'
+require 'nickserver/request_handlers/local_email_handler'
require 'nickserver/request_handlers/email_handler'
require 'nickserver/request_handlers/fingerprint_handler'
@@ -45,6 +47,8 @@ module Nickserver
def handler_chain
[
+ RequestHandlers::InvalidEmailHandler.new,
+ RequestHandlers::LocalEmailHandler.new,
RequestHandlers::EmailHandler.new,
RequestHandlers::FingerprintHandler.new,
Proc.new { Nickserver::Response.new(404, "Not Found\n") }
diff --git a/lib/nickserver/request_handlers/email_handler.rb b/lib/nickserver/request_handlers/email_handler.rb
index b163b27..96fcaf3 100644
--- a/lib/nickserver/request_handlers/email_handler.rb
+++ b/lib/nickserver/request_handlers/email_handler.rb
@@ -1,7 +1,5 @@
require 'nickserver/email_address'
-require 'nickserver/error_response'
require 'nickserver/hkp/source'
-require 'nickserver/couch_db/source'
module Nickserver
module RequestHandlers
@@ -9,46 +7,16 @@ module Nickserver
def call(request)
return unless request.email
- handle_request(request)
- end
-
- protected
-
- def handle_request(request)
email = EmailAddress.new(request.email)
- if email.invalid?
- ErrorResponse.new("Not a valid address")
- else
- send_key(email, request)
- end
- end
-
- def send_key(email, request)
- if local_address?(email, request)
- source = Nickserver::CouchDB::Source.new
- else
- source = Nickserver::Hkp::Source.new
- end
source.query(email)
- rescue MissingHostHeader
- ErrorResponse.new("HTTP request must include a Host header.")
end
- #
- # Return true if the user address is for a user of this service provider.
- # e.g. if the provider is example.org, then alice@example.org returns true.
- #
- # If 'domain' is not configured, we rely on the Host header of the HTTP request.
- #
- def local_address?(email, request)
- domain = Config.domain || request.domain
- raise MissingHostHeader if domain == ''
- email.domain? domain
- end
- end
+ protected
+ def source
+ Nickserver::Hkp::Source.new
+ end
- class MissingHostHeader < StandardError
end
end
end
diff --git a/lib/nickserver/request_handlers/invalid_email_handler.rb b/lib/nickserver/request_handlers/invalid_email_handler.rb
new file mode 100644
index 0000000..eaf8156
--- /dev/null
+++ b/lib/nickserver/request_handlers/invalid_email_handler.rb
@@ -0,0 +1,15 @@
+require 'nickserver/email_address'
+require 'nickserver/error_response'
+
+module Nickserver
+ module RequestHandlers
+ class InvalidEmailHandler
+ def call(request)
+ return unless request.email
+ email = EmailAddress.new(request.email)
+ ErrorResponse.new("Not a valid address") if email.invalid?
+ end
+
+ end
+ end
+end
diff --git a/lib/nickserver/request_handlers/local_email_handler.rb b/lib/nickserver/request_handlers/local_email_handler.rb
new file mode 100644
index 0000000..1f2abc2
--- /dev/null
+++ b/lib/nickserver/request_handlers/local_email_handler.rb
@@ -0,0 +1,32 @@
+require 'nickserver/email_address'
+require 'nickserver/error_response'
+require 'nickserver/couch_db/source'
+
+module Nickserver
+ module RequestHandlers
+ class LocalEmailHandler
+
+ def call(request)
+ return nil unless request.email
+ domain = Config.domain || request.domain
+ return missing_domain_response if domain.nil? || domain == ''
+ email = EmailAddress.new(request.email)
+ return nil unless email.domain?(domain)
+ source.query email
+ end
+
+ protected
+
+ attr_reader :domain
+
+ def source
+ Nickserver::CouchDB::Source.new
+ end
+
+ def missing_domain_response
+ ErrorResponse.new "HTTP request must include a Host header."
+ end
+
+ end
+ end
+end