Files
2026-11-11 14:22:02 +08:00

118 lines
7.9 KiB
PHP

<?php
declare(strict_types=1);
use app\adminapi\service\IdentitySessionService;
use app\adminapi\validate\LoginValidate;
use app\common\cache\AdminAccountSafeCache;
use app\common\service\ConfigService;
use app\adminapi\controller\identity\SessionController;
use think\exception\HttpResponseException;
use think\facade\Db;
require dirname(__DIR__) . '/bootstrap.php';
require_once 'C:/zyt/server/vendor/topthink/framework/src/helper.php';
$checks = 0;
function verifyCheck(bool $ok, string $label): void {
global $checks;
if (!$ok) { throw new RuntimeException($label); }
$checks++;
}
function verifyRejected(array $params): void {
try { IdentitySessionService::verify($params); }
catch (HttpResponseException $error) {
verifyCheck((int) $error->getResponse()->getData()['code'] !== 1, 'failed credential accepted');
return;
}
throw new RuntimeException('expected failed credential');
}
$runtime = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'iam-password-verify-' . bin2hex(random_bytes(10));
mkdir($runtime, 0700, true);
try {
$app = new class(dirname(__DIR__) . DIRECTORY_SEPARATOR) extends think\App {
public function loadEnv(string $envName = ''): void { /* never load real environment files */ }
};
$app->setRuntimePath($runtime . DIRECTORY_SEPARATOR);
$app->initialize();
$app->config->set(['default' => 'fixture', 'connections' => ['fixture' => [
'type' => 'sqlite', 'database' => $runtime . '/fixture.sqlite', 'prefix' => 'f_',
'fields_strict' => true, 'trigger_sql' => false, 'break_reconnect' => false,
]]], 'database');
$app->config->set(['default' => 'file', 'stores' => ['file' => ['type' => 'File',
'path' => $runtime . '/cache/', 'prefix' => 'fixture-']]], 'cache');
$app->config->set(['unique_identification' => 'verify-fixture-salt'], 'project');
Db::execute('CREATE TABLE f_admin (id INTEGER PRIMARY KEY,root INTEGER DEFAULT 0,name TEXT,account TEXT,password TEXT,
disable INTEGER DEFAULT 0,is_paw INTEGER DEFAULT 1,work_wechat_userid TEXT DEFAULT "",avatar TEXT DEFAULT "",
multipoint_login INTEGER DEFAULT 1,login_time INTEGER,login_ip TEXT,create_time INTEGER,update_time INTEGER,delete_time INTEGER)');
Db::execute('CREATE TABLE f_admin_session (id INTEGER PRIMARY KEY,admin_id INTEGER,terminal INTEGER,token TEXT,
expire_time INTEGER,create_time INTEGER,update_time INTEGER)');
Db::execute('CREATE TABLE f_config (id INTEGER PRIMARY KEY,type TEXT,name TEXT,value TEXT,create_time INTEGER,update_time INTEGER)');
$password = 'Fixture-Verify-2026';
$hash = create_password($password, 'verify-fixture-salt');
foreach ([1, 2, 3] as $id) {
Db::name('admin')->insert(['id' => $id, 'account' => 'verify-' . $id, 'name' => 'Fixture ' . $id,
'password' => $hash, 'root' => $id === 1 ? 1 : 0, 'disable' => $id === 2 ? 1 : 0,
'is_paw' => $id === 3 ? 0 : 1, 'login_time' => 123, 'login_ip' => 'fixture',
'create_time' => 123, 'update_time' => 123]);
}
foreach ([1, 7] as $terminal) {
Db::name('admin_session')->insert(['admin_id' => 1, 'terminal' => $terminal,
'token' => 'fixture-existing-' . $terminal, 'expire_time' => time() + 600,
'create_time' => 123, 'update_time' => 123]);
}
$beforeSessions = Db::name('admin_session')->order('id')->select()->toArray();
$beforeAccounts = Db::name('admin')->order('id')->select()->toArray();
$identity = IdentitySessionService::verify(['account' => 'verify-1', 'password' => $password]);
verifyCheck(array_keys($identity) === ['id', 'account', 'name', 'disable', 'requires_password_change', 'requires_wecom_binding'], 'identity fields expanded');
verifyCheck($identity['id'] === 1 && $identity['account'] === 'verify-1' && $identity['disable'] === 0, 'identity mismatch');
verifyCheck($identity['requires_password_change'] === false && is_bool($identity['requires_wecom_binding']), 'gate types mismatch');
verifyCheck(IdentitySessionService::verify(['account' => 'verify-3', 'password' => $password])['requires_password_change'] === true, 'first-password gate missing');
foreach ([['account' => 'verify-1', 'password' => 'incorrect'], ['account' => 'absent', 'password' => $password],
['account' => 'verify-2', 'password' => $password], ['account' => [], 'password' => $password],
['account' => 'verify-1', 'password' => $password, 'terminal' => 1],
['account' => 'verify-1', 'password' => $password, 'root' => 1],
['account' => "verify-1\0", 'password' => $password], ['account' => 'verify-1', 'password' => '']] as $invalid) {
verifyRejected($invalid);
}
$app->env->set('APP_DEBUG', '1');
$app->env->set('LOCAL_DEBUG_LOGIN_SKIP_PASSWORD', '1');
$validator = new LoginValidate();
verifyCheck($validator->password('incorrect', '', ['account' => 'verify-1']) === true, 'debug control was not enabled');
verifyCheck($validator->strictPassword('incorrect', '', ['account' => 'verify-1']) !== true, 'strict verifier inherited debug bypass');
verifyCheck((new LoginValidate())->scene('verify')->check(['account' => 'verify-1', 'password' => $password]), 'verify scene wrongly requires terminal');
verifyCheck(!(new LoginValidate())->scene('verify')->check(['account' => 'verify-1', 'password' => 'incorrect']), 'verify scene accepted wrong password');
ConfigService::set('admin_login', 'login_restrictions', 1);
ConfigService::set('admin_login', 'password_error_times', 2);
ConfigService::set('admin_login', 'limit_login_time', 1);
$safe = new AdminAccountSafeCache();
$safe->relieve();
verifyCheck($validator->strictPassword('incorrect', '', ['account' => 'verify-1']) !== true, 'first failure accepted');
verifyCheck($validator->strictPassword('incorrect', '', ['account' => 'verify-1']) !== true, 'second failure accepted');
verifyCheck($validator->strictPassword($password, '', ['account' => 'verify-1']) !== true, 'source login rate limit bypassed');
$safe->relieve();
$app->request->setMethod('POST');
$app->request->withPost(['account' => 'verify-1', 'password' => $password]);
$controller = new SessionController($app);
$response = $controller->verify()->getData();
verifyCheck($response['code'] === 1 && $response['data'] === $identity, 'controller verify contract mismatch');
$app->request->withPost(['account' => 'verify-1', 'password' => $password, 'terminal' => 1]);
verifyCheck($controller->verify()->getData()['code'] !== 1, 'controller accepted unsupported terminal');
Db::name('admin')->where('id', 1)->update(['password' => create_password('New-Fixture-Only', 'verify-fixture-salt')]);
verifyRejected(['account' => 'verify-1', 'password' => $password]);
verifyCheck(IdentitySessionService::verify(['account' => 'verify-1', 'password' => 'New-Fixture-Only'])['id'] === 1, 'current locked password not used');
Db::name('admin')->where('id', 1)->update(['password' => $hash]);
verifyCheck(Db::name('admin_session')->order('id')->select()->toArray() === $beforeSessions, 'source sessions changed');
verifyCheck(Db::name('admin')->order('id')->select()->toArray() === $beforeAccounts, 'source login metadata changed');
echo 'PASS source password verification: ' . $checks . " checks; temporary SQLite; no source sessions.\n";
} finally {
$resolved = realpath($runtime);
$temp = realpath(sys_get_temp_dir());
if ($resolved && $temp && str_starts_with($resolved, $temp . DIRECTORY_SEPARATOR)
&& str_starts_with(basename($resolved), 'iam-password-verify-')) {
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($resolved, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST);
foreach ($iterator as $entry) {
$entry->isDir() && !$entry->isLink() ? @rmdir($entry->getPathname()) : @unlink($entry->getPathname());
}
@rmdir($resolved);
}
}