+

:4736 Function create_function() is deprecated [8192]

Last commit for index.php: 0a785d97d8501af2ba1664a2cd7f7143bf064387

Use phpCAS::logoutWithUrl() on logout to ask for redirection to local app

Benjamin Renard [2020-12-03 14:25:44]
Use phpCAS::logoutWithUrl() on logout to ask for redirection to local app
  1. <?php
  2.  
  3. /*
  4.  
  5.  ************************************
  6.  * Configuration *
  7.  ************************************
  8.  
  9. */
  10.  
  11. // PhpCAS library path
  12. $phpCAS_path="CAS.php";
  13.  
  14. // All valid CAS servers
  15. $cas_servers=array(
  16. // CAS server hostname
  17. $_SERVER['SERVER_NAME'] => array(
  18. // Context of the CAS Server
  19. 'context' => '/cas',
  20. // CAS server port
  21. 'port' => 443,
  22. // If you running this application in HTTP only, uncomment following parameter
  23. //'insecure' => true,
  24. // Disable CAS server Validation
  25. 'ssl_validation' => false,
  26. // If ssl_validation is enable you must define
  27. 'ssl_cacert_path' => '/path/to/cacert.crt',
  28. 'ssl_cn_validation' => true
  29. )
  30. );
  31.  
  32. // FQDN of CAS server
  33. $default_cas_server=key($cas_servers);
  34.  
  35. // PhpCAS log file
  36. $phpCAS_logfile='cas.log';
  37.  
  38. // Local app URL (auto-detect on first acces if null)
  39. $service_url=null;
  40.  
  41. /*
  42.  
  43.  ************************************
  44.  * Main *
  45.  ************************************
  46.  
  47. */
  48.  
  49. $warnings=array();
  50.  
  51. require $phpCAS_path;
  52. CAS_GracefullTerminationException::throwInsteadOfExiting();
  53.  
  54. // Make sure service URL is defined (otherwise, load it from session or auto-detect)
  55. if (is_null($service_url)) {
  56. if (isset($_SESSION['service_url'])) {
  57. $service_url = $_SESSION['service_url'];
  58. }
  59. else {
  60. $https = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off');
  61. $request_uri = $_SERVER['REQUEST_URI'];
  62. $request_uri = preg_replace('/\?.*$/', '', $request_uri);
  63. $service_url = "http".($https?"s":"")."://".$_SERVER['SERVER_NAME'];
  64. if (($_SERVER['SERVER_PORT'] != 443 && $https) || ($_SERVER['SERVER_PORT'] != 80 && !$https))
  65. $service_url .= ":".$_SERVER['SERVER_PORT'];
  66. $service_url .= $request_uri;
  67. $_SESSION['service_url'] = $service_url;
  68. }
  69. }
  70.  
  71. if (isset($_REQUEST['server']) && !isset($cas_servers[$_REQUEST['server']])) {
  72. $warnings[]="Invalid CAS server choiced";
  73. unset($_REQUEST['server']);
  74. }
  75. if (isset($_REQUEST['server'])) {
  76. $cas_host=$_REQUEST['server'];
  77. if ($_SESSION['cas_server']!=$cas_host) {
  78. $_SESSION['cas_server']=$cas_host;
  79. unset($_SESSION['phpCAS']['user']);
  80. }
  81. }
  82. elseif (isset($_SESSION['cas_server'])) {
  83. $cas_host=$_SESSION['cas_server'];
  84. }
  85. else {
  86. $cas_host=$default_cas_server;
  87. $_SESSION['cas_server']=$cas_host;
  88. unset($_SESSION['phpCAS']['user']);
  89. }
  90. $_SESSION['cas_server']=$cas_host;
  91.  
  92. $_show_cas_client_config=false;
  93. function show_cas_client_config() {
  94. global $phpCAS_config, $_show_cas_client_config;
  95. if ($_show_cas_client_config) return true;
  96. $_show_cas_client_config=true;
  97. echo "<h3>CAS Client configuration</h3><ul>";
  98. foreach($phpCAS_config as $cfg_name => $cfg_val) {
  99. echo "<li><strong>$cfg_name :</strong> <em>$cfg_val</em></li>";
  100. }
  101. echo "</ul>";
  102. }
  103.  
  104. $_show_warnings=false;
  105. function show_warnings() {
  106. global $warnings,$_show_warnings;
  107. if ($_show_warnings) return true;
  108. $_show_warnings=true;
  109. if (!empty($warnings)) {
  110. echo "<h2 style='color: #f00'>Warnings message</h2><ul>";
  111. foreach ($warnings as $msg) {
  112. echo "<li>$msg</li>";
  113. }
  114. echo "</ul>";
  115. }
  116. }
  117.  
  118. function show_cas_log() {
  119. global $phpCAS_logfile;
  120.  
  121. echo "<h2>PhpCAS Debug Log</h2>";
  122. if (is_writable($phpCAS_logfile)) {
  123. $lines=file($phpCAS_logfile);
  124. if (is_array($lines)) {
  125. echo '<pre>'.implode('',$lines).'</pre>';
  126. }
  127. else {
  128. echo "<strong>Error reading PhpCAS debug log file ($phpCAS_logfile).</strong>";
  129. }
  130. }
  131. else {
  132. echo "<strong>PhpCAS debug log file does not exists or is not writable ($phpCAS_logfile).</strong>";
  133. }
  134. }
  135.  
  136. function show_user_infos() {
  137. echo "<strong>Login :</strong> <em>".phpCAS::getUser()."</em><br/>";
  138. echo "<strong>Attributes : </strong><pre>".print_r(phpCAS::getAttributes(),True).'</pre>';
  139. }
  140.  
  141. ?>
  142. <html>
  143. <head>
  144. <title>Test CAS</title>
  145.  
  146. <style>
  147. strong {
  148. font-size: 0.9em;
  149. }
  150.  
  151. em {
  152. font-size: 0.8em;
  153. }
  154.  
  155. pre {
  156. margin-left: 1em;
  157. padding: 1em;
  158. border-left: 1px solid;
  159. background-color: #eee;
  160. font-size: 0.9em;
  161. }
  162.  
  163. div.success, div.error {
  164. padding: 0.2em;
  165. width: 50%;
  166. font-weight: bold;
  167. margin: 1em;
  168. text-align: center;
  169. }
  170.  
  171. div.success {
  172. color: #0E4700;
  173. border: 1px solid #0E4700;
  174. background-color: #99E774;
  175. }
  176.  
  177. div.error {
  178. color: #f00;
  179. border: 1px solid #f00;
  180. padding: 1em;
  181. background-color: #C56E6E;
  182. }
  183.  
  184. h2 {
  185. border-bottom: 1px solid;
  186. }
  187. </style>
  188. <body>
  189. <h1>Test CAS Application</h1>
  190.  
  191. <h2>CAS server selection</h2>
  192. <form action='index.php' method='POST'>
  193. <label for='server'>CAS server</label> :
  194. <select name='server' id='server' onchange="javascript:submit();">
  195. <?php
  196. foreach($cas_servers as $srv => $opts) {
  197. echo "<option value='$srv'".(($cas_host==$srv)?'selected':'').">$srv</option>\n";
  198. }
  199. ?>
  200. </select>
  201. <input type='submit' value='Change'/>
  202. </form>
  203. <h2>Menu</h2>
  204. <ul>
  205. <li><a href='?do=login'>Login</a></li>
  206. <li><a href='?do=caslogout'>Logout on CAS server</a></li>
  207. <li><a href='?do=locallogout'>Logout on local application</a></li>
  208. <?php
  209. if (is_writable($phpCAS_logfile)) {
  210. echo "<li><a href='?truncatelog=true'>Truncate Debug log file content</a></li>";
  211. }
  212. ?>
  213. </ul>
  214.  
  215. <h2>CAS Client Initialization ...</h2>
  216. <?php
  217. try {
  218.  
  219. $phpCAS_config=array(
  220. 'CAS Hostname' => $cas_host,
  221. 'CAS server port' => $cas_servers[$cas_host]['port'],
  222. 'CAS server context' => $cas_servers[$cas_host]['context'],
  223. 'Service URL' => $service_url,
  224. );
  225.  
  226. if (is_writable($phpCAS_logfile)) {
  227. if (isset($_REQUEST['truncatelog'])) {
  228. $fh = fopen($phpCAS_logfile, 'w');
  229. fclose($fh);
  230. }
  231. $phpCAS_config['Debug file'] = $phpCAS_logfile;
  232. phpCAS::setDebug($phpCAS_logfile);
  233. }
  234.  
  235. phpCAS::client(CAS_VERSION_2_0, $cas_host, $cas_servers[$cas_host]['port'], $cas_servers[$cas_host]['context']);
  236. phpCAS::setFixedServiceURL($service_url);
  237.  
  238. if ($cas_servers[$cas_host]['insecure']) {
  239. $phpCAS_config['Insecure'] = 'Yes';
  240. $phpCAS_config['Base URL'] = 'http://'.$cas_host.($cas_servers[$cas_host]['port']?':'.$cas_servers[$cas_host]['port']:'').$cas_servers[$cas_host]['context'];
  241. // Remove trailing slash if present
  242. if (substr($phpCAS_config['Base URL'], -1)=='/')
  243. $phpCAS_config['Base URL'] = substr($phpCAS_config['Base URL'], 0, -1);
  244. $login_url = "$base_url/login";
  245. $service_validate_url = "$base_url/serviceValidate";
  246. $logout_url = "$base_url/logout";
  247. $phpCAS_config['Login URL'] = $phpCAS_config['Base URL']."/login?service=".urlencode($service_url);
  248. $phpCAS_config['Logout URL'] = $phpCAS_config['Base URL']."/logout";
  249. $phpCAS_config['Service validate URL'] = $phpCAS_config['Base URL']."/serviceValidate";
  250. phpCAS::setServerLoginURL($phpCAS_config['Login URL']);
  251. phpCAS::setServerLogoutURL($phpCAS_config['Logout URL']);
  252. phpCAS::setServerServiceValidateURL($phpCAS_config['Service validate URL']);
  253. // Be sure SSL validation is disabled
  254. $cas_servers[$cas_host]['ssl_validation'] = false;
  255. }
  256. else
  257. $phpCAS_config['Insecure'] = 'No';
  258.  
  259. echo "<div class='success'>Client successfully initialized</div>";
  260.  
  261. if ($cas_servers[$cas_host]['ssl_validation']===true) {
  262. if (is_readable($cas_servers[$cas_host]['ssl_cacert_path'])) {
  263. $phpCAS_config['SSL Validation']='Enabled';
  264. $phpCAS_config['SSL CA Cert Validation File']=$cas_servers[$cas_host]['ssl_cacert_path'];
  265. $phpCAS_config['SSL CN Validation']=($cas_servers[$cas_host]['ssl_cn_validation']?'Enabled':'Disabled');
  266. phpCAS::setCasServerCACert($cas_servers[$cas_host]['ssl_cacert_path'],$cas_servers[$cas_host]['ssl_cn_validation']);
  267. }
  268. else {
  269. $warnings[]='SSL validation enable for this server but CA Cert file configured does not exists or is not readable';
  270. $phpCAS_config['SSL Validation']='Disabled';
  271. phpCAS::setNoCasServerValidation();
  272. }
  273. }
  274. else {
  275. $phpCAS_config['SSL Validation']='Disabled';
  276. phpCAS::setNoCasServerValidation();
  277. }
  278.  
  279. phpCAS::setCacheTimesForAuthRecheck(0);
  280.  
  281. show_cas_client_config();
  282. show_warnings();
  283.  
  284. ?>
  285.  
  286. <h2>Action</h2>
  287. <h3>State before running action</h3>
  288. <?php
  289. if (phpCAS::isAuthenticated()) {
  290. echo "Authenticated";
  291. }
  292. else {
  293. echo "Not authenticated";
  294. }
  295. ?>
  296. <h3>Running action...</h3>
  297. <?php
  298.  
  299. if (isset($_REQUEST['do'])) {
  300.  
  301. switch($_REQUEST['do']) {
  302. case 'login':
  303. phpCAS::forceAuthentication();
  304. echo "<div class='success'>Successfully authenticated</div>";
  305. break;
  306. case 'caslogout':
  307. phpCAS::forceAuthentication();
  308. phpCAS::logoutWithUrl($service_url);
  309. break;
  310. case 'locallogout':
  311. unset($_SESSION['session_url']);
  312. unset($_SESSION['phpCAS']);
  313. if (!isset($_SESSION['phpCAS'])) {
  314. echo "<div class='success'>Successfully logout</div>";
  315. }
  316. else {
  317. echo "<div class='error'>Failed to unset phpCAS session informations</div>";
  318. }
  319. break;
  320. default:
  321. echo "<div class='error'>Incorrect parameters</div>";
  322. }
  323. }
  324. else {
  325. echo "Nothing to do";
  326. }
  327.  
  328. if (phpCAS::isAuthenticated()) {
  329. echo "<h2>Authenticated user informations</h2>";
  330. show_user_infos();
  331. }
  332.  
  333. // End of catch
  334. }
  335. catch (CAS_GracefullTerminationException $e) {
  336. echo "<div class='error'>PhpCAS return exception</div>";
  337. show_cas_client_config();
  338. show_warnings();
  339. }
  340.  
  341. show_cas_log();
  342.  
  343.  
  344. ?>
  345.  
  346. </body>
  347. </html>