mirror of
https://github.com/gnmyt/LicenseAPI.git
synced 2026-01-09 17:51:47 +00:00
Create validation.ts#signOfflineKey
This commit is contained in:
@ -3,6 +3,7 @@ import { License } from "@models/License";
|
||||
import { mapLicense } from "@controller/license";
|
||||
import { Group } from "@models/Group";
|
||||
import { Permission } from "@models/Permission";
|
||||
import {createSign} from "node:crypto";
|
||||
|
||||
enum ResponseStatus {
|
||||
VALID = "VALID",
|
||||
@ -39,4 +40,29 @@ export const validateLicense = async (validationKey: string, licenseKey: string)
|
||||
await License.updateOne({ _id: license.id }, { currentUses: license.currentUses + 1 });
|
||||
|
||||
return { status: ResponseStatus.VALID, license: licenseData };
|
||||
}
|
||||
|
||||
export const signOfflineKey = async (validationKey: string, licenseKey: string) => {
|
||||
const license = await validateLicense(validationKey, licenseKey);
|
||||
|
||||
if (license.status !== ResponseStatus.VALID) return { status: license.status, message: license.message, file: null};
|
||||
|
||||
const project = await Project.findOne({ validationKey });
|
||||
if (project === null) return { status: ResponseStatus.INVALID_KEY, message: "The provided validation key is invalid", file: null };
|
||||
|
||||
const signer = createSign("RSA-SHA256");
|
||||
|
||||
const renewalDate = new Date();
|
||||
renewalDate.setDate(renewalDate.getDate() + 7); // TODO: Let the user choose the renewal date
|
||||
|
||||
signer.update(JSON.stringify({...license.license, renewalDate}));
|
||||
|
||||
const signature = signer.sign(project.privateKey, "hex");
|
||||
|
||||
let content = JSON.stringify({signature, data: {...license.license, renewalDate}});
|
||||
content = Buffer.from(content).toString('base64').replace(/(.{64})/g, "$1\n");
|
||||
|
||||
const file = `-----BEGIN LICENSE KEY-----\n${content}\n-----END LICENSE KEY-----`;
|
||||
|
||||
return { status: ResponseStatus.VALID, file };
|
||||
}
|
||||
Reference in New Issue
Block a user