App-Raps2
view release on metacpan or search on metacpan
lib/App/Raps2.pm view on Meta::CPAN
bless( $self, $class );
if ( not $opt{dont_touch_fs} ) {
$self->sanity_check();
$self->load_config();
$self->load_defaults();
}
if ( $opt{master_password} ) {
$self->get_master_password( $opt{master_password} );
}
return $self;
}
sub file_to_hash {
my ( $self, $file ) = @_;
my $ret;
for my $line ( slurp($file) ) {
my ( $key, $value ) = ( $line =~ m{ ^ ([^ ]+) \s+ (.+) $ }x );
if ( not( $key and $value ) ) {
next;
}
$ret->{$key} = $value;
}
return $ret;
}
sub sanity_check {
my ($self) = @_;
make_path( $self->{xdg_conf}, $self->{xdg_data} );
if ( not -e $self->{xdg_conf} . '/password' ) {
$self->create_config();
}
if ( not -e $self->{xdg_conf} . '/defaults' ) {
$self->create_defaults();
}
return;
}
sub get_master_password {
my ( $self, $pass ) = @_;
if ( not defined $pass ) {
$pass = $self->ui->read_pw( 'Master Password', 0 );
}
$self->{pass} = App::Raps2::Password->new(
cost => $self->{master_cost},
salt => $self->{master_salt},
passphrase => $pass,
);
$self->pw->verify( $self->{master_hash} );
return;
}
sub create_config {
my ($self) = @_;
my $cost = $self->{master_cost} = $self->{default}{cost} // 12;
my $pass = $self->{default}{master_password} // $self->ui->read_pw(
'Running for the first time. Please choose a master password', 1 );
$self->{pass} = App::Raps2::Password->new(
cost => $self->{master_cost},
passphrase => $pass,
);
my $hash = $self->{master_hash} = $self->pw->bcrypt;
my $salt = $self->{master_salt} = $self->pw->salt;
write_file(
$self->{xdg_conf} . '/password',
"cost ${cost}\n",
"salt ${salt}\n",
"hash ${hash}\n",
);
return;
}
sub load_config {
my ($self) = @_;
my $cfg = $self->file_to_hash( $self->{xdg_conf} . '/password' );
$self->{master_hash} = $cfg->{hash};
$self->{master_salt} = $cfg->{salt};
$self->{master_cost} = $cfg->{cost};
return;
}
sub create_defaults {
my ($self) = @_;
my $cost = $self->{default}{cost} // 12;
my $pwgen_cmd = $self->{default}{pwgen_cmd} // 'pwgen -s 23 1';
my $xclip_cmd = $self->{default}{xclip_cmd} // 'xclip -l 1';
write_file(
$self->{xdg_conf} . '/defaults',
"cost = ${cost}\n",
"pwgen_cmd = ${pwgen_cmd}\n",
"xclip_cmd = ${xclip_cmd}\n",
);
return;
}
sub load_defaults {
( run in 0.692 second using v1.01-cache-2.11-cpan-13bb782fe5a )