summaryrefslogtreecommitdiff
path: root/lib/nickserver/request_handler.rb
blob: 856ec5d4731a4eceed21f5a082662dac1ef2aa8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
require 'nickserver/hkp/source'
require 'nickserver/couch_db/source'

module Nickserver
  class RequestHandler

    def initialize(responder, adapter)
      @responder = responder
      @adapter = adapter
    end

    def respond_to(params, headers)
      if params && params["address"] && params["address"].any?
        by_email(params, headers)
      elsif params && params["fingerprint"] && params["fingerprint"].any?
        by_fingerprint(params)
      else
        send_not_found
      end

    rescue RuntimeError => exc
      puts "Error: #{exc}"
      puts exc.backtrace
      send_error(exc.to_s)
    end

    protected

    def by_email(params, headers)
      email = EmailAddress.new(params["address"].first)
      if email.invalid?
        send_error("Not a valid address")
      else
        send_key(email, headers)
      end
    end

    def by_fingerprint(params)
      fingerprint = params["fingerprint"].first
      if fingerprint.length == 40 && !fingerprint[/\H/]
        source = Nickserver::Hkp::Source.new(adapter)
        key_response = source.get_key_by_fingerprint(fingerprint)
        send_response key_response.status, key_response.content
      else
        send_error('Fingerprint invalid: ' + fingerprint)
      end
    end

    def send_key(email, headers)
      if local_address?(email, headers)
        source = Nickserver::CouchDB::Source.new(adapter)
      else
        source = Nickserver::Hkp::Source.new(adapter)
      end
      response = source.query(email)
      send_response response.status, response.content
    rescue MissingHostHeader
      send_error("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, headers)
      email.domain?(Config.domain || domain_from_headers(headers))
    end

    # no domain configured, use Host header
    def domain_from_headers(headers)
      host_header = headers['Host']
      raise MissingHostHeader if host_header.nil?
      host_header.split(':')[0].strip.sub(/^nicknym\./, '')
    end

    def send_error(msg = "not supported")
      send_response 500, "500 #{msg}\n"
    end

    def send_not_found(msg = "Not Found")
      send_response 404, "404 #{msg}\n"
    end

    def send_response(status = 200, content = '')
      responder.respond status, content
    end

    attr_reader :responder, :adapter

    class MissingHostHeader < StandardError
    end
  end
end