безопасность для веб-службы eventmachine - PullRequest
0 голосов
/ 20 марта 2012

Я предоставляю доступ к веб-сервису с помощью eventmachine и evma_httpserver следующим образом:

EM.run{
  puts "Query Server running on port 9000"
  EM.start_server '0.0.0.0', 9000, QueryEngineHttpServer
}

Я хотел бы сделать его безопасным, то есть требовать имя пользователя и пароль. Я знаю, как сделать это с помощью Sinatra, но я не использую это для этого, поэтому я не знаю, как поступить.

1 Ответ

1 голос
/ 27 апреля 2012

какой тип аутентификации вам нужен? Basic-auth или cookie на основе?

это то, что может вам помочь?

module QueryEngineHttpServer
  include EM::HttpServer

  def post_init

    # if you want the connection to be encrypted with ssl
    start_tls({:private_key_file => path_to_key_file,
               :cert_chain_file => path_to_key_file,
               :verify_peer => false})

    # don't forget to call super here !
    super
  end

  def process_http_request

    # Block which fulfills the request (generate the data)
    operation = proc do

        # depending of which kind of auth you want you should have to parse the cookie or the 'autorization' header
        auth = check_for_auth @http_cookie, @http_headers

        # create the response object to be used in the EM::defer callback        
        resp = EM::DelegatedHttpResponse.new(self)
        resp.status = auth ? 200 : 401
        resp.content = 'some content here...'
        resp
    end

    # Block which fulfills the reply (send back the data to the client)
    response = proc do |reply|
      reply.send_response      
    end

    # Let the thread pool handle request
    EM.defer(operation, response)
  end

end
...