Minor changes to authentication routines.
[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     # $authnotrequired will be set for scripts which will run without authentication
22     my $authnotrequired=shift;
23     if (my $userid=$ENV{'REMOTE_USERNAME'}) {
24         # Using Basic Authentication, no cookies required
25         my $cookie=$query->cookie(-name => 'sessionID',
26                                   -value => '',
27                                   -expires => '+1y');
28         return ($userid, $cookie, '');
29     }
30     my $sessionID=$query->cookie('sessionID');
31     my $message='';
32     warn "SID: ".$sessionID;
33
34     my $dbh=C4Connect();
35     my $sth=$dbh->prepare("select userid,ip,lasttime from sessions where sessionid=?");
36     $sth->execute($sessionID);
37     if ($sth->rows) {
38         my ($userid, $ip, $lasttime) = $sth->fetchrow;
39         if ($lasttime<time()-20 && $userid ne 'tonnesen') {
40             # timed logout
41             warn "$sessionID logged out due to inactivity.";
42             $message="You have been logged out due to inactivity.";
43             my $sti=$dbh->prepare("delete from sessions where sessionID=?");
44             $sti->execute($sessionID);
45             open L, ">>/tmp/sessionlog";
46             my $time=localtime(time());
47             printf L "%20s from %16s logged out at %30s (inactivity).\n", $userid, $ip, $time;
48             close L;
49         } elsif ($ip ne $ENV{'REMOTE_ADDR'}) {
50             # Different ip than originally logged in from
51             warn "$sessionID came from a new ip address.";
52             $message="ERROR ERROR ERROR ERROR<br>Attempt to re-use a cookie from a different ip address.";
53         } else {
54             my $cookie=$query->cookie(-name => 'sessionID',
55                                       -value => $sessionID,
56                                       -expires => '+1y');
57             warn "$sessionID had a valid cookie.";
58             my $sti=$dbh->prepare("update sessions set lasttime=? where sessionID=?");
59             $sti->execute(time(), $sessionID);
60             return ($userid, $cookie, $sessionID);
61         }
62     }
63
64
65
66     warn "$sessionID wasn't in sessions table.";
67     if ($authnotrequired) {
68         my $cookie=$query->cookie(-name => 'sessionID',
69                                   -value => '',
70                                   -expires => '+1y');
71         return('', $cookie, '');
72     } else {
73         ($sessionID) || ($sessionID=int(rand()*100000).'-'.time());
74         my $userid=$query->param('userid');
75         my $password=$query->param('password');
76         if (($userid eq 'librarian' || $userid eq 'tonnesen' || $userid eq 'patron') && $password eq 'koha') {
77             my $sti=$dbh->prepare("insert into sessions (sessionID, userid, ip,lasttime) values (?, ?, ?, ?)");
78             $sti->execute($sessionID, $userid, $ENV{'REMOTE_ADDR'}, time());
79             open L, ">>/tmp/sessionlog";
80             my $time=localtime(time());
81             printf L "%20s from %16s logged in  at %30s.\n", $userid, $ENV{'REMOTE_ADDR'}, $time;
82             close L;
83             return ($userid, $sessionID, $sessionID);
84         } else {
85             if ($userid) {
86                 $message="Invalid userid or password entered.";
87             }
88             my $parameters;
89             foreach (param $query) {
90                 $parameters->{$_}=$query->{$_};
91             }
92             my $cookie=$query->cookie(-name => 'sessionID',
93                                       -value => $sessionID,
94                                       -expires => '+1y');
95             print $query->header(-cookie=>$cookie);
96             print qq|
97 <html>
98 <body background=/images/kohaback.jpg>
99 <center>
100 <h2>$message</h2>
101
102 <form method=post>
103 <table border=0 cellpadding=10 width=60%>
104     <tr><td align=center valign=top>
105     <table border=0 bgcolor=#dddddd cellpadding=10>
106     <tr><th colspan=2 background=/images/background-mem.gif><font size=+2>Koha Login</font></th></tr>
107     <tr><td>Name:</td><td><input name=userid></td></tr>
108     <tr><td>Password:</td><td><input type=password name=password></td></tr>
109     <tr><td colspan=2 align=center><input type=submit value=login></td></tr>
110     </table>
111     
112     </td><td align=center valign=top>
113
114     <table border=0 bgcolor=#dddddd cellpadding=10>
115     <tr><th background=/images/background-mem.gif><font size=+2>Demo Information</font></th></tr>
116     <td>
117     Log in as librarian/koha or patron/koha.  The timeout is set to 20 seconds of
118     inactivity for the purposes of this demo.  You can navigate to the Circulation
119     or Acquisitions modules and you should see an indicator in the upper left of
120     the screen saying who you are logged in as.  If you want to try it out with
121     a longer timout period, log in as tonnesen/koha and the timeout period will
122     be 10 minutes.
123     </td>
124     </tr>
125     </table>
126     </td></tr>
127 </table>
128 </form>
129 </body>
130 </html>
131 |;
132             exit;
133         }
134     }
135 }
136
137
138 END { }       # module clean-up code here (global destructor)