]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/AuthProxy.pm
Fix empty statuses filter
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / Application / AuthProxy.pm
1 #!/usr/bin/perl
2
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16
17 =head1 NAME
18
19 OpenILS::Application::AuthProxy - Negotiator for proxy-style authentication
20
21 =head1 AUTHOR
22
23 Dan Wells, dbw2@calvin.edu
24
25 =cut
26
27 package OpenILS::Application::AuthProxy;
28
29 use strict;
30 use warnings;
31 use OpenILS::Application;
32 use base qw/OpenILS::Application/;
33 use OpenSRF::Utils::Cache;
34 use OpenSRF::Utils::Logger qw(:logger);
35 use OpenSRF::Utils::SettingsClient;
36 use OpenILS::Application::AppUtils;
37 use OpenILS::Utils::Fieldmapper;
38 use OpenILS::Event;
39 use UNIVERSAL::require;
40 use Digest::MD5 qw/md5_hex/;
41 my $U = 'OpenILS::Application::AppUtils';
42
43 # NOTE: code assumes throughout that '0' is never a valid username, barcode,
44 # or password; some logic will need to be tweaked to support it if needed.
45
46 my @authenticators;
47 my %authenticators_by_name;
48 my $enabled = 'false';
49 my $cache;
50 my $seed_timeout;
51 my $block_timeout;
52 my $block_count;
53
54 sub initialize {
55     my $conf = OpenSRF::Utils::SettingsClient->new;
56     $cache = OpenSRF::Utils::Cache->new();
57
58     my @pfx = ( "apps", "open-ils.auth", "app_settings", "auth_limits" );
59
60     # read in (or set defaults) for brute force blocking settings
61     $seed_timeout = $conf->config_value( @pfx, "seed" );
62     $seed_timeout = 30 if (!$seed_timeout or $seed_timeout < 0);
63     $block_timeout = $conf->config_value( @pfx, "block_time" );
64     $block_timeout = $seed_timeout * 3 if (!$block_timeout or $block_timeout < 0);
65     $block_count = $conf->config_value( @pfx, "block_count" );
66     $block_count = 10 if (!$block_count or $block_count < 0);
67
68     @pfx = ( "apps", "open-ils.auth_proxy", "app_settings" );
69
70     $enabled = $conf->config_value( @pfx, 'enabled' );
71
72     my $auth_configs = $conf->config_value( @pfx, 'authenticators', 'authenticator' );
73     $auth_configs = [$auth_configs] if ref($auth_configs) eq 'HASH';
74
75     if ( !@$auth_configs ) {
76         $logger->error("AuthProxy: authenticators list not found!");
77     } else {
78         foreach my $auth_config (@$auth_configs) {
79             my $auth_handler;
80             if ($auth_config->{'name'} eq 'native') {
81                 $auth_handler = 'OpenILS::Application::AuthProxy::Native';
82             } else {
83                 $auth_handler = $auth_config->{module};
84                 next unless $auth_handler;
85
86                 $logger->debug("Attempting to load AuthProxy handler: $auth_handler");
87                 $auth_handler->use;
88                 if($@) {
89                     $logger->error("Unable to load AuthProxy handler [$auth_handler]: $@");
90                     next;
91                 }
92             }
93
94             &_make_option_array($auth_config, 'login_types', 'type');
95             &_make_option_array($auth_config, 'org_units', 'unit');
96
97             my $authenticator = $auth_handler->new($auth_config);
98             push @authenticators, $authenticator;
99             $authenticators_by_name{$authenticator->name} = $authenticator;
100             $logger->debug("Successfully loaded AuthProxy handler: $auth_handler");
101         }
102         $logger->debug("AuthProxy: authenticators loaded");
103     }
104 }
105
106 # helper function to simplify the config structure
107 sub _make_option_array {
108     my ($auth_config, $container_name, $node_name) = @_;
109
110     if (exists $auth_config->{$container_name}
111         and ref $auth_config->{$container_name} eq 'HASH') {
112         my $nodes = $auth_config->{$container_name}{$node_name};
113         if ($nodes) {
114             if (ref $nodes ne 'ARRAY') {
115                 $auth_config->{$container_name} = [$nodes];
116             } else {
117                 $auth_config->{$container_name} = $nodes;
118             }
119         } else {
120             delete $auth_config->{$container_name};
121         }
122     } else {
123         delete $auth_config->{$container_name};
124     }
125 }
126
127
128
129 __PACKAGE__->register_method(
130     method    => "enabled",
131     api_name  => "open-ils.auth_proxy.enabled",
132     api_level => 1,
133     stream    => 1,
134     argc      => 0,
135     signature => {
136         desc => q/Check if AuthProxy is enabled/,
137         return => {
138             desc => "True if enabled, false if not",
139             type => "bool"
140         }
141     }
142 );
143 sub enabled {
144     return (!$enabled or $enabled eq 'false') ? 0 : 1;
145 }
146
147 __PACKAGE__->register_method(
148     method    => "login",
149     api_name  => "open-ils.auth_proxy.login",
150     api_level => 1,
151     stream    => 1,
152     argc      => 1,
153     signature => {
154         desc => q/Basic single-factor login method/,
155         params => [
156             {name=> "args", desc => q/A hash of arguments.  Valid keys and their meanings:
157     username := Username to authenticate.
158     barcode  := Barcode of user to authenticate (currently supported by 'native' only!)
159     password := Password for verifying the user.
160     type     := Type of login being attempted (Staff Client, OPAC, etc.).
161     org      := Org unit id
162 /,
163                 type => "hash"}
164         ],
165         return => {
166             desc => "Authentication seed or failure event",
167             type => "mixed"
168         }
169     }
170 );
171 sub login {
172     my ( $self, $conn, $args ) = @_;
173
174     return OpenILS::Event->new( 'LOGIN_FAILED' )
175       unless (&enabled() and ($args->{'username'} or $args->{'barcode'}));
176
177     # check for possibility of brute-force attack
178     my $fail_count;
179     # since barcode logins are for 'native' only, we will rely on the blocking
180     # code built-in to 'native' for those logins
181     if ($args->{'username'}) {
182         $fail_count = $cache->get_cache('oils_auth_' . $args->{'username'} . '_count') || 0;
183         if ($fail_count >= $block_count) {
184             $logger->debug("AuthProxy found too many recent failures for '" . $args->{'username'} . "' : $fail_count, forcing failure state.");
185             $cache->put_cache('oils_auth_' . $args->{'username'} . '_count', ++$fail_count, $block_timeout);
186             return OpenILS::Event->new( 'LOGIN_FAILED' );
187         }
188     }
189
190     my @error_events;
191     my $authenticated = 0;
192     my $auths;
193
194     # if they specify an authenticator by name, only try that one
195     if ($args->{'name'}) {
196         $auths = [$authenticators_by_name{$args->{'name'}}];
197     } else {
198         $auths = \@authenticators;
199     }
200
201     foreach my $authenticator (@$auths) {
202         # skip authenticators specified for a different login type
203         # or org unit id
204         if ($authenticator->login_types and $args->{'type'}) {
205             next unless grep(/^(all|$args->{'type'})$/, @{$authenticator->{'login_types'}});
206         }
207         if ($authenticator->org_units and $args->{'org'}) {
208             next unless grep(/^(all|$args->{'org'})$/, @{$authenticator->{'org_units'}});
209         }
210
211         my $event;
212         # treat native specially
213         if ($authenticator->name eq 'native') {
214             $event = &_do_login($args);
215         } else {
216             $event = $authenticator->authenticate($args);
217         }
218         my $code = $U->event_code($event);
219         if ($code) {
220             push @error_events, $event;
221         } elsif (defined $code) { # code is '0', i.e. SUCCESS
222             if (exists $event->{'payload'}) { # we have a complete native login
223                 return $event;
224             } else { # do a 'forced' login
225                 return &_do_login($args, 1);
226             }
227         }
228     }
229
230     # if we got this far, we failed
231     # increment the brute force counter if 'native' didn't already
232     if ($args->{'username'} and !exists $authenticators_by_name{'native'}) {
233         $cache->put_cache('oils_auth_' . $args->{'username'} . '_count', ++$fail_count, $block_timeout);
234     }
235     # TODO: send back some form of collected error events
236     return OpenILS::Event->new( 'LOGIN_FAILED' );
237 }
238
239 sub _do_login {
240     my $args = shift;
241     my $authenticated = shift;
242
243     my $seeder = $args->{'username'} ? $args->{'username'} : $args->{'barcode'};
244     my $seed =
245       OpenSRF::AppSession->create("open-ils.auth")
246       ->request( 'open-ils.auth.authenticate.init', $seeder )->gather(1);
247
248     return OpenILS::Event->new( 'LOGIN_FAILED' )
249       unless $seed;
250
251     my $real_password = $args->{'password'};
252     # if we have already authenticated, look up the password needed to finish
253     if ($authenticated) {
254         # barcode-based login is supported only for 'native' logins
255         return OpenILS::Event->new( 'LOGIN_FAILED' ) if !$args->{'username'};
256         my $user = $U->cstorereq(
257             "open-ils.cstore.direct.actor.user.search.atomic",
258             { usrname => $args->{'username'} }
259         );
260         $args->{'password'} = md5_hex( $seed . $user->[0]->passwd );
261     } else {
262         $args->{'password'} = md5_hex( $seed . md5_hex($real_password) );
263     }
264     my $response = OpenSRF::AppSession->create("open-ils.auth")->request(
265         'open-ils.auth.authenticate.complete',
266         $args
267     )->gather(1);
268     $args->{'password'} = $real_password;
269
270     return OpenILS::Event->new( 'LOGIN_FAILED' )
271       unless $response;
272
273     return $response;
274 }
275
276 __PACKAGE__->register_method(
277     method    => "authenticators",
278     api_name  => "open-ils.auth_proxy.authenticators",
279     api_level => 1,
280     stream    => 1,
281     argc      => 1,
282     signature => {
283         desc => q/Get a list of viable authenticators/,
284         params => [
285             {name=> "args", desc => q/A hash of arguments.  Valid keys and their meanings:
286     type     := Type of login being attempted (Staff Client, OPAC, etc.).
287     org      := Org unit id
288 /,
289                 type => "hash"}
290         ],
291         return => {
292             desc => "List of viable authenticators",
293             type => "array"
294         }
295     }
296 );
297 sub authenticators {
298     my ( $self, $conn, $args ) = @_;
299
300     my @viable_auths;
301
302     foreach my $authenticator (@authenticators) {
303         # skip authenticators specified for a different login type
304         # or org unit id
305         if ($authenticator->login_types and $args->{'type'}) {
306             next unless grep(/^(all|$args->{'type'})$/, @{$authenticator->login_types});
307         }
308         if ($authenticator->org_units and $args->{'org'}) {
309             next unless grep(/^(all|$args->{'org'})$/, @{$authenticator->org_units});
310         }
311
312         push @viable_auths, $authenticator->name;
313     }
314
315     return \@viable_auths;
316 }
317
318
319 # --------------------------------------------------------------------------
320 # Stub package for 'native' authenticator
321 # --------------------------------------------------------------------------
322 package OpenILS::Application::AuthProxy::Native;
323 use strict; use warnings;
324 use base 'OpenILS::Application::AuthProxy::AuthBase';
325
326 1;