Class: OnePass::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/OnePass/application.rb

Overview

OnePass Application

Constant Summary collapse

CONFIG_PATH =
'~/.one_pass'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vault_path = nil) ⇒ Application

Returns a new instance of Application.



8
9
10
11
12
13
14
15
16
17
# 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

Class Method Details

.forgetObject



99
100
101
102
# File 'lib/OnePass/application.rb', line 99

def self.forget
  path = File.expand_path CONFIG_PATH
  File.delete path if File.exist? path
end

.save(vault_path = nil) ⇒ Object



91
92
93
94
95
96
97
# 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

Instance Method Details

#check_for_dependenciesObject



19
20
21
22
23
24
25
# 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) ⇒ Object



43
44
45
46
47
48
49
50
51
# 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) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
88
89
# File 'lib/OnePass/application.rb', line 85

def installed?(program)
  `#{program}`
  result = $CHILD_STATUS
  result.exitstatus != 127
end

#password_loop(prompter) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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

#search(query) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/OnePass/application.rb', line 72

def search(query)
  @vault.find(/#{query}/i).collect do |item|
    data = (@vault.item_overview item).merge(@vault.item_detail(item))
    {
      uuid: data['uuid'],
      title: data['title'],
      username: data['fields'].find({}) do |field|
        field['designation'] == 'username'
      end['value']
    }
  end
end

#show(query, reply_type) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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