]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/Proxy/Authen.pm
2e33aa159e5408cff9f6e1c02d8f68e77ddb8bb4
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / WWW / Proxy / Authen.pm
1 package OpenILS::WWW::Proxy::Authen;
2 use strict; use warnings;
3
4 use Apache2::Access;
5 use Apache2::RequestUtil;
6 use Apache2::Log;
7 use Apache2::Const -compile => qw(OK HTTP_UNAUTHORIZED DECLINED HTTP_MOVED_TEMPORARILY NOT_FOUND :log);
8 use APR::Const    -compile => qw(:error SUCCESS);
9 use CGI;
10 use Data::Dumper;
11 use Digest::MD5 qw/md5_hex/;
12
13 use OpenSRF::EX qw(:try);
14 use OpenSRF::System;
15
16 # set the bootstrap config when 
17 # this module is loaded
18 my $bootstrap;
19 my $ssl_off;
20
21 sub import {
22     my $self = shift;
23     $bootstrap = shift;
24     $ssl_off = shift;
25 }
26
27
28 sub child_init {
29     OpenSRF::System->bootstrap_client( config_file => $bootstrap );
30     return Apache2::Const::OK;
31 }
32
33 sub handler {
34     my $apache = shift;
35
36     my $ltype = $apache->dir_config('OILSProxyLoginType');
37     my $perms = [ split ' ', $apache->dir_config('OILSProxyPermissions') ];
38
39     return Apache2::Const::NOT_FOUND unless (@$perms);
40
41     my $cgi = new CGI;
42     my $auth_ses = $cgi->cookie('ses') || $cgi->param('ses');
43     my $ws_ou = $apache->dir_config('OILSProxyLoginOU') || $cgi->cookie('ws_ou') || $cgi->param('ws_ou');
44
45     my $url = $cgi->url;
46     my $bad_auth = 1; # Assume failure until proven otherwise ;)
47
48     # push everyone to the secure site
49     if (!$ssl_off && $url =~ /^http:/o) {
50         my $base = $cgi->url(-base=>1);
51         $base =~ s/^http:/https:/o;
52         $apache->headers_out->set(Location => $base . $apache->unparsed_uri);
53         return Apache2::Const::HTTP_MOVED_TEMPORARILY;
54     }
55
56     my $tried_login = 0;
57     my $cookie;
58
59     while ($bad_auth && $tried_login == 0) {
60         if (!$auth_ses) {
61             $tried_login = 1;
62             my ($status, $p) = $apache->get_basic_auth_pw;
63             my $u;
64             if ($status == Apache2::Const::OK) {
65                 $u = $apache->user;
66             } else {
67                 $u = $cgi->param('user');
68                 $p = $cgi->param('passwd');
69                 return $status if (!$u);
70             }
71     
72             if ($u) {
73                 $auth_ses = oils_login($u, $p, $ltype);
74                 if ($auth_ses) {
75                     $cookie = $cgi->cookie(
76                         -name=>'ses',
77                         -value=>$auth_ses,
78                         -path=>'/',
79                         -secure=>1
80                     );
81                 }
82             }
83         }
84     
85         my $user = verify_login($auth_ses);
86     
87         if ($user) {
88             $ws_ou ||= $user->home_ou;
89     
90             warn "Checking perms " . join(',', @$perms) . " for user " . $user->id . " at location $ws_ou\n";
91     
92             my $failures = OpenSRF::AppSession
93                 ->create('open-ils.actor')
94                 ->request('open-ils.actor.user.perm.check', $auth_ses, $user->id, $ws_ou, $perms)
95                 ->gather(1);
96     
97             if (@$failures > 0) {
98                 $cookie = $cgi->cookie(
99                         -name=>'ses',
100                         -value=>'',
101                         -path=>'/',
102                         -expires=>'-1h'
103                 );
104             } else {
105                 $bad_auth = 0;
106             }
107         }
108
109         $auth_ses = undef if($bad_auth && !$tried_login);
110     }
111
112     if ($bad_auth) {
113         $apache->err_headers_out->add('Set-Cookie' => $cookie) if($cookie);
114         $apache->note_basic_auth_failure;
115         return Apache2::Const::HTTP_UNAUTHORIZED;
116     }
117
118     if ($tried_login) {
119         # We authenticated, and thus likely got a new auth key.
120         # Set it and redirect in case what we are protecting needs the key.
121
122         # When not redirecting we don't need the err_ variant of this. Noting for reference.
123         $apache->err_headers_out->add('Set-Cookie' => $cookie) if($cookie);
124         my $base = $cgi->url(-base=>1);
125         $apache->headers_out->set(Location => $base . $apache->unparsed_uri);
126         return Apache2::Const::HTTP_MOVED_TEMPORARILY;
127     }
128
129     # they're good, let 'em through
130     return Apache2::Const::OK;
131 }
132
133 # returns the user object if the session is valid, 0 otherwise
134 sub verify_login {
135     my $auth_token = shift;
136     return undef unless $auth_token;
137
138     my $user = OpenSRF::AppSession
139         ->create("open-ils.auth")
140         ->request( "open-ils.auth.session.retrieve", $auth_token )
141         ->gather(1);
142
143     if (ref($user) eq 'HASH' && $user->{ilsevent} == 1001) {
144         return undef;
145     }
146
147     return $user if ref($user);
148     return undef;
149 }
150
151 sub oils_login {
152         my( $username, $password, $type ) = @_;
153
154         $type |= "staff";
155     my $nametype = 'username';
156     $nametype = 'barcode' if ($username =~ /^\d+$/o);
157
158         my $seed = OpenSRF::AppSession
159         ->create("open-ils.auth")
160         ->request( 'open-ils.auth.authenticate.init', $username )
161         ->gather(1);
162
163         return undef unless $seed;
164
165         my $response = OpenSRF::AppSession
166         ->create("open-ils.auth")
167         ->request( 'open-ils.auth.authenticate.complete',
168             { $nametype => $username, agent => 'authproxy',
169               password => md5_hex($seed . md5_hex($password)),
170               type => $type })
171         ->gather(1);
172
173         return undef unless $response;
174
175         return $response->{payload}->{authtoken};
176 }
177
178 1;
179