34 lines
906 B
TypeScript
34 lines
906 B
TypeScript
import { createPublicKey, verify } from "crypto";
|
|
|
|
function getPublicKey() {
|
|
const b64 = process.env.LICENSE_PUBLIC_KEY;
|
|
if (!b64) {
|
|
console.error("[revisione] LICENSE_PUBLIC_KEY is not set — license system not configured");
|
|
throw createError({ statusCode: 500, message: "Internal server error" });
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|