Bug 20019: use Modern::Perl in misc perl scripts
[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
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.
11 #
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.
16 #
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>.
19
20 =head1 DESCRIPTION
21
22 # Here is an example 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 Modern::Perl;
32 use CGI qw ( -utf8 );
33 use Authen::CAS::Client;
34
35 # URL Of the CAS Server
36 my $casServerUrl = 'https://localhost:8443/cas/';
37 my $cas = Authen::CAS::Client->new($casServerUrl);
38 my $cgi = new CGI;
39
40 # URL of the service we're requesting a Service Ticket for (typically this very same page)
41 my $proxy_service = $cgi->url;
42
43
44 # Callback URL (this is an URL the CAS Server will query, providing the Proxy Ticket we'll need 
45 # to query the koha webservice). It can be this page or another. In this example, another page will be 
46 # called back
47 my $pgtUrl = "https://.../proxy_cas_callback.pl";
48
49 print $cgi->header({-type  =>  'text/html'});
50 print $cgi->start_html("proxy cas");
51
52 # If we already have a service ticket
53 if ($cgi->param('ticket')) {
54
55     print "Got a ticket :" . $cgi->param('ticket') . "<br>\n";
56   
57     # We validate it against the CAS Server, providing the callback URL
58     my $r = $cas->service_validate( $proxy_service, $cgi->param('ticket'), pgtUrl => $pgtUrl);
59
60     # If it is successful, we are authenticated
61     if( $r->is_success() ) {
62         print "User authenticated as: ", $r->user(), "<br>\n";
63     } else {
64         print "User authentication failed<br />\n";
65     }
66
67     # If we have a PGTIou ticket, the proxy validation was successful
68     if (defined $r->iou) {
69       print "Proxy granting ticket IOU: ", $r->iou, "<br />\n";
70       my $pgtIou = $r->iou;
71
72       print '<a href="proxy_cas_data.pl?PGTIOU=', $r->iou, '">Next</a>';
73
74       
75             
76     } else {
77       print "Service validation for proxying failed\n";
78    }
79
80 # If we don't have a Service Ticket, we ask for one (ie : the user will be redirected to the CAS Server for authentication)
81 } else {
82
83     my $url = $cas->login_url($proxy_service);
84     print "<a href=\"$url\">Please log in</a>";
85 }
86
87 print $cgi->end_html;
88
89
90