cd89f55afc1239bed58e338c427ccf68315165b2
[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 use FS::Auth;
8
9 sub authen_cred {
10   my( $self, $r, $username, $password ) = @_;
11
12   preuser_setup();
13
14   unless ( _is_valid_user($username, $password) ) {
15     warn "failed auth $username from ". $r->connection->remote_ip. "\n";
16     return undef;
17   }
18
19   warn "authenticated $username from ". $r->connection->remote_ip. "\n";
20
21   FS::CurrentUser->load_user($username);
22
23   FS::CurrentUser->new_session;
24 }
25
26 sub _is_valid_user {
27   my( $username, $password ) = @_;
28
29   FS::Auth->authenticate($username, $password);
30 }
31
32 sub authen_ses_key {
33   my( $self, $r, $sessionkey ) = @_;
34
35   preuser_setup();
36
37   my $curuser = FS::CurrentUser->load_user_session( $sessionkey );
38
39   unless ( $curuser ) {
40     warn "bad session $sessionkey from ". $r->connection->remote_ip. "\n";
41     return undef;
42   }
43
44   $curuser->username;
45 }
46
47 1;