Initial version

This commit is contained in:
Benjamin Renard 2022-07-09 20:16:15 +02:00
commit 5381e56a3f
6 changed files with 637 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
*~
.*.swp
*.log

36
README.md Normal file
View File

@ -0,0 +1,36 @@
# Single page test CAS Client
This single page application acting as CAS Client.
Features :
* Login/Logout on CAS server
* Local logout
* Show authenticated user's informations (login and attributes)
* Show complete debug log of phpCAS library
## Requirements
* phpCAS library (from php-cas Debian package for instance)
* CURL and Dom PHP modules (from php-curl and php-dom Debian packages for instance)
* Apache PHP support (using _mod_php_ or _PHP Fpm_, install _libapache2-mod-php_ Debian packages for instance)
* One directory writable by Apache user for logging
## Installation
* Install the application and its requirements
```
apt install -y libapache2-mod-php php-cas php-curl php-dom git
mkdir -p /var/www/connexion/public_html
git clone https://gogs.zionetrix.net/bn8/test-cas-client-sp.git /var/www/connexion/test-cas-client-sp
ln -s /var/www/connexion/test-cas-client-sp /var/www/connexion/public_html/test-sp
chown www-data: /var/www/connexion/test-cas-client-sp/logs
```
* Configure access to the application, for instance, in an existing Apache _VirtualHost_ definition by adding something like:
```
Alias /test-sp /var/www/connexion/public_html/test-sp
ProxyPass /test-sp !
<Directory /var/www/connexion/public_html/test-sp>
Require all granted
</Directory>
```

178
app.js Normal file
View File

@ -0,0 +1,178 @@
$(document).ready( function() {
// Lookup for UI components
var cas_server_selection = $('#cas_server_selection');
var select_server = $('#select_server');
var div_cas_config = $('#cas_config');
var ul_cas_config = div_cas_config.find('ul').first();
var div_user_info = $('#user_info');
var div_warnings = $('#warnings');
var div_cas_logs = $('#cas_logs');
var pre_cas_logs = div_cas_logs.find('pre').first();
var login_btn = $('#login_btn');
var caslogout_btn = $('#caslogout_btn');
var locallogout_btn = $('#locallogout_btn');
var truncatelog_btn = $('#truncatelog_btn');
// Configure CAS servers selection (from API return)
var configure_cas_server_selection = function(cas_servers, selected) {
select_server.html('');
if (!cas_servers || Object.keys(cas_servers).length <= 1) {
cas_server_selection.css('display', 'none');
return true;
}
for (var cas_host in cas_servers) {
var option = $('<option value="'+cas_host+'">'+cas_host+'</option>');
if (cas_host == selected)
option.attr('selected', 'selected');
select_server.append(option);
}
cas_server_selection.css('display', 'block');
};
// Show CAS configuration (from API return)
var show_cas_config = function(cas_config) {
ul_cas_config.html('');
if (!cas_config) {
div_cas_config.css('display', 'none');
return true;
}
for (var key in cas_config) {
var li = $('<li><strong>'+key+':</strong> <em>'+cas_config[key]+'</em></li>');
ul_cas_config.append(li);
}
div_cas_config.css('display', 'block');
};
// Show warning messages (from API return)
var show_warnings = function(warnings) {
div_warnings.html('<h2>Warning messages</h2>');
if (!warnings.length) {
div_warnings.css('display', 'none');
return true;
}
var ul = $('<ul>');
$(warnings).each(function(idx, warning) {
var li = $('<li>'+warning+'</li>');
ul.append(li);
});
div_warnings.append(ul);
div_warnings.css('display', 'block');
};
// Show phpCAS logs
var show_cas_logs = function(logs) {
pre_cas_logs.html('');
if (!logs) {
div_cas_logs.css('display', 'none');
return true;
}
pre_cas_logs.html(logs);
div_cas_logs.css('display', 'block');
};
// Show user information
var show_user_info = function(username, attributes) {
div_user_info.html('<h2>Connected user information</h2>');
div_user_info.css('display', 'block');
if (!username) {
div_user_info.append('<p>Not connected</p>');
return true;
}
var p_username = $('<p><strong>Username:</strong> <em>'+username+'</em></p>');
div_user_info.append(p_username);
div_user_info.append('<h3>Attributes</h3>');
var ul = $('<ul>');
if (attributes) {
for (var attr in attributes) {
var li = $('<li><strong>'+attr+':</strong> <em>'+attributes[attr]+':</em></li>');
ul.append(li);
}
}
else {
ul.append($('<li>No attribute.</li>'));
}
div_user_info.append(ul);
};
// Handle API AJAX call return
var handle_ajax_return = function(data) {
configure_cas_server_selection(data.cas_servers, data.cas_server);
show_warnings(data.warnings);
show_cas_logs(data.logs);
show_cas_config(data.config);
show_user_info(data.user, data.attributes);
};
// Handle API AJAX call fail
var handle_ajax_fail = function(jqXHR, textStatus, errorThrown) {
alert('An error occurred during AJAX request: '+textStatus);
};
// Make an API AJAX call
var make_ajax_call = function(action, data, callback) {
if (!data) data = {};
data['do'] = action;
$.ajax(
{type: "GET", url: 'index.php', data: data}
).done(function(data) {
handle_ajax_return(data);
if (callback) callback(data);
}).fail(handle_ajax_fail);
};
// On select server change, make API call to change current server
select_server.on('change', function(event) {
make_ajax_call('change_server', {'server': select_server.val()});
});
// On local logout button click, make API call to trigger it
locallogout_btn.on('click', function(event) {
make_ajax_call('locallogout');
});
// On CAS logout button click, make API call to trigger it
caslogout_btn.on('click', function(event) {
make_ajax_call('logout', {}, function(data) {
if (data.data && data.data.logout_url)
window.location = data.data.logout_url;
});
});
// On login button click, make API call to trigger it
login_btn.on('click', function(event) {
make_ajax_call('login', {}, function(data) {
if (data.data && data.data.login_url)
window.location = data.data.login_url;
});
});
// On truncate log button click, make API call to trigger it
if (truncatelog_btn) {
truncatelog_btn.on('click', function(event) {
make_ajax_call('truncatelog');
});
}
// Check if CAS ticket is present in current URL
var check_ticket_is_present = new RegExp('[\?&]ticket=([^&]+)');
var ticket_data = check_ticket_is_present.exec(window.location);
// If CAS ticket present, make API call to validate it
if (ticket_data) {
make_ajax_call('validate_ticket', {'ticket': ticket_data[1]}, function(data) {
// If ticket is valid, clean current URI
if (data.success)
window.location = 'index.php';
});
}
else {
// if not CAS ticket present, make API call to get current status
make_ajax_call('status');
}
});

416
index.php Normal file
View File

@ -0,0 +1,416 @@
<?php
/*
************************************
* Configuration *
************************************
*/
// PhpCAS library path
$phpCAS_path = "CAS.php";
// All valid CAS servers
$cas_servers = array(
// CAS server hostname
$_SERVER['SERVER_NAME'] => array(
// Context of the CAS Server
'context' => '/idp/cas',
// CAS server port
'port' => 8443,
// If you running this application in HTTP only, uncomment following parameter
//'insecure' => true,
// Disable CAS server Validation
'ssl_validation' => false,
// If ssl_validation is enable you must define
'ssl_cacert_path' => '/etc/ssl/certs/ca-certificates.crt',
'ssl_cn_validation' => true,
// Extra CURL options (for phpCAS client)
'extra_curl_options' => array(
// Uncomment it in case of 'dh key too small' error
// 'CURLOPT_SSL_CIPHER_LIST' => 'DEFAULT@SECLEVEL=1',
),
),
$_SERVER['SERVER_NAME'].'2' => array(
// Context of the CAS Server
'context' => '/idp/cas',
// CAS server port
'port' => 8443,
// If you running this application in HTTP only, uncomment following parameter
//'insecure' => true,
// Disable CAS server Validation
'ssl_validation' => false,
// If ssl_validation is enable you must define
'ssl_cacert_path' => '/etc/ssl/certs/ca-certificates.crt',
'ssl_cn_validation' => true,
// Extra CURL options (for phpCAS client)
'extra_curl_options' => array(
// Uncomment it in case of 'dh key too small' error
// 'CURLOPT_SSL_CIPHER_LIST' => 'DEFAULT@SECLEVEL=1',
),
),
);
// FQDN of CAS server
$default_cas_server = key($cas_servers);
// PhpCAS debug logs
// Log directory path
$phpCAS_logdir = 'logs';
// Log filename format
// Compose with :
// - {cas_server} : the CAS server
// - {remote_addr} : connected user remote IP address
// - {session_id} : connected user session_id
$phpCAS_logfile_format = '{session_id}-{cas_server}.log';
// Local app URL (auto-detect on first acces if null)
$service_url = null;
/*
************************************
* Main *
************************************
*/
$warnings = array();
session_start();
require $phpCAS_path;
CAS_GracefullTerminationException::throwInsteadOfExiting();
// Initialize session variables
if (isset($_SESSION['cas_server']) && array_key_exists($_SESSION['cas_server'], $cas_servers)) {
$cas_host = $_SESSION['cas_server'];
}
else {
$_SESSION['cas_server'] = $cas_host = $default_cas_server;
}
if (!isset($_SESSION['user'])) $_SESSION['user'] = null;
if (!isset($_SESSION['attributes'])) $_SESSION['attributes'] = null;
// Generate phpCAS debug log file path
$phpCAS_logfile = "$phpCAS_logdir/$phpCAS_logfile_format";
$phpCAS_logfile = str_replace('{cas_server}', $cas_host, $phpCAS_logfile);
$phpCAS_logfile = str_replace('{remote_addr}', $_SERVER['REMOTE_ADDR'], $phpCAS_logfile);
$phpCAS_logfile = str_replace('{session_id}', session_id(), $phpCAS_logfile);
$phpCAS_config = array();
function init_phpCAS() {
global $phpCAS_logfile, $phpCAS_config, $warnings, $cas_host, $cas_servers, $service_url;
// Make sure service URL is defined (otherwise, load it from session or auto-detect)
if (is_null($service_url)) {
if (isset($_SESSION['service_url'])) {
$service_url = $_SESSION['service_url'];
}
else {
$https = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off');
$request_uri = $_SERVER['REQUEST_URI'];
$request_uri = preg_replace('/\?.*$/', '', $request_uri);
$service_url = "http".($https?"s":"")."://".$_SERVER['SERVER_NAME'];
if (($_SERVER['SERVER_PORT'] != 443 && $https) || ($_SERVER['SERVER_PORT'] != 80 && !$https))
$service_url .= ":".$_SERVER['SERVER_PORT'];
$service_url .= $request_uri;
$_SESSION['service_url'] = $service_url;
}
}
// Compute phpCAS configuration
$phpCAS_config = array(
'CAS Hostname' => $cas_host,
'CAS server port' => $cas_servers[$cas_host]['port'],
'CAS server context' => $cas_servers[$cas_host]['context'],
'Service URL' => $service_url,
);
// Set phpCAS log file
if (is_writable($phpCAS_logfile) || (!is_file($phpCAS_logfile) && is_writable(dirname($phpCAS_logfile)))) {
$phpCAS_config['Debug file'] = $phpCAS_logfile;
phpCAS::setDebug($phpCAS_logfile);
}
try {
phpCAS::client(
CAS_VERSION_2_0,
$cas_host,
$cas_servers[$cas_host]['port'],
$cas_servers[$cas_host]['context']
);
phpCAS::setNoClearTicketsFromUrl();
phpCAS::setFixedServiceURL($service_url);
// Set extra CURL options
if (isset($cas_servers[$cas_host]['extra_curl_options']) && is_array($cas_servers[$cas_host]['extra_curl_options'])) {
foreach($cas_servers[$cas_host]['extra_curl_options'] as $opt => $value) {
if (is_string($opt) && substr($opt, 0, 7) == 'CURLOPT' && defined($opt))
$opt = constant($opt);
phpCAS::setExtraCurlOption($opt, $value);
}
}
if ($cas_servers[$cas_host]['insecure']) {
$phpCAS_config['Insecure'] = 'Yes';
$phpCAS_config['Base URL'] = 'http://'.$cas_host.($cas_servers[$cas_host]['port']?':'.$cas_servers[$cas_host]['port']:'').$cas_servers[$cas_host]['context'];
// Remove trailing slash if present
if (substr($phpCAS_config['Base URL'], -1)=='/')
$phpCAS_config['Base URL'] = substr($phpCAS_config['Base URL'], 0, -1);
$phpCAS_config['Login URL'] = $phpCAS_config['Base URL']."/login?service=".urlencode($service_url);
$phpCAS_config['Logout URL'] = $phpCAS_config['Base URL']."/logout";
$phpCAS_config['Service validate URL'] = $phpCAS_config['Base URL']."/serviceValidate";
phpCAS::setServerLoginURL($phpCAS_config['Login URL']);
phpCAS::setServerLogoutURL($phpCAS_config['Logout URL']);
phpCAS::setServerServiceValidateURL($phpCAS_config['Service validate URL']);
// Be sure SSL validation is disabled
$cas_servers[$cas_host]['ssl_validation'] = false;
}
else
$phpCAS_config['Insecure'] = 'No';
if ($cas_servers[$cas_host]['ssl_validation'] === true) {
if (is_readable($cas_servers[$cas_host]['ssl_cacert_path'])) {
$phpCAS_config['SSL Validation'] = 'Enabled';
$phpCAS_config['SSL CA Cert Validation File'] = $cas_servers[$cas_host]['ssl_cacert_path'];
$phpCAS_config['SSL CN Validation'] = (
$cas_servers[$cas_host]['ssl_cn_validation']?
'Enabled':'Disabled'
);
phpCAS::setCasServerCACert(
$cas_servers[$cas_host]['ssl_cacert_path'],
$cas_servers[$cas_host]['ssl_cn_validation']
);
}
else {
$warnings[] = 'SSL validation enable for this server but CA Cert file configured does not exists or is not readable';
$phpCAS_config['SSL Validation'] = 'Disabled';
phpCAS::setNoCasServerValidation();
}
}
else {
$phpCAS_config['SSL Validation'] = 'Disabled';
phpCAS::setNoCasServerValidation();
}
phpCAS::setCacheTimesForAuthRecheck(0);
}
catch (CAS_GracefullTerminationException $e) {
$warnings[] = 'PhpCAS return exception';
return false;
}
return true;
}
function local_logout() {
unset($_SESSION['session_url']);
unset($_SESSION['phpCAS']);
unset($_SESSION['user']);
unset($_SESSION['attributes']);
return !isset($_SESSION['phpCAS']);
}
function json_output($success=true, $data=null, $return_code=200, $exit=true) {
global $warnings, $phpCAS_config, $phpCAS_logfile, $cas_servers;
// Retreive phpCAS logs
$logs = false;
if (is_writable($phpCAS_logfile)) {
$lines = file($phpCAS_logfile);
if (is_array($lines)) {
$logs = implode('',$lines);
}
else {
$warnings[] = "Error reading PhpCAS debug log file ($phpCAS_logfile).";
}
}
else {
$warnings[] = "PhpCAS debug log file does not exists or is not writable ($phpCAS_logfile).";
}
// Compute return data
$return = array(
'success' => $success,
'data' => $data,
'logs' => $logs,
'warnings' => $warnings,
'config' => $phpCAS_config,
'cas_servers' => $cas_servers,
'cas_server' => $_SESSION['cas_server'],
'user' => $_SESSION['user'],
'attributes' => $_SESSION['attributes'],
);
// Handle JSON output
http_response_code($return_code);
header('Content-Type: application/json; charset=utf-8');
echo json_encode($return);
if ($exit) exit();
}
// Handle API call
if (isset($_REQUEST['do'])) {
switch($_REQUEST['do']) {
case 'status':
json_output(init_phpCAS());
break;
case 'change_server':
if (!isset($_REQUEST['server'])) {
$warnings[] = "Invalid parameters: selected CAS server is missing.";
json_output(false);
}
if (!isset($cas_servers[$_REQUEST['server']])) {
$warnings[] = "Invalid CAS server choiced";
json_output(false);
}
if ($_SESSION['cas_server'] != $_REQUEST['server']) {
local_logout();
}
$cas_host = $_SESSION['cas_server'] = $_REQUEST['server'];
json_output(init_phpCAS());
break;
case 'login':
if (!init_phpCAS()) json_output(false);
if (phpCAS::isAuthenticated()) {
$warnings[] = 'Already authenticated. Please logout (at least local logout) before.';
json_output(false);
}
json_output(true, array('login_url' => phpCAS::getServerLoginURL()));
break;
case 'validate_ticket':
if (!init_phpCAS()) json_output(false);
if (phpCAS::isAuthenticated()) {
$_SESSION['user'] = phpCAS::getUser();
$_SESSION['attributes'] = phpCAS::getAttributes();
json_output(true);
}
else {
$warnings[] = 'Fail to validate ticket, please check logs.';
json_output(false);
}
break;
case 'logout':
if (!init_phpCAS()) json_output(false);
if (!phpCAS::isAuthenticated()) {
$warnings[] = 'Not currently authenticated.';
json_output(false);
}
if (!local_logout()) {
$warnings[] = 'Fail to purge local session.';
json_output(false);
}
$logout_url = phpCAS::getServerLogoutURL().'?<se></se>rvice='.$service_url;
json_output(true, array('logout_url' => $logout_url));
break;
case 'locallogout':
unset($_SESSION['session_url']);
unset($_SESSION['phpCAS']);
unset($_SESSION['user']);
unset($_SESSION['attributes']);
json_output();
break;
case 'truncatelog':
$success = false;
if (is_file($phpCAS_logfile)) {
if (!is_writable(dirname($phpCAS_logfile))) {
$warnings[] = 'Logs directory is not writable ('.dirname($phpCAS_logfile).').';
json_output(false);
}
$fh = fopen($phpCAS_logfile, 'w');
fclose($fh);
}
json_output(true);
break;
default:
$warnings[] = 'Invalid request';
json_output(false);
}
}
?>
<html>
<head>
<title>Test CAS</title>
<script src="jquery-3.6.0.min.js"></script>
<script src="app.js"></script>
<style>
h2 {
border-bottom: 1px solid;
}
strong {
font-size: 0.9em;
}
em {
font-size: 0.8em;
}
#warnings {
color: #fff;
background-color: #C56E6E;
}
pre {
margin-left: 1em;
padding: 1em;
border-left: 1px solid;
background-color: #eee;
font-size: 0.9em;
}
/* Defaulty hide UI components (expect menu) */
#cas_server_selection, #cas_config, #user_info, #warnings, #cas_logs {
display: none;
}
</style>
</style>
<body>
<h1>Test CAS Application</h1>
<div id='cas_server_selection'>
<h2>CAS server selection</h2>
<form action='index.php' method='POST'>
<label for='server'>CAS server</label> :
<select name='server' id='select_server'></select>
</form>
</div>
<div id='cas_config'>
<h2>PhpCAS configuration</h2>
<ul></ul>
</div>
<h2>Menu</h2>
<ul>
<li><button id='login_btn'>Login</button></li>
<li><button id='caslogout_btn'>Logout on CAS server</button></li>
<li><button id='locallogout_btn'>Logout on local application</button></li>
<li><button id='truncatelog_btn'>Truncate Debug log file content</button></li>
</ul>
<div id='user_info'></div>
<div id='warnings'></div>
<div id='cas_logs'>
<h2>PhpCAS Debug logs</h2>
<pre></pre>
</div
</body>
</html>

2
jquery-3.6.0.min.js vendored Normal file

File diff suppressed because one or more lines are too long

2
logs/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.log
*.log.*