You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
133 lines
5.2 KiB
PHP
133 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services {
|
|
final class Config
|
|
{
|
|
public static array $values = [
|
|
'key' => 'cookie-auth-integrity-test-key-0001',
|
|
'enable_login_bind_ip' => 'false',
|
|
];
|
|
|
|
public static function get($name, $default = null)
|
|
{
|
|
return self::$values[$name] ?? $default;
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace App\Utils {
|
|
final class Cookie
|
|
{
|
|
public static array $writes = [];
|
|
|
|
public static function set($values, $expires): void
|
|
{
|
|
self::$writes[] = ['values' => $values, 'expires' => $expires];
|
|
foreach ($values as $name => $value) {
|
|
if ($value === null) {
|
|
unset($_COOKIE[$name]);
|
|
} else {
|
|
$_COOKIE[$name] = (string) $value;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static function get($name)
|
|
{
|
|
return $_COOKIE[$name] ?? '';
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace {
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Models\User;
|
|
use App\Services\Auth\Cookie as CookieAuth;
|
|
use App\Services\Auth\SignedCookieSession;
|
|
use App\Services\Config;
|
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
|
|
$assert = static function ($condition, string $message): void {
|
|
if (!$condition) {
|
|
throw new RuntimeException($message);
|
|
}
|
|
};
|
|
$encode = static function (string $value): string {
|
|
return rtrim(strtr(base64_encode($value), '+/', '-_'), '=');
|
|
};
|
|
|
|
$capsule = new Capsule();
|
|
$capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default');
|
|
$capsule->setAsGlobal();
|
|
$capsule->bootEloquent();
|
|
$capsule->schema('default')->create('user', static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->string('email');
|
|
$table->string('pass');
|
|
$table->integer('enable')->default(1);
|
|
$table->integer('is_admin')->default(0);
|
|
$table->integer('login_session_version')->default(0);
|
|
});
|
|
$user = new User();
|
|
$user->id = 7;
|
|
$user->email = 'cookie@example.test';
|
|
$user->pass = 'unused-password-hash';
|
|
$user->enable = 1;
|
|
$user->is_admin = 0;
|
|
$user->login_session_version = 0;
|
|
$user->save();
|
|
|
|
$_SERVER['REMOTE_ADDR'] = '203.0.113.7';
|
|
$_COOKIE = [
|
|
'uid' => '7', 'email' => $user->email, 'key' => 'legacy-key', 'ip' => 'legacy-ip',
|
|
'expire_in' => (string) (time() + 86400), 'login_session_version' => '0',
|
|
];
|
|
$auth = new CookieAuth();
|
|
$auth->login(7, 300);
|
|
$assert(count(\App\Utils\Cookie::$writes) === 2,
|
|
'Cookie login did not atomically issue the signed cookie and expire the legacy set.');
|
|
$assert(array_keys(\App\Utils\Cookie::$writes[0]['values']) === [SignedCookieSession::COOKIE_NAME],
|
|
'Cookie login issued more than the single signed credential.');
|
|
foreach (['uid', 'email', 'key', 'ip', 'expire_in', 'login_session_version'] as $legacyName) {
|
|
$assert(!isset($_COOKIE[$legacyName]), 'Cookie login retained legacy credential ' . $legacyName . '.');
|
|
}
|
|
$assert($auth->getUser()->isLogin === true, 'A freshly issued signed cookie did not authenticate.');
|
|
|
|
$token = $_COOKIE[SignedCookieSession::COOKIE_NAME];
|
|
[$prefix, $payload, $signature] = explode('.', $token);
|
|
$padding = (4 - strlen($payload) % 4) % 4;
|
|
$claims = json_decode(base64_decode(strtr($payload, '-_', '+/') . str_repeat('=', $padding), true),
|
|
true, 16, JSON_THROW_ON_ERROR);
|
|
$claims['expires_at'] += 86400 * 365;
|
|
$_COOKIE[SignedCookieSession::COOKIE_NAME] = $prefix . '.'
|
|
. $encode(json_encode($claims, JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR)) . '.' . $signature;
|
|
$assert($auth->getUser()->isLogin === false,
|
|
'Changing only expires_at reactivated or extended the signed cookie.');
|
|
|
|
$_COOKIE = [
|
|
'uid' => '7', 'email' => $user->email, 'key' => 'legacy-key', 'ip' => 'legacy-ip',
|
|
'expire_in' => (string) (time() + 86400), 'login_session_version' => '0',
|
|
];
|
|
$assert($auth->getUser()->isLogin === false, 'The retired six-cookie credential was accepted.');
|
|
|
|
$_COOKIE = [SignedCookieSession::COOKIE_NAME => SignedCookieSession::issue(7, 0, time() + 300)];
|
|
User::where('id', 7)->update(['login_session_version' => 1]);
|
|
$assert($auth->getUser()->isLogin === false, 'A revoked signed cookie version was accepted.');
|
|
|
|
Config::$values['enable_login_bind_ip'] = 'true';
|
|
$_COOKIE = [SignedCookieSession::COOKIE_NAME => SignedCookieSession::issue(7, 1, time() + 300, $_SERVER['REMOTE_ADDR'])];
|
|
$assert($auth->getUser()->isLogin === true, 'An IP-bound signed cookie was rejected at its issuing address.');
|
|
$_SERVER['REMOTE_ADDR'] = '203.0.113.8';
|
|
$assert($auth->getUser()->isLogin === false, 'An IP-bound signed cookie was accepted from another address.');
|
|
|
|
\App\Utils\Cookie::$writes = [];
|
|
$auth->logout();
|
|
$assert(count(\App\Utils\Cookie::$writes) === 2
|
|
&& array_key_exists(SignedCookieSession::COOKIE_NAME, \App\Utils\Cookie::$writes[0]['values']),
|
|
'Cookie logout did not expire the signed credential and legacy set.');
|
|
|
|
echo "Cookie auth single-token integrity, revocation, IP binding and clean break passed.\n";
|
|
}
|