Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[x-license] Add support for plan version #13459

Merged
merged 25 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
190a18e
license update proposal
michelengelen Jun 12, 2024
1706016
Revert "license update proposal"
michelengelen Jun 13, 2024
93a4e9a
second iteration on license update
michelengelen Jun 13, 2024
8b4369e
3rd iteration
michelengelen Jun 13, 2024
f815941
changed status return on charts/treeview check
michelengelen Jun 13, 2024
3ccba21
removed tests for now
michelengelen Jun 13, 2024
46cf0d5
adjusted things and fixed tests
michelengelen Jun 13, 2024
da18b3f
review remarks
michelengelen Jun 14, 2024
ac5e804
removed productScope optional marking
michelengelen Jun 14, 2024
300e0b5
Update verifyLicense.ts
michelengelen Jun 14, 2024
e62b985
added license generation tests
michelengelen Jun 17, 2024
f4b0ed5
fixed typo
michelengelen Jun 17, 2024
b5cec8b
Added 'Mui X:' prefix on error
michelengelen Jun 17, 2024
88056dd
Added Key Version 2.2 tests
michelengelen Jun 18, 2024
508c785
added product scope tests
michelengelen Jun 18, 2024
c6e7a17
added useLicenseVerifier test
michelengelen Jun 18, 2024
1a26ce8
Apply suggestions from code review
michelengelen Jun 19, 2024
e572ab4
added some tests requested from @cherniavskii
michelengelen Jun 19, 2024
c67d0f9
Update packages/x-license/src/utils/licenseErrorMessageUtils.ts
michelengelen Jun 19, 2024
001a411
changed error wording
michelengelen Jun 19, 2024
4fffe6a
added link to license page back in
michelengelen Jun 20, 2024
c04fa6b
Merge remote-tracking branch 'refs/remotes/upstream/master' into lice…
michelengelen Jun 20, 2024
f19b30e
added text for new license state to watermark
michelengelen Jun 20, 2024
a0da60e
fix tests
michelengelen Jun 20, 2024
a19da9d
fix tests
michelengelen Jun 20, 2024
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
12 changes: 8 additions & 4 deletions packages/x-license/src/generateLicense/generateLicense.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ describe('License: generateLicense', () => {
orderNumber: 'MUI-123',
scope: 'pro',
licensingModel: 'subscription',
planVersion: 'legacy',
}),
).to.equal(
'b2b2ea9c6fd846e11770da3c795d6f63Tz1NVUktMTIzLEU9MTU5MTcyMzg3OTA2MixTPXBybyxMTT1zdWJzY3JpcHRpb24sS1Y9Mg==',
'076fc290e354b9a04720190023e1f868Tz1NVUktMTIzLEU9MTU5MTcyMzg3OTA2MixTPXBybyxMTT1zdWJzY3JpcHRpb24sUFY9bGVnYWN5LEtWPTI=',
);
});

Expand All @@ -22,9 +23,10 @@ describe('License: generateLicense', () => {
orderNumber: 'MUI-123',
scope: 'premium',
licensingModel: 'subscription',
planVersion: 'legacy',
}),
).to.equal(
'ac8d20b4ecd1f919157f3713f8ba1651Tz1NVUktMTIzLEU9MTU5MTcyMzg3OTA2MixTPXByZW1pdW0sTE09c3Vic2NyaXB0aW9uLEtWPTI=',
'fafc52d1dbb97e825702a7ccf5986fbbTz1NVUktMTIzLEU9MTU5MTcyMzg3OTA2MixTPXByZW1pdW0sTE09c3Vic2NyaXB0aW9uLFBWPWxlZ2FjeSxLVj0y',
);
});

Expand All @@ -35,9 +37,10 @@ describe('License: generateLicense', () => {
orderNumber: 'MUI-123',
scope: 'pro',
licensingModel: 'subscription',
planVersion: 'legacy',
}),
).to.equal(
'b2b2ea9c6fd846e11770da3c795d6f63Tz1NVUktMTIzLEU9MTU5MTcyMzg3OTA2MixTPXBybyxMTT1zdWJzY3JpcHRpb24sS1Y9Mg==',
'076fc290e354b9a04720190023e1f868Tz1NVUktMTIzLEU9MTU5MTcyMzg3OTA2MixTPXBybyxMTT1zdWJzY3JpcHRpb24sUFY9bGVnYWN5LEtWPTI=',
);
});

Expand All @@ -48,9 +51,10 @@ describe('License: generateLicense', () => {
orderNumber: 'MUI-123',
scope: 'pro',
licensingModel: 'perpetual',
planVersion: 'legacy',
}),
).to.equal(
'b16edd8e6bc83293a723779a259f520cTz1NVUktMTIzLEU9MTU5MTcyMzg3OTA2MixTPXBybyxMTT1wZXJwZXR1YWwsS1Y9Mg==',
'a32c4d6c8cce5d9ca0132527a252e999Tz1NVUktMTIzLEU9MTU5MTcyMzg3OTA2MixTPXBybyxMTT1wZXJwZXR1YWwsUFY9bGVnYWN5LEtWPTI=',
);
});
});
16 changes: 12 additions & 4 deletions packages/x-license/src/generateLicense/generateLicense.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { md5 } from '../encoding/md5';
import { base64Encode } from '../encoding/base64';
import { LICENSE_SCOPES, LicenseScope } from '../utils/licenseScope';
import { LICENSE_SCOPES, LicenseScope, PlanVersion } from '../utils/licenseScope';
import { LICENSING_MODELS, LicensingModel } from '../utils/licensingModel';

const licenseVersion = '2';
Expand All @@ -10,6 +10,7 @@ export interface LicenseDetails {
expiryDate: Date;
scope: LicenseScope;
licensingModel: LicensingModel;
planVersion: PlanVersion;
}

function getClearLicenseString(details: LicenseDetails) {
Expand All @@ -21,9 +22,16 @@ function getClearLicenseString(details: LicenseDetails) {
throw new Error('MUI X: Invalid licensing model');
}

return `O=${details.orderNumber},E=${details.expiryDate.getTime()},S=${details.scope},LM=${
details.licensingModel
},KV=${licenseVersion}`;
const keyParts = [
michelengelen marked this conversation as resolved.
Show resolved Hide resolved
`O=${details.orderNumber}`,
`E=${details.expiryDate.getTime()}`,
`S=${details.scope}`,
`LM=${details.licensingModel}`,
`PV=${details.planVersion}`,
`KV=${licenseVersion}`,
];

return keyParts.join(',');
}

export function generateLicense(details: LicenseDetails) {
Expand Down
13 changes: 9 additions & 4 deletions packages/x-license/src/useLicenseVerifier/useLicenseVerifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import {
showExpiredPackageVersionError,
} from '../utils/licenseErrorMessageUtils';
import { LICENSE_STATUS, LicenseStatus } from '../utils/licenseStatus';
import { LicenseScope } from '../utils/licenseScope';
import {
extractAcceptedScopes,
extractProductScope,
LicenseScope,
ProductScope,
} from '../utils/licenseScope';
import MuiLicenseInfoContext from '../Unstable_LicenseInfoProvider/MuiLicenseInfoContext';

export type MuiCommercialPackageName =
Expand Down Expand Up @@ -47,15 +52,15 @@ export function useLicenseVerifier(
return sharedLicenseStatuses[packageName]!.licenseVerifier;
}

const acceptedScopes: LicenseScope[] = packageName.includes('premium')
? ['premium']
: ['pro', 'premium'];
const acceptedScopes: readonly LicenseScope[] = extractAcceptedScopes(packageName);
michelengelen marked this conversation as resolved.
Show resolved Hide resolved
const productScope: ProductScope = extractProductScope(packageName);

const plan = packageName.includes('premium') ? 'Premium' : 'Pro';
const licenseStatus = verifyLicense({
releaseInfo,
licenseKey,
acceptedScopes,
productScope,
});

const fullPackageName = `@mui/${packageName}`;
Expand Down
17 changes: 17 additions & 0 deletions packages/x-license/src/utils/licenseScope.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
export const LICENSE_SCOPES = ['pro', 'premium'] as const;
export const PRODUCT_SCOPES = ['data-grid', 'date-pickers', 'charts', 'tree-view', null] as const;
michelengelen marked this conversation as resolved.
Show resolved Hide resolved
export const PLAN_VERSIONS = ['legacy', 'Q3-2024'] as const;
michelengelen marked this conversation as resolved.
Show resolved Hide resolved

export type LicenseScope = (typeof LICENSE_SCOPES)[number];
export type ProductScope = (typeof PRODUCT_SCOPES)[number];
export type PlanVersion = (typeof PLAN_VERSIONS)[number];

export const extractProductScope = (packageName: string): ProductScope => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A small detail, default function convention we use is function for top level scope and arrow function for lower scope; applied here would be:

Suggested change
export const extractProductScope = (packageName: string): ProductScope => {
export function extractProductScope(packageName: string): ProductScope {

// extract the part between "x-" and "-pro"/"-premium"
const regex = /x-(.*?)(-pro|-premium)?$/;
const match = packageName.match(regex);
return match ? (match[1] as ProductScope) : null;
};

export const extractAcceptedScopes = (packageName: string): readonly LicenseScope[] => {
return packageName.includes('premium')
? LICENSE_SCOPES.filter((scope) => scope.includes('premium'))
: LICENSE_SCOPES;
};
8 changes: 8 additions & 0 deletions packages/x-license/src/verifyLicense/verifyLicense.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ describe('License: verifyLicense', () => {
scope: 'pro',
licensingModel: 'perpetual',
orderNumber: 'MUI-123',
planVersion: 'legacy',
});

expect(
Expand Down Expand Up @@ -83,13 +84,15 @@ describe('License: verifyLicense', () => {
orderNumber: 'MUI-123',
scope: 'pro',
licensingModel: 'subscription',
planVersion: 'legacy',
});

const licenseKeyPremium = generateLicense({
expiryDate: new Date(releaseDate.getTime() + oneDayInMS),
orderNumber: 'MUI-123',
scope: 'premium',
licensingModel: 'subscription',
planVersion: 'legacy',
});

it('should log an error when ReleaseInfo is not valid', () => {
Expand Down Expand Up @@ -147,6 +150,7 @@ describe('License: verifyLicense', () => {
orderNumber: 'MUI-123',
scope: 'pro',
licensingModel: 'subscription',
planVersion: 'legacy',
});

expect(
Expand All @@ -164,6 +168,7 @@ describe('License: verifyLicense', () => {
orderNumber: 'MUI-123',
scope: 'pro',
licensingModel: 'subscription',
planVersion: 'legacy',
});

expect(
Expand All @@ -182,6 +187,7 @@ describe('License: verifyLicense', () => {
orderNumber: 'MUI-123',
scope: 'pro',
licensingModel: 'subscription',
planVersion: 'legacy',
});

expect(
Expand All @@ -199,6 +205,7 @@ describe('License: verifyLicense', () => {
orderNumber: 'MUI-123',
scope: 'pro',
licensingModel: 'perpetual',
planVersion: 'legacy',
});

expect(
Expand Down Expand Up @@ -230,6 +237,7 @@ describe('License: verifyLicense', () => {
orderNumber: 'MUI-123',
scope: 'pro',
licensingModel: 'annual',
planVersion: 'legacy',
});

it('should accept licensingModel="annual"', () => {
Expand Down
22 changes: 19 additions & 3 deletions packages/x-license/src/verifyLicense/verifyLicense.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { base64Decode, base64Encode } from '../encoding/base64';
import { md5 } from '../encoding/md5';
import { LICENSE_STATUS, LicenseStatus } from '../utils/licenseStatus';
import { LicenseScope, LICENSE_SCOPES } from '../utils/licenseScope';
import { LicenseScope, LICENSE_SCOPES, ProductScope, PlanVersion } from '../utils/licenseScope';
import { LicensingModel, LICENSING_MODELS } from '../utils/licensingModel';

const getDefaultReleaseDate = () => {
Expand All @@ -21,6 +21,7 @@ interface MuiLicense {
licensingModel: LicensingModel | null;
scope: LicenseScope | null;
expiryTimestamp: number | null;
planVersion?: PlanVersion | null;
}

/**
Expand All @@ -45,7 +46,7 @@ const decodeLicenseVersion1 = (license: string): MuiLicense => {
};

/**
* Format: O=${orderNumber},E=${expiryTimestamp},S=${scope},LM=${licensingModel},KV=2`;
* Format: O=${orderNumber},E=${expiryTimestamp},S=${scope},LM=${licensingModel},PV=${planVersion},KV=2`;
*/
const decodeLicenseVersion2 = (license: string): MuiLicense => {
const licenseInfo: MuiLicense = {
Expand Down Expand Up @@ -73,6 +74,10 @@ const decodeLicenseVersion2 = (license: string): MuiLicense => {
licenseInfo.expiryTimestamp = expiryTimestamp;
}
}

if (key === 'PV') {
licenseInfo.planVersion = value as PlanVersion;
}
});

return licenseInfo;
Expand All @@ -99,10 +104,12 @@ export function verifyLicense({
releaseInfo,
licenseKey,
acceptedScopes,
productScope = null,
michelengelen marked this conversation as resolved.
Show resolved Hide resolved
}: {
releaseInfo: string;
licenseKey: string | undefined;
acceptedScopes: LicenseScope[];
acceptedScopes: readonly LicenseScope[];
productScope?: ProductScope;
}): { status: LicenseStatus; meta?: any } {
if (!releaseInfo) {
throw new Error('MUI X: The release information is missing. Not able to validate license.');
Expand Down Expand Up @@ -169,6 +176,15 @@ export function verifyLicense({
return { status: LICENSE_STATUS.Invalid };
}

// 'legacy' is only available for licenses ordered between 2024-06-20 and 2024-07-20
if (!license.planVersion || license.planVersion === 'legacy') {
michelengelen marked this conversation as resolved.
Show resolved Hide resolved
// check if the productScope is 'charts' or 'tree-view'
if (productScope === 'charts' || productScope === 'tree-view') {
flaviendelangle marked this conversation as resolved.
Show resolved Hide resolved
console.error('Error checking license. Plan version not found or invalid!');
return { status: LICENSE_STATUS.OutOfScope };
}
}

if (!acceptedScopes.includes(license.scope)) {
return { status: LICENSE_STATUS.OutOfScope };
}
Expand Down