1 package OpenILS::WWW::Proxy::Authen;
2 use strict; use warnings;
5 use Apache2::RequestUtil;
7 use Apache2::Const -compile => qw(OK HTTP_UNAUTHORIZED DECLINED HTTP_MOVED_TEMPORARILY NOT_FOUND :log);
8 use APR::Const -compile => qw(:error SUCCESS);
11 use Digest::MD5 qw/md5_hex/;
13 use OpenSRF::EX qw(:try);
16 # set the bootstrap config when
17 # this module is loaded
29 OpenSRF::System->bootstrap_client( config_file => $bootstrap );
30 return Apache2::Const::OK;
36 my $ltype = $apache->dir_config('OILSProxyLoginType');
37 my $perms = [ split ' ', $apache->dir_config('OILSProxyPermissions') ];
39 return Apache2::Const::NOT_FOUND unless (@$perms);
42 my $auth_ses = $cgi->cookie('ses') || $cgi->param('ses');
43 my $ws_ou = $apache->dir_config('OILSProxyLoginOU') || $cgi->cookie('ws_ou') || $cgi->param('ws_ou');
46 my $bad_auth = 1; # Assume failure until proven otherwise ;)
48 # push everyone to the secure site
49 if (!$ssl_off && $url =~ /^http:/o) {
50 my $base = $cgi->url(-base=>1);
51 $base =~ s/^http:/https:/o;
52 $apache->headers_out->set(Location => $base . $apache->unparsed_uri);
53 return Apache2::Const::HTTP_MOVED_TEMPORARILY;
59 while ($bad_auth && $tried_login == 0) {
62 my ($status, $p) = $apache->get_basic_auth_pw;
64 if ($status == Apache2::Const::OK) {
67 $u = $cgi->param('user');
68 $p = $cgi->param('passwd');
69 return $status if (!$u);
73 $auth_ses = oils_login($u, $p, $ltype);
75 $cookie = $cgi->cookie(
85 my $user = verify_login($auth_ses);
88 $ws_ou ||= $user->home_ou;
90 warn "Checking perms " . join(',', @$perms) . " for user " . $user->id . " at location $ws_ou\n";
92 my $failures = OpenSRF::AppSession
93 ->create('open-ils.actor')
94 ->request('open-ils.actor.user.perm.check', $auth_ses, $user->id, $ws_ou, $perms)
98 $cookie = $cgi->cookie(
109 $auth_ses = undef if($bad_auth && !$tried_login);
113 $apache->err_headers_out->add('Set-Cookie' => $cookie) if($cookie);
114 $apache->note_basic_auth_failure;
115 return Apache2::Const::HTTP_UNAUTHORIZED;
119 # We authenticated, and thus likely got a new auth key.
120 # Set it and redirect in case what we are protecting needs the key.
122 # When not redirecting we don't need the err_ variant of this. Noting for reference.
123 $apache->err_headers_out->add('Set-Cookie' => $cookie) if($cookie);
124 my $base = $cgi->url(-base=>1);
125 $apache->headers_out->set(Location => $base . $apache->unparsed_uri);
126 return Apache2::Const::HTTP_MOVED_TEMPORARILY;
129 # they're good, let 'em through
130 return Apache2::Const::OK;
133 # returns the user object if the session is valid, 0 otherwise
135 my $auth_token = shift;
136 return undef unless $auth_token;
138 my $user = OpenSRF::AppSession
139 ->create("open-ils.auth")
140 ->request( "open-ils.auth.session.retrieve", $auth_token )
143 if (ref($user) eq 'HASH' && $user->{ilsevent} == 1001) {
147 return $user if ref($user);
152 my( $username, $password, $type ) = @_;
155 my $nametype = 'username';
156 $nametype = 'barcode' if ($username =~ /^\d+$/o);
158 my $seed = OpenSRF::AppSession
159 ->create("open-ils.auth")
160 ->request( 'open-ils.auth.authenticate.init', $username )
163 return undef unless $seed;
165 my $response = OpenSRF::AppSession
166 ->create("open-ils.auth")
167 ->request( 'open-ils.auth.authenticate.complete',
168 { $nametype => $username, agent => 'authproxy',
169 password => md5_hex($seed . md5_hex($password)),
173 return undef unless $response;
175 return $response->{payload}->{authtoken};