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

Fix ssl_client_cert and ssl_client_key #33

Merged
merged 3 commits into from
Jul 6, 2023
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
6 changes: 3 additions & 3 deletions lib/faraday/adapter/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,16 @@ def ssl_context(ssl)

def ssl_client_cert(cert)
return nil if cert.nil?
return OpenSSL::X509::Certificate.new(File.read(cert)) if cert.is_a(String)
return OpenSSL::X509::Certificate.new(File.read(cert)) if cert.is_a?(String)
return cert if cert.is_a?(OpenSSL::X509::Certificate)

raise Faraday::Error, "invalid ssl.client_cert: #{cert.inspect}"
end

def ssl_client_key(cert)
return nil if cert.nil?
return OpenSSL::PKey::RSA.new(File.read(cert)) if cert.is_a(String)
return cert if cert.is_a?(OpenSSL::PKey::RSA, OpenSSL::PKey::DSA)
return OpenSSL::PKey::RSA.new(File.read(cert)) if cert.is_a?(String)
return cert if cert.is_a?(OpenSSL::PKey::RSA) || cert.is_a?(OpenSSL::PKey::DSA)

raise Faraday::Error, "invalid ssl.client_key: #{cert.inspect}"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/faraday/http/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Faraday
module Http
VERSION = '2.0.0'
VERSION = '2.0.1'
end
end
31 changes: 31 additions & 0 deletions spec/faraday/http/adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,35 @@
end.to raise_error(Faraday::SSLError, 'foo')
end
end

context 'when client certificate and private key are provided' do
let(:conn) do
conn_options[:ssl] ||= {}
conn_options[:ssl][:client_cert] ||= OpenSSL::X509::Certificate.new
conn_options[:ssl][:private_key] ||= OpenSSL::PKey::RSA.new

Faraday.new(remote, conn_options) do |conn|
conn.request :url_encoded
conn.response :raise_error
conn.adapter described_class, *adapter_options
end
end

subject { conn.get('/', nil, { user_agent: 'Agent Faraday' }) }

it 'makes a request successfully' do
stub_request(:get, 'https://example.com/')
.with(
headers: {
'Connection' => 'close',
'Host' => 'example.com',
'User-Agent' => 'Agent Faraday',
'X-Faraday-Adapter' => 'HTTP'
}
)
.to_return(status: 200, body: '', headers: {})

expect(subject).to be_success
end
end
end