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