فینورا پرو - سامانه جامع حسابداری و صدور فاکتور

feat(A06): verify installation-bound signed offline lease with trusted build key

davoodmehraban89 committed Oct 15, 2026 at 20:44 UTC b45c33bdc2858ea50a0287f8978c6b03a5ed84f3
1 file changed +38
src/core/LicenseActivationCore.mjs new
+38
@@ -0,0 +1,38 @@
1 +import { validateEntitlementPayload, verifyEntitlement } from './EntitlementVerifier.mjs';
2 +import { OFFLINE_LEASE_DAYS } from './LicensePolicy.mjs';
3 +
4 +export function loadProductionLicensePublicKey(rawValue) {
5 + if (typeof rawValue !== 'string' || !rawValue.trim()) return null;
6 + try {
7 + const jwk = JSON.parse(rawValue);
8 + if (!jwk || jwk.kty !== 'EC' || jwk.crv !== 'P-256' ||
9 + typeof jwk.x !== 'string' || typeof jwk.y !== 'string' ||
10 + !jwk.x || !jwk.y || 'd' in jwk ||
11 + (jwk.key_ops && JSON.stringify(jwk.key_ops) !== '["verify"]')) {
12 + return null;
13 + }
14 + return jwk;
15 + } catch { return null; }
16 +}
17 +
18 +/**
19 + * Verify a signed, installation-bound offline lease.
20 + * NEVER pass a public key from the token itself; the trusted key is only
21 + * supplied by the application build configuration.
22 + */
23 +export async function validateOfflineActivation(token, publicJwk, installationId, nowMs = Date.now()) {
24 + if (!publicJwk || !token || typeof token !== 'object' ||
25 + !validateEntitlementPayload(token.payload, installationId, nowMs)) return false;
26 + const lease = token.payload.lastOnlineCheckAt;
27 + if (typeof lease !== 'string' || !lease.trim()) return false;
28 + const checkedAtMs = Date.parse(lease);
29 + if (!Number.isFinite(checkedAtMs) ||
30 + checkedAtMs > nowMs + 5 * 60 * 1000 ||
31 + nowMs - checkedAtMs > OFFLINE_LEASE_DAYS * 86400000) return false;
32 + // Prevent using a token ostensibly signed after its offline lease timestamp.
33 + const issuedAt = token.payload.issuedAt;
34 + if (typeof issuedAt !== 'string' || !Number.isFinite(Date.parse(issuedAt)) ||
35 + Date.parse(issuedAt) > checkedAtMs ||
36 + Date.parse(issuedAt) > nowMs + 5 * 60 * 1000) return false;
37 + return verifyEntitlement(token, publicJwk);
38 +}