Revisione/server/utils/license.ts

31 lines
806 B
TypeScript

import { createPublicKey, verify } from "crypto";
function getPublicKey() {
const b64 = process.env.LICENSE_PUBLIC_KEY;
if (!b64) throw createError({ statusCode: 500, message: "License system not configured" });
return createPublicKey({
key: Buffer.from(b64, "base64"),
format: "der",
type: "spki",
});
}
export function verifyLicenseKey(key: string): boolean {
try {
const pubKey = getPublicKey();
const now = Math.floor(Date.now() / 1000 / 300);
const sig = Buffer.from(key, "base64url");
// accept current window and the one before (edge case tolerance)
for (const window of [now, now - 1]) {
const msg = Buffer.from(String(window));
if (verify(null, msg, pubKey, sig)) return true;
}
return false;
} catch {
return false;
}
}