64 lines
1.6 KiB
TypeScript
64 lines
1.6 KiB
TypeScript
import { getLicenseProjectCode } from './server-config';
|
|
import type { SignedLicense } from './types';
|
|
|
|
interface ExpectedLicenseBinding {
|
|
licenseKey: string;
|
|
instanceId: string;
|
|
projectCode?: string;
|
|
fingerprintHash?: string;
|
|
}
|
|
|
|
function isNonEmptyString(value: unknown): value is string {
|
|
return typeof value === 'string' && value.trim().length > 0;
|
|
}
|
|
|
|
export function validateSignedLicenseBinding(
|
|
license: SignedLicense,
|
|
expected: ExpectedLicenseBinding
|
|
): { valid: true } | { valid: false; error: string } {
|
|
const data = license.data as unknown as Record<string, unknown>;
|
|
const projectCode = expected.projectCode ?? getLicenseProjectCode();
|
|
|
|
if (
|
|
!isNonEmptyString(data.projectCode) ||
|
|
!isNonEmptyString(data.licenseKey) ||
|
|
!isNonEmptyString(data.instanceId) ||
|
|
!isNonEmptyString(data.fingerprintHash)
|
|
) {
|
|
return {
|
|
valid: false,
|
|
error: '许可证缺少机器绑定信息,请重新激活',
|
|
};
|
|
}
|
|
|
|
if (data.projectCode !== projectCode) {
|
|
return {
|
|
valid: false,
|
|
error: '许可证项目不匹配,请重新激活',
|
|
};
|
|
}
|
|
|
|
if (data.licenseKey !== expected.licenseKey) {
|
|
return {
|
|
valid: false,
|
|
error: '许可证激活码不匹配,请重新激活',
|
|
};
|
|
}
|
|
|
|
if (data.instanceId !== expected.instanceId) {
|
|
return {
|
|
valid: false,
|
|
error: '许可证不属于当前设备,请重新激活',
|
|
};
|
|
}
|
|
|
|
if (expected.fingerprintHash && data.fingerprintHash !== expected.fingerprintHash) {
|
|
return {
|
|
valid: false,
|
|
error: '许可证硬件指纹不匹配,请重新激活',
|
|
};
|
|
}
|
|
|
|
return { valid: true };
|
|
}
|