blib/lib/Plack/Middleware/Auth/Basic.pm

filter: Statementstmt 91.1% 31 / 34 Branchbran 64.2% 9 / 14 Conditioncond 42.8% 3 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 88.8% 8 / 9 Podpod 66.6% 2 / 3 totaltotal 74.6% 53 / 71 CC 11 SCAR 15.8
CC
11
Cov
78%
prepare_app
8.8
call
6.1
unauthorized
2.0
CRAP 4.8
« blib/lib/Plack/Util.pm ↑ Summary blib/lib/Plack/Middleware/Static.pm »
1 package Plack::Middleware::Auth::Basic;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use parent qw(Plack::Middleware);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Plack::Util::Accessor qw( realm authenticator );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶sub prepare_app {
Subroutine prepare_app: called CC=5 SCAR=21.7
Pod documented
9 4 my $self = shift;
10  
11 4 ▶ my $auth = $self->authenticator or die 'authenticator is not set';
Branch: 1 branch
BranchTrueFalseTotal
my $auth = $self->authenticator or die "authenticator is not set" 4 0 4
12 4 ▶ if (Scalar::Util::blessed($auth) && $auth->can('authenticate')) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($auth) and $auth->can('authenticate')) { } 0 4 4
elsif (ref $auth ne 'CODE') { } 0 4 4
Condition33%1/3
builtin::blessed($auth) and $auth->can("authenticate")
!ll&&!rl&&r
400
MC/DC0%0/2
builtin::blessed($auth) and $auth->can("authenticate")
builtin::blessed($auth) $auth->can("authenticate")
Truth table33%1/3
A and B
A= builtin::blessed($auth)B= $auth->can("authenticate")
ABresult
0X0
100
111
13 0 ▶ $self->authenticator(sub { $auth->authenticate(@_[0,1]) }); # because Authen::Simple barfs on 3 params
Subroutine __ANON__: not called CC=1 SCAR=6.9
14 } elsif (ref $auth ne 'CODE') {
15 0 die 'authenticator should be a code reference or an object that responds to authenticate()';
16 }
17 }
18  
19 10 ▶sub call {
Subroutine call: called CC=6 SCAR=18.2
Pod documented
20 10 my($self, $env) = @_;
21  
22 my $auth = $env->{HTTP_AUTHORIZATION}
23 10 ▶ or return $self->unauthorized;
Branch: 1 branch
BranchTrueFalseTotal
my $auth = $env->{"HTTP_AUTHORIZATION"} or return $self->unauthorized 8 2 10
24  
25 # note the 'i' on the regex, as, according to RFC2617 this is a
26 # "case-insensitive token to identify the authentication scheme"
27 8 ▶ if ($auth =~ /^Basic (.*)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auth =~ /^Basic (.*)$/i) 8 0 8
28 8 ▶ my($user, $pass) = split /:/, (MIME::Base64::decode($1) || ":"), 2;
Condition50%1/2
&MIME::Base64::decode($1) || ':'
l!l
80
MC/DC0%0/1
&MIME::Base64::decode($1) || ':'
&MIME::Base64::decode($1)
Truth table50%1/2
A
A= &MIME::Base64::decode($1)
Aresult
11
00
29 8 ▶ $pass = '' unless defined $pass;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pass 0 8 8
30 8 ▶ if ($self->authenticator->($user, $pass, $env)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->authenticator->($user, $pass, $env)) 6 2 8
31 6 $env->{REMOTE_USER} = $user;
32 6 return $self->app->($env);
33 }
34 }
35  
36 2 return $self->unauthorized;
37 }
38  
39 4 ▶sub unauthorized {
Subroutine unauthorized: called CC=2 SCAR=7.1
Pod undocumented
40 4 my $self = shift;
41 4 my $body = 'Authorization required';
42 return [
43 4 ▶ 401,
Condition50%1/2
$self->realm || 'restricted area'
l!l
04
MC/DC0%0/1
$self->realm || 'restricted area'
$self->realm
Truth table50%1/2
A
A= $self->realm
Aresult
11
00
44 [ 'Content-Type' => 'text/plain',
45 'Content-Length' => length $body,
46 'WWW-Authenticate' => 'Basic realm="' . ($self->realm || "restricted area") . '"' ],
47 [ $body ],
48 ];
49 }
50  
51 4 1;
52  
53 __END__
« blib/lib/Plack/Util.pm ↑ Summary blib/lib/Plack/Middleware/Static.pm »