blib/lib/WWW/HKP.pm

filter: Statementstmt 54.7% 52 / 95 Branchbran 56.2% 27 / 48 Conditioncond 43.9% 29 / 66 MC/DCmcdc 12.5% 4 / 32 Subroutinesub 56.2% 9 / 16 Podpod 100% 4 / 4 totaltotal 47.8% 125 / 261 SCAR 58.1
CC
50
Cov
52%
query
42.0
_parse_mr
41.6
_get
30.0
CRAP 332.1
↑ Summary
1 package WWW::HKP;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use LWP::UserAgent 6.05;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use URI 1.60;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use URI::Escape 3.31;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 WWW::HKP - Interface to HTTP Keyserver Protocol (HKP)
15  
16 =head1 VERSION
17  
18 Version 0.02
19  
20 =cut
21  
22 3 our $VERSION = '0.02';
23  
24  
25 =head1 SYNOPSIS
26  
27 use WWW::HKP;
28
29 my $hkp = WWW::HKP->new();
30
31 $hkp->query(index => 'foo@bar.baz');
32 $hkp->query(get => 'DEADBEEF');
33  
34 =head1 DESCRIPTION
35  
36 This module implements the IETF draft of the OpenPGP HTTP Keyserver Protocol.
37  
38 More Informationen about HKP is available at L<http://tools.ietf.org/html/draft-shaw-openpgp-hkp-00>.
39  
40 =head1 FUNCTIONS
41  
42 =head2 new([%options])
43  
44 The C<new()> constructor method instanciates a new C<WWW::HKP> object. The following example shows available options and its default values.
45  
46 my $hkp = WWW::HKP->new(
47 host => 'localhost',
48 port => 11371
49 );
50  
51 In most cases you just need to set the I<host> parameter:
52  
53 my $hkp = new WWW::HKP host => 'pool.sks-keyservers.net';
54  
55 =cut
56  
57 sub new($;%) {
58 2 ▶ my ($class, %options) = @_;
Subroutine new: called CC=4 SCAR=14.6
Pod documented
59
60 2 my $uri = URI->new('http:');
61 2 ▶ $uri->host($options{host} || 'localhost');
Condition50%1/2
$options{'host'} || 'localhost'
l!l
02
MC/DC0%0/1
$options{'host'} || 'localhost'
$options{'host'}
Truth table50%1/2
A
A= $options{'host'}
Aresult
11
00
62 2 ▶ $uri->port($options{port} || 11371);
Condition50%1/2
$options{'port'} || 11371
l!l
02
MC/DC0%0/1
$options{'port'} || 11371
$options{'port'}
Truth table50%1/2
A
A= $options{'port'}
Aresult
11
00
63
64 2 my $ua = LWP::UserAgent->new;
65 2 $ua->agent(__PACKAGE__.'/'.$VERSION);
66
67 2 my $self = {
68 ua => $ua,
69 uri => $uri,
70 };
71
72 2 ▶ return bless $self => (ref $class || $class);
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
73 }
74  
75 0 ▶sub _ua($) { shift->{ua} }
Subroutine _ua: not called CC=1 SCAR=6.9
76 0 ▶sub _uri($) { shift->{uri} }
Subroutine _uri: not called CC=1 SCAR=6.9
77  
78 sub _get($$) {
79 0 ▶ my ($self, %query) = @_;
Subroutine _get: not called CC=5 SCAR=34.0
80 0 $self->{error} = undef;
81 0 $self->_uri->path('/pks/lookup');
82 0 $self->_uri->query_form(%query);
83 0 my $response = $self->_ua->get($self->_uri);
84 0 ▶ if (defined $response and ref $response and $response->isa('HTTP::Response') and $response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $response and ref $response and $response->isa('HTTP::Response') and $response->is_success) { } 0 0 0
Condition0%0/9
defined $response and ref $response and $response->isa("HTTP::Response") and $response->is_success
!ll&&!rl&&r
000
defined $response and ref $response and $response->isa("HTTP::Response")
!ll&&!rl&&r
000
defined $response and ref $response
!ll&&!rl&&r
000
MC/DC0%0/4
defined $response and ref $response and $response->isa("HTTP::Response") and $response->is_success
defined $response ref $response $response->isa("HTTP::Response") $response->is_success
Truth table0%0/5
A and B and C and D
A= defined $responseB= ref $responseC= $response->isa("HTTP::Response")D= $response->is_success
ABCDresult
0XXX0
10XX0
110X0
11100
11111
85 0 return $response->decoded_content;
86 } else {
87 0 $self->{error} = $response->status_line;
88 0 return undef;
89 }
90 }
91  
92 sub _post($%) {
93 0 ▶ my ($self, %query) = @_;
Subroutine _post: not called CC=5 SCAR=34.0
94 0 $self->{error} = undef;
95 0 $self->_uri->path('/pks/lookup');
96 0 my $response = $self->_ua->post($self->_uri, \%query);
97 0 ▶ if (defined $response and ref $response and $response->isa('HTTP::Response') and $response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $response and ref $response and $response->isa('HTTP::Response') and $response->is_success) { } 0 0 0
Condition0%0/9
defined $response and ref $response and $response->isa("HTTP::Response") and $response->is_success
!ll&&!rl&&r
000
defined $response and ref $response and $response->isa("HTTP::Response")
!ll&&!rl&&r
000
defined $response and ref $response
!ll&&!rl&&r
000
MC/DC0%0/4
defined $response and ref $response and $response->isa("HTTP::Response") and $response->is_success
defined $response ref $response $response->isa("HTTP::Response") $response->is_success
Truth table0%0/5
A and B and C and D
A= defined $responseB= ref $responseC= $response->isa("HTTP::Response")D= $response->is_success
ABCDresult
0XXX0
10XX0
110X0
11100
11111
98 0 return $response->decoded_content;
99 } else {
100 0 $self->{error} = $response->status_line;
101 0 return undef;
102 }
103  
104 }
105  
106 sub _parse_mr($$$) {
107 3 ▶ my ($self, $lines, $filter_ok) = @_;
Subroutine _parse_mr: called CC=32 SCAR=37.3
108 3 my $keys = {};
109 3 my $key;
110 3 my ($keyc, $keyn) = (0, 0);
111 3 foreach my $line (split /\r?\n/ => $lines) {
112 10 ▶ if ($line =~ /^info:(\d+):(\d+)$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($line =~ /^info:(\d+):(\d+)$/) { } 3 7 10
elsif ($line =~ /^pub:([0-9a-f]{8,16}):(\d*):(\d*):(\d*):(\d*):([der]*)$/i) { } 3 4 7
elsif ($line =~ /^uid:([^:]*):(\d*):(\d*):([der]*)$/i) { } 4 0 4
113 3 ▶ croak "unsupported hkp version: v$1" unless $1 == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $1 == 1 0 3 3
114 3 $keyc = $2;
115 } elsif ($line =~ /^pub:([0-9a-f]{8,16}):(\d*):(\d*):(\d*):(\d*):([der]*)$/i) {
116 3 $key = $1;
117 3 $keyn++;
118 3 my ($algo, $keylen, $created, $expires, $flags, $ok) = ($2, $3, $4, $5, $6, undef);
119 3 ▶ $ok = ((($created and $created > time) or ($expires and $expires < time) or (length $flags)) ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$created && $created > time || $expires && $expires < time || length $flags ? : 1 2 3
Condition58%7/12
$created and $created > time or $expires and $expires < time or length $flags
l!l&&r!l&&!r
102
$created and $created > time or $expires and $expires < time
l!l&&r!l&&!r
012
$created and $created > time
!ll&&!rl&&r
030
$expires and $expires < time
!ll&&!rl&&r
021
MC/DC20%1/5
$created and $created > time or $expires and $expires < time or length $flags
$created $created > time $expires $expires < time length $flags
Truth table18%2/11
A and B or C and D or E
A= $createdB= $created > timeC= $expiresD= $expires < timeE= length $flags
ABCDEresult
11XXX1
0X11X1
1011X1
0X0X11
0X1011
100X11
101011
0X0X00
0X1000
100X00
101000
120 3 ▶ if ($filter_ok and !$ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filter_ok and not $ok) 0 3 3
Condition66%2/3
$filter_ok and not $ok
!ll&&!rl&&r
120
MC/DC0%0/2
$filter_ok and not $ok
$filter_ok not $ok
Truth table66%2/3
A and not(B)
A= $filter_okB= not $ok
ABresult
0X0
100
111
121 0 $key = undef;
122 0 next;
123 }
124 3 ▶ $keys->{$key} = {
Branch: 3 branches
BranchTrueFalseTotal
$flags =~ /r/ ? : 1 2 3
$flags =~ /e/ ? : 1 2 3
$flags =~ /d/ ? : 1 2 3
Condition50%2/4
$created || undef
l!l
30
$expires || undef
l!l
30
MC/DC0%0/1
$created || undef
$created
MC/DC0%0/1
$expires || undef
$expires
Truth table50%1/2
A
A= $created
Aresult
11
00
Truth table50%1/2
A
A= $expires
Aresult
11
00
125 algo => $algo,
126 keylen => $keylen,
127 created => $created || undef,
128 expires => $expires || undef,
129 revoked => ($flags =~ /r/ ? 1 : 0),
130 expired => ($flags =~ /e/ ? 1 : 0),
131 deleted => ($flags =~ /d/ ? 1 : 0),
132 ok => $ok,
133 uids => []
134 };
135 } elsif ($line =~ /^uid:([^:]*):(\d*):(\d*):([der]*)$/i) {
136 4 ▶ next unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 4 4
137 4 my ($uid, $created, $expires, $flags, $ok) = ($1, $2, $3, $4, undef);
138 4 ▶ $ok = ((($created and $created > time) or ($expires and $expires < time) or (length $flags)) ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$created && $created > time || $expires && $expires < time || length $flags ? : 3 1 4
Condition83%10/12
$created and $created > time or $expires and $expires < time or length $flags
l!l&&r!l&&!r
211
$created and $created > time or $expires and $expires < time
l!l&&r!l&&!r
022
$created and $created > time
!ll&&!rl&&r
130
$expires and $expires < time
!ll&&!rl&&r
112
MC/DC20%1/5
$created and $created > time or $expires and $expires < time or length $flags
$created $created > time $expires $expires < time length $flags
Truth table27%3/11
A and B or C and D or E
A= $createdB= $created > timeC= $expiresD= $expires < timeE= length $flags
ABCDEresult
11XXX1
0X11X1
1011X1
0X0X11
0X1011
100X11
101011
0X0X00
0X1000
100X00
101000
139 4 ▶ next if $filter_ok and !$ok;
Branch: 1 branch
BranchTrueFalseTotal
if $filter_ok and not $ok 2 2 4
Condition100%3/3
$filter_ok and not $ok
!ll&&!rl&&r
112
MC/DC100%2/2
$filter_ok and not $ok
$filter_ok not $ok
Truth table100%3/3
A and not(B)
A= $filter_okB= not $ok
ABresult
0X0
100
111
140 2 ▶ push @{ $keys->{$key}->{uids} } => {
Branch: 3 branches
BranchTrueFalseTotal
$flags =~ /r/ ? : 1 1 2
$flags =~ /e/ ? : 1 1 2
$flags =~ /d/ ? : 1 1 2
Condition50%2/4
$created || undef
l!l
20
$expires || undef
l!l
20
MC/DC0%0/1
$created || undef
$created
MC/DC0%0/1
$expires || undef
$expires
Truth table50%1/2
A
A= $created
Aresult
11
00
Truth table50%1/2
A
A= $expires
Aresult
11
00
141 uid => uri_unescape($uid),
142 created => $created || undef,
143 expires => $expires || undef,
144 revoked => ($flags =~ /r/ ? 1 : 0),
145 expired => ($flags =~ /e/ ? 1 : 0),
146 deleted => ($flags =~ /d/ ? 1 : 0),
147 ok => $ok
148 };
149 } else {
150 0 carp "unknown line: $line";
151 }
152 }
153 3 ▶ carp "server said there where $keyc keys, but $keyn keys parsed" unless $keyc == $keyn;
Branch: 1 branch
BranchTrueFalseTotal
unless $keyc == $keyn 0 3 3
154 3 return $keys;
155 }
156  
157 =head2 query($type => $search [, %options ])
158  
159 The C<query()> method implements both query operations of HKP: I<index> and I<get>
160  
161 =cut
162  
163 sub query($$$;%) {
164 0 ▶ my ($self, $type, $search, %options) = @_;
Subroutine query: not called CC=6 SCAR=37.4
Pod documented
165 0 given ($type) {
166  
167 =head3 I<index> operation
168  
169 $hkp->query(index => 'foo@bar.baz');
170  
171 The first parameter must be I<index>, the secondend parameter an email-address or key-id.
172  
173 If any keys where found, a hashref is returned. Otherwise returns undef, an error message can be fetched with C<< $hkp->error() >>.
174  
175 The returned hashref may look like this:
176  
177 {
178 'DEADBEEF' => {
179 'algo' => '1',
180 'keylen' => '2048',
181 'created' => '1253025510',
182 'expires' => '1399901151',
183 'deleted' => 0,
184 'expired' => 0,
185 'revoked' => 0,
186 'ok' => 1,
187 'uids' => [
188 {
189 'uid' => 'Lorem Ipsum (This is an example) <foo@bar.baz>'
190 'created' => '1253025510',
191 'expires' => '1399901151',
192 'deleted' => 0,
193 'expired' => 0,
194 'revoked' => 0,
195 'ok' => 1
196 }
197 ]
198 }
199 }
200  
201 The keys of the hashref are key-ids. The meaning of the hashkeys in the seconded level:
202  
203 =over
204  
205 =item I<algo>
206  
207 The algorithm of the key. The values are described in RFC 2440
208  
209 =item I<keylen>
210  
211 The key length in bytes
212  
213 =item I<created>
214  
215 Creation date of the key, in seconds since 1970-01-01 UTC.
216  
217 =item I<expires>
218  
219 Expiration date of the key
220  
221 =item I<deleted>, I<expired>, I<revoked>
222  
223 Indication details, whether the key is deleted, expired or revoked. If the flag is that, the value is C<1>, otherwise C<0>.
224  
225 =item I<ok>
226  
227 The creation date and expiration date is checked against C<time()>. If it doesn't match or any of the flags obove are set, I<ok> will be C<0>, otherwise C<1>.
228  
229 =item I<uids>
230  
231 A arrayref of user-ids.
232  
233 =over
234  
235 =item I<uid>
236  
237 The user-id in common format. It can be parsed by L<Email::Address> for example.
238  
239 =item I<created>, I<expires>, I<deleted>, I<expired>, I<revoked>, I<ok>
240  
241 This fields have the same meaning as described above. The information is taken from the self-signature, if any. I<created> and I<expired> may be C<undef> if not available (e.g. empty string).
242  
243 =back
244  
245 =back
246  
247 =head4 Available options
248  
249 =over
250  
251 =item I<exact>
252  
253 Set the I<filter_ok> parameter to C<1> (or any expression that evaluates to true), if you want an exact match of your search expression.
254  
255 =item I<filter_ok>
256  
257 Set the I<filter_ok> parameter to C<1> (or any expression that evaluates to true), if you want only valid results. All keys or user IDs having I<ok>-parameter of C<0> are ignored.
258  
259 $hkp->query(index => 'foo@bar.baz', filter_ok => 1);
260  
261 =back
262  
263 =cut
264  
265 0 when ('index') {
266 0 my @options = qw(mr);
267 0 ▶ push @options => 'exact' if $options{exact};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"exact"} 0 0 0
268 0 my $message = $self->_get(op => 'index', options => join(',' => @options), search => $search);
269 0 ▶ return undef unless defined $message;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message 0 0 0
270 0 ▶ return $self->_parse_mr($message, $options{filter_ok} ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$options{'filter_ok'} ? : 0 0 0
271 }
272  
273 =head3 I<get> operation
274  
275 $hkp->query(get => 'DEADBEEF');
276  
277 The operation returns the public key of specified key-id or undef, if not found. Any error messages can be fetched with C<< $hkp->error() >>.
278  
279 =cut
280  
281 0 when ('get') {
282 0 ▶ if ($search !~ /^0x/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($search =~ /^0x/) 0 0 0
283 0 $search = '0x'.$search;
284 }
285 0 my $message = $self->_get(op => 'get', options => 'exact', search => $search);
286 0 ▶ return undef unless defined $message;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message 0 0 0
287 0 return $message;
288 }
289  
290 =head3 unimplemented operations
291  
292 A HKP server may implement various other operations. Unimplemented operation cause the module to die with a stack trace.
293  
294 =cut
295  
296 0 default {
297 0 confess "unknown query type '$type'";
298 }
299 }
300 }
301  
302 =head2 submit
303  
304 Submit one or more ASCII-armored version of public keys to the server.
305  
306 $pubkey = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n...";
307
308 $hkp->submit($pubkey);
309
310 @pubkeys = ($pubkey1, $pubkey2, ...);
311  
312 $hkp->submit(@pubkeys);
313  
314 In case of success, C<1> is returned. Otherweise C<0> and an error message can be fetched from C<$hkp->error>.
315  
316 =cut
317  
318 sub submit($@) {
319 0 ▶ my ($self, @keys) = @_;
Subroutine submit: not called CC=3 SCAR=24.8
Pod documented
320 0 my $status = $self->_post(map {( keytext => $_ )} @keys);
321 0 ▶ return (defined $status and $status ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$status ? : 0 0 0
Condition0%0/3
defined $status && ($status ? 1 : 0)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $status && ($status ? 1 : 0)
defined $status ($status ? 1 : 0)
Truth table0%0/3
A && B
A= defined $statusB= ($status ? 1 : 0)
ABresult
0X0
100
111
322 }
323  
324 =head2 error
325  
326 Returns last error message, if any.
327  
328 $hkp->error; # "404 Not found", for example.
329  
330 =cut
331  
332 0 ▶sub error($) { shift->{error} }
Subroutine error: not called CC=1 SCAR=6.9
Pod documented
333  
334 =head1 AUTHOR
335  
336 David Zurborg, C<< <zurborg at cpan.org> >>
337  
338 =head1 BUGS
339  
340 Please report any bugs or feature requests trough my project management tool at L<http://development.david-zurb.org/projects/libwww-hkp-perl/issues/new>. I will be notified, and then you'll
341 automatically be notified of progress on your bug as I make changes.
342  
343 =head1 SUPPORT
344  
345 You can find documentation for this module with the perldoc command.
346  
347 perldoc WWW::HKP
348  
349 You can also look for information at:
350  
351 =over 4
352  
353 =item * Redmine: Homepage of this module
354  
355 L<http://development.david-zurb.org/projects/libwww-hkp-perl>
356  
357 =item * RT: CPAN's request tracker
358  
359 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=WWW-HKP>
360  
361 =item * AnnoCPAN: Annotated CPAN documentation
362  
363 L<http://annocpan.org/dist/WWW-HKP>
364  
365 =item * CPAN Ratings
366  
367 L<http://cpanratings.perl.org/d/WWW-HKP>
368  
369 =item * Search CPAN
370  
371 L<http://search.cpan.org/dist/WWW-HKP/>
372  
373 =back
374  
375 =head1 COPYRIGHT & LICENSE
376  
377 Copyright 2014 David Zurborg, all rights reserved.
378  
379 This program is not really free software; you can redistribute it and/or modify it under certain circumstances. See file F<LICENSE> for details.
380  
381 =cut
382  
383 3 1; # End of WWW::HKP
↑ Summary