🚀 مُصادَق: تحديث برمجي جديد 2026-05-03 16:43
This commit is contained in:
@@ -1,54 +1,63 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\ApiKeys;
|
||||
|
||||
use App\Core\{Request, Response, Database};
|
||||
use Ramsey\Uuid\Uuid;
|
||||
|
||||
final class ApiKeyController
|
||||
{
|
||||
public function list(Request $request): void
|
||||
public function index(Request $request): void
|
||||
{
|
||||
$tenantId = $request->tenantId;
|
||||
$db = Database::getInstance();
|
||||
$stmt = $db->prepare("SELECT id, name, public_key, created_at, last_used_at, is_active FROM api_keys WHERE tenant_id = ? ORDER BY created_at DESC");
|
||||
$stmt->execute([$tenantId]);
|
||||
|
||||
Response::json([
|
||||
'success' => true,
|
||||
'data' => $stmt->fetchAll()
|
||||
]);
|
||||
$stmt = $db->prepare("SELECT id, public_key, name, is_active, created_at FROM api_keys WHERE tenant_id = ? AND is_active = 1");
|
||||
$stmt->execute([$tenantId]);
|
||||
$keys = $stmt->fetchAll();
|
||||
|
||||
Response::json(['success' => true, 'data' => $keys]);
|
||||
}
|
||||
|
||||
public function create(Request $request): void
|
||||
{
|
||||
$tenantId = $request->tenantId;
|
||||
$userId = $request->user->user_id;
|
||||
$name = $request->input('name');
|
||||
|
||||
if (!$name) {
|
||||
Response::error('يرجى إدخال اسم المفتاح', 'VALIDATION_ERROR', 422);
|
||||
return;
|
||||
}
|
||||
|
||||
$id = Uuid::uuid4()->toString();
|
||||
$publicKey = bin2hex(random_bytes(16));
|
||||
$secretKey = bin2hex(random_bytes(32));
|
||||
$secretHash = password_hash($secretKey, PASSWORD_BCRYPT);
|
||||
|
||||
$data = $request->getBody();
|
||||
$name = $data['name'] ?? 'Default Key';
|
||||
|
||||
$publicKey = bin2hex(random_bytes(16)); // 32 chars
|
||||
$secret = bin2hex(random_bytes(32)); // 64 chars
|
||||
|
||||
$db = Database::getInstance();
|
||||
$stmt = $db->prepare("INSERT INTO api_keys (id, tenant_id, user_id, name, public_key, secret_hash, is_active) VALUES (?, ?, ?, ?, ?, ?, 1)");
|
||||
$stmt->execute([$id, $tenantId, $userId, $name, $publicKey, $secretHash]);
|
||||
$stmt = $db->prepare("INSERT INTO api_keys (id, tenant_id, name, public_key, secret_hash, is_active, created_at) VALUES (?, ?, ?, ?, ?, 1, NOW())");
|
||||
|
||||
$id = \Ramsey\Uuid\Uuid::uuid4()->toString();
|
||||
$stmt->execute([
|
||||
$id,
|
||||
$tenantId,
|
||||
$name,
|
||||
$publicKey,
|
||||
password_hash($secret, PASSWORD_BCRYPT)
|
||||
]);
|
||||
|
||||
Response::json([
|
||||
'success' => true,
|
||||
'message' => 'تم إنشاء مفتاح API بنجاح. يرجى حفظ السر لأنه لن يظهر مرة أخرى.',
|
||||
'message' => 'تم إنشاء مفتاح API بنجاح. يرجى حفظ السر (Secret) الآن لأنه لن يظهر مرة أخرى.',
|
||||
'data' => [
|
||||
'id' => $id,
|
||||
'key' => "msq_{$publicKey}.{$secretKey}"
|
||||
'public_key' => $publicKey,
|
||||
'secret' => $secret
|
||||
]
|
||||
], 201);
|
||||
}
|
||||
|
||||
public function revoke(Request $request, string $id): void
|
||||
{
|
||||
$tenantId = $request->tenantId;
|
||||
$db = Database::getInstance();
|
||||
|
||||
$stmt = $db->prepare("UPDATE api_keys SET is_active = 0 WHERE id = ? AND tenant_id = ?");
|
||||
$stmt->execute([$id, $tenantId]);
|
||||
|
||||
Response::json(['success' => true, 'message' => 'تم إيقاف مفتاح API بنجاح']);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user