File Coverage

lib/App/Tel/Passwd/KeePass.pm
Criterion Covered Total %
statement 27 36 75.0
branch 4 8 50.0
condition 4 8 50.0
subroutine 5 6 83.3
pod 2 2 100.0
total 42 60 70.0


line stmt bran cond sub pod time code
1             package App::Tel::Passwd::KeePass;
2              
3             =head1 name
4              
5             App::Tel::Passwd::KeePass - Passwd module for KeePass
6              
7             =cut
8              
9 2     2   1650 use strict;
  2         3  
  2         44  
10 2     2   6 use warnings;
  2         2  
  2         36  
11 2     2   6 use Module::Load;
  2         2  
  2         7  
12 2     2   64 use Carp;
  2         3  
  2         491  
13              
14             our $_debug = 0;
15              
16             =head1 METHODS
17              
18             =head2 new
19              
20             my $passwd = App::Tel::Passwd::KeePass->new( file => $filename, passwd => $password );
21              
22             Initializes a new passwd object. This will return a Passwd::KeePass Object if the module
23             exists and return undef if it doesn't.
24              
25             Requires filename and password for the file.
26              
27             =cut
28              
29             sub new {
30 4     4 1 2646 my $proto = shift;
31 4   33     17 my $class = ref($proto) || $proto;
32 4         11 my %args = @_;
33 4         10 my $self = { debug => $_debug,
34             %args
35             };
36              
37 4 100 66     62 if (!defined($self->{file}) || ! -r $self->{file} ) {
38 1   50     5 $self->{file} ||= '';
39 1         12 croak "$class: Can't read file $self->{file}";
40             }
41              
42              
43 3         4 $self->{keepass} = eval {
44 3         8 load File::KeePass;
45 3         173 return File::KeePass->new();
46             };
47              
48 3 50       24 if ($@) {
49 0         0 croak $@;
50             }
51              
52             # load failure on bad password or bad filename
53 3         3 my $k = eval { $self->{keepass}->load_db($self->{file}, $self->{passwd}); };
  3         8  
54 3 50       27742 if ($@) {
55 3         41 croak $@;
56             }
57 0           $k->unlock;
58 0           $self->{k} = $k;
59              
60 0           return bless( $self, $class );
61             }
62              
63             =head2 passwd
64              
65             $passwd->passwd($entry);
66              
67             This takes the entry for a key database and returns the password. It returns
68             a blank string if the entry wasn't found.
69              
70             =cut
71              
72             sub passwd {
73 0     0 1   my $self = shift;
74 0           my $k = $self->{k};
75 0           my $entry = shift;
76              
77 0           my $e = $k->find_entry({title => $entry});
78 0 0         return !defined($e->{'password'}) ? '' : $e->{'password'};
79             }
80              
81             1;