pwl 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +10 -12
- data/Gemfile.lock +19 -14
- data/README.md +1 -1
- data/VERSION +1 -1
- data/bin/pwl +51 -43
- data/lib/pwl.rb +6 -2
- data/lib/pwl/{store.rb → locker.rb} +19 -21
- data/lib/pwl/presenter/html.rb +19 -0
- data/lib/pwl/presenter/json.rb +22 -0
- data/lib/pwl/presenter/yaml.rb +23 -0
- data/pwl.gemspec +35 -23
- data/templates/export.html.erb +2 -2
- data/test/acceptance/test_add.rb +19 -0
- data/test/acceptance/test_delete.rb +1 -1
- data/test/acceptance/test_export.rb +4 -4
- data/test/acceptance/test_export_json.rb +59 -0
- data/test/acceptance/test_export_yaml.rb +59 -0
- data/test/acceptance/test_get.rb +1 -1
- data/test/acceptance/test_init.rb +6 -6
- data/test/acceptance/test_list.rb +3 -3
- data/test/acceptance/test_passwd.rb +2 -2
- data/test/fixtures/test_all.json +19 -0
- data/test/fixtures/test_all.yaml +11 -0
- data/test/fixtures/test_empty.json +7 -0
- data/test/fixtures/test_empty.yaml +5 -0
- data/test/helper.rb +8 -8
- data/test/unit/test_store_construction.rb +26 -26
- data/test/unit/test_store_crud.rb +35 -35
- data/test/unit/test_store_metadata.rb +16 -16
- data/test/unit/test_store_password_policy.rb +6 -6
- data/test/unit/test_store_security.rb +13 -13
- metadata +82 -27
- data/test/acceptance/test_put.rb +0 -19
data/Gemfile
CHANGED
@@ -1,22 +1,20 @@
|
|
1
1
|
source "http://rubygems.org"
|
2
|
-
# Add dependencies required to use your gem here.
|
3
|
-
# Example:
|
4
|
-
# gem "activesupport", ">= 2.3.5"
|
5
2
|
|
6
|
-
gem 'encryptor'
|
7
|
-
gem 'commander'
|
8
|
-
gem 'activesupport'
|
3
|
+
gem 'encryptor', "~> 1.1"
|
4
|
+
gem 'commander', "~> 4.1"
|
5
|
+
gem 'activesupport', "~> 3.2"
|
6
|
+
gem 'jbuilder', '~> 0.4'
|
9
7
|
|
10
8
|
group :test do
|
11
|
-
gem "rake"
|
12
|
-
gem 'simplecov', :require => false
|
13
|
-
gem 'nokogiri-diff'
|
9
|
+
gem "rake", '~> 0.9'
|
10
|
+
gem 'simplecov', '~> 0.6', :require => false
|
11
|
+
gem 'nokogiri-diff', '~> 0.1'
|
14
12
|
end
|
15
13
|
|
16
14
|
# Add dependencies to develop your gem here.
|
17
15
|
# Include everything needed to run rake, tests, features, etc.
|
18
16
|
group :development do
|
19
|
-
gem "rdoc", "~> 3.
|
20
|
-
gem "bundler", "~> 1.
|
21
|
-
gem "jeweler", "~> 1.8
|
17
|
+
gem "rdoc", "~> 3.1"
|
18
|
+
gem "bundler", "~> 1.1"
|
19
|
+
gem "jeweler", "~> 1.8"
|
22
20
|
end
|
data/Gemfile.lock
CHANGED
@@ -1,22 +1,26 @@
|
|
1
1
|
GEM
|
2
2
|
remote: http://rubygems.org/
|
3
3
|
specs:
|
4
|
-
activesupport (3.2.
|
4
|
+
activesupport (3.2.3)
|
5
5
|
i18n (~> 0.6)
|
6
6
|
multi_json (~> 1.0)
|
7
|
+
blankslate (2.1.2.4)
|
7
8
|
commander (4.1.2)
|
8
9
|
highline (~> 1.6.11)
|
9
10
|
encryptor (1.1.3)
|
10
11
|
git (1.2.5)
|
11
12
|
highline (1.6.11)
|
12
13
|
i18n (0.6.0)
|
14
|
+
jbuilder (0.4.0)
|
15
|
+
activesupport (>= 3.0.0)
|
16
|
+
blankslate (>= 2.1.2.4)
|
13
17
|
jeweler (1.8.3)
|
14
18
|
bundler (~> 1.0)
|
15
19
|
git (>= 1.2.5)
|
16
20
|
rake
|
17
21
|
rdoc
|
18
|
-
json (1.
|
19
|
-
multi_json (1.
|
22
|
+
json (1.7.0)
|
23
|
+
multi_json (1.3.4)
|
20
24
|
nokogiri (1.4.7)
|
21
25
|
nokogiri-diff (0.1.0)
|
22
26
|
nokogiri (~> 1.4.1)
|
@@ -24,8 +28,8 @@ GEM
|
|
24
28
|
rake (0.9.2.2)
|
25
29
|
rdoc (3.12)
|
26
30
|
json (~> 1.4)
|
27
|
-
simplecov (0.6.
|
28
|
-
multi_json (~> 1.
|
31
|
+
simplecov (0.6.2)
|
32
|
+
multi_json (~> 1.3)
|
29
33
|
simplecov-html (~> 0.5.3)
|
30
34
|
simplecov-html (0.5.3)
|
31
35
|
tdiff (0.3.2)
|
@@ -34,12 +38,13 @@ PLATFORMS
|
|
34
38
|
ruby
|
35
39
|
|
36
40
|
DEPENDENCIES
|
37
|
-
activesupport
|
38
|
-
bundler (~> 1.
|
39
|
-
commander
|
40
|
-
encryptor
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
41
|
+
activesupport (~> 3.2)
|
42
|
+
bundler (~> 1.1)
|
43
|
+
commander (~> 4.1)
|
44
|
+
encryptor (~> 1.1)
|
45
|
+
jbuilder (~> 0.4)
|
46
|
+
jeweler (~> 1.8)
|
47
|
+
nokogiri-diff (~> 0.1)
|
48
|
+
rake (~> 0.9)
|
49
|
+
rdoc (~> 3.1)
|
50
|
+
simplecov (~> 0.6)
|
data/README.md
CHANGED
@@ -19,7 +19,7 @@ Before it can store passwords, pwl needs to initialize the password database. Th
|
|
19
19
|
|
20
20
|
Storing a password requires a name under which the password can be retrieved later on:
|
21
21
|
|
22
|
-
pwl
|
22
|
+
pwl add "Mail Account" s3cret
|
23
23
|
|
24
24
|
This command will store the password "s3cret" under the name "Mail Account". Later on this password can be retrieved using the get command:
|
25
25
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.2
|
data/bin/pwl
CHANGED
@@ -7,44 +7,45 @@ $:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
|
|
7
7
|
require 'pwl'
|
8
8
|
|
9
9
|
program :version, Pwl::VERSION
|
10
|
-
program :description, "#{program(:name)} is a secure password
|
10
|
+
program :description, "#{program(:name)} is a secure password locker for the command line."
|
11
11
|
|
12
12
|
EXIT_CODES = {
|
13
13
|
:success => Pwl::Message.new('Success.'),
|
14
14
|
:aborted => Pwl::Message.new('Aborted by user.', 1),
|
15
15
|
:passwords_dont_match => Pwl::ErrorMessage.new('Passwords do not match.', 2),
|
16
16
|
:no_value_found => Pwl::Message.new('No value found for <%= name %>.', 3, :name => 'NAME'),
|
17
|
-
:file_exists => Pwl::ErrorMessage.new('There already exists a
|
18
|
-
:file_not_found => Pwl::ErrorMessage.new('
|
17
|
+
:file_exists => Pwl::ErrorMessage.new('There already exists a locker at <%= file %>. Use --force to overwrite it or --file to specify a different locker.', 4, :file => 'FILE'),
|
18
|
+
:file_not_found => Pwl::ErrorMessage.new('Locker file <%= file %> could not be found.', 5, :file => 'FILE'),
|
19
19
|
:name_blank => Pwl::ErrorMessage.new('Name may not be blank.', 6),
|
20
20
|
:value_blank => Pwl::ErrorMessage.new('Value may not be blank.', 7),
|
21
21
|
:list_empty => Pwl::Message.new('List is empty.', 8),
|
22
22
|
:list_empty_filter => Pwl::Message.new('No names found that match filter <%= filter %>.', 9, :filter => 'FILTER'),
|
23
23
|
:validation_new_failed => Pwl::ErrorMessage.new('<%= message %>.', 10, :message => 'Validation of new master password failed'),
|
24
|
+
:unknown_export_format => Pwl::ErrorMessage.new('<%= format %> is not a known export format.', 11, :format => 'FORMAT'),
|
24
25
|
}
|
25
26
|
|
26
|
-
program :help, 'Exit Status', "#{program(:name)} sets the following exit status:\n\n" + EXIT_CODES.values.sort{|l,r| l.exit_code <=> r.exit_code}.collect{|m| " #{m.exit_code.to_s}: #{m.to_s}"}.join("\n")
|
27
|
+
program :help, 'Exit Status', "#{program(:name)} sets the following exit status:\n\n" + EXIT_CODES.values.sort{|l,r| l.exit_code <=> r.exit_code}.collect{|m| " #{m.exit_code.to_s.rjust(EXIT_CODES.size.to_s.size)}: #{m.to_s}"}.join("\n")
|
27
28
|
program :help, 'Author', 'Nicholas E. Rabenau <nerab@gmx.at>'
|
28
29
|
|
29
|
-
|
30
|
+
DEFAULT_LOCKER_FILE = File.expand_path("~/.#{program(:name)}.pstore")
|
30
31
|
DEFAULT_EXPORT_TEMPLATE = File.join(File.dirname(__FILE__), *%w[.. templates export.html.erb])
|
31
32
|
|
32
|
-
|
33
|
+
locker_file = DEFAULT_LOCKER_FILE
|
33
34
|
|
34
35
|
global_option '-V', '--verbose', 'Enable verbose output'
|
35
|
-
global_option('-f', '--file FILE', 'Determine the file that holds the
|
36
|
+
global_option('-f', '--file FILE', 'Determine the file that holds the locker'){|file| locker_file = file}
|
36
37
|
global_option '-g', '--gui', 'Request the master password using an OS-specific GUI dialog. This option takes precedence over STDIN.'
|
37
38
|
|
38
39
|
command :init do |c|
|
39
40
|
c.syntax = "#{program(:name)} #{c.name}"
|
40
|
-
c.summary = 'Initializes a new
|
41
|
-
c.description = 'This command initializes a new password
|
42
|
-
c.example "Initializes a new password
|
43
|
-
c.example "Initializes a new password
|
44
|
-
c.option '--force', 'Force-overwrite an existing
|
41
|
+
c.summary = 'Initializes a new locker'
|
42
|
+
c.description = 'This command initializes a new password locker. Password quality is enforced using validation rules.'
|
43
|
+
c.example "Initializes a new password locker in #{DEFAULT_LOCKER_FILE}", "#{program(:name)} #{c.name}"
|
44
|
+
c.example "Initializes a new password locker in /tmp/crackme.txt", "#{program(:name)} #{c.name} --file /tmp/crackme.txt"
|
45
|
+
c.option '--force', 'Force-overwrite an existing locker file'
|
45
46
|
c.action do |args, options|
|
46
|
-
#
|
47
|
-
exit_with(:file_exists, options.verbose, :file =>
|
47
|
+
# Locker checks this too, but we want to fail fast.
|
48
|
+
exit_with(:file_exists, options.verbose, :file => locker_file) if File.exists?(locker_file) && !options.force
|
48
49
|
|
49
50
|
begin
|
50
51
|
begin
|
@@ -64,8 +65,8 @@ command :init do |c|
|
|
64
65
|
exit_with(:aborted, options.verbose)
|
65
66
|
end
|
66
67
|
|
67
|
-
Pwl::
|
68
|
-
STDERR.puts "Successfully initialized new
|
68
|
+
Pwl::Locker.new(locker_file, master_password, {:force => options.force})
|
69
|
+
STDERR.puts "Successfully initialized new locker at #{locker_file}" if options.verbose
|
69
70
|
end
|
70
71
|
end
|
71
72
|
|
@@ -75,12 +76,12 @@ command :get do |c|
|
|
75
76
|
c.description = 'This command retrieves the value stored under NAME and prints it on STDOUT.'
|
76
77
|
c.example 'Reads the value stored under the name "foo" and prints it to STDOUT', "#{program(:name)} #{c.name} foo"
|
77
78
|
c.action do |args, options|
|
78
|
-
#
|
79
|
-
exit_with(:file_not_found, options.verbose, :file =>
|
79
|
+
# Locker checks this too, but we want to fail fast and provide a message.
|
80
|
+
exit_with(:file_not_found, options.verbose, :file => locker_file) unless File.exists?(locker_file)
|
80
81
|
exit_with(:name_blank, options.verbose) if 0 == args.size || args[0].blank?
|
81
82
|
|
82
83
|
begin
|
83
|
-
result = Pwl::
|
84
|
+
result = Pwl::Locker.open(locker_file, get_password("Enter the master password for #{program(:name)}:", options.gui)).get(args[0])
|
84
85
|
if result.blank?
|
85
86
|
exit_with(:no_value_found, options.verbose, :name => args[0])
|
86
87
|
else
|
@@ -101,13 +102,13 @@ command :list do |c|
|
|
101
102
|
c.example 'Prints all names separated by comma', "#{program(:name)} #{c.name} --separator ,"
|
102
103
|
c.option '-s', '--separator SEPARATOR', String, 'Separate names by SEPARATOR'
|
103
104
|
c.action do |args, options|
|
104
|
-
#
|
105
|
-
exit_with(:file_not_found, options.verbose, :file =>
|
105
|
+
# Locker checks this too, but we want to fail fast and provide a message.
|
106
|
+
exit_with(:file_not_found, options.verbose, :file => locker_file) unless File.exists?(locker_file)
|
106
107
|
|
107
108
|
options.default :separator => ' '
|
108
109
|
|
109
110
|
begin
|
110
|
-
result = Pwl::
|
111
|
+
result = Pwl::Locker.open(locker_file, get_password("Enter the master password for #{program(:name)}:", options.gui)).list(args[0]).join(options.separator)
|
111
112
|
if !result.blank?
|
112
113
|
puts result
|
113
114
|
else
|
@@ -123,19 +124,19 @@ command :list do |c|
|
|
123
124
|
end
|
124
125
|
end
|
125
126
|
|
126
|
-
command :
|
127
|
+
command :add do |c|
|
127
128
|
c.syntax = "#{program(:name)} #{c.name} NAME [VALUE]"
|
128
129
|
c.summary = 'Stores VALUE under NAME'
|
129
|
-
c.description = 'Adds or updates the entry stored under NAME. If NAME is already present in the
|
130
|
+
c.description = 'Adds or updates the entry stored under NAME. If NAME is already present in the locker, it will be updated with VALUE. If NAME is not already present in the locker, a new entry will be created. If VALUE is not given, it will be read from STDIN.'
|
130
131
|
c.example 'Stores the value "bar" under the name "foo"', "#{program(:name)} #{c.name} foo bar"
|
131
132
|
c.example 'Reads STDIN and stores that as value under the name "foo"', "#{program(:name)} #{c.name} foo"
|
132
133
|
c.action do |args, options|
|
133
|
-
exit_with(:file_not_found, options.verbose, :file =>
|
134
|
+
exit_with(:file_not_found, options.verbose, :file => locker_file) unless File.exists?(locker_file)
|
134
135
|
exit_with(:name_blank, options.verbose) if 0 == args.size || args[0].blank?
|
135
136
|
|
136
137
|
# Ask for the master password _before_ it may be necessary to ask for the value in a terminal
|
137
138
|
begin
|
138
|
-
|
139
|
+
locker = Pwl::Locker.open(locker_file, get_password("Enter the master password for #{program(:name)}:", options.gui))
|
139
140
|
rescue Pwl::Dialog::Cancelled
|
140
141
|
exit_with(:aborted, options.verbose)
|
141
142
|
end
|
@@ -157,7 +158,7 @@ command :put do |c|
|
|
157
158
|
end
|
158
159
|
end
|
159
160
|
|
160
|
-
|
161
|
+
locker.add(args[0], value)
|
161
162
|
STDERR.puts "Successfully stored new value under #{args[0]}." if options.verbose
|
162
163
|
end
|
163
164
|
end
|
@@ -165,19 +166,19 @@ end
|
|
165
166
|
command :delete do |c|
|
166
167
|
c.syntax = "#{program(:name)} #{c.name} NAME"
|
167
168
|
c.summary = 'Deletes the entry stored under NAME'
|
168
|
-
c.description = 'Deletes the complete entry that is stored under NAME. If NAME is not present in the
|
169
|
+
c.description = 'Deletes the complete entry that is stored under NAME. If NAME is not present in the locker, an error will thrown.'
|
169
170
|
c.example 'Deletes what was stored under the name "foo"', "#{program(:name)} #{c.name} foo"
|
170
171
|
c.action do |args, options|
|
171
|
-
exit_with(:file_not_found, options.verbose, :file =>
|
172
|
+
exit_with(:file_not_found, options.verbose, :file => locker_file) unless File.exists?(locker_file)
|
172
173
|
exit_with(:name_blank, options.verbose) if 0 == args.size || args[0].blank?
|
173
174
|
|
174
175
|
begin
|
175
|
-
|
176
|
+
locker = Pwl::Locker.open(locker_file, get_password("Enter the master password for #{program(:name)}:", options.gui))
|
176
177
|
rescue Pwl::Dialog::Cancelled
|
177
178
|
exit_with(:aborted, options.verbose)
|
178
179
|
end
|
179
180
|
|
180
|
-
|
181
|
+
locker.delete(args[0])
|
181
182
|
STDERR.puts "Successfully deleted the value under #{args[0]}." if options.verbose
|
182
183
|
end
|
183
184
|
end
|
@@ -185,12 +186,12 @@ end
|
|
185
186
|
command :passwd do |c|
|
186
187
|
c.syntax = "#{program(:name)} #{c.name} [NEW_MASTER_PASSWORD]"
|
187
188
|
c.summary = 'Changes the master password to NEW_MASTER_PASSWORD.'
|
188
|
-
c.description = 'This command changes the master password of the
|
189
|
+
c.description = 'This command changes the master password of the locker. Password quality is enforced using validation rules.'
|
189
190
|
c.action do |args, options|
|
190
|
-
exit_with(:file_not_found, options.verbose, :file =>
|
191
|
+
exit_with(:file_not_found, options.verbose, :file => locker_file) unless File.exists?(locker_file)
|
191
192
|
|
192
193
|
begin
|
193
|
-
|
194
|
+
locker = Pwl::Locker.open(locker_file, get_password("Enter the current master password for #{program(:name)}:", options.gui))
|
194
195
|
|
195
196
|
if !STDIN.tty? && !options.gui
|
196
197
|
# If we are in a pipe and do not run in GUI mode, we accept the new master password as args[0]
|
@@ -204,7 +205,7 @@ command :passwd do |c|
|
|
204
205
|
else
|
205
206
|
# If running interactively (console or gui), we loop until we get a valid password or break
|
206
207
|
begin
|
207
|
-
new_master_password = get_password(
|
208
|
+
new_master_password = get_password("Enter the new master password for #{program(:name)}:", options.gui)
|
208
209
|
end while begin
|
209
210
|
validate!(new_master_password)
|
210
211
|
rescue Pwl::InvalidMasterPasswordError => e
|
@@ -213,7 +214,7 @@ command :passwd do |c|
|
|
213
214
|
end
|
214
215
|
|
215
216
|
# Confirm new password
|
216
|
-
if new_master_password != get_password(
|
217
|
+
if new_master_password != get_password("Enter the new master password for #{program(:name)} again:", options.gui)
|
217
218
|
exit_with(:passwords_dont_match, options.verbose)
|
218
219
|
end
|
219
220
|
end
|
@@ -221,8 +222,8 @@ command :passwd do |c|
|
|
221
222
|
exit_with(:aborted, options.verbose)
|
222
223
|
end
|
223
224
|
|
224
|
-
|
225
|
-
STDERR.puts "Successfully changed master password." if options.verbose
|
225
|
+
locker.change_password!(new_master_password)
|
226
|
+
STDERR.puts "Successfully changed master password for #{program(:name)}." if options.verbose
|
226
227
|
end
|
227
228
|
end
|
228
229
|
|
@@ -231,13 +232,20 @@ command :export do |c|
|
|
231
232
|
c.summary = 'Exports all entries.'
|
232
233
|
c.description = 'This command prints all entries to STDOUT.'
|
233
234
|
c.example 'Prints all entries', "#{program(:name)} #{c.name}"
|
235
|
+
c.option '--format FORMAT', String, 'Specify export format. Default is html.'
|
236
|
+
c.example 'Prints all entries in HTML format (default)', "#{program(:name)} #{c.name} --format html"
|
237
|
+
c.example 'Prints all entries in JSON format', "#{program(:name)} #{c.name} --format json"
|
238
|
+
c.example 'Prints all entries in YAML format', "#{program(:name)} #{c.name} --format yaml"
|
234
239
|
c.action do |args, options|
|
235
|
-
exit_with(:file_not_found, options.verbose, :file =>
|
240
|
+
exit_with(:file_not_found, options.verbose, :file => locker_file) unless File.exists?(locker_file)
|
241
|
+
options.default :format => 'html'
|
236
242
|
|
243
|
+
presenter = {:html => Pwl::Presenter::Html, :json => Pwl::Presenter::Json, :yaml => Pwl::Presenter::Yaml}[options.format.to_sym]
|
244
|
+
exit_with(:unknown_export_format, options.verbose, :format => options.format) if presenter.nil?
|
245
|
+
|
237
246
|
begin
|
238
|
-
|
239
|
-
|
240
|
-
puts template.result(binding)
|
247
|
+
locker = Pwl::Locker.open(locker_file, get_password("Enter the master password for #{program(:name)}:", options.gui))
|
248
|
+
puts presenter.new(locker).to_s
|
241
249
|
rescue Pwl::Dialog::Cancelled
|
242
250
|
exit_with(:aborted, options.verbose)
|
243
251
|
end
|
@@ -264,5 +272,5 @@ def get_text(prompt, gui = false)
|
|
264
272
|
end
|
265
273
|
|
266
274
|
def validate!(pwd)
|
267
|
-
Pwl::
|
275
|
+
Pwl::Locker.password_policy.validate!(pwd)
|
268
276
|
end
|
data/lib/pwl.rb
CHANGED
@@ -1,13 +1,17 @@
|
|
1
1
|
require 'pstore'
|
2
2
|
require 'encryptor'
|
3
|
-
require '
|
3
|
+
require 'date'
|
4
|
+
require 'active_support'
|
4
5
|
|
5
6
|
$:.unshift File.join(File.dirname(__FILE__), *%w[pwl])
|
6
7
|
|
7
8
|
require 'message'
|
8
9
|
require 'password_policy'
|
9
|
-
require '
|
10
|
+
require 'locker'
|
10
11
|
require 'dialog'
|
12
|
+
require 'presenter/html'
|
13
|
+
require 'presenter/json'
|
14
|
+
require 'presenter/yaml'
|
11
15
|
|
12
16
|
module Pwl
|
13
17
|
VERSION = File.read(File.join(File.dirname(__FILE__), *%w[.. VERSION]))
|
@@ -1,7 +1,5 @@
|
|
1
|
-
require 'date'
|
2
|
-
|
3
1
|
module Pwl
|
4
|
-
class
|
2
|
+
class Locker
|
5
3
|
class FileAlreadyExistsError < StandardError
|
6
4
|
def initialize(file)
|
7
5
|
super("The file #{file} already exists")
|
@@ -10,7 +8,7 @@ module Pwl
|
|
10
8
|
|
11
9
|
class NotInitializedError < StandardError
|
12
10
|
def initialize(file)
|
13
|
-
super("The
|
11
|
+
super("The locker at #{file} was not initialized yet")
|
14
12
|
end
|
15
13
|
end
|
16
14
|
|
@@ -22,7 +20,7 @@ module Pwl
|
|
22
20
|
|
23
21
|
class FileNotFoundError < StandardError
|
24
22
|
def initialize(file)
|
25
|
-
super("The file #{file} for the
|
23
|
+
super("The file #{file} for the locker was not found")
|
26
24
|
end
|
27
25
|
end
|
28
26
|
|
@@ -56,28 +54,28 @@ module Pwl
|
|
56
54
|
DEFAULT_PASSWORD_POLICY = ReasonableComplexityPasswordPolicy.new
|
57
55
|
|
58
56
|
#
|
59
|
-
# Constructs a new
|
57
|
+
# Constructs a new locker (not only the object, but also the file behind it).
|
60
58
|
#
|
61
59
|
def new(file, master_password, options = {})
|
62
60
|
if File.exists?(file) && !options[:force] # don't allow accedidential override of existing file
|
63
61
|
raise FileAlreadyExistsError.new(file)
|
64
62
|
else
|
65
63
|
password_policy.validate!(master_password)
|
66
|
-
|
67
|
-
|
64
|
+
locker = load(file, master_password)
|
65
|
+
locker.reset!
|
68
66
|
end
|
69
67
|
|
70
|
-
|
68
|
+
locker
|
71
69
|
end
|
72
70
|
|
73
71
|
#
|
74
|
-
# Opens an existing
|
72
|
+
# Opens an existing locker. Throws if the backing file does not exist or isn't initialized.
|
75
73
|
#
|
76
74
|
def open(file, master_password)
|
77
75
|
raise FileNotFoundError.new(file) unless File.exists?(file)
|
78
|
-
|
79
|
-
|
80
|
-
|
76
|
+
locker = load(file, master_password)
|
77
|
+
locker.authenticate # do not allow openeing without successful authentication
|
78
|
+
locker
|
81
79
|
end
|
82
80
|
|
83
81
|
def password_policy
|
@@ -90,9 +88,9 @@ module Pwl
|
|
90
88
|
end
|
91
89
|
|
92
90
|
#
|
93
|
-
# Create a new
|
91
|
+
# Create a new locker object by loading an existing file.
|
94
92
|
#
|
95
|
-
# Beware: New is overridden; it performs additional actions
|
93
|
+
# Beware: New is overridden; it performs additional actions before and after #initialize
|
96
94
|
#
|
97
95
|
def initialize(file, master_password)
|
98
96
|
@backend = PStore.new(file, true)
|
@@ -113,7 +111,7 @@ module Pwl
|
|
113
111
|
end
|
114
112
|
|
115
113
|
#
|
116
|
-
# Check that the master password is correct. This is done to prevent opening an existing but blank
|
114
|
+
# Check that the master password is correct. This is done to prevent opening an existing but blank locker with the wrong password.
|
117
115
|
#
|
118
116
|
def authenticate
|
119
117
|
begin
|
@@ -142,7 +140,7 @@ module Pwl
|
|
142
140
|
#
|
143
141
|
# Store value stored under key
|
144
142
|
#
|
145
|
-
def
|
143
|
+
def add(key, value)
|
146
144
|
raise BlankKeyError if key.blank?
|
147
145
|
raise BlankValueError if value.blank?
|
148
146
|
@backend.transaction{
|
@@ -218,28 +216,28 @@ module Pwl
|
|
218
216
|
end
|
219
217
|
|
220
218
|
#
|
221
|
-
# Return the date when the
|
219
|
+
# Return the date when the locker was created
|
222
220
|
#
|
223
221
|
def created
|
224
222
|
@backend.transaction(true){@backend[:system][:created]}
|
225
223
|
end
|
226
224
|
|
227
225
|
#
|
228
|
-
# Return the date when the
|
226
|
+
# Return the date when the locker was last accessed
|
229
227
|
#
|
230
228
|
def last_accessed
|
231
229
|
@backend.transaction(true){@backend[:system][:last_accessed]}
|
232
230
|
end
|
233
231
|
|
234
232
|
#
|
235
|
-
# Return the date when the
|
233
|
+
# Return the date when the locker was last modified
|
236
234
|
#
|
237
235
|
def last_modified
|
238
236
|
@backend.transaction(true){@backend[:system][:last_modified]}
|
239
237
|
end
|
240
238
|
|
241
239
|
#
|
242
|
-
# Return the path to the file backing this
|
240
|
+
# Return the path to the file backing this locker
|
243
241
|
#
|
244
242
|
def path
|
245
243
|
@backend.path
|