phpCAS  version 1.4.0
example_service_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
24 // Enable verbose error messages. Disable in production!
25 phpCAS::setVerbose(true);
26 
27 // Initialize phpCAS
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!
38 
39 // If you want your service to be proxied you have to enable it (default
40 // disabled) and define an accepable list of proxies that are allowed to
41 // proxy your service.
42 //
43 // Add each allowed proxy definition object. For the normal CAS_ProxyChain
44 // class, the constructor takes an array of proxies to match. The list is in
45 // reverse just as seen from the service. Proxies have to be defined in reverse
46 // from the service to the user. If a user hits service A and gets proxied via
47 // B to service C the list of acceptable on C would be array(B,A). The definition
48 // of an individual proxy can be either a string or a regexp (preg_match is used)
49 // that will be matched against the proxy list supplied by the cas server
50 // when validating the proxy tickets. The strings are compared starting from
51 // the beginning and must fully match with the proxies in the list.
52 // Example:
53 // phpCAS::allowProxyChain(new CAS_ProxyChain(array(
54 // 'https://app.example.com/'
55 // )));
56 // phpCAS::allowProxyChain(new CAS_ProxyChain(array(
57 // '/^https:\/\/app[0-9]\.example\.com\/rest\//',
58 // 'http://client.example.com/'
59 // )));
61 
62 // For quick testing or in certain production screnarios you might want to
63 // allow allow any other valid service to proxy your service. To do so, add
64 // the "Any" chain:
65 // phpCAS::allowProxyChain(new CAS_ProxyChain_Any);
66 // THIS SETTING IS HOWEVER NOT RECOMMENDED FOR PRODUCTION AND HAS SECURITY
67 // IMPLICATIONS: YOU ARE ALLOWING ANY SERVICE TO ACT ON BEHALF OF A USER
68 // ON THIS SERVICE.
69 //phpCAS::allowProxyChain(new CAS_ProxyChain_Any);
70 
71 // force CAS authentication
73 
74 if ($_SERVER['REQUEST_METHOD'] != 'POST') {
75  header('HTTP/1.1 400 Bad Request');
76  print
77  "<h1>I only respond to POST requests. This is a "
78  . $_SERVER['REQUEST_METHOD'] . " request.</h1>";
79  exit;
80 }
81 if (empty($_POST['favorite_color'])) {
82  header('HTTP/1.1 400 Bad Request');
83  print '<h1>You must post a <strong>favorite_color</strong>.</h1>';
84  exit;
85 }
86 
87 print '<h1>I am a service that responds to POST requests.</h1>';
88 
89 // at this step, the user has been authenticated by the CAS server
90 // and the user's login name can be read with phpCAS::getUser().
91 require 'script_info.php';
92 
93 // for this test, simply print that the authentication was successfull
94 echo '<p>The user\'s login is <b>' . phpCAS::getUser() . '</b>.</p>';
95 
96 print
97  '<h1>Your favorite color is ' . htmlentities($_POST['favorite_color'])
98  . '</h1>';
99 
100 // increment the number of requests of the session and print it
101 if (!isset($_SESSION['n'])) {
102  $_SESSION['n'] = 0;
103 }
104 echo '<p>request #' . (++$_SESSION['n']) . '</p>';
105 
$pgtUrlRegexp
static forceAuthentication()
Definition: CAS.php:1146
if($_SERVER['REQUEST_METHOD'] !='POST') if(empty($_POST['favorite_color'])) print
static getUser()
Definition: CAS.php:1227
static allowProxyChain(CAS_ProxyChain_Interface $proxy_chain)
Definition: CAS.php:1795
static client($server_version, $server_hostname, $server_port, $server_uri, $changeSessionID=true, \SessionHandlerInterface $sessionHandler=null)
Definition: CAS.php:345
$phpcas_path
static setLogger($logger=null)
Definition: CAS.php:448
const CAS_VERSION_2_0
Definition: CAS.php:78
static setVerbose($verbose)
Definition: CAS.php:512
$cas_host
$cas_context
$cas_port
static setNoCasServerValidation()
Definition: CAS.php:1689