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

[license] Allow usage of charts an tree view pro package for old premium licenses #13619

Merged
merged 7 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
3 changes: 2 additions & 1 deletion packages/x-license/src/Watermark/Watermark.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react';
import { MuiCommercialPackageName, useLicenseVerifier } from '../useLicenseVerifier';
import { useLicenseVerifier } from '../useLicenseVerifier';
import { LICENSE_STATUS, LicenseStatus } from '../utils/licenseStatus';
import { MuiCommercialPackageName } from '../utils/commercialPackages';

function getLicenseErrorMessage(licenseStatus: LicenseStatus) {
switch (licenseStatus) {
Expand Down
1 change: 0 additions & 1 deletion packages/x-license/src/useLicenseVerifier/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export { useLicenseVerifier } from './useLicenseVerifier';
export type { MuiCommercialPackageName } from './useLicenseVerifier';
15 changes: 2 additions & 13 deletions packages/x-license/src/useLicenseVerifier/useLicenseVerifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,8 @@ import {
showNotAvailableInInitialProPlanError,
} from '../utils/licenseErrorMessageUtils';
import { LICENSE_STATUS, LicenseStatus } from '../utils/licenseStatus';
import { extractAcceptedScopes, extractProductScope } from '../utils/licenseScope';
import MuiLicenseInfoContext from '../Unstable_LicenseInfoProvider/MuiLicenseInfoContext';

export type MuiCommercialPackageName =
| 'x-data-grid-pro'
| 'x-data-grid-premium'
| 'x-date-pickers-pro'
| 'x-tree-view-pro'
| 'x-charts-pro';
import { MuiCommercialPackageName } from '../utils/commercialPackages';

export const sharedLicenseStatuses: {
[packageName in MuiCommercialPackageName]?: {
Expand Down Expand Up @@ -48,15 +41,11 @@ export function useLicenseVerifier(
return sharedLicenseStatuses[packageName]!.licenseVerifier;
}

const acceptedScopes = extractAcceptedScopes(packageName);
const productScope = extractProductScope(packageName);

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

const fullPackageName = `@mui/${packageName}`;
Expand Down
6 changes: 6 additions & 0 deletions packages/x-license/src/utils/commercialPackages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type MuiCommercialPackageName =
| 'x-data-grid-pro'
| 'x-data-grid-premium'
| 'x-date-pickers-pro'
| 'x-tree-view-pro'
| 'x-charts-pro';
1 change: 1 addition & 0 deletions packages/x-license/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './licenseInfo';
export * from './licenseStatus';
export type { LicenseScope } from './licenseScope';
export type { LicensingModel } from './licensingModel';
export type { MuiCommercialPackageName } from './commercialPackages';
15 changes: 0 additions & 15 deletions packages/x-license/src/utils/licenseScope.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
export const LICENSE_SCOPES = ['pro', 'premium'] as const;
export const PRODUCT_SCOPES = ['data-grid', 'date-pickers', 'charts', 'tree-view'] as const;
export const PLAN_VERSIONS = ['initial', 'Q3-2024'] as const;

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 => {
// extract the part between "x-" and "-pro"/"-premium"
const regex = /x-(.*?)(-pro|-premium)?$/;
const match = packageName.match(regex);
return match![1] as ProductScope;
};

export const extractAcceptedScopes = (packageName: string): readonly LicenseScope[] => {
return packageName.includes('premium')
? LICENSE_SCOPES.filter((scope) => scope.includes('premium'))
: LICENSE_SCOPES;
};
54 changes: 18 additions & 36 deletions packages/x-license/src/verifyLicense/verifyLicense.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ describe('License: verifyLicense', () => {
verifyLicense({
releaseInfo: '__RELEASE_INFO__',
licenseKey,
acceptedScopes: ['pro', 'premium'],
productScope: 'data-grid',
packageName: 'x-data-grid-pro',
}).status,
).to.throw('MUI X: The release information is invalid. Not able to validate license.');
});
Expand All @@ -42,8 +41,7 @@ describe('License: verifyLicense', () => {
verifyLicense({
releaseInfo: RELEASE_INFO,
licenseKey,
acceptedScopes: ['pro', 'premium'],
productScope: 'data-grid',
packageName: 'x-data-grid-pro',
}).status,
).to.equal(LICENSE_STATUS.Valid);
});
Expand All @@ -62,8 +60,7 @@ describe('License: verifyLicense', () => {
verifyLicense({
releaseInfo: RELEASE_INFO,
licenseKey: expiredLicenseKey,
acceptedScopes: ['pro', 'premium'],
productScope: 'data-grid',
packageName: 'x-data-grid-pro',
}).status,
).to.equal(LICENSE_STATUS.ExpiredVersion);
});
Expand All @@ -75,8 +72,7 @@ describe('License: verifyLicense', () => {
releaseInfo: RELEASE_INFO,
licenseKey:
'b43ff5f9ac93f021855ff59ff0ba5220TkFNRTpNYC1VSSBTQVMsREVWRUxPUEVSX0NPVU5UPTEwLEVYUElSWT0xNTkxNzIzMDY3MDQyLFZFUlNJT049MS4yLjM',
acceptedScopes: ['pro', 'premium'],
productScope: 'data-grid',
packageName: 'x-data-grid-pro',
}).status,
).to.equal(LICENSE_STATUS.Invalid);
});
Expand Down Expand Up @@ -106,8 +102,7 @@ describe('License: verifyLicense', () => {
verifyLicense({
releaseInfo: '__RELEASE_INFO__',
licenseKey: licenseKeyPro,
acceptedScopes: ['pro', 'premium'],
productScope: 'data-grid',
packageName: 'x-data-grid-pro',
}).status,
).to.throw('MUI X: The release information is invalid. Not able to validate license.');
});
Expand All @@ -119,8 +114,7 @@ describe('License: verifyLicense', () => {
verifyLicense({
releaseInfo: RELEASE_INFO,
licenseKey: licenseKeyPro,
acceptedScopes: ['pro', 'premium'],
productScope: 'data-grid',
packageName: 'x-data-grid-pro',
}).status,
).to.equal(LICENSE_STATUS.Valid);
});
Expand All @@ -131,8 +125,7 @@ describe('License: verifyLicense', () => {
verifyLicense({
releaseInfo: RELEASE_INFO,
licenseKey: licenseKeyPremium,
acceptedScopes: ['premium'],
productScope: 'data-grid',
packageName: 'x-data-grid-premium',
}).status,
).to.equal(LICENSE_STATUS.Valid);
});
Expand All @@ -143,8 +136,7 @@ describe('License: verifyLicense', () => {
verifyLicense({
releaseInfo: RELEASE_INFO,
licenseKey: licenseKeyPro,
acceptedScopes: ['premium'],
productScope: 'data-grid',
packageName: 'x-data-grid-premium',
}).status,
).to.equal(LICENSE_STATUS.OutOfScope);
});
Expand All @@ -165,8 +157,7 @@ describe('License: verifyLicense', () => {
verifyLicense({
releaseInfo: RELEASE_INFO,
licenseKey: expiredLicenseKey,
acceptedScopes: ['pro', 'premium'],
productScope: 'data-grid',
packageName: 'x-data-grid-pro',
}).status,
).to.equal(LICENSE_STATUS.Valid);
});
Expand All @@ -184,8 +175,7 @@ describe('License: verifyLicense', () => {
verifyLicense({
releaseInfo: RELEASE_INFO,
licenseKey: expiredLicenseKey,
acceptedScopes: ['pro', 'premium'],
productScope: 'data-grid',
packageName: 'x-data-grid-pro',
}).status,
).to.equal(LICENSE_STATUS.ExpiredAnnualGrace);
});
Expand All @@ -204,8 +194,7 @@ describe('License: verifyLicense', () => {
verifyLicense({
releaseInfo: RELEASE_INFO,
licenseKey: expiredLicenseKey,
acceptedScopes: ['pro', 'premium'],
productScope: 'data-grid',
packageName: 'x-data-grid-pro',
}).status,
).to.equal(LICENSE_STATUS.ExpiredAnnual);
});
Expand All @@ -223,8 +212,7 @@ describe('License: verifyLicense', () => {
verifyLicense({
releaseInfo: RELEASE_INFO,
licenseKey: expiredLicenseKey,
acceptedScopes: ['pro', 'premium'],
productScope: 'data-grid',
packageName: 'x-data-grid-pro',
}).status,
).to.equal(LICENSE_STATUS.Valid);
});
Expand All @@ -237,8 +225,7 @@ describe('License: verifyLicense', () => {
releaseInfo: RELEASE_INFO,
licenseKey:
'b43ff5f9ac93f021855ff59ff0ba5220TkFNRTpNYC1VSSBTQVMsREVWRUxPUEVSX0NPVU5UPTEwLEVYUElSWT0xNTkxNzIzMDY3MDQyLFZFUlNJT049MS4yLjM',
acceptedScopes: ['pro', 'premium'],
productScope: 'data-grid',
packageName: 'x-data-grid-pro',
}).status,
).to.equal(LICENSE_STATUS.Invalid);
});
Expand All @@ -259,8 +246,7 @@ describe('License: verifyLicense', () => {
verifyLicense({
releaseInfo: RELEASE_INFO,
licenseKey: licenseKeyPro,
acceptedScopes: ['pro', 'premium'],
productScope: 'data-grid',
packageName: 'x-data-grid-pro',
}).status,
).to.equal(LICENSE_STATUS.Valid);
});
Expand Down Expand Up @@ -289,8 +275,7 @@ describe('License: verifyLicense', () => {
verifyLicense({
releaseInfo: RELEASE_INFO,
licenseKey: licenseKeyInitial,
acceptedScopes: ['pro', 'premium'],
productScope: 'charts',
packageName: 'x-charts-pro',
}).status,
).to.equal(LICENSE_STATUS.NotAvailableInInitialProPlan);
});
Expand All @@ -301,8 +286,7 @@ describe('License: verifyLicense', () => {
verifyLicense({
releaseInfo: RELEASE_INFO,
licenseKey: licenseKeyInitial,
acceptedScopes: ['pro', 'premium'],
productScope: 'tree-view',
packageName: 'x-tree-view-pro',
}).status,
).to.equal(LICENSE_STATUS.NotAvailableInInitialProPlan);
});
Expand All @@ -313,8 +297,7 @@ describe('License: verifyLicense', () => {
verifyLicense({
releaseInfo: RELEASE_INFO,
licenseKey: licenseKey2,
acceptedScopes: ['pro', 'premium'],
productScope: 'charts',
packageName: 'x-charts-pro',
}).status,
).to.equal(LICENSE_STATUS.Valid);
});
Expand All @@ -325,8 +308,7 @@ describe('License: verifyLicense', () => {
verifyLicense({
releaseInfo: RELEASE_INFO,
licenseKey: licenseKey2,
acceptedScopes: ['pro', 'premium'],
productScope: 'tree-view',
packageName: 'x-tree-view-pro',
}).status,
).to.equal(LICENSE_STATUS.Valid);
});
Expand Down
46 changes: 34 additions & 12 deletions packages/x-license/src/verifyLicense/verifyLicense.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { base64Decode, base64Encode } from '../encoding/base64';
import { md5 } from '../encoding/md5';
import { LICENSE_STATUS, LicenseStatus } from '../utils/licenseStatus';
import { LicenseScope, LICENSE_SCOPES, ProductScope, PlanVersion } from '../utils/licenseScope';
import { LicenseScope, LICENSE_SCOPES, PlanVersion } from '../utils/licenseScope';
import { LicensingModel, LICENSING_MODELS } from '../utils/licensingModel';
import { MuiCommercialPackageName } from '../utils/commercialPackages';

const getDefaultReleaseDate = () => {
const today = new Date();
Expand All @@ -15,6 +16,22 @@ export function generateReleaseInfo(releaseDate = getDefaultReleaseDate()) {
return base64Encode(releaseDate.getTime().toString());
}

function isLicenseScopeSufficient(
packageName: MuiCommercialPackageName,
licenseScope: LicenseScope,
) {
let acceptedScopes: LicenseScope[];
if (packageName.includes('pro')) {
acceptedScopes = ['pro', 'premium'];
} else if (packageName.includes('premium')) {
flaviendelangle marked this conversation as resolved.
Show resolved Hide resolved
acceptedScopes = ['premium'];
} else {
acceptedScopes = [];
}

return acceptedScopes.includes(licenseScope);
}

const expiryReg = /^.*EXPIRY=([0-9]+),.*$/;

interface MuiLicense {
Expand All @@ -24,6 +41,11 @@ interface MuiLicense {
planVersion: PlanVersion;
}

const PRO_PACKAGES_AVAILABLE_IN_INITIAL_PRO_PLAN: MuiCommercialPackageName[] = [
'x-data-grid-pro',
'x-date-pickers-pro',
];

/**
* Format: ORDER:${orderNumber},EXPIRY=${expiryTimestamp},KEYVERSION=1
*/
Expand Down Expand Up @@ -105,13 +127,11 @@ const decodeLicense = (encodedLicense: string): MuiLicense | null => {
export function verifyLicense({
releaseInfo,
licenseKey,
acceptedScopes,
productScope,
packageName,
}: {
releaseInfo: string;
licenseKey?: string;
acceptedScopes: readonly LicenseScope[];
productScope: ProductScope;
packageName: MuiCommercialPackageName;
}): { 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 @@ -178,15 +198,17 @@ export function verifyLicense({
return { status: LICENSE_STATUS.Invalid };
}

if (license.planVersion === 'initial') {
// 'charts-pro' or 'tree-view-pro' can only be used with a newer license
if (productScope === 'charts' || productScope === 'tree-view') {
return { status: LICENSE_STATUS.NotAvailableInInitialProPlan };
}
if (!isLicenseScopeSufficient(packageName, license.scope)) {
return { status: LICENSE_STATUS.OutOfScope };
}

if (!acceptedScopes.includes(license.scope)) {
return { status: LICENSE_STATUS.OutOfScope };
// 'charts-pro' or 'tree-view-pro' can only be used with a newer Pro license
Copy link
Member Author

Choose a reason for hiding this comment

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

The order change is here to make sure that a pro license used on a premium package always have the OutOfScope status and not the NotAvailableInInitialProPlan

if (
license.planVersion === 'initial' &&
license.scope === 'pro' &&
!PRO_PACKAGES_AVAILABLE_IN_INITIAL_PRO_PLAN.includes(packageName)
) {
return { status: LICENSE_STATUS.NotAvailableInInitialProPlan };
}

return { status: LICENSE_STATUS.Valid };
Expand Down