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 );
63 my $proxyhtml = $apache->dir_config('OILSProxyHTML');
64 my $title = $apache->dir_config('OILSProxyTitle');
65 my $desc = $apache->dir_config('OILSProxyDescription');
66 my $ltype = $apache->dir_config('OILSProxyLoginType');
67 my $perms = [ split ' ', $apache->dir_config('OILSProxyPermissions') ];
69 return Apache2::Const::NOT_FOUND unless ($title || $proxyhtml);
70 return Apache2::Const::NOT_FOUND unless (@$perms);
73 my $auth_ses = $cgi->cookie('ses') || $cgi->param('ses');
74 my $ws_ou = $apache->dir_config('OILSProxyLoginOU') || $cgi->cookie('ws_ou') || $cgi->param('ws_ou');
78 # push everyone to the secure site
79 if (!$ssl_off && $url =~ /^http:/o) {
80 my $base = $cgi->url(-base=>1);
81 $base =~ s/^http:/https:/o;
82 print "Location: $base".$apache->unparsed_uri."\n\n";
83 return Apache2::Const::REDIRECT;
87 my $u = $cgi->param('user');
88 my $p = $cgi->param('passwd');
92 print $cgi->header(-type=>'text/html', -expires=>'-1d');
94 $proxyhtml = $default_template;
95 $proxyhtml =~ s/TITLE/$title/gso;
96 $proxyhtml =~ s/DESCRIPTION/$desc/gso;
98 # XXX template toolkit??
102 return Apache2::Const::OK;
105 $auth_ses = oils_login($u, $p, $ltype);
107 print $cgi->redirect(
108 -uri=> $apache->unparsed_uri,
109 -cookie=>$cgi->cookie(
116 return Apache2::Const::REDIRECT;
118 return back_to_login($apache, $cgi);
122 my $user = verify_login($auth_ses);
123 return back_to_login($apache, $cgi) unless $user;
125 $ws_ou ||= $user->home_ou;
127 warn "Checking perms " . join(',', @$perms) . " for user " . $user->id . " at location $ws_ou\n";
129 my $failures = OpenSRF::AppSession
130 ->create('open-ils.actor')
131 ->request('open-ils.actor.user.perm.check', $auth_ses, $user->id, $ws_ou, $perms)
134 return back_to_login($apache, $cgi) if (@$failures > 0);
136 # they're good, let 'em through
137 return Apache2::Const::DECLINED;
143 print $cgi->redirect(
144 -uri=>$apache->unparsed_uri,
145 -cookie=>$cgi->cookie(
148 -path=>'/',-expires=>'-1h'
151 return Apache2::Const::REDIRECT;
154 # returns the user object if the session is valid, 0 otherwise
156 my $auth_token = shift;
157 return undef unless $auth_token;
159 my $user = OpenSRF::AppSession
160 ->create("open-ils.auth")
161 ->request( "open-ils.auth.session.retrieve", $auth_token )
164 if (ref($user) eq 'HASH' && $user->{ilsevent} == 1001) {
168 return $user if ref($user);
173 my( $username, $password, $type ) = @_;
176 my $nametype = 'username';
177 $nametype = 'barcode' if ($username =~ /^\d+$/o);
179 my $seed = OpenSRF::AppSession
180 ->create("open-ils.auth")
181 ->request( 'open-ils.auth.authenticate.init', $username )
184 return undef unless $seed;
186 my $response = OpenSRF::AppSession
187 ->create("open-ils.auth")
188 ->request( 'open-ils.auth.authenticate.complete',
189 { $nametype => $username, agent => 'authproxy',
190 password => md5_hex($seed . md5_hex($password)),
194 return undef unless $response;
196 return $response->{payload}->{authtoken};