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