phpCAS
version 1.4.0
docs
examples
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
23
phpCAS::setLogger
();
24
// Enable verbose error messages. Disable in production!
25
phpCAS::setVerbose
(
true
);
26
27
// Initialize phpCAS
28
phpCAS::client
(
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
// 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
// )));
60
phpCAS::allowProxyChain
(
new
CAS_ProxyChain
(array(
$pgtUrlRegexp
)));
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
72
phpCAS::forceAuthentication
();
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
$pgtUrlRegexp
Definition:
config.example.php:87
phpCAS\forceAuthentication
static forceAuthentication()
Definition:
CAS.php:1146
print
if($_SERVER['REQUEST_METHOD'] !='POST') if(empty($_POST['favorite_color'])) print
Definition:
example_service_POST.php:81
phpCAS\getUser
static getUser()
Definition:
CAS.php:1227
phpCAS\allowProxyChain
static allowProxyChain(CAS_ProxyChain_Interface $proxy_chain)
Definition:
CAS.php:1795
phpCAS\client
static client($server_version, $server_hostname, $server_port, $server_uri, $changeSessionID=true, \SessionHandlerInterface $sessionHandler=null)
Definition:
CAS.php:345
$phpcas_path
$phpcas_path
Definition:
config.example.php:20
phpCAS\setLogger
static setLogger($logger=null)
Definition:
CAS.php:448
CAS_VERSION_2_0
const CAS_VERSION_2_0
Definition:
CAS.php:78
phpCAS\setVerbose
static setVerbose($verbose)
Definition:
CAS.php:512
$cas_host
$cas_host
Definition:
config.example.php:27
CAS_ProxyChain
Definition:
ProxyChain.php:42
$cas_context
$cas_context
Definition:
config.example.php:30
$cas_port
$cas_port
Definition:
config.example.php:33
phpCAS\setNoCasServerValidation
static setNoCasServerValidation()
Definition:
CAS.php:1689
Generated by
1.8.13