]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/RemoteAuth.pm
LP#1817645: configurable HTTP API for patron auth/retrieval
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / WWW / RemoteAuth.pm
1 # Copyright (C) 2019 BC Libraries Cooperative
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 # ====================================================================== 
18 # - base class for configurable HTTP API for patron auth/retrieval
19 # - provides generic methods shared by all handler subclasses
20 # - handlers take care of endpoint-specific implementation details
21 # ======================================================================
22
23 package OpenILS::WWW::RemoteAuth;
24 use strict; use warnings;
25 use Apache2::Const -compile => qw(OK DECLINED FORBIDDEN AUTH_REQUIRED HTTP_INTERNAL_SERVER_ERROR REDIRECT HTTP_BAD_REQUEST);
26 use DateTime::Format::ISO8601;
27
28 use OpenSRF::EX qw(:try);
29 use OpenSRF::Utils::Logger qw/$logger/;
30 use OpenSRF::System;
31 use OpenILS::Utils::CStoreEditor qw/:funcs/;
32 use OpenILS::Application::AppUtils;
33 our $U = "OpenILS::Application::AppUtils";
34
35 my $bootstrap_config;
36 my @handlers_to_preinit = ();
37
38 sub editor {
39     my ($self, $editor) = @_;
40     $self->{editor} = $editor if $editor;
41     return $self->{editor};
42 }
43
44 sub config {
45     my ($self, $config) = @_;
46     $self->{config} = $config if $config;
47     return $self->{config};
48 }
49
50 sub import {
51     my ($self, $bootstrap_config, $handlers) = @_;
52     @handlers_to_preinit = split /\s+/, $handlers, -1 if defined($handlers);
53 }
54
55 sub child_init {
56     OpenSRF::System->bootstrap_client(config_file => $bootstrap_config);
57     my $idl = OpenSRF::Utils::SettingsClient->new->config_value("IDL");
58     Fieldmapper->import(IDL => $idl);
59     OpenILS::Utils::CStoreEditor->init;
60     foreach my $module (@handlers_to_preinit) {
61         eval {
62             $module->use;
63         };
64     }
65     return Apache2::Const::OK;
66 }
67
68 sub handler {
69     my $r = shift;
70
71     my $stat = Apache2::Const::AUTH_REQUIRED;
72
73     # load the appropriate module and process our request
74     try {
75         my $module = $r->dir_config('OILSRemoteAuthHandler');
76         $module->use;
77         my $handler = $module->new;
78         $stat = $handler->process($r);
79     } catch Error with {
80         $logger->error("processing RemoteAuth handler failed: @_");
81         $stat = Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
82     };
83
84     return $stat;
85 }
86
87 sub load_config {
88     my ($self, $e, $r) = @_;
89
90     # name to use for config lookup
91     my $name = $r->dir_config('OILSRemoteAuthProfile');
92     return undef unless $name;
93
94     # load config
95     my $config = $e->retrieve_config_remoteauth_profile($name);
96     if ($config and $U->is_true($config->enabled)) {
97         return $config;
98     }
99     $logger->info("RemoteAuth: config profile $name not found (or not enabled)");
100     return undef;
101 }
102
103 sub do_client_auth {
104     my ($self, $client_username, $client_password) = @_;
105     my $login_resp = $U->simplereq(
106         'open-ils.auth',
107         'open-ils.auth.login', {
108             username => $client_username,
109             password => $client_password,
110             type => 'staff'
111         }   
112     );
113     if ($login_resp->{textcode} eq 'SUCCESS') {
114         return $login_resp->{payload}->{authtoken};
115     }
116     $logger->info("RemoteAuth: failed to authenticate client $client_username");
117     return undef;
118 }
119
120 sub do_patron_auth {
121     my ($self, $e, $config, $id, $password) = @_;
122     my $org_unit = $config->context_org;
123
124     return $self->backend_error unless $e->checkauth;
125
126     # XXX
127     my $args = {
128         type => 'opac',
129         org => $org_unit,
130         identifier => $id,
131         password => $password,
132         agent => 'remoteauth'
133     };
134
135     my $response = $U->simplereq(
136         'open-ils.auth',
137         'open-ils.auth.login', $args);
138     if($U->event_code($response)) { 
139         $logger->info("RemoteAuth: failed to authenticate user $id at org unit $org_unit");
140         return $self->patron_not_authenticated;
141     }
142
143     # get basic patron info via user authtoken
144     my $authtoken = $response->{payload}->{authtoken};
145     my $user = $U->simplereq(
146         'open-ils.auth',
147         'open-ils.auth.session.retrieve', $authtoken);
148     if (!$user or $U->event_code($user)) {
149         $logger->error("RemoteAuth: failed to retrieve user for session $authtoken");
150         return $self->backend_error;
151     }
152     my $userid = $user->id;
153     my $home_ou = $user->home_ou;
154
155     unless ($e->allowed('VIEW_USER', $home_ou)) {
156         $logger->info("RemoteAuth: client does not have permission to view user $userid");
157         return $self->client_not_authorized;
158     }
159
160     # do basic validation (and skip the permit test where applicable)
161     if ($U->is_true($user->deleted)) {
162         $logger->info("RemoteAuth: user $userid is deleted");
163         return $self->patron_not_found;
164     }
165
166     if ($U->is_true($user->barred)) {
167         $logger->info("RemoteAuth: user $userid is barred");
168         return $self->patron_is_blocked;
169     }
170
171     # check if remoteauth is permitted for this user
172     my $permit_test = $e->json_query(
173         {from => ['actor.permit_remoteauth', $config->name, $userid]}
174     )->[0]{'actor.permit_remoteauth'};;
175
176     if ($permit_test eq 'success') {
177         return $self->success($user);
178     } elsif ($permit_test eq 'not_found') {
179         return $self->patron_not_found;
180     } elsif ($permit_test eq 'expired') {
181         return $self->patron_is_expired;
182     } else {
183         return $self->patron_is_blocked;
184     }
185 }
186
187 # Dummy methods for responding to the client based on
188 # different error (or success) conditions.
189 # The handler will normally want to override these methods
190 # with its own version of them.
191
192 # patron auth succeeded
193 sub success {
194     return Apache2::Const::OK;
195 }
196
197 # generic backend error
198 sub backend_error {
199     return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
200 }
201
202 # client error (e.g. missing params)
203 sub client_error {
204     return Apache2::Const::HTTP_BAD_REQUEST;
205 }
206
207 # client auth failed
208 sub client_not_authorized {
209     return Apache2::Const::AUTH_REQUIRED;
210 }
211
212 # patron auth failed (bad password etc)
213 sub patron_not_authenticated {
214     return Apache2::Const::FORBIDDEN;
215 }
216
217 # patron does not exist or is inactive/deleted
218 sub patron_not_found {
219     return Apache2::Const::DECLINED;
220 }
221
222 # patron is barred or has blocking penalties
223 sub patron_is_blocked {
224     return Apache2::Const::FORBIDDEN;
225 }
226
227 # patron is expired
228 sub patron_is_expired {
229     return Apache2::Const::DECLINED;
230 }
231
232 1;
233