lib/Mojolicious/Plugin/BasicAuthPlus.pm

filter: Statementstmt 50.4% 61 / 121 Branchbran 34.8% 30 / 86 Conditioncond 37.2% 16 / 43 MC/DCmcdc 34.7% 8 / 23 Subroutinesub 92.8% 13 / 14 Podpod 100% 1 / 1 totaltotal 44.7% 129 / 288 CC 62 SCAR 63.9
CC
62
Cov
43%
_check_ldap
1260.0
_check_auth
23.0
_check_simple
3.5
CRAP 594.7
↑ Summary script/sample.pl »
1 package Mojolicious::Plugin::BasicAuthPlus;
2  
3 3 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Mojo::ByteStream;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Authen::Simple::Password;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Authen::Simple::Passwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Net::LDAP;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 our $VERSION = '1.0';
10  
11 1 ▶sub register {
Subroutine register: called CC=1 SCAR=0.0
Pod documented
12 1 my ($plugin, $app) = @_;
13  
14 $app->renderer->add_helper(
15 25 ▶ basic_auth => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
16 25 my $self = shift;
17  
18 # Sent credentials
19 25 ▶ my $auth = $self->req->url->to_abs->userinfo || '';
Condition100%2/2
$self->req->url->to_abs->userinfo || ''
l!l
223
MC/DC100%1/1
$self->req->url->to_abs->userinfo || ''
$self->req->url->to_abs->userinfo
Truth table100%2/2
A
A= $self->req->url->to_abs->userinfo
Aresult
11
00
20  
21 25 my ($hash_ref, $status) = $plugin->_check_auth($self, $auth, @_);
22 25 ▶ if ($status) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status) { } 18 7 25
23 18 return ($hash_ref, $status);
24 }
25 else {
26 # Not verified
27 7 my $realm = $hash_ref->{realm};
28 7 return $plugin->_password_prompt($self, $realm);
29 }
30 }
31 1 );
32 }
33  
34 25 ▶sub _check_auth {
Subroutine _check_auth: called CC=20 SCAR=31.4
35 25 my ($plugin, $c, $auth, @params) = @_;
36  
37 # Required credentials
38 25 my ($realm, $password, $username) = $plugin->_expected_auth(@params);
39 25 ▶ my $callback = $password if ref $password eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $password eq "CODE" 8 17 25
40 25 ▶ my $params = $password if ref $password eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $password eq "HASH" 7 18 25
41  
42 # No credentials entered
43 25 ▶ return {realm => $realm} if !$auth and !$callback and !$params;
Branch: 1 branch
BranchTrueFalseTotal
unless $auth or $callback or $params 1 24 25
Condition100%6/6
$auth or $callback or $params
l!l&&r!l&&!r
2311
$auth or $callback
l!l&&r!l&&!r
2212
MC/DC100%3/3
$auth or $callback or $params
$auth $callback $params
Truth table100%4/4
A or B or C
A= $authB= $callbackC= $params
ABCresult
1XX1
01X1
0011
0000
44  
45 24 my ($auth_username, $auth_password) = _split_auth($auth);
46  
47 # Hash for return data
48 24 my %data;
49 24 ▶ $data{username} = $auth_username if $auth_username;
Branch: 1 branch
BranchTrueFalseTotal
if $auth_username 20 4 24
50  
51 # Verification within callback
52 24 ▶ return (\%data, 1) if $callback and $callback->(_split_auth($auth));
Branch: 1 branch
BranchTrueFalseTotal
if $callback and &$callback(_split_auth($auth)) 5 19 24
Condition100%3/3
$callback and &$callback(_split_auth($auth))
!ll&&!rl&&r
1635
MC/DC100%2/2
$callback and &$callback(_split_auth($auth))
$callback &$callback(_split_auth($auth))
Truth table100%3/3
A and B
A= $callbackB= &$callback(_split_auth($auth))
ABresult
0X0
100
111
53  
54 # Verified with realm => username => password syntax
55 19 ▶ return (\%data, 1) if $auth eq ($username || '') . ":$password";
Branch: 1 branch
BranchTrueFalseTotal
if $auth eq ($username || "") . ":$password" 6 13 19
Condition100%2/2
$username || ''
l!l
514
MC/DC100%1/1
$username || ''
$username
Truth table100%2/2
A
A= $username
Aresult
11
00
56  
57 # Verified via simple, passwd file, LDAP, or Active Directory.
58 13 ▶ if ($auth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auth) 11 2 13
59 11 ▶ if ($params->{'username'} and $params->{'password'}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($params->{'username'} and $params->{'password'}) { } 5 6 11
elsif ($params->{'path'}) { } 2 4 6
elsif ($params->{'host'}) { } 0 4 4
Condition66%2/3
$params->{"username"} and $params->{"password"}
!ll&&!rl&&r
605
MC/DC50%1/2
$params->{"username"} and $params->{"password"}
$params->{"username"} $params->{"password"}
Truth table66%2/3
A and B
A= $params->{"username"}B= $params->{"password"}
ABresult
0X0
100
111
60 5 ▶ return (\%data, 1) if $plugin->_check_simple($c, $auth, $params);
Branch: 1 branch
BranchTrueFalseTotal
if $plugin->_check_simple($c, $auth, $params) 5 0 5
61 }
62 elsif ($params->{'path'}) {
63 2 ▶ return (\%data, 1) if $plugin->_check_passwd($c, $auth, $params);
Branch: 1 branch
BranchTrueFalseTotal
if $plugin->_check_passwd($c, $auth, $params) 2 0 2
64 }
65 elsif ($params->{'host'}) {
66 0 my ($ok, $ldap) = $plugin->_check_ldap($c, $auth, $params);
67 0 ▶ if ($ldap) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ldap) 0 0 0
68 0 $data{ldap} = $ldap;
69 }
70 0 ▶ return (\%data, 1) if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 0 0 0
71 }
72 }
73  
74 # Not verified
75 6 return {realm => $realm};
76 }
77  
78 25 ▶sub _expected_auth {
Subroutine _expected_auth: called CC=2 SCAR=7.0
79 25 my $self = shift;
80 25 my $realm = shift;
81  
82 25 ▶ return @$realm{qw/ realm password username /} if ref $realm eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
if ref $realm eq "HASH" 0 25 25
83  
84 # realm, pass, user || realm, pass, undef || realm, callback
85 25 return $realm, reverse @_;
86 }
87  
88 7 ▶sub _password_prompt {
Subroutine _password_prompt: called CC=1 SCAR=0.0
89 7 my ($self, $c, $realm) = @_;
90  
91 7 $c->res->headers->www_authenticate("Basic realm=\"$realm\"");
92 7 $c->res->code(401);
93 7 $c->rendered;
94  
95 7 return;
96 }
97  
98 39 ▶sub _split_auth {
Subroutine _split_auth: called CC=3 SCAR=11.0
99 # Split $auth into username and password (which may contain ":")
100 39 my ($username, $password) = split ':', $_[0], 2;
101  
102 39 ▶ $username = '' unless defined $username;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $username 3 36 39
103 39 ▶ $password = '' unless defined $password;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $password 3 36 39
104  
105 39 return ($username, $password);
106 }
107  
108 5 ▶sub _check_simple {
Subroutine _check_simple: called CC=3 SCAR=12.5
109 5 my ($self, $c, $auth, $params) = @_;
110 5 my ($username, $password) = _split_auth($auth);
111  
112 return 1
113 if $username eq $params->{'username'}
114 5 ▶ and Authen::Simple::Password->check($password, $params->{'password'});
Branch: 1 branch
BranchTrueFalseTotal
if $username eq $params->{"username"} and "Authen::Simple::Password"->check($password, $params->{"password"}) 5 0 5
Condition33%1/3
$username eq $params->{"username"} and "Authen::Simple::Password"->check($password, $params->{"password"})
!ll&&!rl&&r
005
MC/DC0%0/2
$username eq $params->{"username"} and "Authen::Simple::Password"->check($password, $params->{"password"})
$username eq $params->{"username"} "Authen::Simple::Password"->check($password, $params->{"password"})
Truth table33%1/3
A and B
A= $username eq $params->{"username"}B= "Authen::Simple::Password"->check($password, $params->{"password"})
ABresult
0X0
100
111
115 }
116  
117 0 ▶sub _check_ldap {
Subroutine _check_ldap: not called CC=35 SCAR=71.4
118 0 my ($self, $c, $auth, $params) = @_;
119 0 my ($username, $password) = _split_auth($auth);
120 0 ▶ my $logging = $params->{logging} // 0;
Condition0%0/2
$params->{"logging"} // 0
l!l
00
MC/DC0%0/1
$params->{"logging"} // 0
$params->{"logging"}
Truth table0%0/2
A
A= $params->{"logging"}
Aresult
11
00
121  
122 0 ▶ return 0 unless defined $password;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $password 0 0 0
123  
124 my $ldap = Net::LDAP->new(
125 $params->{host},
126 port => $params->{port} // 389,
127 scheme => $params->{scheme} // 'ldap',
128 debug => $params->{debug} // 0,
129 timeout => $params->{timeout} // 120,
130 0 ▶ version => $params->{version} // 3,
Condition0%0/10
$params->{"port"} // 389
l!l
00
$params->{"scheme"} // "ldap"
l!l
00
$params->{"debug"} // 0
l!l
00
$params->{"timeout"} // 120
l!l
00
$params->{"version"} // 3
l!l
00
MC/DC0%0/1
$params->{"port"} // 389
$params->{"port"}
MC/DC0%0/1
$params->{"scheme"} // "ldap"
$params->{"scheme"}
MC/DC0%0/1
$params->{"debug"} // 0
$params->{"debug"}
MC/DC0%0/1
$params->{"timeout"} // 120
$params->{"timeout"}
MC/DC0%0/1
$params->{"version"} // 3
$params->{"version"}
Truth table0%0/2
A
A= $params->{"port"}
Aresult
11
00
Truth table0%0/2
A
A= $params->{"scheme"}
Aresult
11
00
Truth table0%0/2
A
A= $params->{"debug"}
Aresult
11
00
Truth table0%0/2
A
A= $params->{"timeout"}
Aresult
11
00
Truth table0%0/2
A
A= $params->{"version"}
Aresult
11
00
131 );
132 0 ▶ unless ($ldap) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ldap) 0 0 0
133 0 ▶ $c->app->log->warn("Connection to $params->{host} failed: $@")
Branch: 1 branch
BranchTrueFalseTotal
if $logging 0 0 0
134 if $logging;
135 0 return 0;
136 }
137  
138 0 my $socket_type = ref $ldap->{net_ldap_socket};
139 0 ▶ $c->app->log->warn("LDAP socket type: $socket_type") if $logging;
Branch: 1 branch
BranchTrueFalseTotal
if $logging 0 0 0
140  
141 0 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless ($socket_type eq "IO::Socket::SSL" or defined $params->{"start_tls"} and $params->{"start_tls"} == 0) 0 0 0
Condition0%0/6
$socket_type eq "IO::Socket::SSL" or defined $params->{"start_tls"} and $params->{"start_tls"} == 0
l!l&&r!l&&!r
000
defined $params->{"start_tls"} and $params->{"start_tls"} == 0
!ll&&!rl&&r
000
MC/DC0%0/3
$socket_type eq "IO::Socket::SSL" or defined $params->{"start_tls"} and $params->{"start_tls"} == 0
$socket_type eq "IO::Socket::SSL" defined $params->{"start_tls"} $params->{"start_tls"} == 0
Truth table0%0/4
A or B and C
A= $socket_type eq "IO::Socket::SSL"B= defined $params->{"start_tls"}C= $params->{"start_tls"} == 0
ABCresult
1XX1
0111
00X0
0100
142 # SSL connection already established
143 ($socket_type eq 'IO::Socket::SSL')
144  
145 # Or user doesn't want TLS
146 || (defined($params->{start_tls}) && $params->{start_tls} == 0)
147 )
148 {
149 0 my $dse = $ldap->root_dse();
150 0 my $has_tls = $dse->supported_extension('1.3.6.1.4.1.1466.20037');
151  
152 0 ▶ if ($has_tls) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_tls) 0 0 0
153 my $mesg = $ldap->start_tls(
154 verify => $params->{tls_verify} // 'optional',
155 0 ▶ cafile => $params->{cafile} // '',
Condition0%0/4
$params->{"tls_verify"} // "optional"
l!l
00
$params->{"cafile"} // ""
l!l
00
MC/DC0%0/1
$params->{"tls_verify"} // "optional"
$params->{"tls_verify"}
MC/DC0%0/1
$params->{"cafile"} // ""
$params->{"cafile"}
Truth table0%0/2
A
A= $params->{"tls_verify"}
Aresult
11
00
Truth table0%0/2
A
A= $params->{"cafile"}
Aresult
11
00
156 );
157 0 ▶ if ($mesg->is_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mesg->is_error) 0 0 0
158 0 ▶ if ($logging) {
Branch: 1 branch
BranchTrueFalseTotal
if ($logging) 0 0 0
159 my $text = sprintf 'start_tls() failed for %s [%s] %s: %s',
160 $params->{host},
161 0 $mesg->code,
162 $mesg->error_name,
163 $mesg->error_text;
164  
165 0 $c->app->log->warn($text);
166 }
167 0 $ldap->unbind;
168 0 return 0;
169 }
170 }
171  
172 0 $socket_type = ref $ldap->{net_ldap_socket};
173 0 ▶ $c->app->log->warn("LDAP socket type after start_tls(): $socket_type")
Branch: 1 branch
BranchTrueFalseTotal
if $logging 0 0 0
174 if $logging;
175 }
176  
177 my @credentials
178 = $params->{binddn}
179 ? ($params->{binddn}, password => $params->{bindpw})
180 0 ▶ : ();
Branch: 1 branch
BranchTrueFalseTotal
$params->{'binddn'} ? : 0 0 0
181  
182 0 my $mesg = $ldap->bind(@credentials);
183 0 ▶ if ($mesg->is_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mesg->is_error) 0 0 0
184 $c->app->log->warn("LDAP bind failed"
185 0 ▶ . ($params->{binddn} ? " for $params->{binddn}: " : ": ")
Branch: 2 branches
BranchTrueFalseTotal
if $logging 0 0 0
$params->{'binddn'} ? : 0 0 0
186 . $mesg->error)
187 if $logging;
188 0 $ldap->unbind;
189 0 return 0;
190 }
191  
192 0 my $count = () = $params->{filter} =~ /%s/g;
193 0 my $filter = sprintf($params->{filter}, ($username) x $count);
194 0 ▶ my $scope = $params->{scope} // 'sub';
Condition0%0/2
$params->{"scope"} // "sub"
l!l
00
MC/DC0%0/1
$params->{"scope"} // "sub"
$params->{"scope"}
Truth table0%0/2
A
A= $params->{"scope"}
Aresult
11
00
195 my $search = $ldap->search(
196 base => $params->{basedn},
197 0 scope => $scope,
198 filter => $filter,
199 attrs => ['1.1']
200 );
201  
202 0 ▶ if ($search->is_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($search->is_error) 0 0 0
203 0 ▶ $c->app->log->warn("LDAP search failed: " . $search->error)
Branch: 1 branch
BranchTrueFalseTotal
if $logging 0 0 0
204 if $logging;
205 0 $ldap->unbind;
206 0 return 0;
207 }
208  
209 0 ▶ if ($search->count == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($search->count == 0) 0 0 0
210 0 ▶ $c->app->log->warn(
Branch: 1 branch
BranchTrueFalseTotal
if $logging 0 0 0
211 qq{User '$username' not found with filter '$filter' and scope '$scope'}
212 ) if $logging;
213 0 $ldap->unbind;
214 0 return 0;
215 }
216  
217 0 ▶ if ($search->count > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($search->count > 1) 0 0 0
218 0 ▶ $c->app->log->warn("Found "
Branch: 1 branch
BranchTrueFalseTotal
if $logging 0 0 0
219 . $search->count
220 . qq{ matching entries for $username with filter '$filter'})
221 if $logging;
222 }
223  
224 0 my $entry = $search->entry(0);
225 0 my $dn = $entry->dn;
226 0 $mesg = $ldap->bind($dn, password => $password);
227  
228 0 ▶ if ($mesg->is_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mesg->is_error) { } 0 0 0
229 0 ▶ $c->app->log->warn(
Branch: 1 branch
BranchTrueFalseTotal
if $logging 0 0 0
230 qq{LDAP failed to authenticate user '$username' with dn '$dn': }
231 . $mesg->error)
232 if $logging;
233 0 $ldap->unbind;
234 0 return 0;
235 }
236 else {
237 0 ▶ $c->app->log->info(
Branch: 1 branch
BranchTrueFalseTotal
if $logging 0 0 0
238 qq{LDAP successfully authenticated user '$username' with dn '$dn'}
239 ) if $logging;
240 0 ▶ if ($params->{return_ldap_handle}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{'return_ldap_handle'}) { } 0 0 0
241 0 return (1, $ldap);
242 }
243 else {
244 0 $ldap->unbind;
245 0 return 1;
246 }
247 }
248  
249 0 return 0;
250 }
251  
252 2 ▶sub _check_passwd {
Subroutine _check_passwd: called CC=2 SCAR=7.0
253 2 my ($self, $c, $auth, $params) = @_;
254 2 my ($username, $password) = _split_auth($auth);
255  
256 2 my $passwd = Authen::Simple::Passwd->new(%$params);
257  
258 2 ▶ return 1 if $passwd->authenticate($username, $password);
Branch: 1 branch
BranchTrueFalseTotal
if $passwd->authenticate($username, $password) 2 0 2
259 }
260  
261 3 1;
262  
263 __END__
↑ Summary script/sample.pl »