92c61a5392cc654e62d63b3662b4025bb9b6b8f4
[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 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 # 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 strict;
34 use warnings;
35 use CGI;
36 use Authen::CAS::Client;
37 use Storable qw(fd_retrieve);
38 use LWP::Simple;
39 use URI::Escape;
40
41 my $casServerUrl = 'https://localhost:8443/cas/';
42 my $cas = Authen::CAS::Client->new($casServerUrl);
43
44 # URL of the service we'd like to be proxy for (typically the Koha webservice we want to query)
45 my $target_service = "https://.../koha_webservice.pl";
46
47 my $cgi = new CGI;
48
49 print $cgi->header({-type  =>  'text/html'});
50 print $cgi->start_html("proxy cas");
51
52
53 if ($cgi->param('PGTIOU')) {
54
55       # At this point, we must retrieve the PgtId by matching the PgtIou we
56       # just received and the PgtIou given by the CAS Server to the callback URL
57       # The callback page stored it in the application vars (in our case a storable object in a file)
58       open FILE, "casSession.tmp" or die "Unable to open file";
59       my $hashref = fd_retrieve(\*FILE);
60       my $pgtId = %$hashref->{$cgi->param('PGTIOU')};
61       close FILE;
62
63       # Now that we have a PgtId, we can ask the cas server for a proxy ticket...
64       my $rp = $cas->proxy( $pgtId, $target_service );
65       if( $rp->is_success ) {
66         print "Proxy Ticket issued: ", $rp->proxy_ticket, "<br />\n";
67
68         # ...which we will provide to the target service (the koha webservice) for authentication !
69         my $data = get($target_service . "?PT=" . $rp->proxy_ticket);
70         
71         # And finally, we can display the data gathered from the koha webservice !
72         print "This is the output of the koha webservice we just queried, CAS authenticated : <br/>";
73         print "<code>$data</code>";
74
75       } else {
76         print "Cannot get Proxy Ticket";
77       }
78
79
80 }