mirror of
https://github.com/gnmyt/LicenseAPI.git
synced 2026-01-10 10:11:47 +00:00
Update validation.ts controller
This commit is contained in:
@ -9,8 +9,7 @@ enum ResponseStatus {
|
|||||||
VALID = "VALID",
|
VALID = "VALID",
|
||||||
EXPIRED = "EXPIRED",
|
EXPIRED = "EXPIRED",
|
||||||
INVALID = "INVALID",
|
INVALID = "INVALID",
|
||||||
INVALID_KEY = "INVALID_KEY",
|
INVALID_KEY = "INVALID_KEY"
|
||||||
MAX_USES_REACHED = "MAX_USES_REACHED"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const validateLicense = async (validationKey: string, licenseKey: string) => {
|
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)
|
if (license.expirationDate && license.expirationDate < new Date() && new Date(license.expirationDate).getTime() !== 0)
|
||||||
return { status: ResponseStatus.EXPIRED, message: "The provided license key has expired" };
|
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);
|
const licenseData = await mapLicense(String(project.id), license, true);
|
||||||
|
|
||||||
if (license.groups) {
|
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 });
|
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) => {
|
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 signer = createSign("RSA-SHA256");
|
||||||
|
|
||||||
const renewalDate = new Date();
|
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");
|
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");
|
content = Buffer.from(content).toString("base64").replace(/(.{64})/g, "$1\n");
|
||||||
|
|
||||||
const file = `-----BEGIN LICENSE KEY-----\n${content}\n-----END LICENSE KEY-----`;
|
const file = `-----BEGIN LICENSE KEY-----\n${content}\n-----END LICENSE KEY-----`;
|
||||||
|
|||||||
Reference in New Issue
Block a user