How can I authenticate an http web request with a certificate?

I need to send an HTTP request to a web service requiring authentication clients with a specific certificate. The .net code is as follows:

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

I could not find the equivalent in rails. Any suggestions?

+3
source share
1 answer

If the core network / https is used, then this is pretty 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 combined them, you will have to massage them using some methods of the openssl utility.

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

But in short, something like this should work:

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

.. and then specify the context for your connection.

+2
source

Source: https://habr.com/ru/post/1773515/


All Articles