blib/lib/WWW/Authenticate.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 ###############################################################################
2 # Perl Module WWW::Authenticate
3 #
4 # copyright 2001 D. Scott Barninger <barninger@fairfieldcomputers.com>
5 # copyright 2001 Chris Fleizach <chris@fightliteracy.com>
6 # licensed under the GNU General Public License ver. 2.0
7 # see the accompaning LICENSE file
8 ###############################################################################
9  
10 package WWW::Authenticate;
11  
12 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 BEGIN {
15 1 ▶ use vars qw($VERSION @ISA @EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 0 ▶ use DBI;
Subroutine BEGIN: called CC=1 SCAR=2.6
17  
18 require Exporter;
19  
20 @ISA = qw(Exporter);
21  
22 # exported functions
23 @EXPORT = qw(
24 &CheckAuth
25 &GetSessionCookie
26 &Login
27 &Logout);
28  
29 $VERSION = '0.6.0';
30 }
31  
32 # package globals
33 use vars qw($dbh);
34 $dbh = "";
35  
36  
37 ################################################################################
38 # FUNCTION: CheckAuth($dsn,$sql_username,$sql_password,$sql_table,$session)
39 # DESCRIPTION: authenticates the user using current session number
40 ################################################################################
41 sub CheckAuth
42 {
43 my ($dsn,$sql_username,$sql_password,$sql_table,$session) = @_;
44 my $SQL = qq| select session from $sql_table where session = "$session" |;
45 $dbh = Connect($dsn,$sql_username,$sql_password);
46 my $sth = DatabaseQuery($dbh,$SQL);
47 my ($t_session) = $sth->fetchrow_array();
48 $dbh->disconnect();
49  
50 if (!$t_session) {
51 return 0;
52 }
53 else {
54 return 1;
55 }
56 }
57  
58  
59 ################################################################################
60 # FUNCTION: Cleanup
61 # DESCRIPTION: To be called when exiting
62 ################################################################################
63 sub Cleanup
64 {
65 $dbh->disconnect();
66 exit(0);
67 }
68  
69 ################################################################################
70 # FUNCTION: Connect($dsn,$sql_username,$sql_password)
71 # DESCRIPTION: Connect to the MySQL database
72 ################################################################################
73 sub Connect
74 {
75 my ($dsn,$sql_username,$sql_password) = @_;
76 $dbh = DBI->connect($dsn,$sql_username,$sql_password)
77 or ErrorMessage("Could not connect to the database.");
78 return $dbh;
79 }
80  
81 ################################################################################
82 # FUNCTION: CreateSession
83 # DESCRIPTION: gets info in cookies
84 ################################################################################
85 sub CreateSession
86 {
87 my $size=int(rand(256)+255);
88 my $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
89 my @array= split(//,$chars);
90 my @dots = ();
91 while($size--){
92 push @dots,$array[rand(@array)];
93 }
94  
95 my $line = "";
96 foreach (@dots) {
97 if (int(rand(time^$$)) % 2 == 0) {
98 $line .= $_;
99 }
100 }
101  
102 my $add = "";
103 for (my $k = 0; $k < 40; $k++) {
104 my $n1 = int(rand(length($line)));
105 my $sub = substr($line,$n1,1);
106 $add .= "$sub$n1";
107 }
108  
109 my $session = int(rand(99999999)) . crypt(time*rand(999),"YT") . $add .
110 crypt(rand(730300334) * rand(31443),"EP");
111 return $session;
112 }
113 ################################################################################
114 # FUNCTION: DatabaseQuery($dbh,$SQL)
115 # DESCRIPTION: Allows for a query to the database, for a general query
116 # If they didn't send a where clause, we have to ignore it
117 # Returns a sth handle so that the user can define what the sth
118 # fetchrow_array should return
119 ################################################################################
120 sub DatabaseQuery
121 {
122 my ($dbh,$SQL) = @_;
123  
124 my $sth = $dbh->prepare($SQL) || ErrorMessage($SQL);
125 $sth->execute() || ErrorMessage($SQL);
126 return $sth;
127 }
128  
129 ################################################################################
130 # FUNCTION: ErrorMessage
131 # DESCRIPTION: When something fails should print the error message that is
132 # passed and we also output to a log. Then we call Cleanup and
133 # exit the program
134 ################################################################################
135 sub ErrorMessage
136 {
137 my $message = shift;
138 print qq|$message\n$DBI::err ($DBI::errstr)|;
139 Cleanup();
140 }
141  
142 ################################################################################
143 # FUNCTION: GetSessionCookie
144 # DESCRIPTION: gets info in cookies
145 ################################################################################
146 sub GetSessionCookie
147 {
148 use CGI qw/:standard/;
149 use CGI::Cookie;
150 # fetch existing cookies
151 my %cookies = fetch CGI::Cookie;
152 my ($session,$id);
153 if ($cookies{'session'}) { $session = $cookies{'session'}->value; }
154 if ($cookies{'id'}) { $id = $cookies{'id'}->value; }
155 return ($session,$id);
156 }
157  
158 ################################################################################
159 # FUNCTION: Login($dsn,$sql_username,$sql_password,$sql_table,$username,$password)
160 # DESCRIPTION: The user will log in, sending the username and password
161 ################################################################################
162 sub Login
163 {
164 my ($dsn,$sql_username,$sql_password,$sql_table,$username,$password) = @_;
165 my $SQL = qq| select id from $sql_table where user_name = "$username" and
166 password = "$password" |;
167 $dbh = Connect($dsn,$sql_username,$sql_password);
168 my $sth = DatabaseQuery($dbh,$SQL);
169 my ($uid) = $sth->fetchrow_array();
170  
171 # if we pass this condition, the user has logged in, so retrieve information
172 if (!$uid) {
173 $dbh->disconnect();
174 return 0;
175 }
176 else {
177 $dbh->disconnect();
178 LoginTheUser($dsn,$sql_username,$sql_password,$sql_table,$uid);
179 }
180 }
181  
182 ################################################################################
183 # FUNCTION: LoginTheUser
184 # DESCRIPTION: process screen
185 ################################################################################
186 sub LoginTheUser
187 {
188 my ($dsn,$sql_username,$sql_password,$sql_table,$uid) = @_;
189 my $session = CreateSession();
190 $dbh = Connect($dsn,$sql_username,$sql_password);
191 my $SQL = qq| update $sql_table set session="$session" where id = "$uid" |;
192 my $sth = DatabaseQuery($dbh,$SQL);
193 SetSessionCookie($session,$uid);
194 $dbh->disconnect();
195 return 1;
196 }
197  
198 ################################################################################
199 # FUNCTION: Logout($dsn,$sql_username,$sql_password,$sql_table,$session,$uid)
200 # DESCRIPTION: The user will be logged out deleting the session value in the DB
201 ################################################################################
202 sub Logout
203 {
204 my ($dsn,$sql_username,$sql_password,$sql_table,$session,$uid) = @_;
205 my $SQL = qq| SELECT session FROM $sql_table WHERE session = "$session" AND
206 id = "$uid" |;
207 $dbh = Connect($dsn,$sql_username,$sql_password);
208 my $sth = DatabaseQuery($dbh,$SQL);
209 my ($db_session) = $sth->fetchrow_array();
210 if (!$db_session) {
211 $dbh->disconnect();
212 return 0;
213 }
214 else {
215 my $SQL = qq| update $sql_table set session="NULL" where session = "$session"
216 and id = "$uid" |;
217  
218 my $sth = DatabaseQuery($dbh,$SQL);
219 $dbh->disconnect();
220 }
221 }
222  
223 ################################################################################
224 # FUNCTION: SetSessionCookie
225 # DESCRIPTION: sets session of admin with a cookie
226 ################################################################################
227 sub SetSessionCookie
228 {
229 my ($session,$id) = @_;
230 use CGI qw/:standard/;
231 use CGI::Cookie;
232 my $cookie1 = new CGI::Cookie(-name=>'session',-value=>$session);
233 my $cookie2 = new CGI::Cookie(-name=>'id',-value=>$id);
234  
235 print header(-Cookie=>[$cookie1,$cookie2],-type=>"text/html");
236 }
237  
238 1;
239 __END__
↑ Summary