import { Request, Response } from "express"; import { config } from "../config"; import { createAndSaveToken, TokenType } from "../utils/tokenUtils"; import { getHashCache } from "../utils/getHashCache"; interface GenerateTokenRequest extends Request { query: { code: string; adminUserID?: string; total?: string; key?: string; }, params: { type: TokenType; } } export async function generateTokenRequest(req: GenerateTokenRequest, res: Response): Promise { const { query: { code, adminUserID, total, key }, params: { type } } = req; const adminUserIDHash = adminUserID ? (await getHashCache(adminUserID)) : null; if (!type || (!code && type === TokenType.patreon)) { return res.status(400).send("Invalid request"); } if (type === TokenType.free && (!key || Math.abs(Date.now() - parseInt(key)) > 1000 * 60 * 60 * 24)) { return res.status(400).send("Invalid request"); } if (type === TokenType.patreon || ([TokenType.local, TokenType.gift].includes(type) && adminUserIDHash === config.adminUserID) || type === TokenType.free) { const licenseKeys = await createAndSaveToken(type, code, adminUserIDHash === config.adminUserID ? parseInt(total) : 1); /* istanbul ignore else */ if (licenseKeys) { if (type === TokenType.patreon) { return res.status(200).send(`

Your license key:

${licenseKeys[0]}

Copy this into the textbox in the other tab

`); } else if (type === TokenType.free) { return res.status(200).send({ licenseKey: licenseKeys[0] }); } else { return res.status(200).send(licenseKeys.join("
")); } } else { return res.status(401).send(`

Failed to generate an license key

`); } } else { return res.sendStatus(403); } }