DEUX features liées :
1. CHANNELS : chaque license peut être attribuée à un manifest distinct
(« channel »). Permet de servir des versions différentes selon le
client. Le serveur lit ?channel=X et sert manifest/versions-{X}.json
avec fallback transparent sur versions.json. NULL = default.
2. BÊTA : nouveau flag isBeta + betaNotes par version dans le manifest.
Visible uniquement par les licenses avec can_see_betas=1. Affichage
d'une pill orange « BÊTA » sur la row + tooltip avec les notes pour
les testeurs. Les installations locales déjà présentes restent
visibles même si l'accès BÊTA est retiré ensuite (on n'efface pas
le disque du client).
DB :
002_channel_betas.sql ajoute channel + can_see_betas sur licenses.
Idempotent (ALTER TABLE IF NOT EXISTS), zero data migration.
Serveur PHP :
- ValidateLicense.php signe channel + canSeeBetas dans la réponse
(ordre des clés CRITIQUE pour matcher le canonical client).
- Manifest.php : whitelist regex anti-traversal sur ?channel=, fallback
silencieux sur versions.json si channel inconnu (évite leak de la
liste de channels par probing).
- SignManifest.php prend un channel optionnel → l'admin peut signer
chaque manifest indépendamment.
- admin/licenses.php : dropdown channel + checkbox bêta sur create,
bouton détails repliable par-row pour edit.
- admin/versions.php : channel switcher en tête, badge BÊTA sur chaque
row, dialog repliable « Bêta » avec checkbox + notes des testeurs.
Client C# :
- License.Channel + License.CanSeeBetas (dans le canonical signé).
- VersionManifest.IsBeta + BetaNotes.
- ManifestService prend un channelProvider via DI, lu depuis license
cachée à chaque fetch (lazy, pas de circular dep).
- MainViewModel.RebuildList filtre les versions IsBeta si !CanSeeBetas
(mais conserve les installées locales — on ne retire pas l'accès
rétroactivement à ce qui est déjà sur disque).
- VersionRowViewModel : props IsBeta / BetaNotes / BetaTooltip.
- MainWindow.xaml : pill orange à côté du n° version pour le featured
et les rows compactes, tooltip dynamique avec les notes testeurs.
Backward compat signature :
Anciennes licenses cachées (signées sans channel/canSeeBetas) sont
toujours validées via un fallback canonical legacy dans VerifySignature.
Sans ce fallback, le passage à v0.26 invaliderait toutes les caches
hors-ligne et bloquerait les users en mobilité.
Migration côté admin : jouer 002_channel_betas.sql sur la base, déployer
les fichiers PHP, créer manifest/versions-{channel}.json pour les
nouveaux channels (l'admin versions.php propose un input « Créer/utiliser
un nouveau channel »). Les licenses existantes restent en channel=NULL
= default = comportement actuel.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
155 lines
6.7 KiB
PHP
155 lines
6.7 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace PSLauncher\Routes;
|
|
|
|
use PSLauncher\Crypto;
|
|
use PSLauncher\Db;
|
|
use PSLauncher\Response;
|
|
|
|
final class ValidateLicense
|
|
{
|
|
public static function handle(array $config): void
|
|
{
|
|
// Rate limit basique par IP
|
|
$ip = $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0';
|
|
self::enforceRateLimit($config, $ip);
|
|
|
|
$body = json_decode(file_get_contents('php://input') ?: '[]', true) ?? [];
|
|
$licenseKey = trim((string)($body['licenseKey'] ?? ''));
|
|
$machineId = trim((string)($body['machineId'] ?? ''));
|
|
$machineLabel = trim((string)($body['machineLabel'] ?? ''));
|
|
$launcherVer = trim((string)($body['launcherVersion'] ?? ''));
|
|
|
|
if ($licenseKey === '' || $machineId === '') {
|
|
Response::error('invalid_request', 'licenseKey et machineId sont requis', 400);
|
|
}
|
|
|
|
$db = Db::get($config);
|
|
$now = (new \DateTimeImmutable('now'))->format('Y-m-d H:i:s');
|
|
|
|
$stmt = $db->prepare(
|
|
'SELECT id, license_key, owner_name, issued_at, download_entitlement_until,
|
|
max_machines, channel, can_see_betas, revoked_at
|
|
FROM licenses WHERE license_key = ? LIMIT 1'
|
|
);
|
|
$stmt->execute([$licenseKey]);
|
|
$lic = $stmt->fetch();
|
|
|
|
if (!$lic) {
|
|
self::audit($db, null, $ip, 'validate_invalid', ['key_prefix' => substr($licenseKey, 0, 6)]);
|
|
Response::error('invalid', 'Clé de license invalide', 401);
|
|
}
|
|
if ($lic['revoked_at'] !== null) {
|
|
self::audit($db, (int)$lic['id'], $ip, 'validate_revoked', []);
|
|
Response::error('revoked', 'License révoquée', 403);
|
|
}
|
|
|
|
// Slot machine
|
|
$slotStmt = $db->prepare(
|
|
'SELECT id FROM license_machines WHERE license_id = ? AND machine_id = ? LIMIT 1'
|
|
);
|
|
$slotStmt->execute([$lic['id'], $machineId]);
|
|
$existingSlot = $slotStmt->fetch();
|
|
|
|
if ($existingSlot) {
|
|
$db->prepare('UPDATE license_machines SET last_seen = ? WHERE id = ?')
|
|
->execute([$now, $existingSlot['id']]);
|
|
} else {
|
|
$countStmt = $db->prepare('SELECT COUNT(*) AS c FROM license_machines WHERE license_id = ?');
|
|
$countStmt->execute([$lic['id']]);
|
|
$count = (int)($countStmt->fetch()['c'] ?? 0);
|
|
if ($count >= (int)$lic['max_machines']) {
|
|
self::audit($db, (int)$lic['id'], $ip, 'machine_limit', ['count' => $count]);
|
|
Response::error('machine_limit_exceeded',
|
|
"Cette license autorise {$lic['max_machines']} machine(s) ; toutes les places sont prises.",
|
|
403, ['ownerName' => $lic['owner_name']]);
|
|
}
|
|
$db->prepare(
|
|
'INSERT INTO license_machines (license_id, machine_id, machine_label, first_seen, last_seen)
|
|
VALUES (?, ?, ?, ?, ?)'
|
|
)->execute([$lic['id'], $machineId, $machineLabel ?: null, $now, $now]);
|
|
}
|
|
|
|
// Construit la réponse, signe-la, renvoie.
|
|
// Tous les timestamps sont émis en UTC + format figé "yyyy-MM-ddTHH:mm:ssZ"
|
|
// pour que la canonicalisation côté client produise EXACTEMENT les mêmes bytes.
|
|
$utc = new \DateTimeZone('UTC');
|
|
$fmt = 'Y-m-d\TH:i:s\Z';
|
|
|
|
$issuedAt = (new \DateTimeImmutable($lic['issued_at'], $utc))->format($fmt);
|
|
$entUntil = (new \DateTimeImmutable($lic['download_entitlement_until'], $utc))->format($fmt);
|
|
$serverTime = (new \DateTimeImmutable('now', $utc))->format($fmt);
|
|
$expired = strtotime($lic['download_entitlement_until']) < time();
|
|
|
|
// ATTENTION : ordre des clés CRITIQUE pour la signature Ed25519.
|
|
// Crypto::canonicalJson NE TRIE PAS — il prend l'ordre tel quel. Le client
|
|
// C# (LicenseService.CanonicalBytesFor) reconstruit le même dictionnaire
|
|
// dans le même ordre. Toute modif ici doit être miroir côté client.
|
|
// channel : NULL en DB → null JSON (visible dans le canonical comme "channel":null).
|
|
// can_see_betas : 0/1 en DB → bool JSON.
|
|
$channel = isset($lic['channel']) && $lic['channel'] !== null && $lic['channel'] !== ''
|
|
? (string)$lic['channel']
|
|
: null;
|
|
$canSeeBetas = (bool)($lic['can_see_betas'] ?? 0);
|
|
|
|
$payload = [
|
|
'status' => $expired ? 'expired' : 'valid',
|
|
'licenseId' => 'lic_' . $lic['id'],
|
|
'ownerName' => $lic['owner_name'],
|
|
'issuedAt' => $issuedAt,
|
|
'downloadEntitlementUntil' => $entUntil,
|
|
'maxMachines' => (int)$lic['max_machines'],
|
|
'channel' => $channel,
|
|
'canSeeBetas' => $canSeeBetas,
|
|
'serverTime' => $serverTime,
|
|
];
|
|
|
|
// Signature Ed25519 du payload canonical
|
|
$sk = $config['ed25519']['private_key_hex'] ?? '';
|
|
if ($sk !== '') {
|
|
$payload['signature'] = Crypto::signEd25519(Crypto::canonicalJson($payload), $sk);
|
|
}
|
|
|
|
self::audit($db, (int)$lic['id'], $ip, $expired ? 'validate_expired' : 'validate_ok', [
|
|
'launcher_version' => $launcherVer,
|
|
]);
|
|
|
|
Response::json($payload, $expired ? 200 : 200);
|
|
}
|
|
|
|
private static function enforceRateLimit(array $config, string $ip): void
|
|
{
|
|
$max = (int)($config['validate_max_per_minute_per_ip'] ?? 10);
|
|
if ($max <= 0) return;
|
|
|
|
$db = Db::get($config);
|
|
$now = new \DateTimeImmutable('now');
|
|
$window = $now->modify('-1 minute')->format('Y-m-d H:i:s');
|
|
|
|
$stmt = $db->prepare('SELECT counter, window_start FROM rate_limit WHERE ip = ?');
|
|
$stmt->execute([$ip]);
|
|
$row = $stmt->fetch();
|
|
|
|
if ($row === false || $row['window_start'] < $window) {
|
|
$db->prepare('REPLACE INTO rate_limit (ip, window_start, counter) VALUES (?, ?, 1)')
|
|
->execute([$ip, $now->format('Y-m-d H:i:s')]);
|
|
return;
|
|
}
|
|
|
|
if ((int)$row['counter'] >= $max) {
|
|
Response::error('rate_limited', 'Trop de tentatives. Réessaie dans 1 minute.', 429);
|
|
}
|
|
$db->prepare('UPDATE rate_limit SET counter = counter + 1 WHERE ip = ?')->execute([$ip]);
|
|
}
|
|
|
|
private static function audit(\PDO $db, ?int $licenseId, string $ip, string $event, array $detail): void
|
|
{
|
|
try {
|
|
$db->prepare(
|
|
'INSERT INTO audit_log (ts, license_id, ip, event, detail) VALUES (NOW(), ?, ?, ?, ?)'
|
|
)->execute([$licenseId, $ip, $event, json_encode($detail, JSON_UNESCAPED_UNICODE)]);
|
|
} catch (\Throwable) { /* best-effort */ }
|
|
}
|
|
}
|