initialize project with basic structure and dependencies

This commit is contained in:
ImBenji
2026-04-27 22:26:40 +01:00
parent a3b3bd6b04
commit 83f2837ce6
7 changed files with 250 additions and 6 deletions
+6 -1
View File
@@ -1,8 +1,13 @@
import { db } from "../../db/index";
import { courses } from "../../db/schema";
import { randomUUID } from "crypto";
import { verifyLicenseKey } from "../../utils/license";
export default defineEventHandler(async () => {
export default defineEventHandler(async (event) => {
const key = getHeader(event, "x-license-key") ?? "";
if (!verifyLicenseKey(key)) {
throw createError({ statusCode: 401, message: "Invalid or expired license key" });
}
const id = randomUUID();
await db.insert(courses).values({
+31
View File
@@ -0,0 +1,31 @@
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;
}
}