Beginning of authentication api. Applied to shelves.pl for now as a test case.
[koha.git] / C4 / Auth.pm
1 package C4::Auth;
2
3 use strict;
4 require Exporter;
5 use C4::Database;
6
7 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
8
9 # set the version for version checking
10 $VERSION = 0.01;
11
12 @ISA = qw(Exporter);
13 @EXPORT = qw(
14              &checkauth
15 );
16
17
18
19 sub checkauth {
20     my $query=shift;
21     my $sessionID=$query->cookie('sessionID');
22     my $message='';
23     warn "SID: ".$sessionID;
24
25     my $dbh=C4Connect();
26     my $sth=$dbh->prepare("select userid,ip,lasttime from sessions where sessionid=?");
27     $sth->execute($sessionID);
28     if ($sth->rows) {
29         my ($userid, $ip, $lasttime) = $sth->fetchrow;
30         if ($lasttime<time()-20) {
31             # timed logout
32             warn "$sessionID logged out due to inactivity.";
33             $message="You have been logged out due to inactivity.";
34             my $sti=$dbh->prepare("delete from sessions where sessionID=?");
35             $sti->execute($sessionID);
36         } elsif ($ip ne $ENV{'REMOTE_ADDR'}) {
37             # Different ip than originally logged in from
38             warn "$sessionID came from a new ip address.";
39             $message="ERROR ERROR ERROR ERROR<br>Attempt to re-use a cookie from a different ip address.";
40         } else {
41             my $cookie=$query->cookie(-name => 'sessionID',
42                                       -value => $sessionID,
43                                       -expires => '+1y');
44             warn "$sessionID had a valid cookie.";
45             my $sti=$dbh->prepare("update sessions set lasttime=? where sessionID=?");
46             $sti->execute(time(), $sessionID);
47             return ($userid, $cookie, $sessionID);
48         }
49     }
50
51
52
53     warn "$sessionID wasn't in sessions table.";
54     
55     ($sessionID) || ($sessionID=int(rand()*100000).'-'.time());
56     my $userid=$query->param('userid');
57     my $password=$query->param('password');
58     if ($userid eq 'librarian' && $password eq 'koha') {
59         my $sti=$dbh->prepare("insert into sessions (sessionID, userid, ip,lasttime) values (?, ?, ?, ?)");
60         $sti->execute($sessionID, $userid, $ENV{'REMOTE_ADDR'}, time());
61         return ($userid, $sessionID, $sessionID);
62     } else {
63         if ($userid) {
64             $message="Invalid userid or password entered.";
65         }
66         my $parameters;
67         foreach (param $query) {
68             $parameters->{$_}=$query->{$_};
69         }
70         my $cookie=$query->cookie(-name => 'sessionID',
71                                   -value => $sessionID,
72                                   -expires => '+1y');
73         print $query->header(-cookie=>$cookie);
74         print qq|
75 <html>
76 <body background=/images/kohaback.jpg>
77 <center>
78 <h2>$message</h2>
79 <form method=post>
80 <table border=1>
81 <tr><th colspan=2><font size=+2>Koha Login</font></th></tr>
82 <tr><td>Name:</td><td><input name=userid></td></tr>
83 <tr><td>Password:</td><td><input type=password name=password></td></tr>
84 <tr><td colspan=2 align=center><input type=submit value=login></td></tr>
85 </table>
86 </form>
87 </body>
88 </html>
89 |;
90         exit
91     }
92 }
93
94
95 END { }       # module clean-up code here (global destructor)