Bug 20906: Prevent test failures due to stricter perl
[koha.git] / docs / CAS / CASProxy / examples / proxy_cas_data.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 # This page will display the result of the call to the koha webservice
23
24 =head1 CGI PARAMETERS
25
26 =item PGTIOU
27
28 The Proxy Granting Ticket IOU the CAS Server returned to us when we gave him the Service Ticket
29 This PGTIOU will allow us to retrive the matching PGTID
30
31 =cut 
32
33 use Modern::Perl;
34 use CGI qw ( -utf8 );
35 use Authen::CAS::Client;
36 use Storable qw(fd_retrieve);
37 use LWP::Simple;
38 use URI::Escape;
39
40 my $casServerUrl = 'https://localhost:8443/cas/';
41 my $cas = Authen::CAS::Client->new($casServerUrl);
42
43 # URL of the service we'd like to be proxy for (typically the Koha webservice we want to query)
44 my $target_service = "https://.../koha_webservice.pl";
45
46 my $cgi = new CGI;
47
48 print $cgi->header({-type  =>  'text/html'});
49 print $cgi->start_html("proxy cas");
50
51
52 if ($cgi->param('PGTIOU')) {
53
54       # At this point, we must retrieve the PgtId by matching the PgtIou we
55       # just received and the PgtIou given by the CAS Server to the callback URL
56       # The callback page stored it in the application vars (in our case a storable object in a file)
57       open FILE, "casSession.tmp" or die "Unable to open file";
58       my $hashref = fd_retrieve(\*FILE);
59       my $pgtId = %{$hashref->{$cgi->param('PGTIOU')}};
60       close FILE;
61
62       # Now that we have a PgtId, we can ask the cas server for a proxy ticket...
63       my $rp = $cas->proxy( $pgtId, $target_service );
64       if( $rp->is_success ) {
65         print "Proxy Ticket issued: ", $rp->proxy_ticket, "<br />\n";
66
67         # ...which we will provide to the target service (the koha webservice) for authentication !
68         my $data = get($target_service . "?PT=" . $rp->proxy_ticket);
69         
70         # And finally, we can display the data gathered from the koha webservice !
71         print "This is the output of the koha webservice we just queried, CAS authenticated : <br/>";
72         print "<code>$data</code>";
73
74       } else {
75         print "Cannot get Proxy Ticket";
76       }
77
78
79 }