1 package C4
::Auth_with_cas
;
3 # Copyright 2009 BibLibre SARL
5 # This file is part of Koha.
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.
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.
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>.
25 use Koha
::AuthUtils
qw(get_script_name);
26 use Authen
::CAS
::Client
;
32 use vars
qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $debug);
38 @EXPORT = qw(check_api_auth_cas checkpw_cas login_cas logout_cas login_cas_url);
42 my $yamlauthfile = C4
::Context
->config('intranetdir') . "/C4/Auth_cas_servers.yaml";
45 # If there's a configuration for multiple cas servers, then we get it
47 ($defaultcasserver, $casservers) = YAML
::LoadFile
($yamlauthfile);
48 $defaultcasserver = $defaultcasserver->{'default'};
50 # Else, we fall back to casServerUrl syspref
51 $defaultcasserver = 'default';
52 $casservers = { 'default' => C4
::Context
->preference('casServerUrl') };
55 # Is there a configuration file for multiple cas servers?
57 return (-e
qq($yamlauthfile));
60 # Returns configured CAS servers' list if multiple authentication is enabled
67 my ($query, $type) = @_;
68 my ( $cas, $uri ) = _get_cas_and_service
($query, undef, $type);
69 $uri =~ s/\?logout\.x=1//; # We don't want to keep triggering a logout, if we got here, the borrower is already logged out of Koha
70 print $query->redirect( $cas->logout_url(url
=> $uri));
75 my ($query, $type) = @_;
76 my ( $cas, $uri ) = _get_cas_and_service
($query, undef, $type);
77 print $query->redirect( $cas->login_url($uri));
80 # Returns CAS login URL with callback to the requesting URL
82 my ( $query, $key, $type ) = @_;
83 my ( $cas, $uri ) = _get_cas_and_service
( $query, $key, $type );
84 return $cas->login_url($uri);
87 # Checks for password correctness
88 # In our case : is there a ticket, is it valid and does it match one of our users ?
90 $debug and warn "checkpw_cas";
91 my ($dbh, $ticket, $query, $type) = @_;
93 my ( $cas, $uri ) = _get_cas_and_service
($query, undef, $type);
97 $debug and warn "Got ticket : $ticket";
99 # We try to validate it
100 my $val = $cas->service_validate($uri, $ticket );
103 if ( $val->is_success() ) {
105 my $userid = $val->user();
106 $debug and warn "User CAS authenticated as: $userid";
108 # Does it match one of our users ?
109 my $sth = $dbh->prepare("select cardnumber from borrowers where userid=?");
110 $sth->execute($userid);
112 $retnumber = $sth->fetchrow;
113 return ( 1, $retnumber, $userid );
115 $sth = $dbh->prepare("select userid from borrowers where cardnumber=?");
116 $sth->execute($userid);
118 $retnumber = $sth->fetchrow;
119 return ( 1, $retnumber, $userid );
122 # If we reach this point, then the user is a valid CAS user, but not a Koha user
123 $debug and warn "User $userid is not a valid Koha user";
126 $debug and warn "Problem when validating ticket : $ticket";
127 $debug and warn "Authen::CAS::Client::Response::Error: " . $val->error() if $val->is_error();
128 $debug and warn "Authen::CAS::Client::Response::Failure: " . $val->message() if $val->is_failure();
129 $debug and warn Data
::Dumper
::Dumper
($@
) if $val->is_error() or $val->is_failure();
137 sub check_api_auth_cas
{
138 $debug and warn "check_api_auth_cas";
139 my ($dbh, $PT, $query, $type) = @_;
141 my ( $cas, $uri ) = _get_cas_and_service
($query, undef, $type);
143 # If we have a Proxy Ticket
145 my $r = $cas->proxy_validate( $uri, $PT );
148 if ( $r->is_success ) {
150 # We've got a username !
151 $debug and warn "User authenticated as: ", $r->user, "\n";
152 $debug and warn "Proxied through:\n";
153 $debug and warn " $_\n" for $r->proxies;
155 my $userid = $r->user;
157 # Does it match one of our users ?
158 my $sth = $dbh->prepare("select cardnumber from borrowers where userid=?");
159 $sth->execute($userid);
161 $retnumber = $sth->fetchrow;
162 return ( 1, $retnumber, $userid );
164 $sth = $dbh->prepare("select userid from borrowers where cardnumber=?");
166 $sth->execute($userid);
168 $retnumber = $sth->fetchrow;
169 return ( 1, $retnumber, $userid );
172 # If we reach this point, then the user is a valid CAS user, but not a Koha user
173 $debug and warn "User $userid is not a valid Koha user";
176 $debug and warn "Proxy Ticket authentication failed";
183 # Get CAS handler and service URI
184 sub _get_cas_and_service
{
186 my $key = shift; # optional
189 my $uri = _url_with_get_params
($query, $type);
191 my $casparam = $defaultcasserver;
192 $casparam = $query->param('cas') if defined $query->param('cas');
193 $casparam = $key if defined $key;
194 my $cas = Authen
::CAS
::Client
->new( $casservers->{$casparam} );
196 return ( $cas, $uri );
199 # Get the current URL with parameters contained directly into URL (GET params)
200 # This method replaces $query->url() which will give both GET and POST params
201 sub _url_with_get_params
{
205 my $uri_base_part = ($type eq 'opac') ?
206 C4
::Context
->preference('OPACBaseURL') . get_script_name
() :
207 C4
::Context
->preference('staffClientBaseURL');
209 my $uri_params_part = '';
210 foreach my $param ( $query->url_param() ) {
211 # url_param() always returns parameters that were deleted by delete()
212 # This additional check ensure that parameter was not deleted.
213 my $uriPiece = $query->param($param);
215 $uri_params_part .= '&' if $uri_params_part;
216 $uri_params_part .= $param . '=';
217 $uri_params_part .= URI
::Escape
::uri_escape
( $uriPiece );
220 $uri_base_part .= '?' if $uri_params_part;
222 return $uri_base_part . $uri_params_part;
230 C4::Auth - Authenticates Koha users
234 use C4::Auth_with_cas;