|
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 C4::Branch; |
| 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', '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::Borrower-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, $authParams); |
| 74 |
Koha::Exception::Logout->throw(error => "User logged out. Please redirect me!"); |
| 75 |
} |
| 76 |
#1. Check for password authentication, including LDAP. |
| 77 |
elsif ($rae->{postParams}->{koha_login_context} && $rae->{postParams}->{userid} && $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 |
elsif ($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 |
elsif ($rae->{cookies}->{CGISESSID}) { |
| 87 |
$borrower = Koha::Auth::Route::Cookie::challenge($rae, $permissions, $authParams); |
| 88 |
} |
| 89 |
else { #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::Borrower->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 |
return ($borrower, $cookie); |
| 116 |
} |
| 117 |
|
| 118 |
=head _authenticate_validateAndNormalizeParameters |
| 119 |
|
| 120 |
@PARAM1 CGI- or Mojolicious::Controller-object, this is used to identify which web framework to use. |
| 121 |
@PARAM2 HASHRef or undef, Permissions HASH telling which Koha permissions the user must have, to access the resource. |
| 122 |
@PARAM3 HASHRef or undef, Special authentication parameters, see authenticate() |
| 123 |
@THROWS Koha::Exception::BadParameter, if validating parameters fails. |
| 124 |
=cut |
| 125 |
|
| 126 |
sub _authenticate_validateAndNormalizeParameters { |
| 127 |
my ($controller, $permissions, $authParams) = @_; |
| 128 |
|
| 129 |
#Validate $controller. |
| 130 |
my $requestAuthElements; |
| 131 |
if (blessed($controller) && $controller->isa('CGI')) { |
| 132 |
$requestAuthElements = Koha::Auth::RequestNormalizer::normalizeCGI($controller, \@authenticationHeaders, \@authenticationPOSTparams, \@authenticationCookies); |
| 133 |
} |
| 134 |
elsif (blessed($controller) && $controller->isa('Mojolicious::Controller')) { |
| 135 |
$requestAuthElements = Koha::Auth::RequestNormalizer::normalizeMojolicious($controller, \@authenticationHeaders, \@authenticationPOSTparams, \@authenticationCookies); |
| 136 |
} |
| 137 |
else { |
| 138 |
Koha::Exception::BadParameter->throw(error => "Koha::Auth::authenticate():> The first parameter MUST be either a 'CGI'-object or a 'Mojolicious::Controller'-object"); |
| 139 |
} |
| 140 |
#Validate $permissions |
| 141 |
unless (not($permissions) || (ref $permissions eq 'HASH')) { |
| 142 |
Koha::Exception::BadParameter->throw(error => "Koha::Auth::authenticate():> The second parameter MUST be 'undef' or a HASHRef of Koha permissions. See C4::Auth::haspermission()."); |
| 143 |
} |
| 144 |
#Validate $authParams |
| 145 |
unless (not($authParams) || (ref $authParams eq 'HASH')) { |
| 146 |
Koha::Exception::BadParameter->throw(error => "Koha::Auth::authenticate():> The third parameter MUST be 'undef' or a HASHRef."); |
| 147 |
} |
| 148 |
|
| 149 |
return $requestAuthElements; |
| 150 |
} |
| 151 |
|
| 152 |
=head setUserEnvironment |
| 153 |
Set the C4::Context::user_env() and CGI::Session. |
| 154 |
|
| 155 |
Any idea why there is both the CGI::Session and C4::Context::usernenv?? |
| 156 |
=cut |
| 157 |
|
| 158 |
sub setUserEnvironment { |
| 159 |
my ($controller, $rae, $borrower, $authParams) = @_; |
| 160 |
|
| 161 |
my $session = C4::Auth::get_session( $rae->{cookies}->{CGISESSID} || '' ); |
| 162 |
C4::Context->_new_userenv( $session->id ); |
| 163 |
|
| 164 |
_determineUserBranch($rae, $borrower, $authParams, $session); |
| 165 |
|
| 166 |
#Then start setting remaining session parameters |
| 167 |
$session->param( 'number', $borrower->borrowernumber ); |
| 168 |
$session->param( 'id', $borrower->userid ); |
| 169 |
$session->param( 'cardnumber', $borrower->cardnumber ); |
| 170 |
$session->param( 'firstname', $borrower->firstname ); |
| 171 |
$session->param( 'surname', $borrower->surname ); |
| 172 |
$session->param( 'emailaddress', $borrower->email ); |
| 173 |
$session->param( 'ip', $session->remote_addr() ); |
| 174 |
$session->param( 'lasttime', time() ); |
| 175 |
|
| 176 |
#Finally configure the userenv. |
| 177 |
C4::Context->set_userenv( |
| 178 |
$session->param('number'), $session->param('id'), |
| 179 |
$session->param('cardnumber'), $session->param('firstname'), |
| 180 |
$session->param('surname'), $session->param('branch'), |
| 181 |
$session->param('branchname'), undef, |
| 182 |
$session->param('emailaddress'), $session->param('branchprinter'), |
| 183 |
$session->param('persona'), $session->param('shibboleth') |
| 184 |
); |
| 185 |
|
| 186 |
return $session; |
| 187 |
} |
| 188 |
|
| 189 |
sub _determineUserBranch { |
| 190 |
my ($rae, $borrower, $authParams, $session) = @_; |
| 191 |
|
| 192 |
my ($branchcode, $branchname); |
| 193 |
if ($rae->{postParams}->{branch}) { |
| 194 |
#We are instructed to change the active branch |
| 195 |
$branchcode = $rae->{postParams}->{branch}; |
| 196 |
} |
| 197 |
elsif ($session->param('branch') && $session->param('branch') ne 'NO_LIBRARY_SET') { |
| 198 |
##Branch is already set |
| 199 |
$branchcode = $session->param('branch'); |
| 200 |
} |
| 201 |
elsif ($borrower->branchcode) { |
| 202 |
#Default to the borrower's branch |
| 203 |
$branchcode = $borrower->branchcode; |
| 204 |
} |
| 205 |
else { |
| 206 |
#No borrower branch? This must be the superuser. |
| 207 |
$branchcode = 'NO_LIBRARY_SET'; |
| 208 |
$branchname = 'NO_LIBRARY_SET'; |
| 209 |
} |
| 210 |
$session->param( 'branch', $branchcode ); |
| 211 |
$session->param( 'branchname', ($branchname || C4::Branch::GetBranchName($branchcode) || 'NO_LIBRARY_SET')); |
| 212 |
} |
| 213 |
|
| 214 |
=head clearUserEnvironment |
| 215 |
|
| 216 |
Removes all active authentications |
| 217 |
=cut |
| 218 |
|
| 219 |
sub clearUserEnvironment { |
| 220 |
my ($rae, $authParams) = @_; |
| 221 |
|
| 222 |
my $session = C4::Auth::get_session( $rae->{cookies}->{CGISESSID} ); |
| 223 |
$session->delete(); |
| 224 |
$session->flush; |
| 225 |
#Do we need to unset this if it has never been set? C4::Context::_unset_userenv( $rae->{cookies}->{CGISESSID} ); |
| 226 |
} |
| 227 |
1; |