Skip to content

Instantly share code, notes, and snippets.

@Patrick-Cao
Forked from chills42/faraday_cert_auth.rb
Created September 8, 2016 18:16
Show Gist options
  • Save Patrick-Cao/d07fb8b0d486a669164d05821132b849 to your computer and use it in GitHub Desktop.
Save Patrick-Cao/d07fb8b0d486a669164d05821132b849 to your computer and use it in GitHub Desktop.
Faraday client certificate authentication
# I'm starting with a .pfx, so the first step is to get it to a .pem caintaining the cert and key
# This can be done using the following command
#
# openssl pkcs12 -in certs/mycert.pfx -out certs/mycert.pem -nodes
#
require 'faraday'
require 'openssl'
# create an x509 certificate
def cert_object
OpenSSL::X509::Certificate.new File.read('certs/mycert.pem')
end
# create PKey
def key_object
OpenSSL::PKey.read File.read('certs/mycert.pem')
end
# build the connection object
connection = Faraday::Connection.new 'https://my.secure.site.dev', ssl: {
client_cert: get_cert,
client_key: get_key,
verify: false
}
connection.post do |req|
req.url '/secure-endpoint'
req.body = "secret data"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment