summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Changelog8
-rwxr-xr-xbin/raps2180
-rw-r--r--lib/App/Raps2.pm211
3 files changed, 186 insertions, 213 deletions
diff --git a/Changelog b/Changelog
index dc905fa..9a49db0 100644
--- a/Changelog
+++ b/Changelog
@@ -1,3 +1,11 @@
+git HEAD
+
+ [App::Raps2]
+ * Move cmd_* functions into raps2
+ * Rename pw_add t pw_save
+ * Rename pw_get to pw_load
+ * pw_load now also returns the salt
+
App::Raps2 0.4 - Sun Jul 03 2011
* Rename App::Raps2::Password's crypt method to bcrypt to avoid name
diff --git a/bin/raps2 b/bin/raps2
index 62e70e1..e05a7d5 100755
--- a/bin/raps2
+++ b/bin/raps2
@@ -7,22 +7,186 @@ use warnings;
use 5.010;
use App::Raps2;
+use File::BaseDir qw(data_files data_home);
+use File::Slurp qw(read_dir);
my $raps2 = App::Raps2->new();
my ( $action, @args ) = @ARGV;
our $VERSION = '0.4';
+sub cmd_add {
+ my ($name) = @_;
+
+ my $pwfile = data_home('raps2') . "/${name}";
+
+ if ( -e $pwfile ) {
+ say STDERR "Account already exists: ${name}";
+ exit 2;
+ }
+
+ $raps2->get_master_password();
+
+ my $salt = $raps2->pw->create_salt();
+ my $url = $raps2->ui->read_line('URL');
+ my $login = $raps2->ui->read_line('Login');
+ my $pass = $raps2->ui->read_pw( 'Password', 1 );
+ my $extra = $raps2->ui->read_multiline('Additional content');
+
+ $raps2->pw_save(
+ file => $pwfile,
+ salt => $salt,
+ url => $url,
+ login => $login,
+ password => $pass,
+ extra => $extra,
+ );
+
+ return;
+}
+
+sub cmd_dump {
+ my ($name) = @_;
+
+ my $pwfile = data_files("raps2/${name}");
+
+ if ( not defined $pwfile ) {
+ say STDERR "Account does not exist: ${name}";
+ exit 2;
+ }
+
+ $raps2->get_master_password();
+
+ my $key = $raps2->pw_load( file => $pwfile );
+
+ $raps2->ui->output(
+ [ 'URL', $key->{url} ],
+ [ 'Login', $key->{login} ],
+ [ 'Password', $key->{password} ],
+ );
+ if ( $key->{extra} ) {
+ print $key->{extra};
+ }
+
+ return;
+}
+
+sub cmd_edit {
+ my ($name) = @_;
+
+ my $pwfile = data_files("raps2/${name}");
+
+ if ( not defined $pwfile ) {
+ say STDERR "Account does not exist: ${name}";
+ exit 2;
+ }
+
+ $raps2->get_master_password();
+
+ my $key = $raps2->pw_load( file => $pwfile );
+
+ my $salt = $key->{salt};
+ my $url = $raps2->ui->read_line( 'URL', $key->{url} );
+ my $login = $raps2->ui->read_line( 'Login', $key->{login} );
+ my $pass = $key->{password};
+ my $new_pass = $raps2->ui->read_pw( 'New password (empty to keep old)', 1 );
+ my $extra = $key->{extra} // q{};
+
+ if ( length($new_pass) ) {
+ $pass = $new_pass;
+ }
+
+ $raps2->pw_save(
+ file => $pwfile,
+ salt => $salt,
+ url => $url,
+ login => $login,
+ password => $pass,
+ extra => $extra,
+ );
+
+ return;
+}
+
+sub cmd_get {
+ my ($name) = @_;
+
+ my $pwfile = data_files("raps2/${name}");
+
+ if ( not defined $pwfile ) {
+ say STDERR "Account does not exist: ${name}";
+ exit 2;
+ }
+
+ $raps2->get_master_password();
+
+ my $key = $raps2->pw_load( file => $pwfile );
+
+ $raps2->ui->to_clipboard( $key->{password} );
+
+ if ( $key->{extra} ) {
+ print $key->{extra};
+ }
+
+ return;
+}
+
+sub cmd_info {
+ my ($name) = @_;
+
+ my $pwfile = data_files("raps2/${name}");
+
+ if ( not defined $pwfile ) {
+ say STDERR "Account does not exist: ${name}";
+ exit 2;
+ }
+
+ my %key = $raps2->file_to_hash($pwfile);
+ $raps2->ui->output( [ 'URL', $key{url} ], [ 'Login', $key{login} ], );
+
+ return;
+}
+
+sub cmd_list {
+ my @files = read_dir( data_home('raps2') );
+
+ for my $file ( sort @files ) {
+ my %key = $raps2->file_to_hash( data_files("raps2/${file}") );
+ $raps2->ui->list(
+ [ 'Account', $file ],
+ [ 'Login', $key{login} ],
+ [ 'URL', $key{url} ],
+ );
+ }
+
+ return;
+}
+
+sub cmd_remove {
+ my ($name) = @_;
+
+ my $pwfile = data_files("raps2/${name}");
+
+ if ( defined $pwfile ) {
+ unlink($pwfile);
+ }
+ else {
+ say STDERR "Account did not exist: ${name}";
+ }
+
+ return;
+}
+
given ($action) {
- when ('add') { $raps2->cmd_add(@args) }
- when ('del') { $raps2->cmd_remove(@args) }
- when ('dump') { $raps2->cmd_dump(@args) }
- when ('edit') { $raps2->cmd_edit(@args) }
- when ('get') { $raps2->cmd_get(@args) }
- when ('info') { $raps2->cmd_info(@args) }
- when ('list') { $raps2->cmd_list(@args) }
+ when ('add') { cmd_add(@args) }
+ when ('del') { cmd_remove(@args) }
+ when ('dump') { cmd_dump(@args) }
+ when ('edit') { cmd_edit(@args) }
+ when ('get') { cmd_get(@args) }
+ when ('info') { cmd_info(@args) }
+ when ('list') { cmd_list(@args) }
when ('version') { say "raps2 version ${VERSION}" }
- default { die "Unknown action: $action\n" }
+ default { die "Unknown action: ${action}\n" }
}
__END__
diff --git a/lib/App/Raps2.pm b/lib/App/Raps2.pm
index 2c73f2a..61bbaee 100644
--- a/lib/App/Raps2.pm
+++ b/lib/App/Raps2.pm
@@ -9,7 +9,7 @@ use App::Raps2::UI;
use Carp qw(confess);
use File::BaseDir qw(config_home data_home);
use File::Path qw(make_path);
-use File::Slurp qw(read_dir slurp write_file);
+use File::Slurp qw(slurp write_file);
our $VERSION = '0.4';
@@ -130,7 +130,7 @@ sub ui {
return $self->{ui};
}
-sub pw_add {
+sub pw_save {
my ( $self, %data ) = @_;
$data{file} //= $self->{xdg_data} . "/$data{name}";
@@ -154,36 +154,7 @@ sub pw_add {
return;
}
-sub cmd_add {
- my ( $self, $name ) = @_;
-
- my $pwfile = $self->{xdg_data} . "/${name}";
-
- if ( -e $pwfile ) {
- confess('Password file already exists');
- }
-
- $self->get_master_password();
-
- my $salt = $self->pw->create_salt();
- my $url = $self->ui->read_line('URL');
- my $login = $self->ui->read_line('Login');
- my $pass = $self->ui->read_pw( 'Password', 1 );
- my $extra = $self->ui->read_multiline('Additional content');
-
- $self->pw_add(
- file => $pwfile,
- salt => $salt,
- url => $url,
- login => $login,
- password => $pass,
- extra => $extra,
- );
-
- return;
-}
-
-sub pw_get {
+sub pw_load {
my ( $self, %data ) = @_;
$data{file} //= $self->{xdg_data} . "/$data{name}";
@@ -194,6 +165,7 @@ sub pw_get {
url => $key{url},
login => $key{login},
password => $self->pw->decrypt( $key{hash}, $key{salt} ),
+ salt => $key{salt},
extra => (
$key{extra}
? $self->pw->decrypt( $key{extra}, $key{salt} )
@@ -202,138 +174,6 @@ sub pw_get {
};
}
-sub cmd_dump {
- my ( $self, $name ) = @_;
-
- my $pwfile = $self->{xdg_data} . "/${name}";
-
- if ( not -e $pwfile ) {
- confess('Password file does not exist');
- }
-
- $self->get_master_password();
-
- my $key = $self->pw_get( file => $pwfile );
-
- $self->ui()->output(
- [ 'URL', $key->{url} ],
- [ 'Login', $key->{login} ],
- [ 'Password', $key->{password} ],
- );
- if ( $key->{extra} ) {
- print $key->{extra};
- }
-
- return;
-}
-
-sub cmd_edit {
- my ( $self, $name ) = @_;
-
- my $pwfile = $self->{xdg_data} . "/${name}";
- my $pass_hash;
-
- if ( not -e $pwfile ) {
- confess('Password file does not exist');
- }
-
- my %key = $self->file_to_hash($pwfile);
-
- $self->get_master_password();
-
- my $salt = $key{salt};
- my $url = $self->ui->read_line( 'URL', $key{url} );
- my $login = $self->ui->read_line( 'Login', $key{login} );
- my $pass = $self->ui->read_pw( 'New password (empty to keep old)', 1 );
- my $extra = $key{extra} // q{};
-
- if ( length($pass) ) {
- $pass_hash = $self->pw->encrypt( $pass, $salt );
- }
- else {
- $pass_hash = $key{hash};
- }
-
- write_file(
- $pwfile, "url ${url}\n",
- "login ${login}\n",
- "salt ${salt}\n",
- "hash ${pass_hash}\n",
- "extra ${extra}\n",
- );
-
- return;
-}
-
-sub cmd_get {
- my ( $self, $name ) = @_;
-
- my $pwfile = $self->{xdg_data} . "/${name}";
-
- if ( not -e $pwfile ) {
- confess('Password file does not exist');
- }
-
- my %key = $self->file_to_hash($pwfile);
-
- $self->get_master_password();
-
- $self->ui()->to_clipboard( $self->pw->decrypt( $key{hash}, $key{salt} ) );
-
- if ( $key{extra} ) {
- print $self->pw->decrypt( $key{extra}, $key{salt} );
- }
-
- return;
-}
-
-sub cmd_info {
- my ( $self, $name ) = @_;
-
- my $pwfile = $self->{xdg_data} . "/${name}";
-
- if ( not -e $pwfile ) {
- confess('Password file does not exist');
- }
-
- my %key = $self->file_to_hash($pwfile);
- $self->ui()->output( [ 'URL', $key{url} ], [ 'Login', $key{login} ], );
-
- return;
-}
-
-sub cmd_list {
- my ($self) = @_;
-
- my @files = read_dir( $self->{xdg_data} );
-
- for my $file ( sort @files ) {
- my %key = $self->file_to_hash( $self->{xdg_data} . "/${file}" );
- $self->ui->list(
- [ 'Account', $file ],
- [ 'Login', $key{login} ],
- [ 'URL', $key{url} ],
- );
- }
-
- return;
-}
-
-sub cmd_remove {
- my ( $self, $name ) = @_;
-
- my $pwfile = $self->{xdg_data} . "/${name}";
-
- if ( -e $pwfile ) {
- unlink($pwfile);
- }
- else {
- say STDERR 'File did not exist, so could not be removed';
- }
-
- return;
-}
-
1;
__END__
@@ -347,17 +187,6 @@ App::Raps2 - A Password safe
use App::Raps2;
my $raps2 = App::Raps2->new();
- my ($action, @args) = @ARGV;
-
- $raps2->sanity_check();
- $raps2->load_config();
-
- given ($action) {
- when ('add') { $raps2->cmd_add(@args) }
- when ('dump') { $raps2->cmd_dump(@args) }
- when ('get') { $raps2->cmd_get(@args) }
- when ('info') { $raps2->cmd_info(@args) }
- }
=head1 DESCRIPTION
@@ -394,7 +223,7 @@ with its key/value pairs.
Asks the user for the master passphrase.
-=item $raps2->pw_add(I<%data>)
+=item $raps2->pw_save(I<%data>)
Write an account as specified by I<data> to the store. Requires
B<get_master_password> to have been called before.
@@ -417,7 +246,7 @@ The following I<data> keys are possible:
=back
-=item $raps2->pw_get(B<file> => I<file> | B<name> => I<name>)
+=item $raps2->pw_load(B<file> => I<file> | B<name> => I<name>)
Loads a password from I<file> (or account I<name>), requires
B<get_master_password> to have been called before. Returns a hashref
@@ -455,34 +284,6 @@ Returns the App::Raps2::Password(3pm) object.
Returns the App::Raps2::UI(3pm) object.
-=item $raps2->cmd_add(I<$name>)
-
-Adds a new password file called $name.
-
-=item $raps2->cmd_dump(I<$account>)
-
-Dumps the content of I<account>
-
-=item $raps2->cmd_edit(I<$acount>)
-
-Edit I<account>.
-
-=item $raps2->cmd_get(I<$name>)
-
-Puts the password saved in $name into the X clipboard.
-
-=item $raps2->cmd_info(I<$name>)
-
-Prints unencrypted information about $name.
-
-=item $raps2->cmd_list()
-
-Lists all saved passwords and their logins and urls
-
-=item $raps2->cmd_remove(I<$name>)
-
-Remove (unlink) the account I<name>.
-
=back
=head1 DIAGNOSTICS