summaryrefslogtreecommitdiff
path: root/lib/nickserver/request_handlers/email_handler.rb
diff options
context:
space:
mode:
authorAzul <azul@riseup.net>2016-08-29 09:41:12 +0200
committerAzul <azul@riseup.net>2016-08-29 09:41:12 +0200
commit58d687f927aabc8aa2fdfc46132cb71706bdde46 (patch)
tree5324fe4255437b1e1c050e1b2e087d3023ad22b5 /lib/nickserver/request_handlers/email_handler.rb
parent16d77cc03068b19791e704abfc2eb14f8cd141f0 (diff)
refactor: split up ResponseHandler
Now we have a Dispatcher and two ResponseHandlers that have the same interface. Moving towards a Chain of Responsibility pattern.
Diffstat (limited to 'lib/nickserver/request_handlers/email_handler.rb')
-rw-r--r--lib/nickserver/request_handlers/email_handler.rb49
1 files changed, 49 insertions, 0 deletions
diff --git a/lib/nickserver/request_handlers/email_handler.rb b/lib/nickserver/request_handlers/email_handler.rb
new file mode 100644
index 0000000..3f7515d
--- /dev/null
+++ b/lib/nickserver/request_handlers/email_handler.rb
@@ -0,0 +1,49 @@
+require 'nickserver/email_address'
+require 'nickserver/error_response'
+require 'nickserver/hkp/source'
+require 'nickserver/couch_db/source'
+
+module Nickserver
+ module RequestHandlers
+ class EmailHandler
+
+ def call(request)
+ email = EmailAddress.new(request.email)
+ if email.invalid?
+ ErrorResponse.new("Not a valid address")
+ else
+ send_key(email, request)
+ end
+ end
+
+ protected
+
+ 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
+
+
+ class MissingHostHeader < StandardError
+ end
+ end
+end