Moved CAS configuration from config file to sysprefs
[koha.git] / C4 / Auth_with_cas.pm
1 package C4::Auth_with_cas;
2
3 # Copyright 2009 BibLibre SARL
4 #
5 # This file is part of Koha.
6 #
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
10 # version.
11 #
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.
15 #
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
19
20 use strict;
21
22 use C4::Debug;
23 use C4::Context;
24 use C4::Utils qw( :all );
25 use Authen::CAS::Client;
26 use CGI;
27
28
29 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $debug);
30
31 BEGIN {
32         require Exporter;
33         $VERSION = 3.03;        # set the version for version checking
34         @ISA    = qw(Exporter);
35         @EXPORT = qw( checkpw_cas login_cas logout_cas );
36 }
37
38
39 my $context = C4::Context->new()        or die 'C4::Context->new failed';
40 my $casserver = C4::Context->preference('casServerUrl');
41
42 sub logout_cas {
43     my ($query) = @_;
44     my $cas = Authen::CAS::Client->new($casserver);
45     warn $cas->logout_url();
46     print $query->redirect($cas->logout_url());
47
48 }
49
50 sub login_cas {
51     my ($query) = @_;
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'})); 
55 }
56
57 sub checkpw_cas {
58     warn "checkpw_cas";
59     my ($dbh, $ticket, $query) = @_;
60     my $retnumber;
61     my $cas = Authen::CAS::Client->new($casserver);
62
63     if ($ticket) {
64         warn "Got ticket : $ticket";
65         my $val = $cas->service_validate(%ENV->{'SCRIPT_URI'}, $ticket);
66         if( $val->is_success() ) {
67
68             my $userid = $val->user();
69             warn "User authenticated as: $userid";
70
71             my $sth = $dbh->prepare("select cardnumber from borrowers where userid=?");
72             $sth->execute($userid);
73             if ( $sth->rows ) {
74                 $retnumber = $sth->fetchrow;
75             }
76             my $sth = $dbh->prepare("select userid from borrowers where cardnumber=?");
77             $sth->execute($userid);
78             if ( $sth->rows ) {
79                 $retnumber = $sth->fetchrow;
80             }
81             return (1, $retnumber, $userid);
82         } else {
83             warn "Invalid session ticket";
84             return 0;
85         }
86
87     } else {
88         warn ("Don't have any ticket, let's go get one from the CAS server!");
89         my $url = $cas->login_url(%ENV->{'SCRIPT_URI'});
90         print $query->redirect($url);           
91     }
92
93     warn "We should not reach this point";
94     return 0;
95     #return(1, $retnumber);
96 }
97
98 1;
99 __END__
100
101 =head1 NAME
102
103 C4::Auth - Authenticates Koha users
104
105 =head1 SYNOPSIS
106
107   use C4::Auth_with_cas;
108
109 =cut
110
111 =head1 KOHA_CONF <usecasserver>http://mycasserver/loginurl</usecasserver>
112
113 =head1 SEE ALSO
114
115 CGI(3)
116
117 Authen::CAS::Client
118
119 =cut