activate($key, 'crm.buyer.com', '1.4.2'); // ['valid' => true, 'message' => …] * $latest = $client->checkForUpdate($key, 'crm.buyer.com', '1.4.2'); // null when up to date * if ($latest && isset($latest['download_url'])) { * $client->downloadVerified($latest, '/tmp/acme-crm-'.$latest['version'].'.zip'); * } * * Rules: never switch features off because of a licence answer or because * the server can't be reached. Show the message; hold back updates only. * Sends only the licence key, product, domain and version. * * Needs PHP 7.4+ with curl and sodium. Licence: MIT. */ final class DevEntitleClient { /** @var string */ private $server; /** @var string */ private $product; /** @var string|null base64 Ed25519 public key, built into your product */ private $publicKey; public function __construct(string $server, string $product, ?string $publicKey = null) { $this->server = rtrim($server, '/'); $this->product = $product; $this->publicKey = $publicKey; } /** @return array{valid: bool, message: string, buyer?: string, license_type?: string, supported_until?: ?string} */ public function activate(string $licenseKey, string $domain, string $version): array { return $this->post('activate', $licenseKey, $domain, $version); } /** Free this domain so the key can be used elsewhere. */ public function deactivate(string $licenseKey, string $domain, string $version): void { $this->post('deactivate', $licenseKey, $domain, $version); } /** * The newest release offered to this licence when it is newer than * $version, else null. Check $latest['download_url']: when it is missing, * show $latest['message'] (e.g. support ended) instead of an install button. */ public function checkForUpdate(string $licenseKey, string $domain, string $version): ?array { $result = $this->post('updates', $licenseKey, $domain, $version); $latest = $result['latest'] ?? null; return is_array($latest) && version_compare((string) ($latest['version'] ?? '0'), $version, '>') ? $latest : null; } /** * Download a release and check its SHA-256 and Ed25519 signature before * returning. Throws (and deletes the file) if anything doesn't match. */ public function downloadVerified(array $latest, string $targetPath): void { if (empty($latest['download_url']) || empty($latest['signature'])) { throw new RuntimeException($latest['message'] ?? 'This licence may not download this release.'); } if (! $this->publicKey) { throw new RuntimeException('No release public key is configured, so the download can\'t be checked.'); } $file = fopen($targetPath, 'wb'); $curl = curl_init($latest['download_url']); curl_setopt_array($curl, [CURLOPT_FILE => $file, CURLOPT_FOLLOWLOCATION => false, CURLOPT_TIMEOUT => 600, CURLOPT_FAILONERROR => true]); $ok = curl_exec($curl); $status = curl_getinfo($curl, CURLINFO_RESPONSE_CODE); curl_close($curl); fclose($file); if (! $ok || $status !== 200) { @unlink($targetPath); throw new RuntimeException("The download failed (HTTP {$status}). Check for updates again to get a fresh link."); } $sha = hash_file('sha256', $targetPath); $signature = base64_decode((string) $latest['signature'], true); $public = base64_decode($this->publicKey, true); $message = $this->product.'|'.$latest['version'].'|'.$sha; // Length checks first: sodium throws on a malformed signature or key instead of returning false. if (! hash_equals(strtolower((string) $latest['sha256']), $sha) || $signature === false || strlen($signature) !== SODIUM_CRYPTO_SIGN_BYTES || $public === false || strlen($public) !== SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES || ! sodium_crypto_sign_verify_detached($signature, $message, $public)) { @unlink($targetPath); throw new RuntimeException('The download did not pass the integrity and signature check. Nothing was installed.'); } } private function post(string $endpoint, string $licenseKey, string $domain, string $version): array { $curl = curl_init($this->server.'/api/v1/'.$endpoint); curl_setopt_array($curl, [ CURLOPT_POST => true, CURLOPT_POSTFIELDS => json_encode(['license_key' => $licenseKey, 'product' => $this->product, 'domain' => $domain, 'version' => $version]), CURLOPT_HTTPHEADER => ['Content-Type: application/json', 'Accept: application/json'], CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 20, ]); $body = curl_exec($curl); $status = curl_getinfo($curl, CURLINFO_RESPONSE_CODE); curl_close($curl); $data = is_string($body) ? json_decode($body, true) : null; // Licence answers are HTTP 200 (and 429 when checking too often); anything else is the server's problem. if (! is_array($data) || $status >= 500 || $status === 0) { throw new RuntimeException('The licence server could not be reached (HTTP '.$status.'). Try again later.'); } return $data; } }