Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support setting SSL client cert as a an array, to configure extra_chain_cert #42

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/faraday/adapter/net_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ def configure_ssl(http, ssl)
http.verify_mode = ssl_verify_mode(ssl)
http.cert_store = ssl_cert_store(ssl)

http.cert = ssl[:client_cert] if ssl[:client_cert]
cert, *extra_chain_cert = ssl[:client_cert]
http.cert = cert if cert
http.extra_chain_cert = extra_chain_cert if extra_chain_cert.any?

http.key = ssl[:client_key] if ssl[:client_key]
http.ca_file = ssl[:ca_file] if ssl[:ca_file]
http.ca_path = ssl[:ca_path] if ssl[:ca_path]
Expand Down
44 changes: 44 additions & 0 deletions spec/faraday/adapter/net_http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,48 @@
it { expect(response.body.encoding).to eq(::Encoding::UTF_8) }
end
end

context 'client certificate' do
let(:adapter) { described_class.new }
let(:url) { URI('https://example.com') }
let(:http) { adapter.send(:connection, url: url, request: {}, ssl: ssl_options) }

before do
stub_request(:any, 'https://example.com')
end

context 'when client_cert is provided as an array' do
let(:cert_array) { [OpenSSL::X509::Certificate.new, OpenSSL::X509::Certificate.new] }
let(:ssl_options) do
Faraday::SSLOptions.new.tap do |ssl_options|
ssl_options.client_cert = cert_array
end
end

it 'sets the first cert as cert and the rest as extra_chain_cert' do
adapter.send(:configure_ssl, http, ssl_options)
end

it { expect(http.cert).to eq(cert_array.first) }

it { expect(http.extra_chain_cert).to eq(cert_array[1..]) }
end

context 'when client_cert is provided as a single cert' do
let(:cert) { OpenSSL::X509::Certificate.new }
let(:ssl_options) do
Faraday::SSLOptions.new.tap do |ssl_options|
ssl_options.client_cert = cert
end
end

it 'sets the cert as cert' do
adapter.send(:configure_ssl, http, ssl_options)
end

it { expect(http.cert).to eq(cert) }

it { expect(http.extra_chain_cert).to be_nil }
end
end
end