1 package OpenILS::WWW::Proxy;
2 use strict; use warnings;
5 use Apache2::Const -compile => qw(REDIRECT FORBIDDEN OK NOT_FOUND DECLINED :log);
6 use APR::Const -compile => qw(:error SUCCESS);
9 use Digest::MD5 qw/md5_hex/;
11 use OpenSRF::EX qw(:try);
15 # set the bootstrap config and template include directory when
16 # this module is loaded
20 my $default_template = <<HTML;
29 <table style='border-collapse: collapse; border: 1px solid black;'>
31 <th colspan='2' align='center'><u>DESCRIPTION</u></th>
34 <th align="right">Username or barcode:</th>
35 <td><input type="text" name="user"/></td>
38 <th align="right">Password:</th>
39 <td><input type="password" name="passwd"/></td>
42 <input type="submit" value="Log in"/>
57 OpenSRF::System->bootstrap_client( config_file => $bootstrap );
58 return Apache2::Const::OK;
64 my $proxyhtml = $apache->dir_config('OILSProxyHTML');
65 my $title = $apache->dir_config('OILSProxyTitle');
66 my $desc = $apache->dir_config('OILSProxyDescription');
67 my $ltype = $apache->dir_config('OILSProxyLoginType');
68 my $perms = [ split ' ', $apache->dir_config('OILSProxyPermissions') ];
70 return Apache2::Const::NOT_FOUND unless ($title || $proxyhtml);
71 return Apache2::Const::NOT_FOUND unless (@$perms);
74 my $auth_ses = $cgi->cookie('ses') || $cgi->param('ses');
75 my $ws_ou = $apache->dir_config('OILSProxyLoginOU') || $cgi->cookie('ws_ou') || $cgi->param('ws_ou');
79 # push everyone to the secure site
80 if (!$ssl_off && $url =~ /^http:/o) {
81 my $base = $cgi->url(-base=>1);
82 $base =~ s/^http:/https:/o;
83 print "Location: $base".$apache->unparsed_uri."\n\n";
84 return Apache2::Const::REDIRECT;
88 my $u = $cgi->param('user');
89 my $p = $cgi->param('passwd');
93 print $cgi->header(-type=>'text/html', -expires=>'-1d');
95 $proxyhtml = $default_template;
96 $proxyhtml =~ s/TITLE/$title/gso;
97 $proxyhtml =~ s/DESCRIPTION/$desc/gso;
99 # XXX template toolkit??
103 return Apache2::Const::OK;
106 $auth_ses = oils_login($u, $p, $ltype);
108 print $cgi->redirect(
109 -uri=> $apache->unparsed_uri,
110 -cookie=>$cgi->cookie(
117 return Apache2::Const::REDIRECT;
119 return back_to_login($apache, $cgi);
123 my $user = verify_login($auth_ses);
124 return back_to_login($apache, $cgi) unless $user;
126 $ws_ou ||= $user->home_ou;
128 warn "Checking perms " . join(',', @$perms) . " for user " . $user->id . " at location $ws_ou\n";
130 my $failures = OpenSRF::AppSession
131 ->create('open-ils.actor')
132 ->request('open-ils.actor.user.perm.check', $auth_ses, $user->id, $ws_ou, $perms)
135 return back_to_login($apache, $cgi) if (@$failures > 0);
137 # they're good, let 'em through
138 return Apache2::Const::DECLINED;
144 print $cgi->redirect(
145 -uri=>$apache->unparsed_uri,
146 -cookie=>$cgi->cookie(
149 -path=>'/',-expires=>'-1h'
152 return Apache2::Const::REDIRECT;
155 # returns the user object if the session is valid, 0 otherwise
157 my $auth_token = shift;
158 return undef unless $auth_token;
160 my $user = OpenSRF::AppSession
161 ->create("open-ils.auth")
162 ->request( "open-ils.auth.session.retrieve", $auth_token )
165 if (ref($user) eq 'HASH' && $user->{ilsevent} == 1001) {
169 return $user if ref($user);
174 my( $username, $password, $type ) = @_;
177 my $nametype = 'username';
178 $nametype = 'barcode' if ($username =~ /^\d+$/o);
180 my $seed = OpenSRF::AppSession
181 ->create("open-ils.auth")
182 ->request( 'open-ils.auth.authenticate.init', $username )
185 return undef unless $seed;
187 my $response = OpenSRF::AppSession
188 ->create("open-ils.auth")
189 ->request( 'open-ils.auth.authenticate.complete',
190 { $nametype => $username, agent => 'authproxy',
191 password => md5_hex($seed . md5_hex($password)),
195 return undef unless $response;
197 return $response->{payload}->{authtoken};