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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
24 use C4::Utils qw( :all );
25 use Authen::CAS::Client;
29 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $debug);
33 $VERSION = 3.03; # set the version for version checking
35 @EXPORT = qw(checkpw_cas login_cas logout_cas login_cas_url);
39 my $context = C4::Context->new() or die 'C4::Context->new failed';
40 my $casserver = C4::Context->preference('casServerUrl');
45 my $cas = Authen::CAS::Client->new($casserver);
46 print $query->redirect($cas->logout_url(url => %ENV->{'SCRIPT_URI'}));
52 my $cas = Authen::CAS::Client->new($casserver);
53 warn $cas->login_url(%ENV->{'SCRIPT_URI'});
54 print $query->redirect($cas->login_url(%ENV->{'SCRIPT_URI'}));
57 # Returns CAS login URL with callback to the requesting URL
59 my $cas = Authen::CAS::Client->new($casserver);
60 return $cas->login_url(%ENV->{'SCRIPT_URI'});
63 # Checks for password correctness
64 # In our case : is there a ticket, is it valid and does it match one of our users ?
67 my ($dbh, $ticket, $query) = @_;
69 my $cas = Authen::CAS::Client->new($casserver);
73 warn "Got ticket : $ticket";
75 # We try to validate it
76 my $val = $cas->service_validate(%ENV->{'SCRIPT_URI'}, $ticket);
79 if( $val->is_success() ) {
81 my $userid = $val->user();
82 warn "User authenticated as: $userid";
84 # Does it match one of our users ?
85 my $sth = $dbh->prepare("select cardnumber from borrowers where userid=?");
86 $sth->execute($userid);
88 $retnumber = $sth->fetchrow;
89 return (1, $retnumber, $userid);
91 my $sth = $dbh->prepare("select userid from borrowers where cardnumber=?");
92 $sth->execute($userid);
94 $retnumber = $sth->fetchrow;
95 return (1, $retnumber, $userid);
98 warn "Invalid session ticket : $ticket";
110 C4::Auth - Authenticates Koha users
114 use C4::Auth_with_cas;
118 =head1 KOHA_CONF <usecasserver>http://mycasserver/loginurl</usecasserver>