]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/Proxy/Authen.pm
Merge branch 'master' of git.evergreen-ils.org:Evergreen-DocBook into doc_consolidati...
[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                     );
79                 }
80             }
81         }
82     
83         my $user = verify_login($auth_ses);
84     
85         if ($user) {
86             $ws_ou ||= $user->home_ou;
87     
88             warn "Checking perms " . join(',', @$perms) . " for user " . $user->id . " at location $ws_ou\n";
89     
90             my $failures = OpenSRF::AppSession
91                 ->create('open-ils.actor')
92                 ->request('open-ils.actor.user.perm.check', $auth_ses, $user->id, $ws_ou, $perms)
93                 ->gather(1);
94     
95             if (@$failures > 0) {
96                 $cookie = $cgi->cookie(
97                         -name=>'ses',
98                         -value=>'',
99                         -path=>'/',
100                         -expires=>'-1h'
101                 );
102             } else {
103                 $bad_auth = 0;
104             }
105         }
106
107         $auth_ses = undef if($bad_auth && !$tried_login);
108     }
109
110     if ($bad_auth) {
111         $apache->err_headers_out->add('Set-Cookie' => $cookie) if($cookie);
112         $apache->note_basic_auth_failure;
113         return Apache2::Const::HTTP_UNAUTHORIZED;
114     }
115
116     if ($tried_login) {
117         # We authenticated, and thus likely got a new auth key.
118         # Set it and redirect in case what we are protecting needs the key.
119
120         # When not redirecting we don't need the err_ variant of this. Noting for reference.
121         $apache->err_headers_out->add('Set-Cookie' => $cookie) if($cookie);
122         my $base = $cgi->url(-base=>1);
123         $apache->headers_out->set(Location => $base . $apache->unparsed_uri);
124         return Apache2::Const::HTTP_MOVED_TEMPORARILY;
125     }
126
127     # they're good, let 'em through
128     return Apache2::Const::OK;
129 }
130
131 # returns the user object if the session is valid, 0 otherwise
132 sub verify_login {
133         my $auth_token = shift;
134         return undef unless $auth_token;
135
136         my $user = OpenSRF::AppSession
137                 ->create("open-ils.auth")
138                 ->request( "open-ils.auth.session.retrieve", $auth_token )
139                 ->gather(1);
140
141         if (ref($user) eq 'HASH' && $user->{ilsevent} == 1001) {
142                 return undef;
143         }
144
145         return $user if ref($user);
146         return undef;
147 }
148
149 sub oils_login {
150         my( $username, $password, $type ) = @_;
151
152         $type |= "staff";
153         my $nametype = 'username';
154         $nametype = 'barcode' if ($username =~ /^\d+$/o);
155
156         my $seed = OpenSRF::AppSession
157                 ->create("open-ils.auth")
158                 ->request( 'open-ils.auth.authenticate.init', $username )
159                 ->gather(1);
160
161         return undef unless $seed;
162
163         my $response = OpenSRF::AppSession
164                 ->create("open-ils.auth")
165                 ->request( 'open-ils.auth.authenticate.complete',
166                         { $nametype => $username, agent => 'authproxy',
167                           password => md5_hex($seed . md5_hex($password)),
168                           type => $type })
169                 ->gather(1);
170
171         return undef unless $response;
172
173         return $response->{payload}->{authtoken};
174 }
175
176 1;
177