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.
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.
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.
19 OpenILS::Application::AuthProxy - Negotiator for proxy-style authentication
23 Dan Wells, dbw2@calvin.edu
27 package OpenILS::Application::AuthProxy;
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;
39 use UNIVERSAL::require;
40 use Digest::MD5 qw/md5_hex/;
41 my $U = 'OpenILS::Application::AppUtils';
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.
47 my %authenticators_by_name;
48 my $enabled = 'false';
55 my $conf = OpenSRF::Utils::SettingsClient->new;
56 $cache = OpenSRF::Utils::Cache->new();
58 my @pfx = ( "apps", "open-ils.auth", "app_settings", "auth_limits" );
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);
68 @pfx = ( "apps", "open-ils.auth_proxy", "app_settings" );
70 $enabled = $conf->config_value( @pfx, 'enabled' );
72 my $auth_configs = $conf->config_value( @pfx, 'authenticators', 'authenticator' );
73 $auth_configs = [$auth_configs] if ref($auth_configs) eq 'HASH';
75 if ( !@$auth_configs ) {
76 $logger->error("AuthProxy: authenticators list not found!");
78 foreach my $auth_config (@$auth_configs) {
80 if ($auth_config->{'name'} eq 'native') {
81 $auth_handler = 'OpenILS::Application::AuthProxy::Native';
83 $auth_handler = $auth_config->{module};
84 next unless $auth_handler;
86 $logger->debug("Attempting to load AuthProxy handler: $auth_handler");
89 $logger->error("Unable to load AuthProxy handler [$auth_handler]: $@");
94 &_make_option_array($auth_config, 'login_types', 'type');
95 &_make_option_array($auth_config, 'org_units', 'unit');
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");
102 $logger->debug("AuthProxy: authenticators loaded");
106 # helper function to simplify the config structure
107 sub _make_option_array {
108 my ($auth_config, $container_name, $node_name) = @_;
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};
114 if (ref $nodes ne 'ARRAY') {
115 $auth_config->{$container_name} = [$nodes];
117 $auth_config->{$container_name} = $nodes;
120 delete $auth_config->{$container_name};
123 delete $auth_config->{$container_name};
129 __PACKAGE__->register_method(
131 api_name => "open-ils.auth_proxy.enabled",
136 desc => q/Check if AuthProxy is enabled/,
138 desc => "True if enabled, false if not",
144 return (!$enabled or $enabled eq 'false') ? 0 : 1;
147 __PACKAGE__->register_method(
149 api_name => "open-ils.auth_proxy.login",
154 desc => q/Basic single-factor login method/,
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.).
166 desc => "Authentication seed or failure event",
172 my ( $self, $conn, $args ) = @_;
174 return OpenILS::Event->new( 'LOGIN_FAILED' )
175 unless (&enabled() and ($args->{'username'} or $args->{'barcode'}));
177 # check for possibility of brute-force attack
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' );
191 my $authenticated = 0;
194 # if they specify an authenticator by name, only try that one
195 if ($args->{'name'}) {
196 $auths = [$authenticators_by_name{$args->{'name'}}];
198 $auths = \@authenticators;
201 foreach my $authenticator (@$auths) {
202 # skip authenticators specified for a different login type
204 if ($authenticator->login_types and $args->{'type'}) {
205 next unless grep(/^(all|$args->{'type'})$/, @{$authenticator->{'login_types'}});
207 if ($authenticator->org_units and $args->{'org'}) {
208 next unless grep(/^(all|$args->{'org'})$/, @{$authenticator->{'org_units'}});
212 # treat native specially
213 if ($authenticator->name eq 'native') {
214 $event = &_do_login($args);
216 $event = $authenticator->authenticate($args);
218 my $code = $U->event_code($event);
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
224 } else { # do a 'forced' login
225 return &_do_login($args, 1);
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);
235 # TODO: send back some form of collected error events
236 return OpenILS::Event->new( 'LOGIN_FAILED' );
241 my $authenticated = shift;
243 my $seeder = $args->{'username'} ? $args->{'username'} : $args->{'barcode'};
245 OpenSRF::AppSession->create("open-ils.auth")
246 ->request( 'open-ils.auth.authenticate.init', $seeder )->gather(1);
248 return OpenILS::Event->new( 'LOGIN_FAILED' )
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'} }
260 $args->{'password'} = md5_hex( $seed . $user->[0]->passwd );
262 $args->{'password'} = md5_hex( $seed . md5_hex($real_password) );
264 my $response = OpenSRF::AppSession->create("open-ils.auth")->request(
265 'open-ils.auth.authenticate.complete',
268 $args->{'password'} = $real_password;
270 return OpenILS::Event->new( 'LOGIN_FAILED' )
276 __PACKAGE__->register_method(
277 method => "authenticators",
278 api_name => "open-ils.auth_proxy.authenticators",
283 desc => q/Get a list of viable authenticators/,
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.).
292 desc => "List of viable authenticators",
298 my ( $self, $conn, $args ) = @_;
302 foreach my $authenticator (@authenticators) {
303 # skip authenticators specified for a different login type
305 if ($authenticator->login_types and $args->{'type'}) {
306 next unless grep(/^(all|$args->{'type'})$/, @{$authenticator->login_types});
308 if ($authenticator->org_units and $args->{'org'}) {
309 next unless grep(/^(all|$args->{'org'})$/, @{$authenticator->org_units});
312 push @viable_auths, $authenticator->name;
315 return \@viable_auths;
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';