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

restructiring the code

This commit is contained in:
nishiki 2014-01-12 00:37:46 +01:00
parent 6b9474e538
commit e0a120d8f1
3 changed files with 146 additions and 108 deletions

View file

@ -10,6 +10,7 @@ require 'readline'
require 'i18n' require 'i18n'
require "#{APP_ROOT}/lib/MPW.rb" require "#{APP_ROOT}/lib/MPW.rb"
require "#{APP_ROOT}/lib/MPWConfig.rb"
class Cli class Cli
@ -17,14 +18,15 @@ class Cli
# @args: lang -> the operating system language # @args: lang -> the operating system language
# config_file -> a specify config file # config_file -> a specify config file
def initialize(lang, config_file=nil) def initialize(lang, config_file=nil)
@m = MPW.new(config_file) @config = MPWConfig.new(config_file)
if not @m.checkconfig() if not @config.checkconfig()
self.setup(lang) self.setup(lang)
end end
@mpw = MPW.new(@config.file_gpg, @config.key)
if not self.decrypt() if not self.decrypt()
puts "#{I18n.t('cli.display.error')}: #{@m.error_msg}" puts "#{I18n.t('cli.display.error')}: #{@mpw.error_msg}"
exit 2 exit 2
end end
end end
@ -44,28 +46,28 @@ class Cli
end end
I18n.locale = language.to_sym I18n.locale = language.to_sym
if @m.setup(key, language, file_gpg, timeout_pwd) if @config.setup(key, language, file_gpg, timeout_pwd)
puts I18n.t('cli.form.setup.valid') puts I18n.t('cli.form.setup.valid')
else else
puts "#{I18n.t('cli.display.error')}: #{@m.error_msg}" puts "#{I18n.t('cli.display.error')}: #{@config.error_msg}"
end end
if not @m.checkconfig() if not @config.checkconfig()
puts "#{I18n.t('cli.display.error')}: #{@m.error_msg}" puts "#{I18n.t('cli.display.error')}: #{@config.error_msg}"
end end
end end
# Request the GPG password and decrypt the file # Request the GPG password and decrypt the file
def decrypt() def decrypt()
@passwd = ask(I18n.t('cli.display.gpg_password')) {|q| q.echo = false} @passwd = ask(I18n.t('cli.display.gpg_password')) {|q| q.echo = false}
return @m.decrypt(@passwd) return @mpw.decrypt(@passwd)
end end
# Display the query's result # Display the query's result
# @args: search -> the string to search # @args: search -> the string to search
# protocol -> search from a particular protocol # protocol -> search from a particular protocol
def display(search, protocol=nil, group=nil, format=nil) def display(search, protocol=nil, group=nil, format=nil)
result = @m.search(search, group, protocol) result = @mpw.search(search, group, protocol)
if not result.empty? if not result.empty?
result.each do |r| result.each do |r|
@ -125,21 +127,21 @@ class Cli
port = ask(I18n.t('cli.form.add.port')).to_s port = ask(I18n.t('cli.form.add.port')).to_s
comment = ask(I18n.t('cli.form.add.comment')).to_s comment = ask(I18n.t('cli.form.add.comment')).to_s
if @m.add(name, group, server, protocol, login, passwd, port, comment) if @mpw.add(name, group, server, protocol, login, passwd, port, comment)
if @m.encrypt() if @mpw.encrypt()
puts I18n.t('cli.form.add.valid') puts I18n.t('cli.form.add.valid')
else else
puts "#{I18n.t('cli.display.error')}: #{@m.error_msg}" puts "#{I18n.t('cli.display.error')}: #{@mpw.error_msg}"
end end
else else
puts "#{I18n.t('cli.display.error')}: #{@m.error_msg}" puts "#{I18n.t('cli.display.error')}: #{@mpw.error_msg}"
end end
end end
# Update an item # Update an item
# @args: id -> the item's id # @args: id -> the item's id
def update(id) def update(id)
row = @m.searchById(id) row = @mpw.searchById(id)
if not row.empty? if not row.empty?
puts I18n.t('cli.form.update.title') puts I18n.t('cli.form.update.title')
@ -153,14 +155,14 @@ class Cli
port = ask(I18n.t('cli.form.update.port' , :port => row[MPW::PORT])).to_s port = ask(I18n.t('cli.form.update.port' , :port => row[MPW::PORT])).to_s
comment = ask(I18n.t('cli.form.update.comment' , :comment => row[MPW::COMMENT])).to_s comment = ask(I18n.t('cli.form.update.comment' , :comment => row[MPW::COMMENT])).to_s
if @m.update(id, name, group, server, protocol, login, passwd, port, comment) if @mpw.update(id, name, group, server, protocol, login, passwd, port, comment)
if @m.encrypt() if @mpw.encrypt()
puts I18n.t('cli.form.update.valid') puts I18n.t('cli.form.update.valid')
else else
puts "#{I18n.t('cli.display.error')}: #{@m.error_msg}" puts "#{I18n.t('cli.display.error')}: #{@mpw.error_msg}"
end end
else else
puts "#{I18n.t('cli.display.error')}: #{@m.error_msg}" puts "#{I18n.t('cli.display.error')}: #{@mpw.error_msg}"
end end
else else
puts I18n.t('cli.display.nothing') puts I18n.t('cli.display.nothing')
@ -172,7 +174,7 @@ class Cli
# force -> no resquest a validation # force -> no resquest a validation
def remove(id, force=false) def remove(id, force=false)
if not force if not force
result = @m.searchById(id) result = @mpw.searchById(id)
if result.length > 0 if result.length > 0
self.displayFormat(result) self.displayFormat(result)
@ -187,11 +189,11 @@ class Cli
end end
if force if force
if @m.remove(id) if @mpw.remove(id)
if @m.encrypt() if @mpw.encrypt()
puts I18n.t('cli.form.delete.valid', :id => id) puts I18n.t('cli.form.delete.valid', :id => id)
else else
puts "#{I18n.t('cli.display.error')}: #{@m.error_msg}" puts "#{I18n.t('cli.display.error')}: #{@mpw.error_msg}"
end end
else else
puts I18n.t('cli.form.delete.not_valid') puts I18n.t('cli.form.delete.not_valid')
@ -202,10 +204,10 @@ class Cli
# Export the items in a CSV file # Export the items in a CSV file
# @args: file -> the destination file # @args: file -> the destination file
def export(file) def export(file)
if @m.export(file) if @mpw.export(file)
puts "The export in #{file} is succesfull!" puts "The export in #{file} is succesfull!"
else else
puts "#{I18n.t('cli.display.error')}: #{@m.error_msg}" puts "#{I18n.t('cli.display.error')}: #{@mpw.error_msg}"
end end
end end
@ -214,7 +216,7 @@ class Cli
# @args: file -> the import file # @args: file -> the import file
# force -> no resquest a validation # force -> no resquest a validation
def import(file, force=false) def import(file, force=false)
result = @m.importPreview(file) result = @mpw.importPreview(file)
if not force if not force
if result.is_a?(Array) && !result.empty? if result.is_a?(Array) && !result.empty?
@ -232,10 +234,10 @@ class Cli
end end
if force if force
if @m.import(file) && @m.encrypt() if @mpw.import(file) && @mpw.encrypt()
puts I18n.t('cli.form.import.valid') puts I18n.t('cli.form.import.valid')
else else
puts "#{I18n.t('cli.display.error')}: #{@m.error_msg}" puts "#{I18n.t('cli.display.error')}: #{@mpw.error_msg}"
end end
end end
end end
@ -247,7 +249,7 @@ class Cli
while buf = Readline.readline('<mpw> ', true) while buf = Readline.readline('<mpw> ', true)
if @m.timeout_pwd < Time.now.to_i - last_access if @config.timeout_pwd < Time.now.to_i - last_access
passwd_confirm = ask(I18n.t('cli.interactive.ask_password')) {|q| q.echo = false} passwd_confirm = ask(I18n.t('cli.interactive.ask_password')) {|q| q.echo = false}
if @passwd.eql?(passwd_confirm) if @passwd.eql?(passwd_confirm)

View file

@ -6,7 +6,6 @@
require 'rubygems' require 'rubygems'
require 'gpgme' require 'gpgme'
require 'csv' require 'csv'
require 'yaml'
require 'i18n' require 'i18n'
class MPW class MPW
@ -22,79 +21,12 @@ class MPW
COMMENT = 8 COMMENT = 8
attr_accessor :error_msg attr_accessor :error_msg
attr_accessor :timeout_pwd
# Constructor # Constructor
# @args: file_config -> the specify config file def initialize(file_gpg, key=nil)
def initialize(file_config=nil)
@error_msg = nil @error_msg = nil
@file_config = "#{Dir.home()}/.mpw.cfg" @file_gpg = file_gpg
@key = key
if !file_config.nil? && !file_config.empty?
@file_config = file_config
end
end
# Create a new config file
# @args: key -> the gpg key to encrypt
# lang -> the software language
# file_gpg -> the file who is encrypted
# file_pwd -> the file who stock the password
# timeout_pwd -> time to save the password
# @rtrn: true if le config file is create
def setup(key, lang, file_gpg, timeout_pwd)
if not key =~ /[a-zA-Z0-9.-_]+\@[a-zA-Z0-9]+\.[a-zA-Z]+/
@error_msg = I18n.t('error.config.key_bad_format')
return false
end
if file_gpg.empty?
file_gpg = "#{Dir.home()}/.mpw.gpg"
end
timeout_pwd.empty? ? (timeout_pwd = 60) : (timeout_pwd = timeout_pwd.to_i)
config = {'config' => {'key' => key,
'lang' => lang,
'file_gpg' => file_gpg,
'timeout_pwd' => timeout_pwd}}
begin
File.open(@file_config, 'w') do |file|
file << config.to_yaml
end
rescue Exception => e
@error_msg = "#{I18n.t('error.config.write')}\n#{e}"
return false
end
return true
end
# Check the config file
# @rtrn: true if the config file is correct
def checkconfig()
begin
config = YAML::load_file(@file_config)
@key = config['config']['key']
@lang = config['config']['lang']
@file_gpg = config['config']['file_gpg']
@timeout_pwd = config['config']['timeout_pwd'].to_i
if @key.empty? || @file_gpg.empty?
@error_msg = I18n.t('error.config.check')
return false
end
I18n.locale = @lang.to_sym
rescue Exception => e
@error_msg = "#{I18n.t('error.config.check')}\n#{e}"
return false
end
return true
end end
# Decrypt a gpg file # Decrypt a gpg file
@ -117,10 +49,6 @@ class MPW
return true return true
rescue Exception => e rescue Exception => e
if !@file_pwd.nil? && File.exist?(@file_pwd)
File.delete(@file_pwd)
end
@error_msg = "#{I18n.t('error.gpg_file.decrypt')}\n#{e}" @error_msg = "#{I18n.t('error.gpg_file.decrypt')}\n#{e}"
return false return false
end end
@ -327,7 +255,7 @@ class MPW
end end
end end
# Return # Return a preview import
# @args: file -> path to file import # @args: file -> path to file import
# @rtrn: an array with the items to import, if there is an error return false # @rtrn: an array with the items to import, if there is an error return false
def importPreview(file) def importPreview(file)
@ -372,7 +300,6 @@ class MPW
result << ([*('A'..'Z'),*('a'..'z'),*('0'..'9')]).sample(length).join result << ([*('A'..'Z'),*('a'..'z'),*('0'..'9')]).sample(length).join
return result return result
#return ([*('A'..'Z'),*('a'..'z'),*('0'..'9')]-%w(0 1 I O l i o)).sample(length).join
end end
end end

109
lib/MPWConfig.rb Normal file
View file

@ -0,0 +1,109 @@
#!/usr/bin/ruby
# author: nishiki
# mail: nishiki@yaegashi.fr
# info: a simple script who manage your passwords
require 'rubygems'
require 'yaml'
require 'i18n'
class MPWConfig
attr_accessor :error_msg
attr_accessor :key
attr_accessor :lang
attr_accessor :file_gpg
attr_accessor :timeout_pwd
attr_accessor :sync_host
attr_accessor :sync_port
attr_accessor :sync_pwd
attr_accessor :sync_sufix
# Constructor
# @args: file_config -> the specify config file
def initialize(file_config=nil)
@error_msg = nil
@file_config = "#{Dir.home()}/.mpw.cfg"
if !file_config.nil? && !file_config.empty?
@file_config = file_config
end
end
# Create a new config file
# @args: key -> the gpg key to encrypt
# lang -> the software language
# file_gpg -> the file who is encrypted
# timeout_pwd -> time to save the password
# @rtrn: true if le config file is create
def setup(key, lang, file_gpg, timeout_pwd)
if not key =~ /[a-zA-Z0-9.-_]+\@[a-zA-Z0-9]+\.[a-zA-Z]+/
@error_msg = I18n.t('error.config.key_bad_format')
return false
end
if file_gpg.empty?
file_gpg = "#{Dir.home()}/.mpw.gpg"
end
timeout_pwd.empty? ? (timeout_pwd = 60) : (timeout_pwd = timeout_pwd.to_i)
config = {'config' => {'key' => key,
'lang' => lang,
'file_gpg' => file_gpg,
'timeout_pwd' => timeout_pwd,
'sync_host' => host,
'sync_port' => port,
'sync_pwd' => password,
'sync_suffix' => suffix}}
begin
File.open(@file_config, 'w') do |file|
file << config.to_yaml
end
rescue Exception => e
@error_msg = "#{I18n.t('error.config.write')}\n#{e}"
return false
end
return true
end
# Check the config file
# @rtrn: true if the config file is correct
def checkconfig()
begin
config = YAML::load_file(@file_config)
@key = config['config']['key']
@lang = config['config']['lang']
@file_gpg = config['config']['file_gpg']
@timeout_pwd = config['config']['timeout_pwd'].to_i
@sync_host = config['config']['sync_host']
@sync_port = config['config']['sync_port']
@sync_pwd = config['config']['sync_pwd']
@sync_sufix = config['config']['sync_suffix']
if @key.empty? || @file_gpg.empty?
@error_msg = I18n.t('error.config.check')
return false
end
I18n.locale = @lang.to_sym
rescue Exception => e
@error_msg = "#{I18n.t('error.config.check')}\n#{e}"
return false
end
begin
@sync = TCPSocket.new(@sync_host, @sync_port)
rescue
@sync = false
end
return true
end
end