]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/AuthProxy.pm
LP1203753 AuthProxy barcode login support
[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::Utils::CStoreEditor qw/:funcs/;
39 use OpenILS::Event;
40 use UNIVERSAL::require;
41 use Digest::MD5 qw/md5_hex/;
42 my $U = 'OpenILS::Application::AppUtils';
43
44 # NOTE: code assumes throughout that '0' is never a valid username, barcode,
45 # or password; some logic will need to be tweaked to support it if needed.
46
47 my @authenticators;
48 my %authenticators_by_name;
49 my $enabled = 'false';
50 my $cache;
51 my $seed_timeout;
52 my $block_timeout;
53 my $block_count;
54
55 sub initialize {
56     my $conf = OpenSRF::Utils::SettingsClient->new;
57     $cache = OpenSRF::Utils::Cache->new();
58
59     my @pfx = ( "apps", "open-ils.auth", "app_settings", "auth_limits" );
60
61     # read in (or set defaults) for brute force blocking settings
62     $seed_timeout = $conf->config_value( @pfx, "seed" );
63     $seed_timeout = 30 if (!$seed_timeout or $seed_timeout < 0);
64     $block_timeout = $conf->config_value( @pfx, "block_time" );
65     $block_timeout = $seed_timeout * 3 if (!$block_timeout or $block_timeout < 0);
66     $block_count = $conf->config_value( @pfx, "block_count" );
67     $block_count = 10 if (!$block_count or $block_count < 0);
68
69     @pfx = ( "apps", "open-ils.auth_proxy", "app_settings" );
70
71     $enabled = $conf->config_value( @pfx, 'enabled' );
72
73     my $auth_configs = $conf->config_value( @pfx, 'authenticators', 'authenticator' );
74     $auth_configs = [$auth_configs] if ref($auth_configs) eq 'HASH';
75
76     if ( !@$auth_configs ) {
77         $logger->error("AuthProxy: authenticators list not found!");
78     } else {
79         foreach my $auth_config (@$auth_configs) {
80             my $auth_handler;
81             if ($auth_config->{'name'} eq 'native') {
82                 $auth_handler = 'OpenILS::Application::AuthProxy::Native';
83             } else {
84                 $auth_handler = $auth_config->{module};
85                 next unless $auth_handler;
86
87                 $logger->debug("Attempting to load AuthProxy handler: $auth_handler");
88                 $auth_handler->use;
89                 if($@) {
90                     $logger->error("Unable to load AuthProxy handler [$auth_handler]: $@");
91                     next;
92                 }
93             }
94
95             &_make_option_array($auth_config, 'login_types', 'type');
96             &_make_option_array($auth_config, 'org_units', 'unit');
97
98             my $authenticator = $auth_handler->new($auth_config);
99             push @authenticators, $authenticator;
100             $authenticators_by_name{$authenticator->name} = $authenticator;
101             $logger->debug("Successfully loaded AuthProxy handler: $auth_handler");
102         }
103         $logger->debug("AuthProxy: authenticators loaded");
104     }
105 }
106
107 # helper function to simplify the config structure
108 sub _make_option_array {
109     my ($auth_config, $container_name, $node_name) = @_;
110
111     if (exists $auth_config->{$container_name}
112         and ref $auth_config->{$container_name} eq 'HASH') {
113         my $nodes = $auth_config->{$container_name}{$node_name};
114         if ($nodes) {
115             if (ref $nodes ne 'ARRAY') {
116                 $auth_config->{$container_name} = [$nodes];
117             } else {
118                 $auth_config->{$container_name} = $nodes;
119             }
120         } else {
121             delete $auth_config->{$container_name};
122         }
123     } else {
124         delete $auth_config->{$container_name};
125     }
126 }
127
128
129
130 __PACKAGE__->register_method(
131     method    => "enabled",
132     api_name  => "open-ils.auth_proxy.enabled",
133     api_level => 1,
134     stream    => 1,
135     argc      => 0,
136     signature => {
137         desc => q/Check if AuthProxy is enabled/,
138         return => {
139             desc => "True if enabled, false if not",
140             type => "bool"
141         }
142     }
143 );
144 sub enabled {
145     return (!$enabled or $enabled eq 'false') ? 0 : 1;
146 }
147
148 __PACKAGE__->register_method(
149     method    => "login",
150     api_name  => "open-ils.auth_proxy.login",
151     api_level => 1,
152     stream    => 1,
153     argc      => 1,
154     signature => {
155         desc => q/Basic single-factor login method/,
156         params => [
157             {name=> "args", desc => q/A hash of arguments.  Valid keys and their meanings:
158     username := Username to authenticate.
159     barcode  := Barcode of user to authenticate 
160     password := Password for verifying the user.
161     type     := Type of login being attempted (Staff Client, OPAC, etc.).
162     org      := Org unit id
163 /,
164                 type => "hash"}
165         ],
166         return => {
167             desc => "Authentication seed or failure event",
168             type => "mixed"
169         }
170     }
171 );
172 sub login {
173     my ( $self, $conn, $args ) = @_;
174     $args ||= {};
175
176     return OpenILS::Event->new( 'LOGIN_FAILED' )
177       unless (&enabled() and ($args->{'username'} or $args->{'barcode'}));
178
179     if ($args->{barcode} and !$args->{username}) {
180         # translate barcode logins into username logins by locating
181         # the matching card/user and collecting the username.
182
183         my $card = new_editor()->search_actor_card([
184             {barcode => $args->{barcode}, active => 't'},
185             {flesh => 1, flesh_fields => {ac => ['usr']}}
186         ])->[0];
187
188         $args->{username} = $card->usr->usrname if $card;
189     }
190
191     # check for possibility of brute-force attack
192     my $fail_count;
193     if ($args->{'username'}) {
194         $fail_count = $cache->get_cache('oils_auth_' . $args->{'username'} . '_count') || 0;
195         if ($fail_count >= $block_count) {
196             $logger->debug("AuthProxy found too many recent failures for '" . $args->{'username'} . "' : $fail_count, forcing failure state.");
197             $cache->put_cache('oils_auth_' . $args->{'username'} . '_count', ++$fail_count, $block_timeout);
198             return OpenILS::Event->new( 'LOGIN_FAILED' );
199         }
200     }
201
202     my @error_events;
203     my $authenticated = 0;
204     my $auths;
205
206     # if they specify an authenticator by name, only try that one
207     if ($args->{'name'}) {
208         $auths = [$authenticators_by_name{$args->{'name'}}];
209     } else {
210         $auths = \@authenticators;
211     }
212
213     foreach my $authenticator (@$auths) {
214         # skip authenticators specified for a different login type
215         # or org unit id
216         if ($authenticator->login_types and $args->{'type'}) {
217             next unless grep(/^(all|$args->{'type'})$/, @{$authenticator->{'login_types'}});
218         }
219         if ($authenticator->org_units and $args->{'org'}) {
220             next unless grep(/^(all|$args->{'org'})$/, @{$authenticator->{'org_units'}});
221         }
222
223         my $event;
224         # treat native specially
225         if ($authenticator->name eq 'native') {
226             $event = &_do_login($args);
227         } else {
228             $event = $authenticator->authenticate($args);
229         }
230         my $code = $U->event_code($event);
231         if ($code) {
232             push @error_events, $event;
233         } elsif (defined $code) { # code is '0', i.e. SUCCESS
234             if (exists $event->{'payload'}) { # we have a complete native login
235                 return $event;
236             } else { # do a 'forced' login
237                 return &_do_login($args, 1);
238             }
239         }
240     }
241
242     # if we got this far, we failed
243     # increment the brute force counter if 'native' didn't already
244     if ($args->{'username'} and !exists $authenticators_by_name{'native'}) {
245         $cache->put_cache('oils_auth_' . $args->{'username'} . '_count', ++$fail_count, $block_timeout);
246     }
247     # TODO: send back some form of collected error events
248     return OpenILS::Event->new( 'LOGIN_FAILED' );
249 }
250
251 sub _do_login {
252     my $args = shift;
253     my $authenticated = shift;
254
255     my $seeder = $args->{'username'} ? $args->{'username'} : $args->{'barcode'};
256     my $seed =
257       OpenSRF::AppSession->create("open-ils.auth")
258       ->request( 'open-ils.auth.authenticate.init', $seeder )->gather(1);
259
260     return OpenILS::Event->new( 'LOGIN_FAILED' )
261       unless $seed;
262
263     my $real_password = $args->{'password'};
264     # if we have already authenticated, look up the password needed to finish
265     if ($authenticated) {
266         # username is required
267         return OpenILS::Event->new( 'LOGIN_FAILED' ) if !$args->{'username'};
268         my $user = $U->cstorereq(
269             "open-ils.cstore.direct.actor.user.search.atomic",
270             { usrname => $args->{'username'} }
271         );
272         if (!$user->[0]) {
273             $logger->debug("Authenticated username '" . $args->{'username'} . "' has no Evergreen account, aborting");
274             return OpenILS::Event->new( 'LOGIN_FAILED' );
275         }
276         $args->{'password'} = md5_hex( $seed . $user->[0]->passwd );
277     } else {
278         $args->{'password'} = md5_hex( $seed . md5_hex($real_password) );
279     }
280     my $response = OpenSRF::AppSession->create("open-ils.auth")->request(
281         'open-ils.auth.authenticate.complete',
282         $args
283     )->gather(1);
284     $args->{'password'} = $real_password;
285
286     return OpenILS::Event->new( 'LOGIN_FAILED' )
287       unless $response;
288
289     return $response;
290 }
291
292 __PACKAGE__->register_method(
293     method    => "authenticators",
294     api_name  => "open-ils.auth_proxy.authenticators",
295     api_level => 1,
296     stream    => 1,
297     argc      => 1,
298     signature => {
299         desc => q/Get a list of viable authenticators/,
300         params => [
301             {name=> "args", desc => q/A hash of arguments.  Valid keys and their meanings:
302     type     := Type of login being attempted (Staff Client, OPAC, etc.).
303     org      := Org unit id
304 /,
305                 type => "hash"}
306         ],
307         return => {
308             desc => "List of viable authenticators",
309             type => "array"
310         }
311     }
312 );
313 sub authenticators {
314     my ( $self, $conn, $args ) = @_;
315
316     my @viable_auths;
317
318     foreach my $authenticator (@authenticators) {
319         # skip authenticators specified for a different login type
320         # or org unit id
321         if ($authenticator->login_types and $args->{'type'}) {
322             next unless grep(/^(all|$args->{'type'})$/, @{$authenticator->login_types});
323         }
324         if ($authenticator->org_units and $args->{'org'}) {
325             next unless grep(/^(all|$args->{'org'})$/, @{$authenticator->org_units});
326         }
327
328         push @viable_auths, $authenticator->name;
329     }
330
331     return \@viable_auths;
332 }
333
334
335 # --------------------------------------------------------------------------
336 # Stub package for 'native' authenticator
337 # --------------------------------------------------------------------------
338 package OpenILS::Application::AuthProxy::Native;
339 use strict; use warnings;
340 use base 'OpenILS::Application::AuthProxy::AuthBase';
341
342 1;