a8ee37079aa10fe3d73688c1b5c3128d34166fef
[freeside.git] / FS / FS / AuthCookieHandler.pm
1 package FS::AuthCookieHandler;
2 use base qw( Apache2::AuthCookie );
3
4 use strict;
5 use FS::UID qw( adminsuidsetup preuser_setup );
6 use FS::CurrentUser;
7
8 my $module = 'legacy'; #XXX i am set in a conf somehow?  or a config file
9
10 sub authen_cred {
11   my( $self, $r, $username, $password ) = @_;
12
13   unless ( _is_valid_user($username, $password) ) {
14     warn "failed auth $username from ". $r->connection->remote_ip. "\n";
15     return undef;
16   }
17
18   warn "authenticated $username from ". $r->connection->remote_ip. "\n";
19   adminsuidsetup($username);
20
21   FS::CurrentUser->new_session;
22
23 }
24
25 sub _is_valid_user {
26   my( $username, $password ) = @_;
27   my $class = 'FS::Auth::'.$module;
28
29   #earlier?
30   eval "use $class;";
31   die $@ if $@;
32
33   $class->authenticate($username, $password);
34
35 }
36
37 sub authen_ses_key {
38   my( $self, $r, $sessionkey ) = @_;
39
40   preuser_setup();
41
42   my $curuser = FS::CurrentUser->load_user_session( $sessionkey );
43
44   unless ( $curuser ) {
45     warn "bad session $sessionkey from ". $r->connection->remote_ip. "\n";
46     return undef;
47   }
48
49   $curuser->username;
50
51 }
52
53 1;