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; }, params: { type: TokenType; } } export async function generateTokenRequest(req: GenerateTokenRequest, res: Response): Promise { const { query: { code, adminUserID }, params: { type } } = req; const adminUserIDHash = adminUserID ? (await getHashCache(adminUserID)) : null; if (!code || !type) { return res.status(400).send("Invalid request"); } if (type === TokenType.patreon || (type === TokenType.local && adminUserIDHash === config.adminUserID)) { const licenseKey = await createAndSaveToken(type, code); /* istanbul ignore else */ if (licenseKey) { return res.status(200).send(`

Your license key:

${licenseKey}

Copy this into the textbox in the other tab

`); } else { return res.status(401).send(`

Failed to generate an license key

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