From 0f9ae1c79457c1aa411e5242b506862af7f4668a Mon Sep 17 00:00:00 2001 From: kirtithorat Date: Sat, 14 Mar 2015 11:39:51 -0500 Subject: [PATCH] Fixed multiple issues with configuration of Legacy API on 0.x branch --- README.md | 28 +++++++++++++++++++++------- lib/bigcommerce/connection.rb | 19 ++++++++++++++----- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index c8d42c8..53aad54 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ api = Bigcommerce::Api.new({ }) ``` -You can also enable SSL for the Legacy API. +By default SSL is always enabled for the Legacy API. Remember that the fields `:ssl_client_cert`, `:ssl_client`, `:ssl_ca_file` and `:verify_peer` are all required when specifying your own self-signed SSL certificate. ```rb require 'bigcommerce' @@ -87,14 +87,28 @@ api = Bigcommerce::Api.new({ :store_url => "https://store.mybigcommerce.com", :username => "username", :api_key => "api_key", - :ssl_client_cert => OpenSSL::X509::Certificate.new(File.read("cert.pem")), - :ssl_client_key => OpenSSL::PKey::RSA.new(File.read("key.pem"), "passphrase, if any"), - :ssl_ca_file => "ca_certificate.pem", - :verify_ssl => OpenSSL::SSL::VERIFY_PEER + :ssl_client_cert => "/path/to/cert.pem", + :ssl_client => { path: "/path/to/key.pem", passphrase: "passphrase, if any" }, + :ssl_ca_file => "/path/to/ca_certificate.pem", + :verify_peer => OpenSSL::SSL::VERIFY_PEER +}) +``` + +Disabling the SSL verification is only advisable in development environment and it can be done by explicitly passing `OpenSSL::SSL::VERIFY_NONE` value in `:verify_peer` field as below: + +```rb +require 'bigcommerce' + +api = Bigcommerce::Api.new({ + :store_url => "https://store.mybigcommerce.com", + :username => "username", + :api_key => "api_key", + :ssl_client_cert => "/path/to/cert.pem", + :ssl_client => { path: "/path/to/key.pem", passphrase: "passphrase, if any" }, + :ssl_ca_file => "/path/to/ca_certificate.pem", + :verify_peer => OpenSSL::SSL::VERIFY_NONE }) ``` -Remember that the fields `:ssl_client_cert`, `:ssl_client_key`, `:ssl_ca_file` -and `:verify_ssl` are all required when enabling SSL certificates. ## Connecting to the store diff --git a/lib/bigcommerce/connection.rb b/lib/bigcommerce/connection.rb index 6096a06..89e695b 100644 --- a/lib/bigcommerce/connection.rb +++ b/lib/bigcommerce/connection.rb @@ -39,19 +39,27 @@ def verify_peer=(verify) end def ssl_ca_file=(path) - @configuration.ssl_ca_file = path + @configuration[:ssl_ca_file] = path + end + + def ssl_client=(options) + if options[:passphrase].nil? + send(:ssl_client_key=, options[:path]) + else + send(:ssl_client_key=, options[:path], options[:passphrase]) + end end def ssl_client_key=(path,passphrase=nil) if passphrase.nil? - @configuration.ssl_client_key = OpenSSL::PKey::RSA.new(File.read(path)) + @configuration[:ssl_client_key] = OpenSSL::PKey::RSA.new(File.read(path)) else - @configuration.ssl_client_key = OpenSSL::PKey::RSA.new(File.read(path), passphrase) + @configuration[:ssl_client_key] = OpenSSL::PKey::RSA.new(File.read(path), passphrase) end end def ssl_client_cert=(path) - @configuration.client_cert = OpenSSL::X509::Certificate.new(File.read(path)) + @configuration[:ssl_client_cert] = OpenSSL::X509::Certificate.new(File.read(path)) end def get(path, options = {}, headers = {}) @@ -125,6 +133,7 @@ def request_legacy(method, path, options, headers={}) resource_options = { :user => @configuration[:username], :password => @configuration[:api_key], + :verify_ssl => @configuration[:verify_ssl] || OpenSSL::SSL::VERIFY_PEER, :headers => headers } @@ -133,7 +142,7 @@ def request_legacy(method, path, options, headers={}) if @configuration[:ssl_client_key] && @configuration[:ssl_client_cert] && @configuration[:ssl_ca_file] rest_client = RestClient::Resource.new( "#{@configuration[:store_url]}/api/v2#{path}.json", - :username => @configuration[:username], + :user => @configuration[:username], :password => @configuration[:api_key], :ssl_client_cert => @configuration[:ssl_client_cert], :ssl_client_key => @configuration[:ssl_client_key],