9

Ruby で HTTPS クライアントを作成しようとしています。HTTPS を使用してサーバーに接続し、認証トークン (別のログイン プロセスで取得) と SSL クライアント証明書を渡します。

私はrest-clientで次のことをしています:

client = RestClient::Resource.new(url,
                         :ssl_client_cert  =>  OpenSSL::X509::Certificate.new(File.read('./certificate/client-2048.pem')),
                         :ssl_client_key   =>  OpenSSL::PKey::RSA.new(File.read('./certificate/client-2048.key'), ''),
                         :verify_ssl       =>  OpenSSL::SSL::VERIFY_NONE)

# ...

headers = {
  'X-Application' => APP_KEY,
  'X-Authentication' => @session_token,
  'content-type' => 'application/json',
  'Accept' => 'application/json'
}

response = client.post(request, headers)

これは機能しますが、私がやりたいのは、キープアライブを使用して、リクエストを行うたびに接続を確立するプロセス全体を実行する必要がないようにすることです。これに伴う遅延により、私が作成している監視アプリケーションの有用性が大幅に低下します。

しかし、私が見つけられないように見えるのは、以下を提供する Ruby ライブラリです。

  • HTTPS サポート
  • SSL クライアント証明書のサポート
  • 生き続ける

それを提供する必要があるrest-client のプルリクエストが苦しんでいます。httpartyにはpersistent_httpartyがありますが、SSL クライアント証明書をサポートしている場合、ドキュメントはありません

rest-client を fork して、ちょっと腐った pull-request をマージして、それを使うことができました。しかし、確かにここに何かが欠けています...探しているものを提供する既存のライブラリはありますか? または、そのライブラリでの SSL クライアント証明書の使用を説明する httparty のドキュメントはありますか?

どんな援助でも大歓迎です。

4

3 に答える 3

1

機械化を試しましたか?

によると、次のようなクライアント証明書を渡すことができます。

require 'rubygems'
require 'mechanize'

# create Mechanize instance
agent = Mechanize.new

# set the path of the certificate file
agent.cert = 'example.cer'

# set the path of the private key file
agent.key = 'example.key'

# get the login form & fill it out with the username/password
login_form = agent.get("http://example.com/login_page").form('Login')
login_form.Userid = 'TestUser'
login_form.Password = 'TestPassword'

# submit login form
agent.submit(login_form, login_form.buttons.first)

このトピックによると、mechanize に SSLV3 を強制的に使用させる必要がある場合があります。

page = Mechanize.new{|a| a.ssl_version, a.verify_mode = 'SSLv3', OpenSSL::SSL::VERIFY_NONE}.get "https://yourHTTPSurl"
于 2013-09-19T08:20:58.153 に答える