diff options
Diffstat (limited to 'lib/nickserver/request_handlers')
-rw-r--r-- | lib/nickserver/request_handlers/email_handler.rb | 49 | ||||
-rw-r--r-- | lib/nickserver/request_handlers/fingerprint_handler.rb | 20 |
2 files changed, 69 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 diff --git a/lib/nickserver/request_handlers/fingerprint_handler.rb b/lib/nickserver/request_handlers/fingerprint_handler.rb new file mode 100644 index 0000000..3202c41 --- /dev/null +++ b/lib/nickserver/request_handlers/fingerprint_handler.rb @@ -0,0 +1,20 @@ +require 'nickserver/hkp/source' +require 'nickserver/error_response' + +module Nickserver + module RequestHandlers + class FingerprintHandler + + def call(request) + fingerprint = request.fingerprint + if fingerprint.length == 40 && !fingerprint[/\H/] + source = Nickserver::Hkp::Source.new + source.get_key_by_fingerprint(fingerprint) + else + ErrorResponse.new('Fingerprint invalid: ' + fingerprint) + end + end + + end + end +end |