From 000bc3aaa55c807b5e34cf7594491eca10c9e678 Mon Sep 17 00:00:00 2001 From: Adrien Waksberg Date: Sat, 25 Feb 2017 18:43:30 +0100 Subject: [PATCH] add config option for password parameters --- bin/mpw-config | 30 +++++++++++++++++++++++++++++- i18n/en.yml | 3 +++ i18n/fr.yml | 3 +++ lib/mpw/cli.rb | 10 ++-------- lib/mpw/config.rb | 34 ++++++++++++++++++++++++++-------- lib/mpw/mpw.rb | 6 +++--- 6 files changed, 66 insertions(+), 20 deletions(-) diff --git a/bin/mpw-config b/bin/mpw-config index 74309ce..1034f04 100644 --- a/bin/mpw-config +++ b/bin/mpw-config @@ -56,13 +56,41 @@ OptionParser.new do |opts| values[:gpg_key] = gpg_key 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 end opts.on('-w', '--wallet-dir PATH', I18n.t('option.wallet_dir')) do |wallet_dir| values[:wallet_dir] = wallet_dir end + + opts.on('-l', '--length NUMBER', I18n.t('option.length')) do |length| + values[:pwd_length] = length.to_i + end + + opts.on('-n', '--numeric', I18n.t('option.numeric')) do + values[:pwd_numeric] = true + end + + opts.on('-N', '--disable-numeric', I18n.t('option.disable_numeric')) do + values[:pwd_numeric] = false + end + + opts.on('-s', '--special-chars', I18n.t('option.special_chars')) do + values[:pwd_special] = true + end + + opts.on('-S', '--disable_special-chars', I18n.t('option.special_chars')) do + values[:pwd_special] = false + end + + opts.on('-a', '--alpha', I18n.t('option.alpha')) do + values[:pwd_alpha] = true + end + + opts.on('-A', '--disable-alpha', I18n.t('option.disable_alpha')) do + values[:pwd_alpha] = false + end end.parse! config = MPW::Config.new(options[:config]) diff --git a/i18n/en.yml b/i18n/en.yml index 6dafacf..5ee9868 100644 --- a/i18n/en.yml +++ b/i18n/en.yml @@ -56,6 +56,9 @@ en: clipboard: "Disable the clipboard feature" default_wallet: "Specify the default wallet to use" delete_gpg_key: "Delete the wallet's share with an other GPG key" + disable_alpha: "Don't use letter to generate a password" + disable_numeric: "Don't use number to generate a password" + disable_special_chars: "Don't use special char to generate a password" 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 a20e58f..d9bd279 100644 --- a/i18n/fr.yml +++ b/i18n/fr.yml @@ -56,6 +56,9 @@ fr: clipboard: "Désactive la fonction presse papier" default_wallet: "Spécifie le porte-feuille à utiliser par défaut" delete_gpg_key: "Supprime le partage le portefeuille avec une autre clé GPG" + disable_alpha: "Désactive l'utilisation des lettres dans la génération d'un mot de passe" + disable_numeric: "Désactive l'utilisation des chiffre dans la génération d'un mot de passe" + disable_special_chars: "Désactive l'utilisation des charactères speciaux dans la génération d'un mot de passe" 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 12bbe22..97e7e56 100644 --- a/lib/mpw/cli.rb +++ b/lib/mpw/cli.rb @@ -40,13 +40,7 @@ class Cli # Change a parameter int the config after init # @args: options -> param to change def set_config(options) - gpg_key = options[:gpg_key] || @config.key - lang = options[:lang] || @config.lang - wallet_dir = options[:wallet_dir] || @config.wallet_dir - default_wallet = options[:default_wallet] || @config.default_wallet - gpg_exe = options[:gpg_exe] || @config.gpg_exe - - @config.setup(gpg_key, lang, wallet_dir, default_wallet, gpg_exe) + @config.setup(options) puts "#{I18n.t('form.set_config.valid')}".green rescue Exception => e @@ -433,7 +427,7 @@ class Cli item = Item.new(options) if password - options[:password] = MPW::password(length: 24) + options[:password] = MPW::password(@config.password) end @mpw.add(item) diff --git a/lib/mpw/config.rb b/lib/mpw/config.rb index c1e883c..94b9eb8 100644 --- a/lib/mpw/config.rb +++ b/lib/mpw/config.rb @@ -32,6 +32,7 @@ class Config attr_accessor :default_wallet attr_accessor :wallet_dir attr_accessor :gpg_exe + attr_accessor :password # Constructor # @args: config_file -> the specify config file @@ -50,23 +51,39 @@ class Config end # Create a new config file - # @args: key -> the gpg key to encrypt - # lang -> the software language - # wallet_dir -> the directory where are the wallets password - # default_wallet -> the default wallet - # gpg_exe -> the path of gpg executable + # @args: options -> hash with values # @rtrn: true if le config file is create - def setup(key, lang, wallet_dir, default_wallet, gpg_exe) - if not key =~ /[a-zA-Z0-9.-_]+\@[a-zA-Z0-9]+\.[a-zA-Z]+/ + def setup(options) + gpg_key = options[:gpg_key] || @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, + } + + ['numeric', 'special', 'alpha', 'length'].each do |k| + if options.has_key?("pwd_#{k}".to_sym) + 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]+/ raise I18n.t('error.config.key_bad_format') end wallet_dir = "#{@config_dir}/wallets" if wallet_dir.to_s.empty? - config = { 'key' => key, + config = { 'key' => gpg_key, 'lang' => lang, 'wallet_dir' => wallet_dir, 'default_wallet' => default_wallet, 'gpg_exe' => gpg_exe, + 'password' => password, } FileUtils.mkdir_p(@config_dir, mode: 0700) @@ -119,6 +136,7 @@ class Config @wallet_dir = config['wallet_dir'] @default_wallet = config['default_wallet'] @gpg_exe = config['gpg_exe'] + @password = config['password'] raise if @key.empty? or @wallet_dir.empty? diff --git a/lib/mpw/mpw.rb b/lib/mpw/mpw.rb index c5023e6..761127c 100644 --- a/lib/mpw/mpw.rb +++ b/lib/mpw/mpw.rb @@ -431,9 +431,9 @@ class MPW end chars = [] - chars += [*('!'..'?')] - [*('0'..'9')] if options.include?(:special) - chars += [*('A'..'Z'),*('a'..'z')] if options.include?(:alpha) - chars += [*('0'..'9')] if options.include?(:numeric) + chars += [*('!'..'?')] - [*('0'..'9')] if options[:special] + chars += [*('A'..'Z'),*('a'..'z')] if options[:alpha] + chars += [*('0'..'9')] if options[:numeric] chars = [*('A'..'Z'),*('a'..'z'),*('0'..'9')] if chars.empty? result = ''