| 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
Condition100%3/3
exists $attrs{"key"} and defined &_STRING($attrs{"key"})
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"})
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | # Digest::SHA defaults to SHA-1 | ||||||||||||||||||||||||||||||||||||||||||
| 23 | 3 | ▶ | $attrs{'digest'} ||= 'Digest::SHA'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$attrs{"digest"} ||= "Digest::SHA"
MC/DC0%0/1
$attrs{"digest"} ||= "Digest::SHA"
$attrs{"digest"}
Truth table50%1/2
A
A= $attrs{"digest"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined &_CLASS($attrs{"digest"}) and Class::Load::load_class($attrs{"digest"})
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"})
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 3 | ▶ | $attrs{'length'} ||= 28; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$attrs{"length"} ||= 28
MC/DC0%0/1
$attrs{"length"} ||= 28
$attrs{"length"}
Truth table50%1/2
A
A= $attrs{"length"}
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | Carp::croak('length should be a positive integer') | ||||||||||||||||||||||||||||||||||||||||||
| 30 | 3 | ▶ | unless defined _POSINT($attrs{'length'}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 3 | ▶ | $attrs{'format'} ||= 'path'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$attrs{"format"} ||= "path"
MC/DC100%1/1
$attrs{"format"} ||= "path"
$attrs{"format"}
Truth table100%2/2
A
A= $attrs{"format"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined &_STRING($attrs{"format"}) and &_CLASS($child_class)
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | if $old_code; | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 5 | my ($code, $new_uri) = $self->extract( $uri ); | |||||||||||||||||||||||||||||||||||||||||
| 84 | 5 | ▶ | return if not $code | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
not $code && $uri or $code ne $self->code_for_uri($new_uri)
$code && $uri
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||