blib/lib/Login.pm untested

filter: Statementstmt 0.0% 0 / 221 Branchbran 0.0% 0 / 110 Conditioncond 0.0% 0 / 39 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 412 CC 56 SCAR 55.9
CC
56
Cov
0%
login
506.0
check_user
272.0
get_access
156.0
CRAP 268.2
1 package CGI::AppBuilder::Login;
2  
3 use strict;
4 use vars qw(@ISA $VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS);
5 use Carp;
6 use CGI;
7  
8 $VERSION = 0.01;
9 @ISA = qw(Exporter);
10 @EXPORT = qw(login logout check_user get_access
11 check_timeout get_version);
12 @EXPORT_OK = qw(login logout check_user get_access
13 check_timeout get_version
14 );
15 %EXPORT_TAGS = (
16 all => [@EXPORT_OK],
17 pages => [qw(login)],
18 );
19  
20  
21 =head3 login($cgi, $ar, $tmout)
22  
23 Input variables:
24  
25 $cgi - CGI object
26 $ar - parameter array ref
27 $tmout - whether the session has been timed out
28  
29 Variables used or routines called:
30  
31 to_remember - to remember inputs
32 check_user - to check user logins
33 set_cookies - to set cookies
34  
35 How to use:
36  
37 my $q = new CGI;
38 $self->login($q, 'jsmith', 'jojo'); # login as jsmith
39 my %cfg = (usr=>'jsmith', pwd=>'jojo');
40 $self->login($q, \%cfg, 0); # login as jsmith
41  
42 Return: ($toc, $txt): the login screen HTML code
43  
44 =cut
45  
46 sub login {
47 my $s = shift;
48 my ($q, $ar, $tmout) = @_;
49 $tmout = 0 if ! defined $tmout;
50 $q = new CGI if !$q;
51
52 my ($usr, $pwd, $vbm, $i,$m1,$m2,$m3) = ("","","","","","","");
53 my ($toc, $txt,$tsg, $pid, $pin, $dob) = ("","","","","","");
54 $m1 = $ar->{msg}{'001'} if exists $ar->{msg}{'001'};
55 $m1 = 'Login Page' if !$m1;
56 $m2 = $ar->{msg}{'002'} if exists $ar->{msg}{'002'};
57 $m2 = 'User' if !$m2;
58 $m3 = $ar->{msg}{'003'} if exists $ar->{msg}{'003'};
59 $m3 = 'Password' if !$m3;
60 $vbm = $ar->{v} if exists $ar->{v};
61 $tsg = $ar->{table_noborder} if exists $ar->{table_noborder};
62  
63 my $cu_status = 0;
64 $cu_status = $s->check_user($q, $ar) if !$tmout;
65 $cu_status = 150 if $tmout;
66  
67 $toc .= $s->get_version($q, $ar, 'login');
68 $toc .= "<h2 align=center>$m1</h2>\n";
69
70 if ($cu_status>999) { # login ok, move to next page
71 $s->set_cookies($q, $ar);
72 return $s->info1($q, $ar);
73 }
74 $usr = $ar->{user_id} if exists $ar->{user_id};
75 $pwd = $ar->{user_pwd} if exists $ar->{user_pwd};
76 my $err_msg = "";
77 $err_msg = $ar->{msg}{$cu_status} if exists $ar->{msg}{$cu_status};
78 if ($usr && $pwd) { # XXX:
79 $txt = "<b>User ($usr) has successfully logged in</b>";
80 print "$toc $txt";
81 return ($toc, $txt, "");
82 }
83
84 my $uid = 'user_id';
85  
86 $usr = ${$ar}{$uid} if exists ${$ar}{$uid};
87 my $stm = time; # session start time
88 my $fmt = "<INPUT TYPE=hidden NAME=%s VALUE=%s>\n";
89 my $td1 = "<td colspan=2>";
90 $txt = "";
91 $txt .= "<center>\n<table>\n";
92 $txt .= $q->start_form(${$ar}{method},${$ar}{action},${$ar}{encoding});
93 $txt .= sprintf $fmt, 'a', 'l';
94 $txt .= sprintf $fmt, 'stm', $stm;
95 $txt .= sprintf $fmt, 'v', 'y' if $vbm;
96 $txt .= sprintf $fmt, "task", "login";
97  
98 $usr =~ s/\%40/\@/g;
99 $txt .= $s->to_remember($q, $ar, "$uid|pwd|stm|sid|task")
100 if exists ${$ar}{to_remember} && ${$ar}{to_remember};
101 if ($ENV{SCRIPT_NAME} =~ /_prod/) {
102 $txt .= "<tr><td colspan=3>${$ar}{msg}{'024'}</td></tr>";
103 } else {
104 $txt .= "<tr><td colspan=3>${$ar}{msg}{'023'}</td></tr>";
105 }
106
107 $txt .= "<tr>$td1<b>$ar->{msg}{'020'}</b><br><br></td>\n</tr>\n";
108 $txt .= "<tr><td>$m2: \n<td>";
109 $txt .= $q->textfield("$uid",$usr,50,80) . "\n";
110 $txt .= "</tr>\n<tr><td>$m3: \n<td>";
111 $txt .= $q->password_field('pwd','',50,80) . "\n";
112 $txt .= "<tr><td colspan=2 align=center>";
113 $txt .= $q->submit('submit', 'Go');
114 $txt .= $q->end_form() . "</tr>\n";
115 $txt .= "<tr>$td1<br><p class=err>$err_msg</p></td>\n</tr>\n";
116 $txt .= "</table>\n</center>\n";
117 print "$toc$txt";
118 return ($toc, $txt, "");
119 }
120  
121  
122 sub logout {
123 my $s = shift;
124 my ($q, $ar, $tmout) = @_;
125
126 print "<b>You have been logged out.</b>";
127  
128 }
129  
130  
131 =head3 get_version ($cgi, $ar, $sub)
132  
133 Input variables:
134  
135 $cgi - CGI object
136 $ar - Array ref containing all the parameters
137 $sub - sub procedure name.
138 display user first name if it is 'login'
139  
140 Variables used or routines called:
141  
142 None.
143  
144 How to use:
145  
146 my $q = new CGI;
147 my %cfg = (usr=>'jsmith', pwd=>'jojo');
148 my @names = $q->param;
149 foreach my $k (@names) { $cfg{$k} = $q->param($k) if ! exists $cfg{$k}; }
150 $self->get_version($q, \%cfg);
151  
152 Return: $t - HTML code
153  
154 This method forms HTML code to show demorgraphic information about the
155 subject.
156  
157 =cut
158  
159 sub get_version {
160 my $s = shift;
161 my ($q, $ar, $sub) = @_;
162 $sub = "" if ! $sub;
163
164 my $toc = "";
165 if ($sub =~ /login/) {
166 $toc = "Welcome $ar->{usr_fn}!<br>"
167 if exists $ar->{usr_fn} && $ar->{usr_fn};
168 }
169 $toc .= "[version $ar->{app_version}]" if exists $ar->{app_version};
170 if ($ENV{SCRIPT_NAME} =~ /_prod/) {
171 $toc .= " - production<br>\n";
172 } else {
173 $toc .= " - training<br>\n";
174 }
175 return $toc;
176 }
177  
178  
179 =head3 check_timeout($cgi, $ar)
180  
181 Input variables:
182  
183 $cgi - CGI object
184 $ar - Array ref containing all the parameters
185  
186 Variables used or routines called:
187  
188 None.
189  
190 How to use:
191  
192 my $q = new CGI;
193 my %cfg = (usr=>'jsmith', pwd=>'jojo');
194 my @names = $q->param;
195 foreach my $k (@names) { $cfg{$k} = $q->param($k) if ! exists $cfg{$k}; }
196 $self->check_timeout($q, \%cfg);
197  
198 Return: 1 or 0: 1 - timed out; 0 - not timed out
199  
200 This method checks to see if the session has been timed out.
201 The default time out is 20 minutes.
202  
203 =cut
204  
205 sub check_timeout {
206 my $s = shift;
207 my ($q, $ar) = @_;
208 my ($cdt,$stm, $tmout) = (time, 0, 0);
209 $stm = $ar->{stm} if exists $ar->{stm};
210 $tmout = $ar->{session_timeout} if exists $ar->{session_timeout};
211 return ($tmout && ($cdt-$stm)>$tmout)?1:0;
212 }
213  
214  
215 =head3 get_access ($cgi,$ar)
216  
217 Input variables:
218  
219 $cgi - CGI object
220 $ar - Array ref containing all the parameters
221  
222 Variables used or routines called:
223  
224 None
225  
226 How to use:
227  
228 my $q = new CGI;
229 my %cfg = (usr=>'jsmith', pwd=>'jojo');
230 my @names = $q->param;
231 foreach my $k (@names) { $cfg{$k} = $q->param($k) if ! exists $cfg{$k}; }
232 $self->get_access($q, \%cfg);
233  
234 Return: $hr - access hash array ref: ${$hr}{uid|gid}{$name} = $uid|$giu
235  
236 This method retrieves portal access user and group files and access_users and
237 access_groups in the configuration file to build an access list.
238  
239 =cut
240  
241 sub get_access {
242 my $s = shift;
243 my ($q, $ar) = @_;
244
245 require XML::Simple;
246  
247 my $ua_list = lc ${$ar}{access_users};
248 $ua_list =~ s/\s*,\s*/\|/g;
249 my $ga_list = lc ${$ar}{access_groups};
250 $ga_list =~ s/\s*,\s*/\|/g;
251  
252 # get portal xml access files
253 my $xml_grp = ${$ar}{xml_group};
254 my $xml_usr = ${$ar}{xml_user};
255
256 my $xs = new XML::Simple;
257 my $grf = $xs->XMLin($xml_grp);
258 my $urf = $xs->XMLin($xml_usr);
259
260 my %uid = (); # User ID hash array
261 my %gid = (); # Group ID hash array
262 my %lku = (); # User and group ID lookup
263 foreach my $hr (@{${$urf}{user}}) {
264 my $i = ${$hr}{uid};
265 my $e = lc ${$hr}{email};
266 next if $e !~ /^($ua_list)/i;
267 foreach my $k (keys %{$hr}) {
268 next if $k =~ /^(uid)$/i;
269 $uid{"$i"}{$k} = ${$hr}{$k};
270 $lku{uid}{$e} = $i;
271 }
272 }
273  
274 ${$ar}{_uid} = \%uid;
275 $s->disp_param(${$ar}{_uid}) if ${$ar}{v};
276 foreach my $hr (@{${$grf}{group}}) {
277 my $i = ${$hr}{gid};
278 my $e = lc ${$hr}{groupname};
279 next if $e !~ /^($ga_list)/i;
280 foreach my $k (keys %{$hr}) {
281 next if $k =~ /^gid/i;
282 $gid{$i}{$k} = ${$hr}{$k};
283 $lku{gid}{$e} = $i;
284 }
285 }
286 ${$ar}{_gid} = \%gid;
287 $s->disp_param(${$ar}{_gid}) if ${$ar}{v};
288 # build a access list
289 my %ac = ();
290
291 foreach my $k (split /\|/, $ua_list) {
292 $ac{uid}{$k} = (exists $lku{uid}{$k})?$lku{uid}{$k}:"";
293 print "WARN: User $k does not exist.\n" if !$ac{uid}{$k} && ${$ar}{v};
294 }
295 foreach my $k (split /\|/, $ga_list) {
296 $ac{gid}{$k} = (exists $lku{gid}{$k})?$lku{gid}{$k}:"";
297 print "WARN: Group $k does not exist.\n" if !$ac{gid}{$k} && ${$ar}{v};
298 }
299 $s->disp_param(\%ac) if ${$ar}{v};
300 return \%ac;
301 }
302  
303 =head3 check_user ($cgi,$ar)
304  
305 Input variables:
306  
307 $cgi - CGI object
308 $ar - Array ref containing all the parameters
309  
310 Variables used or routines called:
311  
312 disp_param - display parameters
313 get_cookies - get cookies
314 get_access - get access information
315  
316 How to use:
317  
318 my $q = new CGI;
319 my %cfg = (usr=>'jsmith', pwd=>'jojo');
320 my @names = $q->param;
321 foreach my $k (@names) { $cfg{$k} = $q->param($k) if ! exists $cfg{$k}; }
322 $self->check_user($q, \%cfg);
323  
324 Return: $n - status code
325  
326 0 - no user name from input nor from cookie
327 1 - user name does not exists
328 2 - user does not belong to any group which has granted access
329 >9 - user has access to the application
330  
331 A successful user authentication includes:
332 1) the user has to be a valid web portal user;
333 2) user's password matches
334 3) user has to be a authorized user or in an authorized group to use this
335 application. The autorization parameters are access_users and access_groups
336 in the configuration file.
337  
338 =cut
339  
340 sub check_user {
341 my $s = shift;
342 my ($q, $ar) = @_;
343
344 my ($usr, $pwd) = ("","");
345 $usr = $ar->{user_id} if exists $ar->{user_id};
346 $ar->{user_pwd} = "" if !$usr && exists $ar->{user_pwd};
347 $pwd = $ar->{user_pwd} if exists $ar->{user_pwd};
348 return 160 if !$pwd && $usr;
349  
350 # No user and we try to retrieve it from cookies
351 my $ck1 = 'ckUID'; # User id
352 my $ck2 = 'ckPWD'; # User password
353 my $cr = $s->get_cookies($q, $ar);
354 return 151 if !$usr
355 && !exists $cr->{$ck1}{$ck1} && !exists $cr->{$ck2}{$ck2};
356
357 my ($svr,$obj);
358 $usr = $cr->{$ck1}{$ck1} if !$usr && exists $cr->{$ck1}{$ck1};
359  
360 my ($uid, $obj_u, $s_pwd, $usr_fn, $usr_ln) = ("","","","","");
361 $ar->{user_id} = $usr if $usr;
362 return 0 if !$pwd && !$usr;
363 return 151 if !$usr; # no user name from input nor from cookie
364 return 160 if !$pwd && $usr;
365  
366 # we have user_id and password so far
367 $usr =~ s/\%40/\@/g;
368 # return 152 if !$uid;
369  
370 return 153 if $pwd ne $s_pwd;
371
372 # So far, we have authenticated the user.
373 # We need to authorize the user
374 my $ua_list = ${$ar}{access_users};
375 $ua_list =~ s/\s*,\s*/\|/g;
376 my $ga_list = ${$ar}{access_groups};
377 $ga_list =~ s/\s*,\s*/\|/g;
378 my %ac = (); # access list
379 foreach my $k (split /\|/, $ua_list) {
380 my $u1 = $obj->GetUserID($k); # get user id
381 next if ! $u1; # did not find the user
382 my $o1 = $obj->GetUserObj($u1); # get user object
383 $ac{uid}{"$u1"} = $k; # user id and name
384 }
385 foreach my $k (split /\|/, $ga_list) {
386 my $g1 = $obj->GetGroupID($k); # get group id
387 next if ! $g1; # did not find the group
388 my $o1 = $obj->GetGroupObj($g1); # get group object
389 $ac{gid}{"$g1"} = $k; # group id and name
390 }
391 return 1000 if exists $ac{uid}{"$uid"};
392  
393 foreach my $g (split /,/, ${$obj_u}{GroupsList}) {
394 my $go = $obj->GetGroupObj($g);
395 my $gi = ${$go}{GroupID};
396 return 1001 if exists $ac{gid}{"$gi"};
397 }
398 return 154; # did not find in the group access
399 }
400  
401 1;