4 * Example for a proxy that makes a POST request.
8 * @file example_proxy_POST.php
9 * @category Authentication
11 * @author Joachim Fritschi <jfritschi@freenet.de>
12 * @author Adam Franco <afranco@middlebury.edu>
13 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
14 * @link https://wiki.jasig.org/display/CASC/phpCAS
17 // Load the settings from the central config file
18 require_once 'config.php';
20 require_once $phpcas_path . '/CAS.php';
22 // Uncomment to enable debugging
26 phpCAS::proxy(CAS_VERSION_2_0, $cas_host, $cas_port, $cas_context);
28 // For production use set the CA certificate that is the issuer of the cert
29 // on the CAS server and uncomment the line below
30 // phpCAS::setCasServerCACert($cas_server_ca_cert_path);
32 // For quick testing you can disable SSL validation of the CAS server.
33 // THIS SETTING IS NOT RECOMMENDED FOR PRODUCTION.
34 // VALIDATING THE CAS SERVER IS CRUCIAL TO THE SECURITY OF THE CAS PROTOCOL!
35 phpCAS::setNoCasServerValidation();
37 // force CAS authentication
38 phpCAS::forceAuthentication();
40 // at this step, the user has been authenticated by the CAS server
41 // and the user's login name can be read with phpCAS::getUser().
43 // moreover, a PGT was retrieved from the CAS server that will
44 // permit to gain accesses to new services.
46 $serviceUrl = $curbase . $curdir . 'example_service_POST.php';
51 <title>phpCAS proxy POST example</title>
52 <link rel="stylesheet" type='text/css' href='example.css'/>
55 <h1>phpCAS proxy POST example</h1>
56 <?php require 'script_info.php' ?>
57 <p>the user's login is <b><?php echo phpCAS::getUser(); ?></b>.</p>
58 <h2>Response from service <?php echo $serviceUrl; ?></h2>
62 // call a service and change the color depending on the result
64 $service = phpCAS::getProxiedService(PHPCAS_PROXIED_SERVICE_HTTP_POST);
65 $service->setUrl($serviceUrl);
66 $service->setContentType('application/x-www-form-urlencoded');
67 $service->setBody('favorite_color=blue');
69 if ($service->getResponseStatusCode() == 200) {
70 echo '<div class="success">';
71 echo $service->getResponseBody();
74 // The service responded with an error code 404, 500, etc.
75 echo '<div class="error">';
76 echo 'The service responded with a '
77 . $service->getResponseStatusCode() . ' error.';
78 echo $service->getResponseBody();
81 } catch (CAS_ProxyTicketException $e) {
82 if ($e->getCode() == PHPCAS_SERVICE_PT_FAILURE) {
83 echo '<div class="error">';
84 echo "Your login has timed out. You need to log in again.";
87 // Other proxy ticket errors are from bad request format (shouldn't happen)
88 // or CAS server failure (unlikely) so lets just stop if we hit those.
91 } catch (CAS_ProxiedService_Exception $e) {
92 // Something prevented the service request from being sent or received.
93 // We didn't even get a valid error response (404, 500, etc), so this
94 // might be caused by a network error or a DNS resolution failure.
95 // We could handle it in some way, but for now we will just stop.