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

View File

@@ -1,7 +1,7 @@
import axios, { AxiosError } from "axios";
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 { BadRequestError } from "@app/lib/errors";
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";
const getOfflineLicenseContents = (licenseKey: string): TOfflineLicenseContents => {
return JSON.parse(Buffer.from(licenseKey, "base64").toString("utf8")) as TOfflineLicenseContents;
};
export const isOfflineLicenseKey = (licenseKey: string): boolean => {
const contents = getOfflineLicenseContents(licenseKey);
return "signature" in contents && "license" in contents;
try {
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 => {
const cfg = getConfig();
export const getLicenseKeyConfig = (
config?: Pick<TEnvConfig, "LICENSE_KEY" | "LICENSE_KEY_OFFLINE">
): TLicenseKeyConfig => {
const cfg = config || getConfig();
if (!cfg) {
return { isValid: false };
}
const licenseKey = cfg.LICENSE_KEY;

View File

@@ -77,7 +77,7 @@ export const licenseServiceFactory = ({
let instanceType = InstanceType.OnPrem;
let onPremFeatures: TFeatureSet = getDefaultOnPremFeatures();
let selfHostedLicense: TOfflineLicense | null = null;
const licenseKeyConfig = getLicenseKeyConfig();
const licenseKeyConfig = getLicenseKeyConfig(envConfig);
const licenseServerCloudApi = setupLicenseRequestWithStore(
envConfig.LICENSE_SERVER_URL || "",