From 580c5b419388d1cd7cdb169e4d050ecf8e8e036f Mon Sep 17 00:00:00 2001 From: Adrien Waksberg Date: Thu, 20 Oct 2016 14:31:25 +0200 Subject: [PATCH] add random password when create new item --- bin/mpw-add | 6 +++++- lib/mpw/cli.rb | 15 ++++++++++++--- templates/add_form.erb | 4 ++-- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/bin/mpw-add b/bin/mpw-add index 1e09e07..542b884 100644 --- a/bin/mpw-add +++ b/bin/mpw-add @@ -38,6 +38,10 @@ OptionParser.new do |opts| exit 0 end + opts.on('-r', '--random', I18n.t('option.random_password')) do + options[:password] = true + end + opts.on('-w', '--wallet NAME', I18n.t('option.wallet')) do |wallet| options[:wallet] = wallet end @@ -48,4 +52,4 @@ cli = MPW::Cli.new(config, options[:sync]) cli.get_wallet(options[:wallet]) cli.decrypt -cli.add +cli.add(options[:password]) diff --git a/lib/mpw/cli.rb b/lib/mpw/cli.rb index 3420bc1..b52a63b 100644 --- a/lib/mpw/cli.rb +++ b/lib/mpw/cli.rb @@ -368,7 +368,11 @@ class Cli puts "#{I18n.t('display.error')} #15: #{e}".red end - def text_editor(template_name, item=nil) + # Text editor interface + # @args: template -> template name + # item -> the item to edit + # password -> disable field password + def text_editor(template_name, item=nil, password=false) editor = ENV['EDITOR'] || 'nano' options = {} opts = {} @@ -397,10 +401,15 @@ class Cli end # Form to add a new item - def add - options = text_editor('add_form') + # @args: password -> generate a random password + def add(password=false) + options = text_editor('add_form', nil, password) item = Item.new(options) + if password + options[:password] = MPW::password(length: 24) + end + @mpw.add(item) @mpw.set_password(item.id, options[:password]) if options.has_key?(:password) @mpw.set_otp_key(item.id, options[:otp_key]) if options.has_key?(:otp_key) diff --git a/templates/add_form.erb b/templates/add_form.erb index f4f8cae..c2d6d73 100644 --- a/templates/add_form.erb +++ b/templates/add_form.erb @@ -2,8 +2,8 @@ host: # <%= I18n.t('form.add_item.host') %> user: # <%= I18n.t('form.add_item.login') %> group: # <%= I18n.t('form.add_item.group') %> -protocol: # <%= I18n.t('form.add_item.protocol') %> -password: # <%= I18n.t('form.add_item.password') %> +protocol: # <%= I18n.t('form.add_item.protocol') %><% if not password %> +password: # <%= I18n.t('form.add_item.password') %><% end %> port: # <%= I18n.t('form.add_item.port') %> comment: # <%= I18n.t('form.add_item.comment') %> otp_key: # <%= I18n.t('form.add_item.otp_key') %>