|
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::Route::Password; |
| 27 |
use Koha::Auth::Route::Cookie; |
| 28 |
|
| 29 |
#Define Exceptions |
| 30 |
use Koha::Exception::BadParameter; |
| 31 |
|
| 32 |
#Define the headers, POST-parameters and cookies extracted from the various web-frameworks' |
| 33 |
# request-objects and passed to the authentication system as normalized values. |
| 34 |
our @authenticationHeaders = ('X-Koha-Signature', 'ETag', 'X-Koha-Username'); |
| 35 |
our @authenticationPOSTparams = ('password', 'userid', 'PT', 'branch'); |
| 36 |
our @authenticationCookies = ('CGISESSID'); #Really we should have only one of these. |
| 37 |
|
| 38 |
=head authenticate |
| 39 |
|
| 40 |
@PARAM3 HASHRef of authentication directives. Supported values: |
| 41 |
'inOPAC' => 1, #Authentication context is in OPAC |
| 42 |
'inREST' => 'v1', #Authentication context is in REST API V1 |
| 43 |
'authnotrequired' => 1, #Disregard all Koha::Exception::LoginFailed-exceptions, |
| 44 |
#and authenticate as an anonymous user. |
| 45 |
=cut |
| 46 |
|
| 47 |
sub authenticate { |
| 48 |
my ($controller, $permissions, $authParams) = @_; |
| 49 |
my ($headers, $postParams, $cookies) = _authenticate_validateAndNormalizeParameters(@_); |
| 50 |
|
| 51 |
my $borrower; #Each authentication route returns a Koha::Borrower-object on success. We use this to generate the Context() |
| 52 |
|
| 53 |
##Select the Authentication route. |
| 54 |
##Routes are introduced in priority order, and if one matches, the other routes are ignored. |
| 55 |
try { |
| 56 |
#1. Check for password authentication, including LDAP. |
| 57 |
if ($postParams->{userid} && $postParams->{password}) { |
| 58 |
$borrower = Koha::Auth::Route::Password::challenge($headers, $postParams, $cookies, $permissions, $authParams); |
| 59 |
} |
| 60 |
#2. Check for REST's signature-based authentication. |
| 61 |
elsif ($headers->{'X-Koha-Signature'}) { |
| 62 |
$borrower = Koha::Auth::Route::REST::V1::check($headers, $postParams, $cookies, $permissions, $authParams); |
| 63 |
} |
| 64 |
#3. Check for the cookie. If cookies go stale, they block all subsequent authentication methods, so keep it down. |
| 65 |
elsif ($cookies) { |
| 66 |
$borrower = Koha::Auth::Route::Cookie::challenge($headers, $postParams, $cookies, $permissions, $authParams); |
| 67 |
} |
| 68 |
else { #HTTP CAS ticket or shibboleth or Persona not implemented |
| 69 |
##Backwards compatibility: give the fallback signal to use the legacy authentication mechanism |
| 70 |
return 'FALLBACK'; |
| 71 |
} |
| 72 |
} catch { |
| 73 |
if (blessed($_)) { |
| 74 |
if ($_->isa('Koha::Exception::LoginFailed')) { |
| 75 |
if ($authParams->{authnotrequired}) { #We failed to login, but we can continue anonymously. |
| 76 |
$borrower = Koha::Borrower->new(); |
| 77 |
} |
| 78 |
else { |
| 79 |
$_->rethrow(); #Anonymous login not allowed this time |
| 80 |
} |
| 81 |
} |
| 82 |
else { |
| 83 |
$_->rethrow(); #Propagate other errors to the calling Controller to redirect as it wants. |
| 84 |
} |
| 85 |
} |
| 86 |
else { |
| 87 |
die $_; #Not a Koha::Exception-object |
| 88 |
} |
| 89 |
}; |
| 90 |
|
| 91 |
setUserEnvironment($headers, $postParams, $cookies, $borrower, $authParams); |
| 92 |
} |
| 93 |
|
| 94 |
=head _authenticate_validateAndNormalizeParameters |
| 95 |
|
| 96 |
@THROWS Koha::Exception::BadParameter, if validating parameters fails. |
| 97 |
=cut |
| 98 |
|
| 99 |
sub _authenticate_validateAndNormalizeParameters { |
| 100 |
my ($controller, $permissions, $authParams) = @_; |
| 101 |
|
| 102 |
#Validate $controller. |
| 103 |
my ($headers, $postParams, $cookies); |
| 104 |
if (blessed($controller) && $controller->isa('CGI')) { |
| 105 |
($headers, $postParams, $cookies) = _normalizeCGI($controller); |
| 106 |
} |
| 107 |
elsif (blessed($controller) && $controller->isa('Mojolicious::Controller')) { |
| 108 |
($headers, $postParams, $cookies) = _normalizeMojolicious($controller); |
| 109 |
} |
| 110 |
else { |
| 111 |
Koha::Exception::BadParameter->throw(error => "Koha::Auth::authenticate():> The first parameter MUST be either a 'CGI'-object or a 'Mojolicious::Controller'-object"); |
| 112 |
} |
| 113 |
#Validate $permissions |
| 114 |
unless (not($permissions) || (ref $permissions eq 'HASH')) { |
| 115 |
Koha::Exception::BadParameter->throw(error => "Koha::Auth::authenticate():> The second parameter MUST be 'undef' or a HASHRef of Koha permissions. See C4::Auth::haspermission()."); |
| 116 |
} |
| 117 |
#Validate $authParams |
| 118 |
unless (not($authParams) || (ref $authParams eq 'HASH')) { |
| 119 |
Koha::Exception::BadParameter->throw(error => "Koha::Auth::authenticate():> The third parameter MUST be 'undef' or a HASHRef."); |
| 120 |
} |
| 121 |
|
| 122 |
return ($headers, $postParams, $cookies); |
| 123 |
} |
| 124 |
|
| 125 |
=head _normalizeCGI |
| 126 |
Takes a CGI-object and finds the authentication markers from it. |
| 127 |
@PARAM1 CGI-object. |
| 128 |
@RETURNS List of : HASHRef of headers required for authentication, or undef |
| 129 |
HASHRef of POST parameters required for authentication, or undef |
| 130 |
String of the authenticaton cookie value, or undef |
| 131 |
=cut |
| 132 |
|
| 133 |
sub _normalizeCGI { |
| 134 |
my ($controller) = @_; |
| 135 |
|
| 136 |
my ($headers, $postParams, $cookies); |
| 137 |
foreach my $authHeader (@authenticationHeaders) { |
| 138 |
if (my $val = $controller->http($authHeader)) { |
| 139 |
$headers->{$authHeader} = $val; |
| 140 |
} |
| 141 |
} |
| 142 |
foreach my $authParam (@authenticationPOSTparams) { |
| 143 |
if (my $val = $controller->param($authParam)) { |
| 144 |
$postParams->{$authParam} = $val; |
| 145 |
} |
| 146 |
} |
| 147 |
foreach my $authCookie (@authenticationCookies) { |
| 148 |
if (my $val = $controller->cookie($authCookie)) { |
| 149 |
$cookies->{$authCookie} = $val; |
| 150 |
} |
| 151 |
} |
| 152 |
return ($headers, $postParams, $cookies); |
| 153 |
} |
| 154 |
|
| 155 |
=head _normalizeMojolicious |
| 156 |
Takes a Mojolicious::Controller-object and finds the authentication markers from it. |
| 157 |
@PARAM1 Mojolicious::Controller-object. |
| 158 |
@RETURNS List of : HASHRef of headers required for authentication, or undef |
| 159 |
HASHRef of POST parameters required for authentication, or undef |
| 160 |
String of the authenticaton cookie value, or undef |
| 161 |
=cut |
| 162 |
|
| 163 |
sub _normalizeMojolicious { |
| 164 |
my ($controller) = @_; |
| 165 |
|
| 166 |
my $request = $controller->req(); |
| 167 |
my ($headers, $postParams, $cookies); |
| 168 |
if (blessed($controller) && $controller->isa('CGI')) { |
| 169 |
my $headers = $request->headers(); |
| 170 |
foreach my $authHeader (@authenticationHeaders) { |
| 171 |
if (my $val = $request->headers()->$authHeader()) { |
| 172 |
$headers->{$authHeader} = $val; |
| 173 |
} |
| 174 |
} |
| 175 |
foreach my $authParam (@authenticationPOSTparams) { |
| 176 |
if (my $val = $request->param($authParam)) { |
| 177 |
$postParams->{$authParam} = $val; |
| 178 |
} |
| 179 |
} |
| 180 |
foreach my $authCookie (@authenticationCookies) { |
| 181 |
if (my $val = $request->cookies($authCookie)) { |
| 182 |
$cookies->{$authCookie} = $val; |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
return ($headers, $postParams, $cookies); |
| 187 |
} |
| 188 |
|
| 189 |
=head setUserEnvironment |
| 190 |
Set the C4::Context::user_env() and CGI::Session. |
| 191 |
|
| 192 |
Any idea why there is both the CGI::Session and C4::Context::usernenv?? |
| 193 |
=cut |
| 194 |
|
| 195 |
sub setUserEnvironment { |
| 196 |
my ($headers, $postParams, $cookies, $borrower, $authParams) = @_; |
| 197 |
|
| 198 |
my $session = C4::Auth::get_session( $cookies->{CGISESSID} ); |
| 199 |
C4::Context->_new_userenv( $cookies->{CGISESSID} ); |
| 200 |
|
| 201 |
#First figure out the branch we are at. |
| 202 |
if ($postParams->{branch}) { |
| 203 |
$session->param( 'branch', $postParams->{branch} ); |
| 204 |
$session->param( 'branchname', GetBranchName($postParams->{branch})); |
| 205 |
} |
| 206 |
elsif ($session->param('branch')) { |
| 207 |
##Branch is already set so no reason to change it. |
| 208 |
} |
| 209 |
else { |
| 210 |
$session->param( 'branch', 'NO_LIBRARY_SET' ); |
| 211 |
$session->param( 'branchname', 'NO_LIBRARY_SET' ); |
| 212 |
} |
| 213 |
#Then start setting remaining session parameters |
| 214 |
$session->param( 'number', $borrower->borrowernumber ); |
| 215 |
$session->param( 'id', $borrower->userid ); |
| 216 |
$session->param( 'cardnumber', $borrower->cardnumber ); |
| 217 |
$session->param( 'firstname', $borrower->firstname ); |
| 218 |
$session->param( 'surname', $borrower->surname ); |
| 219 |
$session->param( 'branch', $postParams->{branch} ) if $postParams->{branch}; |
| 220 |
$session->param( 'branchname', GetBranchName($postParams->{branch})) if $postParams->{branch}; |
| 221 |
$session->param( 'flags', $borrower->flags ); |
| 222 |
$session->param( 'emailaddress', $borrower->email ); |
| 223 |
$session->param( 'ip', $session->remote_addr() ); |
| 224 |
$session->param( 'lasttime', time() ); |
| 225 |
|
| 226 |
#Finally configure the userenv. |
| 227 |
C4::Context->set_userenv( |
| 228 |
$session->param('number'), $session->param('id'), |
| 229 |
$session->param('cardnumber'), $session->param('firstname'), |
| 230 |
$session->param('surname'), $session->param('branch'), |
| 231 |
$session->param('branchname'), $session->param('flags'), |
| 232 |
$session->param('emailaddress'), $session->param('branchprinter'), |
| 233 |
$session->param('persona'), $session->param('shibboleth') |
| 234 |
); |
| 235 |
} |
| 236 |
1; |