1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2025-02-21 10:20:05 +00:00

change tab by 2 spaces

change tab by 2 spaces
This commit is contained in:
Adrien Waksberg 2017-03-27 22:18:17 +02:00
parent 871b1dcbed
commit 7c72b2d90f
20 changed files with 1600 additions and 1600 deletions

44
bin/mpw
View file

@ -1,17 +1,17 @@
#!/usr/bin/ruby #!/usr/bin/ruby
# MPW is a software to crypt and manage your passwords # MPW is a software to crypt and manage your passwords
# Copyright (C) 2016 Adrien Waksberg <mpw@yae.im> # Copyright (C) 2016 Adrien Waksberg <mpw@yae.im>
# #
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License # modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2 # as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version. # of the License, or (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details. # GNU General Public License for more details.
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
@ -30,7 +30,7 @@ require 'colorize'
lang = Locale::Tag.parse(ENV['LANG']).to_simple.to_s[0..1] lang = Locale::Tag.parse(ENV['LANG']).to_simple.to_s[0..1]
if defined?(I18n.enforce_available_locales) if defined?(I18n.enforce_available_locales)
I18n.enforce_available_locales = true I18n.enforce_available_locales = true
end end
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks) I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
@ -46,24 +46,24 @@ bin_dir = File.dirname(__FILE__)
command = "#{bin_dir}/mpw-#{ARGV[0]}" command = "#{bin_dir}/mpw-#{ARGV[0]}"
if Dir.glob("#{bin_dir}/mpw-*").include?("#{command}") if Dir.glob("#{bin_dir}/mpw-*").include?("#{command}")
begin begin
Kernel.load(command) Kernel.load(command)
rescue OptionParser::ParseError => e rescue OptionParser::ParseError => e
puts "#{I18n.t('display.error')}: #{e}".red puts "#{I18n.t('display.error')}: #{e}".red
end end
else else
puts "#{I18n.t('option.usage')}: mpw COMMAND [options]\n\n" puts "#{I18n.t('option.usage')}: mpw COMMAND [options]\n\n"
puts 'Commands:' puts 'Commands:'
puts " add #{I18n.t('command.add')}" puts " add #{I18n.t('command.add')}"
puts " config #{I18n.t('command.config')}" puts " config #{I18n.t('command.config')}"
puts " copy #{I18n.t('command.copy')}" puts " copy #{I18n.t('command.copy')}"
puts " delete #{I18n.t('command.delete')}" puts " delete #{I18n.t('command.delete')}"
puts " export #{I18n.t('command.export')}" puts " export #{I18n.t('command.export')}"
puts " genpwd #{I18n.t('command.genpwd')}" puts " genpwd #{I18n.t('command.genpwd')}"
puts " import #{I18n.t('command.import')}" puts " import #{I18n.t('command.import')}"
puts " list #{I18n.t('command.list')}" puts " list #{I18n.t('command.list')}"
puts " update #{I18n.t('command.update')}" puts " update #{I18n.t('command.update')}"
puts " wallet #{I18n.t('command.wallet')}" puts " wallet #{I18n.t('command.wallet')}"
exit 3 exit 3
end end

View file

@ -1,17 +1,17 @@
#!/usr/bin/ruby #!/usr/bin/ruby
# MPW is a software to crypt and manage your passwords # MPW is a software to crypt and manage your passwords
# Copyright (C) 2016 Adrien Waksberg <mpw@yae.im> # Copyright (C) 2016 Adrien Waksberg <mpw@yae.im>
# #
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License # modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2 # as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version. # of the License, or (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details. # GNU General Public License for more details.
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
@ -27,24 +27,24 @@ require 'mpw/cli'
options = {} options = {}
OptionParser.new do |opts| OptionParser.new do |opts|
opts.banner = "#{I18n.t('option.usage')}: mpw add [options]" opts.banner = "#{I18n.t('option.usage')}: mpw add [options]"
opts.on('-c', '--config PATH', I18n.t('option.config')) do |config| opts.on('-c', '--config PATH', I18n.t('option.config')) do |config|
options[:config] = config options[:config] = config
end end
opts.on('-h', '--help', I18n.t('option.help')) do opts.on('-h', '--help', I18n.t('option.help')) do
puts opts puts opts
exit 0 exit 0
end end
opts.on('-r', '--random', I18n.t('option.random_password')) do opts.on('-r', '--random', I18n.t('option.random_password')) do
options[:password] = true options[:password] = true
end end
opts.on('-w', '--wallet NAME', I18n.t('option.wallet')) do |wallet| opts.on('-w', '--wallet NAME', I18n.t('option.wallet')) do |wallet|
options[:wallet] = wallet options[:wallet] = wallet
end end
end.parse! end.parse!
config = MPW::Config.new(options[:config]) config = MPW::Config.new(options[:config])

View file

@ -1,17 +1,17 @@
#!/usr/bin/ruby #!/usr/bin/ruby
# MPW is a software to crypt and manage your passwords # MPW is a software to crypt and manage your passwords
# Copyright (C) 2016 Adrien Waksberg <mpw@yae.im> # Copyright (C) 2016 Adrien Waksberg <mpw@yae.im>
# #
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License # modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2 # as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version. # of the License, or (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details. # GNU General Public License for more details.
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
@ -28,81 +28,81 @@ options = {}
values = {} values = {}
OptionParser.new do |opts| OptionParser.new do |opts|
opts.banner = "#{I18n.t('option.usage')}: mpw config [options]" opts.banner = "#{I18n.t('option.usage')}: mpw config [options]"
opts.on('-c', '--config PATH', I18n.t('option.config')) do |config| opts.on('-c', '--config PATH', I18n.t('option.config')) do |config|
options[:config] = config options[:config] = config
end end
opts.on('-d', '--default-wallet NAME', I18n.t('option.default_wallet')) do |default_wallet| opts.on('-d', '--default-wallet NAME', I18n.t('option.default_wallet')) do |default_wallet|
values[:default_wallet] = default_wallet values[:default_wallet] = default_wallet
end end
opts.on('-g', '--gpg-exe PATH', I18n.t('option.gpg_exe')) do |gpg_exe| opts.on('-g', '--gpg-exe PATH', I18n.t('option.gpg_exe')) do |gpg_exe|
values[:gpg_exe] = gpg_exe values[:gpg_exe] = gpg_exe
end end
opts.on('-h', '--help', I18n.t('option.help')) do opts.on('-h', '--help', I18n.t('option.help')) do
puts opts puts opts
exit 0 exit 0
end end
opts.on('-i', '--init GPG_KEY', I18n.t('option.init')) do |gpg_key| opts.on('-i', '--init GPG_KEY', I18n.t('option.init')) do |gpg_key|
options[:init] = true options[:init] = true
values[:gpg_key] = gpg_key values[:gpg_key] = gpg_key
end end
opts.on('-k', '--key GPG_KEY', I18n.t('option.gpg_key')) do |gpg_key| opts.on('-k', '--key GPG_KEY', I18n.t('option.gpg_key')) do |gpg_key|
values[:gpg_key] = gpg_key values[:gpg_key] = gpg_key
end end
opts.on('-L', '--lang LANG', I18n.t('option.lang')) do |lang| opts.on('-L', '--lang LANG', I18n.t('option.lang')) do |lang|
values[:lang] = lang values[:lang] = lang
end end
opts.on('-w', '--wallet-dir PATH', I18n.t('option.wallet_dir')) do |wallet_dir| opts.on('-w', '--wallet-dir PATH', I18n.t('option.wallet_dir')) do |wallet_dir|
values[:wallet_dir] = wallet_dir values[:wallet_dir] = wallet_dir
end end
opts.on('-l', '--length NUMBER', I18n.t('option.length')) do |length| opts.on('-l', '--length NUMBER', I18n.t('option.length')) do |length|
values[:pwd_length] = length.to_i values[:pwd_length] = length.to_i
end end
opts.on('-n', '--numeric', I18n.t('option.numeric')) do opts.on('-n', '--numeric', I18n.t('option.numeric')) do
values[:pwd_numeric] = true values[:pwd_numeric] = true
end end
opts.on('-N', '--disable-numeric', I18n.t('option.disable_numeric')) do opts.on('-N', '--disable-numeric', I18n.t('option.disable_numeric')) do
values[:pwd_numeric] = false values[:pwd_numeric] = false
end end
opts.on('-s', '--special-chars', I18n.t('option.special_chars')) do opts.on('-s', '--special-chars', I18n.t('option.special_chars')) do
values[:pwd_special] = true values[:pwd_special] = true
end end
opts.on('-S', '--disable_special-chars', I18n.t('option.special_chars')) do opts.on('-S', '--disable_special-chars', I18n.t('option.special_chars')) do
values[:pwd_special] = false values[:pwd_special] = false
end end
opts.on('-a', '--alpha', I18n.t('option.alpha')) do opts.on('-a', '--alpha', I18n.t('option.alpha')) do
values[:pwd_alpha] = true values[:pwd_alpha] = true
end end
opts.on('-A', '--disable-alpha', I18n.t('option.disable_alpha')) do opts.on('-A', '--disable-alpha', I18n.t('option.disable_alpha')) do
values[:pwd_alpha] = false values[:pwd_alpha] = false
end end
end.parse! end.parse!
config = MPW::Config.new(options[:config]) config = MPW::Config.new(options[:config])
cli = MPW::Cli.new(config, nil) cli = MPW::Cli.new(config, nil)
if options.has_key?(:init) if options.has_key?(:init)
cli.setup(values) cli.setup(values)
cli.load_config cli.load_config
cli.get_wallet cli.get_wallet
cli.setup_gpg_key(values[:gpg_key]) cli.setup_gpg_key(values[:gpg_key])
cli.setup_wallet_config cli.setup_wallet_config
else else
cli.load_config cli.load_config
cli.set_config(values) cli.set_config(values)
end end

View file

@ -1,17 +1,17 @@
#!/usr/bin/ruby #!/usr/bin/ruby
# MPW is a software to crypt and manage your passwords # MPW is a software to crypt and manage your passwords
# Copyright (C) 2016 Adrien Waksberg <mpw@yae.im> # Copyright (C) 2016 Adrien Waksberg <mpw@yae.im>
# #
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License # modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2 # as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version. # of the License, or (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details. # GNU General Public License for more details.
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
@ -29,32 +29,32 @@ options[:clipboard] = true
values = {} values = {}
OptionParser.new do |opts| OptionParser.new do |opts|
opts.banner = "#{I18n.t('option.usage')}: mpw copy [options]" opts.banner = "#{I18n.t('option.usage')}: mpw copy [options]"
opts.on('-c', '--config PATH', I18n.t('option.config')) do |config| opts.on('-c', '--config PATH', I18n.t('option.config')) do |config|
options[:config] = config options[:config] = config
end end
opts.on('-d', '--disable-clipboard', I18n.t('option.clipboard')) do opts.on('-d', '--disable-clipboard', I18n.t('option.clipboard')) do
options[:clipboard] = false options[:clipboard] = false
end end
opts.on('-g', '--group NAME', I18n.t('option.group')) do |group| opts.on('-g', '--group NAME', I18n.t('option.group')) do |group|
values[:group] = group values[:group] = group
end end
opts.on('-h', '--help', I18n.t('option.help')) do opts.on('-h', '--help', I18n.t('option.help')) do
puts opts puts opts
exit 0 exit 0
end end
opts.on('-p', '--pattern PATTERN', I18n.t('option.pattern')) do |pattern| opts.on('-p', '--pattern PATTERN', I18n.t('option.pattern')) do |pattern|
values[:pattern] = pattern values[:pattern] = pattern
end end
opts.on('-w', '--wallet NAME', I18n.t('option.wallet')) do |wallet| opts.on('-w', '--wallet NAME', I18n.t('option.wallet')) do |wallet|
options[:wallet] = wallet options[:wallet] = wallet
end end
end.parse! end.parse!
config = MPW::Config.new(options[:config]) config = MPW::Config.new(options[:config])

View file

@ -1,17 +1,17 @@
#!/usr/bin/ruby #!/usr/bin/ruby
# MPW is a software to crypt and manage your passwords # MPW is a software to crypt and manage your passwords
# Copyright (C) 2016 Adrien Waksberg <mpw@yae.im> # Copyright (C) 2016 Adrien Waksberg <mpw@yae.im>
# #
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License # modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2 # as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version. # of the License, or (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details. # GNU General Public License for more details.
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
@ -28,28 +28,28 @@ options = {}
values = {} values = {}
OptionParser.new do |opts| OptionParser.new do |opts|
opts.banner = "#{I18n.t('option.usage')}: mpw delete [options]" opts.banner = "#{I18n.t('option.usage')}: mpw delete [options]"
opts.on('-c', '--config PATH', I18n.t('option.config')) do |config| opts.on('-c', '--config PATH', I18n.t('option.config')) do |config|
options[:config] = config options[:config] = config
end end
opts.on('-g', '--group NAME', I18n.t('option.group')) do |group| opts.on('-g', '--group NAME', I18n.t('option.group')) do |group|
values[:group] = group values[:group] = group
end end
opts.on('-h', '--help', I18n.t('option.help')) do opts.on('-h', '--help', I18n.t('option.help')) do
puts opts puts opts
exit 0 exit 0
end end
opts.on('-p', '--pattern PATTERN', I18n.t('option.pattern')) do |pattern| opts.on('-p', '--pattern PATTERN', I18n.t('option.pattern')) do |pattern|
values[:pattern] = pattern values[:pattern] = pattern
end end
opts.on('-w', '--wallet NAME', I18n.t('option.wallet')) do |wallet| opts.on('-w', '--wallet NAME', I18n.t('option.wallet')) do |wallet|
options[:wallet] = wallet options[:wallet] = wallet
end end
end.parse! end.parse!
config = MPW::Config.new(options[:config]) config = MPW::Config.new(options[:config])

View file

@ -1,17 +1,17 @@
#!/usr/bin/ruby #!/usr/bin/ruby
# MPW is a software to crypt and manage your passwords # MPW is a software to crypt and manage your passwords
# Copyright (C) 2016 Adrien Waksberg <mpw@yae.im> # Copyright (C) 2016 Adrien Waksberg <mpw@yae.im>
# #
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License # modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2 # as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version. # of the License, or (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details. # GNU General Public License for more details.
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
@ -28,32 +28,32 @@ options = {}
values = {} values = {}
OptionParser.new do |opts| OptionParser.new do |opts|
opts.banner = "#{I18n.t('option.usage')}: mpw wallet [options]" opts.banner = "#{I18n.t('option.usage')}: mpw wallet [options]"
opts.on('-c', '--config PATH', I18n.t('option.config')) do |config| opts.on('-c', '--config PATH', I18n.t('option.config')) do |config|
options[:config] = config options[:config] = config
end end
opts.on('-f', '--file PATH', I18n.t('option.file_export')) do |file| opts.on('-f', '--file PATH', I18n.t('option.file_export')) do |file|
options[:file] = file options[:file] = file
end end
opts.on('-g', '--group GROUP', I18n.t('option.group')) do |group| opts.on('-g', '--group GROUP', I18n.t('option.group')) do |group|
values[:group] = group values[:group] = group
end end
opts.on('-h', '--help', I18n.t('option.help')) do opts.on('-h', '--help', I18n.t('option.help')) do
puts opts puts opts
exit 0 exit 0
end end
opts.on('-p', '--pattern PATTERN', I18n.t('option.pattern')) do |pattern| opts.on('-p', '--pattern PATTERN', I18n.t('option.pattern')) do |pattern|
values[:pattern] = pattern values[:pattern] = pattern
end end
opts.on('-w', '--wallet NAME', I18n.t('option.wallet')) do |wallet| opts.on('-w', '--wallet NAME', I18n.t('option.wallet')) do |wallet|
options[:wallet] = wallet options[:wallet] = wallet
end end
end.parse! end.parse!
config = MPW::Config.new(options[:config]) config = MPW::Config.new(options[:config])

View file

@ -1,17 +1,17 @@
#!/usr/bin/ruby #!/usr/bin/ruby
# MPW is a software to crypt and manage your passwords # MPW is a software to crypt and manage your passwords
# Copyright (C) 2016 Adrien Waksberg <mpw@yae.im> # Copyright (C) 2016 Adrien Waksberg <mpw@yae.im>
# #
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License # modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2 # as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version. # of the License, or (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details. # GNU General Public License for more details.
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
@ -22,28 +22,28 @@ require 'mpw/mpw'
options = {} options = {}
OptionParser.new do |opts| OptionParser.new do |opts|
opts.banner = "#{I18n.t('option.usage')}: mpw passwd [options]" opts.banner = "#{I18n.t('option.usage')}: mpw passwd [options]"
opts.on('-h', '--help', I18n.t('option.help')) do opts.on('-h', '--help', I18n.t('option.help')) do
puts opts puts opts
exit 0 exit 0
end end
opts.on('-l', '--length NUMBER', I18n.t('option.length')) do |length| opts.on('-l', '--length NUMBER', I18n.t('option.length')) do |length|
options[:length] = length.to_i options[:length] = length.to_i
end end
opts.on('-n', '--numeric', I18n.t('option.numeric')) do opts.on('-n', '--numeric', I18n.t('option.numeric')) do
options[:numeric] = true options[:numeric] = true
end end
opts.on('-s', '--special-chars', I18n.t('option.special_chars')) do opts.on('-s', '--special-chars', I18n.t('option.special_chars')) do
options[:special] = true options[:special] = true
end end
opts.on('-a', '--alpha', I18n.t('option.alpha')) do opts.on('-a', '--alpha', I18n.t('option.alpha')) do
options[:alpha] = true options[:alpha] = true
end end
end.parse! end.parse!
puts MPW::MPW::password(options) puts MPW::MPW::password(options)

View file

@ -1,17 +1,17 @@
#!/usr/bin/ruby #!/usr/bin/ruby
# MPW is a software to crypt and manage your passwords # MPW is a software to crypt and manage your passwords
# Copyright (C) 2016 Adrien Waksberg <mpw@yae.im> # Copyright (C) 2016 Adrien Waksberg <mpw@yae.im>
# #
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License # modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2 # as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version. # of the License, or (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details. # GNU General Public License for more details.
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
@ -27,24 +27,24 @@ require 'mpw/cli'
options = {} options = {}
OptionParser.new do |opts| OptionParser.new do |opts|
opts.banner = "#{I18n.t('option.usage')}: mpw import [options]" opts.banner = "#{I18n.t('option.usage')}: mpw import [options]"
opts.on('-c', '--config PATH', I18n.t('option.config')) do |config| opts.on('-c', '--config PATH', I18n.t('option.config')) do |config|
options[:config] = config options[:config] = config
end end
opts.on('-f', '--file PATH', I18n.t('option.file_import')) do |file| opts.on('-f', '--file PATH', I18n.t('option.file_import')) do |file|
options[:file] = file options[:file] = file
end end
opts.on('-h', '--help', I18n.t('option.help')) do opts.on('-h', '--help', I18n.t('option.help')) do
puts opts puts opts
exit 0 exit 0
end end
opts.on('-w', '--wallet NAME', I18n.t('option.wallet')) do |wallet| opts.on('-w', '--wallet NAME', I18n.t('option.wallet')) do |wallet|
options[:wallet] = wallet options[:wallet] = wallet
end end
end.parse! end.parse!
config = MPW::Config.new(options[:config]) config = MPW::Config.new(options[:config])

View file

@ -1,17 +1,17 @@
#!/usr/bin/ruby #!/usr/bin/ruby
# MPW is a software to crypt and manage your passwords # MPW is a software to crypt and manage your passwords
# Copyright (C) 2016 Adrien Waksberg <mpw@yae.im> # Copyright (C) 2016 Adrien Waksberg <mpw@yae.im>
# #
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License # modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2 # as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version. # of the License, or (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details. # GNU General Public License for more details.
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
@ -28,28 +28,28 @@ options = {}
values = {} values = {}
OptionParser.new do |opts| OptionParser.new do |opts|
opts.banner = "#{I18n.t('option.usage')}: mpw list [options]" opts.banner = "#{I18n.t('option.usage')}: mpw list [options]"
opts.on('-c', '--config PATH', I18n.t('option.config')) do |config| opts.on('-c', '--config PATH', I18n.t('option.config')) do |config|
options[:config] = config options[:config] = config
end end
opts.on('-g', '--group NAME', I18n.t('option.group')) do |group| opts.on('-g', '--group NAME', I18n.t('option.group')) do |group|
values[:group] = group values[:group] = group
end end
opts.on('-h', '--help', I18n.t('option.help')) do opts.on('-h', '--help', I18n.t('option.help')) do
puts opts puts opts
exit 0 exit 0
end end
opts.on('-p', '--pattern PATTERN', I18n.t('option.pattern')) do |pattern| opts.on('-p', '--pattern PATTERN', I18n.t('option.pattern')) do |pattern|
values[:pattern] = pattern values[:pattern] = pattern
end end
opts.on('-w', '--wallet NAME', I18n.t('option.wallet')) do |wallet| opts.on('-w', '--wallet NAME', I18n.t('option.wallet')) do |wallet|
options[:wallet] = wallet options[:wallet] = wallet
end end
end.parse! end.parse!
config = MPW::Config.new(options[:config]) config = MPW::Config.new(options[:config])

View file

@ -1,17 +1,17 @@
#!/usr/bin/ruby #!/usr/bin/ruby
# MPW is a software to crypt and manage your passwords # MPW is a software to crypt and manage your passwords
# Copyright (C) 2016 Adrien Waksberg <mpw@yae.im> # Copyright (C) 2016 Adrien Waksberg <mpw@yae.im>
# #
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License # modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2 # as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version. # of the License, or (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details. # GNU General Public License for more details.
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
@ -28,32 +28,32 @@ options = {}
values = {} values = {}
OptionParser.new do |opts| OptionParser.new do |opts|
opts.banner = "#{I18n.t('option.usage')}: mpw update [options]" opts.banner = "#{I18n.t('option.usage')}: mpw update [options]"
opts.on('-c', '--config PATH', I18n.t('option.config')) do |config| opts.on('-c', '--config PATH', I18n.t('option.config')) do |config|
options[:config] = config options[:config] = config
end end
opts.on('-g', '--group NAME', I18n.t('option.group')) do |group| opts.on('-g', '--group NAME', I18n.t('option.group')) do |group|
values[:group] = group values[:group] = group
end end
opts.on('-h', '--help', I18n.t('option.help')) do opts.on('-h', '--help', I18n.t('option.help')) do
puts opts puts opts
exit 0 exit 0
end end
opts.on('-p', '--pattern PATTERN', I18n.t('option.pattern')) do |pattern| opts.on('-p', '--pattern PATTERN', I18n.t('option.pattern')) do |pattern|
values[:pattern] = pattern values[:pattern] = pattern
end end
opts.on('-r', '--random', I18n.t('option.random_password')) do opts.on('-r', '--random', I18n.t('option.random_password')) do
options[:password] = true options[:password] = true
end end
opts.on('-w', '--wallet NAME', I18n.t('option.wallet')) do |wallet| opts.on('-w', '--wallet NAME', I18n.t('option.wallet')) do |wallet|
options[:wallet] = wallet options[:wallet] = wallet
end end
end.parse! end.parse!
config = MPW::Config.new(options[:config]) config = MPW::Config.new(options[:config])

View file

@ -1,17 +1,17 @@
#!/usr/bin/ruby #!/usr/bin/ruby
# MPW is a software to crypt and manage your passwords # MPW is a software to crypt and manage your passwords
# Copyright (C) 2016 Adrien Waksberg <mpw@yae.im> # Copyright (C) 2016 Adrien Waksberg <mpw@yae.im>
# #
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License # modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2 # as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version. # of the License, or (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details. # GNU General Public License for more details.
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
@ -28,37 +28,37 @@ options = {}
options[:delete] = false options[:delete] = false
OptionParser.new do |opts| OptionParser.new do |opts|
opts.banner = "#{I18n.t('option.usage')}: mpw wallet [options]" opts.banner = "#{I18n.t('option.usage')}: mpw wallet [options]"
opts.on('-a', '--add-gpg-key NAME', I18n.t('option.add_gpg_key')) do |gpg_key| opts.on('-a', '--add-gpg-key NAME', I18n.t('option.add_gpg_key')) do |gpg_key|
options[:gpg_key] = gpg_key options[:gpg_key] = gpg_key
end end
opts.on('-c', '--config PATH', I18n.t('option.config')) do |config| opts.on('-c', '--config PATH', I18n.t('option.config')) do |config|
options[:config] = config options[:config] = config
end end
opts.on('-d', '--delete-gpg-key NAME', I18n.t('option.delete_gpg_key')) do |gpg_key| opts.on('-d', '--delete-gpg-key NAME', I18n.t('option.delete_gpg_key')) do |gpg_key|
options[:gpg_key] = gpg_key options[:gpg_key] = gpg_key
options[:delete] = true options[:delete] = true
end end
opts.on('-h', '--help', I18n.t('option.help')) do opts.on('-h', '--help', I18n.t('option.help')) do
puts opts puts opts
exit 0 exit 0
end end
opts.on('-l', '--list', I18n.t('option.list')) do opts.on('-l', '--list', I18n.t('option.list')) do
options[:list] = true options[:list] = true
end end
opts.on('-L', '--list-keys', I18n.t('option.list_keys')) do opts.on('-L', '--list-keys', I18n.t('option.list_keys')) do
options[:list_keys] = true options[:list_keys] = true
end end
opts.on('-w', '--wallet NAME', I18n.t('option.wallet')) do |wallet| opts.on('-w', '--wallet NAME', I18n.t('option.wallet')) do |wallet|
options[:wallet] = wallet options[:wallet] = wallet
end end
end.parse! end.parse!
config = MPW::Config.new(options[:config]) config = MPW::Config.new(options[:config])
@ -67,18 +67,18 @@ cli = MPW::Cli.new(config)
cli.load_config cli.load_config
if not options[:list].nil? if not options[:list].nil?
cli.list_wallet cli.list_wallet
else else
cli.get_wallet(options[:wallet]) cli.get_wallet(options[:wallet])
cli.decrypt cli.decrypt
if not options[:list_keys].nil? if not options[:list_keys].nil?
cli.list_keys cli.list_keys
elsif not options[:gpg_key].nil? elsif not options[:gpg_key].nil?
if options[:delete] if options[:delete]
cli.delete_key(options[:gpg_key]) cli.delete_key(options[:gpg_key])
else else
cli.add_key(options[:gpg_key]) cli.add_key(options[:gpg_key])
end end
end end
end end

File diff suppressed because it is too large Load diff

View file

@ -1,17 +1,17 @@
#!/usr/bin/ruby #!/usr/bin/ruby
# MPW is a software to crypt and manage your passwords # MPW is a software to crypt and manage your passwords
# Copyright (C) 2016 Adrien Waksberg <mpw@yae.im> # Copyright (C) 2016 Adrien Waksberg <mpw@yae.im>
# #
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License # modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2 # as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version. # of the License, or (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details. # GNU General Public License for more details.
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
@ -20,140 +20,140 @@ require 'gpgme'
require 'yaml' require 'yaml'
require 'i18n' require 'i18n'
require 'fileutils' require 'fileutils'
module MPW module MPW
class Config class Config
attr_accessor :error_msg
attr_accessor :gpg_key attr_accessor :error_msg
attr_accessor :lang
attr_accessor :config_dir
attr_accessor :default_wallet
attr_accessor :wallet_dir
attr_accessor :gpg_exe
attr_accessor :password
# Constructor attr_accessor :gpg_key
# @args: config_file -> the specify config file attr_accessor :lang
def initialize(config_file=nil) attr_accessor :config_dir
@config_file = config_file attr_accessor :default_wallet
attr_accessor :wallet_dir
attr_accessor :gpg_exe
attr_accessor :password
if /darwin/ =~ RUBY_PLATFORM # Constructor
@config_dir = "#{Dir.home}/Library/Preferences/mpw" # @args: config_file -> the specify config file
elsif /cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM def initialize(config_file=nil)
@config_dir = "#{Dir.home}/AppData/Local/mpw" @config_file = config_file
else
@config_dir = "#{Dir.home}/.config/mpw"
end
@config_file = "#{@config_dir}/mpw.cfg" if @config_file.nil? or @config_file.empty?
end
# Create a new config file if /darwin/ =~ RUBY_PLATFORM
# @args: options -> hash with values @config_dir = "#{Dir.home}/Library/Preferences/mpw"
# @rtrn: true if le config file is create elsif /cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM
def setup(options) @config_dir = "#{Dir.home}/AppData/Local/mpw"
gpg_key = options[:gpg_key] || @gpg_key else
lang = options[:lang] || @lang @config_dir = "#{Dir.home}/.config/mpw"
wallet_dir = options[:wallet_dir] || @wallet_dir end
default_wallet = options[:default_wallet] || @default_wallet
gpg_exe = options[:gpg_exe] || @gpg_exe
password = { numeric: true,
alpha: true,
special: false,
length: 16,
}
['numeric', 'special', 'alpha', 'length'].each do |k| @config_file = "#{@config_dir}/mpw.cfg" if @config_file.nil? or @config_file.empty?
if options.has_key?("pwd_#{k}".to_sym) end
password[k.to_sym] = options["pwd_#{k}".to_sym]
elsif not @password.nil? and @password.has_key?(k.to_sym)
password[k.to_sym] = @password[k.to_sym]
end
end
if not gpg_key =~ /[a-zA-Z0-9.-_]+\@[a-zA-Z0-9]+\.[a-zA-Z]+/ # Create a new config file
raise I18n.t('error.config.key_bad_format') # @args: options -> hash with values
end # @rtrn: true if le config file is create
def setup(options)
gpg_key = options[:gpg_key] || @gpg_key
lang = options[:lang] || @lang
wallet_dir = options[:wallet_dir] || @wallet_dir
default_wallet = options[:default_wallet] || @default_wallet
gpg_exe = options[:gpg_exe] || @gpg_exe
password = { numeric: true,
alpha: true,
special: false,
length: 16,
}
wallet_dir = "#{@config_dir}/wallets" if wallet_dir.to_s.empty? ['numeric', 'special', 'alpha', 'length'].each do |k|
config = { 'gpg_key' => gpg_key, if options.has_key?("pwd_#{k}".to_sym)
'lang' => lang, password[k.to_sym] = options["pwd_#{k}".to_sym]
'wallet_dir' => wallet_dir, elsif not @password.nil? and @password.has_key?(k.to_sym)
'default_wallet' => default_wallet, password[k.to_sym] = @password[k.to_sym]
'gpg_exe' => gpg_exe, end
'password' => password, end
}
FileUtils.mkdir_p(@config_dir, mode: 0700) if not gpg_key =~ /[a-zA-Z0-9.-_]+\@[a-zA-Z0-9]+\.[a-zA-Z]+/
FileUtils.mkdir_p(wallet_dir, mode: 0700) raise I18n.t('error.config.key_bad_format')
end
File.open(@config_file, 'w') do |file| wallet_dir = "#{@config_dir}/wallets" if wallet_dir.to_s.empty?
file << config.to_yaml config = { 'gpg_key' => gpg_key,
end 'lang' => lang,
rescue Exception => e 'wallet_dir' => wallet_dir,
raise "#{I18n.t('error.config.write')}\n#{e}" 'default_wallet' => default_wallet,
end 'gpg_exe' => gpg_exe,
'password' => password,
}
# Setup a new gpg key FileUtils.mkdir_p(@config_dir, mode: 0700)
# @args: password -> the GPG key password FileUtils.mkdir_p(wallet_dir, mode: 0700)
# name -> the name of user
# length -> length of the GPG key
# expire -> the time of expire to GPG key
# @rtrn: true if the GPG key is create, else false
def setup_gpg_key(password, name, length = 4096, expire = 0)
if name.to_s.empty?
raise "#{I18n.t('error.config.genkey_gpg.name')}"
elsif password.to_s.empty?
raise "#{I18n.t('error.config.genkey_gpg.password')}"
end
param = '' File.open(@config_file, 'w') do |file|
param << '<GnupgKeyParms format="internal">' + "\n" file << config.to_yaml
param << "Key-Type: RSA\n" end
param << "Key-Length: #{length}\n" rescue Exception => e
param << "Subkey-Type: ELG-E\n" raise "#{I18n.t('error.config.write')}\n#{e}"
param << "Subkey-Length: #{length}\n" end
param << "Name-Real: #{name}\n"
param << "Name-Comment: #{name}\n"
param << "Name-Email: #{@gpg_key}\n"
param << "Expire-Date: #{expire}\n"
param << "Passphrase: #{password}\n"
param << "</GnupgKeyParms>\n"
ctx = GPGME::Ctx.new # Setup a new gpg key
ctx.genkey(param, nil, nil) # @args: password -> the GPG key password
rescue Exception => e # name -> the name of user
raise "#{I18n.t('error.config.genkey_gpg.exception')}\n#{e}" # length -> length of the GPG key
end # expire -> the time of expire to GPG key
# @rtrn: true if the GPG key is create, else false
def setup_gpg_key(password, name, length = 4096, expire = 0)
if name.to_s.empty?
raise "#{I18n.t('error.config.genkey_gpg.name')}"
elsif password.to_s.empty?
raise "#{I18n.t('error.config.genkey_gpg.password')}"
end
# Load the config file param = ''
def load_config param << '<GnupgKeyParms format="internal">' + "\n"
config = YAML::load_file(@config_file) param << "Key-Type: RSA\n"
@gpg_key = config['gpg_key'] param << "Key-Length: #{length}\n"
@lang = config['lang'] param << "Subkey-Type: ELG-E\n"
@wallet_dir = config['wallet_dir'] param << "Subkey-Length: #{length}\n"
@default_wallet = config['default_wallet'] param << "Name-Real: #{name}\n"
@gpg_exe = config['gpg_exe'] param << "Name-Comment: #{name}\n"
@password = config['password'] || {} param << "Name-Email: #{@gpg_key}\n"
param << "Expire-Date: #{expire}\n"
param << "Passphrase: #{password}\n"
param << "</GnupgKeyParms>\n"
raise if @gpg_key.empty? or @wallet_dir.empty? ctx = GPGME::Ctx.new
ctx.genkey(param, nil, nil)
I18n.locale = @lang.to_sym rescue Exception => e
rescue Exception => e raise "#{I18n.t('error.config.genkey_gpg.exception')}\n#{e}"
raise "#{I18n.t('error.config.load')}\n#{e}" end
end
# Check if private key exist # Load the config file
# @rtrn: true if the key exist, else false def load_config
def check_gpg_key? config = YAML::load_file(@config_file)
ctx = GPGME::Ctx.new @gpg_key = config['gpg_key']
ctx.each_key(@gpg_key, true) do @lang = config['lang']
return true @wallet_dir = config['wallet_dir']
end @default_wallet = config['default_wallet']
@gpg_exe = config['gpg_exe']
@password = config['password'] || {}
return false raise if @gpg_key.empty? or @wallet_dir.empty?
end
I18n.locale = @lang.to_sym
rescue Exception => e
raise "#{I18n.t('error.config.load')}\n#{e}"
end
# Check if private key exist
# @rtrn: true if the key exist, else false
def check_gpg_key?
ctx = GPGME::Ctx.new
ctx.each_key(@gpg_key, true) do
return true
end
return false
end
end end
end end

View file

@ -1,102 +1,102 @@
#!/usr/bin/ruby #!/usr/bin/ruby
# MPW is a software to crypt and manage your passwords # MPW is a software to crypt and manage your passwords
# Copyright (C) 2016 Adrien Waksberg <mpw@yae.im> # Copyright (C) 2016 Adrien Waksberg <mpw@yae.im>
# #
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License # modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2 # as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version. # of the License, or (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details. # GNU General Public License for more details.
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require 'i18n' require 'i18n'
module MPW module MPW
class Item class Item
attr_accessor :id attr_accessor :id
attr_accessor :group attr_accessor :group
attr_accessor :host attr_accessor :host
attr_accessor :protocol attr_accessor :protocol
attr_accessor :user attr_accessor :user
attr_accessor :port attr_accessor :port
attr_accessor :otp attr_accessor :otp
attr_accessor :comment attr_accessor :comment
attr_accessor :last_edit attr_accessor :last_edit
attr_accessor :created attr_accessor :created
# Constructor # Constructor
# Create a new item # Create a new item
# @args: options -> a hash of parameter # @args: options -> a hash of parameter
# raise an error if the hash hasn't the key name # raise an error if the hash hasn't the key name
def initialize(options={}) def initialize(options={})
if not options.has_key?(:host) or options[:host].to_s.empty? if not options.has_key?(:host) or options[:host].to_s.empty?
raise I18n.t('error.update.host_empty') raise I18n.t('error.update.host_empty')
end end
if not options.has_key?(:id) or options[:id].to_s.empty? or not options.has_key?(:created) or options[:created].to_s.empty? if not options.has_key?(:id) or options[:id].to_s.empty? or not options.has_key?(:created) or options[:created].to_s.empty?
@id = generate_id @id = generate_id
@created = Time.now.to_i @created = Time.now.to_i
else else
@id = options[:id] @id = options[:id]
@created = options[:created] @created = options[:created]
@last_edit = options[:last_edit] @last_edit = options[:last_edit]
options[:no_update_last_edit] = true options[:no_update_last_edit] = true
end end
update(options) update(options)
end end
# Update the item # Update the item
# @args: options -> a hash of parameter # @args: options -> a hash of parameter
def update(options={}) def update(options={})
if options.has_key?(:host) and options[:host].to_s.empty? if options.has_key?(:host) and options[:host].to_s.empty?
raise I18n.t('error.update.host_empty') raise I18n.t('error.update.host_empty')
end end
@group = options[:group] if options.has_key?(:group) @group = options[:group] if options.has_key?(:group)
@host = options[:host] if options.has_key?(:host) @host = options[:host] if options.has_key?(:host)
@protocol = options[:protocol] if options.has_key?(:protocol) @protocol = options[:protocol] if options.has_key?(:protocol)
@user = options[:user] if options.has_key?(:user) @user = options[:user] if options.has_key?(:user)
@port = options[:port].to_i if options.has_key?(:port) and not options[:port].to_s.empty? @port = options[:port].to_i if options.has_key?(:port) and not options[:port].to_s.empty?
@otp = options[:otp] if options.has_key?(:otp) @otp = options[:otp] if options.has_key?(:otp)
@comment = options[:comment] if options.has_key?(:comment) @comment = options[:comment] if options.has_key?(:comment)
@last_edit = Time.now.to_i if not options.has_key?(:no_update_last_edit) @last_edit = Time.now.to_i if not options.has_key?(:no_update_last_edit)
end end
# Delete all data # Delete all data
def delete def delete
@id = nil @id = nil
@group = nil @group = nil
@host = nil @host = nil
@protocol = nil @protocol = nil
@user = nil @user = nil
@port = nil @port = nil
@otp = nil @otp = nil
@comment = nil @comment = nil
@created = nil @created = nil
@last_edit = nil @last_edit = nil
end end
def empty? def empty?
return @id.to_s.empty? return @id.to_s.empty?
end end
def nil? def nil?
return false return false
end end
# Generate an random id # Generate an random id
private private
def generate_id def generate_id
return ([*('A'..'Z'),*('a'..'z'),*('0'..'9')]).sample(16).join return ([*('A'..'Z'),*('a'..'z'),*('0'..'9')]).sample(16).join
end end
end end
end end

View file

@ -1,17 +1,17 @@
#!/usr/bin/ruby #!/usr/bin/ruby
# MPW is a software to crypt and manage your passwords # MPW is a software to crypt and manage your passwords
# Copyright (C) 2016 Adrien Waksberg <mpw@yae.im> # Copyright (C) 2016 Adrien Waksberg <mpw@yae.im>
# #
# This program is free software; you can redistribute it and/or # This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License # modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2 # as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version. # of the License, or (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details. # GNU General Public License for more details.
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
@ -22,360 +22,360 @@ require 'i18n'
require 'yaml' require 'yaml'
require 'rotp' require 'rotp'
require 'mpw/item' require 'mpw/item'
module MPW module MPW
class MPW class MPW
# Constructor # Constructor
def initialize(key, wallet_file, gpg_pass=nil, gpg_exe=nil) def initialize(key, wallet_file, gpg_pass=nil, gpg_exe=nil)
@key = key @key = key
@gpg_pass = gpg_pass @gpg_pass = gpg_pass
@gpg_exe = gpg_exe @gpg_exe = gpg_exe
@wallet_file = wallet_file @wallet_file = wallet_file
if not @gpg_exe.to_s.empty? if not @gpg_exe.to_s.empty?
GPGME::Engine.set_info(GPGME::PROTOCOL_OpenPGP, @gpg_exe, "#{Dir.home}/.gnupg") GPGME::Engine.set_info(GPGME::PROTOCOL_OpenPGP, @gpg_exe, "#{Dir.home}/.gnupg")
end end
end end
# Read mpw file # Read mpw file
def read_data def read_data
@config = {} @config = {}
@data = [] @data = []
@keys = {} @keys = {}
@passwords = {} @passwords = {}
@otp_keys = {} @otp_keys = {}
data = nil data = nil
return if not File.exists?(@wallet_file) return if not File.exists?(@wallet_file)
Gem::Package::TarReader.new(File.open(@wallet_file)) do |tar| Gem::Package::TarReader.new(File.open(@wallet_file)) do |tar|
tar.each do |f| tar.each do |f|
case f.full_name case f.full_name
when 'wallet/config.gpg' when 'wallet/config.gpg'
@config = YAML.load(decrypt(f.read)) @config = YAML.load(decrypt(f.read))
when 'wallet/meta.gpg' when 'wallet/meta.gpg'
data = decrypt(f.read) data = decrypt(f.read)
when /^wallet\/keys\/(?<key>.+)\.pub$/ when /^wallet\/keys\/(?<key>.+)\.pub$/
key = Regexp.last_match('key') key = Regexp.last_match('key')
if GPGME::Key.find(:public, key).length == 0 if GPGME::Key.find(:public, key).length == 0
GPGME::Key.import(f.read, armor: true) GPGME::Key.import(f.read, armor: true)
end end
@keys[key] = f.read @keys[key] = f.read
when /^wallet\/passwords\/(?<id>[a-zA-Z0-9]+)\.gpg$/ when /^wallet\/passwords\/(?<id>[a-zA-Z0-9]+)\.gpg$/
@passwords[Regexp.last_match('id')] = f.read @passwords[Regexp.last_match('id')] = f.read
when /^wallet\/otp_keys\/(?<id>[a-zA-Z0-9]+)\.gpg$/ when /^wallet\/otp_keys\/(?<id>[a-zA-Z0-9]+)\.gpg$/
@otp_keys[Regexp.last_match('id')] = f.read @otp_keys[Regexp.last_match('id')] = f.read
else else
next next
end end
end end
end end
if not data.nil? and not data.empty? if not data.nil? and not data.empty?
YAML.load(data).each_value do |d| YAML.load(data).each_value do |d|
@data.push(Item.new(id: d['id'], @data.push(Item.new(id: d['id'],
group: d['group'], group: d['group'],
host: d['host'], host: d['host'],
protocol: d['protocol'], protocol: d['protocol'],
user: d['user'], user: d['user'],
port: d['port'], port: d['port'],
otp: @otp_keys.has_key?(d['id']), otp: @otp_keys.has_key?(d['id']),
comment: d['comment'], comment: d['comment'],
last_edit: d['last_edit'], last_edit: d['last_edit'],
created: d['created'], created: d['created'],
) )
) )
end end
end end
add_key(@key) if @keys[@key].nil? add_key(@key) if @keys[@key].nil?
rescue Exception => e rescue Exception => e
raise "#{I18n.t('error.mpw_file.read_data')}\n#{e}" raise "#{I18n.t('error.mpw_file.read_data')}\n#{e}"
end end
# Encrypt a file # Encrypt a file
def write_data def write_data
data = {} data = {}
tmp_file = "#{@wallet_file}.tmp" tmp_file = "#{@wallet_file}.tmp"
@data.each do |item| @data.each do |item|
next if item.empty? next if item.empty?
data.merge!(item.id => { 'id' => item.id, data.merge!(item.id => { 'id' => item.id,
'group' => item.group, 'group' => item.group,
'host' => item.host, 'host' => item.host,
'protocol' => item.protocol, 'protocol' => item.protocol,
'user' => item.user, 'user' => item.user,
'port' => item.port, 'port' => item.port,
'comment' => item.comment, 'comment' => item.comment,
'last_edit' => item.last_edit, 'last_edit' => item.last_edit,
'created' => item.created, 'created' => item.created,
} }
) )
end end
@config['last_update'] = Time.now.to_i @config['last_update'] = Time.now.to_i
Gem::Package::TarWriter.new(File.open(tmp_file, 'w+')) do |tar| Gem::Package::TarWriter.new(File.open(tmp_file, 'w+')) do |tar|
data_encrypt = encrypt(data.to_yaml) data_encrypt = encrypt(data.to_yaml)
tar.add_file_simple('wallet/meta.gpg', 0400, data_encrypt.length) do |io| tar.add_file_simple('wallet/meta.gpg', 0400, data_encrypt.length) do |io|
io.write(data_encrypt) io.write(data_encrypt)
end end
config = encrypt(@config.to_yaml) config = encrypt(@config.to_yaml)
tar.add_file_simple('wallet/config.gpg', 0400, config.length) do |io| tar.add_file_simple('wallet/config.gpg', 0400, config.length) do |io|
io.write(config) io.write(config)
end end
@passwords.each do |id, password| @passwords.each do |id, password|
tar.add_file_simple("wallet/passwords/#{id}.gpg", 0400, password.length) do |io| tar.add_file_simple("wallet/passwords/#{id}.gpg", 0400, password.length) do |io|
io.write(password) io.write(password)
end end
end end
@otp_keys.each do |id, key| @otp_keys.each do |id, key|
tar.add_file_simple("wallet/otp_keys/#{id}.gpg", 0400, key.length) do |io| tar.add_file_simple("wallet/otp_keys/#{id}.gpg", 0400, key.length) do |io|
io.write(key) io.write(key)
end end
end end
@keys.each do |id, key| @keys.each do |id, key|
tar.add_file_simple("wallet/keys/#{id}.pub", 0400, key.length) do |io| tar.add_file_simple("wallet/keys/#{id}.pub", 0400, key.length) do |io|
io.write(key) io.write(key)
end end
end end
end end
File.rename(tmp_file, @wallet_file) File.rename(tmp_file, @wallet_file)
rescue Exception => e rescue Exception => e
File.unlink(tmp_file) if File.exist?(tmp_file) File.unlink(tmp_file) if File.exist?(tmp_file)
raise "#{I18n.t('error.mpw_file.write_data')}\n#{e}" raise "#{I18n.t('error.mpw_file.write_data')}\n#{e}"
end end
# Get a password # Get a password
# args: id -> the item id # args: id -> the item id
def get_password(id) def get_password(id)
password = decrypt(@passwords[id]) password = decrypt(@passwords[id])
if /^\$[a-zA-Z0-9]{4,9}::(?<password>.+)$/ =~ password
return Regexp.last_match('password')
else
return password
end
end
# Set a password if /^\$[a-zA-Z0-9]{4,9}::(?<password>.+)$/ =~ password
# args: id -> the item id return Regexp.last_match('password')
# password -> the new password else
def set_password(id, password) return password
salt = MPW::password(length: Random.rand(4..9)) end
password = "$#{salt}::#{password}" end
@passwords[id] = encrypt(password) # Set a password
end # args: id -> the item id
# password -> the new password
def set_password(id, password)
salt = MPW::password(length: Random.rand(4..9))
password = "$#{salt}::#{password}"
# Return the list of all gpg keys @passwords[id] = encrypt(password)
# rtrn: an array with the gpg keys name end
def list_keys
return @keys.keys
end
# Add a public key # Return the list of all gpg keys
# args: key -> new public key file or name # rtrn: an array with the gpg keys name
def add_key(key) def list_keys
if File.exists?(key) return @keys.keys
data = File.open(key).read end
key_import = GPGME::Key.import(data, armor: true)
key = GPGME::Key.get(key_import.imports[0].fpr).uids[0].email
else
data = GPGME::Key.export(key, armor: true).read
end
if data.to_s.empty? # Add a public key
raise I18n.t('error.export_key') # args: key -> new public key file or name
end def add_key(key)
if File.exists?(key)
data = File.open(key).read
key_import = GPGME::Key.import(data, armor: true)
key = GPGME::Key.get(key_import.imports[0].fpr).uids[0].email
else
data = GPGME::Key.export(key, armor: true).read
end
@keys[key] = data if data.to_s.empty?
@passwords.each_key { |id| set_password(id, get_password(id)) } raise I18n.t('error.export_key')
@otp_keys.each_key { |id| set_otp_key(id, get_otp_key(id)) } end
end
# Delete a public key @keys[key] = data
# args: key -> public key to delete @passwords.each_key { |id| set_password(id, get_password(id)) }
def delete_key(key) @otp_keys.each_key { |id| set_otp_key(id, get_otp_key(id)) }
@keys.delete(key) end
@passwords.each_key { |id| set_password(id, get_password(id)) }
@otp_keys.each_key { |id| set_otp_key(id, get_otp_key(id)) }
end
# Set config # Delete a public key
# args: config -> a hash with config options # args: key -> public key to delete
def set_config(options={}) def delete_key(key)
@config = {} if @config.nil? @keys.delete(key)
@passwords.each_key { |id| set_password(id, get_password(id)) }
@otp_keys.each_key { |id| set_otp_key(id, get_otp_key(id)) }
end
@config['protocol'] = options[:protocol] if options.has_key?(:protocol) # Set config
@config['host'] = options[:host] if options.has_key?(:host) # args: config -> a hash with config options
@config['port'] = options[:port] if options.has_key?(:port) def set_config(options={})
@config['user'] = options[:user] if options.has_key?(:user) @config = {} if @config.nil?
@config['password'] = options[:password] if options.has_key?(:password)
@config['path'] = options[:path] if options.has_key?(:path)
end
# Add a new item @config['protocol'] = options[:protocol] if options.has_key?(:protocol)
# @args: item -> Object MPW::Item @config['host'] = options[:host] if options.has_key?(:host)
def add(item) @config['port'] = options[:port] if options.has_key?(:port)
if not item.instance_of?(Item) @config['user'] = options[:user] if options.has_key?(:user)
raise I18n.t('error.bad_class') @config['password'] = options[:password] if options.has_key?(:password)
elsif item.empty? @config['path'] = options[:path] if options.has_key?(:path)
raise I18n.t('error.empty') end
else
@data.push(item)
end
end
# Search in some csv data # Add a new item
# @args: options -> a hash with paramaters # @args: item -> Object MPW::Item
# @rtrn: a list with the resultat of the search def add(item)
def list(options={}) if not item.instance_of?(Item)
result = [] raise I18n.t('error.bad_class')
elsif item.empty?
raise I18n.t('error.empty')
else
@data.push(item)
end
end
search = options[:pattern].to_s.downcase # Search in some csv data
group = options[:group].to_s.downcase # @args: options -> a hash with paramaters
# @rtrn: a list with the resultat of the search
def list(options={})
result = []
@data.each do |item| search = options[:pattern].to_s.downcase
next if item.empty? group = options[:group].to_s.downcase
next if not group.empty? and not group.eql?(item.group.to_s.downcase)
host = item.host.to_s.downcase
comment = item.comment.to_s.downcase
if not host =~ /^.*#{search}.*$/ and not comment =~ /^.*#{search}.*$/ @data.each do |item|
next next if item.empty?
end next if not group.empty? and not group.eql?(item.group.to_s.downcase)
result.push(item) host = item.host.to_s.downcase
end comment = item.comment.to_s.downcase
return result if not host =~ /^.*#{search}.*$/ and not comment =~ /^.*#{search}.*$/
end next
end
# Search in some csv data result.push(item)
# @args: id -> the id item end
# @rtrn: a row with the result of the search
def search_by_id(id)
@data.each do |item|
return item if item.id == id
end
return nil return result
end end
# Set an opt key # Search in some csv data
# args: id -> the item id # @args: id -> the id item
# key -> the new key # @rtrn: a row with the result of the search
def set_otp_key(id, key) def search_by_id(id)
if not key.to_s.empty? @data.each do |item|
@otp_keys[id] = encrypt(key.to_s) return item if item.id == id
end end
end
# Get an opt key return nil
# args: id -> the item id end
# key -> the new key
def get_otp_key(id) # Set an opt key
if @otp_keys.has_key?(id) # args: id -> the item id
return decrypt(@otp_keys[id]) # key -> the new key
else def set_otp_key(id, key)
return nil if not key.to_s.empty?
end @otp_keys[id] = encrypt(key.to_s)
end end
end
# Get an opt key
# args: id -> the item id
# key -> the new key
def get_otp_key(id)
if @otp_keys.has_key?(id)
return decrypt(@otp_keys[id])
else
return nil
end
end
# Get an otp code # Get an otp code
# @args: id -> the item id # @args: id -> the item id
# @rtrn: an otp code # @rtrn: an otp code
def get_otp_code(id) def get_otp_code(id)
if not @otp_keys.has_key?(id) if not @otp_keys.has_key?(id)
return 0 return 0
else else
return ROTP::TOTP.new(decrypt(@otp_keys[id])).now return ROTP::TOTP.new(decrypt(@otp_keys[id])).now
end end
end end
# Get remaining time before expire otp code # Get remaining time before expire otp code
# @rtrn: return time in seconde # @rtrn: return time in seconde
def get_otp_remaining_time def get_otp_remaining_time
return (Time.now.utc.to_i / 30 + 1) * 30 - Time.now.utc.to_i return (Time.now.utc.to_i / 30 + 1) * 30 - Time.now.utc.to_i
end end
# Generate a random password # Generate a random password
# @args: options -> :length, :special, :alpha, :numeric # @args: options -> :length, :special, :alpha, :numeric
# @rtrn: a random string # @rtrn: a random string
def self.password(options={}) def self.password(options={})
if not options.include?(:length) or options[:length].to_i <= 0 if not options.include?(:length) or options[:length].to_i <= 0
length = 8 length = 8
elsif options[:length].to_i >= 32768 elsif options[:length].to_i >= 32768
length = 32768 length = 32768
else else
length = options[:length].to_i length = options[:length].to_i
end end
chars = [] chars = []
chars += [*('!'..'?')] - [*('0'..'9')] if options[:special] chars += [*('!'..'?')] - [*('0'..'9')] if options[:special]
chars += [*('A'..'Z'),*('a'..'z')] if options[:alpha] chars += [*('A'..'Z'),*('a'..'z')] if options[:alpha]
chars += [*('0'..'9')] if options[:numeric] chars += [*('0'..'9')] if options[:numeric]
chars = [*('A'..'Z'),*('a'..'z'),*('0'..'9')] if chars.empty? chars = [*('A'..'Z'),*('a'..'z'),*('0'..'9')] if chars.empty?
result = '' result = ''
while length > 62 do while length > 62 do
result << chars.sample(62).join result << chars.sample(62).join
length -= 62 length -= 62
end end
result << chars.sample(length).join result << chars.sample(length).join
return result return result
end end
# Decrypt a gpg file # Decrypt a gpg file
# @args: data -> string to decrypt # @args: data -> string to decrypt
private private
def decrypt(data) def decrypt(data)
return nil if data.to_s.empty? return nil if data.to_s.empty?
crypto = GPGME::Crypto.new(armor: true) crypto = GPGME::Crypto.new(armor: true)
return crypto.decrypt(data, password: @gpg_pass).read.force_encoding('utf-8')
rescue Exception => e
raise "#{I18n.t('error.gpg_file.decrypt')}\n#{e}"
end
# Encrypt a file return crypto.decrypt(data, password: @gpg_pass).read.force_encoding('utf-8')
# args: data -> string to encrypt rescue Exception => e
private raise "#{I18n.t('error.gpg_file.decrypt')}\n#{e}"
def encrypt(data) end
recipients = []
crypto = GPGME::Crypto.new(armor: true, always_trust: true)
recipients.push(@key) # Encrypt a file
@keys.each_key do |key| # args: data -> string to encrypt
next if key == @key private
recipients.push(key) def encrypt(data)
end recipients = []
crypto = GPGME::Crypto.new(armor: true, always_trust: true)
return crypto.encrypt(data, recipients: recipients).read recipients.push(@key)
rescue Exception => e @keys.each_key do |key|
raise "#{I18n.t('error.gpg_file.encrypt')}\n#{e}" next if key == @key
end recipients.push(key)
end
return crypto.encrypt(data, recipients: recipients).read
rescue Exception => e
raise "#{I18n.t('error.gpg_file.encrypt')}\n#{e}"
end
end end
end end

View file

@ -6,59 +6,59 @@ require 'locale'
require 'i18n' require 'i18n'
class TestConfig < Test::Unit::TestCase class TestConfig < Test::Unit::TestCase
def setup def setup
lang = Locale::Tag.parse(ENV['LANG']).to_simple.to_s[0..1] lang = Locale::Tag.parse(ENV['LANG']).to_simple.to_s[0..1]
if defined?(I18n.enforce_available_locales)
I18n.enforce_available_locales = true
end
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
I18n.load_path = Dir["#{File.expand_path('../../i18n', __FILE__)}/*.yml"]
I18n.default_locale = :en
I18n.locale = lang.to_sym
end
def test_00_config if defined?(I18n.enforce_available_locales)
data = { gpg_key: 'test@example.com', I18n.enforce_available_locales = true
lang: 'en', end
wallet_dir: '/tmp/test',
gpg_exe: '',
}
@config = MPW::Config.new I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
@config.setup(data) I18n.load_path = Dir["#{File.expand_path('../../i18n', __FILE__)}/*.yml"]
@config.load_config I18n.default_locale = :en
I18n.locale = lang.to_sym
end
data.each do |k,v| def test_00_config
assert_equal(v, @config.send(k)) data = { gpg_key: 'test@example.com',
end lang: 'en',
wallet_dir: '/tmp/test',
gpg_exe: '',
}
@config.setup_gpg_key('password', 'test@example.com', 2048) @config = MPW::Config.new
assert(@config.check_gpg_key?) @config.setup(data)
end @config.load_config
def test_01_password data.each do |k,v|
data = { pwd_alpha: false, assert_equal(v, @config.send(k))
pwd_numeric: false, end
pwd_special: true,
pwd_length: 32,
}
@config = MPW::Config.new @config.setup_gpg_key('password', 'test@example.com', 2048)
@config.load_config assert(@config.check_gpg_key?)
end
assert_equal(@config.password[:length], 16) def test_01_password
assert(@config.password[:alpha]) data = { pwd_alpha: false,
assert(@config.password[:numeric]) pwd_numeric: false,
assert(!@config.password[:special]) pwd_special: true,
pwd_length: 32,
}
@config.setup(data) @config = MPW::Config.new
@config.load_config @config.load_config
assert_equal(@config.password[:length], data[:pwd_length]) assert_equal(@config.password[:length], 16)
assert(!@config.password[:alpha]) assert(@config.password[:alpha])
assert(!@config.password[:numeric]) assert(@config.password[:numeric])
assert(@config.password[:special]) assert(!@config.password[:special])
end
@config.setup(data)
@config.load_config
assert_equal(@config.password[:length], data[:pwd_length])
assert(!@config.password[:alpha])
assert(!@config.password[:numeric])
assert(@config.password[:special])
end
end end

View file

@ -1,172 +1,172 @@
#!/usr/bin/ruby #!/usr/bin/ruby
require 'mpw/item' require 'mpw/item'
require 'test/unit' require 'test/unit'
require 'yaml' require 'yaml'
class TestItem < Test::Unit::TestCase class TestItem < Test::Unit::TestCase
def setup def setup
@fixture_file = 'test/files/fixtures.yml' @fixture_file = 'test/files/fixtures.yml'
@fixtures = YAML.load_file(@fixture_file) @fixtures = YAML.load_file(@fixture_file)
if defined?(I18n.enforce_available_locales)
I18n.enforce_available_locales = false
end
I18n.load_path = Dir['./i18n/cli/*.yml'] if defined?(I18n.enforce_available_locales)
I18n.default_locale = :en I18n.enforce_available_locales = false
end
I18n.load_path = Dir['./i18n/cli/*.yml']
I18n.default_locale = :en
puts puts
end end
def test_00_add_without_name def test_00_add_without_name
assert_raise(RuntimeError){MPW::Item.new} assert_raise(RuntimeError){MPW::Item.new}
end end
def test_01_add_new def test_01_add_new
data = { group: @fixtures['add_new']['group'], data = { group: @fixtures['add_new']['group'],
host: @fixtures['add_new']['host'], host: @fixtures['add_new']['host'],
protocol: @fixtures['add_new']['protocol'], protocol: @fixtures['add_new']['protocol'],
user: @fixtures['add_new']['user'], user: @fixtures['add_new']['user'],
port: @fixtures['add_new']['port'], port: @fixtures['add_new']['port'],
comment: @fixtures['add_new']['comment'], comment: @fixtures['add_new']['comment'],
} }
item = MPW::Item.new(data)
assert(!item.nil?) item = MPW::Item.new(data)
assert(!item.empty?)
assert_equal(@fixtures['add_new']['group'], item.group) assert(!item.nil?)
assert_equal(@fixtures['add_new']['host'], item.host) assert(!item.empty?)
assert_equal(@fixtures['add_new']['protocol'], item.protocol)
assert_equal(@fixtures['add_new']['user'], item.user)
assert_equal(@fixtures['add_new']['port'].to_i, item.port)
assert_equal(@fixtures['add_new']['comment'], item.comment)
end
def test_02_add_existing assert_equal(@fixtures['add_new']['group'], item.group)
data = { id: @fixtures['add_existing']['id'], assert_equal(@fixtures['add_new']['host'], item.host)
group: @fixtures['add_existing']['group'], assert_equal(@fixtures['add_new']['protocol'], item.protocol)
host: @fixtures['add_existing']['host'], assert_equal(@fixtures['add_new']['user'], item.user)
protocol: @fixtures['add_existing']['protocol'], assert_equal(@fixtures['add_new']['port'].to_i, item.port)
user: @fixtures['add_existing']['user'], assert_equal(@fixtures['add_new']['comment'], item.comment)
port: @fixtures['add_existing']['port'], end
comment: @fixtures['add_existing']['comment'],
created: @fixtures['add_existing']['created'],
}
item = MPW::Item.new(data) def test_02_add_existing
data = { id: @fixtures['add_existing']['id'],
group: @fixtures['add_existing']['group'],
host: @fixtures['add_existing']['host'],
protocol: @fixtures['add_existing']['protocol'],
user: @fixtures['add_existing']['user'],
port: @fixtures['add_existing']['port'],
comment: @fixtures['add_existing']['comment'],
created: @fixtures['add_existing']['created'],
}
assert(!item.nil?) item = MPW::Item.new(data)
assert(!item.empty?)
assert_equal(@fixtures['add_existing']['id'], item.id) assert(!item.nil?)
assert_equal(@fixtures['add_existing']['group'], item.group) assert(!item.empty?)
assert_equal(@fixtures['add_existing']['host'], item.host)
assert_equal(@fixtures['add_existing']['protocol'], item.protocol)
assert_equal(@fixtures['add_existing']['user'], item.user)
assert_equal(@fixtures['add_existing']['port'].to_i, item.port)
assert_equal(@fixtures['add_existing']['comment'], item.comment)
assert_equal(@fixtures['add_existing']['created'], item.created)
end
def test_03_update assert_equal(@fixtures['add_existing']['id'], item.id)
data = { group: @fixtures['add_new']['group'], assert_equal(@fixtures['add_existing']['group'], item.group)
host: @fixtures['add_new']['host'], assert_equal(@fixtures['add_existing']['host'], item.host)
protocol: @fixtures['add_new']['protocol'], assert_equal(@fixtures['add_existing']['protocol'], item.protocol)
user: @fixtures['add_new']['user'], assert_equal(@fixtures['add_existing']['user'], item.user)
port: @fixtures['add_new']['port'], assert_equal(@fixtures['add_existing']['port'].to_i, item.port)
comment: @fixtures['add_new']['comment'], assert_equal(@fixtures['add_existing']['comment'], item.comment)
} assert_equal(@fixtures['add_existing']['created'], item.created)
end
item = MPW::Item.new(data)
assert(!item.nil?) def test_03_update
assert(!item.empty?) data = { group: @fixtures['add_new']['group'],
host: @fixtures['add_new']['host'],
protocol: @fixtures['add_new']['protocol'],
user: @fixtures['add_new']['user'],
port: @fixtures['add_new']['port'],
comment: @fixtures['add_new']['comment'],
}
created = item.created item = MPW::Item.new(data)
last_edit = item.last_edit
data = { group: @fixtures['update']['group'], assert(!item.nil?)
host: @fixtures['update']['host'], assert(!item.empty?)
protocol: @fixtures['update']['protocol'],
user: @fixtures['update']['user'],
port: @fixtures['update']['port'],
comment: @fixtures['update']['comment'],
}
sleep(1)
assert(item.update(data))
assert(!item.empty?) created = item.created
last_edit = item.last_edit
assert_equal(@fixtures['update']['group'], item.group) data = { group: @fixtures['update']['group'],
assert_equal(@fixtures['update']['host'], item.host) host: @fixtures['update']['host'],
assert_equal(@fixtures['update']['protocol'], item.protocol) protocol: @fixtures['update']['protocol'],
assert_equal(@fixtures['update']['user'], item.user) user: @fixtures['update']['user'],
assert_equal(@fixtures['update']['port'].to_i, item.port) port: @fixtures['update']['port'],
assert_equal(@fixtures['update']['comment'], item.comment) comment: @fixtures['update']['comment'],
}
assert_equal(created, item.created) sleep(1)
assert_not_equal(last_edit, item.last_edit) assert(item.update(data))
end
def test_05_update_one_element assert(!item.empty?)
data = { group: @fixtures['add_new']['group'],
host: @fixtures['add_new']['host'],
protocol: @fixtures['add_new']['protocol'],
user: @fixtures['add_new']['user'],
port: @fixtures['add_new']['port'],
comment: @fixtures['add_new']['comment'],
}
item = MPW::Item.new(data)
assert(!item.nil?) assert_equal(@fixtures['update']['group'], item.group)
assert(!item.empty?) assert_equal(@fixtures['update']['host'], item.host)
assert_equal(@fixtures['update']['protocol'], item.protocol)
assert_equal(@fixtures['update']['user'], item.user)
assert_equal(@fixtures['update']['port'].to_i, item.port)
assert_equal(@fixtures['update']['comment'], item.comment)
last_edit = item.last_edit assert_equal(created, item.created)
assert_not_equal(last_edit, item.last_edit)
end
sleep(1) def test_05_update_one_element
assert(item.update({comment: @fixtures['update']['comment']})) data = { group: @fixtures['add_new']['group'],
host: @fixtures['add_new']['host'],
protocol: @fixtures['add_new']['protocol'],
user: @fixtures['add_new']['user'],
port: @fixtures['add_new']['port'],
comment: @fixtures['add_new']['comment'],
}
assert_equal(@fixtures['add_new']['group'], item.group) item = MPW::Item.new(data)
assert_equal(@fixtures['add_new']['host'], item.host)
assert_equal(@fixtures['add_new']['protocol'], item.protocol)
assert_equal(@fixtures['add_new']['user'], item.user)
assert_equal(@fixtures['add_new']['port'].to_i, item.port)
assert_equal(@fixtures['update']['comment'], item.comment)
assert_not_equal(last_edit, item.last_edit)
end
def test_05_delete assert(!item.nil?)
data = { group: @fixtures['add_new']['group'], assert(!item.empty?)
host: @fixtures['add_new']['host'],
protocol: @fixtures['add_new']['protocol'],
user: @fixtures['add_new']['user'],
port: @fixtures['add_new']['port'],
comment: @fixtures['add_new']['comment'],
}
item = MPW::Item.new(data)
assert(!item.nil?) last_edit = item.last_edit
assert(!item.empty?)
item.delete sleep(1)
assert(!item.nil?) assert(item.update({comment: @fixtures['update']['comment']}))
assert(item.empty?)
assert_equal(nil, item.id) assert_equal(@fixtures['add_new']['group'], item.group)
assert_equal(nil, item.group) assert_equal(@fixtures['add_new']['host'], item.host)
assert_equal(nil, item.host) assert_equal(@fixtures['add_new']['protocol'], item.protocol)
assert_equal(nil, item.protocol) assert_equal(@fixtures['add_new']['user'], item.user)
assert_equal(nil, item.user) assert_equal(@fixtures['add_new']['port'].to_i, item.port)
assert_equal(nil, item.port) assert_equal(@fixtures['update']['comment'], item.comment)
assert_equal(nil, item.comment)
assert_equal(nil, item.created) assert_not_equal(last_edit, item.last_edit)
end end
end
def test_05_delete
data = { group: @fixtures['add_new']['group'],
host: @fixtures['add_new']['host'],
protocol: @fixtures['add_new']['protocol'],
user: @fixtures['add_new']['user'],
port: @fixtures['add_new']['port'],
comment: @fixtures['add_new']['comment'],
}
item = MPW::Item.new(data)
assert(!item.nil?)
assert(!item.empty?)
item.delete
assert(!item.nil?)
assert(item.empty?)
assert_equal(nil, item.id)
assert_equal(nil, item.group)
assert_equal(nil, item.host)
assert_equal(nil, item.protocol)
assert_equal(nil, item.user)
assert_equal(nil, item.port)
assert_equal(nil, item.comment)
assert_equal(nil, item.created)
end
end

View file

@ -1,140 +1,140 @@
#!/usr/bin/ruby #!/usr/bin/ruby
require 'mpw/mpw' require 'mpw/mpw'
require 'mpw/item' require 'mpw/item'
require 'test/unit' require 'test/unit'
require 'yaml' require 'yaml'
require 'csv' require 'csv'
class TestMPW < Test::Unit::TestCase class TestMPW < Test::Unit::TestCase
def setup def setup
fixture_file = './test/files/fixtures.yml' fixture_file = './test/files/fixtures.yml'
wallet_file = 'default.gpg' wallet_file = 'default.gpg'
key = 'test@example.com' key = 'test@example.com'
password = 'password' password = 'password'
if defined?(I18n.enforce_available_locales) if defined?(I18n.enforce_available_locales)
I18n.enforce_available_locales = false I18n.enforce_available_locales = false
end end
@mpw = MPW::MPW.new(key, wallet_file, password) @mpw = MPW::MPW.new(key, wallet_file, password)
@fixtures = YAML.load_file(fixture_file) @fixtures = YAML.load_file(fixture_file)
end end
def test_00_decrypt_empty_file
@mpw.read_data
assert_equal(0, @mpw.list.length)
end
def test_01_encrypt_empty_file def test_00_decrypt_empty_file
@mpw.read_data @mpw.read_data
@mpw.write_data assert_equal(0, @mpw.list.length)
end end
def test_02_add_item def test_01_encrypt_empty_file
data = { group: @fixtures['add_new']['group'], @mpw.read_data
host: @fixtures['add_new']['host'], @mpw.write_data
protocol: @fixtures['add_new']['protocol'], end
user: @fixtures['add_new']['user'],
port: @fixtures['add_new']['port'],
comment: @fixtures['add_new']['comment'],
}
item = MPW::Item.new(data)
assert(!item.nil?) def test_02_add_item
assert(!item.empty?) data = { group: @fixtures['add_new']['group'],
host: @fixtures['add_new']['host'],
protocol: @fixtures['add_new']['protocol'],
user: @fixtures['add_new']['user'],
port: @fixtures['add_new']['port'],
comment: @fixtures['add_new']['comment'],
}
@mpw.read_data item = MPW::Item.new(data)
@mpw.add(item)
@mpw.set_password(item.id, @fixtures['add_new']['password'])
assert_equal(1, @mpw.list.length) assert(!item.nil?)
assert(!item.empty?)
item = @mpw.list[0] @mpw.read_data
@fixtures['add_new'].each do |k,v| @mpw.add(item)
if k == 'password' @mpw.set_password(item.id, @fixtures['add_new']['password'])
assert_equal(v, @mpw.get_password(item.id))
else
assert_equal(v, item.send(k).to_s)
end
end
@mpw.write_data assert_equal(1, @mpw.list.length)
end
def test_03_decrypt_file item = @mpw.list[0]
@mpw.read_data @fixtures['add_new'].each do |k,v|
assert_equal(1, @mpw.list.length) if k == 'password'
assert_equal(v, @mpw.get_password(item.id))
else
assert_equal(v, item.send(k).to_s)
end
end
item = @mpw.list[0] @mpw.write_data
@fixtures['add_new'].each do |k,v| end
if k == 'password'
assert_equal(v, @mpw.get_password(item.id))
else
assert_equal(v, item.send(k).to_s)
end
end
end
def test_04_delete_item def test_03_decrypt_file
@mpw.read_data @mpw.read_data
assert_equal(1, @mpw.list.length)
assert_equal(1, @mpw.list.length) item = @mpw.list[0]
@fixtures['add_new'].each do |k,v|
if k == 'password'
assert_equal(v, @mpw.get_password(item.id))
else
assert_equal(v, item.send(k).to_s)
end
end
end
@mpw.list.each do |item| def test_04_delete_item
item.delete @mpw.read_data
end
assert_equal(0, @mpw.list.length) assert_equal(1, @mpw.list.length)
@mpw.write_data @mpw.list.each do |item|
end item.delete
end
def test_05_search assert_equal(0, @mpw.list.length)
@mpw.read_data
@fixtures.each_value do |v| @mpw.write_data
data = { group: v['group'], end
host: v['host'],
protocol: v['protocol'],
user: v['user'],
port: v['port'],
comment: v['comment'],
}
item = MPW::Item.new(data)
assert(!item.nil?)
assert(!item.empty?)
@mpw.add(item)
@mpw.set_password(item.id, v['password'])
end
assert_equal(3, @mpw.list.length) def test_05_search
assert_equal(1, @mpw.list(group: @fixtures['add_new']['group']).length) @mpw.read_data
assert_equal(1, @mpw.list(pattern: 'existing').length)
assert_equal(2, @mpw.list(pattern: 'host_[eu]').length)
end
def test_06_add_gpg_key @fixtures.each_value do |v|
@mpw.read_data data = { group: v['group'],
host: v['host'],
protocol: v['protocol'],
user: v['user'],
port: v['port'],
comment: v['comment'],
}
@mpw.add_key('test2@example.com') item = MPW::Item.new(data)
assert_equal(2, @mpw.list_keys.length)
@mpw.write_data assert(!item.nil?)
end assert(!item.empty?)
def test_07_delete_gpg_key @mpw.add(item)
@mpw.read_data @mpw.set_password(item.id, v['password'])
assert_equal(2, @mpw.list_keys.length) end
@mpw.delete_key('test2@example.com') assert_equal(3, @mpw.list.length)
assert_equal(1, @mpw.list_keys.length) assert_equal(1, @mpw.list(group: @fixtures['add_new']['group']).length)
assert_equal(1, @mpw.list(pattern: 'existing').length)
assert_equal(2, @mpw.list(pattern: 'host_[eu]').length)
end
@mpw.write_data def test_06_add_gpg_key
end @mpw.read_data
@mpw.add_key('test2@example.com')
assert_equal(2, @mpw.list_keys.length)
@mpw.write_data
end
def test_07_delete_gpg_key
@mpw.read_data
assert_equal(2, @mpw.list_keys.length)
@mpw.delete_key('test2@example.com')
assert_equal(1, @mpw.list_keys.length)
@mpw.write_data
end
end end

View file

@ -4,28 +4,28 @@ require 'yaml'
require 'test/unit' require 'test/unit'
class TestTranslate < Test::Unit::TestCase class TestTranslate < Test::Unit::TestCase
def test_00_check_translate def test_00_check_translate
missing = 0 missing = 0
Dir.glob('i18n/*.yml').each do |yaml|
lang = File.basename(yaml, '.yml')
translate = YAML.load_file(yaml)
`grep -r -o "I18n.t('.*)" bin/ lib/ | cut -d"'" -f2`.each_line do |line|
begin
t = translate[lang]
line.strip.split('.').each do |v|
t = t[v]
end
assert(!t.to_s.empty?) Dir.glob('i18n/*.yml').each do |yaml|
rescue lang = File.basename(yaml, '.yml')
puts "#{lang}.#{line}" translate = YAML.load_file(yaml)
missing = 1
end `grep -r -o "I18n.t('.*)" bin/ lib/ | cut -d"'" -f2`.each_line do |line|
end begin
end t = translate[lang]
line.strip.split('.').each do |v|
assert_equal(0, missing) t = t[v]
end end
assert(!t.to_s.empty?)
rescue
puts "#{lang}.#{line}"
missing = 1
end
end
end
assert_equal(0, missing)
end
end end

View file

@ -1,5 +1,5 @@
#!/usr/bin/ruby #!/usr/bin/ruby
require_relative 'init.rb' require_relative 'init.rb'
require_relative 'test_config.rb' require_relative 'test_config.rb'
require_relative 'test_item.rb' require_relative 'test_item.rb'