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);
|
||||
Reference in New Issue
Block a user