开发者

How can I Authenticate a http web request with a certificate?

I need to be able to send a http get request to a web service that requires clients to authenticate with 开发者_开发知识库a specific certificate. The .net code looks like this:

if (certificate != null) 
    request.ClientCertificates.Add(certificate); 
return request; 

I havent been able to figure out the equivalent in rails. Any suggestions?


If using basic net/https, then it's quite simple:

require 'net/https'
require 'uri'

uri = URI.parse(ARGV[0] || 'https://localhost/')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if uri.scheme == "https"  # enable SSL/TLS
http.key = pkey #Sets an OpenSSL::PKey::RSA or OpenSSL::PKey::DSA object.
http.cert= cert #Sets an OpenSSL::X509::Certificate object as client certificate

http.start {
  http.request_get(uri.path) {|res|
    print res.body
  }
}

If you have them combined, you'll have to massage them using some openssl utility methods.

For a custom http client you should read the docs for the ruby openssl library for the gory details.

But in a nutshell, something like this should work:

ctx = OpenSSL::SSL::SSLContext.new
ctx.key = private_key_file
ctx.cert = certificate_file

..and then supply the context to your connection.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜