update api key manage

This commit is contained in:
2026-04-02 10:40:47 +08:00
parent 9a8431de81
commit 3a2b175028
9 changed files with 1034 additions and 0 deletions
@@ -0,0 +1,223 @@
<?php
declare(strict_types=1);
namespace HyperfTest\Cases\Integration\Admin;
use App\Model\ApiKey;
use App\Model\Role;
use App\Model\User;
use HyperfTest\TestCase;
use Qbhy\HyperfAuth\AuthManager;
use function Hyperf\Support\make;
/**
* AdminApiKeyController 集成测试
*
* @internal
* @coversNothing
*/
class AdminApiKeyTest extends TestCase
{
protected function getAdminAuthToken(): string
{
$admin_role = $this->fetchAdminRole();
$user = User::query()
->where('status', 1)
->where('role_id', $admin_role->id)
->first();
if (!$user) {
$this->markTestSkipped('没有可用的 administrator 用户,无法测试');
}
$auth = make(AuthManager::class);
return $auth->guard('jwt')->login($user);
}
protected function fetchAdminRole(): Role
{
return Role::query()->where('name', 'administrator')->firstOrFail();
}
protected function adminHeaders(): array
{
return ['Authorization' => 'Bearer ' . $this->getAdminAuthToken()];
}
protected function createTestUser(string $suffix, array $overrides = []): User
{
return User::query()->create(array_merge([
'username' => 'admin_apikey_test_' . $suffix,
'password' => 'Pass_' . $suffix,
'email' => 'admin_apikey_test_' . $suffix . '@example.com',
'status' => 1,
'api_key_enabled' => true,
], $overrides));
}
protected function getNonAdminToken(): array
{
$user = $this->createTestUser('nonadmin_' . uniqid());
$auth = make(AuthManager::class);
$token = $auth->guard('jwt')->login($user);
return ['Authorization' => 'Bearer ' . $token];
}
public function test_admin_can_list_all_api_keys(): void
{
$user = $this->createTestUser('list_' . uniqid());
$result = ApiKey::generate($user->id, 'Test Key List');
$response = $this->get('/api/v1/admin/api-keys', [], $this->adminHeaders());
$response->assertStatus(200);
$response->assertJsonPath('code', 0);
$body = json_decode($response->getBody()->getContents(), true);
$this->assertArrayHasKey('items', $body['data']);
$this->assertArrayHasKey('total', $body['data']);
$this->assertArrayHasKey('page', $body['data']);
$this->assertArrayHasKey('per_page', $body['data']);
// 验证 user 关联信息
$items = $body['data']['items'];
$found = false;
foreach ($items as $item) {
if ($item['id'] === $result['api_key']->id) {
$found = true;
$this->assertArrayHasKey('user', $item);
$this->assertEquals($user->id, $item['user']['id']);
$this->assertEquals($user->username, $item['user']['username']);
$this->assertArrayHasKey('api_key_enabled', $item['user']);
}
}
$this->assertTrue($found, '应在列表中找到刚创建的 Key');
$user->forceDelete();
}
public function test_admin_list_does_not_expose_key_hash(): void
{
$user = $this->createTestUser('hash_' . uniqid());
ApiKey::generate($user->id, 'Hash Check Key');
$response = $this->get('/api/v1/admin/api-keys', ['user_id' => $user->id], $this->adminHeaders());
$response->assertStatus(200);
$body = json_decode($response->getBody()->getContents(), true);
foreach ($body['data']['items'] as $item) {
$this->assertArrayNotHasKey('key_hash', $item, '响应不应包含 key_hash');
}
$user->forceDelete();
}
public function test_admin_list_filter_by_user_id(): void
{
$user = $this->createTestUser('filter_uid_' . uniqid());
ApiKey::generate($user->id, 'Filter User Key');
$response = $this->get('/api/v1/admin/api-keys', ['user_id' => $user->id], $this->adminHeaders());
$response->assertStatus(200);
$body = json_decode($response->getBody()->getContents(), true);
foreach ($body['data']['items'] as $item) {
$this->assertEquals($user->id, $item['user_id']);
}
$user->forceDelete();
}
public function test_admin_list_filter_by_enabled(): void
{
$user = $this->createTestUser('filter_en_' . uniqid());
$result = ApiKey::generate($user->id, 'Enabled Filter Key');
$result['api_key']->enabled = false;
$result['api_key']->save();
$response = $this->get('/api/v1/admin/api-keys', ['user_id' => $user->id, 'enabled' => 0], $this->adminHeaders());
$response->assertStatus(200);
$body = json_decode($response->getBody()->getContents(), true);
$this->assertCount(1, $body['data']['items']);
$this->assertFalse($body['data']['items'][0]['enabled']);
$user->forceDelete();
}
public function test_admin_list_pagination(): void
{
$response = $this->get('/api/v1/admin/api-keys', ['page' => 1, 'per_page' => 2], $this->adminHeaders());
$response->assertStatus(200);
$body = json_decode($response->getBody()->getContents(), true);
$this->assertEquals(1, $body['data']['page']);
$this->assertEquals(2, $body['data']['per_page']);
$this->assertLessThanOrEqual(2, count($body['data']['items']));
}
public function test_admin_can_toggle_any_key(): void
{
$user = $this->createTestUser('toggle_' . uniqid());
$result = ApiKey::generate($user->id, 'Toggle Key');
$key_id = $result['api_key']->id;
// 禁用
$response = $this->patch('/api/v1/admin/api-keys/' . $key_id . '/toggle', ['enabled' => false], $this->adminHeaders());
$response->assertStatus(200);
$response->assertJsonPath('code', 0);
$body = json_decode($response->getBody()->getContents(), true);
$this->assertFalse($body['data']['enabled']);
// 重新启用
$response = $this->patch('/api/v1/admin/api-keys/' . $key_id . '/toggle', ['enabled' => true], $this->adminHeaders());
$response->assertStatus(200);
$body = json_decode($response->getBody()->getContents(), true);
$this->assertTrue($body['data']['enabled']);
$user->forceDelete();
}
public function test_admin_toggle_does_not_expose_key_hash(): void
{
$user = $this->createTestUser('toggle_hash_' . uniqid());
$result = ApiKey::generate($user->id, 'Toggle Hash Key');
$response = $this->patch('/api/v1/admin/api-keys/' . $result['api_key']->id . '/toggle', ['enabled' => false], $this->adminHeaders());
$response->assertStatus(200);
$body = json_decode($response->getBody()->getContents(), true);
$this->assertArrayNotHasKey('key_hash', $body['data'], '响应不应包含 key_hash');
$user->forceDelete();
}
public function test_admin_can_delete_any_key(): void
{
$user = $this->createTestUser('delete_' . uniqid());
$result = ApiKey::generate($user->id, 'Delete Key');
$key_id = $result['api_key']->id;
$response = $this->delete('/api/v1/admin/api-keys/' . $key_id, [], $this->adminHeaders());
$response->assertStatus(200);
$response->assertJsonPath('code', 0);
$this->assertNull(ApiKey::query()->find($key_id));
$user->forceDelete();
}
public function test_non_admin_cannot_access_admin_api_keys(): void
{
$headers = $this->getNonAdminToken();
$response = $this->get('/api/v1/admin/api-keys', [], $headers);
$response->assertStatus(403);
}
public function test_toggle_nonexistent_key_returns_404(): void
{
$response = $this->patch('/api/v1/admin/api-keys/999999/toggle', ['enabled' => false], $this->adminHeaders());
$response->assertStatus(404);
}
}
@@ -0,0 +1,109 @@
<?php
declare(strict_types=1);
namespace HyperfTest\Cases\Integration\Auth;
use App\Model\ApiKey;
use App\Model\User;
use HyperfTest\TestCase;
/**
* api_key_enabled 全局开关集成测试
*
* @internal
* @coversNothing
*/
class ApiKeyGlobalSwitchTest extends TestCase
{
protected function createTestUser(string $suffix, array $overrides = []): User
{
return User::query()->create(array_merge([
'username' => 'gs_test_' . $suffix,
'password' => 'Pass_' . $suffix,
'email' => 'gs_test_' . $suffix . '@example.com',
'status' => 1,
'api_key_enabled' => true,
], $overrides));
}
public function test_api_key_auth_rejected_when_global_switch_off(): void
{
$user = $this->createTestUser('off_' . uniqid(), ['api_key_enabled' => false]);
$result = ApiKey::generate($user->id, 'Global Off Key');
// 手动启用 keygenerate 默认 enabled=true),但全局开关关闭
$response = $this->get('/api/v1/me', [], [
'X-API-Key' => $result['plain_key'],
]);
$response->assertStatus(403);
$body = json_decode($response->getBody()->getContents(), true);
$this->assertStringContainsString('API Key 功能未启用', $body['message']);
$user->forceDelete();
}
public function test_api_key_auth_works_when_global_switch_on(): void
{
$user = $this->createTestUser('on_' . uniqid(), ['api_key_enabled' => true]);
$result = ApiKey::generate($user->id, 'Global On Key');
$response = $this->get('/api/v1/me', [], [
'X-API-Key' => $result['plain_key'],
]);
$response->assertStatus(200);
$response->assertJsonPath('code', 0);
$response->assertJsonPath('data.id', $user->id);
$user->forceDelete();
}
public function test_api_key_auth_restored_after_reenable(): void
{
$user = $this->createTestUser('restore_' . uniqid(), ['api_key_enabled' => true]);
$result = ApiKey::generate($user->id, 'Restore Key');
// 关闭全局开关
$user->api_key_enabled = false;
$user->save();
$response = $this->get('/api/v1/me', [], [
'X-API-Key' => $result['plain_key'],
]);
$response->assertStatus(403);
// 重新开启
$user->api_key_enabled = true;
$user->save();
$response = $this->get('/api/v1/me', [], [
'X-API-Key' => $result['plain_key'],
]);
$response->assertStatus(200);
$response->assertJsonPath('data.id', $user->id);
$user->forceDelete();
}
public function test_disabled_key_still_rejected_after_global_reenable(): void
{
$user = $this->createTestUser('disabled_key_' . uniqid(), ['api_key_enabled' => true]);
$result = ApiKey::generate($user->id, 'Disabled Key');
// 禁用单个 Key
$result['api_key']->enabled = false;
$result['api_key']->save();
// 全局开关开启,但单 Key 已禁用
$response = $this->get('/api/v1/me', [], [
'X-API-Key' => $result['plain_key'],
]);
// ApiKey::findByPlainKey() 查询条件包含 enabled=true,所以禁用的 Key 返回 401(无效 Key
$response->assertStatus(401);
$user->forceDelete();
}
}
@@ -0,0 +1,164 @@
<?php
declare(strict_types=1);
namespace HyperfTest\Cases\Integration\Auth;
use App\Model\ApiKey;
use App\Model\User;
use HyperfTest\TestCase;
use Qbhy\HyperfAuth\AuthManager;
use function Hyperf\Support\make;
/**
* ApiKeyController toggle + store 增强校验集成测试
*
* @internal
* @coversNothing
*/
class ApiKeyToggleTest extends TestCase
{
protected function createTestUser(string $suffix, array $overrides = []): User
{
return User::query()->create(array_merge([
'username' => 'toggle_test_' . $suffix,
'password' => 'Pass_' . $suffix,
'email' => 'toggle_test_' . $suffix . '@example.com',
'status' => 1,
'api_key_enabled' => true,
], $overrides));
}
protected function getAuthToken(User $user): string
{
$auth = make(AuthManager::class);
return $auth->guard('jwt')->login($user);
}
protected function authHeaders(User $user): array
{
return ['Authorization' => 'Bearer ' . $this->getAuthToken($user)];
}
public function test_user_can_toggle_own_key(): void
{
$user = $this->createTestUser('own_' . uniqid());
$result = ApiKey::generate($user->id, 'Own Toggle Key');
$key_id = $result['api_key']->id;
// 禁用
$response = $this->patch('/api/v1/me/api-keys/' . $key_id . '/toggle', ['enabled' => false], $this->authHeaders($user));
$response->assertStatus(200);
$response->assertJsonPath('code', 0);
$body = json_decode($response->getBody()->getContents(), true);
$this->assertFalse($body['data']['enabled']);
// 重新启用
$response = $this->patch('/api/v1/me/api-keys/' . $key_id . '/toggle', ['enabled' => true], $this->authHeaders($user));
$response->assertStatus(200);
$body = json_decode($response->getBody()->getContents(), true);
$this->assertTrue($body['data']['enabled']);
$user->forceDelete();
}
public function test_user_cannot_toggle_others_key(): void
{
$user_a = $this->createTestUser('a_' . uniqid());
$user_b = $this->createTestUser('b_' . uniqid());
$result = ApiKey::generate($user_b->id, 'Other User Key');
$response = $this->patch('/api/v1/me/api-keys/' . $result['api_key']->id . '/toggle', ['enabled' => false], $this->authHeaders($user_a));
$response->assertStatus(404);
$user_a->forceDelete();
$user_b->forceDelete();
}
public function test_toggle_nonexistent_key_returns_404(): void
{
$user = $this->createTestUser('nokey_' . uniqid());
$response = $this->patch('/api/v1/me/api-keys/999999/toggle', ['enabled' => false], $this->authHeaders($user));
$response->assertStatus(404);
$user->forceDelete();
}
public function test_user_cannot_create_duplicate_name(): void
{
$user = $this->createTestUser('dup_' . uniqid());
// 创建第一个 Key
$response = $this->post('/api/v1/me/api-keys', ['name' => 'Duplicate Name'], $this->authHeaders($user));
$response->assertStatus(200);
// 创建同名 Key
$response = $this->post('/api/v1/me/api-keys', ['name' => 'Duplicate Name'], $this->authHeaders($user));
$response->assertStatus(400);
$body = json_decode($response->getBody()->getContents(), true);
$this->assertStringContainsString('已存在同名', $body['message']);
$user->forceDelete();
}
public function test_user_can_reuse_name_after_delete(): void
{
$user = $this->createTestUser('reuse_' . uniqid());
// 创建 Key
$response = $this->post('/api/v1/me/api-keys', ['name' => 'Reuse Name'], $this->authHeaders($user));
$response->assertStatus(200);
$body = json_decode($response->getBody()->getContents(), true);
$key_id = $body['data']['api_key']['id'];
// 删除
$response = $this->delete('/api/v1/me/api-keys/' . $key_id, [], $this->authHeaders($user));
$response->assertStatus(200);
// 复用名称创建新 Key
$response = $this->post('/api/v1/me/api-keys', ['name' => 'Reuse Name'], $this->authHeaders($user));
$response->assertStatus(200);
$user->forceDelete();
}
public function test_user_cannot_create_more_than_10_keys(): void
{
$user = $this->createTestUser('limit_' . uniqid());
// 通过模型直接创建 10 个 Key
for ($i = 1; $i <= 10; $i++) {
ApiKey::generate($user->id, 'Key ' . $i);
}
// 尝试创建第 11 个
$response = $this->post('/api/v1/me/api-keys', ['name' => 'Key 11'], $this->authHeaders($user));
$response->assertStatus(400);
$body = json_decode($response->getBody()->getContents(), true);
$this->assertStringContainsString('最多创建 10 个', $body['message']);
$user->forceDelete();
}
public function test_user_can_create_after_deleting_to_below_limit(): void
{
$user = $this->createTestUser('dellimit_' . uniqid());
// 创建 10 个 Key
$results = [];
for ($i = 1; $i <= 10; $i++) {
$results[] = ApiKey::generate($user->id, 'Key ' . $i);
}
// 删除一个
$this->delete('/api/v1/me/api-keys/' . $results[0]['api_key']->id, [], $this->authHeaders($user));
// 现在可以创建新的
$response = $this->post('/api/v1/me/api-keys', ['name' => 'New Key'], $this->authHeaders($user));
$response->assertStatus(200);
$user->forceDelete();
}
}
@@ -0,0 +1,112 @@
<?php
declare(strict_types=1);
namespace HyperfTest\Cases\Integration\User;
use App\Model\Role;
use App\Model\User;
use HyperfTest\TestCase;
use Qbhy\HyperfAuth\AuthManager;
use function Hyperf\Support\make;
/**
* UserController::updateApiKeyEnabled 集成测试
*
* @internal
* @coversNothing
*/
class UserApiKeyEnabledTest extends TestCase
{
protected function getAdminAuthToken(): string
{
$admin_role = $this->fetchAdminRole();
$user = User::query()
->where('status', 1)
->where('role_id', $admin_role->id)
->first();
if (!$user) {
$this->markTestSkipped('没有可用的 administrator 用户,无法测试');
}
$auth = make(AuthManager::class);
return $auth->guard('jwt')->login($user);
}
protected function fetchAdminRole(): Role
{
return Role::query()->where('name', 'administrator')->firstOrFail();
}
protected function adminHeaders(): array
{
return ['Authorization' => 'Bearer ' . $this->getAdminAuthToken()];
}
protected function createTestUser(string $suffix, array $overrides = []): User
{
return User::query()->create(array_merge([
'username' => 'ake_test_' . $suffix,
'password' => 'Pass_' . $suffix,
'email' => 'ake_test_' . $suffix . '@example.com',
'status' => 1,
'api_key_enabled' => true,
], $overrides));
}
protected function getNonAdminHeaders(): array
{
$user = $this->createTestUser('nonadmin_' . uniqid());
$auth = make(AuthManager::class);
$token = $auth->guard('jwt')->login($user);
return ['Authorization' => 'Bearer ' . $token];
}
public function test_admin_can_enable_user_api_key(): void
{
$user = $this->createTestUser('enable_' . uniqid(), ['api_key_enabled' => false]);
$response = $this->patch('/api/v1/users/' . $user->id . '/api-key-enabled', ['api_key_enabled' => true], $this->adminHeaders());
$response->assertStatus(200);
$response->assertJsonPath('code', 0);
$body = json_decode($response->getBody()->getContents(), true);
$this->assertTrue($body['data']['api_key_enabled']);
$this->assertEquals($user->username, $body['data']['username']);
$user->forceDelete();
}
public function test_admin_can_disable_user_api_key(): void
{
$user = $this->createTestUser('disable_' . uniqid(), ['api_key_enabled' => true]);
$response = $this->patch('/api/v1/users/' . $user->id . '/api-key-enabled', ['api_key_enabled' => false], $this->adminHeaders());
$response->assertStatus(200);
$body = json_decode($response->getBody()->getContents(), true);
$this->assertFalse($body['data']['api_key_enabled']);
$user->forceDelete();
}
public function test_non_admin_cannot_update_api_key_enabled(): void
{
$target = $this->createTestUser('target_' . uniqid());
$headers = $this->getNonAdminHeaders();
$response = $this->patch('/api/v1/users/' . $target->id . '/api-key-enabled', ['api_key_enabled' => false], $headers);
$response->assertStatus(403);
$target->forceDelete();
}
public function test_update_nonexistent_user_returns_404(): void
{
$response = $this->patch('/api/v1/users/999999/api-key-enabled', ['api_key_enabled' => false], $this->adminHeaders());
$response->assertStatus(404);
}
}