@moonbase.sh/licensing 3.2.2 → 3.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +152 -4
- package/dist/index.cjs +383 -10
- package/dist/index.d.cts +1151 -8
- package/dist/index.d.ts +1151 -8
- package/dist/index.js +379 -10
- package/package.json +3 -2
- package/release-access-vectors.json +150 -0
package/dist/index.js
CHANGED
|
@@ -6,7 +6,7 @@ import process3 from "process";
|
|
|
6
6
|
import fetch from "cross-fetch";
|
|
7
7
|
|
|
8
8
|
// package.json
|
|
9
|
-
var version = "3.
|
|
9
|
+
var version = "3.3.0";
|
|
10
10
|
|
|
11
11
|
// src/errors.ts
|
|
12
12
|
var ErrorType = /* @__PURE__ */ ((ErrorType2) => {
|
|
@@ -20,6 +20,7 @@ var ErrorType = /* @__PURE__ */ ((ErrorType2) => {
|
|
|
20
20
|
ErrorType2["LicenseDeviceMismatch"] = "LicenseDeviceMismatch";
|
|
21
21
|
ErrorType2["DeviceIdentityUnavailable"] = "DeviceIdentityUnavailable";
|
|
22
22
|
ErrorType2["StoreClosed"] = "StoreClosed";
|
|
23
|
+
ErrorType2["ReleaseNotCovered"] = "ReleaseNotCovered";
|
|
23
24
|
return ErrorType2;
|
|
24
25
|
})(ErrorType || {});
|
|
25
26
|
var MoonbaseError = class extends Error {
|
|
@@ -44,6 +45,17 @@ var InsufficientDeviceIdentityError = class extends MoonbaseError {
|
|
|
44
45
|
this.name = "InsufficientDeviceIdentityError";
|
|
45
46
|
}
|
|
46
47
|
};
|
|
48
|
+
var ReleaseNotCoveredError = class extends MoonbaseError {
|
|
49
|
+
constructor(title, detail, details = {}) {
|
|
50
|
+
var _a, _b;
|
|
51
|
+
super(title, detail, "ReleaseNotCovered" /* ReleaseNotCovered */);
|
|
52
|
+
this.name = "ReleaseNotCoveredError";
|
|
53
|
+
this.license = details.license;
|
|
54
|
+
this.appVersion = details.appVersion;
|
|
55
|
+
this.upgradeUrl = details.upgradeUrl;
|
|
56
|
+
this.releaseAccess = (_b = details.releaseAccess) != null ? _b : (_a = details.license) == null ? void 0 : _a.releaseAccess;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
47
59
|
|
|
48
60
|
// src/schemas.ts
|
|
49
61
|
import { z } from "zod";
|
|
@@ -78,20 +90,168 @@ var userSchema = z.object({
|
|
|
78
90
|
email: z.string(),
|
|
79
91
|
properties: z.record(z.unknown()).nullish()
|
|
80
92
|
});
|
|
93
|
+
var subProductSchema = z.object({
|
|
94
|
+
/** The sub-product's own part of its product ID: `addon` for `product.addon`. */
|
|
95
|
+
id: z.string(),
|
|
96
|
+
/**
|
|
97
|
+
* Whether it may be unlocked on this device, as of when the license was
|
|
98
|
+
* issued: the device holds a seat of it, or its trial has not run out. A
|
|
99
|
+
* sub-product the customer owns, but with every seat taken elsewhere, is
|
|
100
|
+
* listed and not active.
|
|
101
|
+
*/
|
|
102
|
+
active: z.boolean(),
|
|
103
|
+
/** Whether a trial backs it rather than a license the customer owns. */
|
|
104
|
+
trial: z.boolean(),
|
|
105
|
+
/**
|
|
106
|
+
* Its seat on this device was revoked, by the customer or the merchant, so
|
|
107
|
+
* validating does not take it back: offer to activate it again with
|
|
108
|
+
* `client.activateSubProduct`.
|
|
109
|
+
*/
|
|
110
|
+
revoked: z.boolean().default(false),
|
|
111
|
+
/** When it ends. Absent when it never does. */
|
|
112
|
+
expiresAt: z.coerce.date().nullish(),
|
|
113
|
+
/** Seats across the customer's licenses for it. Absent in offline licenses and for trials. */
|
|
114
|
+
seats: z.object({
|
|
115
|
+
total: z.number(),
|
|
116
|
+
used: z.number(),
|
|
117
|
+
remaining: z.number()
|
|
118
|
+
}).nullish()
|
|
119
|
+
});
|
|
120
|
+
var subProductPricingSchema = z.object({
|
|
121
|
+
id: z.string(),
|
|
122
|
+
name: z.string(),
|
|
123
|
+
/** What it costs before discounts, by currency code. */
|
|
124
|
+
originalPrice: z.record(z.number()),
|
|
125
|
+
/** What it costs now, by currency code, after any discount. */
|
|
126
|
+
price: z.record(z.number()),
|
|
127
|
+
hasDiscount: z.boolean(),
|
|
128
|
+
/** `OneOff`, or `Recurring` for a subscription, with how often it renews. */
|
|
129
|
+
recurrence: z.object({
|
|
130
|
+
type: z.string(),
|
|
131
|
+
cycleLength: z.string().nullish()
|
|
132
|
+
}).nullish()
|
|
133
|
+
});
|
|
134
|
+
var subProductCatalogEntrySchema = z.object({
|
|
135
|
+
/** The sub-product's own part of its product ID, as {@link License.subProducts} names it. */
|
|
136
|
+
id: z.string(),
|
|
137
|
+
/** The sub-product, with its prices when it is for sale, and whether the customer has it. */
|
|
138
|
+
product: z.object({
|
|
139
|
+
/** The sub-product's whole product ID. */
|
|
140
|
+
id: z.string(),
|
|
141
|
+
name: z.string(),
|
|
142
|
+
tagline: z.string().nullish(),
|
|
143
|
+
description: z.string().nullish(),
|
|
144
|
+
website: z.string().nullish(),
|
|
145
|
+
iconUrl: z.string().nullish(),
|
|
146
|
+
owned: z.boolean(),
|
|
147
|
+
subscribed: z.boolean(),
|
|
148
|
+
/** The price to show first. Absent when it is not for sale. */
|
|
149
|
+
defaultVariation: subProductPricingSchema.nullish(),
|
|
150
|
+
/** Every way it is sold. Absent when it is not for sale. */
|
|
151
|
+
variations: z.array(subProductPricingSchema).nullish()
|
|
152
|
+
}),
|
|
153
|
+
/** Whether it is included in the product's license, so the license reports it. */
|
|
154
|
+
includedInLicense: z.boolean(),
|
|
155
|
+
/**
|
|
156
|
+
* Whether to unlock it on this device. For a sub-product included in the
|
|
157
|
+
* license, as {@link hasSubProduct} would say. For any other, whether the
|
|
158
|
+
* customer owns or subscribes to it.
|
|
159
|
+
*/
|
|
160
|
+
active: z.boolean(),
|
|
161
|
+
/**
|
|
162
|
+
* Its seat on this device was revoked, so validating does not take it back:
|
|
163
|
+
* offer to activate it again with `client.activateSubProduct`.
|
|
164
|
+
*/
|
|
165
|
+
revoked: z.boolean().catch(false),
|
|
166
|
+
/** When the license or trial behind it ends, for one included in the license. */
|
|
167
|
+
expiresAt: z.coerce.date().nullish(),
|
|
168
|
+
/** Seats across the customer's licenses for it, for one included in the license that they own. */
|
|
169
|
+
seats: z.object({
|
|
170
|
+
total: z.number(),
|
|
171
|
+
used: z.number(),
|
|
172
|
+
remaining: z.number()
|
|
173
|
+
}).nullish(),
|
|
174
|
+
/** Its trial on this device, or whether one can be started. Absent when it offers none. */
|
|
175
|
+
trial: z.object({
|
|
176
|
+
/**
|
|
177
|
+
* `Available`: `requestSubProductTrial` can start one now. `Active`: the
|
|
178
|
+
* device's trial is running. `Expired`: it has ended. `Unavailable`: not
|
|
179
|
+
* for this device now (not included in the license, owned, a trial or
|
|
180
|
+
* offline license, or a newsletter opt-in is missing), which is also what
|
|
181
|
+
* a status this version does not know reads as.
|
|
182
|
+
*/
|
|
183
|
+
status: z.enum(["Available", "Unavailable", "Active", "Expired"]).catch("Unavailable"),
|
|
184
|
+
/** How long a trial lasts. Absent when its trials never expire. */
|
|
185
|
+
numberOfDays: z.number().nullish(),
|
|
186
|
+
/** When the device's trial ends. Absent when it has none, or one that never expires. */
|
|
187
|
+
expiresAt: z.coerce.date().nullish(),
|
|
188
|
+
/** The trial needs the customer to opt in to the newsletter, which they have not. */
|
|
189
|
+
requiresNewsletterOptIn: z.boolean().catch(false)
|
|
190
|
+
}).nullish(),
|
|
191
|
+
/** Where the customer buys it, when it is for sale. Add `?variation={id}` for one of its variations. */
|
|
192
|
+
buyUrl: z.string().nullish()
|
|
193
|
+
});
|
|
194
|
+
var subProductCatalogSchema = z.object({
|
|
195
|
+
/** The currency to show prices in, as the store suggests it for the customer. */
|
|
196
|
+
currency: z.string(),
|
|
197
|
+
/**
|
|
198
|
+
* The sub-products the customer can do something with: those they own or
|
|
199
|
+
* subscribe to, those on sale, and those whose trial can be started or is
|
|
200
|
+
* running on this device.
|
|
201
|
+
*/
|
|
202
|
+
subProducts: z.array(subProductCatalogEntrySchema)
|
|
203
|
+
});
|
|
204
|
+
var releaseAccessSchema = z.object({
|
|
205
|
+
/**
|
|
206
|
+
* Releases published until then are covered, and every version while it has
|
|
207
|
+
* not passed: "12 months of updates".
|
|
208
|
+
*/
|
|
209
|
+
updatesUntil: z.coerce.date().optional(),
|
|
210
|
+
/** Every version whose major is at most this is covered: "up to 2.x". */
|
|
211
|
+
upToMajor: z.number().optional(),
|
|
212
|
+
/**
|
|
213
|
+
* Once `updatesUntil` has passed, the last version published before it
|
|
214
|
+
* (`MAJOR.MINOR.PATCH`), which is covered along with every older one. Absent
|
|
215
|
+
* while updates last, and when nothing was published before they ended.
|
|
216
|
+
*/
|
|
217
|
+
upToVersion: z.string().optional()
|
|
218
|
+
});
|
|
81
219
|
var licenseSchema = z.object({
|
|
82
220
|
id: z.string(),
|
|
83
221
|
trial: z.boolean(),
|
|
84
222
|
activationMethod: z.nativeEnum(ActivationMethod),
|
|
85
223
|
product: productSchema,
|
|
86
224
|
ownedSubProductIds: z.array(z.string()).default([]),
|
|
225
|
+
/**
|
|
226
|
+
* The sub-products included in the product's license that the customer owns
|
|
227
|
+
* or trials, each with whether it may be unlocked on this device. Empty for a
|
|
228
|
+
* trial license and for products without such sub-products.
|
|
229
|
+
*/
|
|
230
|
+
subProducts: z.array(subProductSchema).optional(),
|
|
87
231
|
subscriptionId: z.string().nullish(),
|
|
88
232
|
issuedAt: z.coerce.date(),
|
|
89
233
|
issuedTo: userSchema,
|
|
90
234
|
expiresAt: z.coerce.date().nullish(),
|
|
91
235
|
validatedAt: z.coerce.date(),
|
|
236
|
+
/** Which versions of the app the license covers. Absent when it covers every version. */
|
|
237
|
+
releaseAccess: releaseAccessSchema.optional(),
|
|
92
238
|
properties: z.record(z.unknown()).nullish(),
|
|
93
239
|
token: z.string()
|
|
94
240
|
});
|
|
241
|
+
var apiDateSchema = z.string().transform((value) => new Date(/T[^Z+-]*$/i.test(value) ? `${value}Z` : value)).pipe(z.date());
|
|
242
|
+
var releaseNotCoveredProblemSchema = z.object({
|
|
243
|
+
/** On validation, the license freshly issued, for the app to keep. */
|
|
244
|
+
license: z.string().nullish().catch(void 0),
|
|
245
|
+
/** The version the app said it runs, as Moonbase read it. */
|
|
246
|
+
appVersion: z.string().nullish().catch(void 0),
|
|
247
|
+
/** Where the customer buys the upgrade. */
|
|
248
|
+
upgradeUrl: z.string().nullish().catch(void 0),
|
|
249
|
+
releaseAccess: z.object({
|
|
250
|
+
updatesUntil: apiDateSchema.nullish().catch(void 0),
|
|
251
|
+
upToMajor: z.number().nullish().catch(void 0),
|
|
252
|
+
upToVersion: z.string().nullish().catch(void 0)
|
|
253
|
+
}).nullish().catch(void 0)
|
|
254
|
+
});
|
|
95
255
|
|
|
96
256
|
// src/client.ts
|
|
97
257
|
var defaultFetchOptions = {
|
|
@@ -120,7 +280,7 @@ var LicenseClient = class {
|
|
|
120
280
|
body: JSON.stringify(content)
|
|
121
281
|
});
|
|
122
282
|
if (response.status >= 400) {
|
|
123
|
-
throw await errorFromResponse(response);
|
|
283
|
+
throw await errorFromResponse(response, this.licenseValidator);
|
|
124
284
|
}
|
|
125
285
|
try {
|
|
126
286
|
return activationRequestResponseSchema.parse(await response.json());
|
|
@@ -156,6 +316,55 @@ var LicenseClient = class {
|
|
|
156
316
|
});
|
|
157
317
|
return await this.handleLicenseResponse(response);
|
|
158
318
|
}
|
|
319
|
+
async activateSubProduct(license, subProductId) {
|
|
320
|
+
const response = await fetch(`${this.configuration.endpoint}/api/client/licenses/${this.configuration.productId}/sub-products/${encodeURIComponent(subProductId)}/activate${this.buildQueryString()}`, {
|
|
321
|
+
...defaultFetchOptions,
|
|
322
|
+
method: "POST",
|
|
323
|
+
headers: {
|
|
324
|
+
...defaultFetchOptions.headers,
|
|
325
|
+
"Content-Type": "text/plain"
|
|
326
|
+
},
|
|
327
|
+
body: license.token
|
|
328
|
+
});
|
|
329
|
+
return await this.handleLicenseResponse(response);
|
|
330
|
+
}
|
|
331
|
+
async requestSubProductTrial(license, subProductId) {
|
|
332
|
+
const response = await fetch(`${this.configuration.endpoint}/api/client/licenses/${this.configuration.productId}/sub-products/${encodeURIComponent(subProductId)}/trial${this.buildQueryString()}`, {
|
|
333
|
+
...defaultFetchOptions,
|
|
334
|
+
method: "POST",
|
|
335
|
+
headers: {
|
|
336
|
+
...defaultFetchOptions.headers,
|
|
337
|
+
"Content-Type": "text/plain"
|
|
338
|
+
},
|
|
339
|
+
body: license.token
|
|
340
|
+
});
|
|
341
|
+
return await this.handleLicenseResponse(response);
|
|
342
|
+
}
|
|
343
|
+
async listSubProducts(license) {
|
|
344
|
+
const response = await fetch(`${this.configuration.endpoint}/api/client/licenses/${this.configuration.productId}/sub-products`, {
|
|
345
|
+
...defaultFetchOptions,
|
|
346
|
+
method: "POST",
|
|
347
|
+
headers: {
|
|
348
|
+
...defaultFetchOptions.headers,
|
|
349
|
+
"Content-Type": "text/plain"
|
|
350
|
+
},
|
|
351
|
+
body: license.token
|
|
352
|
+
});
|
|
353
|
+
if (response.status >= 400) {
|
|
354
|
+
throw await errorFromResponse(response, this.licenseValidator);
|
|
355
|
+
}
|
|
356
|
+
try {
|
|
357
|
+
return subProductCatalogSchema.parse(await response.json());
|
|
358
|
+
} catch (err) {
|
|
359
|
+
const error = err;
|
|
360
|
+
throw new MoonbaseError(
|
|
361
|
+
"Could not list sub-products",
|
|
362
|
+
error.message,
|
|
363
|
+
"ApiError" /* ApiError */,
|
|
364
|
+
error
|
|
365
|
+
);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
159
368
|
async validateLicense(license) {
|
|
160
369
|
const response = await fetch(`${this.configuration.endpoint}/api/client/licenses/${this.configuration.productId}/validate${this.buildQueryString()}`, {
|
|
161
370
|
...defaultFetchOptions,
|
|
@@ -169,9 +378,10 @@ var LicenseClient = class {
|
|
|
169
378
|
return await this.handleLicenseResponse(response);
|
|
170
379
|
}
|
|
171
380
|
async validateRawLicense(rawLicense) {
|
|
172
|
-
const
|
|
381
|
+
const token = rawLicense.toString("utf8");
|
|
382
|
+
const license = await this.licenseValidator.validateLicense(token, { enforceReleaseAccess: false });
|
|
173
383
|
if (license.activationMethod === "Offline" /* Offline */)
|
|
174
|
-
return
|
|
384
|
+
return await this.licenseValidator.validateLicense(token);
|
|
175
385
|
return await this.validateLicense(license);
|
|
176
386
|
}
|
|
177
387
|
async revokeLicense(license) {
|
|
@@ -185,12 +395,12 @@ var LicenseClient = class {
|
|
|
185
395
|
body: license.token
|
|
186
396
|
});
|
|
187
397
|
if (response.status >= 400) {
|
|
188
|
-
throw await errorFromResponse(response);
|
|
398
|
+
throw await errorFromResponse(response, this.licenseValidator);
|
|
189
399
|
}
|
|
190
400
|
}
|
|
191
401
|
async handleLicenseResponse(response) {
|
|
192
402
|
if (response.status >= 400) {
|
|
193
|
-
throw await errorFromResponse(response);
|
|
403
|
+
throw await errorFromResponse(response, this.licenseValidator);
|
|
194
404
|
}
|
|
195
405
|
return await this.licenseValidator.validateLicense(await response.text());
|
|
196
406
|
}
|
|
@@ -208,6 +418,8 @@ var LicenseClient = class {
|
|
|
208
418
|
parts.push(`meta[${encodeURIComponent(key)}]=${encodeURIComponent(value)}`);
|
|
209
419
|
}
|
|
210
420
|
}
|
|
421
|
+
if (this.configuration.autoActivateSubProducts === false)
|
|
422
|
+
parts.push("autoActivateSubProducts=false");
|
|
211
423
|
return `?${parts.join("&")}`;
|
|
212
424
|
}
|
|
213
425
|
};
|
|
@@ -217,9 +429,10 @@ var licenseErrorTypes = /* @__PURE__ */ new Set([
|
|
|
217
429
|
"LicenseRevoked" /* LicenseRevoked */,
|
|
218
430
|
"LicenseActivationRevoked" /* LicenseActivationRevoked */,
|
|
219
431
|
"LicenseExpired" /* LicenseExpired */,
|
|
220
|
-
"StoreClosed" /* StoreClosed
|
|
432
|
+
"StoreClosed" /* StoreClosed */,
|
|
433
|
+
"ReleaseNotCovered" /* ReleaseNotCovered */
|
|
221
434
|
]);
|
|
222
|
-
async function errorFromResponse(response) {
|
|
435
|
+
async function errorFromResponse(response, licenseValidator) {
|
|
223
436
|
const fallback = new MoonbaseError(
|
|
224
437
|
"Request not successful",
|
|
225
438
|
`The API responded with a ${response.status} ${response.statusText} response`,
|
|
@@ -235,12 +448,41 @@ async function errorFromResponse(response) {
|
|
|
235
448
|
if (!problem.success)
|
|
236
449
|
return fallback;
|
|
237
450
|
const { title, detail, errorType } = problem.data;
|
|
451
|
+
if (errorType === "ReleaseNotCovered" /* ReleaseNotCovered */)
|
|
452
|
+
return await releaseNotCoveredError(body, title != null ? title : "Version not covered", detail != null ? detail : fallback.detail, licenseValidator);
|
|
238
453
|
return new MoonbaseError(
|
|
239
454
|
title != null ? title : fallback.title,
|
|
240
455
|
detail != null ? detail : fallback.detail,
|
|
241
456
|
errorType && licenseErrorTypes.has(errorType) ? errorType : "ApiError" /* ApiError */
|
|
242
457
|
);
|
|
243
458
|
}
|
|
459
|
+
async function releaseNotCoveredError(body, title, detail, licenseValidator) {
|
|
460
|
+
const problem = releaseNotCoveredProblemSchema.safeParse(body);
|
|
461
|
+
const { license: token, appVersion, upgradeUrl, releaseAccess } = problem.success ? problem.data : {};
|
|
462
|
+
let license;
|
|
463
|
+
if (token) {
|
|
464
|
+
try {
|
|
465
|
+
license = await licenseValidator.validateLicense(token, { enforceReleaseAccess: false });
|
|
466
|
+
} catch (e) {
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
return new ReleaseNotCoveredError(title, detail, {
|
|
470
|
+
license,
|
|
471
|
+
appVersion: appVersion != null ? appVersion : void 0,
|
|
472
|
+
upgradeUrl: upgradeUrl != null ? upgradeUrl : void 0,
|
|
473
|
+
releaseAccess: releaseAccessFrom(releaseAccess)
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
function releaseAccessFrom(problem) {
|
|
477
|
+
const releaseAccess = {};
|
|
478
|
+
if (problem == null ? void 0 : problem.updatesUntil)
|
|
479
|
+
releaseAccess.updatesUntil = problem.updatesUntil;
|
|
480
|
+
if (typeof (problem == null ? void 0 : problem.upToMajor) === "number")
|
|
481
|
+
releaseAccess.upToMajor = problem.upToMajor;
|
|
482
|
+
if (problem == null ? void 0 : problem.upToVersion)
|
|
483
|
+
releaseAccess.upToVersion = problem.upToVersion;
|
|
484
|
+
return Object.keys(releaseAccess).length > 0 ? releaseAccess : void 0;
|
|
485
|
+
}
|
|
244
486
|
|
|
245
487
|
// src/deviceIdResolver.ts
|
|
246
488
|
import cp2 from "child_process";
|
|
@@ -751,6 +993,59 @@ var FileLicenseStore = class {
|
|
|
751
993
|
// src/validator.ts
|
|
752
994
|
import jwt from "jsonwebtoken";
|
|
753
995
|
import { z as z2 } from "zod";
|
|
996
|
+
|
|
997
|
+
// src/releaseAccess.ts
|
|
998
|
+
var maxVersionPart = 9999999;
|
|
999
|
+
function parseAppVersion(version2) {
|
|
1000
|
+
let text = version2 == null ? void 0 : version2.trim();
|
|
1001
|
+
if (!text)
|
|
1002
|
+
return null;
|
|
1003
|
+
if (text.startsWith("v") || text.startsWith("V"))
|
|
1004
|
+
text = text.slice(1);
|
|
1005
|
+
const suffix = text.search(/[-+ ]/);
|
|
1006
|
+
if (suffix >= 0)
|
|
1007
|
+
text = text.slice(0, suffix);
|
|
1008
|
+
const parts = text.split(".");
|
|
1009
|
+
if (parts.length < 2 || parts.length > 4)
|
|
1010
|
+
return null;
|
|
1011
|
+
const numbers = [0, 0, 0];
|
|
1012
|
+
for (const [index, part] of parts.entries()) {
|
|
1013
|
+
if (!/^\d+$/.test(part))
|
|
1014
|
+
return null;
|
|
1015
|
+
const number = Number(part);
|
|
1016
|
+
if (number > maxVersionPart)
|
|
1017
|
+
return null;
|
|
1018
|
+
if (index < 3)
|
|
1019
|
+
numbers[index] = number;
|
|
1020
|
+
}
|
|
1021
|
+
return { major: numbers[0], minor: numbers[1], patch: numbers[2] };
|
|
1022
|
+
}
|
|
1023
|
+
function compareVersions(a, b) {
|
|
1024
|
+
return a.major - b.major || a.minor - b.minor || a.patch - b.patch;
|
|
1025
|
+
}
|
|
1026
|
+
function coversVersion(license, version2, now = /* @__PURE__ */ new Date()) {
|
|
1027
|
+
var _a;
|
|
1028
|
+
const { updatesUntil, upToMajor, upToVersion } = (_a = license.releaseAccess) != null ? _a : {};
|
|
1029
|
+
if (updatesUntil === void 0 && upToMajor === void 0)
|
|
1030
|
+
return true;
|
|
1031
|
+
const appVersion = parseAppVersion(version2);
|
|
1032
|
+
if (!appVersion)
|
|
1033
|
+
return true;
|
|
1034
|
+
if (upToMajor !== void 0 && appVersion.major <= upToMajor)
|
|
1035
|
+
return true;
|
|
1036
|
+
if (updatesUntil === void 0)
|
|
1037
|
+
return false;
|
|
1038
|
+
if (now.getTime() <= updatesUntil.getTime())
|
|
1039
|
+
return true;
|
|
1040
|
+
const lastCovered = parseAppVersion(upToVersion);
|
|
1041
|
+
return !lastCovered || compareVersions(appVersion, lastCovered) <= 0;
|
|
1042
|
+
}
|
|
1043
|
+
|
|
1044
|
+
// src/validator.ts
|
|
1045
|
+
var integerClaimSchema = z2.union([
|
|
1046
|
+
z2.number().int(),
|
|
1047
|
+
z2.string().regex(/^-?\d+$/).transform(Number)
|
|
1048
|
+
]).nullish().catch(void 0);
|
|
754
1049
|
var licenseClaimsSchema = z2.object({
|
|
755
1050
|
["l:id" /* LicenseId */]: z2.string(),
|
|
756
1051
|
["trial" /* Trial */]: z2.string().or(z2.boolean()).transform((v) => v === true || v === "true").pipe(z2.boolean()),
|
|
@@ -763,6 +1058,7 @@ var licenseClaimsSchema = z2.object({
|
|
|
763
1058
|
["p:name" /* ProductName */]: z2.string(),
|
|
764
1059
|
["p:rel" /* ProductReleaseVersion */]: z2.string().nullish(),
|
|
765
1060
|
["sp:owned" /* OwnedSubProducts */]: z2.string().nullish().transform((v) => v ? v.split(",") : []),
|
|
1061
|
+
["sp" /* SubProducts */]: z2.array(z2.unknown()).nullish().transform(readSubProducts),
|
|
766
1062
|
["s:id" /* SubscriptionId */]: z2.string().nullish(),
|
|
767
1063
|
["u:id" /* UserId */]: z2.string(),
|
|
768
1064
|
["u:name" /* UserName */]: z2.string(),
|
|
@@ -770,14 +1066,18 @@ var licenseClaimsSchema = z2.object({
|
|
|
770
1066
|
["p:properties" /* ProductProperties */]: z2.record(z2.unknown()).nullish(),
|
|
771
1067
|
["u:properties" /* UserProperties */]: z2.record(z2.unknown()).nullish(),
|
|
772
1068
|
["l:properties" /* LicenseProperties */]: z2.record(z2.unknown()).nullish(),
|
|
773
|
-
["t:properties" /* TrialProperties */]: z2.record(z2.unknown()).nullish()
|
|
1069
|
+
["t:properties" /* TrialProperties */]: z2.record(z2.unknown()).nullish(),
|
|
1070
|
+
["l:rel:until" /* ReleaseAccessUpdatesUntil */]: integerClaimSchema,
|
|
1071
|
+
["l:rel:major" /* ReleaseAccessUpToMajor */]: integerClaimSchema,
|
|
1072
|
+
["l:rel:upto" /* ReleaseAccessUpToVersion */]: z2.string().nullish().catch(void 0)
|
|
774
1073
|
});
|
|
775
1074
|
var LicenseValidator = class {
|
|
776
1075
|
constructor(configuration, deviceIdResolver) {
|
|
777
1076
|
this.configuration = configuration;
|
|
778
1077
|
this.deviceIdResolver = deviceIdResolver;
|
|
779
1078
|
}
|
|
780
|
-
async validateLicense(token) {
|
|
1079
|
+
async validateLicense(token, options) {
|
|
1080
|
+
var _a, _b;
|
|
781
1081
|
const [license, claims] = await this.parseLicenseToken(token);
|
|
782
1082
|
if (license.expiresAt && license.expiresAt < /* @__PURE__ */ new Date()) {
|
|
783
1083
|
throw new MoonbaseError(
|
|
@@ -790,6 +1090,15 @@ var LicenseValidator = class {
|
|
|
790
1090
|
const boundSignature = claims["sig" /* ComputerSignature */];
|
|
791
1091
|
if (expectedSignature !== boundSignature && !await this.acceptsHistoricalDeviceId(boundSignature))
|
|
792
1092
|
throw deviceMismatchError(expectedSignature, boundSignature);
|
|
1093
|
+
const { appVersion } = this.configuration;
|
|
1094
|
+
const enforceReleaseAccess = (_b = (_a = options == null ? void 0 : options.enforceReleaseAccess) != null ? _a : this.configuration.enforceReleaseAccess) != null ? _b : true;
|
|
1095
|
+
if (enforceReleaseAccess && appVersion && !coversVersion(license, appVersion)) {
|
|
1096
|
+
throw new ReleaseNotCoveredError(
|
|
1097
|
+
"Version not covered",
|
|
1098
|
+
`The license doesn't cover version ${appVersion}`,
|
|
1099
|
+
{ license, appVersion }
|
|
1100
|
+
);
|
|
1101
|
+
}
|
|
793
1102
|
return license;
|
|
794
1103
|
}
|
|
795
1104
|
/**
|
|
@@ -828,6 +1137,7 @@ var LicenseValidator = class {
|
|
|
828
1137
|
}
|
|
829
1138
|
try {
|
|
830
1139
|
const claims = licenseClaimsSchema.parse(decoded);
|
|
1140
|
+
const releaseAccess = readReleaseAccess(claims);
|
|
831
1141
|
resolve([{
|
|
832
1142
|
id: claims["l:id" /* LicenseId */],
|
|
833
1143
|
trial: claims["trial" /* Trial */],
|
|
@@ -839,6 +1149,7 @@ var LicenseValidator = class {
|
|
|
839
1149
|
properties: (_b = claims["p:properties" /* ProductProperties */]) != null ? _b : void 0
|
|
840
1150
|
},
|
|
841
1151
|
ownedSubProductIds: claims["sp:owned" /* OwnedSubProducts */],
|
|
1152
|
+
subProducts: claims["sp" /* SubProducts */],
|
|
842
1153
|
subscriptionId: (_c = claims["s:id" /* SubscriptionId */]) != null ? _c : void 0,
|
|
843
1154
|
issuedTo: {
|
|
844
1155
|
id: claims["u:id" /* UserId */],
|
|
@@ -849,6 +1160,7 @@ var LicenseValidator = class {
|
|
|
849
1160
|
expiresAt: claims["exp" /* ExpiresAt */] || void 0,
|
|
850
1161
|
issuedAt: claims["iat" /* IssuedAt */],
|
|
851
1162
|
validatedAt: claims["validated" /* ValidatedAt */],
|
|
1163
|
+
...releaseAccess && { releaseAccess },
|
|
852
1164
|
properties: (_e = claims["trial" /* Trial */] ? claims["t:properties" /* TrialProperties */] : claims["l:properties" /* LicenseProperties */]) != null ? _e : void 0,
|
|
853
1165
|
token
|
|
854
1166
|
}, claims]);
|
|
@@ -865,6 +1177,50 @@ var LicenseValidator = class {
|
|
|
865
1177
|
});
|
|
866
1178
|
}
|
|
867
1179
|
};
|
|
1180
|
+
var subProductClaimSchema = z2.object({
|
|
1181
|
+
id: z2.string(),
|
|
1182
|
+
active: z2.boolean().catch(false),
|
|
1183
|
+
trial: z2.boolean().catch(false),
|
|
1184
|
+
revoked: z2.boolean().catch(false),
|
|
1185
|
+
exp: z2.number().nullish().catch(void 0),
|
|
1186
|
+
seats: z2.object({
|
|
1187
|
+
total: z2.number(),
|
|
1188
|
+
used: z2.number(),
|
|
1189
|
+
remaining: z2.number()
|
|
1190
|
+
}).nullish().catch(void 0)
|
|
1191
|
+
});
|
|
1192
|
+
function readSubProducts(entries) {
|
|
1193
|
+
return (entries != null ? entries : []).flatMap((entry) => {
|
|
1194
|
+
const parsed = subProductClaimSchema.safeParse(entry);
|
|
1195
|
+
if (!parsed.success)
|
|
1196
|
+
return [];
|
|
1197
|
+
const { id, active, trial, revoked, exp, seats } = parsed.data;
|
|
1198
|
+
return [{
|
|
1199
|
+
id,
|
|
1200
|
+
active,
|
|
1201
|
+
trial,
|
|
1202
|
+
revoked,
|
|
1203
|
+
expiresAt: exp ? new Date(exp * 1e3) : void 0,
|
|
1204
|
+
seats: seats != null ? seats : void 0
|
|
1205
|
+
}];
|
|
1206
|
+
});
|
|
1207
|
+
}
|
|
1208
|
+
function readReleaseAccess(claims) {
|
|
1209
|
+
var _a, _b;
|
|
1210
|
+
const updatesUntil = (_a = claims["l:rel:until" /* ReleaseAccessUpdatesUntil */]) != null ? _a : void 0;
|
|
1211
|
+
const upToMajor = (_b = claims["l:rel:major" /* ReleaseAccessUpToMajor */]) != null ? _b : void 0;
|
|
1212
|
+
if (updatesUntil === void 0 && upToMajor === void 0)
|
|
1213
|
+
return void 0;
|
|
1214
|
+
const releaseAccess = {};
|
|
1215
|
+
if (updatesUntil !== void 0)
|
|
1216
|
+
releaseAccess.updatesUntil = new Date(updatesUntil * 1e3);
|
|
1217
|
+
if (upToMajor !== void 0)
|
|
1218
|
+
releaseAccess.upToMajor = upToMajor;
|
|
1219
|
+
const upToVersion = claims["l:rel:upto" /* ReleaseAccessUpToVersion */];
|
|
1220
|
+
if (upToVersion)
|
|
1221
|
+
releaseAccess.upToVersion = upToVersion;
|
|
1222
|
+
return releaseAccess;
|
|
1223
|
+
}
|
|
868
1224
|
function deviceMismatchError(expected, bound) {
|
|
869
1225
|
const detail = "This license is not for this device";
|
|
870
1226
|
const stampNote = describeStampDifference(expected, bound);
|
|
@@ -911,6 +1267,15 @@ function describeSourceDifference(expected, bound) {
|
|
|
911
1267
|
return "The binding was created from hardware identity, while this SDK has fallen back to the host name \u2014 check why hardware identity cannot be read here rather than re-activating, which would rebind the device to the weaker id.";
|
|
912
1268
|
}
|
|
913
1269
|
|
|
1270
|
+
// src/subProducts.ts
|
|
1271
|
+
function hasSubProduct(license, subProductId) {
|
|
1272
|
+
var _a;
|
|
1273
|
+
const parentPrefix = `${license.product.id}.`;
|
|
1274
|
+
const id = subProductId.startsWith(parentPrefix) ? subProductId.slice(parentPrefix.length) : subProductId;
|
|
1275
|
+
const now = /* @__PURE__ */ new Date();
|
|
1276
|
+
return ((_a = license.subProducts) != null ? _a : []).some((subProduct) => subProduct.id === id && subProduct.active && (!subProduct.expiresAt || subProduct.expiresAt > now));
|
|
1277
|
+
}
|
|
1278
|
+
|
|
914
1279
|
// src/index.ts
|
|
915
1280
|
function detectPlatform() {
|
|
916
1281
|
if (typeof process3 === "undefined")
|
|
@@ -971,13 +1336,17 @@ export {
|
|
|
971
1336
|
MoonbaseDeviceIdResolver,
|
|
972
1337
|
MoonbaseError,
|
|
973
1338
|
MoonbaseLicensing,
|
|
1339
|
+
ReleaseNotCoveredError,
|
|
974
1340
|
buildFingerprintMaterial,
|
|
975
1341
|
canonicalizeParams,
|
|
976
1342
|
canonicalizeValue,
|
|
1343
|
+
coversVersion,
|
|
977
1344
|
defaultDeviceIdentityReader,
|
|
978
1345
|
fingerprintDeviceId,
|
|
979
1346
|
fingerprintDigest,
|
|
1347
|
+
hasSubProduct,
|
|
980
1348
|
identitySource,
|
|
1349
|
+
parseAppVersion,
|
|
981
1350
|
parseDeviceIdStamp,
|
|
982
1351
|
parseIoregPlatformUuid,
|
|
983
1352
|
parseSmbiosParams,
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@moonbase.sh/licensing",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "3.
|
|
4
|
+
"version": "3.3.0",
|
|
5
5
|
"description": "Package to add sotftware licensing using Moonbase.sh to your node.js apps",
|
|
6
6
|
"author": "Tobias Lønnerød Madsen <m@dsen.tv>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -12,7 +12,8 @@
|
|
|
12
12
|
"files": [
|
|
13
13
|
"FINGERPRINT_SPEC.md",
|
|
14
14
|
"dist/**",
|
|
15
|
-
"fingerprint-vectors.json"
|
|
15
|
+
"fingerprint-vectors.json",
|
|
16
|
+
"release-access-vectors.json"
|
|
16
17
|
],
|
|
17
18
|
"dependencies": {
|
|
18
19
|
"cross-fetch": "^3.1.5",
|