1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2024-11-30 08:53:05 +00:00

remove the password file

This commit is contained in:
nishiki 2013-09-08 20:04:31 +02:00
parent 59cffbf3eb
commit fb0126d519
2 changed files with 32 additions and 55 deletions

View file

@ -31,10 +31,9 @@ class Cli
puts "# --------------------" puts "# --------------------"
key = ask("Enter the GPG key: ") key = ask("Enter the GPG key: ")
file_gpg = ask("Enter the path to encrypt file [default=#{Dir.home()}/.mpw.gpg]: ") file_gpg = ask("Enter the path to encrypt file [default=#{Dir.home()}/.mpw.gpg]: ")
file_pwd = ask("Enter te path to password file [default=#{Dir.home()}/.mpw.pwd]: ") timeout_pwd = ask("Enter the timeout (in seconde) to GPG password [default=60]: ")
timeout_pwd = ask("Enter the timeout (in seconde) to GPG password [default=300]: ")
if @m.setup(key, file_gpg, file_pwd, timeout_pwd) if @m.setup(key, file_gpg, timeout_pwd)
puts "The config file has been created!" puts "The config file has been created!"
else else
puts "ERROR: #{@m.error_msg}" puts "ERROR: #{@m.error_msg}"
@ -43,12 +42,8 @@ class Cli
# Request the GPG password and decrypt the file # Request the GPG password and decrypt the file
def decrypt() def decrypt()
if not @m.checkFilePassword() @passwd = ask("Password GPG: ") {|q| q.echo = false}
passwd = ask("Password GPG: ") {|q| q.echo = false} return @m.decrypt(@passwd)
return @m.decrypt(passwd)
else
return @m.decrypt()
end
end end
# Display the query's result # Display the query's result
@ -60,9 +55,9 @@ class Cli
if not result.empty? if not result.empty?
result.each do |r| result.each do |r|
if format.nil? || !format if format.nil? || !format
displayFormat(r) self.displayFormat(r)
else else
displayFormatAlt(r) self.displayFormatAlt(r)
end end
end end
else else
@ -165,7 +160,7 @@ class Cli
result = @m.searchById(id) result = @m.searchById(id)
if result.length > 0 if result.length > 0
displayFormat(result) self.displayFormat(result)
confirm = ask("Are you sure to remove the item: #{id} ? (y/N) ") confirm = ask("Are you sure to remove the item: #{id} ? (y/N) ")
if confirm =~ /^(y|yes|YES|Yes|Y)$/ if confirm =~ /^(y|yes|YES|Yes|Y)$/
@ -209,7 +204,7 @@ class Cli
if not force if not force
if result.is_a?(Array) && !result.empty? if result.is_a?(Array) && !result.empty?
result.each do |r| result.each do |r|
displayFormat(r) self.displayFormat(r)
end end
confirm = ask("Are you sure to import this file: #{file} ? (y/N) ") confirm = ask("Are you sure to import this file: #{file} ? (y/N) ")
@ -232,23 +227,38 @@ class Cli
# Interactive mode # Interactive mode
def interactive() def interactive()
last_access = Time.now.to_i
while true while true
if @m.timeout_pwd < Time.now.to_i - last_access
passwd_confirm = ask("Password GPG: ") {|q| q.echo = false}
if @passwd.eql?(passwd_confirm)
last_access = Time.now.to_i
else
puts 'Bad password!'
next
end
else
last_access = Time.now.to_i
end
command = ask("<mpw> ").split(' ') command = ask("<mpw> ").split(' ')
case command[0] case command[0]
when 'display', 'show', 'd', 's' when 'display', 'show', 'd', 's'
if !command[1].nil? && !command[1].empty? if !command[1].nil? && !command[1].empty?
display(command[1], command[2]) self.display(command[1], command[2])
end end
when 'add', 'a' when 'add', 'a'
cli.add() add()
when 'update', 'u' when 'update', 'u'
if !command[1].nil? && !command[1].empty? if !command[1].nil? && !command[1].empty?
update(command[1]) self.update(command[1])
end end
when 'remove', 'delete', 'r', 'd' when 'remove', 'delete', 'r', 'd'
if !command[1].nil? && !command[1].empty? if !command[1].nil? && !command[1].empty?
remove(command[1]) self.remove(command[1])
end end
when 'help', 'h', '?' when 'help', 'h', '?'
puts '# Help' puts '# Help'

View file

@ -21,6 +21,7 @@ class MPW
COMMENT = 8 COMMENT = 8
attr_accessor :error_msg attr_accessor :error_msg
attr_accessor :timeout_pwd
# Constructor # Constructor
def initialize() def initialize()
@ -34,7 +35,7 @@ class MPW
# file_pwd -> the file who stock the password # file_pwd -> the file who stock the password
# timeout_pwd -> time to save the password # timeout_pwd -> time to save the password
# @rtrn: true if le config file is create # @rtrn: true if le config file is create
def setup(key, file_gpg, file_pwd, timeout_pwd) def setup(key, file_gpg, timeout_pwd)
if not key =~ /[a-zA-Z0-9.-_]+\@[a-zA-Z0-9]+\.[a-zA-Z]+/ if not key =~ /[a-zA-Z0-9.-_]+\@[a-zA-Z0-9]+\.[a-zA-Z]+/
@error_msg = "The key string isn't in good format!" @error_msg = "The key string isn't in good format!"
@ -45,16 +46,11 @@ class MPW
file_gpg = "#{Dir.home()}/.mpw.gpg" file_gpg = "#{Dir.home()}/.mpw.gpg"
end end
if file_pwd.empty? timeout_pwd.empty? ? (timeout_pwd = 60) : (timeout_pwd = timeout_pwd.to_i)
file_pwd = "#{Dir.home()}/.mpw.pwd"
end
timeout_pwd.empty? ? (timeout_pwd = 300) : (timeout_pwd = timeout_pwd.to_i)
config = {'config' => {'key' => key, config = {'config' => {'key' => key,
'file_gpg' => file_gpg, 'file_gpg' => file_gpg,
'timeout_pwd' => timeout_pwd, 'timeout_pwd' => timeout_pwd}}
'file_pwd' => file_pwd}}
begin begin
File.open(@file_config, 'w') do |file| File.open(@file_config, 'w') do |file|
@ -75,10 +71,9 @@ class MPW
config = YAML::load_file(@file_config) config = YAML::load_file(@file_config)
@key = config['config']['key'] @key = config['config']['key']
@file_gpg = config['config']['file_gpg'] @file_gpg = config['config']['file_gpg']
@file_pwd = config['config']['file_pwd']
@timeout_pwd = config['config']['timeout_pwd'].to_i @timeout_pwd = config['config']['timeout_pwd'].to_i
if @key.empty? || @file_gpg.empty? || @file_pwd.empty? if @key.empty? || @file_gpg.empty?
@error_msg = "Checkconfig failed!" @error_msg = "Checkconfig failed!"
return false return false
end end
@ -97,21 +92,6 @@ class MPW
def decrypt(passwd=nil) def decrypt(passwd=nil)
@data = Array.new @data = Array.new
begin
if passwd.nil? || passwd.empty?
passwd = IO.read(@file_pwd)
elsif !passwd.nil? && !passwd.empty?
file_pwd = File.new(@file_pwd, 'w')
File.chmod(0600, @file_pwd)
file_pwd << passwd
file_pwd.close
end
rescue Exception => e
@error_msg = "Can't decrypt file!\n#{e}"
return false
end
begin begin
if File.exist?(@file_gpg) if File.exist?(@file_gpg)
crypto = GPGME::Crypto.new(:armor => true) crypto = GPGME::Crypto.new(:armor => true)
@ -135,19 +115,6 @@ class MPW
end end
end end
# Check if a password it saved
# @rtrn: true if a password exist in the password file
def checkFilePassword()
if !@file_pwd.nil? && File.exist?(@file_pwd) && File.stat(@file_pwd).mtime.to_i + @timeout_pwd < Time.now.to_i
File.delete(@file_pwd)
return false
elsif !@file_pwd.nil? && File.exist?(@file_pwd)
return true
else
return false
end
end
# Encrypt a file # Encrypt a file
# @rtrn: true if the file has been encrypted # @rtrn: true if the file has been encrypted
def encrypt() def encrypt()