]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/Proxy.pm
Post-2.5-m1 whitespace fixup
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / WWW / Proxy.pm
1 package OpenILS::WWW::Proxy;
2 use strict; use warnings;
3
4 use Apache2::Log;
5 use Apache2::Const -compile => qw(REDIRECT FORBIDDEN OK NOT_FOUND DECLINED :log);
6 use APR::Const    -compile => qw(:error SUCCESS);
7 use CGI;
8 use Data::Dumper;
9 use Digest::MD5 qw/md5_hex/;
10
11 use OpenSRF::EX qw(:try);
12 use OpenSRF::System;
13
14
15 # set the bootstrap config and template include directory when
16 # this module is loaded
17 my $bootstrap;
18 my $ssl_off;
19
20 my $default_template = <<HTML;
21 <html>
22     <head>
23         <title>TITLE</title>
24     </head>
25     <body>
26         <br/><br/><br/>
27         <center>
28         <form method='POST'>
29             <table style='border-collapse: collapse; border: 1px solid black;'>
30                 <tr>
31                     <th colspan='2' align='center'><u>DESCRIPTION</u></th>
32                 </tr>
33                 <tr>
34                     <th align="right">Username or barcode:</th>
35                     <td><input type="text" name="user"/></td>
36                 </tr>
37                 <tr>
38                     <th align="right">Password:</th>
39                     <td><input type="password" name="passwd"/></td>
40                 </tr>
41             </table>
42             <input type="submit" value="Log in"/>
43         </form>
44         </center>
45     </body>
46 </html>
47 HTML
48
49 sub import {
50     my $self = shift;
51     $bootstrap = shift;
52     $ssl_off = shift;
53 }
54
55
56 sub child_init {
57     OpenSRF::System->bootstrap_client( config_file => $bootstrap );
58     return Apache2::Const::OK;
59 }
60
61 sub handler {
62     my $apache = shift;
63
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') ];
69
70     return Apache2::Const::NOT_FOUND unless ($title || $proxyhtml);
71     return Apache2::Const::NOT_FOUND unless (@$perms);
72
73     my $cgi = new CGI;
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');
76
77     my $url = $cgi->url;
78
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;
85     }
86
87     if (!$auth_ses) {
88         my $u = $cgi->param('user');
89         my $p = $cgi->param('passwd');
90
91         if (!$u) {
92
93             print $cgi->header(-type=>'text/html', -expires=>'-1d');
94             if (!$proxyhtml) {
95                 $proxyhtml = $default_template;
96                 $proxyhtml =~ s/TITLE/$title/gso;
97                 $proxyhtml =~ s/DESCRIPTION/$desc/gso;
98             } else {
99                 # XXX template toolkit??
100             }
101
102             print $proxyhtml;
103             return Apache2::Const::OK;
104         }
105
106         $auth_ses = oils_login($u, $p, $ltype);
107         if ($auth_ses) {
108             print $cgi->redirect(
109                 -uri=> $apache->unparsed_uri,
110                 -cookie=>$cgi->cookie(
111                     -name=>'ses',
112                     -value=>$auth_ses,
113                     -path=>'/',
114                     -secure=>1
115                 )
116             );
117             return Apache2::Const::REDIRECT;
118         } else {
119             return back_to_login($apache, $cgi);
120         }
121     }
122
123     my $user = verify_login($auth_ses);
124     return back_to_login($apache, $cgi) unless $user;
125
126     $ws_ou ||= $user->home_ou;
127
128     warn "Checking perms " . join(',', @$perms) . " for user " . $user->id . " at location $ws_ou\n";
129
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)
133         ->gather(1);
134
135     return back_to_login($apache, $cgi) if (@$failures > 0);
136
137     # they're good, let 'em through
138     return Apache2::Const::DECLINED;
139 }
140
141 sub back_to_login {
142     my $apache = shift;
143     my $cgi = shift;
144     print $cgi->redirect(
145         -uri=>$apache->unparsed_uri,
146         -cookie=>$cgi->cookie(
147             -name=>'ses',
148             -value=>'',
149             -path=>'/',-expires=>'-1h'
150         )
151     );
152     return Apache2::Const::REDIRECT;
153 }
154
155 # returns the user object if the session is valid, 0 otherwise
156 sub verify_login {
157     my $auth_token = shift;
158     return undef unless $auth_token;
159
160     my $user = OpenSRF::AppSession
161         ->create("open-ils.auth")
162         ->request( "open-ils.auth.session.retrieve", $auth_token )
163         ->gather(1);
164
165     if (ref($user) eq 'HASH' && $user->{ilsevent} == 1001) {
166         return undef;
167     }
168
169     return $user if ref($user);
170     return undef;
171 }
172
173 sub oils_login {
174         my( $username, $password, $type ) = @_;
175
176         $type |= "staff";
177     my $nametype = 'username';
178     $nametype = 'barcode' if ($username =~ /^\d+$/o);
179
180         my $seed = OpenSRF::AppSession
181         ->create("open-ils.auth")
182         ->request( 'open-ils.auth.authenticate.init', $username )
183         ->gather(1);
184
185         return undef unless $seed;
186
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)),
192               type => $type })
193         ->gather(1);
194
195         return undef unless $response;
196
197         return $response->{payload}->{authtoken};
198 }
199
200 1;
201