diff options
author | Daniel Friesel <derf@finalrewind.org> | 2011-07-20 19:04:29 +0200 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2011-07-20 19:04:29 +0200 |
commit | d40f159f56eb098a9ddf082ebd64b414e2e8801d (patch) | |
tree | 3e67810231a553b13265bf246e3959aabe73eb78 | |
parent | f5ebcadaae26ef13e4bdd47af63cb6056a43e25d (diff) |
raps2 add: Use pwgen to generate password if the user didn't specify one
-rwxr-xr-x | bin/raps2 | 10 | ||||
-rw-r--r-- | lib/App/Raps2.pm | 31 |
2 files changed, 29 insertions, 12 deletions
@@ -62,6 +62,16 @@ sub cmd_add { my $pass = $raps2->ui->read_pw( 'Password', 1 ); my $extra = $raps2->ui->read_multiline('Additional content'); + if ( length($pass) == 0 ) { + $pass = $raps2->generate_password(); + + if ( not $pass ) { + say STDERR "Password generation failed: ${!}: " + . $raps2->conf('pwgen_cmd'); + exit 3; + } + } + $raps2->pw_save( file => $pwfile, url => $url, diff --git a/lib/App/Raps2.pm b/lib/App/Raps2.pm index 073ff33..d173adc 100644 --- a/lib/App/Raps2.pm +++ b/lib/App/Raps2.pm @@ -6,7 +6,7 @@ use 5.010; use App::Raps2::Password; use App::Raps2::UI; -use Carp qw(confess); +use Carp qw(cluck confess); use Config::Tiny; use File::BaseDir qw(config_home data_home); use File::Path qw(make_path); @@ -155,15 +155,10 @@ sub load_defaults { return; } -sub cost { - my ( $self, $cost ) = @_; +sub conf { + my ( $self, $key ) = @_; - if ($cost) { - $self->{default}{cost} = $cost; - $self->write_config(); - } - - return $self->{default}{cost}; + return $self->{default}{$key}; } sub pw { @@ -187,6 +182,18 @@ sub ui { return $self->{ui}; } +sub generate_password { + my ($self) = @_; + + open( my $pwgen, q{-|}, $self->conf('pwgen_cmd') ) or return; + my $password = <$pwgen>; + close($pwgen) or cluck("Cannot close pwgen pipe: $!"); + + chomp $password; + + return $password; +} + sub pw_save { my ( $self, %data ) = @_; @@ -236,9 +243,9 @@ sub pw_load { # use the one of the master password return { - url => $key->{url}, - login => $key->{login}, - cost => $key->{cost} // $self->{master_cost}, + url => $key->{url}, + login => $key->{login}, + cost => $key->{cost} // $self->{master_cost}, password => $self->pw->decrypt( data => $key->{hash}, salt => $key->{salt}, |