Bug 5630 CAS improvements
[koha.git] / docs / CAS / CASProxy / examples / proxy_cas.pl
1 #!/usr/bin/perl
2
3 # Copyright 2009 SARL BibLibre
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 =head1 DESCRIPTION
21
22 # Here is an exemple of a CAS Proxy
23 # The proxy is a foreign application that will authenticate the user against CAS
24 # Once authenticated as a proxy, the foreign application will be able to call some
25 # Koha webservices, proving authentication only by giving a proxy ticket
26
27 # Note: please keep in mind that all url's must be https and their certificates must be trusted
28
29 =cut
30
31 use strict;
32 use warnings;
33 use CGI;
34 use Authen::CAS::Client;
35
36 # URL Of the CAS Server
37 my $casServerUrl = 'https://localhost:8443/cas/';
38 my $cas = Authen::CAS::Client->new($casServerUrl);
39 my $cgi = new CGI;
40
41 # URL of the service we're requesting a Service Ticket for (typically this very same page)
42 my $proxy_service = $cgi->url;
43
44
45 # Callback URL (this is an URL the CAS Server will query, providing the Proxy Ticket we'll need 
46 # to query the koha webservice). It can be this page or another. In this example, another page will be 
47 # called back
48 my $pgtUrl = "https://.../proxy_cas_callback.pl";
49
50 print $cgi->header({-type  =>  'text/html'});
51 print $cgi->start_html("proxy cas");
52
53 # If we already have a service ticket
54 if ($cgi->param('ticket')) {
55
56     print "Got a ticket :" . $cgi->param('ticket') . "<br>\n";
57   
58     # We validate it against the CAS Server, providing the callback URL
59     my $r = $cas->service_validate( $proxy_service, $cgi->param('ticket'), pgtUrl => $pgtUrl);
60
61     # If it is sucessful, we are authenticated
62     if( $r->is_success() ) {
63         print "User authenticated as: ", $r->user(), "<br>\n";
64     } else {
65         print "User authentication failed<br />\n";
66     }
67
68     # If we have a PGTIou ticket, the proxy validation was sucessful 
69     if (defined $r->iou) {
70       print "Proxy granting ticket IOU: ", $r->iou, "<br />\n";
71       my $pgtIou = $r->iou;
72
73       print '<a href="proxy_cas_data.pl?PGTIOU=', $r->iou, '">Next</a>';
74
75       
76             
77     } else {
78       print "Service validation for proxying failed\n";
79    }
80
81 # If we don't have a Service Ticket, we ask for one (ie : the user will be redirected to the CAS Server for authentication)
82 } else {
83
84     my $url = $cas->login_url($proxy_service);
85     print "<a href=\"$url\">Please log in</a>";
86 }
87
88 print $cgi->end_html;
89
90
91