Update validation.ts controller

This commit is contained in:
Mathias Wagner
2024-07-30 21:41:32 +02:00
parent 896d37c162
commit 03748e0a73

View File

@ -9,8 +9,7 @@ enum ResponseStatus {
VALID = "VALID",
EXPIRED = "EXPIRED",
INVALID = "INVALID",
INVALID_KEY = "INVALID_KEY",
MAX_USES_REACHED = "MAX_USES_REACHED"
INVALID_KEY = "INVALID_KEY"
}
export const validateLicense = async (validationKey: string, licenseKey: string) => {
@ -23,9 +22,6 @@ export const validateLicense = async (validationKey: string, licenseKey: string)
if (license.expirationDate && license.expirationDate < new Date() && new Date(license.expirationDate).getTime() !== 0)
return { status: ResponseStatus.EXPIRED, message: "The provided license key has expired" };
if (license.maxUses && license.maxUses !== -1 && license.maxUses <= license.currentUses)
return { status: ResponseStatus.MAX_USES_REACHED, message: "The provided license key has reached its maximum uses" };
const licenseData = await mapLicense(String(project.id), license, true);
if (license.groups) {
@ -39,7 +35,25 @@ export const validateLicense = async (validationKey: string, licenseKey: string)
await License.updateOne({ _id: license.id }, { currentUses: license.currentUses + 1 });
return { status: ResponseStatus.VALID, license: licenseData };
return { status: ResponseStatus.VALID, ...licenseData };
}
export const signKey = 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 timestamp = Math.floor(new Date().getTime() / 1000);
const signer = createSign("RSA-SHA256");
signer.update(JSON.stringify({...license, timestamp}));
const signature = signer.sign(project.privateKey, "hex");
return {signature, object: {...license, timestamp}};
}
export const signOfflineKey = async (validationKey: string, licenseKey: string) => {
@ -53,13 +67,14 @@ export const signOfflineKey = async (validationKey: string, licenseKey: string)
const signer = createSign("RSA-SHA256");
const renewalDate = new Date();
renewalDate.setDate(renewalDate.getDate() + (project.offlineRenewalDays || 7));
renewalDate.setMinutes(renewalDate.getMinutes() + 1);
// renewalDate.setDate(renewalDate.getDate() + (project.offlineRenewalDays || 7));
signer.update(JSON.stringify({...license.license, renewalDate}));
signer.update(JSON.stringify({...license, renewalDate}));
const signature = signer.sign(project.privateKey, "hex");
let content = JSON.stringify({signature, data: {...license.license, renewalDate}});
let content = JSON.stringify({signature, data: {...license, renewalDate}});
content = Buffer.from(content).toString("base64").replace(/(.{64})/g, "$1\n");
const file = `-----BEGIN LICENSE KEY-----\n${content}\n-----END LICENSE KEY-----`;