summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo <mo.khan@gmail.com>2018-02-11 13:38:02 -0700
committermo <mo.khan@gmail.com>2018-02-11 13:38:02 -0700
commit74c4021f389593d2d195052b2710d9393843630f (patch)
tree078c1c8350a9030923c5023046fa82d94d8482de
parentd0db2beb86ca58b852c60955a107f5ec8b687abb (diff)
check if a file is encrypted before decrypting.
-rwxr-xr-xexe/tfa8
-rw-r--r--lib/tfa/secure_proxy.rb15
2 files changed, 17 insertions, 6 deletions
diff --git a/exe/tfa b/exe/tfa
index d546e95..a2e7495 100755
--- a/exe/tfa
+++ b/exe/tfa
@@ -1,5 +1,9 @@
#!/usr/bin/env ruby
require 'tfa'
-result = TFA::CLI.start(ARGV)
-puts result unless result.is_a?(IO)
+begin
+ result = TFA::CLI.start(ARGV)
+ puts result unless result.is_a?(IO)
+rescue OpenSSL::Cipher::CipherError => error
+ puts error.message
+end
diff --git a/lib/tfa/secure_proxy.rb b/lib/tfa/secure_proxy.rb
index 96481e5..fcfa55e 100644
--- a/lib/tfa/secure_proxy.rb
+++ b/lib/tfa/secure_proxy.rb
@@ -20,8 +20,6 @@ module TFA
end
def decrypt!
- return unless File.exist?(@original.path)
-
data = JSON.parse(IO.read(@original.path), symbolize_names: true)
decipher = OpenSSL::Cipher.new(data[:algorithm])
decipher.decrypt
@@ -35,10 +33,19 @@ module TFA
def method_missing(name, *args, &block)
super unless @original.respond_to?(name)
- decrypt!
+ was_encrypted = encrypted?
+ decrypt! if was_encrypted
result = @original.public_send(name, *args, &block)
- encrypt!
+ encrypt! if was_encrypted
result
end
+
+ def encrypted?
+ return false unless File.exist?(@original.path)
+ JSON.parse(IO.read(@original.path))
+ true
+ rescue JSON::ParseError
+ false
+ end
end
end