Files
Deploymentcenter/public/api/license/v1/index.php
T

80 lines
3.0 KiB
PHP

<?php
declare(strict_types=1);
header('Content-Type: application/json; charset=utf-8');
require_once __DIR__ . '/../../../../src/Core/Db.php';
require_once __DIR__ . '/../../../../src/Core/Auth.php';
require_once __DIR__ . '/../../../../src/Modules/License/Audit.php';
require_once __DIR__ . '/../../../../src/Modules/License/KeyGen.php';
require_once __DIR__ . '/../../../../src/Modules/License/RateLimiter.php';
require_once __DIR__ . '/../../../../src/Modules/License/LicenseService.php';
use Deploymentcenter\Core\Db;
use Deploymentcenter\Core\Auth;
use Deploymentcenter\Modules\License\LicenseService;
use Deploymentcenter\Modules\License\RateLimiter;
function sendResponse(array $data, int $statusCode = 200): void {
http_response_code($statusCode);
echo json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
exit;
}
try {
$config = require __DIR__ . '/../../../../config/config.php';
$pdo = Db::init($config);
$limiter = new RateLimiter($pdo, 120, 60);
$ip = $_SERVER['REMOTE_ADDR'] ?? '127.0.0.1';
if (!$limiter->check($ip)) {
sendResponse(['error' => 'Too Many Requests', 'message' => 'Rate limit exceeded.'], 429);
}
$uri = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH);
$method = strtoupper($_SERVER['REQUEST_METHOD'] ?? 'GET');
$rawInput = file_get_contents('php://input');
$inputData = !empty($rawInput) ? (json_decode($rawInput, true) ?? []) : $_POST;
$licenseService = new LicenseService($pdo);
// Validate Endpoint (Public API for clients)
if (str_ends_with($uri, '/validate') && $method === 'POST') {
$res = $licenseService->validate($inputData, $ip);
sendResponse($res);
}
// Deactivate Endpoint (AUTHENTICATED ONLY - Security Protection)
if (str_ends_with($uri, '/deactivate') && $method === 'POST') {
$authHeader = $_SERVER['HTTP_X_WATCHDOG_KEY'] ?? $_SERVER['HTTP_AUTHORIZATION'] ?? $_SERVER['HTTP_X_LICENSE_KEY'] ?? null;
if ($authHeader && str_starts_with($authHeader, 'Bearer ')) {
$authHeader = substr($authHeader, 7);
}
$sharedKey = $config['security']['shared_key'] ?? '';
$isAuthenticated = ($authHeader && hash_equals($sharedKey, $authHeader)) || Auth::isLoggedIn();
if (!$isAuthenticated) {
sendResponse([
'error' => 'Unauthorized',
'message' => 'Authentication required for license deactivation. Pass Bearer token or master key.'
], 401);
}
$res = $licenseService->deactivate($inputData, $ip);
sendResponse($res);
}
// Status Endpoint
if (str_ends_with($uri, '/status') && $method === 'GET') {
sendResponse(['status' => 'ok', 'module' => 'Lizenzen', 'version' => '1.0']);
}
sendResponse(['error' => 'Not Found', 'message' => 'Endpoint not found'], 404);
} catch (Throwable $t) {
sendResponse(['error' => 'Server Error', 'message' => $t->getMessage()], 500);
}