diff --git a/bin/mpw-wallet b/bin/mpw-wallet index b81e6b2..8bdfb1c 100644 --- a/bin/mpw-wallet +++ b/bin/mpw-wallet @@ -24,17 +24,27 @@ require 'mpw/cli' # Options # --------------------------------------------------------- # -options = {} -options[:sync] = {} -values = {} +options = {} +options[:sync] = {} +options[:delete] = false +values = {} OptionParser.new do |opts| 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| + options[:gpg_key] = gpg_key + end + opts.on('-c', '--config PATH', I18n.t('option.config')) do |config| options[:config] = config end + opts.on('-d', '--delete-gpg-key NAME', I18n.t('option.delete_gpg_key')) do |gpg_key| + options[:gpg_key] = gpg_key + options[:delete] = true + end + opts.on('-h', '--help', I18n.t('option.help')) do puts opts exit 0 @@ -84,6 +94,12 @@ cli.load_config if not options[:list].nil? cli.list_wallet +elsif not options[:gpg_key].nil? + if options[:delete] + cli.delete_key(options[:gpg_key]) + else + cli.add_key(options[:gpg_key]) + end else cli.get_wallet(options[:wallet]) cli.decrypt diff --git a/i18n/en.yml b/i18n/en.yml index 252efee..16a8c69 100644 --- a/i18n/en.yml +++ b/i18n/en.yml @@ -50,9 +50,11 @@ en: option: add: "Add an item or key" + add_gpg_key: "Share the wallet with an other GPG key" alpha: "Use letter to generate a password" config: "Specify the configuration file to use" clipboard: "Disable the clipboard feature" + delete_gpg_key: "Delete the wallet's share with an other GPG key" export: "Export a wallet in an yaml file" file_export: "Specify the file where export data" file_import: "Specify the file to import" diff --git a/i18n/fr.yml b/i18n/fr.yml index 16160cd..0e4e9f6 100644 --- a/i18n/fr.yml +++ b/i18n/fr.yml @@ -50,9 +50,11 @@ fr: option: add: "Ajoute un élément ou une clé" + add_gpg_key: "Partage le portefeuille avec une autre clé GPG" alpha: "Utilise des lettres dans la génération d'un mot de passe" config: "Spécifie le fichier de configuration à utiliser" clipboard: "Désactive la fonction presse papier" + delete_gpg_key: "Supprime le partage le portefeuille avec une autre clé GPG" export: "Exporte un portefeuille dans un fichier yaml" file_export: "Spécifie le fichier où exporter les données" file_import: "Spécifie le fichier à importer" diff --git a/lib/mpw/cli.rb b/lib/mpw/cli.rb index 47dfa04..09e3ebd 100644 --- a/lib/mpw/cli.rb +++ b/lib/mpw/cli.rb @@ -334,10 +334,9 @@ class Cli end # Add a new public key - # args: key -> the key name to add - # file -> gpg public file to import - def add_key(key, file=nil) - @mpw.add_key(key, file) + # args: key -> the key name or key file to add + def add_key(key) + @mpw.add_key(key) @mpw.write_data @mpw.sync(true) if @sync diff --git a/lib/mpw/mpw.rb b/lib/mpw/mpw.rb index 91bd04a..c5023e6 100644 --- a/lib/mpw/mpw.rb +++ b/lib/mpw/mpw.rb @@ -184,13 +184,19 @@ class MPW @passwords[id] = encrypt(password) end + # Return the list of all gpg keys + # rtrn: an array with the gpg keys name + def list_keys + return @keys.keys + end + # Add a public key - # args: key -> new public key - # file -> public gpg file to import - def add_key(key, file=nil) - if not file.nil? and File.exists?(file) - data = File.open(file).read - GPGME::Key.import(data, armor: true) + # args: key -> new public key file or name + 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 diff --git a/test/init.rb b/test/init.rb new file mode 100644 index 0000000..be7a73a --- /dev/null +++ b/test/init.rb @@ -0,0 +1,19 @@ +#!/usr/bin/ruby + +require 'gpgme' + +param = '' +param << '' + "\n" +param << "Key-Type: RSA\n" +param << "Key-Length: 2048\n" +param << "Subkey-Type: ELG-E\n" +param << "Subkey-Length: 2048\n" +param << "Name-Real: test\n" +param << "Name-Comment: test\n" +param << "Name-Email: test2@example.com\n" +param << "Expire-Date: 0\n" +param << "Passphrase: password\n" +param << "\n" + +ctx = GPGME::Ctx.new +ctx.genkey(param, nil, nil) diff --git a/test/test_mpw.rb b/test/test_mpw.rb index 0c6a25b..c4748f6 100644 --- a/test/test_mpw.rb +++ b/test/test_mpw.rb @@ -118,4 +118,23 @@ class TestMPW < Test::Unit::TestCase assert_equal(1, @mpw.list(pattern: 'existing').length) assert_equal(2, @mpw.list(pattern: 'host_[eu]').length) end + + def test_06_add_gpg_key + @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 diff --git a/test/tests.rb b/test/tests.rb index e4efc5d..904f64e 100644 --- a/test/tests.rb +++ b/test/tests.rb @@ -1,5 +1,6 @@ #!/usr/bin/ruby +require_relative 'init.rb' require_relative 'test_config.rb' require_relative 'test_item.rb' require_relative 'test_mpw.rb'