+

Last commit for index.php: 699ab244d233f275011a8df88c658e3cce3ce5cf

Initial commit

Benjamin Renard [2014-12-29 20:54:44]
Initial commit
  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. // Disable CAS server Validation
  23. 'ssl_validation' => false,
  24. // If ssl_validation is enable you must define
  25. 'ssl_cacert_path' => '/path/to/cacert.crt',
  26. 'ssl_cn_validation' => true
  27. )
  28. );
  29.  
  30. // FQDN of CAS server
  31. $default_cas_server=key($cas_servers);
  32.  
  33. // PhpCAS log file
  34. $phpCAS_logfile='/tmp/cas.log';
  35.  
  36. /*
  37.  
  38.  ************************************
  39.  * Main *
  40.  ************************************
  41.  
  42. */
  43.  
  44. $warnings=array();
  45.  
  46. require $phpCAS_path;
  47. CAS_GracefullTerminationException::throwInsteadOfExiting();
  48.  
  49. if (isset($_REQUEST['server']) && !isset($cas_servers[$_REQUEST['server']])) {
  50. $warnings[]="Invalid CAS server choiced";
  51. unset($_REQUEST['server']);
  52. }
  53. if (isset($_REQUEST['server'])) {
  54. $cas_host=$_REQUEST['server'];
  55. if ($_SESSION['cas_server']!=$cas_host) {
  56. $_SESSION['cas_server']=$cas_host;
  57. unset($_SESSION['phpCAS']['user']);
  58. }
  59. }
  60. elseif (isset($_SESSION['cas_server'])) {
  61. $cas_host=$_SESSION['cas_server'];
  62. }
  63. else {
  64. $cas_host=$default_cas_server;
  65. $_SESSION['cas_server']=$cas_host;
  66. unset($_SESSION['phpCAS']['user']);
  67. }
  68. $_SESSION['cas_server']=$cas_host;
  69.  
  70. $_show_cas_client_config=false;
  71. function show_cas_client_config() {
  72. global $phpCAS_config, $_show_cas_client_config;
  73. if ($_show_cas_client_config) return true;
  74. $_show_cas_client_config=true;
  75. echo "<h3>CAS Client configuration</h3><ul>";
  76. foreach($phpCAS_config as $cfg_name => $cfg_val) {
  77. echo "<li><strong>$cfg_name :</strong> <em>$cfg_val</em></li>";
  78. }
  79. echo "</ul>";
  80. }
  81.  
  82. $_show_warnings=false;
  83. function show_warnings() {
  84. global $warnings,$_show_warnings;
  85. if ($_show_warnings) return true;
  86. $_show_warnings=true;
  87. if (!empty($warnings)) {
  88. echo "<h2 style='color: #f00'>Warnings message</h2><ul>";
  89. foreach ($warnings as $msg) {
  90. echo "<li>$msg</li>";
  91. }
  92. echo "</ul>";
  93. }
  94. }
  95.  
  96. function show_cas_log() {
  97. global $phpCAS_logfile;
  98.  
  99. echo "<h2>PhpCAS Debug Log</h2>";
  100. if (is_writable($phpCAS_logfile)) {
  101. $lines=file($phpCAS_logfile);
  102. if (is_array($lines)) {
  103. echo '<pre>'.implode('',$lines).'</pre>';
  104. }
  105. else {
  106. echo "<strong>Error reading PhpCAS debug log file ($phpCAS_logfile).</strong>";
  107. }
  108. }
  109. else {
  110. echo "<strong>PhpCAS debug log file does not exists or is not writable ($phpCAS_logfile).</strong>";
  111. }
  112. }
  113.  
  114. function show_user_infos() {
  115. echo "<strong>Login :</strong> <em>".phpCAS::getUser()."</em><br/>";
  116. echo "<strong>Attributes : </strong><pre>".print_r(phpCAS::getAttributes(),True).'</pre>';
  117. }
  118.  
  119. ?>
  120. <html>
  121. <head>
  122. <title>Test CAS</title>
  123.  
  124. <style>
  125. strong {
  126. font-size: 0.9em;
  127. }
  128.  
  129. em {
  130. font-size: 0.8em;
  131. }
  132.  
  133. pre {
  134. margin-left: 1em;
  135. padding: 1em;
  136. border-left: 1px solid;
  137. background-color: #eee;
  138. font-size: 0.9em;
  139. }
  140.  
  141. div.success, div.error {
  142. padding: 0.2em;
  143. width: 50%;
  144. font-weight: bold;
  145. margin: 1em;
  146. text-align: center;
  147. }
  148.  
  149. div.success {
  150. color: #0E4700;
  151. border: 1px solid #0E4700;
  152. background-color: #99E774;
  153. }
  154.  
  155. div.error {
  156. color: #f00;
  157. border: 1px solid #f00;
  158. padding: 1em;
  159. background-color: #C56E6E;
  160. }
  161.  
  162. h2 {
  163. border-bottom: 1px solid;
  164. }
  165. </style>
  166. <body>
  167. <h1>Test CAS Application</h1>
  168.  
  169. <h2>CAS server selection</h2>
  170. <form action='index.php' method='POST'>
  171. <label for='server'>CAS server</label> :
  172. <select name='server' id='server' onchange="javascript:submit();">
  173. <?php
  174. foreach($cas_servers as $srv => $opts) {
  175. echo "<option value='$srv'".(($cas_host==$srv)?'selected':'').">$srv</option>\n";
  176. }
  177. ?>
  178. </select>
  179. <input type='submit' value='Change'/>
  180. </form>
  181. <h2>Menu</h2>
  182. <ul>
  183. <li><a href='?do=login'>Login</a></li>
  184. <li><a href='?do=caslogout'>Logout on CAS server</a></li>
  185. <li><a href='?do=locallogout'>Logout on local application</a></li>
  186. <?php
  187. if (is_writable($phpCAS_logfile)) {
  188. echo "<li><a href='?truncatelog=true'>Truncate Debug log file content</a></li>";
  189. }
  190. ?>
  191. </ul>
  192.  
  193. <h2>CAS Client Initialization ...</h2>
  194. <?php
  195. try {
  196.  
  197. $phpCAS_config=array(
  198. 'CAS Hostname' => $cas_host,
  199. 'CAS server port' => $cas_servers[$cas_host]['port'],
  200. 'CAS server context' => $cas_servers[$cas_host]['context'],
  201. );
  202.  
  203. if (is_writable($phpCAS_logfile)) {
  204. if (isset($_REQUEST['truncatelog'])) {
  205. $fh = fopen($phpCAS_logfile, 'w');
  206. fclose($fh);
  207. }
  208. $phpCAS_config['Debug file'] = $phpCAS_logfile;
  209. phpCAS::setDebug($phpCAS_logfile);
  210. }
  211.  
  212. phpCAS::client(CAS_VERSION_2_0, $cas_host, $cas_servers[$cas_host]['port'], $cas_servers[$cas_host]['context']);
  213.  
  214. echo "<div class='success'>Client successfully initialized</div>";
  215.  
  216. if ($cas_servers[$cas_host]['ssl_validation']===true) {
  217. if (is_readable($cas_servers[$cas_host]['ssl_cacert_path'])) {
  218. $phpCAS_config['SSL Validation']='Enabled';
  219. $phpCAS_config['SSL CA Cert Validation File']=$cas_servers[$cas_host]['ssl_cacert_path'];
  220. $phpCAS_config['SSL CN Validation']=($cas_servers[$cas_host]['ssl_cn_validation']?'Enabled':'Disabled');
  221. phpCAS::setCasServerCACert($cas_servers[$cas_host]['ssl_cacert_path'],$cas_servers[$cas_host]['ssl_cn_validation']);
  222. }
  223. else {
  224. $warnings[]='SSL validation enable for this server but CA Cert file configured does not exists or is not readable';
  225. $phpCAS_config['SSL Validation']='Disabled';
  226. phpCAS::setNoCasServerValidation();
  227. }
  228. }
  229. else {
  230. $phpCAS_config['SSL Validation']='Disabled';
  231. phpCAS::setNoCasServerValidation();
  232. }
  233.  
  234. phpCAS::setCacheTimesForAuthRecheck(0);
  235.  
  236. show_cas_client_config();
  237. show_warnings();
  238.  
  239. ?>
  240.  
  241. <h2>Action</h2>
  242. <h3>State before running action</h3>
  243. <?php
  244. if (phpCAS::isAuthenticated()) {
  245. echo "Authenticated";
  246. }
  247. else {
  248. echo "Not authenticated";
  249. }
  250. ?>
  251. <h3>Running action...</h3>
  252. <?php
  253.  
  254. if (isset($_REQUEST['do'])) {
  255.  
  256. switch($_REQUEST['do']) {
  257. case 'login':
  258. phpCAS::forceAuthentication();
  259. echo "<div class='success'>Successfully authenticated</div>";
  260. break;
  261. case 'caslogout':
  262. phpCAS::forceAuthentication();
  263. phpCAS::logout();
  264. break;
  265. case 'locallogout':
  266. unset($_SESSION['phpCAS']);
  267. if (!isset($_SESSION['phpCAS'])) {
  268. echo "<div class='success'>Successfully logout</div>";
  269. }
  270. else {
  271. echo "<div class='error'>Failed to unset phpCAS session informations</div>";
  272. }
  273. break;
  274. default:
  275. echo "<div class='error'>Incorrect parameters</div>";
  276. }
  277. }
  278. else {
  279. echo "Nothing to do";
  280. }
  281.  
  282. if (phpCAS::isAuthenticated()) {
  283. echo "<h2>Authenticated user informations</h2>";
  284. show_user_infos();
  285. }
  286.  
  287. // End of catch
  288. }
  289. catch (CAS_GracefullTerminationException $e) {
  290. echo "<div class='error'>PhpCAS return exception</div>";
  291. show_cas_client_config();
  292. show_warnings();
  293. }
  294.  
  295. show_cas_log();
  296.  
  297.  
  298. ?>
  299.  
  300. </body>
  301. </html>