Portal/Sessions.pm

filter: Statementstmt 17.4% 11 / 63 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 27.2% 3 / 11 Podpod 0.0% 0 / 8 totaltotal 12.6% 14 / 111 CC 12 SCAR 31.6
CC
12
Cov
13%
authenticate_user
56.0
getcookie
20.0
start_session
12.0
CRAP 23.7
« Portal/Controls/register.pm ↑ Summary Portal/Controls/changepw.pm »
1 package CGI::Portal::Sessions;
2 # Copyright (c) 2008 Alexander David P. All rights reserved.
3 #
4 # Authentication and Session class
5  
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 $VERSION = "0.12";
13  
14 1 1;
15  
16 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
17 0 my ($class, $i) = @_;
18 0 bless $i, $class;
19 0 return $i;
20 }
21  
22 # Verify password or session
23 0 ▶sub authenticate_user {
Subroutine authenticate_user: not called CC=7 SCAR=40.3
Pod undocumented
24 0 my $self = shift;
25  
26 # User is logging in
27 0 ▶ if ($self->{'in'}{'user'} && $self->{'in'}{'password'}){
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'in'}{'user'} and $self->{'in'}{'password'}) { } 0 0 0
elsif (my $sid = getcookie('sid')) { } 0 0 0
Condition0%0/3
$self->{"in"}{"user"} and $self->{"in"}{"password"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"in"}{"user"} and $self->{"in"}{"password"}
$self->{"in"}{"user"} $self->{"in"}{"password"}
Truth table0%0/3
A and B
A= $self->{"in"}{"user"}B= $self->{"in"}{"password"}
ABresult
0X0
100
111
28  
29 # Get users stored password hash
30 0 my $users = $self->{'rdb'}->exec("select $self->{'conf'}{'user_user_field'},$self->{'conf'}{'user_passw_field'} from $self->{'conf'}{'user_table'} where $self->{'conf'}{'user_user_field'} like " . $self->{'rdb'}->escape($self->{'in'}{'user'}))->fetch;
31  
32 # Compare password hashes
33 0 ▶ if (md5_hex($self->{'in'}{'password'}) eq $users->[1]){
Branch: 1 branch
BranchTrueFalseTotal
if (md5_hex($self->{"in"}{"password"}) eq $users->[1]) 0 0 0
34  
35 # Assign user to object
36 0 $self->{'user'} = $users->[0];
37  
38 # Start session
39 0 $self->start_session($users->[0]);
40  
41 # Clean sessions
42 0 $self->clean_sessions();
43 0 return;
44 }
45 }elsif (my $sid = getcookie('sid')){
46  
47 # Session expiration
48 0 my $exps = time() - $self->{'conf'}{'session_length'};
49  
50 # Get session start
51 0 my $sessions = $self->{'rdb'}->exec("select $self->{'conf'}{'session_user_field'},$self->{'conf'}{'session_start_field'} from $self->{'conf'}{'session_table'} where $self->{'conf'}{'session_sid_field'}=" . $self->{'rdb'}->escape($sid))->fetch;
52  
53 # Session not expired
54 0 ▶ if ($sessions->[0] && $sessions->[1] >= $exps){
Branch: 1 branch
BranchTrueFalseTotal
if ($sessions->[0] and $sessions->[1] >= $exps) 0 0 0
Condition0%0/3
$sessions->[0] and $sessions->[1] >= $exps
!ll&&!rl&&r
000
MC/DC0%0/2
$sessions->[0] and $sessions->[1] >= $exps
$sessions->[0] $sessions->[1] >= $exps
Truth table0%0/3
A and B
A= $sessions->[0]B= $sessions->[1] >= $exps
ABresult
0X0
100
111
55  
56 # Assign user to object
57 0 $self->{'user'} = $sessions->[0];
58  
59 # Renew session
60 0 $self->renew_session($self->{'user'});
61 0 return;
62 }
63 }
64  
65 # Assign tmpl
66 0 $self->assign_tmpl("Sessions.html");
67 }
68  
69 # Create a session
70 0 ▶sub start_session {
Subroutine start_session: not called CC=3 SCAR=24.8
Pod undocumented
71 0 my ($self, $user) = @_;
72 0 my $current_time = time();
73  
74 # Generate a session id
75 0 my $sid;
76 my $cids;
77 0 ▶ while ($cids->[0] || ! $sid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cids->[0] or not $sid) 0 0 0
Condition0%0/3
$cids->[0] or not $sid
l!l&&r!l&&!r
000
MC/DC0%0/2
$cids->[0] or not $sid
$cids->[0] not $sid
Truth table0%0/3
A or not(B)
A= $cids->[0]B= not $sid
ABresult
1X1
011
000
78 0 $sid = md5_hex($$ , time() , rand(8888) );
79 0 $cids = $self->{'rdb'}->exec("select $self->{'conf'}{'session_index_field'} from $self->{'conf'}{'session_table'} where $self->{'conf'}{'session_sid_field'} = $sid limit 1")->fetch;
80 }
81  
82 # Get current session index
83 0 my $cc = $self->{'rdb'}->exec("select $self->{'conf'}{'session_index_field'} from $self->{'conf'}{'session_table'} order by $self->{'conf'}{'session_index_field'} desc limit 1")->fetch;
84 0 my $c = $cc->[0]+1;
85  
86 # Insert session and prepare cookie
87 0 $self->{'rdb'}->exec("insert into $self->{'conf'}{'session_table'} ($self->{'conf'}{'session_index_field'},$self->{'conf'}{'session_sid_field'},$self->{'conf'}{'session_user_field'},$self->{'conf'}{'session_start_field'}) values (" . $self->{'rdb'}->escape($c, $sid, $user, $current_time) . ")");
88 0 $self->{'cookies'} .= "Set-Cookie: sid=$sid; path=/\n";
89 }
90  
91 # Update session start
92 0 ▶sub renew_session {
Subroutine renew_session: not called CC=1 SCAR=6.9
Pod undocumented
93 0 my $self = shift;
94 0 my $sid = getcookie('sid');
95 0 my $current_time = time();
96 0 $self->{'rdb'}->exec("update $self->{'conf'}{'session_table'} set $self->{'conf'}{'session_start_field'}=$current_time where $self->{'conf'}{'session_sid_field'}=" . $self->{'rdb'}->escape($sid));
97 }
98  
99 # Remove session
100 0 ▶sub logoff {
Subroutine logoff: not called CC=1 SCAR=6.9
Pod undocumented
101 0 my $self = shift;
102 0 my $sid = getcookie('sid');
103 0 $self->{'rdb'}->exec("delete from $self->{'conf'}{'session_table'} where $self->{'conf'}{'session_sid_field'}=" . $self->{'rdb'}->escape($sid));
104 0 $self->{'user'} = "";
105 }
106  
107 # Remove expired sessions
108 0 ▶sub clean_sessions {
Subroutine clean_sessions: not called CC=1 SCAR=6.9
Pod undocumented
109 0 my $self = shift;
110 0 my $exps = time() - $self->{'conf'}{'session_length'};
111 0 $self->{'rdb'}->exec("delete from $self->{'conf'}{'session_table'} where $self->{'conf'}{'session_start_field'} < $exps");
112 }
113  
114 # Assign template output to object out
115 0 ▶sub assign_tmpl {
Subroutine assign_tmpl: not called CC=1 SCAR=6.9
Pod undocumented
116 0 my ($self, $i) = @_;
117  
118 # Read template
119 0 my $template = HTML::Template->new(die_on_bad_params => 0, filename => "$self->{'conf'}{'template_dir'}$i");
120 0 $self->{'tmpl_vars'}{'SCRIPT_NAME'} = $ENV{'SCRIPT_NAME'};
121  
122 # Assign vars to template
123 0 $template->param(%{$self->{'tmpl_vars'}});
124  
125 # Assign template output to object out
126 0 $self->{'out'} = $template->output;
127 }
128  
129 0 ▶sub getcookie {
Subroutine getcookie: not called CC=4 SCAR=30.0
Pod undocumented
130 0 my $cookiename = shift;
131 0 my $cookie;
132 my $value;
133 0 ▶ if ($ENV{'HTTP_COOKIE'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"HTTP_COOKIE"}) 0 0 0
134 0 foreach (split(/; /,$ENV{'HTTP_COOKIE'})) {
135 0 ($cookie,$value) = split(/=/);
136 0 ▶ if ($cookiename eq $cookie) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cookiename eq $cookie) 0 0 0
137 0 return $value;
138 }
139 }
140 }
141 }
« Portal/Controls/register.pm ↑ Summary Portal/Controls/changepw.pm »