HTTP/Auth.pm

filter: Statementstmt 93.7% 240 / 256 Branchbran 80.7% 134 / 166 Conditioncond 54.1% 52 / 96 MC/DCmcdc 28.3% 15 / 53 Subroutinesub 95.2% 20 / 21 Podpod 100% 5 / 5 totaltotal 78.0% 466 / 597 CC 126 SCAR 29.3
CC
126
Cov
82%
prepare_authentication
40.4
_parse_field
24.6
new
18.6
CRAP 18.7
« HTTP/Auth/Digest.pm ↑ Summary HTTP/Auth/Basic.pm »
1 package Uniform::HTTP::Auth;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 11 ▶use Uniform::HTTP::Auth::Basic ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Uniform::HTTP::Auth::Bearer ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use Uniform::HTTP::Auth::Digest ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 11 our $VERSION = '0.04';
12  
13 11 my %SCHEME_CLASS = (
14 basic => 'Uniform::HTTP::Auth::Basic',
15 bearer => 'Uniform::HTTP::Auth::Bearer',
16 digest => 'Uniform::HTTP::Auth::Digest',
17 );
18  
19 19 ▶sub new {
Subroutine new: called CC=18 SCAR=29.2
Pod documented
20 19 my ($class, %args) = @_;
21  
22 19 for my $name (keys %args) {
23 20 ▶ croak "unknown constructor option '$name'"
Branch: 1 branch
BranchTrueFalseTotal
unless $name eq "schemes" or $name eq "credentials" or $name eq "origin" 1 19 20
Condition100%6/6
$name eq "schemes" or $name eq "credentials" or $name eq "origin"
l!l&&r!l&&!r
1451
$name eq "schemes" or $name eq "credentials"
l!l&&r!l&&!r
596
MC/DC100%3/3
$name eq "schemes" or $name eq "credentials" or $name eq "origin"
$name eq "schemes" $name eq "credentials" $name eq "origin"
Truth table100%4/4
A or B or C
A= $name eq "schemes"B= $name eq "credentials"C= $name eq "origin"
ABCresult
1XX1
01X1
0011
0000
24 unless $name eq 'schemes'
25 || $name eq 'credentials'
26 || $name eq 'origin';
27 }
28  
29 my $schemes = exists $args{schemes}
30 ? $args{schemes}
31 18 ▶ : [qw(digest bearer basic)];
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'schemes'} ? : 5 13 18
32  
33 18 ▶ croak "schemes must be an array reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $schemes eq "ARRAY" 0 18 18
34 unless ref($schemes) eq 'ARRAY';
35  
36 18 my (@normalized, %seen);
37 18 for my $scheme (@$schemes) {
38 47 ▶ croak "scheme names must be plain scalars"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $scheme or ref $scheme 0 47 47
Condition33%1/3
not defined $scheme or ref $scheme
l!l&&r!l&&!r
0047
MC/DC0%0/2
not defined $scheme or ref $scheme
not defined $scheme ref $scheme
Truth table33%1/3
not(A) or B
A= not defined $schemeB= ref $scheme
ABresult
1X1
011
000
39 if !defined($scheme) || ref($scheme);
40 47 my $name = lc $scheme;
41 croak "unsupported authentication scheme '$scheme'"
42 47 ▶ unless exists $SCHEME_CLASS{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $SCHEME_CLASS{$name} 1 46 47
43 croak "duplicate authentication scheme '$scheme'"
44 46 ▶ if $seen{$name}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$name}++ 0 46 46
45 46 push @normalized, $name;
46 }
47  
48 17 ▶ _validate_origin($args{origin}) if exists $args{origin};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"origin"} 5 12 17
49  
50 17 my $credentials = $args{credentials};
51 17 ▶ if (exists $args{credentials}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"credentials"}) 9 8 17
52 9 my $type = ref($credentials);
53 9 ▶ croak "credentials must be a hash reference or coderef"
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "HASH" or $type eq "CODE" 0 9 9
Condition66%2/3
$type eq "HASH" or $type eq "CODE"
l!l&&r!l&&!r
630
MC/DC0%0/2
$type eq "HASH" or $type eq "CODE"
$type eq "HASH" $type eq "CODE"
Truth table66%2/3
A or B
A= $type eq "HASH"B= $type eq "CODE"
ABresult
1X1
011
000
54 unless $type eq 'HASH' || $type eq 'CODE';
55  
56 9 ▶ if ($type eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "HASH") 6 3 9
57 croak "origin is required when credentials is a hash reference"
58 6 ▶ unless exists $args{origin};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"origin"} 1 5 6
59 5 _validate_static_credentials($credentials);
60 3 $credentials = { %$credentials };
61 }
62 }
63  
64 my $self = bless {
65 schemes => \@normalized,
66 credentials => $credentials,
67 origin => $args{origin},
68 14 digest => Uniform::HTTP::Auth::Digest->new,
69 }, $class;
70  
71 14 return $self;
72 }
73  
74 0 ▶sub schemes {
Subroutine schemes: not called CC=1 SCAR=6.9
Pod documented
75 0 my ($self) = @_;
76 0 return [ @{ $self->{schemes} } ];
77 }
78  
79 23 ▶sub parse_challenges {
Subroutine parse_challenges: called CC=10 SCAR=24.3
Pod documented
80 23 my ($self, @values) = @_;
81  
82 23 my @out;
83 23 for my $value (@values) {
84 24 ▶ if (!defined($value) || ref($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value or ref $value) 0 24 24
Condition33%1/3
not defined $value or ref $value
l!l&&r!l&&!r
0024
MC/DC0%0/2
not defined $value or ref $value
not defined $value ref $value
Truth table33%1/3
not(A) or B
A= not defined $valueB= ref $value
ABresult
1X1
011
000
85 0 ▶ push @out, {
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 0 0 0
86 scheme => undef,
87 raw => defined($value) ? "$value" : '',
88 params => {},
89 token68 => undef,
90 malformed => 1,
91 error => 'challenge header value must be a plain scalar',
92 };
93 0 next;
94 }
95 24 push @out, @{ _parse_field($value) };
96 }
97  
98 23 for my $challenge (@out) {
99 28 ▶ next if $challenge->{malformed};
Branch: 1 branch
BranchTrueFalseTotal
if $challenge->{"malformed"} 4 24 28
100 24 my $scheme = $challenge->{scheme};
101 24 ▶ next unless defined $scheme && exists $SCHEME_CLASS{$scheme};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $scheme and exists $SCHEME_CLASS{$scheme} 1 23 24
Condition66%2/3
defined $scheme and exists $SCHEME_CLASS{$scheme}
!ll&&!rl&&r
0123
MC/DC50%1/2
defined $scheme and exists $SCHEME_CLASS{$scheme}
defined $scheme exists $SCHEME_CLASS{$scheme}
Truth table66%2/3
A and B
A= defined $schemeB= exists $SCHEME_CLASS{$scheme}
ABresult
0X0
100
111
102  
103 23 my $class = $SCHEME_CLASS{$scheme};
104 23 my $error = $class->validate_challenge($challenge);
105 23 ▶ if (defined $error) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error) 1 22 23
106 1 $challenge->{malformed} = 1;
107 1 $challenge->{error} = $error;
108 }
109 }
110  
111 23 return \@out;
112 }
113  
114 2 ▶sub select {
Subroutine select: called CC=8 SCAR=22.2
Pod documented
115 2 my ($self, $challenges) = @_;
116 2 ▶ croak "select() requires an array reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $challenges eq "ARRAY" 0 2 2
117 unless ref($challenges) eq 'ARRAY';
118  
119 2 for my $scheme (@{ $self->{schemes} }) {
120 my @candidates = grep {
121 4 ref($_) eq 'HASH'
122 && !$_->{malformed}
123 && defined($_->{scheme})
124 9 ▶ && $_->{scheme} eq $scheme
Condition44%4/9
ref $_ eq 'HASH' && !$_->{'malformed'} && defined $_->{'scheme'} && $_->{'scheme'} eq $scheme
!ll&&!rl&&r
063
ref $_ eq "HASH" and not $_->{"malformed"} and defined $_->{"scheme"}
!ll&&!rl&&r
009
ref $_ eq "HASH" and not $_->{"malformed"}
!ll&&!rl&&r
009
MC/DC25%1/4
ref $_ eq 'HASH' && !$_->{'malformed'} && defined $_->{'scheme'} && $_->{'scheme'} eq $scheme
ref $_ eq "HASH" not $_->{"malformed"} defined $_->{"scheme"} $_->{'scheme'} eq $scheme
Truth table40%2/5
A and not(B) and C && D
A= ref $_ eq "HASH"B= not $_->{"malformed"}C= defined $_->{"scheme"}D= $_->{'scheme'} eq $scheme
ABCDresult
0XXX0
10XX0
110X0
11100
11111
125 } @$challenges;
126  
127 4 ▶ next unless @candidates;
Branch: 1 branch
BranchTrueFalseTotal
unless @candidates 1 3 4
128  
129 3 my $handler = $self->_handler($scheme);
130 3 my $chosen = $handler->select_challenge(\@candidates);
131 3 ▶ return $chosen if $chosen;
Branch: 1 branch
BranchTrueFalseTotal
if $chosen 2 1 3
132 }
133  
134 0 return;
135 }
136  
137 12 ▶sub prepare_authentication {
Subroutine prepare_authentication: called CC=35 SCAR=37.0
Pod documented
138 12 my ($self, %args) = @_;
139  
140 croak "prepare_authentication() requires credentials"
141 unless ref($self->{credentials}) eq 'HASH'
142 12 ▶ || ref($self->{credentials}) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"credentials"} eq "HASH" or ref $self->{"credentials"} eq "CODE" 1 11 12
Condition100%3/3
ref $self->{"credentials"} eq "HASH" or ref $self->{"credentials"} eq "CODE"
l!l&&r!l&&!r
741
MC/DC100%2/2
ref $self->{"credentials"} eq "HASH" or ref $self->{"credentials"} eq "CODE"
ref $self->{"credentials"} eq "HASH" ref $self->{"credentials"} eq "CODE"
Truth table100%3/3
A or B
A= ref $self->{"credentials"} eq "HASH"B= ref $self->{"credentials"} eq "CODE"
ABresult
1X1
011
000
143  
144 croak "challenge_headers must be an array reference"
145 11 ▶ unless ref($args{challenge_headers}) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args{"challenge_headers"} eq "ARRAY" 0 11 11
146  
147 11 ▶ if (exists $args{request}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"request"}) 4 7 11
148 4 my $request = $args{request};
149 4 ▶ croak "request must implement method(), target(), and has_buffered_body()"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $request and $request->can("method") and $request->can("target") and $request->can("has_buffered_body") 1 3 4
Condition66%6/9
ref $request and $request->can("method") and $request->can("target") and $request->can("has_buffered_body")
!ll&&!rl&&r
103
ref $request and $request->can("method") and $request->can("target")
!ll&&!rl&&r
103
ref $request and $request->can("method")
!ll&&!rl&&r
013
MC/DC25%1/4
ref $request and $request->can("method") and $request->can("target") and $request->can("has_buffered_body")
ref $request $request->can("method") $request->can("target") $request->can("has_buffered_body")
Truth table40%2/5
A and B and C and D
A= ref $requestB= $request->can("method")C= $request->can("target")D= $request->can("has_buffered_body")
ABCDresult
0XXX0
10XX0
110X0
11100
11111
150 unless ref($request)
151 && $request->can('method')
152 && $request->can('target')
153 && $request->can('has_buffered_body');
154  
155 $args{method} = $request->method
156 3 ▶ unless exists $args{method};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"method"} 2 1 3
157 $args{request_target} = $request->target
158 3 ▶ unless exists $args{request_target};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"request_target"} 2 1 3
159 3 ▶ if (!exists($args{entity_body}) && $request->has_buffered_body) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $args{"entity_body"} and $request->has_buffered_body) 2 1 3
Condition66%2/3
not exists $args{"entity_body"} and $request->has_buffered_body
!ll&&!rl&&r
012
MC/DC50%1/2
not exists $args{"entity_body"} and $request->has_buffered_body
not exists $args{"entity_body"} $request->has_buffered_body
Truth table66%2/3
not(A) and B
A= not exists $args{"entity_body"}B= $request->has_buffered_body
ABresult
0X0
100
111
160 2 ▶ croak "request reports a buffered body but does not implement body()"
Branch: 1 branch
BranchTrueFalseTotal
unless $request->can("body") 0 2 2
161 unless $request->can('body');
162 2 $args{entity_body} = $request->body;
163 }
164 }
165  
166 10 my $origin;
167 10 ▶ if (exists $args{origin}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{'origin'}) { } 5 5 10
168 5 _validate_origin($args{origin});
169 3 ▶ if (defined($self->{origin}) && $args{origin} ne $self->{origin}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"origin"} and $args{"origin"} ne $self->{"origin"}) 1 2 3
Condition66%2/3
defined $self->{"origin"} and $args{"origin"} ne $self->{"origin"}
!ll&&!rl&&r
201
MC/DC50%1/2
defined $self->{"origin"} and $args{"origin"} ne $self->{"origin"}
defined $self->{"origin"} $args{"origin"} ne $self->{"origin"}
Truth table66%2/3
A and B
A= defined $self->{"origin"}B= $args{"origin"} ne $self->{"origin"}
ABresult
0X0
100
111
170 1 croak "origin does not match the origin bound at construction";
171 }
172 2 $origin = $args{origin};
173 }
174 else {
175 5 $origin = $self->{origin};
176 5 _validate_origin($origin);
177 }
178  
179 7 my $challenges = $self->parse_challenges(@{ $args{challenge_headers} });
180  
181 7 for my $scheme (@{ $self->{schemes} }) {
182 my @candidates = grep {
183 11 !$_->{malformed}
184 && defined($_->{scheme})
185 15 ▶ && $_->{scheme} eq $scheme
Condition50%3/6
!$_->{'malformed'} && defined $_->{'scheme'} && $_->{'scheme'} eq $scheme
!ll&&!rl&&r
069
not $_->{"malformed"} and defined $_->{"scheme"}
!ll&&!rl&&r
0015
MC/DC33%1/3
!$_->{'malformed'} && defined $_->{'scheme'} && $_->{'scheme'} eq $scheme
not $_->{"malformed"} defined $_->{"scheme"} $_->{'scheme'} eq $scheme
Truth table50%2/4
not(A) and B && C
A= not $_->{"malformed"}B= defined $_->{"scheme"}C= $_->{'scheme'} eq $scheme
ABCresult
0XX0
10X0
1100
1111
186 } @$challenges;
187 11 ▶ next unless @candidates;
Branch: 1 branch
BranchTrueFalseTotal
unless @candidates 2 9 11
188  
189 9 my $handler = $self->_handler($scheme);
190 9 my $challenge = $handler->select_challenge(\@candidates);
191 9 ▶ next unless $challenge;
Branch: 1 branch
BranchTrueFalseTotal
unless $challenge 0 9 9
192  
193 my $context = {
194 scheme => $scheme,
195 origin => $origin,
196 realm => $challenge->{params}{realm},
197 9 challenge => $challenge,
198 };
199  
200 9 my $credentials;
201 9 ▶ if (ref($self->{credentials}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'credentials'} eq 'CODE') { } 3 6 9
202 3 $credentials = $self->{credentials}->($context);
203 3 ▶ next unless defined $credentials;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $credentials 1 2 3
204 2 ▶ croak "credentials callback must return a hash reference or undef"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $credentials eq "HASH" 0 2 2
205 unless ref($credentials) eq 'HASH';
206 }
207 else {
208 $credentials = _static_credentials_for_scheme(
209 6 $self->{credentials}, $scheme,
210 );
211 6 ▶ next unless $credentials;
Branch: 1 branch
BranchTrueFalseTotal
unless $credentials 1 5 6
212 }
213  
214 7 my $value;
215 7 ▶ if ($scheme eq 'basic') {
Branch: 3 branches
BranchTrueFalseTotal
if ($scheme eq 'basic') { } 2 5 7
elsif ($scheme eq 'bearer') { } 1 4 5
elsif ($scheme eq 'digest') { } 4 0 4
216 2 _require_fields($credentials, qw(username password));
217 2 $value = Uniform::HTTP::Auth::Basic->authorization(
218 %$credentials,
219 challenge => $challenge,
220 );
221 }
222 elsif ($scheme eq 'bearer') {
223 1 _require_fields($credentials, qw(token));
224 1 $value = Uniform::HTTP::Auth::Bearer->authorization(
225 %$credentials,
226 challenge => $challenge,
227 );
228 }
229 elsif ($scheme eq 'digest') {
230 4 _require_fields($credentials, qw(username password));
231 croak "method is required for Digest authentication"
232 4 ▶ unless defined($args{method}) && !ref($args{method});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"method"} and not ref $args{"method"} 0 4 4
Condition33%1/3
defined $args{"method"} and not ref $args{"method"}
!ll&&!rl&&r
004
MC/DC0%0/2
defined $args{"method"} and not ref $args{"method"}
defined $args{"method"} not ref $args{"method"}
Truth table33%1/3
A and not(B)
A= defined $args{"method"}B= not ref $args{"method"}
ABresult
0X0
100
111
233 croak "request_target is required for Digest authentication"
234 4 ▶ unless defined($args{request_target}) && !ref($args{request_target});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"request_target"} and not ref $args{"request_target"} 0 4 4
Condition33%1/3
defined $args{"request_target"} and not ref $args{"request_target"}
!ll&&!rl&&r
004
MC/DC0%0/2
defined $args{"request_target"} and not ref $args{"request_target"}
defined $args{"request_target"} not ref $args{"request_target"}
Truth table33%1/3
A and not(B)
A= defined $args{"request_target"}B= not ref $args{"request_target"}
ABresult
0X0
100
111
235  
236 my %digest_args = (
237 %$credentials,
238 challenge => $challenge,
239 origin => $origin,
240 method => $args{method},
241 request_target => $args{request_target},
242 4 );
243 $digest_args{entity_body} = $args{entity_body}
244 4 ▶ if exists $args{entity_body};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"entity_body"} 3 1 4
245  
246 4 $value = $handler->authorization(%digest_args);
247 3 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 3 3
248 }
249  
250 return {
251 6 scheme => $scheme,
252 value => $value,
253 challenge => $challenge,
254 };
255 }
256  
257 0 return;
258 }
259  
260 12 ▶sub _handler {
Subroutine _handler: called CC=2 SCAR=6.9
261 12 my ($self, $scheme) = @_;
262 12 ▶ return $self->{digest} if $scheme eq 'digest';
Branch: 1 branch
BranchTrueFalseTotal
if $scheme eq "digest" 7 5 12
263 5 return $SCHEME_CLASS{$scheme};
264 }
265  
266 5 ▶sub _validate_static_credentials {
Subroutine _validate_static_credentials: called CC=9 SCAR=22.1
267 5 my ($credentials) = @_;
268  
269 5 my $has_token = exists $credentials->{token};
270 5 my $has_username = exists $credentials->{username};
271 5 my $has_password = exists $credentials->{password};
272  
273 5 ▶ croak "static credentials require both username and password"
Branch: 1 branch
BranchTrueFalseTotal
if $has_username != $has_password 1 4 5
274 if $has_username != $has_password;
275 4 ▶ croak "static credentials require token or username/password"
Branch: 1 branch
BranchTrueFalseTotal
unless $has_token or $has_username and $has_password 1 3 4
Condition83%5/6
$has_token or $has_username and $has_password
l!l&&r!l&&!r
121
$has_username and $has_password
!ll&&!rl&&r
102
MC/DC66%2/3
$has_token or $has_username and $has_password
$has_token $has_username $has_password
Truth table75%3/4
A or B and C
A= $has_tokenB= $has_usernameC= $has_password
ABCresult
1XX1
0111
00X0
0100
276 unless $has_token || ($has_username && $has_password);
277  
278 3 for my $field (qw(token username password)) {
279 9 ▶ next unless exists $credentials->{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $credentials->{$field} 4 5 9
280 croak "credential '$field' must be defined"
281 5 ▶ unless defined $credentials->{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $credentials->{$field} 0 5 5
282 croak "credential '$field' must be a plain scalar"
283 5 ▶ if ref($credentials->{$field});
Branch: 1 branch
BranchTrueFalseTotal
if ref $credentials->{$field} 0 5 5
284 }
285 }
286  
287 6 ▶sub _static_credentials_for_scheme {
Subroutine _static_credentials_for_scheme: called CC=5 SCAR=16.2
288 6 my ($credentials, $scheme) = @_;
289  
290 6 ▶ if ($scheme eq 'bearer') {
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme eq 'bearer') { } 1 5 6
291 1 ▶ return unless exists $credentials->{token};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $credentials->{"token"} 0 1 1
292 }
293 else {
294 return unless exists($credentials->{username})
295 5 ▶ && exists($credentials->{password});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $credentials->{"username"} and exists $credentials->{"password"} 1 4 5
Condition66%2/3
exists $credentials->{"username"} and exists $credentials->{"password"}
!ll&&!rl&&r
104
MC/DC50%1/2
exists $credentials->{"username"} and exists $credentials->{"password"}
exists $credentials->{"username"} exists $credentials->{"password"}
Truth table66%2/3
A and B
A= exists $credentials->{"username"}B= exists $credentials->{"password"}
ABresult
0X0
100
111
296 }
297  
298 5 return { %$credentials };
299 }
300  
301 7 ▶sub _require_fields {
Subroutine _require_fields: called CC=5 SCAR=18.2
302 7 my ($credentials, @fields) = @_;
303 7 for my $field (@fields) {
304 croak "credentials require '$field'"
305 13 ▶ unless exists($credentials->{$field}) && defined($credentials->{$field});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $credentials->{$field} and defined $credentials->{$field} 0 13 13
Condition33%1/3
exists $credentials->{$field} and defined $credentials->{$field}
!ll&&!rl&&r
0013
MC/DC0%0/2
exists $credentials->{$field} and defined $credentials->{$field}
exists $credentials->{$field} defined $credentials->{$field}
Truth table33%1/3
A and B
A= exists $credentials->{$field}B= defined $credentials->{$field}
ABresult
0X0
100
111
306 croak "credential '$field' must be a plain scalar"
307 13 ▶ if ref($credentials->{$field});
Branch: 1 branch
BranchTrueFalseTotal
if ref $credentials->{$field} 0 13 13
308 }
309 }
310  
311 15 ▶sub _validate_origin {
Subroutine _validate_origin: called CC=6 SCAR=19.6
312 15 my ($origin) = @_;
313 15 ▶ croak "origin is required"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $origin and not ref $origin and length $origin 0 15 15
Condition33%2/6
defined $origin and not ref $origin and length $origin
!ll&&!rl&&r
0015
defined $origin and not ref $origin
!ll&&!rl&&r
0015
MC/DC0%0/3
defined $origin and not ref $origin and length $origin
defined $origin not ref $origin length $origin
Truth table25%1/4
A and not(B) and C
A= defined $originB= not ref $originC= length $origin
ABCresult
0XX0
10X0
1100
1111
314 unless defined($origin) && !ref($origin) && length($origin);
315 15 ▶ croak "origin must not contain whitespace or control characters"
Branch: 1 branch
BranchTrueFalseTotal
if $origin =~ /[\x00-\x20\x7f]/ 1 14 15
316 if $origin =~ /[\x00-\x20\x7f]/;
317 14 ▶ croak "origin must be a normalized origin without credentials or path"
Branch: 1 branch
BranchTrueFalseTotal
unless $origin =~ m[\A[A-Za-z][A-Za-z0-9+.-]*://[^/?#@]+\z] 1 13 14
318 unless $origin =~ m{\A[A-Za-z][A-Za-z0-9+.-]*://[^/?#@]+\z};
319 }
320  
321 11 my $TCHAR = q{!#$%&'*+\-.^_`|~0-9A-Za-z};
322 11 my $TOKEN_RE = qr/[$TCHAR]+/;
323 11 my $TOKEN68_RE = qr/[A-Za-z0-9\-._~+\/]+={0,}/;
324  
325 24 ▶sub _parse_field {
Subroutine _parse_field: called CC=17 SCAR=32.0
326 24 my ($value) = @_;
327  
328 24 ▶ if ($value =~ /[\r\n]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /[\r\n]/) 1 23 24
329 return [{
330 1 scheme => undef,
331 raw => $value,
332 params => {},
333 token68 => undef,
334 malformed => 1,
335 error => 'challenge header value contains a newline',
336 }];
337 }
338  
339 23 my @parts = _split_top_level_commas($value);
340 23 my @out;
341 my $current;
342  
343 23 for my $part (@parts) {
344 75 my $trim = $part;
345 75 $trim =~ s/\A[ \t]+//;
346 75 $trim =~ s/[ \t]+\z//;
347 75 ▶ next unless length $trim;
Branch: 1 branch
BranchTrueFalseTotal
unless length $trim 0 75 75
348  
349 75 ▶ if ($current && $current->{_mode} && $current->{_mode} eq 'params') {
Branch: 1 branch
BranchTrueFalseTotal
if ($current and $current->{"_mode"} and $current->{"_mode"} eq "params") 52 23 75
Condition66%4/6
$current and $current->{"_mode"} and $current->{"_mode"} eq "params"
!ll&&!rl&&r
23052
$current and $current->{"_mode"}
!ll&&!rl&&r
23052
MC/DC33%1/3
$current and $current->{"_mode"} and $current->{"_mode"} eq "params"
$current $current->{"_mode"} $current->{"_mode"} eq "params"
Truth table50%2/4
A and B and C
A= $currentB= $current->{"_mode"}C= $current->{"_mode"} eq "params"
ABCresult
0XX0
10X0
1100
1111
350 52 my ($name, $param_value, $error) = _parse_auth_param($trim);
351 52 ▶ if (defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) 48 4 52
352 48 $current->{raw} .= ',' . $part;
353 48 ▶ if (exists $current->{params}{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $current->{'params'}{$name}) { } 1 47 48
354 1 $current->{malformed} = 1;
355 1 ▶ $current->{error} ||= "duplicate authentication parameter '$name'";
Condition50%1/2
$current->{"error"} ||= "duplicate authentication parameter '${name}'"
l!l
01
MC/DC0%0/1
$current->{"error"} ||= "duplicate authentication parameter '${name}'"
$current->{"error"}
Truth table50%1/2
A
A= $current->{"error"}
Aresult
11
00
356 }
357 else {
358 47 $current->{params}{$name} = $param_value;
359 }
360 48 next;
361 }
362  
363 4 ▶ if ($trim =~ /\A$TOKEN_RE[ \t]*=/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trim =~ /\A${TOKEN_RE}[ \t]*=/) 0 4 4
364 0 $current->{raw} .= ',' . $part;
365 0 $current->{malformed} = 1;
366 0 ▶ $current->{error} ||= $error || 'malformed authentication parameter';
Condition0%0/5
$current->{"error"} ||= $error || "malformed authentication parameter"
l!l&&r!l&&!r
000
$error || 'malformed authentication parameter'
l!l
00
MC/DC0%0/2
$current->{"error"} ||= $error || "malformed authentication parameter"
$current->{"error"} $error
Truth table0%0/3
A ||= B
A= $current->{"error"}B= $error
ABresult
1X1
011
000
367 0 next;
368 }
369 }
370  
371 27 ▶ push @out, _finish_challenge($current) if $current;
Branch: 1 branch
BranchTrueFalseTotal
if $current 4 23 27
372 27 $current = _start_challenge($part);
373 }
374  
375 23 ▶ push @out, _finish_challenge($current) if $current;
Branch: 1 branch
BranchTrueFalseTotal
if $current 23 0 23
376  
377 23 ▶ if (!@out && length $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @out and length $value) 0 23 23
Condition33%1/3
not @out and length $value
!ll&&!rl&&r
2300
MC/DC0%0/2
not @out and length $value
not @out length $value
Truth table33%1/3
not(A) and B
A= not @outB= length $value
ABresult
0X0
100
111
378 0 push @out, {
379 scheme => undef,
380 raw => $value,
381 params => {},
382 token68 => undef,
383 malformed => 1,
384 error => 'no authentication challenge found',
385 };
386 }
387  
388 23 return \@out;
389 }
390  
391 27 ▶sub _start_challenge {
Subroutine _start_challenge: called CC=7 SCAR=19.5
392 27 my ($raw) = @_;
393 27 my $trim = $raw;
394 27 $trim =~ s/\A[ \t]+//;
395 27 $trim =~ s/[ \t]+\z//;
396  
397 27 my $challenge = {
398 scheme => undef,
399 raw => $raw,
400 params => {},
401 token68 => undef,
402 malformed => 0,
403 error => undef,
404 _mode => undef,
405 };
406  
407 27 ▶ unless ($trim =~ /\A($TOKEN_RE)(?:[ \t]+(.*))?\z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($trim =~ /\A($TOKEN_RE)(?:[ \t]+(.*))?\z/) 1 26 27
408 1 $challenge->{malformed} = 1;
409 1 $challenge->{error} = 'malformed authentication challenge';
410 1 return $challenge;
411 }
412  
413 26 $challenge->{scheme} = lc $1;
414 26 my $rest = $2;
415 26 ▶ return $challenge unless defined($rest) && length($rest);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest and length $rest 0 26 26
Condition33%1/3
defined $rest and length $rest
!ll&&!rl&&r
0026
MC/DC0%0/2
defined $rest and length $rest
defined $rest length $rest
Truth table33%1/3
A and B
A= defined $restB= length $rest
ABresult
0X0
100
111
416  
417 26 $rest =~ s/[ \t]+\z//;
418  
419 26 ▶ if ($rest =~ /\A($TOKEN68_RE)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ /\A($TOKEN68_RE)\z/) 1 25 26
420 1 $challenge->{token68} = $1;
421 1 $challenge->{_mode} = 'token68';
422 1 return $challenge;
423 }
424  
425 25 my ($name, $value, $error) = _parse_auth_param($rest);
426 25 ▶ unless (defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 1 24 25
427 1 $challenge->{malformed} = 1;
428 1 ▶ $challenge->{error} = $error || 'malformed authentication parameters';
Condition50%1/2
$error || 'malformed authentication parameters'
l!l
10
MC/DC0%0/1
$error || 'malformed authentication parameters'
$error
Truth table50%1/2
A
A= $error
Aresult
11
00
429 1 return $challenge;
430 }
431  
432 24 $challenge->{params}{$name} = $value;
433 24 $challenge->{_mode} = 'params';
434 24 return $challenge;
435 }
436  
437 27 ▶sub _finish_challenge {
Subroutine _finish_challenge: called CC=1 SCAR=0.0
438 27 my ($challenge) = @_;
439 27 delete $challenge->{_mode};
440 27 $challenge->{raw} =~ s/\A[ \t]+//;
441 27 $challenge->{raw} =~ s/[ \t]+\z//;
442 27 return $challenge;
443 }
444  
445 77 ▶sub _parse_auth_param {
Subroutine _parse_auth_param: called CC=8 SCAR=21.1
446 77 my ($text) = @_;
447  
448 77 ▶ unless ($text =~ /\A($TOKEN_RE)[ \t]*=[ \t]*(.*)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($text =~ /\A($TOKEN_RE)[ \t]*=[ \t]*(.*)\z/) 4 73 77
449 4 return (undef, undef, 'authentication parameter requires name=value syntax');
450 }
451  
452 73 my $name = lc $1;
453 73 my $raw_value = $2;
454  
455 73 ▶ if ($raw_value =~ /\A($TOKEN_RE)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw_value =~ /\A($TOKEN_RE)\z/) 16 57 73
456 16 return ($name, $1, undef);
457 }
458  
459 57 ▶ if ($raw_value =~ /\A"(.*)"\z/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw_value =~ /\A"(.*)"\z/s) 57 0 57
460 57 my $inner = $1;
461 57 ▶ return (undef, undef, 'quoted authentication parameter contains a newline')
Branch: 1 branch
BranchTrueFalseTotal
if $inner =~ /[\r\n]/ 0 57 57
462 if $inner =~ /[\r\n]/;
463  
464 57 my $decoded = '';
465 57 ▶ while (length $inner) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $inner) 58 56 114
466 58 ▶ if ($inner =~ s/\A\\([\x09\x20-\x7e\x80-\xff])//s) {
Branch: 2 branches
BranchTrueFalseTotal
if ($inner =~ s/\A\\([\x09\x20-\x7e\x80-\xff])//s) { } 0 58 58
elsif ($inner =~ s/\A([\x09\x20-\x21\x23-\x5b\x5d-\x7e\x80-\xff]+)//s) { } 57 1 58
467 0 $decoded .= $1;
468 }
469 elsif ($inner =~ s/\A([\x09\x20-\x21\x23-\x5b\x5d-\x7e\x80-\xff]+)//s) {
470 57 $decoded .= $1;
471 }
472 else {
473 1 return (undef, undef, 'malformed quoted authentication parameter');
474 }
475 }
476 56 return ($name, $decoded, undef);
477 }
478  
479 0 return (undef, undef, 'authentication parameter value must be a token or quoted string');
480 }
481  
482 23 ▶sub _split_top_level_commas {
Subroutine _split_top_level_commas: called CC=8 SCAR=20.9
483 23 my ($text) = @_;
484 23 my @parts;
485 23 my $start = 0;
486 23 my $quoted = 0;
487 23 my $escaped = 0;
488  
489 23 ▶ for (my $i = 0; $i < length($text); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $text) 1472 23 1495
490 1472 my $ch = substr($text, $i, 1);
491  
492 1472 ▶ if ($quoted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quoted) 540 932 1472
493 540 ▶ if ($escaped) {
Branch: 3 branches
BranchTrueFalseTotal
if ($escaped) { } 0 540 540
elsif ($ch eq '\\') { } 0 540 540
elsif ($ch eq '"') { } 57 483 540
494 0 $escaped = 0;
495 }
496 elsif ($ch eq '\\') {
497 0 $escaped = 1;
498 }
499 elsif ($ch eq '"') {
500 57 $quoted = 0;
501 }
502 540 next;
503 }
504  
505 932 ▶ if ($ch eq '"') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"") 57 875 932
506 57 $quoted = 1;
507 57 next;
508 }
509  
510 875 ▶ if ($ch eq ',') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq ",") 52 823 875
511 52 push @parts, substr($text, $start, $i - $start);
512 52 $start = $i + 1;
513 }
514 }
515  
516 23 push @parts, substr($text, $start);
517 23 return @parts;
518 }
519  
520 11 1;
521  
522 __END__
« HTTP/Auth/Digest.pm ↑ Summary HTTP/Auth/Basic.pm »