refactor: enhance license key handling and error management

This commit is contained in:
Piyush Gupta
2025-11-14 20:37:44 +05:30
parent 10a2d7e9ae
commit c2bdd12dd5
2 changed files with 17 additions and 10 deletions
+16 -9
View File
@@ -1,7 +1,7 @@
import axios, { AxiosError } from "axios"; import axios, { AxiosError } from "axios";
import { TLicenseServiceFactory } from "@app/ee/services/license/license-service"; import { TLicenseServiceFactory } from "@app/ee/services/license/license-service";
import { getConfig } from "@app/lib/config/env"; import { getConfig, TEnvConfig } from "@app/lib/config/env";
import { request } from "@app/lib/config/request"; import { request } from "@app/lib/config/request";
import { BadRequestError } from "@app/lib/errors"; import { BadRequestError } from "@app/lib/errors";
import { logger } from "@app/lib/logger"; import { logger } from "@app/lib/logger";
@@ -9,17 +9,24 @@ import { UserAliasType } from "@app/services/user-alias/user-alias-types";
import { TFeatureSet, TLicenseKeyConfig, TOfflineLicenseContents } from "./license-types"; import { TFeatureSet, TLicenseKeyConfig, TOfflineLicenseContents } from "./license-types";
const getOfflineLicenseContents = (licenseKey: string): TOfflineLicenseContents => {
return JSON.parse(Buffer.from(licenseKey, "base64").toString("utf8")) as TOfflineLicenseContents;
};
export const isOfflineLicenseKey = (licenseKey: string): boolean => { export const isOfflineLicenseKey = (licenseKey: string): boolean => {
const contents = getOfflineLicenseContents(licenseKey); try {
return "signature" in contents && "license" in contents; const contents = JSON.parse(Buffer.from(licenseKey, "base64").toString("utf8")) as TOfflineLicenseContents;
return "signature" in contents && "license" in contents;
} catch (error) {
return false;
}
}; };
export const getLicenseKeyConfig = (): TLicenseKeyConfig => { export const getLicenseKeyConfig = (
const cfg = getConfig(); config?: Pick<TEnvConfig, "LICENSE_KEY" | "LICENSE_KEY_OFFLINE">
): TLicenseKeyConfig => {
const cfg = config || getConfig();
if (!cfg) {
return { isValid: false };
}
const licenseKey = cfg.LICENSE_KEY; const licenseKey = cfg.LICENSE_KEY;
@@ -77,7 +77,7 @@ export const licenseServiceFactory = ({
let instanceType = InstanceType.OnPrem; let instanceType = InstanceType.OnPrem;
let onPremFeatures: TFeatureSet = getDefaultOnPremFeatures(); let onPremFeatures: TFeatureSet = getDefaultOnPremFeatures();
let selfHostedLicense: TOfflineLicense | null = null; let selfHostedLicense: TOfflineLicense | null = null;
const licenseKeyConfig = getLicenseKeyConfig(); const licenseKeyConfig = getLicenseKeyConfig(envConfig);
const licenseServerCloudApi = setupLicenseRequestWithStore( const licenseServerCloudApi = setupLicenseRequestWithStore(
envConfig.LICENSE_SERVER_URL || "", envConfig.LICENSE_SERVER_URL || "",