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