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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 use Authen::CAS::Client;
30 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $debug);
34 $VERSION = 3.07.00.049; # set the version for version checking
37 @EXPORT = qw(check_api_auth_cas checkpw_cas login_cas logout_cas login_cas_url);
39 my $context = C4::Context->new() or die 'C4::Context->new failed';
42 my $yamlauthfile = "../C4/Auth_cas_servers.yaml";
45 # If there's a configuration for multiple cas servers, then we get it
47 ($defaultcasserver, $casservers) = YAML::LoadFile(qq($FindBin::Bin/$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($FindBin::Bin/$yamlauthfile));
60 # Returns configured CAS servers' list if multiple authentication is enabled
68 my ( $cas, $uri ) = _get_cas_and_service($query);
69 print $query->redirect( $cas->logout_url($uri));
75 my ( $cas, $uri ) = _get_cas_and_service($query);
76 print $query->redirect( $cas->login_url($uri));
79 # Returns CAS login URL with callback to the requesting URL
81 my ( $query, $key ) = @_;
82 my ( $cas, $uri ) = _get_cas_and_service( $query, $key );
83 return $cas->login_url($uri);
86 # Checks for password correctness
87 # In our case : is there a ticket, is it valid and does it match one of our users ?
89 $debug and warn "checkpw_cas";
90 my ($dbh, $ticket, $query) = @_;
92 my ( $cas, $uri ) = _get_cas_and_service($query);
96 $debug and warn "Got ticket : $ticket";
98 # We try to validate it
99 my $val = $cas->service_validate($uri, $ticket );
102 if ( $val->is_success() ) {
104 my $userid = $val->user();
105 $debug and warn "User CAS authenticated as: $userid";
107 # Does it match one of our users ?
108 my $sth = $dbh->prepare("select cardnumber from borrowers where userid=?");
109 $sth->execute($userid);
111 $retnumber = $sth->fetchrow;
112 return ( 1, $retnumber, $userid );
114 $sth = $dbh->prepare("select userid from borrowers where cardnumber=?");
115 $sth->execute($userid);
117 $retnumber = $sth->fetchrow;
118 return ( 1, $retnumber, $userid );
121 # If we reach this point, then the user is a valid CAS user, but not a Koha user
122 $debug and warn "User $userid is not a valid Koha user";
125 $debug and warn "Problem when validating ticket : $ticket";
126 $debug and warn "Authen::CAS::Client::Response::Error: " . $val->error() if $val->is_error();
127 $debug and warn "Authen::CAS::Client::Response::Failure: " . $val->message() if $val->is_failure();
128 $debug and warn Data::Dumper::Dumper($@) if $val->is_error() or $val->is_failure();
136 sub check_api_auth_cas {
137 $debug and warn "check_api_auth_cas";
138 my ($dbh, $PT, $query) = @_;
140 my ( $cas, $uri ) = _get_cas_and_service($query);
142 # If we have a Proxy Ticket
144 my $r = $cas->proxy_validate( $uri, $PT );
147 if ( $r->is_success ) {
149 # We've got a username !
150 $debug and warn "User authenticated as: ", $r->user, "\n";
151 $debug and warn "Proxied through:\n";
152 $debug and warn " $_\n" for $r->proxies;
154 my $userid = $r->user;
156 # Does it match one of our users ?
157 my $sth = $dbh->prepare("select cardnumber from borrowers where userid=?");
158 $sth->execute($userid);
160 $retnumber = $sth->fetchrow;
161 return ( 1, $retnumber, $userid );
163 $sth = $dbh->prepare("select userid from borrowers where cardnumber=?");
165 $sth->execute($userid);
167 $retnumber = $sth->fetchrow;
168 return ( 1, $retnumber, $userid );
171 # If we reach this point, then the user is a valid CAS user, but not a Koha user
172 $debug and warn "User $userid is not a valid Koha user";
175 $debug and warn "Proxy Ticket authentication failed";
182 # Get CAS handler and service URI
183 sub _get_cas_and_service {
185 my $key = shift; # optional
187 my $uri = C4::Context->preference('OPACBaseURL'); # server address
188 $uri .= $query->url( -absolute => 1, -query => 1 ); # page with params
190 my $casparam = $defaultcasserver;
191 $casparam = $query->param('cas') if defined $query->param('cas');
192 $casparam = $key if defined $key;
193 my $cas = Authen::CAS::Client->new( $casservers->{$casparam} );
195 return ( $cas, $uri );
203 C4::Auth - Authenticates Koha users
207 use C4::Auth_with_cas;