Signature.pm

filter: Statementstmt 100% 61 / 61 Branchbran 57.1% 8 / 14 Conditioncond 52.3% 11 / 21 MC/DCmcdc 25.0% 3 / 12 Subroutinesub 81.2% 13 / 16 Podpod 100% 7 / 7 totaltotal 78.6% 103 / 131 CC 16 SCAR 19.4
CC
16
Cov
83%
new
14.2
validate
6.3
BEGIN
1.0
CRAP 7.0
↑ Summary Signature/Path.pm »
1 package URL::Signature;
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  
6 3 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use URI::QueryParam ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use MIME::Base64 3.11 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Digest::HMAC ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Class::Load ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Params::Util qw( _STRING _POSINT _NONNEGINT _CLASS );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 our $VERSION = '0.03';
15  
16  
17 5 ▶sub new {
Subroutine new: called CC=12 SCAR=26.6
Pod documented
18 5 my ($class, %attrs) = @_;
19 Carp::croak('you must specify a secret key!')
20 5 ▶ unless exists $attrs{'key'} and defined _STRING($attrs{'key'});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $attrs{"key"} and defined &_STRING($attrs{"key"}) 2 3 5
Condition100%3/3
exists $attrs{"key"} and defined &_STRING($attrs{"key"})
!ll&&!rl&&r
113
MC/DC100%2/2
exists $attrs{"key"} and defined &_STRING($attrs{"key"})
exists $attrs{"key"} defined &_STRING($attrs{"key"})
Truth table100%3/3
A and B
A= exists $attrs{"key"}B= defined &_STRING($attrs{"key"})
ABresult
0X0
100
111
21  
22 # Digest::SHA defaults to SHA-1
23 3 ▶ $attrs{'digest'} ||= 'Digest::SHA';
Condition50%1/2
$attrs{"digest"} ||= "Digest::SHA"
l!l
03
MC/DC0%0/1
$attrs{"digest"} ||= "Digest::SHA"
$attrs{"digest"}
Truth table50%1/2
A
A= $attrs{"digest"}
Aresult
11
00
24 Carp::croak('digest must be a valid Perl class')
25 unless defined _CLASS($attrs{'digest'})
26 3 ▶ and Class::Load::load_class($attrs{'digest'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined &_CLASS($attrs{"digest"}) and Class::Load::load_class($attrs{"digest"}) 0 3 3
Condition33%1/3
defined &_CLASS($attrs{"digest"}) and Class::Load::load_class($attrs{"digest"})
!ll&&!rl&&r
003
MC/DC0%0/2
defined &_CLASS($attrs{"digest"}) and Class::Load::load_class($attrs{"digest"})
defined &_CLASS($attrs{"digest"}) Class::Load::load_class($attrs{"digest"})
Truth table33%1/3
A and B
A= defined &_CLASS($attrs{"digest"})B= Class::Load::load_class($attrs{"digest"})
ABresult
0X0
100
111
27  
28 3 ▶ $attrs{'length'} ||= 28;
Condition50%1/2
$attrs{"length"} ||= 28
l!l
03
MC/DC0%0/1
$attrs{"length"} ||= 28
$attrs{"length"}
Truth table50%1/2
A
A= $attrs{"length"}
Aresult
11
00
29 Carp::croak('length should be a positive integer')
30 3 ▶ unless defined _POSINT($attrs{'length'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined &_POSINT($attrs{"length"}) 0 3 3
31  
32 3 ▶ $attrs{'format'} ||= 'path';
Condition100%2/2
$attrs{"format"} ||= "path"
l!l
21
MC/DC100%1/1
$attrs{"format"} ||= "path"
$attrs{"format"}
Truth table100%2/2
A
A= $attrs{"format"}
Aresult
11
00
33 3 my $child_class = 'URL::Signature::' . ucfirst $attrs{'format'};
34  
35 Carp::croak(qq[invalid format '$attrs{format}'])
36 3 ▶ unless defined _STRING($attrs{'format'}) and _CLASS($child_class);
Branch: 1 branch
BranchTrueFalseTotal
unless defined &_STRING($attrs{"format"}) and &_CLASS($child_class) 0 3 3
Condition33%1/3
defined &_STRING($attrs{"format"}) and &_CLASS($child_class)
!ll&&!rl&&r
003
MC/DC0%0/2
defined &_STRING($attrs{"format"}) and &_CLASS($child_class)
defined &_STRING($attrs{"format"}) &_CLASS($child_class)
Truth table33%1/3
A and B
A= defined &_STRING($attrs{"format"})B= &_CLASS($child_class)
ABresult
0X0
100
111
37  
38 3 $attrs{'hmac'} = Digest::HMAC->new( $attrs{'key'}, $attrs{'digest'} );
39  
40 3 my ($loaded, $error) = Class::Load::try_load_class( $child_class );
41 3 ▶ Carp::croak(qq[error trying to load format class '$child_class': $error])
Branch: 1 branch
BranchTrueFalseTotal
unless $loaded 0 3 3
42 unless $loaded;
43  
44 3 my $self = bless \%attrs, $child_class;
45 3 $self->BUILD;
46 3 return $self;
47 }
48  
49  
50 10 ▶sub code_for_uri {
Subroutine code_for_uri: called CC=1 SCAR=0.0
Pod documented
51 10 my ($self, $uri) = @_;
52  
53 # ensure query parameters are sorted before encoding.
54 10 my %params = $uri->query_form;
55 10 $uri->query_form( map { $_ => $params{$_} } sort keys %params );
56  
57 my $code = MIME::Base64::encode_base64url(
58 10 $self->{hmac}->reset->add( $uri->as_string )->digest
59 );
60  
61 # truncate the digest, if necessary
62 10 $code = substr $code, 0, $self->{'length'};
63 10 return $code;
64 }
65  
66  
67 5 ▶sub sign {
Subroutine sign: called CC=1 SCAR=0.0
Pod documented
68 5 my ($self, $path) = @_;
69 5 my $uri = URI->new( $path );
70 5 my $code = $self->code_for_uri( $uri );
71  
72 5 return $self->append( $uri, $code );
73 }
74  
75  
76 5 ▶sub validate {
Subroutine validate: called CC=5 SCAR=18.4
Pod documented
77 5 my ($self, $path, $old_code) = @_;
78 5 my $uri = URI->new( $path );
79  
80 5 ▶ return $old_code eq $self->code_for_uri( $uri )
Branch: 1 branch
BranchTrueFalseTotal
if $old_code 0 5 5
81 if $old_code;
82  
83 5 my ($code, $new_uri) = $self->extract( $uri );
84 5 ▶ return if not $code
Branch: 1 branch
BranchTrueFalseTotal
if not $code && $uri or $code ne $self->code_for_uri($new_uri) 0 5 5
Condition33%2/6
not $code && $uri or $code ne $self->code_for_uri($new_uri)
l!l&&r!l&&!r
005
$code && $uri
!ll&&!rl&&r
005
MC/DC0%0/3
not $code && $uri or $code ne $self->code_for_uri($new_uri)
$code $uri $code ne $self->code_for_uri($new_uri)
Truth table25%1/4
not(A && B) or C
A= $codeB= $uriC= $code ne $self->code_for_uri($new_uri)
ABCresult
0XX1
10X1
1111
1100
85 or not $uri
86 or $code ne $self->code_for_uri( $new_uri );
87  
88 5 return $new_uri;
89 }
90  
91  
92 # let our subclasses implement those
93 0 ▶sub BUILD {}
Subroutine BUILD: not called CC=1 SCAR=0.0
Pod documented
94 0 ▶sub extract {}
Subroutine extract: not called CC=1 SCAR=0.0
Pod documented
95 0 ▶sub append {}
Subroutine append: not called CC=1 SCAR=0.0
Pod documented
96  
97  
98 3 42;
99 __END__
↑ Summary Signature/Path.pm »