summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2011-05-13 12:46:01 +0200
committerDaniel Friesel <derf@finalrewind.org>2011-05-13 12:46:01 +0200
commite00c62bde48404379d25a8ce2ffc2ce212a811ee (patch)
treea7678171e402b03c86731d4bc00432b49cdf42f6
parent4149a04c9857873553b2b39feaa84181c2320813 (diff)
App::Raps2: Make create_salt and file_to_hash oop-ish as well
-rw-r--r--lib/App/Raps2.pm121
-rw-r--r--t/29-app-raps2.t9
2 files changed, 71 insertions, 59 deletions
diff --git a/lib/App/Raps2.pm b/lib/App/Raps2.pm
index 6b4929c..bc0bea4 100644
--- a/lib/App/Raps2.pm
+++ b/lib/App/Raps2.pm
@@ -27,33 +27,62 @@ B<App::Raps2> is the backend for B<raps2>, a simple commandline password safe.
=cut
-
use strict;
use warnings;
use autodie;
use 5.010;
-use base 'Exporter';
-
use App::Raps2::Password;
use App::Raps2::UI;
-use Carp q(confess);
+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);
-our @EXPORT_OK = ();
our $VERSION = '0.1';
=head1 METHODS
-=head2 create_salt()
+=over
+
+=item $raps2 = App::Raps2->new(%conf)
+
+Returns a new B<App::Raps2> object.
+
+Accepted configuration parameters are:
+
+=over
+
+=item B<cost> => I<int>
+
+B<cost> of key setup, passed on to App::Raps2::Password(3pm).
+
+=back
+
+=cut
+
+sub new {
+ my ($obj, %conf) = @_;
+ my $ref = {};
+
+ $ref->{'xdg_conf'} = config_home('raps2');
+ $ref->{'xdg_data'} = data_home('raps2');
+
+ $ref->{'ui'} = App::Raps2::UI->new();
+
+ $ref->{'default'} = \%conf;
+
+ return bless($ref, $obj);
+}
+
+=item $raps2->create_salt()
Returns a 16-character random salt for App::Raps2::Password(3pm).
=cut
sub create_salt {
+ my ($self) = @_;
my $salt = q{};
for (1 .. 16) {
@@ -63,7 +92,7 @@ sub create_salt {
return $salt;
}
-=head2 file_to_hash($file)
+=item $raps2->file_to_hash($file)
Reads $file (lines with key/value separated by whitespace) and returns a hash
with its key/value pairs.
@@ -71,7 +100,7 @@ with its key/value pairs.
=cut
sub file_to_hash {
- my ($file) = @_;
+ my ($self, $file) = @_;
my %ret;
for my $line (slurp($file)) {
@@ -86,37 +115,7 @@ sub file_to_hash {
return %ret;
}
-=head2 new(%conf)
-
-Returns a new B<App::Raps2> object.
-
-Accepted configuration parameters are:
-
-=over
-
-=item B<cost> => I<int>
-
-B<cost> of key setup, passed on to App::Raps2::Password(3pm).
-
-=back
-
-=cut
-
-sub new {
- my ($obj, %conf) = @_;
- my $ref = {};
-
- $ref->{'xdg_conf'} = config_home('raps2');
- $ref->{'xdg_data'} = data_home('raps2');
-
- $ref->{'ui'} = App::Raps2::UI->new();
-
- $ref->{'default'} = \%conf;
-
- return bless($ref, $obj);
-}
-
-=head2 ->sanity_check()
+=item $raps2->sanity_check()
Create working directories (~/.config/raps2 and ~/.local/share/raps2, or the
respective XDG environment variable contents), if they don't exist yet.
@@ -138,7 +137,7 @@ sub sanity_check {
return;
}
-=head2 ->get_master_password()
+=item $raps2->get_master_password()
Asks the user for the master passphrase.
@@ -157,7 +156,7 @@ sub get_master_password {
$self->{'pass'}->verify($self->{'master_hash'});
}
-=head2 ->create_config()
+=item $raps2->create_config()
Creates a default config and asks the user to set a master password.
@@ -166,7 +165,7 @@ Creates a default config and asks the user to set a master password.
sub create_config {
my ($self) = @_;
my $cost = 12;
- my $salt = create_salt();
+ my $salt = $self->create_salt();
my $pass = $self->ui()->read_pw('Master Password', 1);
$self->{'pass'} = App::Raps2::Password->new(
@@ -184,7 +183,7 @@ sub create_config {
);
}
-=head2 ->load_config()
+=item $raps2->load_config()
Load config
@@ -192,13 +191,13 @@ Load config
sub load_config {
my ($self) = @_;
- my %cfg = file_to_hash($self->{'xdg_conf'} . '/password');
+ my %cfg = $self->file_to_hash($self->{'xdg_conf'} . '/password');
$self->{'master_hash'} = $cfg{'hash'};
$self->{'master_salt'} = $cfg{'salt'};
$self->{'default'}->{'cost'} //= $cfg{'cost'};
}
-=head2 ->ui()
+=item $raps2->ui()
Returns the App::Raps2::UI(3pm) object.
@@ -209,7 +208,7 @@ sub ui {
return $self->{'ui'};
}
-=head2 ->cmd_add($name)
+=item $raps2->cmd_add($name)
Adds a new password file called $name.
@@ -225,7 +224,7 @@ sub cmd_add {
$self->get_master_password();
- my $salt = create_salt();
+ my $salt = $self->create_salt();
my $url = $self->ui()->read_line('URL');
my $login = $self->ui()->read_line('Login');
my $pass = $self->ui()->read_pw('Password', 1);
@@ -250,7 +249,7 @@ sub cmd_add {
);
}
-=head2 ->cmd_dump($name)
+=item $raps2->cmd_dump($name)
Dumps the content of $name.
@@ -264,7 +263,7 @@ sub cmd_dump {
confess('Password file does not exist');
}
- my %key = file_to_hash($pwfile);
+ my %key = $self->file_to_hash($pwfile);
$self->get_master_password();
@@ -280,7 +279,7 @@ sub cmd_dump {
}
}
-=head2 ->cmd_get($name)
+=item $raps2->cmd_get($name)
Puts the password saved in $name into the X clipboard.
@@ -294,7 +293,7 @@ sub cmd_get {
confess('Password file does not exist');
}
- my %key = file_to_hash($pwfile);
+ my %key = $self->file_to_hash($pwfile);
$self->get_master_password();
@@ -307,7 +306,7 @@ sub cmd_get {
}
}
-=head2 ->cmd_info($name)
+=item $raps2->cmd_info($name)
Prints unencrypted information about $name.
@@ -321,14 +320,14 @@ sub cmd_info {
confess('Password file does not exist');
}
- my %key = file_to_hash($pwfile);
+ my %key = $self->file_to_hash($pwfile);
$self->ui()->output(
['URL', $key{'url'}],
['Login', $key{'login'}],
);
}
-=head2 ->cmd_list()
+=item $raps2->cmd_list()
Lists all saved passwords and their logins and urls
@@ -338,7 +337,7 @@ sub cmd_list {
my ($self) = @_;
for my $file (read_dir($self->{xdg_data})) {
- my %key = file_to_hash($self->{xdg_data} . "/${file}");
+ my %key = $self->file_to_hash($self->{xdg_data} . "/${file}");
$self->ui->list(
['Account', $file],
['Login', $key{login}],
@@ -346,3 +345,15 @@ sub cmd_list {
);
}
}
+
+=head1 DEPENDENCIES
+
+L<App::Raps2::Password>, L<App::Raps2::UI>, L<File::BaseDir>, L<File::Slurp>.
+
+=head1 AUTHOR
+
+Copyright (C) 2011 by Daniel Friesel E<lt>derf@finalrewind.orgE<gt>
+
+=head1 LICENSE
+
+ 0. You just DO WHAT THE FUCK YOU WANT TO.
diff --git a/t/29-app-raps2.t b/t/29-app-raps2.t
index cbb170e..ceaaf51 100644
--- a/t/29-app-raps2.t
+++ b/t/29-app-raps2.t
@@ -8,13 +8,14 @@ use Test::Fatal;
use_ok('App::Raps2');
-is(length(App::Raps2::create_salt()), 16, 'create_salt: correct length');
+my $r2 = App::Raps2->new();
+isa_ok($r2, 'App::Raps2');
+
+is(length($r2->create_salt()), 16, 'create_salt: correct length');
is_deeply(
- { App::Raps2::file_to_hash('t/in/hash') },
+ { $r2->file_to_hash('t/in/hash') },
{ key => 'value', otherkey => 'othervalue' },
'file_to_hash works',
);
-my $r2 = App::Raps2->new();
-isa_ok($r2, 'App::Raps2');