lib/Dancer/Plugin/Auth/Basic.pm

filter: Statementstmt 98.6% 73 / 74 Branchbran 96.1% 25 / 26 Conditioncond 84.0% 21 / 25 MC/DCmcdc 75.0% 12 / 16 Subroutinesub 100% 10 / 10 Podpod n/a totaltotal 93.3% 141 / 151 CC 22 SCAR 22.1
CC
22
Cov
95%
_auth_basic
15.3
_check_password
6.0
__ANON__
3.0
CRAP 9.1
↑ Summary
1 package Dancer::Plugin::Auth::Basic;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: Basic HTTP authentication for Dancer web apps
7  
8 6 our $VERSION = '0.030'; # VERSION
9  
10 6 ▶use Dancer ':syntax';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use Dancer::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Dancer::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 6 my $settings = plugin_setting;
17  
18 # Protected paths defined in the configuration
19 6 my $paths = {};
20 # "Global" users
21 6 my $users = {};
22  
23 6 ▶if (exists $settings->{paths}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $settings->{"paths"}) 5 1 6
24 5 $paths = $settings->{paths};
25 }
26  
27 6 ▶if (exists $settings->{users}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $settings->{"users"}) 1 5 6
28 1 $users = $settings->{users};
29 }
30  
31 22 ▶sub _check_password {
Subroutine _check_password: called CC=6 SCAR=17.9
32 22 my ($password, $text) = @_;
33
34 22 my $crypt;
35
36 22 ▶ if (($crypt = $password =~ /^\$\w+\$/) || $password =~ /^\{\w+\}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($crypt = $password =~ /^\$\w+\$/ or $password =~ /^\{\w+\}/) { } 7 15 22
Condition100%3/3
$crypt = $password =~ /^\$\w+\$/ or $password =~ /^\{\w+\}/
l!l&&r!l&&!r
1615
MC/DC100%2/2
$crypt = $password =~ /^\$\w+\$/ or $password =~ /^\{\w+\}/
$crypt = $password =~ /^\$\w+\$/ $password =~ /^\{\w+\}/
Truth table100%3/3
A or B
A= $crypt = $password =~ /^\$\w+\$/B= $password =~ /^\{\w+\}/
ABresult
1X1
011
000
37 # Crypt or RFC 2307 format
38 eval {
39 7 require Authen::Passphrase;
40 6 1;
41 }
42 7 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Authen::Passphrase; 1 } } or do { error(q[Can't use Authen::Passphrase: ] . $@); return 0 } 6 1 7
43 1 error "Can't use Authen::Passphrase: " . $@;
44 1 return 0;
45 };
46
47 6 my $ppr;
48
49 eval {
50 6 ▶ $ppr = $crypt ? Authen::Passphrase->from_crypt($password) :
Branch: 1 branch
BranchTrueFalseTotal
$crypt ? : 1 5 6
51 Authen::Passphrase->from_rfc2307($password);
52 }
53 6 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $ppr = $crypt ? "Authen::Passphrase"->from_crypt($password) : "Authen::Passphrase"->from_rfc2307($password) } } or do { error(q[Can't construct an Authen::Passphrase recognizer object: ] . $@); return 0 } 4 2 6
54 2 error "Can't construct an Authen::Passphrase recognizer object: " .
55 $@;
56 2 return 0;
57 };
58
59 4 return $ppr->match($text);
60 }
61 else {
62 # Password in cleartext
63 15 return $password eq $text;
64 }
65 }
66  
67 32 ▶sub _auth_basic {
Subroutine _auth_basic: called CC=15 SCAR=27.3
68 32 my (%options) = @_;
69  
70 # Get authentication data from request
71 32 my $auth = request->header('Authorization');
72
73 32 my $authorized = undef;
74
75 32 ▶ if (defined $auth && $auth =~ /^Basic (.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $auth and $auth =~ /^Basic (.*)$/) 23 9 32
Condition66%2/3
defined $auth and $auth =~ /^Basic (.*)$/
!ll&&!rl&&r
9023
MC/DC50%1/2
defined $auth and $auth =~ /^Basic (.*)$/
defined $auth $auth =~ /^Basic (.*)$/
Truth table66%2/3
A and B
A= defined $authB= $auth =~ /^Basic (.*)$/
ABresult
0X0
100
111
76 23 ▶ my ($user, $password) = split(/:/, (MIME::Base64::decode($1) || ":"));
Condition50%1/2
&MIME::Base64::decode($1) || ':'
l!l
230
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
77
78 23 ▶ if (exists $options{user}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options{"user"}) 8 15 23
79 # A single user is defined
80 $authorized = $user eq $options{user} &&
81 8 ▶ _check_password($options{password}, $password);
Condition100%3/3
$user eq $options{'user'} && _check_password($options{'password'}, $password)
!ll&&!rl&&r
116
MC/DC100%2/2
$user eq $options{'user'} && _check_password($options{'password'}, $password)
$user eq $options{'user'} _check_password($options{'password'}, $password)
Truth table100%3/3
A && B
A= $user eq $options{'user'}B= _check_password($options{'password'}, $password)
ABresult
0X0
100
111
82 }
83
84 23 ▶ if (!defined($authorized) && exists($options{users})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $authorized and exists $options{"users"}) 13 10 23
Condition100%3/3
not defined $authorized and exists $options{"users"}
!ll&&!rl&&r
8213
MC/DC100%2/2
not defined $authorized and exists $options{"users"}
not defined $authorized exists $options{"users"}
Truth table100%3/3
not(A) and B
A= not defined $authorizedB= exists $options{"users"}
ABresult
0X0
100
111
85 # Multiple users are defined
86 $authorized = exists($options{users}->{$user}) &&
87 13 ▶ _check_password($options{users}->{$user}, $password);
Condition100%3/3
exists $options{'users'}{$user} && _check_password($options{'users'}{$user}, $password)
!ll&&!rl&&r
238
MC/DC100%2/2
exists $options{'users'}{$user} && _check_password($options{'users'}{$user}, $password)
exists $options{'users'}{$user} _check_password($options{'users'}{$user}, $password)
Truth table100%3/3
A && B
A= exists $options{'users'}{$user}B= _check_password($options{'users'}{$user}, $password)
ABresult
0X0
100
111
88 }
89
90 23 ▶ if (!$authorized && defined($users)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $authorized and defined $users) 9 14 23
Condition66%2/3
not $authorized and defined $users
!ll&&!rl&&r
1409
MC/DC50%1/2
not $authorized and defined $users
not $authorized defined $users
Truth table66%2/3
not(A) and B
A= not $authorizedB= defined $users
ABresult
0X0
100
111
91 # Use the "global" users list
92 $authorized = exists $users->{$user} &&
93 9 ▶ _check_password($users->{$user}, $password);
Condition66%2/3
exists $users->{$user} && _check_password($users->{$user}, $password)
!ll&&!rl&&r
504
MC/DC50%1/2
exists $users->{$user} && _check_password($users->{$user}, $password)
exists $users->{$user} _check_password($users->{$user}, $password)
Truth table66%2/3
A && B
A= exists $users->{$user}B= _check_password($users->{$user}, $password)
ABresult
0X0
100
111
94 }
95
96 23 ▶ if ($authorized) {
Branch: 1 branch
BranchTrueFalseTotal
if ($authorized) 18 5 23
97 # Authorization successful
98 18 request->env->{REMOTE_USER} = $user;
99 18 return 1;
100 }
101
102 5 ▶ if (!defined($authorized)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $authorized) 0 5 5
103 # No users defined? NONE SHALL PASS!
104 0 warning __PACKAGE__ . ": No user/password defined";
105 }
106 }
107
108 14 my $content = "Authorization required";
109
110 return halt(Dancer::Response->new(
111 status => 401,
112 content => $content,
113 headers => [
114 'Content-Type' => 'text/plain',
115 'Content-Length' => length($content),
116 'WWW-Authenticate' => 'Basic realm="' . ($options{realm} ||
117 14 ▶ "Restricted area") . '"'
Condition100%2/2
$options{'realm'} || 'Restricted area'
l!l
113
MC/DC100%1/1
$options{'realm'} || 'Restricted area'
$options{'realm'}
Truth table100%2/2
A
A= $options{'realm'}
Aresult
11
00
118 ]
119 ));
120 }
121  
122 35 ▶my $check = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
123 # Check if the request matches one of the protected paths (reverse sort the
124 # paths to find the longest matching path first)
125 35 foreach my $path (reverse sort keys %$paths) {
126 47 my $path_re = '^' . quotemeta($path);
127 47 ▶ if (request->path_info =~ qr{$path_re}) {
Branch: 1 branch
BranchTrueFalseTotal
if (request()->path_info =~ qr/$path_re/) 27 20 47
128 27 _auth_basic %{$paths->{$path}};
129 15 last;
130 }
131 }
132 6 };
133  
134 # Dynamic paths
135 6 hook before => $check;
136 # Static paths
137 6 hook before_file_render => $check;
138  
139 6 register auth_basic => \&_auth_basic;
140 6 register_plugin;
141  
142 6 1; # End of Dancer::Plugin::Auth::Basic
143  
144 __END__
↑ Summary