]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/Proxy.pm
Merge branch 'master' of git+ssh://yeti.esilibrary.com/home/evergreen/evergreen-equin...
[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 }
59
60 sub handler {
61         my $apache = shift;
62
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') ];
68
69         return Apache2::Const::NOT_FOUND unless ($title || $proxyhtml);
70         return Apache2::Const::NOT_FOUND unless (@$perms);
71
72         my $cgi = new CGI;
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');
75
76         my $url = $cgi->url;
77
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;
84         }
85
86         if (!$auth_ses) {
87                 my $u = $cgi->param('user');
88                 my $p = $cgi->param('passwd');
89
90                 if (!$u) {
91
92                         print $cgi->header(-type=>'text/html', -expires=>'-1d');
93                         if (!$proxyhtml) {
94                                 $proxyhtml = $default_template;
95                                 $proxyhtml =~ s/TITLE/$title/gso;
96                                 $proxyhtml =~ s/DESCRIPTION/$desc/gso;
97                         } else {
98                                 # XXX template toolkit??
99                         }
100
101                         print $proxyhtml;
102                         return Apache2::Const::OK;
103                 }
104
105                 $auth_ses = oils_login($u, $p, $ltype);
106                 if ($auth_ses) {
107                         print $cgi->redirect(
108                                 -uri=> $apache->unparsed_uri,
109                                 -cookie=>$cgi->cookie(
110                                         -name=>'ses',
111                                         -value=>$auth_ses,
112                                         -path=>'/'
113                                 )
114                         );
115                         return Apache2::Const::REDIRECT;
116                 } else {
117             return back_to_login($apache, $cgi);
118         }
119         }
120
121         my $user = verify_login($auth_ses);
122     return back_to_login($apache, $cgi) unless $user;
123
124         $ws_ou ||= $user->home_ou;
125
126         warn "Checking perms " . join(',', @$perms) . " for user " . $user->id . " at location $ws_ou\n";
127
128         my $failures = OpenSRF::AppSession
129                 ->create('open-ils.actor')
130                 ->request('open-ils.actor.user.perm.check', $auth_ses, $user->id, $ws_ou, $perms)
131                 ->gather(1);
132
133         return back_to_login($apache, $cgi) if (@$failures > 0);
134
135         # they're good, let 'em through
136         return Apache2::Const::DECLINED;
137 }
138
139 sub back_to_login {
140     my $apache = shift;
141     my $cgi = shift;
142     print $cgi->redirect(
143         -uri=>$apache->unparsed_uri,
144         -cookie=>$cgi->cookie(
145             -name=>'ses',
146             -value=>'',
147             -path=>'/',-expires=>'-1h'
148         )
149     );
150     return Apache2::Const::REDIRECT;
151 }
152
153 # returns the user object if the session is valid, 0 otherwise
154 sub verify_login {
155         my $auth_token = shift;
156         return undef unless $auth_token;
157
158         my $user = OpenSRF::AppSession
159                 ->create("open-ils.auth")
160                 ->request( "open-ils.auth.session.retrieve", $auth_token )
161                 ->gather(1);
162
163         if (ref($user) eq 'HASH' && $user->{ilsevent} == 1001) {
164                 return undef;
165         }
166
167         return $user if ref($user);
168         return undef;
169 }
170
171 sub oils_login {
172         my( $username, $password, $type ) = @_;
173
174         $type |= "staff";
175         my $nametype = 'username';
176         $nametype = 'barcode' if ($username =~ /^\d+$/o);
177
178         my $seed = OpenSRF::AppSession
179                 ->create("open-ils.auth")
180                 ->request( 'open-ils.auth.authenticate.init', $username )
181                 ->gather(1);
182
183         return undef unless $seed;
184
185         my $response = OpenSRF::AppSession
186                 ->create("open-ils.auth")
187                 ->request( 'open-ils.auth.authenticate.complete',
188                         { $nametype => $username,
189                           password => md5_hex($seed . md5_hex($password)),
190                           type => $type })
191                 ->gather(1);
192
193         return undef unless $response;
194
195         return $response->{payload}->{authtoken};
196 }
197
198 1;
199