feat: implement license platform business flows and delivery tooling
Some checks failed
Check / delivery-static (push) Has been cancelled
Check / api (push) Has been cancelled
Check / web (push) Has been cancelled

Add admin and customer interfaces, orders, signed activation and leases, protected package delivery, keyring verification, migrations, security controls, tests and deployment documentation.

Co-Authored-By: Craft Agent <agents-noreply@craft.do>
This commit is contained in:
2026-10-09 15:33:22 +08:00
parent 3ab651d3a8
commit 54c318c615
142 changed files with 11105 additions and 178 deletions

52
client/verify.mjs Normal file
View File

@@ -0,0 +1,52 @@
import { createHash, createPublicKey, verify } from 'node:crypto';
const fields = {
'license-v2': ['version', 'instance_id', 'license_id', 'customer_id', 'product_id', 'starts_at', 'expires_at', 'key_id'],
'lease-v2': ['version', 'instance_id', 'state', 'server_time', 'entitlement_expires_at', 'lease_expires_at', 'key_id'],
};
export function canonical(payload) {
const names = fields[payload.version];
if (!names || Object.keys(payload).length !== names.length) throw new Error('unsupported_payload');
return names.map(name => {
const value = payload[name];
if (['license_id', 'customer_id', 'product_id'].includes(name)) {
if (!Number.isSafeInteger(value) || value <= 0) throw new Error('invalid_id');
} else if (typeof value !== 'string' || !value || /[\r\n\0]/.test(value)) throw new Error('invalid_field');
return `${name}=${value}`;
}).join('\n');
}
export function verifyEnvelope(envelope, pinnedKeys) {
const p = envelope.payload;
if (!p || !/^[a-zA-Z0-9_-]{1,64}$/.test(p.key_id ?? '')) throw new Error('invalid_key_id');
const matches = pinnedKeys.filter(k => k.key_id === p.key_id);
if (matches.length !== 1 || matches[0].revoked) throw new Error('unknown_or_revoked_key');
const key = createPublicKey(matches[0].public_key_pem);
if (key.asymmetricKeyType !== 'ec' || key.asymmetricKeyDetails.namedCurve !== 'prime256v1') throw new Error('invalid_key');
const text = canonical(p);
if (typeof envelope.signature !== 'string' || !/^[A-Za-z0-9+/]+$/.test(envelope.signature)) throw new Error('invalid_signature');
if (!verify('sha256', Buffer.from(text), key, Buffer.from(envelope.signature, 'base64'))) throw new Error('invalid_signature');
if (envelope.canonical_sha256 && createHash('sha256').update(text).digest('hex') !== envelope.canonical_sha256) throw new Error('invalid_digest');
return p;
}
function instant(value) {
if (typeof value !== 'string' || !/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{1,9})?Z$/.test(value)) throw new Error('invalid_time');
const time = Date.parse(value);
if (!Number.isFinite(time)) throw new Error('invalid_time');
return time;
}
// Persist the returned state atomically in trusted storage BEFORE enabling work.
export function acceptLease(envelope, pinnedKeys, expectedInstance, prior, now) {
const p = verifyEnvelope(envelope, pinnedKeys);
if (p.version !== 'lease-v2' || p.instance_id !== expectedInstance || !Number.isFinite(now)) throw new Error('instance_or_version');
const server = instant(p.server_time), expiry = instant(p.lease_expires_at), entitlement = instant(p.entitlement_expires_at);
const previous = prior ?? { instance_id: expectedInstance, server_high_water: 0, local_high_water: 0, lease_expires: 0, enabled: false };
if (previous.instance_id !== expectedInstance || !Number.isFinite(previous.server_high_water) || !Number.isFinite(previous.local_high_water)) throw new Error('invalid_state');
if (now < previous.local_high_water || server < previous.server_high_water) throw new Error('clock_or_server_rollback');
if (Math.abs(server - now) > 300000 || expiry > entitlement || expiry > server + 14 * 86400000) throw new Error('lease_bounds');
if (!['active', 'frozen', 'revoked', 'expired', 'not_started', 'not_activated'].includes(p.state)) throw new Error('invalid_state');
const enabled = p.state === 'active' && now < expiry && now < entitlement;
return { instance_id: expectedInstance, server_high_water: server, local_high_water: now, lease_expires: expiry, enabled };
}
export function canRunOffline(state, expectedInstance, now) {
return state?.instance_id === expectedInstance && state.enabled === true && Number.isFinite(now) && now >= state.local_high_water && now < state.lease_expires;
}