[ * 'server' => env('DEVENTITLE_SERVER', 'https://terminal.deventitle.com'), * 'product' => 'acme-crm', * 'public_key' => env('DEVENTITLE_RELEASE_PUBLIC_KEY'), // or hard-code it * ], * * $result = DevEntitle::activate($key); // ['valid' => true, 'message' => …] * $latest = DevEntitle::checkForUpdate($key); // null when up to date * DevEntitle::downloadVerified($latest, storage_path('app/update.zip')); * * Never switch features off because of a licence answer or an unreachable * server. Sends only the licence key, product, domain and version. */ class DevEntitle { public static function domain(): string { return strtolower((string) parse_url((string) config('app.url'), PHP_URL_HOST)) ?: 'localhost'; } public static function activate(string $licenseKey, ?string $version = null): array { return static::post('activate', $licenseKey, $version); } public static function deactivate(string $licenseKey, ?string $version = null): void { static::post('deactivate', $licenseKey, $version); } /** The newest offered release when newer than $version (default: your app's version), else null. */ public static function checkForUpdate(string $licenseKey, ?string $version = null): ?array { $version ??= (string) config('app.version', '0.0.0'); $latest = static::post('updates', $licenseKey, $version)['latest'] ?? null; return is_array($latest) && version_compare($latest['version'] ?? '0', $version, '>') ? $latest : null; } /** Download and check SHA-256 + Ed25519 signature; throws and deletes the file on any mismatch. */ public static 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.'); } $response = Http::timeout(600)->withoutRedirecting()->sink($targetPath)->get($latest['download_url']); if (! $response->successful()) { @unlink($targetPath); throw new RuntimeException('The download failed (HTTP '.$response->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((string) config('services.deventitle.public_key'), true); $message = config('services.deventitle.product').'|'.$latest['version'].'|'.$sha; // Length checks first: sodium throws on a malformed signature or key instead of returning false. if (! hash_equals(strtolower($latest['sha256']), $sha) || ! $signature || strlen($signature) !== SODIUM_CRYPTO_SIGN_BYTES || ! $public || 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.'); } } protected static function post(string $endpoint, string $licenseKey, ?string $version): array { $response = Http::acceptJson()->timeout(20)->post(rtrim((string) config('services.deventitle.server'), '/').'/api/v1/'.$endpoint, [ 'license_key' => $licenseKey, 'product' => config('services.deventitle.product'), 'domain' => static::domain(), 'version' => $version ?? (string) config('app.version', '0.0.0'), ]); if ($response->serverError() || ! is_array($response->json())) { throw new RuntimeException('The licence server could not be reached (HTTP '.$response->status().'). Try again later.'); } return $response->json(); } }