phpCAS version 1.3.6

example_proxy_POST.php
Go to the documentation of this file.
1 <?php
2
17 // Load the settings from the central config file
18 require_once 'config.php';
19 // Load the CAS lib
20 require_once $phpcas_path . '/CAS.php';
21
22 // Enable debugging
23 phpCAS::setDebug();
24 // Enable verbose error messages. Disable in production!
25 phpCAS::setVerbose(true);
26
27 // Initialize phpCAS
28 phpCAS::proxy(CAS_VERSION_2_0, $cas_host, $cas_port, $cas_context);
29
30 // For production use set the CA certificate that is the issuer of the cert
31 // on the CAS server and uncomment the line below
32 // phpCAS::setCasServerCACert($cas_server_ca_cert_path);
33
34 // For quick testing you can disable SSL validation of the CAS server.
35 // THIS SETTING IS NOT RECOMMENDED FOR PRODUCTION.
36 // VALIDATING THE CAS SERVER IS CRUCIAL TO THE SECURITY OF THE CAS PROTOCOL!
37 phpCAS::setNoCasServerValidation();
38
39 // force CAS authentication
40 phpCAS::forceAuthentication();
41
42 // at this step, the user has been authenticated by the CAS server
43 // and the user's login name can be read with phpCAS::getUser().
44
45 // moreover, a PGT was retrieved from the CAS server that will
46 // permit to gain accesses to new services.
47
48 $serviceUrl = $curbase . $curdir . 'example_service_POST.php';
49
50 ?>
51 <html>
52 <head>
53 <title>phpCAS proxy POST example</title>
54 <link rel="stylesheet" type='text/css' href='example.css'/>
55 </head>
56 <body>
57 <h1>phpCAS proxy POST example</h1>
58 <?php require 'script_info.php' ?>
59 <p>the user's login is <b><?php echo phpCAS::getUser(); ?></b>.</p>
60 <h2>Response from service <?php echo $serviceUrl; ?></h2>
61 <?php
62 flush();
63
64 // call a service and change the color depending on the result
65 try {
66 $service = phpCAS::getProxiedService(PHPCAS_PROXIED_SERVICE_HTTP_POST);
67 $service->setUrl($serviceUrl);
68 $service->setContentType('application/x-www-form-urlencoded');
69 $service->setBody('favorite_color=blue');
70 $service->send();
71 if ($service->getResponseStatusCode() == 200) {
72 echo '<div class="success">';
73 echo $service->getResponseBody();
74 echo '</div>';
75 } else {
76 // The service responded with an error code 404, 500, etc.
77 echo '<div class="error">';
78 echo 'The service responded with a '
79 . $service->getResponseStatusCode() . ' error.';
80 echo $service->getResponseBody();
81 echo '</div>';
82 }
83 } catch (CAS_ProxyTicketException $e) {
84 if ($e->getCode() == PHPCAS_SERVICE_PT_FAILURE) {
85 echo '<div class="error">';
86 echo "Your login has timed out. You need to log in again.";
87 echo '</div>';
88 } else {
89 // Other proxy ticket errors are from bad request format (shouldn't happen)
90 // or CAS server failure (unlikely) so lets just stop if we hit those.
91 throw $e;
92 }
93 } catch (CAS_ProxiedService_Exception $e) {
94 // Something prevented the service request from being sent or received.
95 // We didn't even get a valid error response (404, 500, etc), so this
96 // might be caused by a network error or a DNS resolution failure.
97 // We could handle it in some way, but for now we will just stop.
98 throw $e;
99 }
100
101 ?>
102 </body>
103 </html>
phpCAS\forceAuthentication
static forceAuthentication()
Definition: CAS.php:1094
phpCAS
Definition: CAS.php:278
CAS_ProxiedService_Exception
Definition: Exception.php:40
$phpcas_path
$phpcas_path
Definition: config.example.php:20
phpCAS\proxy
static proxy($server_version, $server_hostname, $server_port, $server_uri, $changeSessionID=true)
Definition: CAS.php:382
$serviceUrl
$serviceUrl
Definition: example_proxy_POST.php:48
CAS_VERSION_2_0
const CAS_VERSION_2_0
Definition: CAS.php:78
phpCAS\setVerbose
static setVerbose($verbose)
Definition: CAS.php:481
$cas_host
$cas_host
Definition: config.example.php:27
$cas_context
$cas_context
Definition: config.example.php:30
phpCAS\setDebug
static setDebug($filename='')
Definition: CAS.php:439
$curdir
if($_SERVER['SERVER_PORT'] !=80 && $_SERVER['SERVER_PORT'] !=443) $curdir
Definition: config.example.php:75
$cas_port
$cas_port
Definition: config.example.php:33
phpCAS\setNoCasServerValidation
static setNoCasServerValidation()
Definition: CAS.php:1639