]> ruin.nu Git - ndwebbie.git/blob - NDWeb/AuthHandler.pm
086d46b0ee44e2fdf03687b89a7c7486345fa4c0
[ndwebbie.git] / NDWeb / AuthHandler.pm
1 #!/usr/bin/perl -w -T
2 #**************************************************************************
3 #   Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu>               *
4 #                                                                         *
5 #   This program is free software; you can redistribute it and/or modify  *
6 #   it under the terms of the GNU General Public License as published by  *
7 #   the Free Software Foundation; either version 2 of the License, or     *
8 #   (at your option) any later version.                                   *
9 #                                                                         *
10 #   This program is distributed in the hope that it will be useful,       *
11 #   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13 #   GNU General Public License for more details.                          *
14 #                                                                         *
15 #   You should have received a copy of the GNU General Public License     *
16 #   along with this program; if not, write to the                         *
17 #   Free Software Foundation, Inc.,                                       *
18 #   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19 #**************************************************************************/
20
21 package NDWeb::AuthHandler;
22 use strict;
23 use warnings FATAL => 'all';
24
25 use ND::DB;
26 use Apache2::Access ();
27
28 sub handler {
29         my $r = shift;
30         my($res, $sent_pw) = $r->get_basic_auth_pw;
31         return $res if $res != Apache2::Const::OK;
32
33         my $dbh = ND::DB::DB();
34         my ($username) = $dbh->selectrow_array(q{SELECT username FROM users WHERE
35                 lower(username) = lower(?) AND password = MD5(?)},undef,$r->user,$sent_pw);
36         $dbh->disconnect;
37         if ($username){
38                 $r->user($username);
39                 return Apache2::Const::OK;
40         }
41         $r->note_basic_auth_failure();
42         return Apache2::Const::AUTH_REQUIRED;
43 }
44
45 1;