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 );
35 my $ltype = $apache->dir_config('OILSProxyLoginType');
36 my $perms = [ split ' ', $apache->dir_config('OILSProxyPermissions') ];
38 return Apache2::Const::NOT_FOUND unless (@$perms);
41 my $auth_ses = $cgi->cookie('ses') || $cgi->param('ses');
42 my $ws_ou = $apache->dir_config('OILSProxyLoginOU') || $cgi->cookie('ws_ou') || $cgi->param('ws_ou');
45 my $bad_auth = 1; # Assume failure until proven otherwise ;)
47 # push everyone to the secure site
48 if (!$ssl_off && $url =~ /^http:/o) {
49 my $base = $cgi->url(-base=>1);
50 $base =~ s/^http:/https:/o;
51 $apache->headers_out->set(Location => $base . $apache->unparsed_uri);
52 return Apache2::Const::HTTP_MOVED_TEMPORARILY;
58 while ($bad_auth && $tried_login == 0) {
61 my ($status, $p) = $apache->get_basic_auth_pw;
63 if ($status == Apache2::Const::OK) {
66 $u = $cgi->param('user');
67 $p = $cgi->param('passwd');
68 return $status if (!$u);
72 $auth_ses = oils_login($u, $p, $ltype);
74 $cookie = $cgi->cookie(
84 my $user = verify_login($auth_ses);
87 $ws_ou ||= $user->home_ou;
89 warn "Checking perms " . join(',', @$perms) . " for user " . $user->id . " at location $ws_ou\n";
91 my $failures = OpenSRF::AppSession
92 ->create('open-ils.actor')
93 ->request('open-ils.actor.user.perm.check', $auth_ses, $user->id, $ws_ou, $perms)
97 $cookie = $cgi->cookie(
108 $auth_ses = undef if($bad_auth && !$tried_login);
112 $apache->err_headers_out->add('Set-Cookie' => $cookie) if($cookie);
113 $apache->note_basic_auth_failure;
114 return Apache2::Const::HTTP_UNAUTHORIZED;
118 # We authenticated, and thus likely got a new auth key.
119 # Set it and redirect in case what we are protecting needs the key.
121 # When not redirecting we don't need the err_ variant of this. Noting for reference.
122 $apache->err_headers_out->add('Set-Cookie' => $cookie) if($cookie);
123 my $base = $cgi->url(-base=>1);
124 $apache->headers_out->set(Location => $base . $apache->unparsed_uri);
125 return Apache2::Const::HTTP_MOVED_TEMPORARILY;
128 # they're good, let 'em through
129 return Apache2::Const::OK;
132 # returns the user object if the session is valid, 0 otherwise
134 my $auth_token = shift;
135 return undef unless $auth_token;
137 my $user = OpenSRF::AppSession
138 ->create("open-ils.auth")
139 ->request( "open-ils.auth.session.retrieve", $auth_token )
142 if (ref($user) eq 'HASH' && $user->{ilsevent} == 1001) {
146 return $user if ref($user);
151 my( $username, $password, $type ) = @_;
154 my $nametype = 'username';
155 $nametype = 'barcode' if ($username =~ /^\d+$/o);
157 my $seed = OpenSRF::AppSession
158 ->create("open-ils.auth")
159 ->request( 'open-ils.auth.authenticate.init', $username )
162 return undef unless $seed;
164 my $response = OpenSRF::AppSession
165 ->create("open-ils.auth")
166 ->request( 'open-ils.auth.authenticate.complete',
167 { $nametype => $username, agent => 'authproxy',
168 password => md5_hex($seed . md5_hex($password)),
172 return undef unless $response;
174 return $response->{payload}->{authtoken};