class OnePass::Application

OnePass Application

Constants

CONFIG_PATH

Public Class Methods

forget() click to toggle source
# File lib/OnePass/application.rb, line 99
def self.forget
  path = File.expand_path CONFIG_PATH
  File.delete path if File.exist? path
end
new(vault_path = nil) click to toggle source
# File lib/OnePass/application.rb, line 8
def initialize(vault_path = nil)
  @vault_path = get_vault vault_path
  @vault = OpVault.new @vault_path

  check_for_dependencies
  prompter = OnePass::Password.new(vault_path: @vault_path)
  password_loop prompter
ensure
  prompter && prompter.done
end
save(vault_path = nil) click to toggle source
# File lib/OnePass/application.rb, line 91
def self.save(vault_path = nil)
  new vault_path # if succeeds, path & pw is good
  path = File.expand_path CONFIG_PATH
  File.open path, File::CREAT | File::TRUNC | File::RDWR do |file|
    file.write "path=#{File.expand_path vault_path}\n"
  end
end

Public Instance Methods

check_for_dependencies() click to toggle source
# File lib/OnePass/application.rb, line 19
def check_for_dependencies
  unless installed? 'pinentry --version'
    puts 'Please install the `pinentry` program.'
    puts '  on macOS, we recommend using Homebrew: `brew install pinentry`.'
    exit 127
  end
end
get_vault(vault_path = nil) click to toggle source
# File lib/OnePass/application.rb, line 43
def get_vault(vault_path = nil)
  return vault_path if vault_path
  path = File.expand_path CONFIG_PATH
  raise 'Config file missing, please log in' unless File.exist? path

  config = File.read path
  raise 'Config file error' unless config.start_with? 'path='
  config[5..-1].strip
end
installed?(program) click to toggle source
# File lib/OnePass/application.rb, line 85
def installed?(program)
  `#{program}`
  result = $CHILD_STATUS
  result.exitstatus != 127
end
password_loop(prompter) click to toggle source
# File lib/OnePass/application.rb, line 27
def password_loop(prompter)
  error_message = nil
  loop do
    password = prompter.prompt error_message
    exit if password.nil? # cancelled
    begin
      @vault.unlock password
      @vault.load_items
      break
    rescue => error
      error_message = error.message
      next
    end
  end
end
show(query, reply_type) click to toggle source
# File lib/OnePass/application.rb, line 53
def show(query, reply_type)
  item = @vault.find(/#{query}/i).first
  data = @vault.item_overview item
  unless %i( uuid url title ).include? reply_type
    data.merge!(@vault.item_detail(item))
  end

  case reply_type
  when %i( uuid url title )
    data[reply_type.to_s]
  when :username
    data['fields'].find({}) { |field| field['designation'] == 'username' }['value']
  when :password
    data['fields'].find({}) { |field| field['designation'] == 'password' }['value']
  else
    data
  end
end