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(
83 my $user = verify_login($auth_ses);
86 $ws_ou ||= $user->home_ou;
88 warn "Checking perms " . join(',', @$perms) . " for user " . $user->id . " at location $ws_ou\n";
90 my $failures = OpenSRF::AppSession
91 ->create('open-ils.actor')
92 ->request('open-ils.actor.user.perm.check', $auth_ses, $user->id, $ws_ou, $perms)
96 $cookie = $cgi->cookie(
107 $auth_ses = undef if($bad_auth && !$tried_login);
111 $apache->err_headers_out->add('Set-Cookie' => $cookie) if($cookie);
112 $apache->note_basic_auth_failure;
113 return Apache2::Const::HTTP_UNAUTHORIZED;
117 # We authenticated, and thus likely got a new auth key.
118 # Set it and redirect in case what we are protecting needs the key.
120 # When not redirecting we don't need the err_ variant of this. Noting for reference.
121 $apache->err_headers_out->add('Set-Cookie' => $cookie) if($cookie);
122 my $base = $cgi->url(-base=>1);
123 $apache->headers_out->set(Location => $base . $apache->unparsed_uri);
124 return Apache2::Const::HTTP_MOVED_TEMPORARILY;
127 # they're good, let 'em through
128 return Apache2::Const::OK;
131 # returns the user object if the session is valid, 0 otherwise
133 my $auth_token = shift;
134 return undef unless $auth_token;
136 my $user = OpenSRF::AppSession
137 ->create("open-ils.auth")
138 ->request( "open-ils.auth.session.retrieve", $auth_token )
141 if (ref($user) eq 'HASH' && $user->{ilsevent} == 1001) {
145 return $user if ref($user);
150 my( $username, $password, $type ) = @_;
153 my $nametype = 'username';
154 $nametype = 'barcode' if ($username =~ /^\d+$/o);
156 my $seed = OpenSRF::AppSession
157 ->create("open-ils.auth")
158 ->request( 'open-ils.auth.authenticate.init', $username )
161 return undef unless $seed;
163 my $response = OpenSRF::AppSession
164 ->create("open-ils.auth")
165 ->request( 'open-ils.auth.authenticate.complete',
166 { $nametype => $username,
167 password => md5_hex($seed . md5_hex($password)),
171 return undef unless $response;
173 return $response->{payload}->{authtoken};