|
| 1 | +import { FORMAT } from '../const'; |
| 2 | +import { |
| 3 | + DESERIALIZATION_ERROR, |
| 4 | + type ErrorToken, |
| 5 | + GENERAL_ERROR, |
| 6 | + PRODUCT_KIND_ERROR, |
| 7 | + type Token, |
| 8 | + TokenKind, |
| 9 | + VERIFICATION_ERROR, |
| 10 | +} from '../types'; |
| 11 | +import { |
| 12 | + LCP_SIGNATURE, |
| 13 | + RSA_PUBLIC_KEY_XML, |
| 14 | + SIGN_LENGTH, |
| 15 | +} from './const'; |
| 16 | +import { findLatestDevExtremeVersion } from './license_info'; |
| 17 | +import { createProductInfo, type ProductInfo } from './product_info'; |
| 18 | +import { encodeString, shiftDecodeText, verifyHash } from './utils'; |
| 19 | + |
| 20 | +interface ParsedProducts { |
| 21 | + products: ProductInfo[]; |
| 22 | + errorToken?: ErrorToken; |
| 23 | +} |
| 24 | + |
| 25 | +export function isProductOnlyLicense(license: string): boolean { |
| 26 | + return typeof license === 'string' && license.startsWith(LCP_SIGNATURE); |
| 27 | +} |
| 28 | + |
| 29 | +function productsFromString(encodedString: string): ParsedProducts { |
| 30 | + if (!encodedString) { |
| 31 | + return { |
| 32 | + products: [], |
| 33 | + errorToken: GENERAL_ERROR, |
| 34 | + }; |
| 35 | + } |
| 36 | + |
| 37 | + try { |
| 38 | + const splitInfo = encodedString.split(';'); |
| 39 | + const productTuples = splitInfo.slice(1).filter((entry) => entry.length > 0); |
| 40 | + const products = productTuples.map((tuple) => { |
| 41 | + const parts = tuple.split(','); |
| 42 | + const version = Number.parseInt(parts[0], 10); |
| 43 | + const productsValue = BigInt(parts[1]); |
| 44 | + return createProductInfo( |
| 45 | + version, |
| 46 | + productsValue, |
| 47 | + ); |
| 48 | + }); |
| 49 | + |
| 50 | + return { |
| 51 | + products, |
| 52 | + }; |
| 53 | + } catch (error) { |
| 54 | + return { |
| 55 | + products: [], |
| 56 | + errorToken: DESERIALIZATION_ERROR, |
| 57 | + }; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +export function parseDevExpressProductKey(productsLicenseSource: string): Token { |
| 62 | + if (!isProductOnlyLicense(productsLicenseSource)) { |
| 63 | + return GENERAL_ERROR; |
| 64 | + } |
| 65 | + |
| 66 | + try { |
| 67 | + const productsLicense = atob( |
| 68 | + shiftDecodeText(productsLicenseSource.substring(LCP_SIGNATURE.length)), |
| 69 | + ); |
| 70 | + |
| 71 | + const signature = productsLicense.substring(0, SIGN_LENGTH); |
| 72 | + const productsPayload = productsLicense.substring(SIGN_LENGTH); |
| 73 | + |
| 74 | + if (!verifyHash(RSA_PUBLIC_KEY_XML, productsPayload, signature)) { |
| 75 | + return VERIFICATION_ERROR; |
| 76 | + } |
| 77 | + |
| 78 | + const { |
| 79 | + products, |
| 80 | + errorToken, |
| 81 | + } = productsFromString( |
| 82 | + encodeString(productsPayload, shiftDecodeText), |
| 83 | + ); |
| 84 | + |
| 85 | + if (errorToken) { |
| 86 | + return errorToken; |
| 87 | + } |
| 88 | + |
| 89 | + const maxVersionAllowed = findLatestDevExtremeVersion({ products }); |
| 90 | + |
| 91 | + if (!maxVersionAllowed) { |
| 92 | + return PRODUCT_KIND_ERROR; |
| 93 | + } |
| 94 | + |
| 95 | + return { |
| 96 | + kind: TokenKind.verified, |
| 97 | + payload: { |
| 98 | + customerId: '', |
| 99 | + maxVersionAllowed, |
| 100 | + format: FORMAT, |
| 101 | + }, |
| 102 | + }; |
| 103 | + } catch (error) { |
| 104 | + return GENERAL_ERROR; |
| 105 | + } |
| 106 | +} |
0 commit comments