getResponse()->getData()['code'] === $code, 'Unexpected rejection code'); return; } throw new RuntimeException('Expected operation to reject'); } try { if (!extension_loaded('pdo_sqlite')) { throw new RuntimeException('pdo_sqlite is required'); } $runtime = dirname(__DIR__) . '/runtime/source-session-' . bin2hex(random_bytes(10)); mkdir($runtime, 0700, true); $app = new class(dirname(__DIR__) . DIRECTORY_SEPARATOR) extends think\App { public function loadEnv(string $envName = ''): void { /* 不读取真实环境文件。 */ } }; $app->setRuntimePath($runtime . DIRECTORY_SEPARATOR); $app->initialize(); $app->config->set([ 'default' => 'fixture', 'auto_timestamp' => true, 'connections' => ['fixture' => ['type' => 'sqlite', 'database' => $runtime . '/fixture.sqlite', 'prefix' => 'f_', 'fields_strict' => true, 'trigger_sql' => false, 'break_reconnect' => false]], ], 'database'); $app->config->set(['unique_identification' => 'fixture-salt', 'admin_token' => ['expire_duration' => 3600, 'be_expire_duration' => 600], 'default_image' => ['admin_avatar' => 'fixture/avatar.png'], 'admin_login' => ['login_restrictions' => 0, 'password_error_times' => 5, 'limit_login_time' => 30], ], '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 AUTOINCREMENT,admin_id INTEGER,terminal INTEGER, token TEXT UNIQUE,expire_time INTEGER,update_time INTEGER)'); Db::execute('CREATE TABLE f_system_role (id INTEGER PRIMARY KEY,name TEXT,`desc` TEXT,sort INTEGER, data_scope INTEGER,create_time INTEGER,update_time INTEGER,delete_time INTEGER)'); Db::execute('CREATE TABLE f_system_role_menu (role_id INTEGER,menu_id INTEGER)'); Db::execute('CREATE TABLE f_system_menu (id INTEGER PRIMARY KEY,pid INTEGER DEFAULT 0,perms TEXT,is_disable INTEGER DEFAULT 0)'); Db::execute('CREATE TABLE f_admin_role (admin_id INTEGER,role_id INTEGER)'); Db::execute('CREATE TABLE f_admin_dept (admin_id INTEGER,dept_id INTEGER)'); Db::execute('CREATE TABLE f_admin_jobs (admin_id INTEGER,jobs_id INTEGER)'); Db::execute('CREATE TABLE f_config (id INTEGER PRIMARY KEY,type TEXT,name TEXT,value TEXT,create_time INTEGER,update_time INTEGER)'); $password = 'Original-' . bin2hex(random_bytes(6)); $nextPassword = 'Changed-' . bin2hex(random_bytes(6)); $hash = create_password($password, 'fixture-salt'); foreach ([1, 2, 3, 4, 5] as $id) { Db::name('admin')->insert(['id' => $id, 'name' => 'fixture-' . $id, 'account' => 'fixture-' . $id, 'password' => $hash, 'is_paw' => in_array($id, [3, 4], true) ? 0 : 1, 'create_time' => time(), 'update_time' => time()]); } $cache = new AdminTokenCache(); $newSession = static function (int $id, int $ttl = 3600) use ($cache): string { $token = bin2hex(random_bytes(16)); Db::name('admin_session')->insert(['admin_id' => $id, 'terminal' => 1, 'token' => $token, 'expire_time' => time() + $ttl, 'update_time' => time()]); $cache->set('token_admin_' . $token, ['admin_id' => $id], 3600); return $token; }; $tokenA = $newSession(1); $tokenB = $newSession(1); $other = $newSession(2); sharedRejected(static fn () => IdentitySessionService::changePassword($tokenA, ['old_password' => 'incorrect-password', 'password' => $nextPassword, 'password_confirm' => $nextPassword]), 0); sharedCheck(Db::name('admin')->where('id', 1)->value('password') === $hash, 'Wrong password changed identity'); IdentitySessionService::changePassword($tokenA, ['old_password' => $password, 'password' => $nextPassword, 'password_confirm' => $nextPassword]); $newHash = create_password($nextPassword, 'fixture-salt'); sharedCheck(Db::name('admin')->where('id', 1)->value('password') === $newHash, 'Original password format was not preserved'); sharedCheck(Db::name('admin_session')->where('admin_id', 1)->where('expire_time', '>', time())->count() === 0, 'Not all own sessions were revoked'); sharedCheck(Db::name('admin_session')->where('token', $other)->value('expire_time') > time(), 'Another user session was revoked'); sharedCheck(!$cache->get('token_admin_' . $tokenA) && !$cache->get('token_admin_' . $tokenB), 'Original token cache not cleared'); sharedRejected(static fn () => IdentitySessionService::changePassword($tokenB, ['old_password' => $password, 'password' => $password, 'password_confirm' => $password]), -1); sharedCheck(Db::name('admin')->where('id', 1)->value('password') === $newHash, 'Stale session overwrote password'); $first = $newSession(3); IdentitySessionService::changePassword($first, ['password' => $nextPassword, 'password_confirm' => $nextPassword]); sharedCheck((int) Db::name('admin')->where('id', 3)->value('is_paw') === 1, 'First-password state was not updated'); sharedCheck(Db::name('admin')->where('id', 3)->value('password') === $newHash, 'First password did not use original hash'); $expired = $newSession(2, -10); sharedRejected(static fn () => IdentitySessionService::logout($expired), -1); $ownSecond = $newSession(2); IdentitySessionService::logout($other); sharedCheck(Db::name('admin_session')->where('token', $other)->value('expire_time') < time(), 'Current session remains active'); sharedCheck(Db::name('admin_session')->where('token', $ownSecond)->value('expire_time') > time(), 'Single-session logout revoked another session'); sharedCheck(!$cache->get('token_admin_' . $other), 'Logout left original cache'); IdentitySessionService::logout($ownSecond, true); sharedCheck(Db::name('admin_session')->where('admin_id', 2)->where('expire_time', '>', time())->count() === 0, 'All-session logout incomplete'); $app->env->set(['WORK_WECHAT_FORCE_BIND_LOGIN' => true, 'WORK_WECHAT_CORP_ID' => 'fixture-corp', 'WORK_WECHAT_SECRET' => 'fixture-secret', 'WORK_WECHAT_AGENT_ID' => 'fixture-agent']); $restricted = $newSession(2); sharedRejected(static fn () => IdentitySessionService::changePassword($restricted, ['old_password' => $password, 'password' => $nextPassword, 'password_confirm' => $nextPassword]), 10); $firstRestricted = $newSession(4); IdentitySessionService::changePassword($firstRestricted, ['password' => $nextPassword, 'password_confirm' => $nextPassword]); sharedCheck((int) Db::name('admin')->where('id', 4)->value('is_paw') === 1, 'First password must precede binding gate'); Db::name('system_role')->insert(['id' => 1, 'name' => 'fixture-role', 'desc' => '', 'sort' => 0, 'data_scope' => 3]); Db::name('system_menu')->insert(['id' => 1, 'perms' => 'auth.admin/lists']); Db::name('system_role_menu')->insert(['role_id' => 1, 'menu_id' => 1]); sharedCheck(RoleLogic::edit(['id' => 1, 'name' => 'fixture-role', 'menu_id' => []]) === true, 'Clearing original role failed'); sharedCheck(Db::name('system_role_menu')->where('role_id', 1)->count() === 0, 'Explicit empty grants did not clear'); sharedCheck((int) Db::name('system_role')->where('id', 1)->value('data_scope') === 3, 'Omitted scope was reset'); Db::name('system_role_menu')->insert(['role_id' => 1, 'menu_id' => 1]); sharedCheck(RoleLogic::edit(['id' => 1, 'name' => 'fixture-role']) === true, 'Editing original role failed'); sharedCheck(Db::name('system_role_menu')->where('role_id', 1)->count() === 1, 'Omitted grants did not preserve'); // 模拟旧密码已在事务外通过验证,然后改密提交;新登录服务不能沿用旧快照签发。 $app->env->set(['APP_DEBUG' => false, 'LOCAL_DEBUG_LOGIN_SKIP_PASSWORD' => false]); $validator = new LoginValidate(); sharedCheck($validator->password($password, '', ['account' => 'fixture-5']) === true, 'Original credential precheck failed'); $oldLogin = ['account' => 'fixture-5', 'password' => $password, 'terminal' => 1]; Db::name('admin')->where('id', 5)->update(['password' => $newHash]); $sessionCount = Db::name('admin_session')->where('admin_id', 5)->count(); sharedRejected(static fn () => IdentitySessionService::login($oldLogin), -1); sharedCheck(Db::name('admin_session')->where('admin_id', 5)->count() === $sessionCount, 'Stale precheck minted a session'); sharedCheck(Db::name('admin')->where('id', 5)->value('login_time') === null, 'Rejected login updated login metadata'); // 即使原验证器在调试模式跳过密码,新接口也必须真实复核当前hash。 $app->env->set(['APP_DEBUG' => true, 'LOCAL_DEBUG_LOGIN_SKIP_PASSWORD' => true]); sharedCheck($validator->password('incorrect-password', '', ['account' => 'fixture-5']) === true, 'Fixture did not enable original debug bypass'); sharedRejected(static fn () => IdentitySessionService::login(['account' => 'fixture-5', 'password' => 'incorrect-password', 'terminal' => 1]), -1); sharedCheck(Db::name('admin_session')->where('admin_id', 5)->count() === $sessionCount, 'Debug bypass minted a session'); $app->env->set(['APP_DEBUG' => false, 'LOCAL_DEBUG_LOGIN_SKIP_PASSWORD' => false]); Db::name('admin_role')->insert(['admin_id' => 5, 'role_id' => 1]); $login = IdentitySessionService::login(['account' => 'fixture-5', 'password' => $nextPassword, 'terminal' => 1]); sharedCheck(is_string($login['token'] ?? null) && $login['token'] !== '', 'Current password did not issue an original token'); sharedCheck(Db::name('admin_session')->where('admin_id', 5)->where('token', $login['token'])->where('expire_time', '>', time())->count() === 1, 'Issued original session not persisted'); sharedCheck(($login['role_name'] ?? '') === 'fixture-role' && isset($login['avatar'], $login['name'], $login['is_paw']), 'Original login response contract changed'); sharedCheck(($login['need_bind_work_wechat'] ?? null) === true, 'Original login binding gate was lost'); Db::name('admin')->where('id', 5)->update(['disable' => 1]); sharedRejected(static fn () => IdentitySessionService::login(['account' => 'fixture-5', 'password' => $nextPassword, 'terminal' => 1]), -1); sharedCheck(Db::name('admin_session')->where('admin_id', 5)->count() === $sessionCount + 1, 'Disabled account minted another session'); Db::name('admin')->where('id', 5)->update(['disable' => 0, 'delete_time' => time()]); sharedRejected(static fn () => IdentitySessionService::login(['account' => 'fixture-5', 'password' => $nextPassword, 'terminal' => 1]), -1); sharedCheck(Db::name('admin_session')->where('admin_id', 5)->count() === $sessionCount + 1, 'Deleted account minted another session'); fwrite(STDOUT, "PASS original shared session/role regression: {$checks} checks; isolated SQLite only.\n"); } catch (Throwable $error) { fwrite(STDERR, 'FAIL shared regression: ' . $error->getMessage() . "\n"); exit(1); }