From 391929448038a478f23cfa28db528e1f4a1e60d9 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Tue, 17 May 2011 14:20:14 +0200 Subject: Add "raps2 edit" --- Changelog | 1 + bin/raps2 | 9 +++++++ lib/App/Raps2.pm | 76 +++++++++++++++++++++++++++++++++++++++++++++-------- lib/App/Raps2/UI.pm | 4 +-- 4 files changed, 76 insertions(+), 14 deletions(-) diff --git a/Changelog b/Changelog index 4936b47..9aa5ac1 100644 --- a/Changelog +++ b/Changelog @@ -1,6 +1,7 @@ git HEAD * Terminal input is now read via Term::ReadLine (included in perl core) + * New commands: "raps2 del " and "raps2 edit " raps2 0.2 - Fri May 13 2011 diff --git a/bin/raps2 b/bin/raps2 index 3847457..4c35592 100755 --- a/bin/raps2 +++ b/bin/raps2 @@ -21,6 +21,7 @@ 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) } @@ -50,6 +51,9 @@ accountname >> and paste the corresponding password into whatever application requires it. B will automatically initialize its store when used for the first time. +Supported metadata are "URL", "Login" and the multiline "Extra" field. URL +and Login will be saved as plaintext, Extra is encrypted like the password. + =head1 ACTIONS =over @@ -69,6 +73,11 @@ Remove I from the store. Dump everything saved for I, including the clear-text password, to stdout. +=item B I + +Edit saved data for I. Note that editing the multiline "extra" field +is not yet possible. + =item B I Decrypt I's password and store it in the X Clipboard. Note that it diff --git a/lib/App/Raps2.pm b/lib/App/Raps2.pm index 32e46f6..9d43364 100644 --- a/lib/App/Raps2.pm +++ b/lib/App/Raps2.pm @@ -197,6 +197,17 @@ sub load_config { $self->{default}->{cost} //= $cfg{cost}; } +=item $raps2->pw() + +Returns the App::Raps2::Password(3pm) object. + +=cut + +sub pw { + my ($self) = @_; + return $self->{pass}; +} + =item $raps2->ui() Returns the App::Raps2::UI(3pm) object. @@ -230,11 +241,11 @@ sub cmd_add { my $pass = $self->ui->read_pw('Password', 1); my $extra = $self->ui->read_multiline('Additional content'); - $self->{pass}->salt($salt); - my $pass_hash = $self->{pass}->encrypt($pass); + $self->pw->salt($salt); + my $pass_hash = $self->pw->encrypt($pass); my $extra_hash = ( $extra ? - $self->{pass}->encrypt($extra) : + $self->pw->encrypt($extra) : q{} ); @@ -249,9 +260,9 @@ sub cmd_add { ); } -=item $raps2->cmd_dump(I<$name>) +=item $raps2->cmd_dump(I<$account>) -Dumps the content of $name. +Dumps the content of I =cut @@ -267,16 +278,59 @@ sub cmd_dump { $self->get_master_password(); - $self->{pass}->salt($key{salt}); + $self->pw->salt($key{salt}); $self->ui()->output( ['URL', $key{url}], ['Login', $key{login}], - ['Password', $self->{pass}->decrypt($key{hash})], + ['Password', $self->pw->decrypt($key{hash})], ); if ($key{extra}) { - print $self->{pass}->decrypt($key{extra}); + print $self->pw->decrypt($key{extra}); + } +} + +=item $raps2->cmd_edit(I<$acount>) + +Edit I. + +=cut + +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(); + $self->pw->salt($key{salt}); + + 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); + } + else { + $pass_hash = $key{hash}; } + + write_file( + $pwfile, + "url ${url}\n", + "login ${login}\n", + "salt ${salt}\n", + "hash ${pass_hash}\n", + "extra ${extra}\n", + ); } =item $raps2->cmd_get(I<$name>) @@ -297,12 +351,12 @@ sub cmd_get { $self->get_master_password(); - $self->{pass}->salt($key{salt}); + $self->pw->salt($key{salt}); - $self->ui()->to_clipboard($self->{pass}->decrypt($key{hash})); + $self->ui()->to_clipboard($self->pw->decrypt($key{hash})); if ($key{extra}) { - print $self->{pass}->decrypt($key{extra}) + print $self->pw->decrypt($key{extra}) } } diff --git a/lib/App/Raps2/UI.pm b/lib/App/Raps2/UI.pm index 089290b..9807f52 100644 --- a/lib/App/Raps2/UI.pm +++ b/lib/App/Raps2/UI.pm @@ -32,9 +32,7 @@ sub list { sub read_line { my ($self, $str, $pre) = @_; - $pre //= q{}; - - my $input = $self->{term_readline}->readline("${str}: ${pre}"); + my $input = $self->{term_readline}->readline("${str}: ", $pre); return $input; } -- cgit v1.2.3