blib/lib/CGI/Application/Plugin/Authentication.pm

filter: Statementstmt 86.1% 385 / 447 Branchbran 79.0% 207 / 262 Conditioncond 68.5% 85 / 124 MC/DCmcdc 54.9% 39 / 71 Subroutinesub 96.3% 52 / 54 Podpod 93.1% 27 / 29 totaltotal 80.5% 795 / 987 CC 186 SCAR 42.8
CC
186
Cov
81%
login_styles
552.0
config
55.0
_devpopup_report
42.0
CRAP 72.2
« blib/lib/CGI/Application/Plugin/Authentication/Driver/DBI.pm ↑ Summary blib/lib/CGI/Application/Plugin/Authentication/Display/Classic.pm »
1 package CGI::Application::Plugin::Authentication;
2  
3 29 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 29 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 29 our $VERSION = '0.18_2';
6  
7 29 our %__CONFIG;
8  
9 29 ▶use Class::ISA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 29 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 29 ▶use UNIVERSAL::require;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 29 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 29 ▶use CGI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 sub import {
16 30 ▶ my $pkg = shift;
Subroutine import: called CC=2 SCAR=6.9
17 30 my $callpkg = caller;
18 {
19 29 ▶ no strict qw(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 30 *{$callpkg.'::authen'} = \&CGI::Application::Plugin::_::Authentication::authen;
21 }
22 30 ▶ if ( ! UNIVERSAL::isa($callpkg, 'CGI::Application') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not UNIVERSAL::isa($callpkg, 'CGI::Application')) { } 2 28 30
23 2 warn "Calling package is not a CGI::Application module so not setting up the prerun hook. If you are using \@ISA instead of 'use base', make sure it is in a BEGIN { } block, and make sure these statements appear before the plugin is loaded";
24 } else {
25 28 $callpkg->add_callback( prerun => \&prerun_callback );
26 }
27 }
28  
29 29 ▶use Attribute::Handlers;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 29 my %RUNMODES;
31  
32 sub CGI::Application::RequireAuthentication : ATTR(CODE) {
33 1 ▶ my ( $package, $symbol, $referent, $attr, $data, $phase ) = @_;
Subroutine RequireAuthentication: called CC=2 SCAR=7.2
Pod undocumented
34 1 ▶ $RUNMODES{$referent} = $data || 1;
Condition50%1/2
$data || 1
l!l
01
MC/DC0%0/1
$data || 1
$data
Truth table50%1/2
A
A= $data
Aresult
11
00
35 29 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
36 sub CGI::Application::Authen : ATTR(CODE) {
37 1 ▶ my ( $package, $symbol, $referent, $attr, $data, $phase ) = @_;
Subroutine Authen: called CC=2 SCAR=7.2
Pod undocumented
38 1 ▶ $RUNMODES{$referent} = $data || 1;
Condition50%1/2
$data || 1
l!l
10
MC/DC0%0/1
$data || 1
$data
Truth table50%1/2
A
A= $data
Aresult
11
00
39 29 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41  
42 =head1 NAME
43  
44 CGI::Application::Plugin::Authentication - Authentication framework for CGI::Application
45  
46  
47 =head1 SYNOPSIS
48  
49 package MyCGIApp;
50  
51 use base qw(CGI::Application); # make sure this occurs before you load the plugin
52  
53 use CGI::Application::Plugin::Authentication;
54  
55 MyCGIApp->authen->config(
56 DRIVER => [ 'Generic', { user1 => '123' } ],
57 );
58 MyCGIApp->authen->protected_runmodes('myrunmode');
59  
60 sub myrunmode {
61 my $self = shift;
62  
63 # The user should be logged in if we got here
64 my $username = $self->authen->username;
65  
66 }
67  
68 =head1 DESCRIPTION
69  
70 CGI::Application::Plugin::Authentication adds the ability to authenticate users
71 in your L<CGI::Application> modules. It imports one method called 'authen' into your
72 CGI::Application module. Through the authen method you can call all the methods of
73 the CGI::Application::Plugin::Authentication plugin.
74  
75 There are two main decisions that you need to make when using this module. How will
76 the usernames and password be verified (i.e. from a database, LDAP, etc...), and how
77 can we keep the knowledge that a user has already logged in persistent, so that they
78 will not have to enter their credentials again on the next request (i.e. how do we 'Store'
79 the authentication information across requests).
80  
81 =head2 Choosing a Driver
82  
83 There are three drivers that are included with the distribution. Also, there
84 is built in support for all of the Authen::Simple modules (search CPAN for
85 Authen::Simple for more information). This should be enough to cover
86 everyone's needs.
87  
88 If you need to authenticate against a source that is not provided, you can use
89 the Generic driver which will accept either a hash of username/password pairs,
90 or an array of arrays of credentials, or a subroutine reference that can verify
91 the credentials. So through the Generic driver you should be able to write
92 your own verification system. There is also a Dummy driver, which blindly
93 accepts any credentials (useful for testing). See the
94 L<CGI::Application::Plugin::Authentication::Driver::Generic>,
95 L<CGI::Application::Plugin::Authentication::Driver::DBI> and,
96 L<CGI::Application::Plugin::Authentication::Driver::Dummy> docs for more
97 information on how to use these drivers. And see the L<Authen::Simple> suite
98 of modules for information on those drivers.
99  
100 =head2 Choosing a Store
101  
102 The Store modules keep information about the authentication status of the user persistent
103 across multiple requests. The information that is stored in the store include the username,
104 and the expiry time of the login. There are two Store modules included with this distribution.
105 A Session based store, and a Cookie based store. If your application is already using
106 Sessions (through the L<CGI::Application::Plugin::Session> module), then I would recommend
107 that you use the Session store for authentication. If you are not using the Session
108 plugin, then you can use the Cookie store. The Cookie store keeps all the authentication
109 in a cookie, which contains a checksum to ensure that users can not change the information.
110  
111 If you do not specify which Store module you wish to use, the plugin will try to determine
112 the best one for you.
113  
114 =head2 Login page
115  
116 The Authentication plugin comes with a default login page that can be used if you do not
117 want to create a custom login page. This login form will automatically be used if you
118 do not provide either a LOGIN_URL or LOGIN_RUNMODE parameter in the configuration.
119 If you plan to create your own login page, I would recommend that you start with the HTML
120 code for the default login page, so that your login page will contain the correct form
121 fields and hidden fields.
122  
123 =head2 Ticket based authentication
124  
125 This Authentication plugin can handle ticket based authentication systems as well. All that
126 is required of you is to write a Store module that can understand the contents of the ticket.
127 The Authentication plugin will require at least the 'username' to be retrieved from the
128 ticket. A Ticket based authentication scheme will not need a Driver module at all, since the
129 actual verification of credentials is done by an external authentication system, possibly
130 even on a different host. You will need to specify the location of the login page using
131 the LOGIN_URL configuration variable, and unauthenticated users will automatically
132 be redirected to your ticket authentication login page.
133  
134  
135 =head1 EXPORTED METHODS
136  
137 =head2 authen
138  
139 This is the only method exported from this module. Everything is controlled
140 through this method call, which will return a CGI::Application::Plugin::Authentication
141 object, or just the class name if called as a class method. When using
142 the plugin, you will always first call $self->authen or __PACKAGE__->authen and
143 then the method you wish to invoke. For example:
144  
145 __PACKAGE__->authen->config(
146 LOGIN_RUNMODE => 'login',
147 );
148  
149 - or -
150  
151 $self->authen->protected_runmodes(qw(one two));
152  
153 =cut
154  
155  
156 { package # Hide from PAUSE
157 CGI::Application::Plugin::_::Authentication;
158  
159 ##############################################
160 ###
161 ### authen
162 ###
163 ##############################################
164 #
165 # Return an authen object that can be used
166 # for managing authentication.
167 #
168 # This will return a class name if called
169 # as a class name, and a singleton object
170 # if called as an object method
171 #
172 sub authen {
173 1384 ▶ my $cgiapp = shift;
Subroutine authen: called CC=2 SCAR=6.9
174  
175 1384 ▶ if (ref($cgiapp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cgiapp) { } 1364 20 1384
176 1364 return CGI::Application::Plugin::Authentication->instance($cgiapp);
177 } else {
178 20 return 'CGI::Application::Plugin::Authentication';
179 }
180 }
181  
182 }
183  
184 29 package CGI::Application::Plugin::Authentication;
185  
186  
187  
188 =head1 METHODS
189  
190 =head2 config
191  
192 This method is used to configure the CGI::Application::Plugin::Authentication
193 module. It can be called as an object method, or as a class method. Calling this function,
194 will not itself generate cookies or session ids.
195  
196 The following parameters are accepted:
197  
198 =over 4
199  
200 =item DRIVER
201  
202 Here you can choose which authentication module(s) you want to use to perform the authentication.
203 For simplicity, you can leave off the CGI::Application::Plugin::Authentication::Driver:: part
204 when specifying the DRIVER name If this module requires extra parameters, you
205 can pass an array reference that contains as the first parameter the name of the module,
206 and the rest of the values in the array will be considered options for the driver. You can provide
207 multiple drivers which will be used, in order, to check the credentials until
208 a valid response is received.
209  
210 DRIVER => 'Dummy' # let anyone in regardless of the password
211  
212 - or -
213  
214 DRIVER => [ 'DBI',
215 DBH => $self->dbh,
216 TABLE => 'user',
217 CONSTRAINTS => {
218 'user.name' => '__CREDENTIAL_1__',
219 'MD5:user.password' => '__CREDENTIAL_2__'
220 },
221 ],
222  
223 - or -
224  
225 DRIVER => [
226 [ 'Generic', { user1 => '123' } ],
227 [ 'Generic', sub { my ($u, $p) = @_; is_prime($p) ? 1 : 0 } ]
228 ],
229  
230 - or -
231  
232 DRIVER => [ 'Authen::Simple::LDAP',
233 host => 'ldap.company.com',
234 basedn => 'ou=People,dc=company,dc=net'
235 ],
236  
237  
238 =item STORE
239  
240 Here you can choose how we store the authenticated information after a user has successfully
241 logged in. We need to store the username so that on the next request we can tell the user
242 has already logged in, and we do not have to present them with another login form. If you
243 do not provide the STORE option, then the plugin will look to see if you are using the
244 L<CGI::Application::Plugin::Session> module and based on that info use either the Session
245 module, or fall back on the Cookie module. If the module requires extra parameters, you
246 can pass an array reference that contains as the first parameter the name of the module,
247 and the rest of the array should contain key value pairs of options for this module.
248 These storage modules generally live under the CGI::Application::Plugin::Authentication::Store::
249 name-space, and this part of the package name can be left off when specifying the STORE
250 parameter.
251  
252 STORE => 'Session'
253  
254 - or -
255  
256 STORE => ['Cookie',
257 NAME => 'MYAuthCookie',
258 SECRET => 'FortyTwo',
259 EXPIRY => '1d',
260 ]
261  
262  
263 =item POST_LOGIN_RUNMODE
264  
265 Here you can specify a runmode that the user will be redirected to if they successfully login.
266  
267 POST_LOGIN_RUNMODE => 'welcome'
268  
269 =item POST_LOGIN_URL
270  
271 Here you can specify a URL that the user will be redirected to if they successfully login.
272 If both POST_LOGIN_URL and POST_LOGIN_RUNMODE are specified, then the latter
273 will take precedence.
274  
275 POST_LOGIN_URL => 'http://example.com/start.cgi'
276  
277 =item POST_LOGIN_CALLBACK
278  
279 A code reference that is executed after login processing but before POST_LOGIN_RUNMODE or
280 redirecting to POST_LOGIN_URL. This is normally a method in your CGI::Application application
281 and as such the CGI::Application object is passed as a parameter.
282  
283 POST_LOGIN_CALLBACK => \&update_login_date
284  
285 and later in your code:
286  
287 sub update_login_date {
288 my $self = shift;
289  
290 return unless($self->authen->is_authenticated);
291  
292 ...
293 }
294  
295  
296 =item LOGIN_RUNMODE
297  
298 Here you can specify a runmode that the user will be redirected to if they need to login.
299  
300 LOGIN_RUNMODE => 'login'
301  
302 =item LOGIN_URL
303  
304 If your login page is external to this module, then you can use this option to specify a
305 URL that the user will be redirected to when they need to login. If both
306 LOGIN_URL and LOGIN_RUNMODE are specified, then the latter will take precedence.
307  
308 LOGIN_URL => 'http://example.com/login.cgi'
309  
310 =item LOGOUT_RUNMODE
311  
312 Here you can specify a runmode that the user will be redirected to if they ask to logout.
313  
314 LOGOUT_RUNMODE => 'logout'
315  
316 =item LOGOUT_URL
317  
318 If your logout page is external to this module, then you can use this option to specify a
319 URL that the user will be redirected to when they ask to logout. If both
320 LOGOUT_URL and LOGOUT_RUNMODE are specified, then the latter will take precedence.
321  
322 LOGIN_URL => 'http://example.com/logout.html'
323  
324 =item DETAINT_URL_REGEXP
325  
326 This is a regular expression used to detaint URLs used in the login form. By default it will be set to
327  
328 ^([\w\_\%\?\&\;\-\/\@\.\+\$\=\#\:\!\*\"\'\(\)\,]+)$
329  
330 This regular expression is based upon the document http://www.w3.org/Addressing/URL/url-spec.txt. You could
331 set it to a more specific regular expression to limit the domains to which users could be directed.
332  
333 =item DETAINT_USERNAME_REGEXP
334  
335 This is a regular expression used to detaint the username parameter used in the login form. By default it will be set to
336  
337 ^([\w\_]+)$
338  
339 =item CREDENTIALS
340  
341 Set this to the list of form fields where the user will type in their username and password.
342 By default this is set to ['authen_username', 'authen_password']. The form field names should
343 be set to a value that you are not likely to use in any other forms. This is important
344 because this plugin will automatically look for query parameters that match these values on
345 every request to see if a user is trying to log in. So if you use the same parameter names
346 on a user management page, you may inadvertently perform a login when that was not intended.
347 Most of the Driver modules will return the first CREDENTIAL as the username, so make sure
348 that you list the username field first. This option can be ignored if you use the built in
349 login box
350  
351 CREDENTIALS => 'authen_password'
352  
353 - or -
354  
355 CREDENTIALS => [ 'authen_username', 'authen_domain', 'authen_password' ]
356  
357  
358 =item LOGIN_SESSION_TIMEOUT
359  
360  
361 This option can be used to tell the system when to force the user to re-authenticate. There are
362 a few different possibilities that can all be used concurrently:
363  
364 =over 4
365  
366 =item IDLE_FOR
367  
368 If this value is set, a re-authentication will be forced if the user was idle for more then x amount of time.
369  
370 =item EVERY
371  
372 If this value is set, a re-authentication will be forced every x amount of time.
373  
374 =item CUSTOM
375  
376 This value can be set to a subroutine reference that returns true if the session should be timed out,
377 and false if it is still active. This can allow you to be very selective about how the timeout system
378 works. The authen object will be passed in as the only parameter.
379  
380 =back
381  
382 Time values are specified in seconds. You can also specify the time by using a number with the
383 following suffixes (m h d w), which represent minutes, hours, days and weeks. The default
384 is 0 which means the login will never timeout.
385  
386 Note that the login is also dependant on the type of STORE that is used. If the Session store is used,
387 and the session expires, then the login will also automatically expire. The same goes for the Cookie
388 store.
389  
390 For backwards compatibility, if you set LOGIN_SESSION_TIMEOUT to a time value instead of a hashref,
391 it will be treated as an IDLE_FOR time out.
392  
393 # force re-authentication if idle for more than 15 minutes
394 LOGIN_SESSION_TIMEOUT => '15m'
395  
396 # Everyone must re-authentication if idle for more than 30 minutes
397 # also, everyone must re-authentication at least once a day
398 # and root must re-authentication if idle for more than 5 minutes
399 LOGIN_SESSION_TIMEOUT => {
400 IDLE_FOR => '30m',
401 EVERY => '1d',
402 CUSTOM => sub {
403 my $authen = shift;
404 return ($authen->username eq 'root' && (time() - $authen->last_access) > 300) ? 1 : 0;
405 }
406 }
407  
408 =item RENDER_LOGIN
409  
410 This value can be set to a subroutine reference that returns the HTML of a login
411 form. The subroutine reference overrides the default call to login_box.
412 The subroutine is normally a method in your CGI::Application application and as such the
413 CGI::Application object is passed as the first parameter.
414  
415 RENDER_LOGIN => \&login_form
416  
417 and later in your code:
418  
419 sub login_form {
420 my $self = shift;
421  
422 ...
423 return $html
424 }
425  
426 =item LOGIN_FORM
427  
428 You can set this option to customize the login form that is created when a user
429 needs to be authenticated. If you wish to replace the entire login form with a
430 completely custom version, then just set LOGIN_RUNMODE to point to your custom
431 runmode.
432  
433 All of the parameters listed below are optional, and a reasonable default will
434 be used if left blank:
435  
436 =over 4
437  
438 =item DISPLAY_CLASS (default: Classic)
439  
440 the class used to display the login form. The alternative is C<Basic>
441 which aims for XHTML compliance and leaving style to CSS. See
442 L<CGI::Application::Plugin::Authentication::Display> for more details.
443  
444 =item TITLE (default: Sign In)
445  
446 the heading at the top of the login box
447  
448 =item USERNAME_LABEL (default: User Name)
449  
450 the label for the user name input
451  
452 =item PASSWORD_LABEL (default: Password)
453  
454 the label for the password input
455  
456 =item SUBMIT_LABEL (default: Sign In)
457  
458 the label for the submit button
459  
460 =item COMMENT (default: Please enter your username and password in the fields below.)
461  
462 a message provided on the first login attempt
463  
464 =item REMEMBERUSER_OPTION (default: 1)
465  
466 provide a checkbox to offer to remember the users name in a cookie so that
467 their user name will be pre-filled the next time they log in
468  
469 =item REMEMBERUSER_LABEL (default: Remember User Name)
470  
471 the label for the remember user name checkbox
472  
473 =item REMEMBERUSER_COOKIENAME (default: CAPAUTHTOKEN)
474  
475 the name of the cookie where the user name will be saved
476  
477 =item REGISTER_URL (default: <none>)
478  
479 the URL for the register new account link
480  
481 =item REGISTER_LABEL (default: Register Now!)
482  
483 the label for the register new account link
484  
485 =item FORGOTPASSWORD_URL (default: <none>)
486  
487 the URL for the forgot password link
488  
489 =item FORGOTPASSWORD_LABEL (default: Forgot Password?)
490  
491 the label for the forgot password link
492  
493 =item INVALIDPASSWORD_MESSAGE (default: Invalid username or password<br />(login attempt %d)
494  
495 a message given when a login failed
496  
497 =item INCLUDE_STYLESHEET (default: 1)
498  
499 use this to disable the built in style-sheet for the login box so you can provide your own custom styles
500  
501 =item FORM_SUBMIT_METHOD (default: post)
502  
503 use this to get the form to submit using 'get' instead of 'post'
504  
505 =item FOCUS_FORM_ONLOAD (default: 1)
506  
507 use this to automatically focus the login form when the page loads so a user can start typing right away.
508  
509 =item BASE_COLOUR (default: #445588)
510  
511 This is the base colour that will be used in the included login box. All other
512 colours are automatically calculated based on this colour (unless you hardcode
513 the colour values). In order to calculate other colours, you will need the
514 Color::Calc module. If you do not have the Color::Calc module, then you will
515 need to use fixed values for all of the colour options. All colour values
516 besides the BASE_COLOUR can be simple percentage values (including the % sign).
517 For example if you set the LIGHTER_COLOUR option to 80%, then the calculated
518 colour will be 80% lighter than the BASE_COLOUR.
519  
520 =item LIGHT_COLOUR (default: 50% or #a2aac4)
521  
522 A colour that is lighter than the base colour.
523  
524 =item LIGHTER_COLOUR (default: 75% or #d0d5e1)
525  
526 A colour that is another step lighter than the light colour.
527  
528 =item DARK_COLOUR (default: 30% or #303c5f)
529  
530 A colour that is darker than the base colour.
531  
532 =item DARKER_COLOUR (default: 60% or #1b2236)
533  
534 A colour that is another step darker than the dark colour.
535  
536 =item GREY_COLOUR (default: #565656)
537  
538 A grey colour that is calculated by desaturating the base colour.
539  
540 =back
541  
542 =back
543  
544 =cut
545  
546 sub config {
547 113 ▶ my $self = shift;
Subroutine config: called CC=55 SCAR=40.1
Pod documented
548 113 ▶ my $class = ref $self ? ref $self : $self;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 94 19 113
549  
550 die "Calling config after the Authentication object has already been initialized"
551 113 ▶ if ref $self && defined $self->{initialized};
Branch: 1 branch
BranchTrueFalseTotal
if ref $self and defined $self->{"initialized"} 1 112 113
Condition100%3/3
ref $self and defined $self->{"initialized"}
!ll&&!rl&&r
19931
MC/DC100%2/2
ref $self and defined $self->{"initialized"}
ref $self defined $self->{"initialized"}
Truth table100%3/3
A and B
A= ref $selfB= defined $self->{"initialized"}
ABresult
0X0
100
111
552 112 my $config = $self->_config;
553  
554 112 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 111 1 112
555 111 my $props;
556 111 ▶ if ( ref( $_[0] ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 44 67 111
557 44 my $rthash = %{ $_[0] };
558 44 $props = CGI::Application->_cap_hash( $_[0] );
559 } else {
560 67 $props = CGI::Application->_cap_hash( {@_} );
561 }
562  
563 # Check for DRIVER
564 111 ▶ if ( defined $props->{DRIVER} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $props->{"DRIVER"}) 67 44 111
565 croak "authen config error: parameter DRIVER is not a string or arrayref"
566 67 ▶ if ref $props->{DRIVER} && Scalar::Util::reftype( $props->{DRIVER} ) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $props->{"DRIVER"} and builtin::reftype($props->{"DRIVER"}) ne "ARRAY" 1 66 67
Condition100%3/3
ref $props->{"DRIVER"} and builtin::reftype($props->{"DRIVER"}) ne "ARRAY"
!ll&&!rl&&r
2641
MC/DC100%2/2
ref $props->{"DRIVER"} and builtin::reftype($props->{"DRIVER"}) ne "ARRAY"
ref $props->{"DRIVER"} builtin::reftype($props->{"DRIVER"}) ne "ARRAY"
Truth table100%3/3
A and B
A= ref $props->{"DRIVER"}B= builtin::reftype($props->{"DRIVER"}) ne "ARRAY"
ABresult
0X0
100
111
567 66 $config->{DRIVER} = delete $props->{DRIVER};
568 # We will accept a string, or an arrayref of options, but what we really want
569 # is an array of arrayrefs of options, so that we can support multiple drivers
570 # each with their own custom options
571 29 ▶ no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
572 66 ▶ $config->{DRIVER} = [ $config->{DRIVER} ] if Scalar::Util::reftype( $config->{DRIVER} ) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($config->{"DRIVER"}) ne "ARRAY" 2 64 66
573 66 ▶ $config->{DRIVER} = [ $config->{DRIVER} ] if Scalar::Util::reftype( $config->{DRIVER}->[0] ) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($config->{"DRIVER"}[0]) ne "ARRAY" 63 3 66
574 }
575  
576 # Check for STORE
577 110 ▶ if ( defined $props->{STORE} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $props->{"STORE"}) 69 41 110
578 croak "authen config error: parameter STORE is not a string or arrayref"
579 69 ▶ if ref $props->{STORE} && Scalar::Util::reftype( $props->{STORE} ) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $props->{"STORE"} and builtin::reftype($props->{"STORE"}) ne "ARRAY" 1 68 69
Condition100%3/3
ref $props->{"STORE"} and builtin::reftype($props->{"STORE"}) ne "ARRAY"
!ll&&!rl&&r
15531
MC/DC100%2/2
ref $props->{"STORE"} and builtin::reftype($props->{"STORE"}) ne "ARRAY"
ref $props->{"STORE"} builtin::reftype($props->{"STORE"}) ne "ARRAY"
Truth table100%3/3
A and B
A= ref $props->{"STORE"}B= builtin::reftype($props->{"STORE"}) ne "ARRAY"
ABresult
0X0
100
111
580 68 $config->{STORE} = delete $props->{STORE};
581 # We will accept a string, but what we really want is an arrayref of the store driver,
582 # and any custom options
583 29 ▶ no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
584 68 ▶ $config->{STORE} = [ $config->{STORE} ] if Scalar::Util::reftype( $config->{STORE} ) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($config->{"STORE"}) ne "ARRAY" 15 53 68
585 }
586  
587 # Check for POST_LOGIN_RUNMODE
588 109 ▶ if ( defined $props->{POST_LOGIN_RUNMODE} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $props->{"POST_LOGIN_RUNMODE"}) 6 103 109
589 croak "authen config error: parameter POST_LOGIN_RUNMODE is not a string"
590 6 ▶ if ref $props->{POST_LOGIN_RUNMODE};
Branch: 1 branch
BranchTrueFalseTotal
if ref $props->{"POST_LOGIN_RUNMODE"} 1 5 6
591 5 $config->{POST_LOGIN_RUNMODE} = delete $props->{POST_LOGIN_RUNMODE};
592 }
593  
594 # Check for POST_LOGIN_URL
595 108 ▶ if ( defined $props->{POST_LOGIN_URL} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $props->{"POST_LOGIN_URL"}) 10 98 108
596 carp "authen config warning: parameter POST_LOGIN_URL ignored since we already have POST_LOGIN_RUNMODE"
597 10 ▶ if $config->{POST_LOGIN_RUNMODE};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"POST_LOGIN_RUNMODE"} 3 7 10
598 croak "authen config error: parameter POST_LOGIN_URL is not a string"
599 10 ▶ if ref $props->{POST_LOGIN_URL};
Branch: 1 branch
BranchTrueFalseTotal
if ref $props->{"POST_LOGIN_URL"} 1 9 10
600 9 $config->{POST_LOGIN_URL} = delete $props->{POST_LOGIN_URL};
601 }
602  
603 # Check for LOGIN_RUNMODE
604 107 ▶ if ( defined $props->{LOGIN_RUNMODE} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $props->{"LOGIN_RUNMODE"}) 4 103 107
605 croak "authen config error: parameter LOGIN_RUNMODE is not a string"
606 4 ▶ if ref $props->{LOGIN_RUNMODE};
Branch: 1 branch
BranchTrueFalseTotal
if ref $props->{"LOGIN_RUNMODE"} 1 3 4
607 3 $config->{LOGIN_RUNMODE} = delete $props->{LOGIN_RUNMODE};
608 }
609  
610 # Check for LOGIN_URL
611 106 ▶ if ( defined $props->{LOGIN_URL} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $props->{"LOGIN_URL"}) 7 99 106
612 carp "authen config warning: parameter LOGIN_URL ignored since we already have LOGIN_RUNMODE"
613 7 ▶ if $config->{LOGIN_RUNMODE};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"LOGIN_RUNMODE"} 1 6 7
614 croak "authen config error: parameter LOGIN_URL is not a string"
615 7 ▶ if ref $props->{LOGIN_URL};
Branch: 1 branch
BranchTrueFalseTotal
if ref $props->{"LOGIN_URL"} 1 6 7
616 6 $config->{LOGIN_URL} = delete $props->{LOGIN_URL};
617 }
618  
619 # Check for LOGOUT_RUNMODE
620 105 ▶ if ( defined $props->{LOGOUT_RUNMODE} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $props->{"LOGOUT_RUNMODE"}) 4 101 105
621 croak "authen config error: parameter LOGOUT_RUNMODE is not a string"
622 4 ▶ if ref $props->{LOGOUT_RUNMODE};
Branch: 1 branch
BranchTrueFalseTotal
if ref $props->{"LOGOUT_RUNMODE"} 1 3 4
623 3 $config->{LOGOUT_RUNMODE} = delete $props->{LOGOUT_RUNMODE};
624 }
625  
626 # Check for LOGOUT_URL
627 104 ▶ if ( defined $props->{LOGOUT_URL} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $props->{"LOGOUT_URL"}) 7 97 104
628 carp "authen config warning: parameter LOGOUT_URL ignored since we already have LOGOUT_RUNMODE"
629 7 ▶ if $config->{LOGOUT_RUNMODE};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"LOGOUT_RUNMODE"} 1 6 7
630 croak "authen config error: parameter LOGOUT_URL is not a string"
631 7 ▶ if ref $props->{LOGOUT_URL};
Branch: 1 branch
BranchTrueFalseTotal
if ref $props->{"LOGOUT_URL"} 1 6 7
632 6 $config->{LOGOUT_URL} = delete $props->{LOGOUT_URL};
633 }
634  
635 # Check for CREDENTIALS
636 103 ▶ if ( defined $props->{CREDENTIALS} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $props->{"CREDENTIALS"}) 14 89 103
637 croak "authen config error: parameter CREDENTIALS is not a string or arrayref"
638 14 ▶ if ref $props->{CREDENTIALS} && Scalar::Util::reftype( $props->{CREDENTIALS} ) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $props->{"CREDENTIALS"} and builtin::reftype($props->{"CREDENTIALS"}) ne "ARRAY" 1 13 14
Condition100%3/3
ref $props->{"CREDENTIALS"} and builtin::reftype($props->{"CREDENTIALS"}) ne "ARRAY"
!ll&&!rl&&r
1121
MC/DC100%2/2
ref $props->{"CREDENTIALS"} and builtin::reftype($props->{"CREDENTIALS"}) ne "ARRAY"
ref $props->{"CREDENTIALS"} builtin::reftype($props->{"CREDENTIALS"}) ne "ARRAY"
Truth table100%3/3
A and B
A= ref $props->{"CREDENTIALS"}B= builtin::reftype($props->{"CREDENTIALS"}) ne "ARRAY"
ABresult
0X0
100
111
639 13 $config->{CREDENTIALS} = delete $props->{CREDENTIALS};
640 # We will accept a string, but what we really want is an arrayref of the credentials
641 29 ▶ no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
642 13 ▶ $config->{CREDENTIALS} = [ $config->{CREDENTIALS} ] if Scalar::Util::reftype( $config->{CREDENTIALS} ) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($config->{"CREDENTIALS"}) ne "ARRAY" 1 12 13
643 }
644  
645 # Check for LOGIN_SESSION_TIMEOUT
646 102 ▶ if ( defined $props->{LOGIN_SESSION_TIMEOUT} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $props->{"LOGIN_SESSION_TIMEOUT"}) 20 82 102
647 croak "authen config error: parameter LOGIN_SESSION_TIMEOUT is not a string or a hashref"
648 20 ▶ if ref $props->{LOGIN_SESSION_TIMEOUT} && ref$props->{LOGIN_SESSION_TIMEOUT} ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $props->{"LOGIN_SESSION_TIMEOUT"} and ref $props->{"LOGIN_SESSION_TIMEOUT"} ne "HASH" 1 19 20
Condition100%3/3
ref $props->{"LOGIN_SESSION_TIMEOUT"} and ref $props->{"LOGIN_SESSION_TIMEOUT"} ne "HASH"
!ll&&!rl&&r
7121
MC/DC100%2/2
ref $props->{"LOGIN_SESSION_TIMEOUT"} and ref $props->{"LOGIN_SESSION_TIMEOUT"} ne "HASH"
ref $props->{"LOGIN_SESSION_TIMEOUT"} ref $props->{"LOGIN_SESSION_TIMEOUT"} ne "HASH"
Truth table100%3/3
A and B
A= ref $props->{"LOGIN_SESSION_TIMEOUT"}B= ref $props->{"LOGIN_SESSION_TIMEOUT"} ne "HASH"
ABresult
0X0
100
111
649 19 my $options = {};
650 19 ▶ if (! ref $props->{LOGIN_SESSION_TIMEOUT}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $props->{'LOGIN_SESSION_TIMEOUT'}) { } 7 12 19
651 7 $options->{IDLE_FOR} = _time_to_seconds( $props->{LOGIN_SESSION_TIMEOUT} );
652 7 ▶ croak "authen config error: parameter LOGIN_SESSION_TIMEOUT is not a valid time string" unless defined $options->{IDLE_FOR};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"IDLE_FOR"} 1 6 7
653 } else {
654 12 ▶ if ($props->{LOGIN_SESSION_TIMEOUT}->{IDLE_FOR}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($props->{"LOGIN_SESSION_TIMEOUT"}{"IDLE_FOR"}) 5 7 12
655 5 $options->{IDLE_FOR} = _time_to_seconds( delete $props->{LOGIN_SESSION_TIMEOUT}->{IDLE_FOR} );
656 5 ▶ croak "authen config error: IDLE_FOR option to LOGIN_SESSION_TIMEOUT is not a valid time string" unless defined $options->{IDLE_FOR};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"IDLE_FOR"} 1 4 5
657 }
658 11 ▶ if ($props->{LOGIN_SESSION_TIMEOUT}->{EVERY}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($props->{"LOGIN_SESSION_TIMEOUT"}{"EVERY"}) 5 6 11
659 5 $options->{EVERY} = _time_to_seconds( delete $props->{LOGIN_SESSION_TIMEOUT}->{EVERY} );
660 5 ▶ croak "authen config error: EVERY option to LOGIN_SESSION_TIMEOUT is not a valid time string" unless defined $options->{EVERY};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"EVERY"} 1 4 5
661 }
662 10 ▶ if ($props->{LOGIN_SESSION_TIMEOUT}->{CUSTOM}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($props->{"LOGIN_SESSION_TIMEOUT"}{"CUSTOM"}) 5 5 10
663 5 $options->{CUSTOM} = delete $props->{LOGIN_SESSION_TIMEOUT}->{CUSTOM};
664 5 ▶ croak "authen config error: CUSTOM option to LOGIN_SESSION_TIMEOUT must be a code reference" unless ref $options->{CUSTOM} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $options->{"CUSTOM"} eq "CODE" 1 4 5
665 }
666 9 ▶ croak "authen config error: Invalid option(s) (" . join( ', ', keys %{$props->{LOGIN_SESSION_TIMEOUT}} ) . ") passed to LOGIN_SESSION_TIMEOUT" if %{$props->{LOGIN_SESSION_TIMEOUT}};
Branch: 1 branch
BranchTrueFalseTotal
if %{$props->{"LOGIN_SESSION_TIMEOUT"};} 1 8 9
667 }
668  
669 14 $config->{LOGIN_SESSION_TIMEOUT} = $options;
670 14 delete $props->{LOGIN_SESSION_TIMEOUT};
671 }
672  
673 # Check for POST_LOGIN_CALLBACK
674 96 ▶ if ( defined $props->{POST_LOGIN_CALLBACK} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $props->{"POST_LOGIN_CALLBACK"}) 31 65 96
675 croak "authen config error: parameter POST_LOGIN_CALLBACK is not a coderef"
676 31 ▶ unless( ref $props->{POST_LOGIN_CALLBACK} eq 'CODE' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $props->{"POST_LOGIN_CALLBACK"} eq "CODE" 2 29 31
677 29 $config->{POST_LOGIN_CALLBACK} = delete $props->{POST_LOGIN_CALLBACK};
678 }
679  
680 # Check for RENDER_LOGIN
681 94 ▶ if ( defined $props->{RENDER_LOGIN} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $props->{"RENDER_LOGIN"}) 3 91 94
682 croak "authen config error: parameter RENDER_LOGIN is not a coderef"
683 3 ▶ unless( ref $props->{RENDER_LOGIN} eq 'CODE' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $props->{"RENDER_LOGIN"} eq "CODE" 2 1 3
684 1 $config->{RENDER_LOGIN} = delete $props->{RENDER_LOGIN};
685 }
686  
687 # Check for LOGIN_FORM
688 92 ▶ if ( defined $props->{LOGIN_FORM} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $props->{"LOGIN_FORM"}) 24 68 92
689 croak "authen config error: parameter LOGIN_FORM is not a hashref"
690 24 ▶ unless( ref $props->{LOGIN_FORM} eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $props->{"LOGIN_FORM"} eq "HASH" 1 23 24
691 23 $config->{LOGIN_FORM} = delete $props->{LOGIN_FORM};
692 }
693  
694 # Check for DETAINT_URL_REGEXP
695 91 ▶ if ( defined $props->{DETAINT_URL_REGEXP} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $props->{'DETAINT_URL_REGEXP'}) { } 3 88 91
696 3 $config->{DETAINT_URL_REGEXP} = delete $props->{DETAINT_URL_REGEXP};
697 }
698 else {
699 88 $config->{DETAINT_URL_REGEXP} = '^([\w\_\%\?\&\;\-\/\@\.\+\$\=\#\:\!\*\"\'\(\)\,]+)$';
700 }
701  
702 # Check for DETAINT_USERNAME_REGEXP
703 91 ▶ if ( defined $props->{DETAINT_USERNAME_REGEXP} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $props->{'DETAINT_USERNAME_REGEXP'}) { } 2 89 91
704 2 $config->{DETAINT_USERNAME_REGEXP} = delete $props->{DETAINT_USERNAME_REGEXP};
705 }
706 else {
707 89 $config->{DETAINT_USERNAME_REGEXP} = '^([\w\_]+)$';
708 }
709  
710 # If there are still entries left in $props then they are invalid
711 91 ▶ croak "Invalid option(s) (" . join( ', ', keys %$props ) . ") passed to config" if %$props;
Branch: 1 branch
BranchTrueFalseTotal
if %$props 1 90 91
712 }
713 }
714  
715 =head2 protected_runmodes
716  
717 This method takes a list of runmodes that are to be protected by authentication. If a user
718 tries to access one of these runmodes, then they will be redirected to a login page
719 unless they are properly logged in. The runmode names can be a list of simple strings, regular
720 expressions, or special directives that start with a colon. This method is cumulative, so
721 if it is called multiple times, the new values are added to existing entries. It returns
722 a list of all entries that have been saved so far. Calling this function,
723 will not itself generate cookies or session ids.
724  
725 =over 4
726  
727 =item :all - All runmodes in this module will require authentication
728  
729 =back
730  
731 # match all runmodes
732 __PACKAGE__->authen->protected_runmodes(':all');
733  
734 # only protect runmodes one two and three
735 __PACKAGE__->authen->protected_runmodes(qw(one two three));
736  
737 # protect only runmodes that start with auth_
738 __PACKAGE__->authen->protected_runmodes(qr/^auth_/);
739  
740 # protect all runmodes that *do not* start with public_
741 __PACKAGE__->authen->protected_runmodes(qr/^(?!public_)/);
742  
743 =cut
744  
745 sub protected_runmodes {
746 204 ▶ my $self = shift;
Subroutine protected_runmodes: called CC=3 SCAR=11.0
Pod documented
747 204 my $config = $self->_config;
748  
749 204 ▶ $config->{PROTECTED_RUNMODES} ||= [];
Condition100%2/2
$config->{"PROTECTED_RUNMODES"} ||= []
l!l
14757
MC/DC100%1/1
$config->{"PROTECTED_RUNMODES"} ||= []
$config->{"PROTECTED_RUNMODES"}
Truth table100%2/2
A
A= $config->{"PROTECTED_RUNMODES"}
Aresult
11
00
750 204 ▶ push @{$config->{PROTECTED_RUNMODES}}, @_ if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 120 84 204
751  
752 204 return @{$config->{PROTECTED_RUNMODES}};
753 }
754  
755 =head2 is_protected_runmode
756  
757 This method accepts the name of a runmode, and will tell you if that runmode is
758 a protected runmode (i.e. does a user need to be authenticated to access this runmode).
759 Calling this function, will not itself generate cookies or session ids.
760  
761 =cut
762  
763 sub is_protected_runmode {
764 79 ▶ my $self = shift;
Subroutine is_protected_runmode: called CC=11 SCAR=24.0
Pod documented
765 79 my $runmode = shift;
766  
767 79 foreach my $runmode_test ($self->protected_runmodes) {
768 123 ▶ if (overload::StrVal($runmode_test) =~ /^Regexp=/) {
Branch: 3 branches
BranchTrueFalseTotal
if (overload::StrVal($runmode_test) =~ /^Regexp=/) { } 9 114 123
elsif (ref $runmode_test and ref $runmode_test eq 'CODE') { } 8 106 114
elsif ($runmode_test eq ':all') { } 1 105 106
769 # We were passed a regular expression
770 9 ▶ return 1 if $runmode =~ $runmode_test;
Branch: 1 branch
BranchTrueFalseTotal
if $runmode =~ /$runmode_test/ 1 8 9
771 ▶ } elsif (ref $runmode_test && ref $runmode_test eq 'CODE') {
Condition66%2/3
ref $runmode_test and ref $runmode_test eq "CODE"
!ll&&!rl&&r
10608
MC/DC50%1/2
ref $runmode_test and ref $runmode_test eq "CODE"
ref $runmode_test ref $runmode_test eq "CODE"
Truth table66%2/3
A and B
A= ref $runmode_testB= ref $runmode_test eq "CODE"
ABresult
0X0
100
111
772 # We were passed a code reference
773 8 ▶ return 1 if $runmode_test->($runmode);
Branch: 1 branch
BranchTrueFalseTotal
if &$runmode_test($runmode) 1 7 8
774 } elsif ($runmode_test eq ':all') {
775 # all runmodes are protected
776 1 return 1;
777 } else {
778 # assume we were passed a string
779 105 ▶ return 1 if $runmode eq $runmode_test;
Branch: 1 branch
BranchTrueFalseTotal
if $runmode eq $runmode_test 60 45 105
780 }
781 }
782  
783 # See if the user is using attributes
784 16 my $sub = $self->_cgiapp->can($runmode);
785 16 ▶ return 1 if $sub && $RUNMODES{$sub};
Branch: 1 branch
BranchTrueFalseTotal
if $sub and $RUNMODES{$sub} 2 14 16
Condition100%3/3
$sub and $RUNMODES{$sub}
!ll&&!rl&&r
862
MC/DC100%2/2
$sub and $RUNMODES{$sub}
$sub $RUNMODES{$sub}
Truth table100%3/3
A and B
A= $subB= $RUNMODES{$sub}
ABresult
0X0
100
111
786  
787 14 return;
788 }
789  
790 =head2 redirect_after_login
791  
792 This method is be called during the prerun stage to
793 redirect the user to the page that has been configured
794 as the destination after a successful login. The location is determined as follows:
795  
796 =over
797  
798 =item POST_LOGIN_RUNMODE
799  
800 If the POST_LOGIN_RUNMODE config parameter is set, that run mode will be the chosen location.
801  
802 =item POST_LOGIN_URL
803  
804 If the above fails and the POST_LOGIN_URL config parameter is set, then there will be a 302 redirection to that location.
805  
806 =item destination
807  
808 If the above fails and there is a destination query parameter, which must a taint check against the DETAINT_URL_REGEXP config parameter,
809 then there will be a 302 redirection to that location.
810  
811 =item original destination
812  
813 If all the above fail then there the originally requested page will be delivered.
814  
815 =back
816  
817 =cut
818  
819 sub redirect_after_login {
820 29 ▶ my $self = shift;
Subroutine redirect_after_login: called CC=4 SCAR=13.9
Pod documented
821 29 my $cgiapp = $self->_cgiapp;
822 29 my $config = $self->_config;
823  
824 29 ▶ if ($config->{POST_LOGIN_RUNMODE}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($config->{'POST_LOGIN_RUNMODE'}) { } 1 28 29
elsif ($config->{'POST_LOGIN_URL'}) { } 1 27 28
elsif (my $destination = $cgiapp->authen->_detaint_destination) { } 1 26 27
825 1 $cgiapp->prerun_mode($config->{POST_LOGIN_RUNMODE});
826 } elsif ($config->{POST_LOGIN_URL}) {
827 1 $cgiapp->header_add(-location => $config->{POST_LOGIN_URL});
828 1 $cgiapp->header_type('redirect');
829 1 $cgiapp->prerun_mode('authen_dummy_redirect');
830 } elsif (my $destination = $cgiapp->authen->_detaint_destination()) {
831 1 $cgiapp->header_add(-location => $destination);
832 1 $cgiapp->header_type('redirect');
833 1 $cgiapp->prerun_mode('authen_dummy_redirect');
834 }
835 29 return;
836 }
837  
838 =head2 redirect_to_login
839  
840 This method is be called during the prerun stage if
841 the current user is not logged in, and they are trying to
842 access a protected runmode. It will redirect to the page
843 that has been configured as the login page, based on the value
844 of LOGIN_RUNMODE or LOGIN_URL If nothing is configured
845 a simple login page will be automatically provided.
846  
847 =cut
848  
849 sub redirect_to_login {
850 58 ▶ my $self = shift;
Subroutine redirect_to_login: called CC=3 SCAR=13.6
Pod documented
851 58 my $cgiapp = $self->_cgiapp;
852 58 my $config = $self->_config;
853  
854 58 ▶ if ($config->{LOGIN_RUNMODE}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($config->{'LOGIN_RUNMODE'}) { } 0 58 58
elsif ($config->{'LOGIN_URL'}) { } 0 58 58
855 0 $cgiapp->prerun_mode($config->{LOGIN_RUNMODE});
856 } elsif ($config->{LOGIN_URL}) {
857 0 $cgiapp->header_add(-location => $config->{LOGIN_URL});
858 0 $cgiapp->header_type('redirect');
859 0 $cgiapp->prerun_mode('authen_dummy_redirect');
860 } else {
861 58 $cgiapp->prerun_mode('authen_login');
862 }
863 }
864  
865 =head2 redirect_to_logout
866  
867 This method is called during the prerun stage if the user
868 has requested to be logged out. It will redirect to the page
869 that has been configured as the logout page, based on the value
870 of LOGOUT_RUNMODE or LOGOUT_URL If nothing is
871 configured, the page will redirect to the website homepage.
872  
873 =cut
874  
875 sub redirect_to_logout {
876 1 ▶ my $self = shift;
Subroutine redirect_to_logout: called CC=3 SCAR=12.5
Pod documented
877 1 my $cgiapp = $self->_cgiapp;
878 1 my $config = $self->_config;
879 1 $self->logout();
880  
881 1 ▶ if ($config->{LOGOUT_RUNMODE}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($config->{'LOGOUT_RUNMODE'}) { } 0 1 1
elsif ($config->{'LOGOUT_URL'}) { } 0 1 1
882 0 $cgiapp->prerun_mode($config->{LOGOUT_RUNMODE});
883 } elsif ($config->{LOGOUT_URL}) {
884 0 $cgiapp->header_add(-location => $config->{LOGOUT_URL});
885 0 $cgiapp->header_type('redirect');
886 0 $cgiapp->prerun_mode('authen_dummy_redirect');
887 } else {
888 1 $cgiapp->header_add(-location => '/');
889 1 $cgiapp->header_type('redirect');
890 1 $cgiapp->prerun_mode('authen_dummy_redirect');
891 }
892 }
893  
894 =head2 setup_runmodes
895  
896 This method is called during the prerun stage to register some custom
897 runmodes that the Authentication plugin requires in order to function.
898 Calling this function, will not itself generate cookies or session ids.
899  
900 =cut
901  
902 sub setup_runmodes {
903 97 ▶ my $self = shift;
Subroutine setup_runmodes: called CC=5 SCAR=18.4
Pod documented
904 97 my $config = $self->_config;
905  
906 $self->_cgiapp->run_modes( authen_login => \&authen_login_runmode )
907 97 ▶ unless $config->{LOGIN_RUNMODE} || $config->{LOGIN_URL};
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{"LOGIN_RUNMODE"} or $config->{"LOGIN_URL"} 97 0 97
Condition33%1/3
$config->{"LOGIN_RUNMODE"} or $config->{"LOGIN_URL"}
l!l&&r!l&&!r
0097
MC/DC0%0/2
$config->{"LOGIN_RUNMODE"} or $config->{"LOGIN_URL"}
$config->{"LOGIN_RUNMODE"} $config->{"LOGIN_URL"}
Truth table33%1/3
A or B
A= $config->{"LOGIN_RUNMODE"}B= $config->{"LOGIN_URL"}
ABresult
1X1
011
000
908 $self->_cgiapp->run_modes( authen_logout => \&authen_logout_runmode )
909 97 ▶ unless $config->{LOGOUT_RUNMODE} || $config->{LOGOUT_URL};
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{"LOGOUT_RUNMODE"} or $config->{"LOGOUT_URL"} 97 0 97
Condition33%1/3
$config->{"LOGOUT_RUNMODE"} or $config->{"LOGOUT_URL"}
l!l&&r!l&&!r
0097
MC/DC0%0/2
$config->{"LOGOUT_RUNMODE"} or $config->{"LOGOUT_URL"}
$config->{"LOGOUT_RUNMODE"} $config->{"LOGOUT_URL"}
Truth table33%1/3
A or B
A= $config->{"LOGOUT_RUNMODE"}B= $config->{"LOGOUT_URL"}
ABresult
1X1
011
000
910 97 $self->_cgiapp->run_modes( authen_dummy_redirect => \&authen_dummy_redirect );
911 97 return;
912 }
913  
914 =head2 last_login
915  
916 This will return return the time of the last login for this user
917  
918 my $last_login = $self->authen->last_login;
919  
920 This function will initiate a session or cookie if one has not been created already.
921  
922 =cut
923  
924 sub last_login {
925 5 ▶ my $self = shift;
Subroutine last_login: called CC=3 SCAR=11.0
Pod documented
926 5 my $new = shift;
927 5 $self->initialize;
928  
929 5 ▶ return unless $self->username;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->username 2 3 5
930 3 my $old = $self->store->fetch('last_login');
931 3 ▶ $self->store->save('last_login' => $new) if $new;
Branch: 1 branch
BranchTrueFalseTotal
if $new 0 3 3
932 3 return $old;
933 }
934  
935 =head2 last_access
936  
937 This will return return the time of the last access for this user
938  
939 my $last_access = $self->authen->last_access;
940  
941 This function will initiate a session or cookie if one has not been created already.
942  
943 =cut
944  
945 sub last_access {
946 9 ▶ my $self = shift;
Subroutine last_access: called CC=3 SCAR=11.0
Pod documented
947 9 my $new = shift;
948 9 $self->initialize;
949  
950 9 ▶ return unless $self->username;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->username 5 4 9
951 4 my $old = $self->store->fetch('last_access');
952 4 ▶ $self->store->save('last_access' => $new) if $new;
Branch: 1 branch
BranchTrueFalseTotal
if $new 1 3 4
953 4 return $old;
954 }
955  
956 =head2 is_login_timeout
957  
958 This will return true or false depending on whether the users login status just timed out
959  
960 $self->add_message('login session timed out') if $self->authen->is_login_timeout;
961  
962 This function will initiate a session or cookie if one has not been created already.
963  
964 =cut
965  
966 sub is_login_timeout {
967 5 ▶ my $self = shift;
Subroutine is_login_timeout: called CC=2 SCAR=6.9
Pod documented
968 5 $self->initialize;
969  
970 5 ▶ return $self->{is_login_timeout} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'is_login_timeout'} ? : 1 4 5
971 }
972  
973 =head2 is_authenticated
974  
975 This will return true or false depending on the login status of this user
976  
977 assert($self->authen->is_authenticated); # The user should be logged in if we got here
978  
979 This function will initiate a session or cookie if one has not been created already.
980  
981 =cut
982  
983 sub is_authenticated {
984 146 ▶ my $self = shift;
Subroutine is_authenticated: called CC=2 SCAR=6.9
Pod documented
985 146 $self->initialize;
986  
987 146 ▶ return $self->username ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->username ? : 30 116 146
988 }
989  
990 =head2 login_attempts
991  
992 This method will return the number of failed login attempts have been made by this
993 user since the last successful login. This is not a number that can be trusted,
994 as it is dependant on the underlying store to be able to return the correct value for
995 this user. For example, if the store uses a cookie based session, the user trying
996 to login could delete their cookies, and hence get a new session which will not have
997 any login attempts listed. The number will be cleared upon a successful login.
998 This function will initiate a session or cookie if one has not been created already.
999  
1000 =cut
1001  
1002 sub login_attempts {
1003 80 ▶ my $self = shift;
Subroutine login_attempts: called CC=1 SCAR=0.0
Pod documented
1004 80 $self->initialize;
1005  
1006 80 my $la = $self->store->fetch('login_attempts');
1007 80 return $la;
1008 }
1009  
1010 =head2 username
1011  
1012 This will return the username of the currently logged in user, or undef if
1013 no user is currently logged in.
1014  
1015 my $username = $self->authen->username;
1016  
1017 This function will initiate a session or cookie if one has not been created already.
1018  
1019 =cut
1020  
1021 sub username {
1022 310 ▶ my $self = shift;
Subroutine username: called CC=1 SCAR=0.0
Pod documented
1023 310 $self->initialize;
1024  
1025 310 my $u = $self->store->fetch('username');
1026 310 return $u;
1027 }
1028  
1029 =head2 is_new_login
1030  
1031 This will return true or false depending on if this is a fresh login
1032  
1033 $self->log->info("New Login") if $self->authen->is_new_login;
1034  
1035 This function will initiate a session or cookie if one has not been created already.
1036  
1037 =cut
1038  
1039 sub is_new_login {
1040 96 ▶ my $self = shift;
Subroutine is_new_login: called CC=1 SCAR=0.0
Pod documented
1041 96 $self->initialize;
1042  
1043 96 return $self->{is_new_login};
1044 }
1045  
1046 =head2 credentials
1047  
1048 This method will return the names of the form parameters that will be
1049 looked for during a login. By default they are authen_username and authen_password,
1050 but these values can be changed by supplying the CREDENTIALS parameters in the
1051 configuration. Calling this function, will not itself generate cookies or session ids.
1052  
1053 =cut
1054  
1055 sub credentials {
1056 127 ▶ my $self = shift;
Subroutine credentials: called CC=2 SCAR=6.9
Pod documented
1057 127 my $config = $self->_config;
1058 127 ▶ return $config->{CREDENTIALS} || [qw(authen_username authen_password)];
Condition100%2/2
$config->{'CREDENTIALS'} || ['authen_username', 'authen_password']
l!l
19108
MC/DC100%1/1
$config->{'CREDENTIALS'} || ['authen_username', 'authen_password']
$config->{'CREDENTIALS'}
Truth table100%2/2
A
A= $config->{'CREDENTIALS'}
Aresult
11
00
1059 }
1060  
1061 =head2 logout
1062  
1063 This will attempt to logout the user. If during a request the Authentication
1064 module sees a parameter called 'authen_logout', it will automatically call this method
1065 to log out the user.
1066  
1067 $self->authen->logout();
1068  
1069 This function will initiate a session or cookie if one has not been created already.
1070  
1071 =cut
1072  
1073 sub logout {
1074 5 ▶ my $self = shift;
Subroutine logout: called CC=1 SCAR=0.0
Pod documented
1075 5 $self->initialize;
1076  
1077 5 $self->store->clear;
1078 }
1079  
1080 =head2 drivers
1081  
1082 This method will return a list of driver objects that are used for
1083 verifying the login credentials. Calling this function, will not itself generate cookies or session ids.
1084  
1085 =cut
1086  
1087 sub drivers {
1088 95 ▶ my $self = shift;
Subroutine drivers: called CC=7 SCAR=19.5
Pod documented
1089  
1090 95 ▶ if ( !$self->{drivers} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"drivers"}) 86 9 95
1091 86 my $config = $self->_config;
1092  
1093 # Fetch the configuration parameters for the driver(s)
1094 86 ▶ my $driver_configs = defined $config->{DRIVER} ? $config->{DRIVER} : [['Dummy']];
Branch: 1 branch
BranchTrueFalseTotal
defined $config->{'DRIVER'} ? : 83 3 86
1095  
1096 86 foreach my $driver_config (@$driver_configs) {
1097 118 my ($drivername, @params) = @$driver_config;
1098 # add support for Authen::Simple modules
1099 118 ▶ if (index($drivername, 'Authen::Simple') == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($drivername, "Authen::Simple") == 0) 2 116 118
1100 2 unshift @params, $drivername;
1101 2 $drivername = 'Authen::Simple';
1102 }
1103 # Load the the class for this driver
1104 118 ▶ my $driver_class = _find_deligate_class(
Condition100%2/2
_find_deligate_class('CGI::Application::Plugin::Authentication::Driver::' . $drivername, $drivername) || die('Driver ' . $drivername . ' can not be found')
l!l
1171
MC/DC100%1/1
_find_deligate_class('CGI::Application::Plugin::Authentication::Driver::' . $drivername, $drivername) || die('Driver ' . $drivername . ' can not be found')
_find_deligate_class('CGI::Application::Plugin::Authentication::Driver::' . $drivername, $drivername)
Truth table100%2/2
A
A= _find_deligate_class('CGI::Application::Plugin::Authentication::Driver::' . $drivername, $drivername)
Aresult
11
00
1105 'CGI::Application::Plugin::Authentication::Driver::' . $drivername,
1106 $drivername
1107 ) || die "Driver ".$drivername." can not be found";
1108  
1109 # Create the driver object
1110 117 ▶ my $driver = $driver_class->new( $self, @params )
Condition100%2/2
$driver_class->new($self, @params) || die("Could not create new $driver_class object")
l!l
1161
MC/DC100%1/1
$driver_class->new($self, @params) || die("Could not create new $driver_class object")
$driver_class->new($self, @params)
Truth table100%2/2
A
A= $driver_class->new($self, @params)
Aresult
11
00
1111 || die "Could not create new $driver_class object";
1112 116 push @{$self->{drivers}}, $driver;
1113 }
1114 }
1115  
1116 93 my $drivers = $self->{drivers};
1117 93 return @$drivers[0..$#$drivers];
1118 }
1119  
1120 =head2 store
1121  
1122 This method will return a store object that is used to store information
1123 about the status of the authentication across multiple requests.
1124 This function will initiate a session or cookie if one has not been created already.
1125  
1126 =cut
1127  
1128 sub store {
1129 502 ▶ my $self = shift;
Subroutine store: called CC=9 SCAR=28.0
Pod documented
1130  
1131 502 ▶ if ( !$self->{store} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"store"}) 105 397 502
1132 105 my $config = $self->_config;
1133  
1134 # Fetch the configuration parameters for the store
1135 105 my ($store_module, @store_config);
1136 105 ▶ ($store_module, @store_config) = @{ $config->{STORE} } if $config->{STORE} && ref $config->{STORE} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"STORE"} and ref $config->{"STORE"} eq "ARRAY" 105 0 105
Condition33%1/3
$config->{"STORE"} and ref $config->{"STORE"} eq "ARRAY"
!ll&&!rl&&r
00105
MC/DC0%0/2
$config->{"STORE"} and ref $config->{"STORE"} eq "ARRAY"
$config->{"STORE"} ref $config->{"STORE"} eq "ARRAY"
Truth table33%1/3
A and B
A= $config->{"STORE"}B= ref $config->{"STORE"} eq "ARRAY"
ABresult
0X0
100
111
1137 105 ▶ if (!$store_module) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($store_module) 0 105 105
1138 # No STORE configuration was provided
1139 0 ▶ if ($self->_cgiapp->can('session') && UNIVERSAL::isa($self->_cgiapp->session, 'CGI::Session')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_cgiapp->can('session') and UNIVERSAL::isa($self->_cgiapp->session, 'CGI::Session')) { } 0 0 0
Condition0%0/3
$self->_cgiapp->can("session") and UNIVERSAL::isa($self->_cgiapp->session, "CGI::Session")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->_cgiapp->can("session") and UNIVERSAL::isa($self->_cgiapp->session, "CGI::Session")
$self->_cgiapp->can("session") UNIVERSAL::isa($self->_cgiapp->session, "CGI::Session")
Truth table0%0/3
A and B
A= $self->_cgiapp->can("session")B= UNIVERSAL::isa($self->_cgiapp->session, "CGI::Session")
ABresult
0X0
100
111
1140 # The user is already using the Session plugin
1141 0 ($store_module, @store_config) = ( 'Session' );
1142 } else {
1143 # Fall back to the Cookie Store
1144 0 ($store_module, @store_config) = ( 'Cookie' );
1145 }
1146 }
1147  
1148 # Load the the class for this store
1149 105 ▶ my $store_class = _find_deligate_class(
Condition50%1/2
_find_deligate_class('CGI::Application::Plugin::Authentication::Store::' . $store_module, $store_module) || die("Store $store_module can not be found")
l!l
1050
MC/DC0%0/1
_find_deligate_class('CGI::Application::Plugin::Authentication::Store::' . $store_module, $store_module) || die("Store $store_module can not be found")
_find_deligate_class('CGI::Application::Plugin::Authentication::Store::' . $store_module, $store_module)
Truth table50%1/2
A
A= _find_deligate_class('CGI::Application::Plugin::Authentication::Store::' . $store_module, $store_module)
Aresult
11
00
1150 'CGI::Application::Plugin::Authentication::Store::' . $store_module,
1151 $store_module
1152 ) || die "Store $store_module can not be found";
1153  
1154 # Create the store object
1155 105 ▶ $self->{store} = $store_class->new( $self, @store_config )
Condition50%1/2
$store_class->new($self, @store_config) || die("Could not create new $store_class object")
l!l
1050
MC/DC0%0/1
$store_class->new($self, @store_config) || die("Could not create new $store_class object")
$store_class->new($self, @store_config)
Truth table50%1/2
A
A= $store_class->new($self, @store_config)
Aresult
11
00
1156 || die "Could not create new $store_class object";
1157 }
1158  
1159 502 return $self->{store};
1160 }
1161  
1162 =head2 initialize
1163  
1164 This does most of the heavy lifting for the Authentication plugin. It will
1165 check to see if the user is currently attempting to login by looking for the
1166 credential form fields in the query object. It will load the required driver
1167 objects and authenticate the user. It is OK to call this method multiple times
1168 as it checks to see if it has already been executed and will just return
1169 without doing anything if called multiple times. This allows us to call
1170 initialize as late as possible in the request so that no unnecessary work is
1171 done.
1172  
1173 The user will be logged out by calling the C<logout()> method if the login
1174 session has been idle for too long, if it has been too long since the last
1175 login, or if the login has timed out. If you need to know if a user was logged
1176 out because of a time out, you can call the C<is_login_timeout> method.
1177  
1178 If all goes well, a true value will be returned, although it is usually not
1179 necessary to check.
1180  
1181 This function will initiate a session or cookie if one has not been created already.
1182  
1183 =cut
1184  
1185 sub initialize {
1186 762 ▶ my $self = shift;
Subroutine initialize: called CC=24 SCAR=32.3
Pod documented
1187 762 ▶ return 1 if $self->{initialized};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"initialized"} 656 106 762
1188  
1189 # It would seem to make more sense to do this at the /end/ of the routine
1190 # but that causes an infinite loop.
1191 106 $self->{initialized} = 1;
1192  
1193 106 ▶ if (UNIVERSAL::can($self->_cgiapp, 'devpopup')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($self->_cgiapp, "devpopup")) 0 106 106
1194 0 $self->_cgiapp->add_callback( 'devpopup_report', \&_devpopup_report );
1195 }
1196  
1197 106 my $config = $self->_config;
1198  
1199 # See if the user is trying to log in
1200 # We do this before checking to see if the user is already logged in, since
1201 # a logged in user may want to log in as a different user.
1202 106 ▶ my $field_names = $config->{CREDENTIALS} || [qw(authen_username authen_password)];
Condition100%2/2
$config->{'CREDENTIALS'} || ['authen_username', 'authen_password']
l!l
1987
MC/DC100%1/1
$config->{'CREDENTIALS'} || ['authen_username', 'authen_password']
$config->{'CREDENTIALS'}
Truth table100%2/2
A
A= $config->{'CREDENTIALS'}
Aresult
11
00
1203  
1204 106 my $query = $self->_cgiapp->query;
1205 106 my @credentials = map { $query->param($_) } @$field_names;
1206 106 ▶ if ($credentials[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($credentials[0]) 76 30 106
1207 # The user is trying to login
1208 # make sure if they are already logged in, that we log them out first
1209 76 my $store = $self->store;
1210 76 ▶ $store->clear if $store->fetch('username');
Branch: 1 branch
BranchTrueFalseTotal
if $store->fetch("username") 0 76 76
1211 76 foreach my $driver ($self->drivers) {
1212 104 ▶ if (my $username = $driver->verify_credentials(@credentials)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $username = $driver->verify_credentials(@credentials)) 30 66 96
1213 # This user provided the correct credentials
1214 # so save this new login in the store
1215 30 my $now = time();
1216 30 $store->save( username => $username, login_attempts => 0, last_login => $now, last_access => $now );
1217 30 $self->{is_new_login} = 1;
1218 # See if we are remembering the username for this user
1219 30 ▶ my $login_config = $config->{LOGIN_FORM} || {};
Condition100%2/2
$config->{'LOGIN_FORM'} || {}
l!l
426
MC/DC100%1/1
$config->{'LOGIN_FORM'} || {}
$config->{'LOGIN_FORM'}
Truth table100%2/2
A
A= $config->{'LOGIN_FORM'}
Aresult
11
00
1220 30 ▶ if ($login_config->{REMEMBERUSER_OPTION} && $query->param('authen_rememberuser')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($login_config->{"REMEMBERUSER_OPTION"} and $query->param("authen_rememberuser")) 0 30 30
Condition33%1/3
$login_config->{"REMEMBERUSER_OPTION"} and $query->param("authen_rememberuser")
!ll&&!rl&&r
3000
MC/DC0%0/2
$login_config->{"REMEMBERUSER_OPTION"} and $query->param("authen_rememberuser")
$login_config->{"REMEMBERUSER_OPTION"} $query->param("authen_rememberuser")
Truth table33%1/3
A and B
A= $login_config->{"REMEMBERUSER_OPTION"}B= $query->param("authen_rememberuser")
ABresult
0X0
100
111
1221 my $cookie = $query->cookie(
1222 0 ▶ -name => $login_config->{REMEMBERUSER_COOKIENAME} || 'CAPAUTHTOKEN',
Condition0%0/2
$login_config->{'REMEMBERUSER_COOKIENAME'} || 'CAPAUTHTOKEN'
l!l
00
MC/DC0%0/1
$login_config->{'REMEMBERUSER_COOKIENAME'} || 'CAPAUTHTOKEN'
$login_config->{'REMEMBERUSER_COOKIENAME'}
Truth table0%0/2
A
A= $login_config->{'REMEMBERUSER_COOKIENAME'}
Aresult
11
00
1223 -value => $username,
1224 -expiry => '10y',
1225 );
1226 0 $self->_cgiapp->header_add(-cookie => [$cookie]);
1227 }
1228 30 last;
1229 }
1230 }
1231 68 ▶ unless ($self->username) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->username) 38 30 68
1232 # password mismatch - increment failed login attempts
1233 38 ▶ my $attempts = $store->fetch('login_attempts') || 0;
Condition100%2/2
$store->fetch('login_attempts') || 0
l!l
335
MC/DC100%1/1
$store->fetch('login_attempts') || 0
$store->fetch('login_attempts')
Truth table100%2/2
A
A= $store->fetch('login_attempts')
Aresult
11
00
1234 38 $store->save( login_attempts => $attempts + 1 );
1235 }
1236  
1237 $config->{POST_LOGIN_CALLBACK}->($self->_cgiapp)
1238 68 ▶ if($config->{POST_LOGIN_CALLBACK});
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"POST_LOGIN_CALLBACK"} 17 51 68
1239 }
1240  
1241 # Check the user name last of all because only this check might create a session behind the scenes.
1242 # In other words if a website works perfectly well without authentication,
1243 # then adding a protected run mode should not add session to the unprotected modes.
1244 # See 60_parsimony.t for the test.
1245 98 ▶ if ($config->{LOGIN_SESSION_TIMEOUT} && !$self->{is_new_login} && $self->username) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"LOGIN_SESSION_TIMEOUT"} and not $self->{"is_new_login"} and $self->username) 4 94 98
Condition100%6/6
$config->{"LOGIN_SESSION_TIMEOUT"} and not $self->{"is_new_login"} and $self->username
!ll&&!rl&&r
9314
$config->{"LOGIN_SESSION_TIMEOUT"} and not $self->{"is_new_login"}
!ll&&!rl&&r
8945
MC/DC100%3/3
$config->{"LOGIN_SESSION_TIMEOUT"} and not $self->{"is_new_login"} and $self->username
$config->{"LOGIN_SESSION_TIMEOUT"} not $self->{"is_new_login"} $self->username
Truth table100%4/4
A and not(B) and C
A= $config->{"LOGIN_SESSION_TIMEOUT"}B= not $self->{"is_new_login"}C= $self->username
ABCresult
0XX0
10X0
1100
1111
1246 # This is not a fresh login, and there are time out rules, so make sure the login is still valid
1247 4 ▶ if ($config->{LOGIN_SESSION_TIMEOUT}->{IDLE_FOR} && time() - $self->last_access >= $config->{LOGIN_SESSION_TIMEOUT}->{IDLE_FOR}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($config->{'LOGIN_SESSION_TIMEOUT'}{'IDLE_FOR'} and time - $self->last_access >= $config->{'LOGIN_SESSION_TIMEOUT'}{'IDLE_FOR'}) { } 1 3 4
elsif ($config->{'LOGIN_SESSION_TIMEOUT'}{'EVERY'} and time - $self->last_login >= $config->{'LOGIN_SESSION_TIMEOUT'}{'EVERY'}) { } 1 2 3
elsif ($config->{'LOGIN_SESSION_TIMEOUT'}{'CUSTOM'} and $config->{'LOGIN_SESSION_TIMEOUT'}{'CUSTOM'}->($self)) { } 1 1 2
Condition100%3/3
$config->{"LOGIN_SESSION_TIMEOUT"}{"IDLE_FOR"} and time - $self->last_access >= $config->{"LOGIN_SESSION_TIMEOUT"}{"IDLE_FOR"}
!ll&&!rl&&r
211
MC/DC100%2/2
$config->{"LOGIN_SESSION_TIMEOUT"}{"IDLE_FOR"} and time - $self->last_access >= $config->{"LOGIN_SESSION_TIMEOUT"}{"IDLE_FOR"}
$config->{"LOGIN_SESSION_TIMEOUT"}{"IDLE_FOR"} time - $self->last_access >= $config->{"LOGIN_SESSION_TIMEOUT"}{"IDLE_FOR"}
Truth table100%3/3
A and B
A= $config->{"LOGIN_SESSION_TIMEOUT"}{"IDLE_FOR"}B= time - $self->last_access >= $config->{"LOGIN_SESSION_TIMEOUT"}{"IDLE_FOR"}
ABresult
0X0
100
111
1248 # this login has been idle for too long
1249 1 $self->{is_login_timeout} = 1;
1250 1 $self->logout;
1251 ▶ } elsif ($config->{LOGIN_SESSION_TIMEOUT}->{EVERY} && time() - $self->last_login >= $config->{LOGIN_SESSION_TIMEOUT}->{EVERY}) {
Condition100%3/3
$config->{"LOGIN_SESSION_TIMEOUT"}{"EVERY"} and time - $self->last_login >= $config->{"LOGIN_SESSION_TIMEOUT"}{"EVERY"}
!ll&&!rl&&r
111
MC/DC100%2/2
$config->{"LOGIN_SESSION_TIMEOUT"}{"EVERY"} and time - $self->last_login >= $config->{"LOGIN_SESSION_TIMEOUT"}{"EVERY"}
$config->{"LOGIN_SESSION_TIMEOUT"}{"EVERY"} time - $self->last_login >= $config->{"LOGIN_SESSION_TIMEOUT"}{"EVERY"}
Truth table100%3/3
A and B
A= $config->{"LOGIN_SESSION_TIMEOUT"}{"EVERY"}B= time - $self->last_login >= $config->{"LOGIN_SESSION_TIMEOUT"}{"EVERY"}
ABresult
0X0
100
111
1252 # it has been too long since the last login
1253 1 $self->{is_login_timeout} = 1;
1254 1 $self->logout;
1255 ▶ } elsif ($config->{LOGIN_SESSION_TIMEOUT}->{CUSTOM} && $config->{LOGIN_SESSION_TIMEOUT}->{CUSTOM}->($self)) {
Condition66%2/3
$config->{"LOGIN_SESSION_TIMEOUT"}{"CUSTOM"} and $config->{"LOGIN_SESSION_TIMEOUT"}{"CUSTOM"}->($self)
!ll&&!rl&&r
011
MC/DC50%1/2
$config->{"LOGIN_SESSION_TIMEOUT"}{"CUSTOM"} and $config->{"LOGIN_SESSION_TIMEOUT"}{"CUSTOM"}->($self)
$config->{"LOGIN_SESSION_TIMEOUT"}{"CUSTOM"} $config->{"LOGIN_SESSION_TIMEOUT"}{"CUSTOM"}->($self)
Truth table66%2/3
A and B
A= $config->{"LOGIN_SESSION_TIMEOUT"}{"CUSTOM"}B= $config->{"LOGIN_SESSION_TIMEOUT"}{"CUSTOM"}->($self)
ABresult
0X0
100
111
1256 # this login has timed out
1257 1 $self->{is_login_timeout} = 1;
1258 1 $self->logout;
1259 }
1260 }
1261 98 return 1;
1262  
1263 }
1264  
1265 =head2 display
1266  
1267 This method will return the
1268 L<CGI::Application::Plugin::Authentication::Display> object, creating
1269 and caching it if necessary.
1270  
1271 =cut
1272  
1273 sub display {
1274 128 ▶ my $self = shift;
Subroutine display: called CC=4 SCAR=13.9
Pod documented
1275 128 ▶ return $self->{display} if $self->{display};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"display"} 67 61 128
1276 61 ▶ my $config = $self->_config->{LOGIN_FORM} || {};
Condition100%2/2
$self->_config->{'LOGIN_FORM'} || {}
l!l
1942
MC/DC100%1/1
$self->_config->{'LOGIN_FORM'} || {}
$self->_config->{'LOGIN_FORM'}
Truth table100%2/2
A
A= $self->_config->{'LOGIN_FORM'}
Aresult
11
00
1277 my $class = "CGI::Application::Plugin::Authentication::Display::".
1278 61 ▶ ($config->{DISPLAY_CLASS} || 'Classic');
Condition100%2/2
$config->{'DISPLAY_CLASS'} || 'Classic'
l!l
655
MC/DC100%1/1
$config->{'DISPLAY_CLASS'} || 'Classic'
$config->{'DISPLAY_CLASS'}
Truth table100%2/2
A
A= $config->{'DISPLAY_CLASS'}
Aresult
11
00
1279 61 $class->require;
1280 61 $self->{display} = $class->new($self->_cgiapp);
1281 61 return $self->{display};
1282 }
1283  
1284 =head2 login_box
1285  
1286 This method will return the HTML for a login box that can be
1287 embedded into another page. This is the same login box that is used
1288 in the default authen_login runmode that the plugin provides.
1289  
1290 This function will initiate a session or cookie if one has not been created already.
1291  
1292 =cut
1293  
1294 sub login_box {
1295 8 ▶ my $self = shift;
Subroutine login_box: called CC=1 SCAR=0.0
Pod documented
1296 8 return $self->display->login_box;
1297 }
1298  
1299 =head2 login_styles
1300  
1301 This method returns a style-sheet that can be used for the login box that the
1302 plugin provides. The login box automatically includes these default styles in
1303 the page unless you set the LOGIN_FORM => INCLUDE_STYLESHEET option to 0. The
1304 colours used in the returned styles can be customized by providing colour
1305 options to LOGIN_FORM configuration parameter.
1306  
1307 Calling this function, will not itself generate cookies or session ids.
1308  
1309 =cut
1310  
1311 sub login_styles {
1312 0 ▶ my $self = shift;
Subroutine login_styles: not called CC=23 SCAR=63.1
Pod documented
1313 0 ▶ my $login_form = $self->_config->{LOGIN_FORM} || {};
Condition0%0/2
$self->_config->{'LOGIN_FORM'} || {}
l!l
00
MC/DC0%0/1
$self->_config->{'LOGIN_FORM'} || {}
$self->_config->{'LOGIN_FORM'}
Truth table0%0/2
A
A= $self->_config->{'LOGIN_FORM'}
Aresult
11
00
1314 0 my %colour = ();
1315 0 warn "CGI::Application::Plugin::Application::login_styles is deprecated";
1316  
1317 0 ▶ $colour{base} = $login_form->{BASE_COLOUR} || '#445588';
Condition0%0/2
$login_form->{'BASE_COLOUR'} || '#445588'
l!l
00
MC/DC0%0/1
$login_form->{'BASE_COLOUR'} || '#445588'
$login_form->{'BASE_COLOUR'}
Truth table0%0/2
A
A= $login_form->{'BASE_COLOUR'}
Aresult
11
00
1318 0 ▶ $colour{lighter} = $login_form->{LIGHTER_COLOUR} if $login_form->{LIGHTER_COLOUR};
Branch: 1 branch
BranchTrueFalseTotal
if $login_form->{"LIGHTER_COLOUR"} 0 0 0
1319 0 ▶ $colour{light} = $login_form->{LIGHT_COLOUR} if $login_form->{LIGHT_COLOUR};
Branch: 1 branch
BranchTrueFalseTotal
if $login_form->{"LIGHT_COLOUR"} 0 0 0
1320 0 ▶ $colour{dark} = $login_form->{DARK_COLOUR} if $login_form->{DARK_COLOUR};
Branch: 1 branch
BranchTrueFalseTotal
if $login_form->{"DARK_COLOUR"} 0 0 0
1321 0 ▶ $colour{darker} = $login_form->{DARKER_COLOUR} if $login_form->{DARKER_COLOUR};
Branch: 1 branch
BranchTrueFalseTotal
if $login_form->{"DARKER_COLOUR"} 0 0 0
1322 0 ▶ $colour{grey} = $login_form->{GREY_COLOUR} if $login_form->{GREY_COLOUR};
Branch: 1 branch
BranchTrueFalseTotal
if $login_form->{"GREY_COLOUR"} 0 0 0
1323
1324 0 ▶ if ( grep { ! defined $colour{$_} || index($colour{$_}, '%') >= 0 } qw(lighter light dark darker) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (grep {not defined $colour{$_} or index($colour{$_}, "%") != -1;} "lighter", "light", "dark", "darker") 0 0 0
not defined $colour{$_} or index($colour{$_}, "%") != -1 0 0 0
1325 0 eval { require Color::Calc };
1326 0 ▶ if ($@ && $login_form->{BASE_COLOUR}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ and $login_form->{"BASE_COLOUR"}) 0 0 0
Condition0%0/3
$@ and $login_form->{"BASE_COLOUR"}
!ll&&!rl&&r
000
MC/DC0%0/2
$@ and $login_form->{"BASE_COLOUR"}
$@ $login_form->{"BASE_COLOUR"}
Truth table0%0/3
A and B
A= $@B= $login_form->{"BASE_COLOUR"}
ABresult
0X0
100
111
1327 0 warn "Color::Calc is required when specifying a custom BASE_COLOUR, and leaving LIGHTER_COLOUR, LIGHT_COLOUR, DARK_COLOUR or DARKER_COLOUR blank or when providing percentage based colour";
1328 }
1329 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
1330 0 $colour{base} = '#445588';
1331 0 $colour{lighter} = '#d0d5e1';
1332 0 $colour{light} = '#a2aac4';
1333 0 $colour{dark} = '#303c5f';
1334 0 $colour{darker} = '#1b2236';
1335 0 $colour{grey} = '#565656';
1336 } else {
1337 $colour{lighter} = !$colour{lighter} ? Color::Calc::light_html($colour{base}, 0.75)
1338 : $colour{lighter} =~ m#(\d+)%# ? Color::Calc::light_html($colour{base}, $1 / 100)
1339 0 ▶ : $colour{lighter};
Branch: 2 branches
BranchTrueFalseTotal
!$colour{'lighter'} ? : 0 0 0
$colour{'lighter'} =~ /(\d+)%/ ? : 0 0 0
1340 $colour{light} = !$colour{light} ? Color::Calc::light_html($colour{base}, 0.5)
1341 : $colour{light} =~ m#(\d+)%# ? Color::Calc::light_html($colour{base}, $1 / 100)
1342 0 ▶ : $colour{light};
Branch: 2 branches
BranchTrueFalseTotal
!$colour{'light'} ? : 0 0 0
$colour{'light'} =~ /(\d+)%/ ? : 0 0 0
1343 $colour{dark} = !$colour{dark} ? Color::Calc::dark_html($colour{base}, 0.3)
1344 : $colour{dark} =~ m#(\d+)%# ? Color::Calc::dark_html($colour{base}, $1 / 100)
1345 0 ▶ : $colour{dark};
Branch: 2 branches
BranchTrueFalseTotal
!$colour{'dark'} ? : 0 0 0
$colour{'dark'} =~ /(\d+)%/ ? : 0 0 0
1346 $colour{darker} = !$colour{darker} ? Color::Calc::dark_html($colour{base}, 0.6)
1347 : $colour{darker} =~ m#(\d+)%# ? Color::Calc::dark_html($colour{base}, $1 / 100)
1348 0 ▶ : $colour{darker};
Branch: 2 branches
BranchTrueFalseTotal
!$colour{'darker'} ? : 0 0 0
$colour{'darker'} =~ /(\d+)%/ ? : 0 0 0
1349 0 ▶ $colour{grey} ||= Color::Calc::bw_html($colour{base});
Condition0%0/3
$colour{"grey"} ||= Color::Calc::bw_html($colour{"base"})
l!l&&r!l&&!r
000
MC/DC0%0/2
$colour{"grey"} ||= Color::Calc::bw_html($colour{"base"})
$colour{"grey"} Color::Calc::bw_html($colour{"base"})
Truth table0%0/3
A ||= B
A= $colour{"grey"}B= Color::Calc::bw_html($colour{"base"})
ABresult
1X1
011
000
1350 }
1351 }
1352 0 ▶ $colour{grey} ||= '#565656';
Condition0%0/2
$colour{"grey"} ||= "#565656"
l!l
00
MC/DC0%0/1
$colour{"grey"} ||= "#565656"
$colour{"grey"}
Truth table0%0/2
A
A= $colour{"grey"}
Aresult
11
00
1353 0 return <<END;
1354 div.login {
1355 width: 25em;
1356 margin: auto;
1357 padding: 3px;
1358 font-weight: bold;
1359 border: 2px solid $colour{base};
1360 color: $colour{dark};
1361 font-family: sans-serif;
1362 }
1363 div.login div {
1364 margin: 0;
1365 padding: 0;
1366 border: none;
1367 }
1368 div.login .login_header {
1369 background: $colour{base};
1370 border-bottom: 1px solid $colour{darker};
1371 height: 1.5em;
1372 padding: 0.45em;
1373 text-align: left;
1374 color: #fff;
1375 font-size: 100%;
1376 font-weight: bold;
1377 }
1378 div.login .login_content {
1379 background: $colour{lighter};
1380 padding: 0.8em;
1381 border-top: 1px solid white;
1382 border-bottom: 1px solid $colour{grey};
1383 font-size: 80%;
1384 }
1385 div.login .login_footer {
1386 background: $colour{light};
1387 border-top: 1px solid white;
1388 border-bottom: 1px solid white;
1389 text-align: left;
1390 padding: 0;
1391 margin: 0;
1392 min-height: 2.8em;
1393 }
1394 div.login fieldset {
1395 margin: 0;
1396 padding: 0;
1397 border: none;
1398 width: 100%;
1399 }
1400 div.login label {
1401 clear: left;
1402 float: left;
1403 padding: 0.6em 1em 0.6em 0;
1404 width: 8em;
1405 text-align: right;
1406 }
1407 /* image courtesy of http://www.famfamfam.com/lab/icons/silk/ */
1408 #authen_loginfield {
1409 background: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAG5SURBVHjaYvz//z8DJQAggFiIVfh0twHn9w8KD9+/ZBT+9/cfExfvwwc87GxWAAFEtAFf3yl++/9XikHXL56BkYmJ4dKmcoUPT99PBQggRmK8ALT9v4BUBQMLrxxQMztY7N+PjwyXtk76BxBATMRoFjGewsDCx8jw9Oxyht9vboIxCDAxs/wCCCC8LoBrZv/A8PPpVoZ/39gZ7p57xcDLJ8Xw5tkdBrO8DYwAAcRElOYXaxn+/73DwC4vzyAmzsLw58kJsGaQOoAAYiJK868nDGwSXgxvjp1n+Hz7HoNawRFGmFqAAMIw4MBEDaI1gwBAAKEYsKtL/b9x2HSiNYMAQACBA3FmiqKCohrbfQ2nLobn97Yz6Br/JEozCAAEEDgh/eb6d98yYhEDBxsnw5VNZxnOffjLIKltw/D52B6GH89fMVjUnGbEFdgAAQRPiexMzAyfDk9gMJbmYbh17irDueMrGbjExBi8Oy8z4ksnAAEENuDY1S8MjjsnMSgaezJ8Z2Bm+P95PgPX6ycENYMAQACBwyDSUeQ/GzB926kLMEjwsjOwifKvcy05EkxMHgEIIEZKszNAgAEA+j3MEVmacXUAAAAASUVORK5CYII=') no-repeat 0 1px;
1410 background-color: #fff;
1411 border-top: solid 1px $colour{grey};
1412 border-left: solid 1px $colour{grey};
1413 border-bottom: solid 1px $colour{light};
1414 border-right: solid 1px $colour{light};
1415 padding: 2px 0 2px 18px;
1416 margin: 0.3em 0;
1417 width: 12em;
1418 }
1419 /* image courtesy of http://www.famfamfam.com/lab/icons/silk/ */
1420 #authen_passwordfield {
1421 background: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAKbSURBVHjaYvz//z8DPvBko+s0IJUJ5U6X8d+dhSwPEEAMIANw4ccbXKYB8f8/P+6BMYgNEkNWAxBAhDV/Pff/5+t5/39/2gcU/gc25P5qpzkwdQABxIjNCzBnS7p2Mfz5tJ+BkVWE4dWRxWA5oBcYHiyyYnj5heGAedYxR4AAwmXAf0mPWQx/3q9n+P/3I9AAMaCoBsPr4x0MDH/+MUgHrGG4P8eF4fVf9gMAAcSEK/D+/3oA1gxm/3kLJG8wSDhWMAjoeTJ8fxjNoJDQzyD0+7sDQACx4DKAkVWcgZGZG2jIV6AJfxn+/37F8OfPO6BhRxl+f/nIwC7xluHPm58MAAHEhMX5ILHp787OYvj/7zvDr7f7Gf59vw804DUwPM4x/P3+loFb0ZfhVlc1wxMu7psAAcSCEd9MjAzswoYMAppmDD9e9DKwcIkwMHFyMPx+dZnh7+9vDDxqwQx3Ji1jeMrJc9W1/JQOQAAheyFT2mctw9+vpxh+fz7A8O1JDQMrEz/QK2YMb47uZpD0SmEAmsRwu7eJ4QUX1wWXklOGIE0AAcQIim9YShOzSmf49W4xw5+PdxlYeIUYWLh9GS6vXPH+3U/Gd3K/vikzcTAzvOTkOmNXeNIUZitAALFAbF4D9N8Bhl+vJjP8/vCUgY1fkoGZ24PhysoV7178Y9vmW3M8FqZBHS3MAAIIZMDnP59P835/3Mnw98t7Bg5xNQZGNnOgzSvfv2ZgX+dbfiwVX14BCCCQAbyMrNwMDKxcDOxi/Az/WU0YLi1b8/E9K8cqr6JjGQwEAEAAMf378+/cn+//GFi5bRiYuMOBzt7w4RMH50IPIjSDAEAAsbz8+Gfdh9VFEr9//WX7//s/009uzlmuWUcqGYgEAAEGAIZWUhP4bjW1AAAAAElFTkSuQmCC') no-repeat 0 1px;
1422 background-color: #fff;
1423 border-top: solid 1px $colour{grey};
1424 border-left: solid 1px $colour{grey};
1425 border-bottom: solid 1px $colour{light};
1426 border-right: solid 1px $colour{light};
1427 padding: 2px 0 2px 18px;
1428 margin: 0.3em 0;
1429 width: 12em;
1430 }
1431 #authen_rememberuserfield {
1432 clear: left;
1433 margin-left: 8em;
1434 }
1435 #authen_loginfield:focus {
1436 background-color: #ffc;
1437 color: #000;
1438 }
1439 #authen_passwordfield:focus {
1440 background-color: #ffc;
1441 color: #000;
1442 }
1443 div.login a {
1444 font-size: 80%;
1445 color: $colour{dark};
1446 }
1447 div.login div.buttons input {
1448 border-top: solid 2px $colour{light};
1449 border-left: solid 2px $colour{light};
1450 border-bottom: solid 2px $colour{grey};
1451 border-right: solid 2px $colour{grey};
1452 background-color: $colour{lighter};
1453 padding: .2em 1em ;
1454 font-size: 80%;
1455 font-weight: bold;
1456 color: $colour{dark};
1457 }
1458 div.login div.buttons {
1459 display: block;
1460 margin: 8px 4px;
1461 width: 100%;
1462 }
1463 #authen_loginbutton {
1464 float: right;
1465 margin-right: 1em;
1466 }
1467 #authen_registerlink {
1468 display: block;
1469 }
1470 #authen_forgotpasswordlink {
1471 display: block;
1472 }
1473 ul.message {
1474 margin-top: 0;
1475 margin-bottom: 0;
1476 list-style: none;
1477 }
1478 ul.message li {
1479 text-indent: -2em;
1480 padding: 0px;
1481 margin: 0px;
1482 font-style: italic;
1483 }
1484 ul.message li.warning {
1485 color: red;
1486 }
1487 END
1488 }
1489  
1490 =head2 new
1491  
1492 This method creates a new CGI::Application::Plugin::Authentication object. It requires
1493 as it's only parameter a CGI::Application object. This method should never be called
1494 directly, since the 'authen' method that is imported into the CGI::Application module
1495 will take care of creating the CGI::Application::Plugin::Authentication object when it
1496 is required. Calling this function, will not itself generate cookies or session ids.
1497  
1498 =cut
1499  
1500 sub new {
1501 166 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
1502 166 my $cgiapp = shift;
1503 166 my $self = {};
1504  
1505 166 bless $self, $class;
1506 166 $self->{cgiapp} = $cgiapp;
1507 166 Scalar::Util::weaken($self->{cgiapp}); # weaken circular reference
1508  
1509 166 return $self;
1510 }
1511  
1512 =head2 instance
1513  
1514 This method works the same way as 'new', except that it returns the same Authentication
1515 object for the duration of the request. This method should never be called
1516 directly, since the 'authen' method that is imported into the CGI::Application module
1517 will take care of creating the CGI::Application::Plugin::Authentication object when it
1518 is required. Calling this function, will not itself generate cookies or session ids.
1519  
1520 =cut
1521  
1522 sub instance {
1523 1365 ▶ my $class = shift;
Subroutine instance: called CC=4 SCAR=13.9
Pod documented
1524 1365 my $cgiapp = shift;
1525 1365 ▶ die "CGI::Application::Plugin::Authentication->instance must be called with a CGI::Application object"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cgiapp and UNIVERSAL::isa($cgiapp, "CGI::Application") 2 1363 1365
Condition100%3/3
defined $cgiapp and UNIVERSAL::isa($cgiapp, "CGI::Application")
!ll&&!rl&&r
111363
MC/DC100%2/2
defined $cgiapp and UNIVERSAL::isa($cgiapp, "CGI::Application")
defined $cgiapp UNIVERSAL::isa($cgiapp, "CGI::Application")
Truth table100%3/3
A and B
A= defined $cgiappB= UNIVERSAL::isa($cgiapp, "CGI::Application")
ABresult
0X0
100
111
1526 unless defined $cgiapp && UNIVERSAL::isa( $cgiapp, 'CGI::Application' );
1527  
1528 1363 ▶ $cgiapp->{__CAP_AUTHENTICATION_INSTANCE} = $class->new($cgiapp) unless defined $cgiapp->{__CAP_AUTHENTICATION_INSTANCE};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cgiapp->{"__CAP_AUTHENTICATION_INSTANCE"} 166 1197 1363
1529 1363 return $cgiapp->{__CAP_AUTHENTICATION_INSTANCE};
1530 }
1531  
1532  
1533 =head1 CGI::Application CALLBACKS
1534  
1535 =head2 prerun_callback
1536  
1537 This method is a CGI::Application prerun callback that will be
1538 automatically registered for you if you are using CGI::Application
1539 4.0 or greater. If you are using an older version of CGI::Application
1540 you will have to create your own cgiapp_prerun method and make sure you
1541 call this method from there.
1542  
1543 sub cgiapp_prerun {
1544 my $self = shift;
1545  
1546 $self->CGI::Application::Plugin::Authentication::prerun_callback();
1547 }
1548  
1549 =cut
1550  
1551 sub prerun_callback {
1552 105 ▶ my $self = shift;
Subroutine prerun_callback: called CC=7 SCAR=19.5
Pod documented
1553 105 my $authen = $self->authen;
1554  
1555 105 $authen->initialize;
1556  
1557 # setup the default login and logout runmodes
1558 97 $authen->setup_runmodes;
1559  
1560 # The user is asking to be logged out
1561 97 ▶ if ($self->query->param('authen_logout')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->query->param("authen_logout")) 1 96 97
1562 # The user wants to logout
1563 1 return $self->authen->redirect_to_logout;
1564 }
1565  
1566 # If the user just logged in then we may want to redirect them
1567 96 ▶ if ($authen->is_new_login) {
Branch: 1 branch
BranchTrueFalseTotal
if ($authen->is_new_login) 29 67 96
1568 # User just logged in, so where to we send them?
1569 29 return $self->authen->redirect_after_login;
1570 }
1571  
1572 # Update any time out info
1573 67 my $config = $authen->_config;
1574 67 ▶ if ( $config->{LOGIN_SESSION_TIMEOUT} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"LOGIN_SESSION_TIMEOUT"}) 4 63 67
1575 # update the last access time
1576 4 my $now = time;
1577 4 $authen->last_access($now);
1578 }
1579  
1580 # If a perun mode is set check against that.
1581 # This allows cooperation with plugins such as CAP::ActionDispatch
1582 # that also have preun hooks.
1583 # Note the comments in the CGI::Application docs on the ordering of
1584 # callback execution.
1585 67 my $run_mode = $self->prerun_mode;
1586 67 ▶ $run_mode ||= $self->get_current_runmode;
Condition33%1/3
$run_mode ||= $self->get_current_runmode
l!l&&r!l&&!r
0670
MC/DC0%0/2
$run_mode ||= $self->get_current_runmode
$run_mode $self->get_current_runmode
Truth table33%1/3
A ||= B
A= $run_modeB= $self->get_current_runmode
ABresult
1X1
011
000
1587
1588 67 ▶ if ( $authen->is_protected_runmode( $run_mode ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($authen->is_protected_runmode($run_mode)) 59 8 67
1589 # This runmode requires authentication
1590 59 ▶ unless ($authen->is_authenticated) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($authen->is_authenticated) 58 1 59
1591 # This user is NOT logged in
1592 58 return $self->authen->redirect_to_login;
1593 }
1594 }
1595 }
1596  
1597 =head1 CGI::Application RUNMODES
1598  
1599 =head2 authen_login_runmode
1600  
1601 This runmode is provided if you do not want to create your
1602 own login runmode. It will display a simple login form for the user, which
1603 can be replaced by assigning RENDER_LOGIN a coderef that returns the HTML.
1604  
1605 =cut
1606  
1607 sub authen_login_runmode {
1608 58 ▶ my $self = shift;
Subroutine authen_login_runmode: called CC=2 SCAR=7.0
Pod documented
1609 58 my $authen = $self->authen;
1610  
1611 58 my $credentials = $authen->credentials;
1612 58 my $username = $credentials->[0];
1613 58 my $password = $credentials->[1];
1614  
1615 58 my $html;
1616 58 ▶ if ( my $sub = $authen->_config->{RENDER_LOGIN} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = $authen->_config->{'RENDER_LOGIN'}) { } 0 58 58
1617 0 $html = $sub->($self);
1618 }
1619 else {
1620 58 $html = join( "\n",
1621 CGI::start_html( -title => $authen->display->login_title ),
1622 $authen->display->login_box,
1623 CGI::end_html(),
1624 );
1625 }
1626  
1627 58 return $html;
1628 }
1629  
1630 =head2 authen_dummy_redirect
1631  
1632 This runmode is provided for convenience when an external redirect needs
1633 to be done. It just returns an empty string.
1634  
1635 =cut
1636  
1637 sub authen_dummy_redirect {
1638 3 ▶ return '';
Subroutine authen_dummy_redirect: called CC=1 SCAR=0.0
Pod documented
1639 }
1640  
1641 ###
1642 ### Detainting helper methods
1643 ###
1644  
1645 sub _detaint_destination {
1646 103 ▶ my $self = shift;
Subroutine _detaint_destination: called CC=3 SCAR=11.0
1647 103 my $query = $self->_cgiapp->query;
1648 103 my $destination = $query->param('destination');
1649 103 my $regexp = $self->_config->{DETAINT_URL_REGEXP};
1650 103 ▶ if ($destination && $destination =~ /$regexp/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($destination and $destination =~ /$regexp/) { } 4 99 103
Condition100%3/3
$destination and $destination =~ /$regexp/
!ll&&!rl&&r
9724
MC/DC100%2/2
$destination and $destination =~ /$regexp/
$destination $destination =~ /$regexp/
Truth table100%3/3
A and B
A= $destinationB= $destination =~ /$regexp/
ABresult
0X0
100
111
1651 4 $destination = $1;
1652 }
1653 else {
1654 99 $destination = "";
1655 }
1656 103 return $destination;
1657 }
1658  
1659 sub _detaint_selfurl {
1660 73 ▶ my $self = shift;
Subroutine _detaint_selfurl: called CC=2 SCAR=6.9
1661 73 my $query = $self->_cgiapp->query;
1662 73 my $destination = "";
1663 73 my $regexp = $self->_config->{DETAINT_URL_REGEXP};
1664 73 ▶ if ($query->self_url =~ /$regexp/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query->self_url =~ /$regexp/) 71 2 73
1665 71 $destination = $1;
1666 }
1667 73 return $destination;
1668 }
1669  
1670 sub _detaint_url {
1671 76 ▶ my $self = shift;
Subroutine _detaint_url: called CC=2 SCAR=7.1
1672 76 my $query = $self->_cgiapp->query;
1673 76 my $regexp = $self->_config->{DETAINT_URL_REGEXP};
1674 76 my $url = "";
1675 76 ▶ if ($query->url( -absolute => 1, -path_info => 1 ) =~ /$regexp/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query->url("-absolute", 1, "-path_info", 1) =~ /$regexp/) 0 76 76
1676 0 $url = $1;
1677 }
1678 76 return $url;
1679 }
1680  
1681 sub _detaint_username {
1682 65 ▶ my $self = shift;
Subroutine _detaint_username: called CC=4 SCAR=13.9
1683 65 my $username = shift;
1684 65 my $cookiename = shift;
1685 65 my $query = $self->_cgiapp->query;
1686 65 my $regexp = $self->_config->{DETAINT_USERNAME_REGEXP};
1687 65 my $username_value = "";
1688 65 ▶ if (($query->param($username) || $query->cookie($cookiename) || '') =~ /$regexp/) {
Branch: 1 branch
BranchTrueFalseTotal
if (($query->param($username) || $query->cookie($cookiename) || "") =~ /$regexp/) 33 32 65
Condition80%4/5
$query->param($username) || $query->cookie($cookiename) || ''
l!l
3530
$query->param($username) or $query->cookie($cookiename)
l!l&&r!l&&!r
35030
MC/DC50%1/2
$query->param($username) || $query->cookie($cookiename) || ''
$query->param($username) $query->cookie($cookiename)
Truth table66%2/3
A or B
A= $query->param($username)B= $query->cookie($cookiename)
ABresult
1X1
011
000
1689 33 $username_value = $1;
1690 }
1691 65 return $username_value;
1692 }
1693
1694 ###
1695 ### Helper methods
1696 ###
1697  
1698 sub _cgiapp {
1699 1069 ▶ return $_[0]->{cgiapp};
Subroutine _cgiapp: called CC=1 SCAR=0.0
1700 }
1701  
1702 sub _find_deligate_class {
1703 223 ▶ foreach my $class (@_) {
Subroutine _find_deligate_class: called CC=3 SCAR=11.0
1704 282 ▶ $class->require && return $class;
Branch: 1 branch
BranchTrueFalseTotal
$class->require and return $class 222 60 282
1705 }
1706 1 return;
1707 }
1708  
1709 sub _config {
1710 1618 ▶ my $self = shift;
Subroutine _config: called CC=6 SCAR=17.9
1711 1618 ▶ my $class = ref $self ? ref $self : $self;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 1599 19 1618
1712 1618 my $config;
1713 1618 ▶ if ( ref $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 1599 19 1618
1714 1599 ▶ $config = $self->{__CAP_AUTHENTICATION_CONFIG} ||= $__CONFIG{$class} || {};
Condition80%4/5
$self->{"__CAP_AUTHENTICATION_CONFIG"} ||= $CGI::Application::Plugin::Authentication::__CONFIG{$class} || {}
l!l&&r!l&&!r
14351640
$CGI::Application::Plugin::Authentication::__CONFIG{$class} || {}
l!l
7391
MC/DC0%0/2
$self->{"__CAP_AUTHENTICATION_CONFIG"} ||= $CGI::Application::Plugin::Authentication::__CONFIG{$class} || {}
$self->{"__CAP_AUTHENTICATION_CONFIG"} $CGI::Application::Plugin::Authentication::__CONFIG{$class}
Truth table66%2/3
A ||= B
A= $self->{"__CAP_AUTHENTICATION_CONFIG"}B= $CGI::Application::Plugin::Authentication::__CONFIG{$class}
ABresult
1X1
011
000
1715 } else {
1716 19 ▶ $__CONFIG{$class} ||= {};
Condition100%2/2
$CGI::Application::Plugin::Authentication::__CONFIG{$class} ||= {}
l!l
613
MC/DC100%1/1
$CGI::Application::Plugin::Authentication::__CONFIG{$class} ||= {}
$CGI::Application::Plugin::Authentication::__CONFIG{$class}
Truth table100%2/2
A
A= $CGI::Application::Plugin::Authentication::__CONFIG{$class}
Aresult
11
00
1717 19 $config = $__CONFIG{$class};
1718 }
1719 1618 return $config;
1720 }
1721  
1722 sub _devpopup_report {
1723 0 ▶ my $cgiapp = shift;
Subroutine _devpopup_report: not called CC=6 SCAR=37.4
1724 0 my @list;
1725 0 my $self=$cgiapp->authen;
1726 0 ▶ if ($self->username) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->username) 0 0 0
1727 0 push @list,['username',$self->username];
1728 }
1729 0 my $config = $self->_config;
1730 0 ▶ my $field_names = $config->{CREDENTIALS} || [qw(authen_username authen_password)];
Condition0%0/2
$config->{'CREDENTIALS'} || ['authen_username', 'authen_password']
l!l
00
MC/DC0%0/1
$config->{'CREDENTIALS'} || ['authen_username', 'authen_password']
$config->{'CREDENTIALS'}
Truth table0%0/2
A
A= $config->{'CREDENTIALS'}
Aresult
11
00
1731 0 my $query = $cgiapp->query;
1732 0 foreach my $name (@$field_names) {
1733 0 ▶ push @list, [ $name, $query->param($name) || ''];
Condition0%0/2
$query->param($name) || ''
l!l
00
MC/DC0%0/1
$query->param($name) || ''
$query->param($name)
Truth table0%0/2
A
A= $query->param($name)
Aresult
11
00
1734 }
1735 0 my $r=0;
1736 my $text = join $/, map {
1737 0 $r=1-$r;
1738 0 ▶ qq(<tr class="@{[$r?'odd':'even']}"><td valign="top">$_->[0]</td><td>$_->[1]</td></tr>)
Branch: 1 branch
BranchTrueFalseTotal
$r ? : 0 0 0
1739 }
1740 @list;
1741 0 $cgiapp->devpopup->add_report(
1742 title => 'Authentication',
1743 summary => '',
1744 report => qq(
1745 <style type="text/css">
1746 tr.even{background-color:#eee}
1747 </style>
1748 <div style="font-size: 80%">
1749 <table>
1750 <thead><tr><th>Parameter</th><th>Value</th></tr></thead>
1751 <tbody>$text</tbody>
1752 </table>
1753 </div>
1754 ),
1755 );
1756 }
1757  
1758 ###
1759 ### Helper functions
1760 ###
1761  
1762 sub _time_to_seconds {
1763 32 ▶ my $time = shift;
Subroutine _time_to_seconds: called CC=7 SCAR=19.5
1764 32 ▶ return unless defined $time;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $time 0 32 32
1765  
1766 # Most of this function is borrowed from CGI::Util v1.4 by Lincoln Stein
1767 32 my (%mult) = (
1768 's' => 1,
1769 'm' => 60,
1770 'h' => 60 * 60,
1771 'd' => 60 * 60 * 24,
1772 'w' => 60 * 60 * 24 * 7,
1773 'M' => 60 * 60 * 24 * 30,
1774 'y' => 60 * 60 * 24 * 365
1775 );
1776 # format for time can be in any of the forms...
1777 # "180" -- in 180 seconds
1778 # "180s" -- in 180 seconds
1779 # "2m" -- in 2 minutes
1780 # "12h" -- in 12 hours
1781 # "1d" -- in 1 day
1782 # "4w" -- in 4 weeks
1783 # "3M" -- in 3 months
1784 # "2y" -- in 2 years
1785 32 my $offset;
1786 32 ▶ if ( $time =~ /^([+-]?(?:\d+|\d*\.\d*))([smhdwMy]?)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time =~ /^([+-]?(?:\d+|\d*\.\d*))([smhdwMy]?)$/) 29 3 32
1787 29 ▶ return if (!$2 || $2 eq 's') && $1 != int $1; #
Branch: 1 branch
BranchTrueFalseTotal
if !$2 || $2 eq "s" and $1 != int $1 1 28 29
Condition100%6/6
!$2 || $2 eq "s" and $1 != int $1
!ll&&!rl&&r
18101
not $2 or $2 eq "s"
l!l&&r!l&&!r
10118
MC/DC66%2/3
!$2 || $2 eq "s" and $1 != int $1
not $2 $2 eq "s" $1 != int $1
Truth table80%4/5
not(A) or B and C
A= not $2B= $2 eq "s"C= $1 != int $1
ABCresult
00X0
1X00
0100
1X11
0111
1788 28 ▶ $offset = int ( ( $mult{$2} || 1 ) * $1 );
Condition100%2/2
$mult{$2} || 1
l!l
199
MC/DC100%1/1
$mult{$2} || 1
$mult{$2}
Truth table100%2/2
A
A= $mult{$2}
Aresult
11
00
1789 }
1790 31 return $offset;
1791 }
1792  
1793  
1794 =head1 EXAMPLE
1795  
1796 In a CGI::Application module:
1797  
1798 use base qw(CGI::Application);
1799 use CGI::Application::Plugin::AutoRunmode;
1800 use CGI::Application::Plugin::Session;
1801 use CGI::Application::Plugin::Authentication;
1802  
1803 __PACKAGE__->authen->config(
1804 DRIVER => [ 'Generic', { user1 => '123' } ],
1805 STORE => 'Session',
1806 LOGOUT_RUNMODE => 'start',
1807 );
1808 __PACKAGE__->authen->protected_runmodes(qr/^auth_/, 'one');
1809  
1810 sub start : RunMode {
1811 my $self = shift;
1812  
1813 }
1814  
1815 sub one : RunMode {
1816 my $self = shift;
1817  
1818 # The user will only get here if they are logged in
1819 }
1820  
1821 sub auth_two : RunMode {
1822 my $self = shift;
1823  
1824 # This is also protected because of the
1825 # regexp call to protected_runmodes above
1826 }
1827  
1828 =head1 COMPATIBILITY WITH L<CGI::Application::Plugin::ActionDispatch>
1829  
1830 The prerun callback has been modified so that it will check for the presence of a prerun mode.
1831 This is for compatibility with L<CGI::Application::Plugin::ActionDispatch>. This
1832 change should be considered experimental. It is necessary to load the ActionDispatch
1833 module so that the two prerun callbacks will be called in the correct order.
1834  
1835 =head1 RECOMMENDED USAGE
1836  
1837 =over
1838  
1839 =item CSS
1840  
1841 The best practice nowadays is generally considered to be to not have CSS
1842 embedded in HTML. Thus it should be best to set LOGIN_FORM -> DISPLAY_CLASS to
1843 'Basic'.
1844  
1845 =item Post login destination
1846  
1847 Of the various means of selecting a post login destination the most secure would
1848 seem to be POST_LOGIN_URL. The C<destination> parameter could potentially be hijacked by hackers.
1849 The POST_LOGIN_RUNMODE parameter requires a hidden parameter that could potentially
1850 be hijacked.
1851  
1852 =item Taint mode
1853  
1854 Do run your code under taint mode. It should help protect your application
1855 against a number of attacks.
1856  
1857 =item URL and username checking
1858  
1859 Please set the C<DETAINT_URL_REGEXP> and C<DETAINT_USERNAME_REGEXP> parameters
1860 as tightly as possible. In particular you should prevent the destination parameter
1861 being used to redirect authenticated users to external sites; unless of course that
1862 is what you want in which case that site should be the only possible external site.
1863  
1864 =item The login form
1865  
1866 The HTML currently generated does not seem to be standards compliant as per
1867 RT bug 58023. Also the default login form includes hidden forms which could
1868 conceivably be hijacked.
1869 Set LOGIN_FORM -> DISPLAY_CLASS to 'Basic' to fix this.
1870  
1871 =back
1872  
1873 =head1 TODO
1874  
1875 There are lots of things that can still be done to improve this plugin. If anyone else is interested
1876 in helping out feel free to dig right in. Many of these things don't need my input, but if you want
1877 to avoid duplicated efforts, send me a note, and I'll let you know of anyone else is working in the same area.
1878  
1879 =over 4
1880  
1881 =item review the code for security bugs and report
1882  
1883 =item complete the separation of presentation and logic
1884  
1885 =item write a tutorial
1886  
1887 =item build more Drivers (Class::DBI, LDAP, Radius, etc...)
1888  
1889 =item Add support for method attributes to identify runmodes that require authentication
1890  
1891 =item finish the test suite
1892  
1893 =item provide more example code
1894  
1895 =item clean up the documentation
1896  
1897 =item build a DB driver that builds it's own table structure. This can be used by people that don't have their own user database to work with, and could include a simple user management application.
1898  
1899  
1900 =back
1901  
1902 =head1 BUGS
1903  
1904 This is alpha software and as such, the features and interface
1905 are subject to change. So please check the Changes file when upgrading.
1906  
1907 Some of the test scripts appear to be incompatible with versions of
1908 L<Devel::Cover> later than 0.65.
1909  
1910 =head1 SEE ALSO
1911  
1912 L<CGI::Application>, perl(1)
1913  
1914  
1915 =head1 AUTHOR
1916  
1917 Author: Cees Hek <ceeshek@gmail.com>; Co-maintainer: Nicholas Bamber <nicholas@periapt.co.uk>.
1918  
1919 =head1 CREDITS
1920  
1921 Thanks to L<SiteSuite|http://www.sitesuite.com.au> for funding the
1922 development of this plugin and for releasing it to the world.
1923  
1924 Thanks to Christian Walde for suggesting changes to fix the incompatibility with
1925 L<CGI::Application::Plugin::ActionDispatch> and for help with github.
1926  
1927 Thanks to Alexandr Ciornii for pointing out some typos.
1928  
1929 =head1 LICENCE AND COPYRIGHT
1930  
1931 Copyright (c) 2005, SiteSuite. All rights reserved.
1932 Copyright (c) 2010, Nicholas Bamber. (Portions of the code).
1933  
1934 This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
1935  
1936 The background images in the default login forms are used courtesy of
1937 L<www.famfamfam.com|http://www.famfamfam.com/lab/icons/silk/>. Those icons
1938 are issued under the
1939 L<Creative Commons Attribution 3.0 License|http://creativecommons.org/licenses/by/3.0/>.
1940 Those icons are copyrighted 2006 by Mark James <mjames at gmail dot com>
1941  
1942 =head1 DISCLAIMER OF WARRANTY
1943  
1944 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
1945  
1946 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
1947  
1948 =cut
1949  
1950 29 1;
« blib/lib/CGI/Application/Plugin/Authentication/Driver/DBI.pm ↑ Summary blib/lib/CGI/Application/Plugin/Authentication/Display/Classic.pm »