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