Sha256: b4d2512bc7406de76a5ee83d9133bb0db9ba7bd5ec846a40d3baedda6765bbf0

Contents?: true

Size: 1.95 KB

Versions: 10

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

require 'openssl'

module NubankSdk
  #
  # Controller of certifications
  #
  class Certificate
    FILES_PATH = './certificates/'

    #
    # Controller of certifications
    #
    # @param [String] cpf
    def initialize(cpf)
      @cpf = cpf
    end

    #
    # Create a certificate file
    #
    # @param [OpenSSL::PKey::RSA] key
    # @param [String] certificate
    #
    # @return [File]
    def process_decoded(key, certificate)
      encoded = encode certificate

      p12 = create_pkcs12_from(key, encoded)
      save p12
    end

    #
    # Load the certificate file
    #
    # @return [OpenSSL::PKCS12]
    def encoded
      @encoded ||= OpenSSL::PKCS12.new(file.read, 'password')
    end

    private

    # @!visibility private
    # Open the encrypted certificate file
    #
    # @return [OpenSSL::PKCS12]
    def file
      File.open("#{FILES_PATH}#{@cpf}.p12", 'rb')
    end

    # @!visibility private
    # Create a file with the encrypted certificate
    #
    # @param [OpenSSL::PKCS12] p12
    #
    # @return [File]
    def save(p12)
      create_folder

      File.open("#{FILES_PATH}#{@cpf}.p12", 'wb') do |file|
        file.write p12.to_der
      end
    end

    # @!visibility private
    # Create certificates folder
    #
    # @return [File]
    def create_folder
      Dir.mkdir(FILES_PATH) unless Dir.exist?(FILES_PATH)
    end

    # @!visibility private
    # crypt key and certificate to pkcs12
    #
    # @param [OpenSSL::PKey::RSA] key
    # @param [OpenSSL::X509::Certificate] certificate
    #
    # @return [OpenSSL::PKCS12]
    def create_pkcs12_from(key, certificate)
      OpenSSL::PKCS12.create('password', 'key', key, certificate)
    end

    # @!visibility private
    # Enconde certificate string to certificate object
    #
    # @param [String] certificate
    #
    # @return [OpenSSL::X509::Certificate]
    def encode(certificate)
      OpenSSL::X509::Certificate.new certificate
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
nubank_sdk-0.7.9 lib/nubank_sdk/certificate.rb
nubank_sdk-0.7.8 lib/nubank_sdk/certificate.rb
nubank_sdk-0.7.7 lib/nubank_sdk/certificate.rb
nubank_sdk-0.7.6 lib/nubank_sdk/certificate.rb
nubank_sdk-0.7.5 lib/nubank_sdk/certificate.rb
nubank_sdk-0.7.4 lib/nubank_sdk/certificate.rb
nubank_sdk-0.7.3 lib/nubank_sdk/certificate.rb
nubank_sdk-0.7.2 lib/nubank_sdk/certificate.rb
nubank_sdk-0.7.1 lib/nubank_sdk/certificate.rb
nubank_sdk-0.7.0 lib/nubank_sdk/certificate.rb