OpenFlyers SSO WordPress plugin 1.1.1
Import of the 1.1.0 archive plus a CLI guard on the two test scripts: served over HTTP from wp-content/plugins they would delete the plugin settings and create test accounts on the site.
This commit is contained in:
184
tests/smoke-test.php
Normal file
184
tests/smoke-test.php
Normal file
@@ -0,0 +1,184 @@
|
||||
<?php
|
||||
|
||||
// Command-line only: this script rewrites settings and accounts, it must never run from a web request.
|
||||
if (PHP_SAPI !== 'cli') {
|
||||
exit;
|
||||
}
|
||||
/**
|
||||
* Smoke test for the WordPress plugin protocol client, run outside WordPress.
|
||||
* Checks the HTTP signature generation and the response verification (Digest + Signature)
|
||||
* against a locally generated certificate, the same way the OpenFlyers server does it.
|
||||
*/
|
||||
|
||||
define('ABSPATH', __DIR__);
|
||||
define('OF_SSO_VERSION', 'test');
|
||||
|
||||
class OF_SSO_Logger
|
||||
{
|
||||
public static function log($message)
|
||||
{
|
||||
echo " [log] $message\n";
|
||||
}
|
||||
}
|
||||
|
||||
require __DIR__ . '/../includes/class-of-sso-client.php';
|
||||
|
||||
$dir = sys_get_temp_dir() . '/of-sso-test-certs';
|
||||
@mkdir($dir);
|
||||
|
||||
// Generate a throwaway key pair and self-signed certificate.
|
||||
$key = openssl_pkey_new(array('private_key_bits' => 2048, 'private_key_type' => OPENSSL_KEYTYPE_RSA));
|
||||
$csr = openssl_csr_new(array('commonName' => 'test.local'), $key);
|
||||
$crt = openssl_csr_sign($csr, null, $key, 3650);
|
||||
openssl_pkey_export($key, $keyPem);
|
||||
openssl_x509_export($crt, $crtPem);
|
||||
file_put_contents("$dir/sign.key", $keyPem);
|
||||
file_put_contents("$dir/sign_cert.crt", $crtPem);
|
||||
file_put_contents("$dir/auth.key", $keyPem);
|
||||
file_put_contents("$dir/auth_cert.crt", $crtPem);
|
||||
file_put_contents("$dir/server_sign.crt", $crtPem);
|
||||
|
||||
$config = array(
|
||||
'client_id' => 'testclient',
|
||||
'client_secret' => 'secret',
|
||||
'authorize_uri' => 'https://openflyers.com/demo/oauth/authorize.php',
|
||||
'token_uri' => 'https://openflyers.com/demo/oauth/access_token.php',
|
||||
'resource_uri' => 'https://openflyers.com/demo/oauth/resources.php',
|
||||
'revoke_uri' => 'https://openflyers.com/demo/oauth/revoke.php',
|
||||
'auth_cert' => "$dir/auth_cert.crt",
|
||||
'auth_key' => "$dir/auth.key",
|
||||
'sign_cert' => "$dir/sign_cert.crt",
|
||||
'sign_key' => "$dir/sign.key",
|
||||
'ca_cert' => '',
|
||||
'server_sign_cert' => "$dir/server_sign.crt",
|
||||
'key_passphrase' => '',
|
||||
'send_x_ssl_cert' => 0,
|
||||
);
|
||||
|
||||
$client = new OF_SSO_Client($config);
|
||||
$reflection = new ReflectionClass($client);
|
||||
$fail = 0;
|
||||
|
||||
function check($label, $ok)
|
||||
{
|
||||
global $fail;
|
||||
echo ($ok ? ' OK ' : ' FAIL ') . $label . "\n";
|
||||
if (!$ok) {
|
||||
$fail++;
|
||||
}
|
||||
}
|
||||
|
||||
// --- PKCE -------------------------------------------------------------------
|
||||
$verifier = OF_SSO_Client::generateCodeVerifier();
|
||||
$challenge = OF_SSO_Client::computeCodeChallenge($verifier);
|
||||
check('code_verifier length is 128', strlen($verifier) === 128);
|
||||
check(
|
||||
'code_challenge is base64url of sha256(verifier)',
|
||||
$challenge === rtrim(strtr(base64_encode(hash('sha256', $verifier, true)), '+/', '-_'), '=')
|
||||
);
|
||||
check('code_challenge has no padding or unsafe chars', preg_match('#^[A-Za-z0-9_-]+$#', $challenge) === 1);
|
||||
|
||||
// --- authorize URL ----------------------------------------------------------
|
||||
$url = $client->getAuthorizeUrl('https://www.exemple.fr/?of_sso=callback', 'abc123', $challenge);
|
||||
parse_str(parse_url($url, PHP_URL_QUERY), $query);
|
||||
check('authorize URL targets the platform endpoint', strpos($url, 'https://openflyers.com/demo/oauth/authorize.php?') === 0);
|
||||
check('response_type=code', $query['response_type'] === 'code');
|
||||
check('code_challenge_method=S256', $query['code_challenge_method'] === 'S256');
|
||||
check('scope defaults to default.login', $query['scope'] === 'default.login');
|
||||
check('redirect_uri is preserved exactly', $query['redirect_uri'] === 'https://www.exemple.fr/?of_sso=callback');
|
||||
|
||||
// --- HTTP signature ---------------------------------------------------------
|
||||
$method = $reflection->getMethod('generateSignatureHeader');
|
||||
$method->setAccessible(true);
|
||||
|
||||
$postData = http_build_query(array('grant_type' => 'authorization_code', 'client_id' => 'testclient'));
|
||||
$headersToSign = array(
|
||||
'Content-Type' => 'application/x-www-form-urlencoded',
|
||||
'digest' => 'SHA-256=' . base64_encode(hash('sha256', $postData, true)),
|
||||
'(request-target)' => 'post /demo/oauth/access_token.php',
|
||||
'Host' => 'openflyers.com',
|
||||
'Date' => gmdate('D, j M Y H:i:s T'),
|
||||
);
|
||||
|
||||
$keyId = openssl_x509_fingerprint($crtPem);
|
||||
$header = $method->invoke($client, $headersToSign, $keyId, $keyPem);
|
||||
|
||||
preg_match('/keyId="([^"]+)"/', $header, $m);
|
||||
check('keyId is the signing certificate fingerprint', $m[1] === $keyId);
|
||||
preg_match('/headers="([^"]+)"/', $header, $m);
|
||||
check(
|
||||
'signed header list matches the server expectation',
|
||||
$m[1] === 'content-type digest (request-target) host date'
|
||||
);
|
||||
|
||||
// Verify exactly the way the server does: rebuild the signing string, verify with the certificate.
|
||||
preg_match('/signature="([^"]+)"/', $header, $m);
|
||||
$signingString = '';
|
||||
foreach ($headersToSign as $name => $value) {
|
||||
$signingString .= strtolower($name) . ': ' . $value . "\n";
|
||||
}
|
||||
$verified = openssl_verify(trim($signingString), base64_decode($m[1]), openssl_get_publickey($crtPem), 'RSA-SHA256');
|
||||
check('signature verifies against the certificate', $verified === 1);
|
||||
|
||||
// --- response parsing, digest and signature ---------------------------------
|
||||
$parse = $reflection->getMethod('parseResponse');
|
||||
$parse->setAccessible(true);
|
||||
|
||||
$body = json_encode(array('token_type' => 'Bearer', 'access_token' => 'abc', 'expires_in' => 3600));
|
||||
$digest = 'SHA-256=' . base64_encode(hash('sha256', $body, true));
|
||||
$date = gmdate('D, j M Y H:i:s T');
|
||||
|
||||
$serverSigningString = "date: $date\ndigest: $digest";
|
||||
openssl_sign($serverSigningString, $serverSignature, openssl_get_privatekey($keyPem), 'RSA-SHA256');
|
||||
$serverSignatureHeader = 'keyId="' . $keyId . '",algorithm="rsa-sha256",headers="date digest",signature="'
|
||||
. base64_encode($serverSignature) . '"';
|
||||
|
||||
$response = "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nDate: $date\r\nDigest: $digest\r\n"
|
||||
. "Signature: $serverSignatureHeader\r\n\r\n" . $body;
|
||||
|
||||
$result = $parse->invoke($client, $response, $config['token_uri'], true);
|
||||
check('valid signed response is accepted and decoded', is_array($result) && $result['access_token'] === 'abc');
|
||||
|
||||
// A '100 Continue' preamble must not break the parsing.
|
||||
$withContinue = "HTTP/1.1 100 Continue\r\n\r\n" . $response;
|
||||
$result = $parse->invoke($client, $withContinue, $config['token_uri'], true);
|
||||
check('100 Continue preamble is handled', is_array($result) && $result['access_token'] === 'abc');
|
||||
|
||||
// Tampered body must be rejected by the digest check.
|
||||
$tampered = str_replace($body, json_encode(array('access_token' => 'stolen')), $response);
|
||||
$result = $parse->invoke($client, $tampered, $config['token_uri'], true);
|
||||
check('tampered body is rejected (digest)', $result === false);
|
||||
|
||||
// Tampered signature must be rejected.
|
||||
$badSignature = str_replace(base64_encode($serverSignature), base64_encode(strrev($serverSignature)), $response);
|
||||
$result = $parse->invoke($client, $badSignature, $config['token_uri'], true);
|
||||
check('forged signature is rejected', $result === false);
|
||||
|
||||
// A non 200 status must be reported as an error, not decoded.
|
||||
$error = "HTTP/1.1 400 Bad Request\r\nContent-Type: application/json\r\n\r\n{\"error\":\"invalid_client\"}";
|
||||
$result = $parse->invoke($client, $error, $config['token_uri'], true);
|
||||
check('HTTP 400 is reported as a failure', $result === false);
|
||||
check('error message mentions the status', strpos($client->getLastError(), '400') !== false);
|
||||
|
||||
// A 200 whose status line merely contains "200" elsewhere must still be accepted only on the status code.
|
||||
$weird = "HTTP/1.1 500 Internal Server Error 200\r\nContent-Type: application/json\r\n\r\n{}";
|
||||
$result = $parse->invoke($client, $weird, $config['token_uri'], true);
|
||||
check('a 500 containing "200" in its reason phrase is rejected', $result === false);
|
||||
|
||||
// A "100" inside a cookie or a length must not be mistaken for a '100 Continue' preamble:
|
||||
// the body would then be split at the wrong place and the payload lost.
|
||||
$decoy = "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nSet-Cookie: s=a100b; path=/\r\nDate: $date\r\n"
|
||||
. "Digest: $digest\r\nSignature: $serverSignatureHeader\r\n\r\n" . $body;
|
||||
$result = $parse->invoke($client, $decoy, $config['token_uri'], true);
|
||||
check('a "100" inside a header value is not read as a preamble', is_array($result) && $result['access_token'] === 'abc');
|
||||
|
||||
// The web server refuses an unaccepted client certificate itself, with an HTML page and no JSON.
|
||||
$nginx = "HTTP/1.1 400 Bad Request\r\nContent-Type: text/html\r\n\r\n"
|
||||
. "<html><head><title>400 The SSL certificate error</title></head><body></body></html>";
|
||||
$result = $parse->invoke($client, $nginx, $config['token_uri'], true);
|
||||
check('an HTML certificate refusal is reported as a failure', $result === false);
|
||||
check('it is recognized as a certificate problem', $client->getLastErrorCode() === 'ssl_certificate_error');
|
||||
check('the reason reaches the error message', strpos($client->getLastError(), 'SSL certificate error') !== false);
|
||||
|
||||
echo $fail === 0 ? "\nAll checks passed.\n" : "\n$fail check(s) failed.\n";
|
||||
exit($fail === 0 ? 0 : 1);
|
||||
417
tests/wordpress-test.php
Normal file
417
tests/wordpress-test.php
Normal file
@@ -0,0 +1,417 @@
|
||||
<?php
|
||||
|
||||
// Command-line only: this script rewrites settings and accounts, it must never run from a web request.
|
||||
if (PHP_SAPI !== 'cli') {
|
||||
exit;
|
||||
}
|
||||
/**
|
||||
* Functional test of the plugin inside a real WordPress installation.
|
||||
*
|
||||
* Run it from the command line, from the root of a WordPress site where this plugin is installed:
|
||||
* php wp-content/plugins/openflyers-sso/tests/wordpress-test.php
|
||||
*
|
||||
* The site can also be named explicitly, which is needed when the plugin folder is a symbolic
|
||||
* link or a junction pointing outside the site:
|
||||
* php wordpress-test.php C:\wamp64\www\my-site
|
||||
*
|
||||
* It activates the plugin, then exercises the login start, the callback error paths,
|
||||
* the account resolution modes, the shortcodes, the settings screen and the member
|
||||
* linking field. It creates and removes its own test accounts and contacts no server.
|
||||
*/
|
||||
|
||||
define('WP_USE_THEMES', false);
|
||||
$_SERVER['HTTP_HOST'] = 'localhost';
|
||||
$_SERVER['REQUEST_URI'] = '/';
|
||||
$_SERVER['SERVER_NAME'] = 'localhost';
|
||||
$_SERVER['REQUEST_METHOD'] = 'GET';
|
||||
|
||||
$fail = 0;
|
||||
|
||||
function check($label, $ok, $detail = '')
|
||||
{
|
||||
global $fail;
|
||||
echo ($ok ? ' OK ' : ' FAIL ') . $label . ($detail !== '' ? " [$detail]" : '') . "\n";
|
||||
if (!$ok) {
|
||||
$fail++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Locate the WordPress bootstrap.
|
||||
*
|
||||
* The plugin folder is often a symbolic link or a junction to a working copy kept outside the
|
||||
* site, in which case walking up from this file lands somewhere else entirely. So an explicit
|
||||
* path wins, then the working directory, then this file's own location.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
function ofSsoFindWordPress()
|
||||
{
|
||||
global $argv;
|
||||
|
||||
$candidates = array();
|
||||
|
||||
if (!empty($argv[1])) {
|
||||
$candidates[] = rtrim($argv[1], '/\\') . '/wp-load.php';
|
||||
}
|
||||
|
||||
foreach (array(getcwd(), __DIR__) as $start) {
|
||||
$directory = $start;
|
||||
|
||||
for ($depth = 0; $depth < 8 and $directory; $depth++) {
|
||||
$candidates[] = $directory . '/wp-load.php';
|
||||
$parent = dirname($directory);
|
||||
|
||||
if ($parent === $directory) {
|
||||
break;
|
||||
}
|
||||
|
||||
$directory = $parent;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($candidates as $candidate) {
|
||||
if (is_readable($candidate)) {
|
||||
return $candidate;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
$wpLoad = ofSsoFindWordPress();
|
||||
|
||||
if ($wpLoad === null) {
|
||||
echo "WordPress introuvable.\n"
|
||||
. "Lancez ce test depuis la racine du site, ou indiquez le chemin du site :\n"
|
||||
. " php wordpress-test.php C:\\chemin\\vers\\le\\site\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
require $wpLoad;
|
||||
|
||||
// Align the fake request with the site's own address so home_url() stays consistent.
|
||||
$_SERVER['HTTP_HOST'] = (string) parse_url(get_option('home'), PHP_URL_HOST);
|
||||
require_once ABSPATH . 'wp-admin/includes/plugin.php';
|
||||
require_once ABSPATH . 'wp-admin/includes/user.php';
|
||||
// The settings screen only ever runs inside wp-admin, which loads these helpers.
|
||||
require_once ABSPATH . 'wp-admin/includes/template.php';
|
||||
require_once ABSPATH . 'wp-admin/includes/screen.php';
|
||||
|
||||
// Remove anything left by a previous run so the script is repeatable.
|
||||
foreach (array('pilote.test', 'of-7777', 'of-8888') as $login) {
|
||||
$stale = get_user_by('login', $login);
|
||||
if ($stale) {
|
||||
wp_delete_user($stale->ID);
|
||||
}
|
||||
}
|
||||
delete_option('of_sso_settings');
|
||||
|
||||
// --- activation -------------------------------------------------------------
|
||||
$activation = activate_plugin('openflyers-sso/openflyers-sso.php');
|
||||
check('plugin activates without error', !is_wp_error($activation), is_wp_error($activation) ? $activation->get_error_message() : '');
|
||||
check('plugin is listed as active', is_plugin_active('openflyers-sso/openflyers-sso.php'));
|
||||
check('protocol client class is loaded', class_exists('OF_SSO_Client'));
|
||||
check('admin class is loaded', class_exists('OF_SSO_Admin'));
|
||||
check('helper functions are available', function_exists('of_sso_login_url') && function_exists('of_sso_is_member'));
|
||||
|
||||
// The plugin file is loaded by wp-load only on the next request, so load it now.
|
||||
if (!class_exists('OF_SSO_Auth')) {
|
||||
require_once ABSPATH . 'wp-content/plugins/openflyers-sso/openflyers-sso.php';
|
||||
OF_SSO_Auth::init();
|
||||
OF_SSO_Admin::init();
|
||||
}
|
||||
|
||||
// --- settings ---------------------------------------------------------------
|
||||
$certDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'of-sso-test-certs';
|
||||
$settings = array(
|
||||
'base_url' => 'https://openflyers.com',
|
||||
'platform' => 'acat-lfcl',
|
||||
'client_id' => 'testclient',
|
||||
'client_secret' => 'secret',
|
||||
'cert_dir' => $certDir,
|
||||
'auto_create' => 0,
|
||||
'default_role' => 'subscriber',
|
||||
'debug' => 0,
|
||||
);
|
||||
update_option('of_sso_settings', OF_SSO_Admin::sanitize($settings));
|
||||
|
||||
$stored = of_sso_get_settings();
|
||||
check('settings are stored and read back', $stored['platform'] === 'acat-lfcl' && $stored['client_id'] === 'testclient');
|
||||
check('secret is kept when resubmitted empty', OF_SSO_Admin::sanitize(array('client_secret' => ''))['client_secret'] === 'secret');
|
||||
check('secret is replaced when resubmitted', OF_SSO_Admin::sanitize(array('client_secret' => 'new'))['client_secret'] === 'new');
|
||||
|
||||
// --- certificate folder -----------------------------------------------------
|
||||
// One folder is enough: the files are found by their standard names.
|
||||
$paths = of_sso_resolve_cert_paths();
|
||||
$required = array('auth_cert', 'auth_key', 'sign_cert', 'sign_key', 'server_sign_cert');
|
||||
|
||||
check(
|
||||
'the folder resolves every required certificate path',
|
||||
count(array_filter(array_intersect_key($paths, array_flip($required)))) === count($required)
|
||||
);
|
||||
check('the TLS certificate keeps its standard name', basename($paths['auth_cert']) === 'auth_cert.crt', basename($paths['auth_cert']));
|
||||
check('the signing key keeps its standard name', basename($paths['sign_key']) === 'sign.key', basename($paths['sign_key']));
|
||||
check('every required path sits in the configured folder', dirname($paths['auth_cert']) === $certDir, dirname($paths['auth_cert']));
|
||||
|
||||
// ca.crt is the exception: it only overrides the bundle used to validate the OpenFlyers server,
|
||||
// so its absence must fall back to the system store instead of blocking every call.
|
||||
$caFolder = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'of-sso-ca-test';
|
||||
if (!is_dir($caFolder)) {
|
||||
mkdir($caFolder, 0777, true);
|
||||
}
|
||||
|
||||
$caSettings = $settings;
|
||||
$caSettings['cert_dir'] = $caFolder;
|
||||
update_option('of_sso_settings', OF_SSO_Admin::sanitize($caSettings));
|
||||
check('an absent ca.crt falls back to the system store', of_sso_resolve_cert_paths()['ca_cert'] === '');
|
||||
|
||||
file_put_contents($caFolder . DIRECTORY_SEPARATOR . 'ca.crt', 'placeholder');
|
||||
check(
|
||||
'a ca.crt sitting in the folder is used',
|
||||
of_sso_resolve_cert_paths()['ca_cert'] === $caFolder . DIRECTORY_SEPARATOR . 'ca.crt'
|
||||
);
|
||||
|
||||
unlink($caFolder . DIRECTORY_SEPARATOR . 'ca.crt');
|
||||
rmdir($caFolder);
|
||||
update_option('of_sso_settings', OF_SSO_Admin::sanitize($settings));
|
||||
|
||||
// A file stored elsewhere, or under another name, can still be pointed at individually.
|
||||
$override = $settings;
|
||||
$override['sign_key'] = $certDir . DIRECTORY_SEPARATOR . 'autre-nom.key';
|
||||
update_option('of_sso_settings', OF_SSO_Admin::sanitize($override));
|
||||
$paths = of_sso_resolve_cert_paths();
|
||||
check('an explicit path overrides the folder', basename($paths['sign_key']) === 'autre-nom.key');
|
||||
check('the other paths still come from the folder', basename($paths['auth_key']) === 'auth.key');
|
||||
|
||||
// The connection test must never announce success when nothing could be sent.
|
||||
$missing = $settings;
|
||||
$missing['cert_dir'] = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'of-sso-absent';
|
||||
update_option('of_sso_settings', OF_SSO_Admin::sanitize($missing));
|
||||
$verdict = of_sso_get_client()->testConnection(of_sso_get_redirect_uri());
|
||||
check('the connection test reports a problem when certificates are missing', $verdict['status'] !== 'ok', $verdict['status']);
|
||||
|
||||
update_option('of_sso_settings', OF_SSO_Admin::sanitize($settings));
|
||||
|
||||
$client = of_sso_get_client();
|
||||
check('client is built from the settings', $client instanceof OF_SSO_Client);
|
||||
check(
|
||||
'the platform code becomes a path segment',
|
||||
strpos($client->getAuthorizeUrl('http://x/', 's', 'c'), 'https://openflyers.com/acat-lfcl/oauth/authorize.php') === 0
|
||||
);
|
||||
|
||||
// A dedicated or local install answers at the domain root, with no platform code.
|
||||
$rootSettings = $settings;
|
||||
$rootSettings['platform'] = '';
|
||||
$rootSettings['base_url'] = 'https://openflyers.exemple.fr';
|
||||
update_option('of_sso_settings', OF_SSO_Admin::sanitize($rootSettings));
|
||||
|
||||
$rootClient = of_sso_get_client();
|
||||
check('a platform served at the root still builds a client', $rootClient instanceof OF_SSO_Client);
|
||||
check(
|
||||
'no empty segment is inserted in the endpoint URL',
|
||||
strpos($rootClient->getAuthorizeUrl('http://x/', 's', 'c'), 'https://openflyers.exemple.fr/oauth/authorize.php') === 0,
|
||||
substr($rootClient->getAuthorizeUrl('http://x/', 's', 'c'), 0, 60)
|
||||
);
|
||||
|
||||
// Without a client identifier there is nothing to build, whatever the rest holds.
|
||||
$noClient = $settings;
|
||||
$noClient['client_id'] = '';
|
||||
update_option('of_sso_settings', OF_SSO_Admin::sanitize($noClient));
|
||||
check('no client is built without a client_id', of_sso_get_client() === false);
|
||||
|
||||
update_option('of_sso_settings', OF_SSO_Admin::sanitize($settings));
|
||||
$client = of_sso_get_client();
|
||||
check('callback URL is derived from the site URL', of_sso_get_redirect_uri() === home_url('/?of_sso=callback'), of_sso_get_redirect_uri());
|
||||
|
||||
// --- login start ------------------------------------------------------------
|
||||
// wp_redirect() is intercepted so the flow can be observed without leaving the script.
|
||||
$redirects = array();
|
||||
add_filter(
|
||||
'wp_redirect',
|
||||
function ($location) use (&$redirects) {
|
||||
$redirects[] = $location;
|
||||
throw new Exception('redirect');
|
||||
},
|
||||
10,
|
||||
1
|
||||
);
|
||||
|
||||
function capture($callable)
|
||||
{
|
||||
try {
|
||||
ob_start();
|
||||
$callable();
|
||||
ob_end_clean();
|
||||
} catch (Exception $e) {
|
||||
ob_end_clean();
|
||||
}
|
||||
}
|
||||
|
||||
$_GET = array('of_sso' => 'login', 'redirect_to' => rawurlencode(home_url('/espace-membres/')));
|
||||
capture(array('OF_SSO_Auth', 'route'));
|
||||
|
||||
check('starting a login triggers a redirect', count($redirects) === 1);
|
||||
$authorizeUrl = end($redirects);
|
||||
check(
|
||||
'the member is sent to the platform authorize endpoint',
|
||||
strpos($authorizeUrl, 'https://openflyers.com/acat-lfcl/oauth/authorize.php?') === 0,
|
||||
substr($authorizeUrl, 0, 70)
|
||||
);
|
||||
|
||||
parse_str((string) parse_url($authorizeUrl, PHP_URL_QUERY), $query);
|
||||
check('client_id is sent', $query['client_id'] === 'testclient');
|
||||
check('PKCE challenge is sent', !empty($query['code_challenge']) && $query['code_challenge_method'] === 'S256');
|
||||
check('redirect_uri matches the registered callback', $query['redirect_uri'] === of_sso_get_redirect_uri());
|
||||
check('scope is default.login', $query['scope'] === 'default.login');
|
||||
|
||||
$state = $query['state'];
|
||||
$pending = get_transient('of_sso_state_' . $state);
|
||||
check('the pending request is stored under its state', is_array($pending) && !empty($pending['code_verifier']));
|
||||
check('the requested destination is kept', $pending['redirect_to'] === home_url('/espace-membres/'));
|
||||
check(
|
||||
'the stored verifier matches the sent challenge',
|
||||
OF_SSO_Client::computeCodeChallenge($pending['code_verifier']) === $query['code_challenge']
|
||||
);
|
||||
|
||||
// --- callback error paths ---------------------------------------------------
|
||||
$redirects = array();
|
||||
$_GET = array('of_sso' => 'callback', 'code' => 'abc', 'state' => 'unknown-state');
|
||||
capture(array('OF_SSO_Auth', 'route'));
|
||||
check('an unknown state is refused', count($redirects) === 1 && strpos(end($redirects), 'of_sso_error=1') !== false);
|
||||
|
||||
$redirects = array();
|
||||
$_GET = array('of_sso' => 'callback', 'error' => 'access_denied');
|
||||
capture(array('OF_SSO_Auth', 'route'));
|
||||
check('a refused authorization is handled', count($redirects) === 1 && strpos(end($redirects), 'of_sso_error=1') !== false);
|
||||
|
||||
$redirects = array();
|
||||
$_GET = array('of_sso' => 'callback', 'state' => $state);
|
||||
capture(array('OF_SSO_Auth', 'route'));
|
||||
check('a callback without code is refused', count($redirects) === 1);
|
||||
|
||||
// A used state must not be replayable.
|
||||
$redirects = array();
|
||||
$_GET = array('of_sso' => 'callback', 'code' => 'abc', 'state' => $state);
|
||||
capture(array('OF_SSO_Auth', 'route'));
|
||||
check('a consumed state cannot be replayed', get_transient('of_sso_state_' . $state) === false);
|
||||
|
||||
$_GET = array();
|
||||
|
||||
// --- account resolution -----------------------------------------------------
|
||||
$resolve = new ReflectionMethod('OF_SSO_Auth', 'resolveUser');
|
||||
$resolve->setAccessible(true);
|
||||
|
||||
$result = $resolve->invoke(null, 4242, array(), $client);
|
||||
check(
|
||||
'an unlinked member is refused when auto-creation is off',
|
||||
is_wp_error($result) && $result->get_error_code() === 'of_sso_no_account'
|
||||
);
|
||||
|
||||
$linkedId = wp_insert_user(array('user_login' => 'pilote.test', 'user_pass' => 'x', 'user_email' => 'pilote@example.test'));
|
||||
update_user_meta($linkedId, OF_SSO_USER_META, '4242');
|
||||
|
||||
$result = $resolve->invoke(null, 4242, array(), $client);
|
||||
check('a linked member is recognized', $result instanceof WP_User && (int) $result->ID === (int) $linkedId);
|
||||
|
||||
$settings['auto_create'] = 1;
|
||||
update_option('of_sso_settings', OF_SSO_Admin::sanitize($settings));
|
||||
|
||||
$result = $resolve->invoke(null, 7777, array(), $client);
|
||||
check('an unknown member is created when auto-creation is on', $result instanceof WP_User, is_wp_error($result) ? $result->get_error_message() : '');
|
||||
check('the created account carries the OpenFlyers identifier', get_user_meta($result->ID, OF_SSO_USER_META, true) === '7777');
|
||||
check('the created account has the configured role', in_array('subscriber', $result->roles, true));
|
||||
|
||||
// The profile filter is the documented way to feed name and e-mail from a report.
|
||||
add_filter(
|
||||
'of_sso_user_profile',
|
||||
function ($profile) {
|
||||
return array('user_email' => 'nouveau@example.test', 'display_name' => 'Nouveau Membre');
|
||||
}
|
||||
);
|
||||
|
||||
$result = $resolve->invoke(null, 8888, array(), $client);
|
||||
$created = get_user_by('id', $result->ID);
|
||||
check('the profile filter fills the e-mail', $created->user_email === 'nouveau@example.test', $created->user_email);
|
||||
check('the profile filter fills the display name', $created->display_name === 'Nouveau Membre', $created->display_name);
|
||||
remove_all_filters('of_sso_user_profile');
|
||||
|
||||
// --- shortcodes -------------------------------------------------------------
|
||||
check('the login shortcode is registered', shortcode_exists('openflyers_login'));
|
||||
check('the member shortcode is registered', shortcode_exists('openflyers_membre'));
|
||||
|
||||
$html = do_shortcode('[openflyers_login]');
|
||||
check('the login shortcode renders a link to the login route', strpos($html, 'of_sso=login') !== false);
|
||||
|
||||
$html = do_shortcode('[openflyers_membre]Contenu reserve[/openflyers_membre]');
|
||||
check('reserved content is hidden from visitors', strpos($html, 'Contenu reserve') === false);
|
||||
check('visitors are offered the login button', strpos($html, 'of_sso=login') !== false);
|
||||
|
||||
wp_set_current_user($linkedId);
|
||||
$html = do_shortcode('[openflyers_membre]Contenu reserve[/openflyers_membre]');
|
||||
check('reserved content is shown to a linked member', strpos($html, 'Contenu reserve') !== false);
|
||||
check('of_sso_is_member() recognizes a linked member', of_sso_is_member() === true);
|
||||
|
||||
wp_set_current_user(0);
|
||||
check('of_sso_is_member() is false for a visitor', of_sso_is_member() === false);
|
||||
|
||||
// --- settings screen --------------------------------------------------------
|
||||
wp_set_current_user(1);
|
||||
|
||||
ob_start();
|
||||
OF_SSO_Admin::renderPage();
|
||||
$page = ob_get_clean();
|
||||
|
||||
check('the settings page renders', strlen($page) > 1000, strlen($page) . ' bytes');
|
||||
check('it shows the callback URL to declare', strpos($page, esc_html(of_sso_get_redirect_uri())) !== false);
|
||||
check('it exposes every certificate field', substr_count($page, 'of_sso_settings[') >= 15);
|
||||
check('it offers the certificate folder field', strpos($page, 'name="of_sso_settings[cert_dir]"') !== false);
|
||||
check('it offers the connection test button', strpos($page, 'name="of_sso_test_connection"') !== false);
|
||||
check('the stored secret is never printed back', strpos($page, 'value="secret"') === false);
|
||||
|
||||
// The diagnostics must flag what is missing, whatever the machine running the test holds.
|
||||
$absent = $settings;
|
||||
$absent['cert_dir'] = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'of-sso-absent';
|
||||
update_option('of_sso_settings', OF_SSO_Admin::sanitize($absent));
|
||||
|
||||
ob_start();
|
||||
OF_SSO_Admin::renderPage();
|
||||
$brokenPage = ob_get_clean();
|
||||
|
||||
check('the diagnostics block flags unreadable certificates', strpos($brokenPage, '✗') !== false);
|
||||
check('it names the file it could not read', strpos($brokenPage, 'of-sso-absent') !== false);
|
||||
|
||||
update_option('of_sso_settings', OF_SSO_Admin::sanitize($settings));
|
||||
|
||||
// --- user profile field -----------------------------------------------------
|
||||
$_POST = array(
|
||||
'of_sso_user_nonce' => wp_create_nonce('of_sso_user_field'),
|
||||
'of_sso_user_id' => '9999',
|
||||
);
|
||||
OF_SSO_Admin::saveUserField($linkedId);
|
||||
check('an administrator can link a member from the user profile', get_user_meta($linkedId, OF_SSO_USER_META, true) === '9999');
|
||||
|
||||
$_POST['of_sso_user_nonce'] = 'forged';
|
||||
$_POST['of_sso_user_id'] = '1';
|
||||
OF_SSO_Admin::saveUserField($linkedId);
|
||||
check('a forged nonce is rejected', get_user_meta($linkedId, OF_SSO_USER_META, true) === '9999');
|
||||
|
||||
$_POST = array();
|
||||
|
||||
// --- PHP notices ------------------------------------------------------------
|
||||
$log = WP_CONTENT_DIR . '/debug.log';
|
||||
if (file_exists($log)) {
|
||||
$lines = array_filter(
|
||||
file($log),
|
||||
function ($line) {
|
||||
// Header warnings are expected: the flow is exercised from the command line.
|
||||
return strpos($line, 'openflyers-sso') !== false && strpos($line, 'Cannot modify header information') === false;
|
||||
}
|
||||
);
|
||||
check('no PHP notice from the plugin', count($lines) === 0, implode(' | ', array_slice($lines, 0, 3)));
|
||||
} else {
|
||||
check('no PHP notice from the plugin', true, 'no debug.log');
|
||||
}
|
||||
|
||||
echo $fail === 0 ? "\nAll checks passed.\n" : "\n$fail check(s) failed.\n";
|
||||
exit($fail === 0 ? 0 : 1);
|
||||
Reference in New Issue
Block a user