lib/CGI/Auth/Auto.pm

filter: Statementstmt 33.0% 44 / 133 Branchbran 10.8% 5 / 46 Conditioncond 21.4% 6 / 28 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 30.0% 9 / 30 Podpod 60.0% 9 / 15 totaltotal 27.1% 73 / 269 CC 35 SCAR 30.0
CC
35
Cov
27%
_requested_logout
72.0
new
32.9
_pre_check
20.0
CRAP 20.1
↑ Summary
1 package CGI::Auth::Auto;
2 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use base qw(CGI::Auth);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use LEOCHARRE::DEBUG;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use CGI::Scriptpaths;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 $VERSION = sprintf "%d.%02d", q$Revision: 1.21 $ =~ /(\d+)/g;
9  
10 1 $CGI::Auth::Auto::CGI_APP_COMPATIBLE = 'rm=logout';
11  
12  
13 1 ▶sub new {
Subroutine new: called CC=13 SCAR=34.9
Pod documented
14 1 my $proto = shift;
15 1 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
16 1 my $self = {};
17 1 bless $self, $class;
18  
19 1 my $param = shift;
20 $param->{-authfields} ||= [
21 1 ▶ {id => 'user', display => 'User Name', hidden => 0, required => 1},
Condition50%1/2
$param->{"-authfields"} ||= [{"id", "user", "display", "User Name", "hidden", 0, "required", 1}, {"id", "pw", "display", "Password", "hidden", 1, "required", 1}]
l!l
01
MC/DC0%0/1
$param->{"-authfields"} ||= [{"id", "user", "display", "User Name", "hidden", 0, "required", 1}, {"id", "pw", "display", "Password", "hidden", 1, "required", 1}]
$param->{"-authfields"}
Truth table50%1/2
A
A= $param->{"-authfields"}
Aresult
11
00
22 {id => 'pw', display => 'Password', hidden => 1, required => 1},
23 ];
24
25 1 ▶ $param->{-authdir} ||= _guess_authdir();
Condition33%1/3
$param->{"-authdir"} ||= _guess_authdir()
l!l&&r!l&&!r
010
MC/DC0%0/2
$param->{"-authdir"} ||= _guess_authdir()
$param->{"-authdir"} _guess_authdir()
Truth table33%1/3
A ||= B
A= $param->{"-authdir"}B= _guess_authdir()
ABresult
1X1
011
000
26
27 1 ▶ $param->{-formaction} ||= CGI::Scriptpaths::script_rel_path(); #_guess_formaction();
Condition33%1/3
$param->{"-formaction"} ||= CGI::Scriptpaths::script_rel_path()
l!l&&r!l&&!r
010
MC/DC0%0/2
$param->{"-formaction"} ||= CGI::Scriptpaths::script_rel_path()
$param->{"-formaction"} CGI::Scriptpaths::script_rel_path()
Truth table33%1/3
A ||= B
A= $param->{"-formaction"}B= CGI::Scriptpaths::script_rel_path()
ABresult
1X1
011
000
28 1 ▶ $param->{-sessdir} ||= $param->{-authdir}.'/sess';
Condition50%1/2
$param->{"-sessdir"} ||= $param->{"-authdir"} . '/sess'
l!l
01
MC/DC0%0/1
$param->{"-sessdir"} ||= $param->{"-authdir"} . '/sess'
$param->{"-sessdir"}
Truth table50%1/2
A
A= $param->{"-sessdir"}
Aresult
11
00
29
30 1 ▶ if (defined $param->{-logintmplpath} or defined $param->{-logintmpl}){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param->{"-logintmplpath"} or defined $param->{"-logintmpl"}) 0 1 1
Condition33%1/3
defined $param->{"-logintmplpath"} or defined $param->{"-logintmpl"}
l!l&&r!l&&!r
001
MC/DC0%0/2
defined $param->{"-logintmplpath"} or defined $param->{"-logintmpl"}
defined $param->{"-logintmplpath"} defined $param->{"-logintmpl"}
Truth table33%1/3
A or B
A= defined $param->{"-logintmplpath"}B= defined $param->{"-logintmpl"}
ABresult
1X1
011
000
31 0 ▶ $param->{-logintmplpath} ||= $param->{-authdir};
Condition0%0/3
$param->{"-logintmplpath"} ||= $param->{"-authdir"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$param->{"-logintmplpath"} ||= $param->{"-authdir"}
$param->{"-logintmplpath"} $param->{"-authdir"}
Truth table0%0/3
A ||= B
A= $param->{"-logintmplpath"}B= $param->{"-authdir"}
ABresult
1X1
011
000
32 0 ▶ $param->{-logintmpl} ||= 'login.html';
Condition0%0/2
$param->{"-logintmpl"} ||= "login.html"
l!l
00
MC/DC0%0/1
$param->{"-logintmpl"} ||= "login.html"
$param->{"-logintmpl"}
Truth table0%0/2
A
A= $param->{"-logintmpl"}
Aresult
11
00
33 }
34
35 1 ▶ if (DEBUG){
Branch: 1 branch
BranchTrueFalseTotal
if (DEBUG()) 1 0 1
36 1 require Data::Dumper;
37 1 printf STDERR __PACKAGE__."::new() params: %s\n", Data::Dumper::Dumper($param);
38 #debug(Data::Dumper::Dumper(\%ENV)."\n");
39 }
40
41 1 ▶ if (!defined $param->{-authdir}){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $param->{"-authdir"}) 0 1 1
42 0 carp(__PACKAGE__."::new() missing -authdir param to constructor or setting \$ENV{DOCUMENT_ROOT}");
43 0 return;
44 }
45
46 1 ▶ unless( $self->init($param) ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->init($param)) 0 1 1
47 0 warn( sprintf "%s\::init() failed, authdir [%s], userfile expected at:[%s]",__PACKAGE__,$param->{-authdir}, $param->{-authdir}.'/user.dat');
48 0 return undef;
49 }
50  
51 1 return $self;
52 }
53  
54  
55  
56 0 ▶sub authdir {
Subroutine authdir: not called CC=1 SCAR=6.9
Pod undocumented
57 0 my $self = shift;
58 0 return $self->{authdir};
59 }
60  
61 0 ▶sub userdat {
Subroutine userdat: not called CC=1 SCAR=6.9
Pod undocumented
62 0 my $self = shift;
63 0 return $self->{userdat};
64 }
65  
66 0 ▶sub sessdir {
Subroutine sessdir: not called CC=1 SCAR=6.9
Pod undocumented
67 0 my $self = shift;
68 0 return $self->{sessdir};
69 }
70  
71 0 ▶sub userfile {
Subroutine userfile: not called CC=1 SCAR=6.9
Pod undocumented
72 0 my $self = shift;
73 0 return $self->{userfile};
74 }
75  
76  
77 # override check so that we can do cookie thing
78 0 ▶sub check {
Subroutine check: not called CC=1 SCAR=6.9
Pod documented
79 0 my $self = shift;
80 0 $self->_pre_check;
81 0 $self->SUPER::check; # access overridden method
82 0 $self->_post_check;
83 0 return;
84 }
85  
86  
87  
88 # this runs before auth check
89 # RATIONALE: pre only tries to load an auth string (unless logout is detected)
90 0 ▶sub _pre_check {
Subroutine _pre_check: not called CC=4 SCAR=30.0
91 0 my $self = shift;
92  
93  
94  
95 # 1) first of all see if a prev sess_file id (filename really) can be gotten from cookie
96 0 ▶ my $sess_file = $self->_get_sess_file_from_cookie
Branch: 1 branch
BranchTrueFalseTotal
my $sess_file = $self->_get_sess_file_from_cookie or return 0 0 0
97 or # no sess_file on cooie? no harm done.. just return.
98 return;
99  
100  
101  
102  
103  
104 # 2) ok. so the cookie has a sess_file in it...
105 # TODO: had to mess with internals of CGI::Auth ( with $self->{sess_file} ) because that module
106 # does not provide for a set() type of method for the sess_file, it does accept as constructor
107 # but i'd rather leave the constructor to do what it does, which seems to be to assure that
108 # CGI::Auth finds its support files, user db, template, etc.
109
110 0 $self->{sess_file} = $sess_file; # <- had to mess with CGI::Auth internals here.
111 0 ▶ unless( $self->OpenSessionFile ){ # CGI::Auth::OpenSessionFile() checks with $CGI::Auth::OpenSessionFile::sess_file
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->OpenSessionFile) 0 0 0
112 # delete the cookie
113 0 ▶ $self->_ruin_cookie_and_redirect and exit(0);
Branch: 1 branch
BranchTrueFalseTotal
$self->_ruin_cookie_and_redirect and exit 0 0 0 0
114 }
115
116
117
118
119 # 3) cookie was found, sess_file was ok.. now pass it for CGI::Auth::check() to use later.
120 ### $sess_file
121 0 $self->{cgi}->param( -name=> $self->sfparam_name, -value=> $sess_file );
122
123 0 return 1;
124 }
125  
126  
127  
128 0 ▶sub _ruin_cookie_and_redirect {
Subroutine _ruin_cookie_and_redirect: not called CC=1 SCAR=6.9
129 0 my $self = shift;
130
131 print $self->get_cgi->redirect(
132 -uri => $self->{formaction},
133 0 -cookie =>
134 $self->get_cgi->cookie(
135 -name => $self->sfparam_name,
136 -value => '',
137 -expire => 'now'
138 )
139 );
140  
141 0 return 1;
142 }
143  
144 0 ▶sub _set_cookie_and_redirect {
Subroutine _set_cookie_and_redirect: not called CC=1 SCAR=6.9
145 0 my $self = shift;
146
147 print $self->get_cgi->redirect(
148 -uri => $self->{formaction},
149 0 -cookie =>
150 $self->get_cgi->cookie(
151 -name => $self->sfparam_name,
152 -value => $self->sfparam_value,
153 -expire => $self->get_cookie_expire_time
154 )
155 );
156  
157 0 return 1;
158 }
159  
160  
161  
162  
163  
164  
165  
166  
167 # post_check() only runs if user is successfully authenticated.
168 # its task is
169 # a) to assure a cookie is present.
170 # b) check for a logout for this already authenticated user
171 0 ▶sub _post_check {
Subroutine _post_check: not called CC=4 SCAR=30.0
172 0 my $self = shift;
173  
174 # 1) assure cookie is here
175 0 ▶ unless ( $self->_get_sess_file_from_cookie ) { # if no cookie
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_get_sess_file_from_cookie) 0 0 0
176 0 ▶ $self->_set_cookie_and_redirect() and exit(0);
Branch: 1 branch
BranchTrueFalseTotal
$self->_set_cookie_and_redirect and exit 0 0 0 0
177 }
178  
179 # 2) detect logout for authenticated user
180 # ok. so now we found cookie and sess_file id in it- did the user request a logout???
181
182 0 ▶ if ( $self->_requested_logout ) { # check if logout was requested.
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_requested_logout) 0 0 0
183 0 $self->logout; # logout will exit(0). we dont do it here because logout() method could be called directly.
184 };
185
186 0 return 1;
187 }
188  
189  
190  
191  
192  
193  
194 0 ▶sub logout {
Subroutine logout: not called CC=2 SCAR=17.9
Pod documented
195 0 my $self = shift;
196  
197 # delete auth session
198 0 $self->endsession;
199  
200 # ruin cookie and redirects back here
201 0 ▶ $self->_ruin_cookie_and_redirect and exit(0);
Branch: 1 branch
BranchTrueFalseTotal
$self->_ruin_cookie_and_redirect and exit 0 0 0 0
202 }
203  
204 # legacy
205 0 ▶sub run {
Subroutine run: not called CC=1 SCAR=6.9
Pod undocumented
206 0 my $self = shift;
207 0 $self->check;
208 }
209  
210  
211  
212  
213 # basic get and set methods. useful..
214 # these methods dont do anything major like exit or redirect etc
215  
216 0 ▶sub get_cgi {
Subroutine get_cgi: not called CC=1 SCAR=6.9
Pod documented
217 0 my $self = shift;
218 0 return $self->{cgi};
219 }
220  
221 0 ▶sub username {
Subroutine username: not called CC=2 SCAR=17.9
Pod documented
222 0 my $self = shift;
223 0 my ($username, undef) = $self->OpenSessionFile;
224 0 ▶ $username or return;
Branch: 1 branch
BranchTrueFalseTotal
$username or return 0 0 0
225 0 return $username;
226 }
227  
228  
229 0 ▶sub start_session {
Subroutine start_session: not called CC=1 SCAR=6.9
Pod undocumented
230 0 my $self = shift;
231 0 return $self->SUPER::start_session;
232 }
233  
234 0 ▶sub _get_sess_file_from_cookie {
Subroutine _get_sess_file_from_cookie: not called CC=2 SCAR=17.9
235 ## _load_cookie()
236 0 my $self = shift;
237 0 my $session_file = $self->get_cgi->cookie($self->sfparam_name);
238 0 ▶ $session_file or return;
Branch: 1 branch
BranchTrueFalseTotal
$session_file or return 0 0 0
239 0 return $session_file;
240 }
241  
242 0 ▶sub _requested_logout {
Subroutine _requested_logout: not called CC=8 SCAR=42.8
243 0 my $self= shift;
244  
245 # does the query string look like we are trying to log out?
246  
247  
248 # for cgi application:
249
250 0 ▶ if ($CGI::Auth::Auto::CGI_APP_COMPATIBLE){
Branch: 1 branch
BranchTrueFalseTotal
if ($CGI::Auth::Auto::CGI_APP_COMPATIBLE) 0 0 0
251 0 my($param,$runmode) = split(/\=/, $CGI::Auth::Auto::CGI_APP_COMPATIBLE );
252
253 0 ▶ if ( defined $self->get_cgi->param($param) and $self->get_cgi->param($param) eq $runmode ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->get_cgi->param($param) and $self->get_cgi->param($param) eq $runmode) 0 0 0
Condition0%0/3
defined $self->get_cgi->param($param) and $self->get_cgi->param($param) eq $runmode
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->get_cgi->param($param) and $self->get_cgi->param($param) eq $runmode
defined $self->get_cgi->param($param) $self->get_cgi->param($param) eq $runmode
Truth table0%0/3
A and B
A= defined $self->get_cgi->param($param)B= $self->get_cgi->param($param) eq $runmode
ABresult
0X0
100
111
254 0 debug("detected $CGI::Auth::Auto::CGI_APP_COMPATIBLE\n");
255 0 return 1;
256 }
257 }
258  
259 0 ▶ if ( defined $ENV{QUERY_STRING} ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{"QUERY_STRING"}) 0 0 0
260 0 debug("\$ENV{QUERY_STRING} $ENV{QUERY_STRING}\n");
261 0 ▶ return 1 if $ENV{QUERY_STRING} eq 'logout';
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"QUERY_STRING"} eq "logout" 0 0 0
262 }
263
264 0 my $paramname = $self->get_logout_param_name;
265 0 my $paramval = $self->get_cgi->param($self->get_logout_param_name);
266 0 ▶ debug( sprintf " param name: $paramname [$paramval:%s]", ( defined $paramval ? 1 : 0 ));
Branch: 1 branch
BranchTrueFalseTotal
defined $paramval ? : 0 0 0
267  
268 0 ▶ defined $paramval or return 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $paramval or return 0 0 0 0
269 0 return 1;
270 }
271  
272 0 ▶sub set_cookie_expire_time {
Subroutine set_cookie_expire_time: not called CC=2 SCAR=17.9
Pod documented
273 0 my $self= shift;
274 0 ▶ my $val = shift; $val or croak("must have valid arg to set_cookie_expire()");
Branch: 1 branch
BranchTrueFalseTotal
$val or croak("must have valid arg to set_cookie_expire()") 0 0 0
275 0 $self->{cookie_expire_time}= $val;
276 0 return $self->{cookie_expire_time};
277 }
278  
279 0 ▶sub get_cookie_expire_time {
Subroutine get_cookie_expire_time: not called CC=2 SCAR=17.9
Pod documented
280 0 my $self= shift;
281 0 ▶ $self->{cookie_expire_time} ||= '+1h';
Condition0%0/2
$self->{"cookie_expire_time"} ||= "+1h"
l!l
00
MC/DC0%0/1
$self->{"cookie_expire_time"} ||= "+1h"
$self->{"cookie_expire_time"}
Truth table0%0/2
A
A= $self->{"cookie_expire_time"}
Aresult
11
00
282 0 return $self->{cookie_expire_time};
283 }
284  
285 0 ▶sub get_logout_param_name {
Subroutine get_logout_param_name: not called CC=2 SCAR=17.9
Pod documented
286 0 my $self = shift;
287 0 ▶ $self->{logout_param_name} ||= 'logout';
Condition0%0/2
$self->{"logout_param_name"} ||= "logout"
l!l
00
MC/DC0%0/1
$self->{"logout_param_name"} ||= "logout"
$self->{"logout_param_name"}
Truth table0%0/2
A
A= $self->{"logout_param_name"}
Aresult
11
00
288 0 return $self->{logout_param_name};
289 }
290  
291 0 ▶sub set_logout_param_name {
Subroutine set_logout_param_name: not called CC=2 SCAR=17.9
Pod documented
292 0 my $self = shift;
293 0 ▶ my $val = shift; $val or croak("must have arg to set_logout_param_name()");
Branch: 1 branch
BranchTrueFalseTotal
$val or croak("must have arg to set_logout_param_name()") 0 0 0
294 0 $self->{logout_param_name} = $val;
295 0 return $self->{logout_param_name};
296 }
297  
298  
299  
300  
301  
302 # GUESSING SUBS
303  
304  
305 1 ▶sub _guess_authdir {
Subroutine _guess_authdir: called CC=1 SCAR=0.0
306 1 my $dir = __guess_base().'/auth';
307 1 debug("$dir\n");
308 1 return $dir;
309 }
310  
311 1 ▶sub __guess_base {
Subroutine __guess_base: called CC=3 SCAR=13.3
312 1 my $cgibin = CGI::Scriptpaths::abs_cgibin();
313  
314 1 ▶ unless(defined $cgibin){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cgibin) 0 1 1
315 0 ▶ $cgibin = script_abs_loc() or confess("cant get script's absolute location");
Branch: 1 branch
BranchTrueFalseTotal
$cgibin = script_abs_loc() or confess("cant get script's absolute location") 0 0 0
316 }
317 1 debug($cgibin);
318 1 return $cgibin;
319 }
320  
321 0 ▶sub _guess_sessdir {
Subroutine _guess_sessdir: not called CC=1 SCAR=6.9
322 0 my $dir = __guess_authdir().'/sess';
323 0 debug("$dir\n");
324 0 return $dir;
325 }
326  
327  
328 1 1;
329  
330  
331 __END__
↑ Summary