Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion ee/app/license/server/license.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class LicenseClass {

private licenses: IValidLicense[] = [];

private encryptedLicenses = new Set<string>();

private tags = new Set<ILicenseTag>();

private modules = new Set<string>();
Expand Down Expand Up @@ -115,6 +117,18 @@ class LicenseClass {
this.validate();
}

lockLicense(encryptedLicense: string): void {
this.encryptedLicenses.add(encryptedLicense);
}

isLicenseDuplicate(encryptedLicense: string): boolean {
if (this.encryptedLicenses.has(encryptedLicense)) {
return true;
}

return false;
}

hasModule(module: string): boolean {
return this.modules.has(module);
}
Expand Down Expand Up @@ -211,7 +225,7 @@ class LicenseClass {
const License = new LicenseClass();

export function addLicense(encryptedLicense: string): boolean {
if (!encryptedLicense || String(encryptedLicense).trim() === '') {
if (!encryptedLicense || String(encryptedLicense).trim() === '' || License.isLicenseDuplicate(encryptedLicense)) {
return false;
}

Expand All @@ -228,6 +242,7 @@ export function addLicense(encryptedLicense: string): boolean {
}

License.addLicense(JSON.parse(decrypted));
License.lockLicense(encryptedLicense);

return true;
} catch (e) {
Expand All @@ -239,6 +254,19 @@ export function addLicense(encryptedLicense: string): boolean {
}
}

export function validateFormat(encryptedLicense: string): boolean {
if (!encryptedLicense || String(encryptedLicense).trim() === '') {
return false;
}

const decrypted = decrypt(encryptedLicense);
if (!decrypted) {
return false;
}

return true;
}

export function setURL(url: string): void {
License.setURL(url);
}
Expand Down Expand Up @@ -283,6 +311,15 @@ export function onValidateLicenses(cb: (...args: any[]) => void): void {
EnterpriseLicenses.on('validate', cb);
}

export function flatModules(modulesAndBundles: string[]): string[] {
const bundles = modulesAndBundles.filter(isBundle);
const modules = modulesAndBundles.filter((x) => !isBundle(x));

const modulesFromBundles = bundles.map(getBundleModules).flat();

return modules.concat(modulesFromBundles);
}

export interface IOverrideClassProperties {
[key: string]: (...args: any[]) => any;
}
Expand Down
21 changes: 13 additions & 8 deletions ee/server/api/licenses.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
import { check } from 'meteor/check';

import { addLicense, getLicenses } from '../../app/license/server/license';
import { ILicense, getLicenses, validateFormat, flatModules } from '../../app/license/server/license';
import { Settings } from '../../../app/models/server';
import { API } from '../../../app/api/server/api';
import { hasPermission } from '../../../app/authorization/server';

function licenseTransform(license: ILicense): ILicense {
return {
...license,
modules: flatModules(license.modules),
};
}

API.v1.addRoute('licenses.get', { authRequired: true }, {
get() {
if (!hasPermission(this.userId, 'view-privileged-setting')) {
return API.v1.unauthorized();
}

const licenses = getLicenses().map((x) => x.license);

if (!licenses || licenses.length === 0) {
return API.v1.failure('Could not find registered licenses');
}
const licenses = getLicenses()
.filter(({ valid }) => valid)
.map(({ license }) => licenseTransform(license));

return API.v1.success({ licenses });
},
Expand All @@ -32,8 +37,8 @@ API.v1.addRoute('licenses.add', { authRequired: true }, {
}

const { license } = this.bodyParams;
if (!addLicense(license)) {
return API.v1.failure({ error: 'Invalid license' });
if (!validateFormat(license)) {
return API.v1.failure('Invalid license');
}

Settings.updateValueById('Enterprise_License', license);
Expand Down