| 1 | package WWW::KeePassHttp; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 8 | ▶ | use 5.012; # //, strict, s//r | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use MIME::Base64; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 8 | ▶ | use Crypt::Mode::CBC; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 8 | ▶ | use HTTP::Tiny; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 8 | ▶ | use JSON; # will use JSON::XS when available | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 8 | ▶ | use Time::HiRes qw/gettimeofday sleep/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 8 | ▶ | use MIME::Base64; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 8 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 8 | our $VERSION = '0.010'; # rrr.mmmsss : rrr is major revision; mmm is minor revision; sss is sub-revision (new feature path or bugfix); optionally use _sss instead, for alpha sub-releases | |||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 8 | my $dumpfn; | |||||||||||||||||||||||||||||||||||||||||
| 16 | BEGIN { | ||||||||||||||||||||||||||||||||||||||||||
| 17 | 7 | ▶ | $dumpfn = sub { JSON->new->utf8->pretty->encode($_[0]) } # hidden from podcheckers and external namespace | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 8 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | WWW::KeePassHttp - Interface with KeePass PasswordSafe through the KeePassHttp plugin | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | use WWW::KeePassHttp; | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | my $kph = WWW::KeePassHttp->new(Key => $key); | ||||||||||||||||||||||||||||||||||||||||||
| 31 | $kph->associate() unless $kph->test_associate(); | ||||||||||||||||||||||||||||||||||||||||||
| 32 | my $entries = $kph->get_logins($search_string); | ||||||||||||||||||||||||||||||||||||||||||
| 33 | print "$_ => $entries->[0]{$_}\n" for qw/Name Login Password/; | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | Interface with KeePass PasswordSafe through the KeePassHttp plugin. Allows reading entries based on URL or TITLE. Maybe will allow creating a new entry as well. | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | =head2 REQUIREMENTS | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | You need to have KeePass (or compatible) on your system, with the KeePassHttp plugin installed. | ||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | =head1 INTERFACE | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | =head2 CONSTRUCTOR AND CONFIGURATION | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | =item new | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | my $kph = WWW::KeePassHttp->new( Key => $key, %options); | ||||||||||||||||||||||||||||||||||||||||||
| 52 | my $kph = WWW::KeePassHttp->new( Key => $key, keep_alive => 0, %options); | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | Creates a new KeePassHttp connection, and sets up the AES encryption. | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | The C<Key =E<gt> $key> is required; pass in a string of 32 octets that | ||||||||||||||||||||||||||||||||||||||||||
| 57 | represent a 256-bit key value. If you have your key as 64 hex nibbles, | ||||||||||||||||||||||||||||||||||||||||||
| 58 | then use C<$key = pack 'H*', $hexnibbles;> to convert it to the value. | ||||||||||||||||||||||||||||||||||||||||||
| 59 | If you have your key as a Base64 string, use | ||||||||||||||||||||||||||||||||||||||||||
| 60 | C<$key = decode_base64($base64string);> to convert it to the value. | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | There is also a C<keep_alive> option, which will tell the HTTP user | ||||||||||||||||||||||||||||||||||||||||||
| 63 | agent to keep the connection alive when the option is set to C<1> (or | ||||||||||||||||||||||||||||||||||||||||||
| 64 | when it's not specified); setting the option to a C<0> will disable | ||||||||||||||||||||||||||||||||||||||||||
| 65 | that feature of the user agent. | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | The C<%options> share the same name and purposes with the | ||||||||||||||||||||||||||||||||||||||||||
| 68 | configuration methods that follow, and can be individually specified in | ||||||||||||||||||||||||||||||||||||||||||
| 69 | the constructor as key/value pairs, or passing in an C<%options> hash. | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | sub new | ||||||||||||||||||||||||||||||||||||||||||
| 74 | { | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 21 | ▶ | my ($class, %opts) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 21 | my $self = bless {}, $class; | |||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | # user agent and URL | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 21 | ▶ | $opts{keep_alive} //= 1; # default to keep_alive | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts{"keep_alive"} //= 1
MC/DC100%1/1
$opts{"keep_alive"} //= 1
$opts{"keep_alive"}
Truth table100%2/2
A
A= $opts{"keep_alive"}
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 21 | $self->{ua} = HTTP::Tiny->new(keep_alive => $opts{keep_alive} ); | |||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 21 | ▶ | $self->{request_base} = $opts{request_base} // 'http://localhost'; # default to localhost | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts{"request_base"} // "http://localhost"
MC/DC100%1/1
$opts{"request_base"} // "http://localhost"
$opts{"request_base"}
Truth table100%2/2
A
A= $opts{"request_base"}
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 21 | ▶ | $self->{request_port} = $opts{request_port} // 19455; # default to 19455 | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts{"request_port"} // 19455
MC/DC100%1/1
$opts{"request_port"} // 19455
$opts{"request_port"}
Truth table100%2/2
A
A= $opts{"request_port"}
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 21 | $self->{request_url} = $self->{request_base} . ':' . $self->{request_port}; | |||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | # encryption object | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 21 | $self->{cbc} = Crypt::Mode::CBC->new('AES'); | |||||||||||||||||||||||||||||||||||||||||
| 88 | 21 | $self->{key} = $opts{Key}; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 21 | for($self->{key}) { | |||||||||||||||||||||||||||||||||||||||||
| 90 | 21 | ▶ | croak "256-bit AES key is required" unless defined $_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 19 | ▶ | last if length($_) == 32; # a 32-octet string is assumed to be a valid key | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 5 | chomp; | |||||||||||||||||||||||||||||||||||||||||
| 93 | 5 | ▶ | croak "256-bit AES key must be in octets, not hex nibbles" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | if /^(0x)?[[:xdigit:]]{64}$/; | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 3 | ▶ | croak "256-bit AES key must be in octets, not in base64" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | if length($_) == 44; | ||||||||||||||||||||||||||||||||||||||||||
| 97 | 1 | croak "Key not recognized as 256-bit AES"; | |||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | 14 | $self->{key64} = encode_base64($self->{key}, ''); | |||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | # appid | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 14 | ▶ | $self->{appid} = $opts{appid} // 'WWW::KeePassHttp'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts{"appid"} // "WWW::KeePassHttp"
MC/DC100%1/1
$opts{"appid"} // "WWW::KeePassHttp"
$opts{"appid"}
Truth table100%2/2
A
A= $opts{"appid"}
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | 14 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | =item appid | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | %options = ( ..., appid => 'name of your app', ... ); | ||||||||||||||||||||||||||||||||||||||||||
| 110 | or | ||||||||||||||||||||||||||||||||||||||||||
| 111 | $kph->appid('name of your app'); | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | Changes the appid, which is the name that is used to map your | ||||||||||||||||||||||||||||||||||||||||||
| 114 | application with the stored key in the KeePassHttp settings in | ||||||||||||||||||||||||||||||||||||||||||
| 115 | KeePass. | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | If not defined in the initial options or via this method, | ||||||||||||||||||||||||||||||||||||||||||
| 118 | the module will use a default appid of C<WWW::KeePassHttp>. | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | sub appid | ||||||||||||||||||||||||||||||||||||||||||
| 123 | { | ||||||||||||||||||||||||||||||||||||||||||
| 124 | 2 | ▶ | my ($self, $val) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
appid: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 2 | ▶ | $self->{appid} = $val if defined $val; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 2 | return $self->{appid}; | |||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | =item request_base | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | %options = ( ..., request_base => 'localhost', ... ); | ||||||||||||||||||||||||||||||||||||||||||
| 132 | or | ||||||||||||||||||||||||||||||||||||||||||
| 133 | $kph->request_base('127.0.0.1'); | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | Changes the protocol and host: the KeePassHttp plugin defaults to C<http://localhost>, but can be configured differently, so you will need to make your object match your plugin settings. | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | sub request_base | ||||||||||||||||||||||||||||||||||||||||||
| 140 | { | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 2 | ▶ | my ($self, $val) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
request_base: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | 2 | ▶ | $self->{request_base} = $val if defined $val; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 2 | $self->{request_url} = $self->{request_base} . ':' . $self->{request_port}; | |||||||||||||||||||||||||||||||||||||||||
| 144 | 2 | return $self->{request_base}; | |||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | =item request_port | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | %options = ( ..., request_port => 19455, ... ); | ||||||||||||||||||||||||||||||||||||||||||
| 150 | or | ||||||||||||||||||||||||||||||||||||||||||
| 151 | $kph->request_port(19455); | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | Changes the port: the KeePassHttp plugin defaults to port 19455, but can be configured differently, so you will need to make your object match your plugin settings. | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | sub request_port | ||||||||||||||||||||||||||||||||||||||||||
| 158 | { | ||||||||||||||||||||||||||||||||||||||||||
| 159 | 2 | ▶ | my ($self, $val) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
request_port: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | 2 | ▶ | $self->{request_port} = $val if defined $val; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | 2 | $self->{request_url} = $self->{request_base} . ':' . $self->{request_port}; | |||||||||||||||||||||||||||||||||||||||||
| 162 | 2 | return $self->{request_port}; | |||||||||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | =for comment END OF CONSTRUCTOR AND CONFIGURATION | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | =head2 USER INTERFACE | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | These methods implement the L<KeePassHttp plugin's commmunication protocol|https://github.com/pfn/keepasshttp/#a-little-deeper-into-protocol>, with one method for each RequestType. | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | =item test_associate | ||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | $kph->associate unless $kph->test_associate(); | ||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | Sends the C<test-assocate> request to the KeePassHttp server, | ||||||||||||||||||||||||||||||||||||||||||
| 180 | which is used to see whether or not your application has been | ||||||||||||||||||||||||||||||||||||||||||
| 181 | associated with the KeePassHttp plugin or not. Returns a true | ||||||||||||||||||||||||||||||||||||||||||
| 182 | value if your application is already associated, or a false | ||||||||||||||||||||||||||||||||||||||||||
| 183 | value otherwise. | ||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | sub test_associate | ||||||||||||||||||||||||||||||||||||||||||
| 188 | { | ||||||||||||||||||||||||||||||||||||||||||
| 189 | 2 | ▶ | my ($self, %args) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
test_associate: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 190 | 2 | my $content = $self->request('test-associate', %args); | |||||||||||||||||||||||||||||||||||||||||
| 191 | 2 | return $content->{Success}; | |||||||||||||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||
| 194 | =item associate | ||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | $kph->associate unless $kph->test_associate(); | ||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | Sends the C<assocate> request to the KeePassHttp server, | ||||||||||||||||||||||||||||||||||||||||||
| 199 | which is used to give your application's key to the KeePassHttp | ||||||||||||||||||||||||||||||||||||||||||
| 200 | plugin. | ||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | When this request is received, KeePass will pop up a dialog | ||||||||||||||||||||||||||||||||||||||||||
| 203 | asking for a name -- this name should match the C<appid> value | ||||||||||||||||||||||||||||||||||||||||||
| 204 | that you defined for the C<$kph> instance. All requests sent | ||||||||||||||||||||||||||||||||||||||||||
| 205 | to the plugin will include this C<appid> so that KeePassHttp can | ||||||||||||||||||||||||||||||||||||||||||
| 206 | look up your application's key, so it must match exactly. | ||||||||||||||||||||||||||||||||||||||||||
| 207 | As per the C<KeePassHttp plugin docs|https://github.com/pfn/keepasshttp/>, | ||||||||||||||||||||||||||||||||||||||||||
| 208 | the server saves your application's key in the C<KeePassHttp Settings> | ||||||||||||||||||||||||||||||||||||||||||
| 209 | entry, in the B<Advanced E<gt> String Fields> with a name of | ||||||||||||||||||||||||||||||||||||||||||
| 210 | C<AES Key: XXXX>, where C<XXXX> is the name you type in the dialog | ||||||||||||||||||||||||||||||||||||||||||
| 211 | box (which needs to match your C<appid>). | ||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||
| 213 | B<Please note>: this C<associate> communication is insecure, | ||||||||||||||||||||||||||||||||||||||||||
| 214 | since KeePassHttp plugin is not using HTTPS. Every other | ||||||||||||||||||||||||||||||||||||||||||
| 215 | communication between your application and the plugin uses the | ||||||||||||||||||||||||||||||||||||||||||
| 216 | key (which both your application and the plugin know) to | ||||||||||||||||||||||||||||||||||||||||||
| 217 | encrypt the critical data (usernames, passwords, titles, etc), | ||||||||||||||||||||||||||||||||||||||||||
| 218 | and is thus secure; | ||||||||||||||||||||||||||||||||||||||||||
| 219 | but the C<associate> interaction, because it happens before | ||||||||||||||||||||||||||||||||||||||||||
| 220 | the plugin has your key, by its nature cannot be encrypted by | ||||||||||||||||||||||||||||||||||||||||||
| 221 | that key, so it sends the encoded key I<unencrypted>. If this | ||||||||||||||||||||||||||||||||||||||||||
| 222 | worries you, I suggest that you manually insert the key: do an | ||||||||||||||||||||||||||||||||||||||||||
| 223 | C<assocate> once with a dummy key, then manually overwrite the | ||||||||||||||||||||||||||||||||||||||||||
| 224 | encoded key that it stores with the encoded version of your real | ||||||||||||||||||||||||||||||||||||||||||
| 225 | key. (This limitation is due to the design of the KeePassHttp | ||||||||||||||||||||||||||||||||||||||||||
| 226 | plugin and its protocol for the C<associate> command, not due | ||||||||||||||||||||||||||||||||||||||||||
| 227 | to the wrapper around that protocol that this module implements.) | ||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||
| 229 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||
| 231 | sub associate | ||||||||||||||||||||||||||||||||||||||||||
| 232 | { | ||||||||||||||||||||||||||||||||||||||||||
| 233 | 3 | ▶ | my ($self, %args) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
associate: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 234 | 3 | my $content = $self->request('associate', Key64 => $self->{key64}, %args); | |||||||||||||||||||||||||||||||||||||||||
| 235 | 3 | ▶ | croak ("Wrong ID: ", $dumpfn->( { wrong_id => $content } )) unless $self->{appid} eq ($content->{Id}//'<undef>'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$content->{"Id"} // "<undef>"
MC/DC100%1/1
$content->{"Id"} // "<undef>"
$content->{"Id"}
Truth table100%2/2
A
A= $content->{"Id"}
| |||||||||||||||||||||||||||||||||||||||||||
| 236 | 1 | return $content; | |||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | =item get_logins | ||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||
| 241 | my $entries = $kph->get_logins($search_string); | ||||||||||||||||||||||||||||||||||||||||||
| 242 | print "$_ => $entries->[0]{$_}\n" for qw/Name Login Password/; | ||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||
| 244 | Sends the C<get-logins> request, which returns the Name, | ||||||||||||||||||||||||||||||||||||||||||
| 245 | Login, and Password for each of the matching entries. | ||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||
| 247 | C<$entries> is an AoH structure: it is an array reference, | ||||||||||||||||||||||||||||||||||||||||||
| 248 | and each element of that array is a hash reference; each | ||||||||||||||||||||||||||||||||||||||||||
| 249 | referenced hash includes Name, Login, and Password entries. | ||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | The rules for the matching of the search string are defined in the | ||||||||||||||||||||||||||||||||||||||||||
| 252 | L<KeePassHttp plugin documentation|https://github.com/pfn/keepasshttp/>. | ||||||||||||||||||||||||||||||||||||||||||
| 253 | But, in brief, it will do a fuzzy match on the URL, and an exact match | ||||||||||||||||||||||||||||||||||||||||||
| 254 | on the entry title. (The plugin was designed to be used for browser plugins | ||||||||||||||||||||||||||||||||||||||||||
| 255 | to request passwords for URLs from KeePass, hence its focus on URLs.) | ||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||
| 257 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||
| 259 | sub get_logins | ||||||||||||||||||||||||||||||||||||||||||
| 260 | { | ||||||||||||||||||||||||||||||||||||||||||
| 261 | 3 | ▶ | my ($self, $search_term, %args) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_logins: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 262 | 3 | $args{Url} = $search_term; | |||||||||||||||||||||||||||||||||||||||||
| 263 | 3 | ▶ | $args{SubmitUrl} = $self->{appid} unless exists $args{SubmitUrl}; # "SubmitUrl" is actually the name of the requestor; in the browser->keePassHttp interface, the requestor is the website requesting the password; but here, I am using it as the app identifier | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 264 | 3 | my $content = $self->request('get-logins', Url => $search_term, %args); | |||||||||||||||||||||||||||||||||||||||||
| 265 | 3 | ▶ | return [] unless $content->{Count}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 266 | 2 | my $entries = $content->{Entries}; | |||||||||||||||||||||||||||||||||||||||||
| 267 | 2 | for my $entry ( @$entries ) { | |||||||||||||||||||||||||||||||||||||||||
| 268 | 2 | for my $k ( sort keys %$entry ) { | |||||||||||||||||||||||||||||||||||||||||
| 269 | 8 | $entry->{$k} = $self->{cbc}->decrypt( decode_base64($entry->{$k}), $self->{key}, decode_base64($content->{Nonce})); | |||||||||||||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||||||||||||||||
| 272 | #$dumpfn->( { Entries => $entries } ); | ||||||||||||||||||||||||||||||||||||||||||
| 273 | 2 | return $entries; | |||||||||||||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||
| 276 | =item get_logins_count | ||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||
| 278 | my $count = $kph->get_logins_count($search_string); | ||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||
| 280 | Sends the C<get-logins> request, which returns a count of | ||||||||||||||||||||||||||||||||||||||||||
| 281 | the number of matches for the search string. | ||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | The rules for the matching of the search string are defined in the | ||||||||||||||||||||||||||||||||||||||||||
| 284 | L<KeePassHttp plugin documentation|https://github.com/pfn/keepasshttp/>. | ||||||||||||||||||||||||||||||||||||||||||
| 285 | But, in brief, it will do a fuzzy match on the URL, and an exact match | ||||||||||||||||||||||||||||||||||||||||||
| 286 | on the entry title. (The plugin was designed to be used for browser plugins | ||||||||||||||||||||||||||||||||||||||||||
| 287 | to request passwords for URLs from KeePass, hence its focus on URLs.) | ||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||
| 289 | This method is useful when the fuzzy-URL-match might match a large | ||||||||||||||||||||||||||||||||||||||||||
| 290 | number of entries in the database; if after seeing this count, you | ||||||||||||||||||||||||||||||||||||||||||
| 291 | would rather refine your search instead of requesting that many entries, | ||||||||||||||||||||||||||||||||||||||||||
| 292 | this method enables knowing that right away, rather than after you | ||||||||||||||||||||||||||||||||||||||||||
| 293 | accidentally matched virtually every entry in your database by searching | ||||||||||||||||||||||||||||||||||||||||||
| 294 | for C<www>. | ||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||
| 296 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | sub get_logins_count | ||||||||||||||||||||||||||||||||||||||||||
| 299 | { | ||||||||||||||||||||||||||||||||||||||||||
| 300 | 2 | ▶ | my ($self, $search_term, %args) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_logins_count: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 301 | 2 | $args{Url} = $search_term; | |||||||||||||||||||||||||||||||||||||||||
| 302 | 2 | ▶ | $args{SubmitUrl} = $self->{appid} unless exists $args{SubmitUrl}; # "SubmitUrl" is actually the name of the requestor; in the browser->keePassHttp interface, the requestor is the website requesting the password; but here, I am using it as the app identifier | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 303 | 2 | my $content = $self->request('get-logins-count', Url => $search_term, %args); | |||||||||||||||||||||||||||||||||||||||||
| 304 | 2 | return $content->{Count}; | |||||||||||||||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||
| 308 | =item set_login | ||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||
| 310 | $kph->set_login( Login => $username, Url => $url_and_title, Password => $password ); | ||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||
| 312 | Sends the C<set-login> request, which adds a new entry to your | ||||||||||||||||||||||||||||||||||||||||||
| 313 | KeePass database, in the "KeePassHttp Passwords" group (folder). | ||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||
| 315 | As far as I know, the plugin doesn't allow choosing a different group | ||||||||||||||||||||||||||||||||||||||||||
| 316 | for your entry. The plugin uses the URL that you supply as both the | ||||||||||||||||||||||||||||||||||||||||||
| 317 | entry title and the URL field in that entry. (Once again, the plugin | ||||||||||||||||||||||||||||||||||||||||||
| 318 | was designed around browser password needs, and thus is URL-focused). | ||||||||||||||||||||||||||||||||||||||||||
| 319 | I don't know if that's a deficiency in the plugin's implementation, | ||||||||||||||||||||||||||||||||||||||||||
| 320 | or just its documentation, or my interpretation of that documentation. | ||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||
| 322 | The arguments to the method define the C<Login> (username), C<Url> (for | ||||||||||||||||||||||||||||||||||||||||||
| 323 | entry title and URL field), and C<Password> (secret value) for the new | ||||||||||||||||||||||||||||||||||||||||||
| 324 | entry. All three of those parameters are required by the protocol, and | ||||||||||||||||||||||||||||||||||||||||||
| 325 | thus by this method. | ||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||
| 327 | If you would prefer not to give one or more of those parameters a value, | ||||||||||||||||||||||||||||||||||||||||||
| 328 | just pass an empty string. You could afterword then manually access | ||||||||||||||||||||||||||||||||||||||||||
| 329 | your KeePass database and edit the entry yourself. | ||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||
| 331 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||
| 333 | sub set_login | ||||||||||||||||||||||||||||||||||||||||||
| 334 | { | ||||||||||||||||||||||||||||||||||||||||||
| 335 | 4 | ▶ | my ($self, %args) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_login: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 336 | 4 | ▶ | croak "set_login(): missing Login parameter" unless defined $args{Login}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 337 | 3 | ▶ | croak "set_login(): missing Url parameter" unless defined $args{Url}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 338 | 2 | ▶ | croak "set_login(): missing Password parameter" unless defined $args{Password}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 339 | 1 | my $content = $self->request('set-login', %args); | |||||||||||||||||||||||||||||||||||||||||
| 340 | 1 | return $content->{Success}; | |||||||||||||||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||
| 344 | =item request | ||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||
| 346 | my $results = $kph->request( $type, %options ); | ||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||
| 348 | This is the generic method for making a request of the | ||||||||||||||||||||||||||||||||||||||||||
| 349 | KeePassHttp plugin. In general, other methods should handle | ||||||||||||||||||||||||||||||||||||||||||
| 350 | most requests. However, maybe a new method has been exposed | ||||||||||||||||||||||||||||||||||||||||||
| 351 | in the plugin but not yet implemented here, so you can use | ||||||||||||||||||||||||||||||||||||||||||
| 352 | this method for handling that. | ||||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||
| 354 | The C<$type> indicates the RequestType, which include | ||||||||||||||||||||||||||||||||||||||||||
| 355 | C<test-associate>, C<associate>, C<get-logins>, | ||||||||||||||||||||||||||||||||||||||||||
| 356 | C<get-logins-count>, and C<set-login>. | ||||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||
| 358 | This method automatically fills out the RequestType, TriggerUnlock, Id, Nonce, and Verifier parameters. If your RequestType requires | ||||||||||||||||||||||||||||||||||||||||||
| 359 | any other parameters, add them to the C<%options>. | ||||||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||||||
| 361 | It then encodes the request into the JSON payload, and | ||||||||||||||||||||||||||||||||||||||||||
| 362 | sends that request to the KeePassHttp plugin, and gets the response, | ||||||||||||||||||||||||||||||||||||||||||
| 363 | decoding the JSON content back into a Perl hashref. It verifies that | ||||||||||||||||||||||||||||||||||||||||||
| 364 | the response's Nonce and Verifier parameters are appropriate for the | ||||||||||||||||||||||||||||||||||||||||||
| 365 | communication channel, to make sure communications from the plugin | ||||||||||||||||||||||||||||||||||||||||||
| 366 | are properly encrypted. | ||||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||
| 368 | Returns the hashref decoded from the JSON | ||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||
| 370 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||
| 372 | sub request { | ||||||||||||||||||||||||||||||||||||||||||
| 373 | 18 | ▶ | my ($self, $type, %params) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
request: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 374 | 18 | my ($iv, $nonce) = generate_nonce(); | |||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||
| 376 | #print STDERR "request($type):\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||
| 378 | # these are required in every request | ||||||||||||||||||||||||||||||||||||||||||
| 379 | my %request = ( | ||||||||||||||||||||||||||||||||||||||||||
| 380 | RequestType => $type, | ||||||||||||||||||||||||||||||||||||||||||
| 381 | TriggerUnlock => JSON::true, # was intended for TRUE to request that KeePass unlock, but that doesn't actually happen | ||||||||||||||||||||||||||||||||||||||||||
| 382 | Id => $self->{appid}, | ||||||||||||||||||||||||||||||||||||||||||
| 383 | Nonce => $nonce, | ||||||||||||||||||||||||||||||||||||||||||
| 384 | 18 | Verifier => encode_base64($self->{cbc}->encrypt($nonce, $self->{key}, $iv), ''), | |||||||||||||||||||||||||||||||||||||||||
| 385 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||
| 387 | # don't want to encrypt the key during an association request | ||||||||||||||||||||||||||||||||||||||||||
| 388 | 18 | delete $params{Key}; # only allow Key64 | |||||||||||||||||||||||||||||||||||||||||
| 389 | 18 | ▶ | $request{Key} = delete $params{Key64} if( exists $params{Key64} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||
| 391 | # encrypt all remaining parameter values | ||||||||||||||||||||||||||||||||||||||||||
| 392 | 18 | ▶ | while(my ($k,$v) = each %params) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 393 | 13 | $request{$k} = encode_base64($self->{cbc}->encrypt($v, $self->{key}, $iv), ''); | |||||||||||||||||||||||||||||||||||||||||
| 394 | } | ||||||||||||||||||||||||||||||||||||||||||
| 395 | #$dumpfn->({final_request => \%request}); | ||||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||
| 397 | # send the request | ||||||||||||||||||||||||||||||||||||||||||
| 398 | 18 | my $response = $self->{ua}->get($self->{request_url}, {content=> encode_json \%request}); | |||||||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||||||||
| 400 | # error checking | ||||||||||||||||||||||||||||||||||||||||||
| 401 | 18 | ▶ | croak $dumpfn->( { request_error => $response } ) unless $response->{success}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 402 | 17 | ▶ | croak $dumpfn->( { no_json => $response } ) unless exists $response->{content}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||
| 404 | # get the JSON | ||||||||||||||||||||||||||||||||||||||||||
| 405 | 16 | my $content = decode_json $response->{content}; | |||||||||||||||||||||||||||||||||||||||||
| 406 | #$dumpfn->( { their_response => $response, their_content => $content } ); | ||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||
| 408 | # verification before returning the content -- if their verifier doesn't match their nonce, | ||||||||||||||||||||||||||||||||||||||||||
| 409 | # then we don't have secure communication | ||||||||||||||||||||||||||||||||||||||||||
| 410 | # Don't need to check on test-associate/associate if verifier is missing, because there can | ||||||||||||||||||||||||||||||||||||||||||
| 411 | # reasonably be no verifier on those (ie, when test-associate returns false, or when the associate fails) | ||||||||||||||||||||||||||||||||||||||||||
| 412 | 15 | ▶ | if(exists $content->{Verifier} or ($type ne 'test-associate' and $type ne 'associate')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
exists $content->{"Verifier"} or $type ne "test-associate" and $type ne "associate"
$type ne "test-associate" and $type ne "associate"
MC/DC66%2/3
exists $content->{"Verifier"} or $type ne "test-associate" and $type ne "associate"
exists $content->{"Verifier"}
$type ne "test-associate"
$type ne "associate"
Truth table75%3/4
A or B and C
A= exists $content->{"Verifier"}B= $type ne "test-associate"C= $type ne "associate"
| |||||||||||||||||||||||||||||||||||||||||||
| 413 | 13 | ▶ | croak $dumpfn->( { missing_verifier => $content } ) unless exists $content->{Nonce} and exists $content->{Verifier}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $content->{"Nonce"} and exists $content->{"Verifier"}
MC/DC100%2/2
exists $content->{"Nonce"} and exists $content->{"Verifier"}
exists $content->{"Nonce"}
exists $content->{"Verifier"}
Truth table100%3/3
A and B
A= exists $content->{"Nonce"}B= exists $content->{"Verifier"}
| |||||||||||||||||||||||||||||||||||||||||||
| 414 | 11 | my $their_iv = decode_base64($content->{Nonce}); | |||||||||||||||||||||||||||||||||||||||||
| 415 | 11 | my $decode_their_verifier = $self->{cbc}->decrypt( decode_base64($content->{Verifier}), $self->{key}, $their_iv ); | |||||||||||||||||||||||||||||||||||||||||
| 416 | 11 | ▶ | if( $decode_their_verifier ne $content->{Nonce} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 417 | 1 | croak $dumpfn->( { "Decoded Verifier $decode_their_verifier" => $content } ); | |||||||||||||||||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||||||
| 421 | # If it made it to here, it's safe to return the content | ||||||||||||||||||||||||||||||||||||||||||
| 422 | 12 | return $content; | |||||||||||||||||||||||||||||||||||||||||
| 423 | } | ||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||
| 425 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||||||||||
| 427 | =for comment END OF USER INTERFACE | ||||||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||||||
| 429 | =head2 HELPER METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||||||||||||
| 431 | In general, most users won't need these. But maybe I<you> will. | ||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||
| 433 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||||||||||
| 435 | =item generate_nonce | ||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||
| 437 | my ($iv, $base64) = $kph->generate_nonce(); | ||||||||||||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||||||||||||
| 439 | This is used by the L</request> method to generate the IV nonce | ||||||||||||||||||||||||||||||||||||||||||
| 440 | for communication. I don't think you need to use it yourself, but | ||||||||||||||||||||||||||||||||||||||||||
| 441 | it's available to you, if you find a need for it. | ||||||||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||||||||
| 443 | The C<$iv> is the string of octets (the actual 128 IV nonce value). | ||||||||||||||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||||||||||||||
| 445 | The C<$base64> is the base64 representation of the C<$iv>. | ||||||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||||||
| 447 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||
| 449 | sub generate_nonce | ||||||||||||||||||||||||||||||||||||||||||
| 450 | { | ||||||||||||||||||||||||||||||||||||||||||
| 451 | # generate 10 bytes of random numbers, 2 bytes of microsecond time, and 4 bytes of seconds | ||||||||||||||||||||||||||||||||||||||||||
| 452 | # this gives randomness from two sources (rand and usecond), | ||||||||||||||||||||||||||||||||||||||||||
| 453 | # plus a deterministic counter that won't repeat for 2^31 seconds (almost 70 years) | ||||||||||||||||||||||||||||||||||||||||||
| 454 | # so as long as you aren't using the same key for 70 years, the nonce should be unique | ||||||||||||||||||||||||||||||||||||||||||
| 455 | 21 | ▶ | my $hex = ''; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
generate_nonce: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 456 | 21 | $hex .= sprintf '%02X', rand(256) for 1..10; | |||||||||||||||||||||||||||||||||||||||||
| 457 | 21 | my ($s,$us) = gettimeofday(); | |||||||||||||||||||||||||||||||||||||||||
| 458 | 21 | $hex .= sprintf '%04X%08X', $us&0xFFFF, $s&0xFFFFFFFF; | |||||||||||||||||||||||||||||||||||||||||
| 459 | 21 | my $iv = pack 'H*', $hex; | |||||||||||||||||||||||||||||||||||||||||
| 460 | 21 | my $nonce = encode_base64($iv, ''); | |||||||||||||||||||||||||||||||||||||||||
| 461 | 21 | ▶ | return wantarray ? ($iv, $nonce) : $iv; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 462 | } | ||||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||
| 464 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||
| 466 | =for comment END OF HELPER METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||||||
| 469 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||||||||||||
| 471 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||||||||||
| 473 | =item * L<KeePass Plugins list|https://keepass.info/plugins.html> | ||||||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||||||
| 475 | =item * L<KeePassHttp Plugin home|https://github.com/pfn/keepasshttp/> | ||||||||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||||||||
| 477 | =item * L<WWW::KeePassRest> = A similar interface which uses the KeePassRest plugin to interface with KeePass | ||||||||||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||||||||
| 479 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||||||||||||||||
| 481 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||||||||
| 483 | Thank you to L<KeePass|https://keepass.info/> for providing a free | ||||||||||||||||||||||||||||||||||||||||||
| 484 | password manager with plugin capability. | ||||||||||||||||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||||||||||||||||
| 486 | Thank you to the L<KeePassHttp Plugin|https://github.com/pfn/keepasshttp/> | ||||||||||||||||||||||||||||||||||||||||||
| 487 | for providing a free and open source plugin which allows for easy | ||||||||||||||||||||||||||||||||||||||||||
| 488 | communication between an external application and the KeePass application, | ||||||||||||||||||||||||||||||||||||||||||
| 489 | enabling the existence of this module (and the ability for it to give | ||||||||||||||||||||||||||||||||||||||||||
| 490 | applications access to the passwords stored in KeePass). | ||||||||||||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||||||||||||
| 492 | This module and author are not affiliated with either KeePass or KeePassHttp | ||||||||||||||||||||||||||||||||||||||||||
| 493 | except as a user of those fine products. | ||||||||||||||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||||||||||||||
| 495 | =head1 TODO | ||||||||||||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||||||||||||
| 497 | The entries should be full-fledged objects, with method-based access to | ||||||||||||||||||||||||||||||||||||||||||
| 498 | the underlying Login, Url, and Password values. | ||||||||||||||||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||||||||||||||||
| 500 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||||||||||||
| 502 | Peter C. Jones C<E<lt>petercj AT cpan DOT orgE<gt>> | ||||||||||||||||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||||||||||||||||
| 504 | Please report any bugs or feature requests | ||||||||||||||||||||||||||||||||||||||||||
| 505 | thru the repository's interface at L<https://github.com/pryrt/WWW-KeePassHttp/issues>. | ||||||||||||||||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||||||||||||||||
| 507 | =begin html | ||||||||||||||||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||||||||||||||||
| 509 | <a href="https://metacpan.org/pod/WWW::KeePassHttp"><img src="https://img.shields.io/cpan/v/WWW-KeePassHttp.svg?colorB=00CC00" alt="" title="metacpan"></a> | ||||||||||||||||||||||||||||||||||||||||||
| 510 | <a href="https://matrix.cpantesters.org/?dist=WWW-KeePassHttp"><img src="https://cpants.cpanauthors.org/dist/WWW-KeePassHttp.png" alt="" title="cpan testers"></a> | ||||||||||||||||||||||||||||||||||||||||||
| 511 | <a href="https://github.com/pryrt/WWW-KeePassHttp/releases"><img src="https://img.shields.io/github/release/pryrt/WWW-KeePassHttp.svg" alt="" title="github release"></a> | ||||||||||||||||||||||||||||||||||||||||||
| 512 | <a href="https://github.com/pryrt/WWW-KeePassHttp/issues"><img src="https://img.shields.io/github/issues/pryrt/WWW-KeePassHttp.svg" alt="" title="issues"></a> | ||||||||||||||||||||||||||||||||||||||||||
| 513 | <a href="https://coveralls.io/github/pryrt/WWW-KeePassHttp?branch=main"><img src="https://coveralls.io/repos/github/pryrt/WWW-KeePassHttp/badge.svg?branch=main" alt="Coverage Status" /></a> | ||||||||||||||||||||||||||||||||||||||||||
| 514 | <a href="https://github.com/pryrt/WWW-KeePassHttp/actions/"><img src="https://github.com/pryrt/WWW-KeePassHttp/actions/workflows/perl-ci.yml/badge.svg" alt="github perl-ci"></a> | ||||||||||||||||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||||||||||||||||
| 516 | =end html | ||||||||||||||||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||||||||||||||||
| 518 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||||||||||||||||
| 520 | Copyright (C) 2021 Peter C. Jones | ||||||||||||||||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||||||||||||||||
| 522 | =head1 LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||||||||||
| 524 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||||||||||||
| 525 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||||||||||||||||||||||
| 526 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||||||||||||||||
| 527 | See L<http://dev.perl.org/licenses/> for more information. | ||||||||||||||||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||||||||||||||||
| 529 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||||||||||||||||
| 531 | 8 | 1; | |||||||||||||||||||||||||||||||||||||||||