Sha256: e856a7692b1e668ac243e8356ad756f3a8521e3f8ae1ee901ef12308ce071836

Contents?: true

Size: 1.44 KB

Versions: 7

Compression:

Stored size: 1.44 KB

Contents

require 'openssl'

module RESTinPeace
  module Faraday
    class SSLConfigCreator
      class MissingParam < Exception; end

      def initialize(config, verify = :peer)
        @config = config
        @verify = verify

        raise MissingParam, 'Specify :ca_cert in ssl options' unless @config[:ca_cert]
        raise MissingParam, 'Specify :client_key in ssl options' unless @config[:client_key]
        raise MissingParam, 'Specify :client_cert in ssl options' unless @config[:client_cert]
      end

      def faraday_options
        {
          client_cert: client_cert,
          client_key: client_key,
          ca_file: ca_cert_path,
          verify_mode: verify_mode,
        }
      end

      def client_cert
        OpenSSL::X509::Certificate.new(open_file(client_cert_path))
      end

      def client_cert_path
        path(@config[:client_cert])
      end

      def client_key
        OpenSSL::PKey::RSA.new open_file(client_key_path), @config[:client_key_passphrase]
      end

      def client_key_path
        path(@config[:client_key])
      end

      def ca_cert_path
        path(@config[:ca_cert])
      end

      def verify_mode
        case @verify
        when :peer
          OpenSSL::SSL::VERIFY_PEER
        else
          raise "Unknown verify variant '#{@verify}'"
        end
      end

      private

      def open_file(file)
        File.open(file)
      end

      def path(file)
        File.join(file)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rest-in-peace-4.2.0 lib/rest_in_peace/faraday/ssl_config_creator.rb
rest-in-peace-4.1.1 lib/rest_in_peace/faraday/ssl_config_creator.rb
rest-in-peace-4.1.0 lib/rest_in_peace/faraday/ssl_config_creator.rb
rest-in-peace-4.0.0 lib/rest_in_peace/faraday/ssl_config_creator.rb
rest-in-peace-3.0.0 lib/rest_in_peace/faraday/ssl_config_creator.rb
rest-in-peace-2.0.4 lib/rest_in_peace/faraday/ssl_config_creator.rb
rest-in-peace-2.0.3 lib/rest_in_peace/faraday/ssl_config_creator.rb