Line 0
Link Here
|
|
|
1 |
package Koha::Auth; |
2 |
|
3 |
# Copyright 2015 Vaara-kirjastot |
4 |
# |
5 |
# This file is part of Koha. |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it |
8 |
# under the terms of the GNU General Public License as published by |
9 |
# the Free Software Foundation; either version 3 of the License, or |
10 |
# (at your option) any later version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but |
13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
# GNU General Public License for more details. |
16 |
# |
17 |
# You should have received a copy of the GNU General Public License |
18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
19 |
|
20 |
#Define common packages |
21 |
use Modern::Perl; |
22 |
use Scalar::Util qw(blessed); |
23 |
use Try::Tiny; |
24 |
|
25 |
#Define Koha packages |
26 |
use Koha::Auth::RequestNormalizer; |
27 |
use Koha::Auth::Route::Password; |
28 |
use Koha::Auth::Route::Cookie; |
29 |
use Koha::Auth::Route::RESTV1; |
30 |
|
31 |
#Define Exceptions |
32 |
use Koha::Exception::BadParameter; |
33 |
use Koha::Exception::Logout; |
34 |
use Koha::Exception::UnknownProgramState; |
35 |
|
36 |
use Koha::Libraries; |
37 |
|
38 |
#Define the headers, POST-parameters and cookies extracted from the various web-frameworks' |
39 |
# request-objects and passed to the authentication system as normalized values. |
40 |
our @authenticationHeaders = ('X-Koha-Date', 'Authorization'); |
41 |
our @authenticationPOSTparams = ('password', 'userid', 'cardnumber', 'PT', 'branch', 'logout.x', 'koha_login_context'); |
42 |
our @authenticationCookies = ('CGISESSID'); #Really we should have only one of these. |
43 |
|
44 |
=head authenticate |
45 |
|
46 |
@PARAM3 HASHRef of authentication directives. Supported values: |
47 |
'inOPAC' => 1, #Authentication context is in OPAC |
48 |
'inREST' => 'v1', #Authentication context is in REST API V1 |
49 |
'inSC' => 1, #Authentication context is in the staff client |
50 |
'authnotrequired' => 1, #Disregard all Koha::Exception::LoginFailed||NoPermission-exceptions, |
51 |
#and authenticate as an anonymous user if normal authentication |
52 |
#fails. |
53 |
@THROWS Koha::Exception::VersionMismatch |
54 |
Koha::Exception::BadSystemPreference |
55 |
Koha::Exception::BadParameter |
56 |
Koha::Exception::ServiceTemporarilyUnavailable |
57 |
Koha::Exception::LoginFailed |
58 |
Koha::Exception::NoPermission |
59 |
Koha::Exception::Logout, catch this and redirect the request to the logout page. |
60 |
=cut |
61 |
|
62 |
sub authenticate { |
63 |
my ($controller, $permissions, $authParams) = @_; |
64 |
my $rae = _authenticate_validateAndNormalizeParameters(@_); #Get the normalized request authentication elements |
65 |
|
66 |
my $borrower; #Each authentication route returns a Koha::Patron-object on success. We use this to generate the Context() |
67 |
|
68 |
##Select the Authentication route. |
69 |
##Routes are introduced in priority order, and if one matches, the other routes are ignored. |
70 |
try { |
71 |
#0. Logout |
72 |
if ($rae->{postParams}->{'logout.x'}) { |
73 |
clearUserEnvironment($rae->{cookies}->{CGISESSID}, $authParams); |
74 |
Koha::Exception::Logout->throw(error => "User logged out. Please redirect me!"); |
75 |
} |
76 |
#1. Check for password authentication, including LDAP. |
77 |
if (not($borrower) && $rae->{postParams}->{koha_login_context} && ($rae->{postParams}->{userid} || $rae->{postParams}->{cardnumber}) && $rae->{postParams}->{password}) { |
78 |
$borrower = Koha::Auth::Route::Password::challenge($rae, $permissions, $authParams); |
79 |
} |
80 |
#2. Check for REST's signature-based authentication. |
81 |
#elsif ($rae->{headers}->{'Authorization'} && $rae->{headers}->{'Authorization'} =~ /Koha/) { |
82 |
if (not($borrower) && $rae->{headers}->{'Authorization'}) { |
83 |
$borrower = Koha::Auth::Route::RESTV1::challenge($rae, $permissions, $authParams); |
84 |
} |
85 |
#3. Check for the cookie. If cookies go stale, they block all subsequent authentication methods, so keep it down on this list. |
86 |
if (not($borrower) && $rae->{cookies}->{CGISESSID}) { |
87 |
$borrower = Koha::Auth::Route::Cookie::challenge($rae, $permissions, $authParams); |
88 |
} |
89 |
if (not($borrower)) { #HTTP CAS ticket or shibboleth or Persona not implemented |
90 |
#We don't know how to authenticate, or there is no authentication attempt. |
91 |
Koha::Exception::LoginFailed->throw(error => "Koha doesn't understand your authentication protocol."); |
92 |
} |
93 |
} catch { |
94 |
if (blessed($_)) { |
95 |
if ($_->isa('Koha::Exception::LoginFailed') || $_->isa('Koha::Exception::NoPermission')) { |
96 |
if ($authParams->{authnotrequired}) { #We failed to login, but we can continue anonymously. |
97 |
$borrower = Koha::Patron->new(); |
98 |
} |
99 |
else { |
100 |
$_->rethrow(); #Anonymous login not allowed this time |
101 |
} |
102 |
} |
103 |
else { |
104 |
die $_; #Propagate other errors to the calling Controller to redirect as it wants. |
105 |
} |
106 |
} |
107 |
else { |
108 |
die $_; #Not a Koha::Exception-object |
109 |
} |
110 |
}; |
111 |
|
112 |
my $session = setUserEnvironment($controller, $rae, $borrower, $authParams); |
113 |
my $cookie = Koha::Auth::RequestNormalizer::getSessionCookie($controller, $session); |
114 |
|
115 |
if ($ENV{KOHA_REST_API_DEBUG} > 2) { |
116 |
my @cc = caller(0); |
117 |
print "\n".$cc[3]."\nSESSIONID ".$session->id().", FIRSTNAME ".$session->param('firstname')."\n"; |
118 |
} |
119 |
|
120 |
return ($borrower, $cookie); |
121 |
} |
122 |
|
123 |
=head _authenticate_validateAndNormalizeParameters |
124 |
|
125 |
@PARAM1 CGI- or Mojolicious::Controller-object, this is used to identify which web framework to use. |
126 |
@PARAM2 HASHRef or undef, Permissions HASH telling which Koha permissions the user must have, to access the resource. |
127 |
@PARAM3 HASHRef or undef, Special authentication parameters, see authenticate() |
128 |
@THROWS Koha::Exception::BadParameter, if validating parameters fails. |
129 |
=cut |
130 |
|
131 |
sub _authenticate_validateAndNormalizeParameters { |
132 |
my ($controller, $permissions, $authParams) = @_; |
133 |
|
134 |
#Validate $controller. |
135 |
my $requestAuthElements; |
136 |
if (blessed($controller) && $controller->isa('CGI')) { |
137 |
$requestAuthElements = Koha::Auth::RequestNormalizer::normalizeCGI($controller, \@authenticationHeaders, \@authenticationPOSTparams, \@authenticationCookies); |
138 |
} |
139 |
elsif (blessed($controller) && $controller->isa('Mojolicious::Controller')) { |
140 |
$requestAuthElements = Koha::Auth::RequestNormalizer::normalizeMojolicious($controller, \@authenticationHeaders, \@authenticationPOSTparams, \@authenticationCookies); |
141 |
} |
142 |
else { |
143 |
Koha::Exception::BadParameter->throw(error => "Koha::Auth::authenticate():> The first parameter MUST be either a 'CGI'-object or a 'Mojolicious::Controller'-object"); |
144 |
} |
145 |
#Validate $permissions |
146 |
unless (not($permissions) || (ref $permissions eq 'HASH')) { |
147 |
Koha::Exception::BadParameter->throw(error => "Koha::Auth::authenticate():> The second parameter MUST be 'undef' or a HASHRef of Koha permissions. See C4::Auth::haspermission()."); |
148 |
} |
149 |
#Validate $authParams |
150 |
unless (not($authParams) || (ref $authParams eq 'HASH')) { |
151 |
Koha::Exception::BadParameter->throw(error => "Koha::Auth::authenticate():> The third parameter MUST be 'undef' or a HASHRef."); |
152 |
} |
153 |
|
154 |
return $requestAuthElements; |
155 |
} |
156 |
|
157 |
=head setUserEnvironment |
158 |
Set the C4::Context::user_env() and CGI::Session. |
159 |
|
160 |
Any idea why there is both the CGI::Session and C4::Context::usernenv?? |
161 |
=cut |
162 |
|
163 |
sub setUserEnvironment { |
164 |
my ($controller, $rae, $borrower, $authParams) = @_; |
165 |
|
166 |
my $session = C4::Auth::get_session( $rae->{cookies}->{CGISESSID} || '' ); |
167 |
if ($rae->{postParams} && $rae->{postParams}->{koha_login_context} && $rae->{postParams}->{koha_login_context} eq 'REST' && |
168 |
(not($session->param('koha_login_context')) || $session->param('koha_login_context') ne 'REST') #Make sure we dont create new Sessions for users who want to login many times in a row. |
169 |
) { |
170 |
#We are logging in a user using the REST API, so we need to create a new session context outside of the usual CGISESSID-cookie |
171 |
$session = C4::Auth::get_session(); |
172 |
$session->param( 'koha_login_context', $rae->{postParams}->{koha_login_context} ); |
173 |
} |
174 |
|
175 |
C4::Context->_new_userenv( $session->id ); |
176 |
|
177 |
_determineUserBranch($rae, $borrower, $authParams, $session); |
178 |
|
179 |
#Then start setting remaining session parameters |
180 |
$session->param( 'number', $borrower->borrowernumber ); |
181 |
$session->param( 'id', $borrower->userid ); |
182 |
$session->param( 'cardnumber', $borrower->cardnumber ); |
183 |
$session->param( 'firstname', $borrower->firstname ); |
184 |
$session->param( 'surname', $borrower->surname ); |
185 |
$session->param( 'emailaddress', $borrower->email ); |
186 |
#originIps contain all the IP's this request has been proxied through. |
187 |
#Get the last value. This is in line with how the CGI-layer deals with IP-based authentication. |
188 |
$session->param( 'ip', $rae->{originIps}->[ -1 ] ); |
189 |
$session->param( 'lasttime', time() ); |
190 |
$session->flush(); #CGI::Session recommends to flush since auto-flush is not guaranteed. |
191 |
|
192 |
#Finally configure the userenv. |
193 |
C4::Context->set_userenv( |
194 |
$session->param('number'), $session->param('id'), |
195 |
$session->param('cardnumber'), $session->param('firstname'), |
196 |
$session->param('surname'), $session->param('branch'), |
197 |
$session->param('branchname'), undef, |
198 |
$session->param('emailaddress'), $session->param('branchprinter'), |
199 |
$session->param('persona'), $session->param('shibboleth') |
200 |
); |
201 |
|
202 |
return $session; |
203 |
} |
204 |
|
205 |
sub _determineUserBranch { |
206 |
my ($rae, $borrower, $authParams, $session) = @_; |
207 |
|
208 |
my ($branchcode, $branchname); |
209 |
if ($rae->{postParams}->{branch}) { |
210 |
#We are instructed to change the active branch |
211 |
$branchcode = $rae->{postParams}->{branch}; |
212 |
} |
213 |
elsif ($session->param('branch') && $session->param('branch') ne 'NO_LIBRARY_SET') { |
214 |
##Branch is already set |
215 |
$branchcode = $session->param('branch'); |
216 |
} |
217 |
elsif ($borrower->branchcode) { |
218 |
#Default to the borrower's branch |
219 |
$branchcode = $borrower->branchcode; |
220 |
} |
221 |
else { |
222 |
#No borrower branch? This must be the superuser. |
223 |
$branchcode = 'NO_LIBRARY_SET'; |
224 |
$branchname = 'NO_LIBRARY_SET'; |
225 |
} |
226 |
unless ($branchname) { |
227 |
my $library = Koha::Libraries->find($branchcode); |
228 |
$branchname = $library->branchname if $library; |
229 |
} |
230 |
$session->param( 'branch', $branchcode ); |
231 |
$session->param( 'branchname', ($branchname || 'NO_LIBRARY_SET')); |
232 |
} |
233 |
|
234 |
=head clearUserEnvironment |
235 |
|
236 |
Removes the active authentication |
237 |
|
238 |
=cut |
239 |
|
240 |
sub clearUserEnvironment { |
241 |
my ($sessionid, $authParams) = @_; |
242 |
|
243 |
my $session; |
244 |
unless (blessed($sessionid)) { |
245 |
$session = C4::Auth::get_session( $sessionid ); |
246 |
} |
247 |
else { |
248 |
$session = $sessionid; |
249 |
} |
250 |
|
251 |
if (C4::Context->userenv()) { |
252 |
C4::Context::_unset_userenv( $session->id ); |
253 |
} |
254 |
$session->delete(); |
255 |
$session->flush(); |
256 |
} |
257 |
|
258 |
1; |