uberpass 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/lib/uberpass.rb +183 -96
  2. data/lib/uberpass/version.rb +1 -1
  3. metadata +6 -6
@@ -1,6 +1,8 @@
1
1
  require 'uberpass/version'
2
2
  require 'openssl'
3
3
  require 'yaml'
4
+ require 'ostruct'
5
+ require 'securerandom'
4
6
 
5
7
  module Uberpass
6
8
  class Decrypt
@@ -68,13 +70,16 @@ module Uberpass
68
70
  File.expand_path("~/.uberpass/#{name_spaced_file("iv")}")
69
71
  end
70
72
 
71
- def show_password(key)
72
- passwords = decrypted_passwords
73
- passwords[key]["password"] unless passwords[key].nil?
74
- end
75
-
76
- def list_keys
77
- decrypted_passwords.keys
73
+ def write(encryptor)
74
+ File.open(passwords_file, "w") { |file|
75
+ file.write(encryptor.encrypted_data)
76
+ }
77
+ File.open(key_file, "w") { |file|
78
+ file.write(encryptor.encrypted_key)
79
+ }
80
+ File.open(iv_file, "w") { |file|
81
+ file.write(encryptor.encrypted_iv)
82
+ }
78
83
  end
79
84
 
80
85
  def decrypted_passwords
@@ -92,136 +97,218 @@ module Uberpass
92
97
  end
93
98
  end
94
99
 
95
- def new_password(key)
100
+ def show(key)
101
+ Hash[*[key, decrypted_passwords[key]]]
102
+ end
103
+
104
+ def all
105
+ decrypted_passwords.map do |entry|
106
+ Hash[*entry]
107
+ end
108
+ end
109
+
110
+ def generate_short(key)
111
+ encrypt key, SecureRandom.urlsafe_base64(8)
112
+ end
113
+
114
+ def generate(key)
115
+ encrypt key, SecureRandom.urlsafe_base64(24)
116
+ end
117
+
118
+ def encrypt(key, password)
96
119
  passwords = decrypted_passwords
97
- passwords[key] = {
98
- "password" => Array.new(32).map{ rand(2) == 1 ? (65 + rand(58)).chr : rand(10) }.join,
120
+ entry = passwords[key] = {
121
+ "password" => password,
99
122
  "created_at" => Time.now
100
123
  }
101
124
  encryptor = Encrypt.new(File.read(public_key_file), passwords.to_yaml)
102
125
  write(encryptor)
103
- passwords[key]["password"]
126
+ Hash[*[key, entry]]
104
127
  end
105
128
 
106
- def store(key, password)
129
+ def rename(old, new)
107
130
  passwords = decrypted_passwords
108
- passwords[key] = {
109
- "password" => password,
110
- "created_at" => Time.now
111
- }
131
+ entry = passwords.delete old
132
+ passwords[new] = entry
112
133
  encryptor = Encrypt.new(File.read(public_key_file), passwords.to_yaml)
113
134
  write(encryptor)
114
- passwords[key]["password"]
135
+ Hash[*[new, entry]]
115
136
  end
116
137
 
117
- def destroy_password(key)
138
+ def destroy(key)
118
139
  passwords = decrypted_passwords
119
140
  entry = passwords.delete key
120
141
  encryptor = Encrypt.new(File.read(public_key_file), passwords.to_yaml)
121
142
  write(encryptor)
122
- entry
143
+ Hash[*[key, entry]]
123
144
  end
124
145
 
125
- def write(encryptor)
126
- File.open(passwords_file, "w") { |file|
127
- file.write(encryptor.encrypted_data)
128
- }
129
- File.open(key_file, "w") { |file|
130
- file.write(encryptor.encrypted_key)
131
- }
132
- File.open(iv_file, "w") { |file|
133
- file.write(encryptor.encrypted_iv)
134
- }
135
- end
136
146
  end
137
147
  end
138
148
 
139
149
  class CLI
150
+ class InvalidActionError < StandardError; end
151
+ class MissingArgumentError < StandardError; end
152
+
153
+ module Actions
154
+ attr_accessor :actions
155
+
156
+ def register_action(*args, &block)
157
+ options = args.size == 1 ? {} : (args.last || {})
158
+ options[:short] = options[:short].nil? ? args.first[0] : options.delete(:short).to_s
159
+ options[:steps] = [] if options[:steps].nil?
160
+ options[:filter] = [] if options[:filter].nil?
161
+ options[:confirm] = false if options[:confirm].nil?
162
+ (@actions ||= []) << OpenStruct.new({ :name => args.first, :proc => block }.merge(options))
163
+ end
164
+ end
165
+
166
+ module Formater
167
+ def bold(obj)
168
+ "\033[0;1m#{obj}\033[0m"
169
+ end
170
+
171
+ def red(obj)
172
+ "\033[01;31m#{obj}\033[0m"
173
+ end
174
+
175
+ def green(obj)
176
+ "\033[0;32m#{obj}\033[0m"
177
+ end
178
+
179
+ def gray(obj)
180
+ "\033[1;30m#{obj}\033[0m"
181
+ end
182
+ end
183
+
184
+ extend Actions
185
+ include Formater
186
+
187
+ register_action :generate, :steps => ["key"] do |key|
188
+ FileHandler.generate key
189
+ end
190
+
191
+ register_action :generate_short, :steps => ["key"], :short => :gs do |key|
192
+ FileHandler.generate_short key
193
+ end
194
+
195
+ register_action :destroy, :steps => ["key"], :short => :rm, :confirm => true do |key|
196
+ FileHandler.destroy key
197
+ end
198
+
199
+ register_action :show, :steps => ["key"] do |key|
200
+ FileHandler.show key
201
+ end
202
+
203
+ register_action :encrypt, :steps => ["key", "password"] do |key, password|
204
+ FileHandler.encrypt key, password
205
+ end
206
+
207
+ register_action :rename, :steps => ["key", "new name"] do |old, new|
208
+ FileHandler.rename old, new
209
+ end
210
+
211
+ register_action :list, :filter => ["password"] do
212
+ FileHandler.all
213
+ end
214
+
215
+ register_action :dump do
216
+ FileHandler.all
217
+ end
218
+
219
+ register_action :help do
220
+ CLI.help
221
+ []
222
+ end
223
+
224
+ register_action :exit, :short => :ex do
225
+ exit
226
+ end
227
+
140
228
  def initialize(namespace)
141
229
  FileHandler.configure do |handler|
142
230
  handler.namespace = namespace
143
231
  end
144
- print "\nactions:\n"
145
- print " generate\n"
146
- print " destroy\n"
147
- print " reveal\n"
148
- print " list\n"
149
- print " encrypt\n"
150
- print " exit\n"
151
- actions
232
+ line
233
+ do_action
152
234
  end
153
235
 
154
- def actions
155
- print "\n> "
156
- action, argument = $stdin.gets.chomp.split(' ')
157
- do_action_with_rescue action, argument
236
+ def line(message = nil, format = :bold)
237
+ parts = []
238
+ parts << "\nuberpass"
239
+ parts << "#{VERSION}:"
240
+ parts << send(format, message) unless message.nil?
241
+ parts << "> "
242
+ print parts.join(' ')
158
243
  end
159
244
 
160
- def do_action_with_rescue(action, argument)
161
- begin
162
- do_action action, argument
163
- rescue OpenSSL::PKey::RSAError
164
- print "\nInvalid PEM pass phrase. Please try again.\n\n"
165
- do_action_with_rescue action, argument
245
+ def self.help
246
+ print "\nactions:\n"
247
+ actions.each do |action|
248
+ print " #{action.name}\n"
166
249
  end
167
250
  end
168
251
 
169
- def do_two_step
252
+ def confirm_action
253
+ line "are you sure you? [yn]", :green
254
+ $stdin.gets.chomp == "y"
255
+ end
170
256
 
257
+ def do_action
258
+ input = $stdin.gets.chomp
259
+ do_action_with_rescue input
171
260
  end
172
261
 
173
- def do_action(action, argument)
174
- case action
175
- when "generate", "g"
176
- if argument.to_s.strip == ""
177
- print "choose a name ie. generate twitter"
178
- else
179
- password = FileHandler.new_password(argument)
180
- print "password for #{argument}: #{password}\n"
181
- end
182
- when "destroy", "d"
183
- if argument.to_s.strip == ""
184
- print "choose a name ie. destroy twitter"
185
- else
186
- print "\nare you sure you? [yn] "
187
- if $stdin.gets.chomp == "y"
188
- FileHandler.destroy_password(argument)
189
- print "password removed\n"
190
- end
191
- end
192
- when "reveal", "r"
193
- if argument.to_s.strip == ""
194
- print "choose a name ie. reveal twitter"
195
- else
196
- password = FileHandler.show_password(argument)
197
- print "password for #{argument}: #{password}\n"
198
- end
199
- when "encrypt", "e"
200
- if argument.to_s.strip == ""
201
- print "choose a name ie. encrypt router"
262
+ def do_action_with_rescue(input)
263
+ args = input.split(/ /)
264
+ begin
265
+ action = fetch_action args.slice!(0)
266
+ action.steps[args.size, action.steps.size].each do |instruction|
267
+ line instruction, :green
268
+ arg = $stdin.gets.chomp
269
+ raise MissingArgumentError, instruction if arg == ""
270
+ args << arg
271
+ end
272
+ if action.confirm
273
+ pp(action.proc.call(*args), action.filter) if confirm_action
202
274
  else
203
- print "\nenter item"
204
- print "\n> "
205
- value = $stdin.gets.chomp
206
- if value == ""
207
- print "nothing encrypted"
208
- else
209
- password = FileHandler.store(argument, value)
210
- print "password encrypted"
211
- end
275
+ pp(action.proc.call(*args), action.filter)
212
276
  end
213
- when "list", "l"
214
- keys = FileHandler.list_keys
215
277
  print "\n"
216
- keys.each do |key|
217
- print " - #{key}\n"
218
- end
219
- when "exit"
220
- exit
278
+ line
279
+ do_action
280
+ rescue MissingArgumentError => e
281
+ line e, :red
282
+ do_action
283
+ rescue InvalidActionError => e
284
+ line e, :red
285
+ do_action
286
+ rescue OpenSSL::PKey::RSAError => e
287
+ line e, :red
288
+ do_action
289
+ end
290
+ end
291
+
292
+ def fetch_action(key)
293
+ self.class.actions.each do |action|
294
+ return action if action.name == key.to_sym || action.short == key
295
+ end
296
+ raise InvalidActionError, key
297
+ end
298
+
299
+ def pp(entry, filter)
300
+ if entry.is_a? Array
301
+ entry.each do |entry|
302
+ pp entry, filter
303
+ end
221
304
  else
222
- print "invalid option"
305
+ key = entry.keys.first
306
+ filter.each do |f|
307
+ entry[key].delete f
308
+ end
309
+ print "\n#{gray entry[key]["created_at"].strftime("%d/%m/%Y")} #{bold key} "
310
+ print "\n#{entry[key]["password"]}" unless entry[key]["password"].nil?
223
311
  end
224
- actions
225
312
  end
226
313
  end
227
314
  end
@@ -1,3 +1,3 @@
1
1
  module Uberpass
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uberpass
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-12 00:00:00.000000000 Z
12
+ date: 2012-02-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70237106182980 !ruby/object:Gem::Requirement
16
+ requirement: &70366937140340 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70237106182980
24
+ version_requirements: *70366937140340
25
25
  description: uses open ssl and a cli to generate and retrieve passwords
26
26
  email:
27
27
  - rufuspost@gmail.com
@@ -52,7 +52,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
52
52
  version: '0'
53
53
  segments:
54
54
  - 0
55
- hash: 3087924462459114461
55
+ hash: -4545846251252195404
56
56
  required_rubygems_version: !ruby/object:Gem::Requirement
57
57
  none: false
58
58
  requirements:
@@ -61,7 +61,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
61
61
  version: '0'
62
62
  segments:
63
63
  - 0
64
- hash: 3087924462459114461
64
+ hash: -4545846251252195404
65
65
  requirements: []
66
66
  rubyforge_project: uberpass
67
67
  rubygems_version: 1.8.11