diff --git a/README.md b/README.md index c55e1e0..e8c883c 100644 --- a/README.md +++ b/README.md @@ -273,6 +273,15 @@ Pincers.for_chenso do |pincers| end ``` +Chenso also supports **client SSL certificate**, to use a client certificate use the `ssl_cert` and `ssl_key` options: + +```ruby +Pincers.for_chenso( + ssl_cert: OpenSSL::X509::Certificate.new(File.read('./client.cert.pem')), + ssl_key: OpenSSL::PKey::RSA.new(File.read('./client.key.pem')) +) +``` + #### Navigating frames Pincers operations can only target one frame at a time. By default, the top frame is selected when location is changed. To switch to a diferent frame use the `goto` method with the `frame:` option: diff --git a/lib/pincers/http/client.rb b/lib/pincers/http/client.rb index 3636ffc..e858ce7 100644 --- a/lib/pincers/http/client.rb +++ b/lib/pincers/http/client.rb @@ -17,6 +17,11 @@ def self.build_from_options(_options = {}) session.headers.merge! _options[:headers] if _options.key? :headers session.redirect_limit = _options[:redirect_limit] if _options.key? :redirect_limit + if _options.key? :ssl_cert + session.ssl_cert = _options[:ssl_cert] + session.ssl_key = _options[:ssl_key] + end + client = self.new session, _options[:document] client.freeze if _options[:freeze] client diff --git a/lib/pincers/http/session.rb b/lib/pincers/http/session.rb index ba448a0..e0bb362 100644 --- a/lib/pincers/http/session.rb +++ b/lib/pincers/http/session.rb @@ -11,7 +11,8 @@ class Session } attr_reader :cookie_jar, :headers - attr_accessor :proxy_addr, :proxy_port, :proxy_user, :proxy_password, :redirect_limit + attr_accessor :proxy_addr, :proxy_port, :proxy_user, :proxy_password, :redirect_limit, + :ssl_cert, :ssl_key def initialize(_other = nil) if _other @@ -20,6 +21,8 @@ def initialize(_other = nil) @proxy_addr = _other.proxy_addr @proxy_port = _other.proxy_port @redirect_limit = _other.redirect_limit + @ssl_cert = _other.ssl_cert + @ssl_key = _other.ssl_key else @headers = DEFAULT_HEADERS @cookie_jar = CookieJar.new @@ -102,6 +105,12 @@ def connect(_uri) ) conn.use_ssl = true if _uri.scheme == 'https' + + if ssl_cert + conn.cert = ssl_cert + conn.key = ssl_key + end + conn.verify_mode = OpenSSL::SSL::VERIFY_NONE conn end