/** * DevEntitle licence client for Node.js 18+ (no packages). Copy into your product. * * import { DevEntitleClient } from './deventitle-client.mjs'; * const client = new DevEntitleClient({ server: 'https://terminal.deventitle.com', product: 'acme-crm', publicKey: ACME_RELEASE_PUBLIC_KEY }); * const result = await client.activate(key, 'crm.buyer.com', '1.4.2'); // { valid: true, message: … } * const latest = await client.checkForUpdate(key, 'crm.buyer.com', '1.4.2'); // null when up to date * if (latest?.download_url) await client.downloadVerified(latest, `/tmp/acme-crm-${latest.version}.zip`); * * Never switch features off because of a licence answer or an unreachable * server. Sends only the licence key, product, domain and version. Licence: MIT. */ import { createHash, createPublicKey, verify } from 'node:crypto'; import { writeFile } from 'node:fs/promises'; export class DevEntitleClient { constructor({ server, product, publicKey = null }) { this.server = server.replace(/\/+$/, ''); this.product = product; this.publicKey = publicKey; } activate(licenseKey, domain, version) { return this.#post('activate', licenseKey, domain, version); } async deactivate(licenseKey, domain, version) { await this.#post('deactivate', licenseKey, domain, version); } /** The newest offered release when newer than `version`, else null. Without `download_url`, show `message`. */ async checkForUpdate(licenseKey, domain, version) { const { latest } = await this.#post('updates', licenseKey, domain, version); return latest && compareVersions(latest.version, version) > 0 ? latest : null; } /** Download and check SHA-256 + Ed25519 signature; throws (and deletes the file) on any mismatch. */ async downloadVerified(latest, targetPath) { if (!latest?.download_url || !latest?.signature) throw new Error(latest?.message ?? 'This licence may not download this release.'); if (!this.publicKey) throw new Error("No release public key is configured, so the download can't be checked."); const response = await fetch(latest.download_url, { redirect: 'error' }); if (response.status !== 200) throw new Error(`The download failed (HTTP ${response.status}). Check for updates again to get a fresh link.`); const body = Buffer.from(await response.arrayBuffer()); const sha = createHash('sha256').update(body).digest('hex'); const key = createPublicKey({ key: { kty: 'OKP', crv: 'Ed25519', x: Buffer.from(this.publicKey, 'base64').toString('base64url') }, format: 'jwk' }); const ok = sha === String(latest.sha256).toLowerCase() && verify(null, Buffer.from(`${this.product}|${latest.version}|${sha}`), key, Buffer.from(latest.signature, 'base64')); if (!ok) throw new Error('The download did not pass the integrity and signature check. Nothing was installed.'); await writeFile(targetPath, body); } async #post(endpoint, licenseKey, domain, version) { let response; try { response = await fetch(`${this.server}/api/v1/${endpoint}`, { method: 'POST', headers: { 'Content-Type': 'application/json', Accept: 'application/json' }, body: JSON.stringify({ license_key: licenseKey, product: this.product, domain, version }), signal: AbortSignal.timeout(20000), }); } catch { throw new Error('The licence server could not be reached. Try again later.'); } const data = await response.json().catch(() => null); if (response.status >= 500 || !data) throw new Error(`The licence server could not be reached (HTTP ${response.status}). Try again later.`); return data; } } /** 1.10.0 > 1.9.0; a pre-release (1.0.0-beta.1) is older than its release. */ export function compareVersions(a, b) { const [mainA, preA] = String(a).split('-', 2); const [mainB, preB] = String(b).split('-', 2); const partsA = mainA.split('.').map(Number); const partsB = mainB.split('.').map(Number); for (let i = 0; i < Math.max(partsA.length, partsB.length); i++) { const diff = (partsA[i] ?? 0) - (partsB[i] ?? 0); if (diff !== 0) return Math.sign(diff); } if (preA === preB) return 0; if (!preA) return 1; if (!preB) return -1; return preA.localeCompare(preB, 'en', { numeric: true }); }