Sha256: 726be68452afb871be2d02d2fd50ea941d93aae916c13989069b6295bd402075

Contents?: true

Size: 1.91 KB

Versions: 14

Compression:

Stored size: 1.91 KB

Contents

require 'sym/app/private_key/decryptor'
require 'sym/app/password/cache'
require 'sym/errors'
module Sym
  module App
    module PrivateKey
      class Decryptor
        include Sym

        attr_accessor :encrypted_key, :input_handler, :password_cache

        def initialize(encrypted_key, input_handler, password_cache)
          self.encrypted_key  = encrypted_key
          self.input_handler  = input_handler
          self.password_cache = password_cache
          @cache_checked      = false
        end

        def key
          return nil if encrypted_key.nil?
          decrypted_key = nil
          if should_decrypt?
            begin
              retries                                   ||= 0
              p                                         = determine_key_password
              decrypted_key                             = decrypt(p)

              # if the password is valid, let's add it to the cache.
              password_cache[encrypted_key] = p

            rescue ::OpenSSL::Cipher::CipherError => e
              input_handler.puts 'Invalid password. Please try again.'

              if ((retries += 1) < 3)
                retry
              else
                raise(Sym::Errors::InvalidPasswordProvidedForThePrivateKey.new('Invalid password.'))
              end
            end
          else
            decrypted_key = encrypted_key
          end
          decrypted_key
        end

        private

        def should_decrypt?
          encrypted_key && (encrypted_key.length > 32)
        end

        def decrypt(password)
          decr_password(encrypted_key, password)
        end

        def determine_key_password
          check_cache || ask_user
        end

        def ask_user
          input_handler.ask
        end

        def check_cache
          return nil if @cache_checked
          @cache_checked = true
          password_cache[encrypted_key]
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
sym-2.8.5 lib/sym/app/private_key/decryptor.rb
sym-2.8.4 lib/sym/app/private_key/decryptor.rb
sym-2.8.2 lib/sym/app/private_key/decryptor.rb
sym-2.8.1 lib/sym/app/private_key/decryptor.rb
sym-2.8.0 lib/sym/app/private_key/decryptor.rb
sym-2.7.0 lib/sym/app/private_key/decryptor.rb
sym-2.6.3 lib/sym/app/private_key/decryptor.rb
sym-2.6.2 lib/sym/app/private_key/decryptor.rb
sym-2.6.1 lib/sym/app/private_key/decryptor.rb
sym-2.6.0 lib/sym/app/private_key/decryptor.rb
sym-2.5.3 lib/sym/app/private_key/decryptor.rb
sym-2.5.1 lib/sym/app/private_key/decryptor.rb
sym-2.5.0 lib/sym/app/private_key/decryptor.rb
sym-2.4.3 lib/sym/app/private_key/decryptor.rb