diff options
| author | mo <mo.khan@gmail.com> | 2018-02-11 11:43:07 -0700 |
|---|---|---|
| committer | mo <mo.khan@gmail.com> | 2018-02-11 11:43:07 -0700 |
| commit | 4c3fc39c75526bc20017e44a03a17927706302c3 (patch) | |
| tree | 7e03e43be683d45fbeb2a3785ebfb872c1adb05f | |
| parent | 54a8f0b546a41a8cf691ac6f3f24ec0dbec48299 (diff) | |
decrypt database before usage.
| -rw-r--r-- | lib/tfa/cli.rb | 44 |
1 files changed, 34 insertions, 10 deletions
diff --git a/lib/tfa/cli.rb b/lib/tfa/cli.rb index 998e3ec..b1fe340 100644 --- a/lib/tfa/cli.rb +++ b/lib/tfa/cli.rb @@ -8,32 +8,38 @@ module TFA desc "add NAME SECRET", "add a new secret to the database" def add(name, secret) - storage.save(name, clean(secret)) + open_database do + storage.save(name, clean(secret)) + end "Added #{name}" end desc "destroy NAME", "remove the secret associated with the name" def destroy(name) - storage.delete(name) + open_database do + storage.delete(name) + end end desc "show NAME", "shows the secret for the given key" def show(name = nil) - name ? storage.secret_for(name) : storage.all - rescue Psych::SyntaxError - say_status :error, "Unable to open database. Is it encrypted?", :red + open_database do + name ? storage.secret_for(name) : storage.all + end end desc "totp NAME", "generate a Time based One Time Password using the secret associated with the given NAME." def totp(name = nil) - TotpCommand.new(storage).run(name) - rescue Psych::SyntaxError - say_status :error, "Unable to open database. Is it encrypted?", :red + open_database do + TotpCommand.new(storage).run(name) + end end desc "now SECRET", "generate a Time based One Time Password for the given secret" def now(secret) - TotpCommand.new(storage).run('', secret) + open_database do + TotpCommand.new(storage).run('', secret) + end end desc "upgrade", "upgrade the pstore database to a yml database." @@ -42,6 +48,10 @@ module TFA say_status :error, "Unable to detect #{pstore_path}", :red return end + if File.exist?(yaml_path) + say_status :error, "The new database format was detected.", :red + return + end if yes? "Upgrade to #{yaml_path}?" pstore_storage.each do |row| @@ -49,6 +59,7 @@ module TFA yaml_storage.save(name, secret) if yes?("Migrate `#{name}`?") end end + yaml_storage.encrypt!(passphrase) File.delete(pstore_path) if yes?("Delete `#{pstore_path}`?") end end @@ -110,12 +121,25 @@ module TFA end def ensure_upgraded! - if File.exist?(pstore_path) + unless upgraded? say_status :error, "Use the `upgrade` command to upgrade your database.", :red false else true end end + + def upgraded? + !File.exist?(pstore_path) && File.exist?(yaml_path) + end + + def open_database + if upgraded? + yaml_storage.decrypt!(passphrase) + end + result = yield + yaml_storage.encrypt!(passphrase) + result + end end end |
