login/login pages and cookie/session-based auth
[freeside.git] / FS / FS / AuthCookieHandler.pm
1 package FS::AuthCookieHandler;
2 use base qw( Apache2::AuthCookie );
3
4 use strict;
5 use Digest::SHA qw( sha1_hex );
6 use FS::UID qw( adminsuidsetup );
7
8 my $secret = "XXX temporary"; #XXX move to a DB session with random number as key
9
10 my $module = 'legacy'; #XXX i am set in a conf somehow?  or a config file
11
12 sub authen_cred {
13   my( $self, $r, $username, $password ) = @_;
14
15   if ( _is_valid_user($username, $password) ) {
16       warn "authenticated $username from ". $r->connection->remote_ip. "\n";
17       adminsuidsetup($username);
18       my $session_key =
19         $username . '::' . sha1_hex( $username, $secret );
20       return $session_key;
21   } else {
22       warn "failed authentication $username from ". $r->connection->remote_ip. "\n";
23   }
24
25   return undef; #?
26 }
27
28 sub _is_valid_user {
29   my( $username, $password ) = @_;
30   my $class = 'FS::Auth::'.$module;
31
32   #earlier?
33   eval "use $class;";
34   die $@ if $@;
35
36   $class->authenticate($username, $password);
37
38 }
39
40 sub authen_ses_key {
41   my( $self, $r, $session_key ) = @_;
42
43   my ($username, $mac) = split /::/, $session_key;
44
45   if ( sha1_hex( $username, $secret ) eq $mac ) {
46     adminsuidsetup($username);
47     return $username;
48   } else {
49     warn "bad session $session_key from ". $r->connection->remote_ip. "\n";
50   }
51
52   return undef;
53
54 }
55
56 1;