Signature/Parser.pm

filter: Statementstmt 98.9% 90 / 91 Branchbran 80.4% 37 / 46 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 16 / 16 Podpod 100% 1 / 1 totaltotal 91.8% 146 / 159 CC 26 SCAR 19.0
CC
26
Cov
92%
parse
14.0
__ANON__
5.4
_check_skew
5.0
CRAP 6.7
↑ Summary Signature.pm »
1 package Authen::HTTP::Signature::Parser;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Authen::HTTP::Signature;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use HTTP::Date qw(str2time);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 Authen::HTTP::Signature::Parser - Parse HTTP signature headers
15  
16 =cut
17  
18 3 our $VERSION = '0.03';
19  
20 =head1 PURPOSE
21  
22 This class parses a HTTP signature 'Authorization' header (if one exists) from a request
23 object and populates attributes in a L<Authen::HTTP::Signature> object.
24  
25 =head1 ATTRIBUTES
26  
27 =over
28  
29 =item request
30  
31 The request to be parsed.
32  
33 =back
34  
35 =cut
36  
37 has 'request' => (
38 is => 'rw',
39 14 ▶ isa => sub { confess "'request' must be blessed" unless blessed($_[0]) },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($_[0]) 0 14 14
40 3 predicate => 'has_request',
41 );
42  
43 15 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
44 15 my $orig = shift;
45 15 my $class = shift;
46  
47 15 ▶ if ( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 13 2 15
48 13 unshift @_, "request";
49 }
50  
51 15 return $class->$orig(@_);
52 3 };
53  
54 =over
55  
56 =item get_header
57  
58 A call back to get a header from C<request>.
59  
60 =back
61  
62 =cut
63  
64 has 'get_header' => (
65 is => 'rw',
66 14 ▶ isa => sub { die "'get_header' expects a CODE ref\n" unless ref($_[0]) eq "CODE" },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "CODE" 0 14 14
67 predicate => 'has_get_header',
68 14 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.1
69 72 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=16.8
70 72 ▶ confess "Didn't get 2 arguments" unless @_ == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 0 72 72
71 72 my $request = shift;
72 72 ▶ confess "'request' isn't blessed" unless blessed $request;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($request) 0 72 72
73 72 my $name = shift;
74  
75 72 ▶ if( $name eq 'request-line' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($name eq 'request-line') { } 12 60 72
elsif ($name eq '(request-target)') { } 0 60 60
76 12 sprintf("%s %s",
77 $request->uri->path_query,
78 $request->protocol);
79 } elsif( $name eq '(request-target)' ) {
80 0 sprintf("%s %s",
81 lc($request->method),
82 $request->uri->path_query);
83 } else {
84 60 $request->header($name);
85 }
86 14 };
87 },
88 3 lazy => 1,
89 );
90  
91 =over
92  
93 =item skew
94  
95 Defaults to 300 seconds in either direction from your clock. If the Date header data is outside of this range,
96 the request is considered invalid.
97  
98 Set this value to 0 to disable skew checks for testing purposes.
99  
100 =back
101  
102 =cut
103  
104 has 'skew' => (
105 is => 'rw',
106 15 ▶ isa => sub { die "$_[0] isn't an integer" unless $_[0] =~ /[0-9]+/ },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /[0-9]+/ 0 15 15
107 14 ▶ default => sub { 300 },
Subroutine __ANON__: called CC=1 SCAR=0.0
108 3 );
109  
110  
111 =head1 METHOD
112  
113 Errors are fatal.
114  
115 =over
116  
117 =item parse()
118  
119 This method parses signature header components.
120  
121 =back
122  
123 =cut
124  
125 15 ▶sub parse {
Subroutine parse: called CC=14 SCAR=26.4
Pod documented
126 15 my $self = shift;
127 15 ▶ my $request = shift || $self->request;
Condition66%2/3
shift() || $self->request
l!l&&r!l&&!r
2130
MC/DC0%0/2
shift() || $self->request
shift() $self->request
Truth table66%2/3
A || B
A= shift()B= $self->request
ABresult
1X1
011
000
128  
129 15 ▶ confess "There was no request to parse!" unless $request;
Branch: 1 branch
BranchTrueFalseTotal
unless $request 0 15 15
130  
131 15 my $sig_str = $self->get_header->($request, 'authorization');
132 15 ▶ confess 'No authorization header value was returned!' unless $sig_str;
Branch: 1 branch
BranchTrueFalseTotal
unless $sig_str 1 14 15
133  
134 14 $self->_check_skew($request);
135  
136 13 my ( $sig_text ) = $sig_str =~ /^(Signature).*$/;
137 13 ▶ confess "does not match required string 'Signature'" unless $sig_text;
Branch: 1 branch
BranchTrueFalseTotal
unless $sig_text 1 12 13
138  
139 12 my ( $b64_str ) = $sig_str =~ /^Signature.*signature="(.*?)".*$/;
140 12 ▶ confess "No signature data found!" unless $b64_str;
Branch: 1 branch
BranchTrueFalseTotal
unless $b64_str 1 11 12
141  
142 11 my ( $key_id ) = $sig_str =~ /^Signature.*(keyId=".*?").*$/;
143 11 my ( $algo ) = $sig_str =~ /^Signature.*(algorithm=".*?").*$/;
144 11 my ( $ext ) = $sig_str =~ /^Signature.*(ext=".*?").*$/;
145 11 my ( $hdrs ) = $sig_str =~ /^Signature.*(headers=".*?").*$/;
146  
147 11 $key_id =~ s/^keyId="(.*)"$/$1/;
148 11 $algo =~ s/^algorithm="(.*)"$/$1/;
149 11 ▶ $ext =~ s/^ext="(.*)"/$1/ if $ext;
Branch: 1 branch
BranchTrueFalseTotal
if $ext 1 10 11
150  
151 11 ▶ confess "No key id found!" unless $key_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $key_id 1 10 11
152 10 ▶ confess "No algorithm found" unless $algo;
Branch: 1 branch
BranchTrueFalseTotal
unless $algo 1 9 10
153  
154 9 my @headers;
155 9 ▶ if ( $hdrs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hdrs) 7 2 9
156 7 $hdrs =~ s/^headers="(.*)"$/$1/;
157 7 @headers = split / /, $hdrs;
158 }
159  
160 9 ▶ push @headers, "date" unless @headers;
Branch: 1 branch
BranchTrueFalseTotal
unless @headers 2 7 9
161  
162 # die on duplicate headers
163 9 my %h;
164 9 foreach my $hdr ( @headers ) {
165 26 ▶ if ( exists $h{$hdr} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $h{$hdr}) 1 25 26
166 1 confess "Duplicate header '$hdr' found in signature header parameter. Aborting.";
167 }
168 25 $h{$hdr}++;
169 }
170  
171 # normalize headers to lower-case
172 8 @headers = map { lc } @headers;
173  
174 my $ss = join "\n", map {
175 8 ▶ if( $self->get_header->($request, $_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_header->($request, $_)) { } 22 1 23
176 22 sprintf("%s: %s", $_, $self->get_header->($request, $_) );
177 } else {
178 1 confess "Couldn't get header value for $_\n";
179 } } @headers;
180  
181 7 return Authen::HTTP::Signature->new(
182 key_id => $key_id,
183 headers => \@headers,
184 signing_string => $ss,
185 algorithm => $algo,
186 extensions => $ext,
187 signature => $b64_str,
188 request => $request,
189 );
190 }
191  
192 14 ▶sub _check_skew {
Subroutine _check_skew: called CC=5 SCAR=16.2
193 14 my $self = shift;
194  
195 14 ▶ if ( $self->skew ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->skew) 12 2 14
196 12 my $request = shift;
197 12 ▶ confess "No request found" unless $request;
Branch: 1 branch
BranchTrueFalseTotal
unless $request 0 12 12
198 12 my $header_time = str2time( $self->get_header->($request, 'date') );
199 12 ▶ confess "No Date header was returned (or could be parsed)" unless $header_time;
Branch: 1 branch
BranchTrueFalseTotal
unless $header_time 0 12 12
200  
201 12 my $diff = abs(time - $header_time);
202 12 ▶ if ( $diff >= $self->skew ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($diff >= $self->skew) 1 11 12
203 1 confess "Request is outside of clock skew tolerance: $diff seconds computed, " . $self->skew . " seconds allowed.\n";
204 }
205 }
206  
207 13 return 1;
208  
209 }
210  
211  
212 =head1 SEE ALSO
213  
214 L<Authen::HTTP::Signature>
215  
216 =cut
217  
218 3 1;
↑ Summary Signature.pm »