Class: PWKeep::Main

Inherits:
Object
  • Object
show all
Defined in:
lib/pwkeep/main.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMain

Returns a new instance of Main.



8
9
10
# File 'lib/pwkeep/main.rb', line 8

def initialize
  @opts = { :home => ENV['PWKEEP_HOME'] || '~/.pwkeep' } # required value
end

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



6
7
8
# File 'lib/pwkeep/main.rb', line 6

def opts
  @opts
end

Class Method Details

.runObject



50
51
52
# File 'lib/pwkeep/main.rb', line 50

def self.run
  Main.new.run
end

Instance Method Details

#load_configObject



54
55
56
57
58
59
# File 'lib/pwkeep/main.rb', line 54

def load_config
  config_file = Pathname.new(@opts[:home]).expand_path.join('config.yml')
  if config_file.exist? 
     PWKeep::Config.instance.load(config_file)
  end 
end

#runObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/pwkeep/main.rb', line 61

def run
  setup
  load_config
  @storage = PWKeep::Storage.new(:path => opts[:home])
  
  begin
    if opts[:initialize]
      @storage.create
  
      if @storage.valid? 
        say("<%= color('WARNING!', BOLD) %> a valid pwkeep storage was found!")
        say("If you continue, the existing storage becomes <%= color('UNUSABLE', BOLD) %>")
        unless agree("Continue (y/n)? ") 
          raise PWKeep::Exception, "Storage initialization aborted" 
        end
      end
  
      # create a keypair
      pw_a = ""
      pw_b = ""
      while(pw_a == "" or pw_a != pw_b)
         pw_a = ask("Enter your password:   ") { |q| q.echo = false }
         pw_b = ask("Confirm your password: ") { |q| q.echo = false }
         say("<%= color('Passwords did not match', RED %>") unless pw_a == pw_b
      end
      @storage.keypair_create(pw_b)
      # this concludes initialization
      say("<%= color('Password storage initialized', GREEN %>")
      return
    end
   
    raise "Storage not initialized (run with --initialize)" unless @storage.valid? 
  
    if opts[:view]
      pw = ask("Enter your password:") { |q| q.echo = false }
      @storage.keypair_load pw
   
      data = @storage.load_system opts[:system]
  
      say("Last edited: #{data[:stored_at]}\nSystem: #{data[:system]}\n\n")
      say(data[:data])
      return 
    end
  
    if opts[:create]
      data = "Username: \nPassword: #{KeePass::Password.generate('uullA{6}')}"
      
      result = PWKeep.run_editor(data, {})
  
      unless result[0]
        raise "Not modified"
      end
  
      pw = ask("Enter your password:") { |q| q.echo = false }
      @storage.keypair_load pw
      @storage.save_system opts[:system], result[1]
      say("<%= color('Changes stored', GREEN)%>")
      return
    end
  
    if opts[:edit]
      pw = ask("Enter your password:") { |q| q.echo = false }
      @storage.keypair_load pw
      data = @storage.load_system opts[:system]
      result = PWKeep.run_editor(data[:data], {})
      unless result[0]
        raise "Not modified"
      end
      @storage.save_system opts[:system], result[1]
      say("<%= color('Changes stored', GREEN)%>")
      return
    end
  
    if opts[:delete]
      pw = ask("Enter your password:") { |q| q.echo = false }
      @storage.keypair_load pw
      data = @storage.load_system opts[:system]
      # just to be sure
      unless agree("Are you <%=color('SURE',BOLD)%> you want to delete #{data[:system]}?")
        @storage.delete data[:system]
      end
      say("<%= color('System deleted', YELLOW)%>")
      return
    end
  
    if opts[:search]
      pw = ask("Enter your password:") { |q| q.echo = false }
      @storage.keypair_load pw
      say("All matching systems\n")
      @storage.list_all_systems.sort.each do |system|
        if system.match opts[:search]
          say("  - #{system}")
        end
      end
      return
    end
  
    if opts[:list]
      pw = ask("Enter your password:") { |q| q.echo = false }
      @storage.keypair_load pw
      say("All known systems\n")
      @storage.list_all_systems.sort.each do |system| 
        say("  - #{system}")
      end
      return
    end
  rescue PWKeep::Exception => e
   PWKeep::logger.error e.message.colorize(:red)
  rescue OpenSSL::PKey::RSAError => e
   PWKeep::logger.error "Cannot load private key".colorize(:red)
  end
end

#setupObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pwkeep/main.rb', line 12

def setup
  @opts = Trollop::options do
    version "0.0.1 (c) 2014 Aki Tuomi"
    banner <<-EOS
This program is a simple password storage utility. Distributed under MIT license. NO WARRANTY.
  
Usage:
  #{$0} [options]
  
Where [options] are
    
EOS
    opt :system, "System name", :type => :string, :short => '-n'
    opt :initialize, "Initialize storage at ~/.pwkeep (you can change this with PWKEEP_HOME or --home)", :short => '-i'
    opt :create, "Create new entry", :short => '-c'
    opt :view, "View entry", :short => '-v'
    opt :edit, "Edit entry", :short => '-e'
    opt :delete, "Delete entry", :short => '-d'
    opt :search, "Search for system or username", :type => :string, :short => '-s'
    opt :list, "List all known systems", :short => '-l'
    opt :help, "Show usage", :short => '-h'
    opt :home, "Home directory", :short => '-H', :type => :string, :default => ( ENV['PWKEEP_HOME'] || '~/.pwkeep' )
    opt :version, "Show version", :short => '-V'
  end
  
  # validate options
  Trollop::die :system, "must be given for create/show/edit/delete" if opts[:system].nil? and (opts[:edit] or opts[:view] or opts[:delete] or opts[:create])
  Trollop::die :create, "can only have one mode of operation" if opts[:create] and (opts[:edit] or opts[:view] or opts[:delete] or opts[:search] or opts[:initialize] or opts[:list])
  Trollop::die :edit, "can only have one mode of operation" if opts[:edit] and (opts[:create] or opts[:view] or opts[:delete] or opts[:search] or opts[:initialize] or opts[:list])
  Trollop::die :view, "can only have one mode of operation" if opts[:view] and (opts[:edit] or opts[:create] or opts[:delete] or opts[:search] or opts[:initialize] or opts[:list])
  Trollop::die :delete, "can only have one mode of operation" if opts[:delete] and (opts[:edit] or opts[:view] or opts[:create] or opts[:search] or opts[:initialize] or opts[:list])
  Trollop::die :search, "can only have one mode of operation" if opts[:search] and (opts[:edit] or opts[:view] or opts[:delete] or opts[:create] or opts[:initialize] or opts[:list])
  Trollop::die :initialize, "can only have one mode of operation" if opts[:initialize] and (opts[:edit] or opts[:view] or opts[:delete] or opts[:create] or opts[:search] or opts[:list])
  Trollop::die :list, "can only have one mode of operation" if opts[:list] and (opts[:edit] or opts[:view] or opts[:delete] or opts[:create] or opts[:search] or opts[:initialize])
  
  Trollop::die "You must choose one mode of operation" unless opts[:create] or opts[:edit] or opts[:view] or opts[:delete] or opts[:search] or opts[:initialize] or opts[:list]
end