'1', 'IAM_GROUP_OWNER_IDS' => '1', 'IAM_GROUP_NAME' => '测试集团', 'IAM_HOSPITAL_BASE_URL' => 'http://127.0.0.1:18090/admin', 'IAM_GROUP_DB_PATH' => $runtime . '/group.sqlite', 'IAM_RUNTIME_PATH' => $runtime, 'IAM_SESSION_TTL' => '28800'] as $key => $value) { putenv($key . '=' . $value); } $now = time(); $source = new class extends SourceApiClient { public bool $disabled = false; public bool $firstPassword = false; public int $issues = 0; public int $reads = 0; public function __construct() {} public function sourceKey(): string { return hash('sha256', 'group-fixture-only'); } public function request(string $method, string $path, array $data = [], string $token = ''): array { if ($path === 'identity.session/login') { return ['token' => $data['account'] === 'owner' ? 'fixture-owner' : 'fixture-member']; } $id = $token === 'fixture-owner' ? 1 : 2; $user = static fn (int $id): array => ['id' => $id, 'name' => '成员' . $id, 'account' => 'fixture' . $id, 'root' => 1, 'disable' => 0, 'role_id' => [], 'dept_id' => []]; if ($path === 'auth.admin/mySelf') { return ['user' => $user($id) + ['is_paw' => $this->firstPassword ? 0 : 1], 'permissions' => ['*'], 'menu' => []]; } if ($path === 'auth.admin/lists') { $this->reads++; return ['lists' => [$user(1), $user(2)], 'count' => 2]; } if ($path === 'auth.admin/detail') { if (!in_array($data['id'], [1, 2], true)) { throw new ApiError(404, '员工不存在'); } return $user($data['id']); } if (in_array($path, ['auth.role/all', 'auth.menu/all', 'dept.dept/all'], true)) { return []; } if ($path === 'auth.menu/lists') { return ['lists' => []]; } if ($path === 'identity.hospitalSso/issue') { $this->issues++; return ['ticket' => str_repeat('c', 64)]; } if ($path === 'identity.session/logout') { return []; } throw new RuntimeException('Unexpected fixture operation'); } }; $sessions = new SessionStore($runtime . '/sessions', static function () use (&$now): int { return $now; }); $bridge = new SharedIdentityService($source, $sessions); $checks = 0; function check(bool $condition, string $message): void { global $checks; if (!$condition) { throw new RuntimeException($message); } $checks++; } function rejects(callable $action, int $status): void { try { $action(); } catch (ApiError $error) { check($error->status === $status, 'Unexpected rejection status'); return; } throw new RuntimeException('Expected rejection'); } try { $ownerToken = $bridge->dispatch('login', '', ['account' => 'owner', 'password' => 'fixture-only'])['token']; $memberToken = $bridge->dispatch('login', '', ['account' => 'member', 'password' => 'fixture-only'])['token']; $ownerAuth = 'Bearer ' . $ownerToken; $memberAuth = 'Bearer ' . $memberToken; $owner = $bridge->dispatch('myself', $ownerAuth); $member = $bridge->dispatch('myself', $memberAuth); check((bool) preg_match('/^gid_[a-f0-9]{32}$/D', $owner['gid']), 'Stable GID format'); check($bridge->dispatch('myself', $ownerAuth)['gid'] === $owner['gid'], 'GID changes on refresh'); check($member['gid'] !== $owner['gid'], 'Different users share GID'); check($owner['is_group_admin'] && !$member['is_group_admin'], 'Hospital root automatically became group owner'); check($owner['company_permissions'] === ['company.zyt-hospital.application.internet-hospital.*'], 'Company namespace missing'); $context = $bridge->dispatch('group.context', $ownerAuth); check(count($context['companies']) === 1 && $context['applications'][0]['ready'], 'Initial company/application not ready'); rejects(fn () => $bridge->dispatch('group.company.save', $memberAuth, ['code' => 'other-co', 'name' => '其他公司', 'parent_id' => 0, 'disable' => 0]), 403); $new = $bridge->dispatch('group.company.save', $ownerAuth, ['code' => 'other-co', 'name' => '其他公司', 'parent_id' => 0, 'disable' => 0]); check($new['id'] === 2 && !$new['is_directory_company'], 'New company wrongly exposes hospital directory'); rejects(fn () => $bridge->dispatch('group.company.save', $ownerAuth, ['code' => 'other-co', 'name' => '重复公司', 'parent_id' => 0, 'disable' => 0]), 409); rejects(fn () => $bridge->dispatch('group.company.save', $ownerAuth, ['id' => 1, 'name' => '医院', 'parent_id' => 1, 'disable' => 0]), 422); rejects(fn () => $bridge->dispatch('group.company.save', $ownerAuth, ['id' => 2, 'code' => 'changed', 'name' => '其他公司', 'parent_id' => 0, 'disable' => 0]), 422); $bridge->dispatch('group.company.save', $ownerAuth, ['id' => 2, 'name' => '其他公司改名', 'parent_id' => 1, 'disable' => 0]); rejects(fn () => $bridge->dispatch('group.company.save', $ownerAuth, ['id' => 1, 'name' => '医院', 'parent_id' => 2, 'disable' => 0]), 422); rejects(fn () => $bridge->dispatch('group.members', $memberAuth, [], ['company_id' => 1]), 403); $members = $bridge->dispatch('group.members', $ownerAuth, [], ['company_id' => 1]); check(count($members['lists']) === 2 && $members['lists'][1]['gid'] === $member['gid'], 'Directory identity mismatch'); $bridge->dispatch('group.member.save', $ownerAuth, ['company_id' => 2, 'admin_id' => 2, 'member_status' => 1, 'can_login' => 1]); $reads = $source->reads; check($bridge->dispatch('admin.lists', $memberAuth, [], [], 2)['count'] === 2, 'Company picker restricted global role-authorized users'); check(isset($bridge->dispatch('options', $ownerAuth, [], [], 2)['roles']), 'Company picker restricted global role options'); check($source->reads === $reads + 1, 'Global role-authorized user request did not reach its source'); $list = $bridge->dispatch('admin.lists', $memberAuth, [], [], 1); check($list['count'] === 2 && isset($list['lists'][0]['gid']), 'Hospital directory stopped working'); $verifier = str_repeat('v', 64); $challenge = rtrim(strtr(base64_encode(hash('sha256', $verifier, true)), '+/', '-_'), '='); $authorization = ['client_id' => 'internet-hospital', 'redirect_uri' => GroupService::hospitalEntry(), 'state' => str_repeat('s', 64), 'code_challenge' => $challenge, 'code_challenge_method' => 'S256']; $issue = function (string $auth) use ($bridge, $authorization): array { $url = $bridge->dispatch('group.sso.authorize', $auth, $authorization)['redirect_url']; parse_str(parse_url($url, PHP_URL_QUERY), $query); check(!str_contains($url, 'token') && $query['state'] === $authorization['state'], 'Redirect leaks token or loses state'); return $query; }; $exchange = static fn (string $code, string $proof): array => ['client_id' => 'internet-hospital', 'redirect_uri' => GroupService::hospitalEntry(), 'code' => $code, 'code_verifier' => $proof]; rejects(fn () => $bridge->dispatch('group.sso.authorize', $memberAuth, array_replace($authorization, ['redirect_uri' => 'https://attacker.invalid/admin/group-login'])), 422); rejects(fn () => $bridge->dispatch('group.sso.authorize', $memberAuth, array_replace($authorization, ['code_challenge_method' => 'plain'])), 422); rejects(fn () => $bridge->dispatch('group.sso.authorize', $memberAuth, $authorization + ['admin_id' => 1]), 422); $query = $issue($memberAuth); rejects(fn () => $bridge->dispatch('group.sso.exchange', '', $exchange($query['code'], str_repeat('wrong', 13))), 422); $result = $bridge->dispatch('group.sso.exchange', '', $exchange($query['code'], $verifier)); check(isset($result['ticket']) && !isset($result['token']) && $result['gid'] === $member['gid'], 'Exchange returns wrong identity/credential'); rejects(fn () => $bridge->dispatch('group.sso.exchange', '', $exchange($query['code'], $verifier)), 422); $query = $issue($memberAuth); $bridge->dispatch('group.member.save', $ownerAuth, ['company_id' => 1, 'admin_id' => 2, 'member_status' => 0, 'can_login' => 0]); check(isset($bridge->dispatch('group.sso.exchange', '', $exchange($query['code'], $verifier))['ticket']), 'Membership removed role-authorized hospital login'); check($bridge->dispatch('admin.lists', $memberAuth)['count'] === 2, 'Membership removed role-authorized global access'); check(isset($bridge->dispatch('group.sso.authorize', $memberAuth, $authorization)['redirect_url']), 'Membership incorrectly gates hospital role'); $memberContext = $bridge->dispatch('group.context', $memberAuth); check($memberContext['applications'][0]['can_login'], 'Hospital role authorization must remain independent of membership'); $bridge->dispatch('group.member.save', $ownerAuth, ['company_id' => 1, 'admin_id' => 2, 'member_status' => 1, 'can_login' => 1]); $query = $issue($memberAuth); $source->firstPassword = true; rejects(fn () => $bridge->dispatch('group.sso.exchange', '', $exchange($query['code'], $verifier)), 403); $source->firstPassword = false; $query = $issue($memberAuth); $sessions->revoke($memberToken); rejects(fn () => $bridge->dispatch('group.sso.exchange', '', $exchange($query['code'], $verifier)), 401); $groupStore = new GroupStore($source->sourceKey()); $group = new GroupService($source, $sessions, $groupStore, static function () use (&$now): int { return $now; }); $ownerSession = $sessions->resolve($ownerToken, $source->sourceKey()); $url = $group->dispatch('group.sso.authorize', $owner, $ownerSession, $ownerToken, $authorization, [])['redirect_url']; parse_str(parse_url($url, PHP_URL_QUERY), $expired); $now += 61; rejects(fn () => $group->exchange($exchange($expired['code'], $verifier), static fn () => $owner), 422); $now = time(); $bridge->dispatch('group.company.save', $ownerAuth, ['id' => 1, 'name' => '医院', 'parent_id' => 0, 'disable' => 1]); rejects(fn () => $bridge->dispatch('group.sso.authorize', $ownerAuth, $authorization), 403); check($bridge->dispatch('admin.lists', $ownerAuth)['count'] === 2, 'Disabled company restricted global account administration'); check($bridge->dispatch('group.context', $ownerAuth)['companies'][0]['disable'] === 1, 'Disabled company cannot be managed back'); putenv('IAM_HOSPITAL_BASE_URL=http://remote.invalid/admin'); check(GroupService::hospitalEntry() === '', 'Remote plaintext HTTP accepted'); $rows = $groupStore->all('SELECT * FROM group_audit'); check(count($rows) > 0 && !str_contains(json_encode($rows), 'fixture-owner') && !str_contains(json_encode($rows), $ownerToken), 'Audit leaks credentials'); fwrite(STDOUT, "Group identity regression passed ({$checks} checks, isolated data only).\n"); } catch (Throwable $error) { fwrite(STDERR, get_class($error) . ': ' . $error->getMessage() . " at " . $error->getLine() . "\n"); exit(1); }