@usertour/helpers 0.0.9 → 0.0.11

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/dist/server.cjs DELETED
@@ -1,331 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
- var __export = (target, all) => {
10
- for (var name in all)
11
- __defProp(target, name, { get: all[name], enumerable: true });
12
- };
13
- var __copyProps = (to, from, except, desc) => {
14
- if (from && typeof from === "object" || typeof from === "function") {
15
- for (let key of __getOwnPropNames(from))
16
- if (!__hasOwnProp.call(to, key) && key !== except)
17
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18
- }
19
- return to;
20
- };
21
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
- // If the importer is in node compatibility mode or this is not an ESM
23
- // file that has been converted to a CommonJS file using a Babel-
24
- // compatible transform (i.e. "__esModule" has not been set), then set
25
- // "default" to the CommonJS "module.exports" for node compatibility.
26
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
27
- mod
28
- ));
29
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
30
- var __publicField = (obj, key, value) => {
31
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
32
- return value;
33
- };
34
-
35
- // src/server.ts
36
- var server_exports = {};
37
- __export(server_exports, {
38
- JWTLicenseSigner: () => JWTLicenseSigner,
39
- JWTLicenseValidator: () => JWTLicenseValidator
40
- });
41
- module.exports = __toCommonJS(server_exports);
42
-
43
- // src/jwt-license-signer.ts
44
- var fs = __toESM(require("fs"), 1);
45
- var path = __toESM(require("path"), 1);
46
- var import_jsonwebtoken = __toESM(require("jsonwebtoken"), 1);
47
- var JWTLicenseSigner = class {
48
- constructor(options) {
49
- __publicField(this, "privateKey");
50
- __publicField(this, "issuer");
51
- __publicField(this, "algorithm");
52
- this.privateKey = this.loadPrivateKey(options.privateKeyPath);
53
- this.issuer = options.issuer || "https://www.usertour.io";
54
- this.algorithm = options.algorithm || "RS256";
55
- }
56
- /**
57
- * Load RSA private key from file
58
- */
59
- loadPrivateKey(keyPath) {
60
- try {
61
- const fullPath = path.resolve(keyPath);
62
- return fs.readFileSync(fullPath, "utf8");
63
- } catch (error) {
64
- throw new Error(`Failed to load private key from ${keyPath}: ${error}`);
65
- }
66
- }
67
- /**
68
- * Generate a JWT license token
69
- */
70
- generateLicense(options) {
71
- const now = Math.floor(Date.now() / 1e3);
72
- const expiresAt = now + options.expiresInDays * 24 * 60 * 60;
73
- const payload = {
74
- plan: options.plan,
75
- sub: options.subject,
76
- projectId: options.projectId,
77
- iat: now,
78
- exp: expiresAt,
79
- issuer: options.issuer || this.issuer,
80
- features: options.features
81
- };
82
- try {
83
- return import_jsonwebtoken.default.sign(payload, this.privateKey, {
84
- algorithm: this.algorithm,
85
- issuer: this.issuer
86
- });
87
- } catch (error) {
88
- throw new Error(`Failed to generate JWT license: ${error}`);
89
- }
90
- }
91
- /**
92
- * Generate a license and return both token and payload info
93
- */
94
- generateLicenseWithInfo(options) {
95
- const now = Math.floor(Date.now() / 1e3);
96
- const expiresAt = now + options.expiresInDays * 24 * 60 * 60;
97
- const payload = {
98
- plan: options.plan,
99
- sub: options.subject,
100
- projectId: options.projectId,
101
- iat: now,
102
- exp: expiresAt,
103
- issuer: options.issuer || this.issuer,
104
- features: options.features
105
- };
106
- const token = import_jsonwebtoken.default.sign(payload, this.privateKey, {
107
- algorithm: this.algorithm,
108
- issuer: this.issuer
109
- });
110
- return {
111
- token,
112
- payload,
113
- expiresAt: new Date(expiresAt * 1e3)
114
- };
115
- }
116
- /**
117
- * Decode a JWT token without verification (for debugging)
118
- */
119
- decodeToken(token) {
120
- try {
121
- return import_jsonwebtoken.default.decode(token);
122
- } catch (error) {
123
- console.error("Failed to decode JWT token:", error);
124
- return null;
125
- }
126
- }
127
- /**
128
- * Get token information without verification
129
- */
130
- getTokenInfo(token) {
131
- try {
132
- const decoded = import_jsonwebtoken.default.decode(token, { complete: true });
133
- if (!decoded || typeof decoded === "string") {
134
- return null;
135
- }
136
- return {
137
- header: decoded.header,
138
- payload: decoded.payload,
139
- signature: decoded.signature
140
- };
141
- } catch (error) {
142
- console.error("Failed to get token info:", error);
143
- return null;
144
- }
145
- }
146
- };
147
-
148
- // src/jwt-license-validator.ts
149
- var import_jsonwebtoken2 = __toESM(require("jsonwebtoken"), 1);
150
- var JWTLicenseValidator = {
151
- /**
152
- * Validate a JWT license
153
- * @param license - JWT license string
154
- * @param publicKey - RSA public key in PEM format
155
- * @param options - Validation options
156
- * @returns Validation result
157
- */
158
- validateLicense(license, publicKey, options = {}) {
159
- try {
160
- const { checkExpiration = true, currentTime = /* @__PURE__ */ new Date() } = options;
161
- const decoded = import_jsonwebtoken2.default.verify(license, publicKey, {
162
- algorithms: ["RS256"],
163
- ignoreExpiration: !checkExpiration
164
- });
165
- const fieldValidation = this.validateRequiredFields(decoded);
166
- if (!fieldValidation.isValid) {
167
- return fieldValidation;
168
- }
169
- if (checkExpiration) {
170
- const expirationValidation = this.checkExpiration(decoded, currentTime);
171
- if (!expirationValidation.isValid) {
172
- return expirationValidation;
173
- }
174
- }
175
- const hasFeature = (feature) => {
176
- return decoded.features.includes("*") || decoded.features.includes(feature);
177
- };
178
- return {
179
- isValid: true,
180
- payload: decoded,
181
- isExpired: false,
182
- hasFeature
183
- };
184
- } catch (error) {
185
- if (error instanceof import_jsonwebtoken2.default.JsonWebTokenError) {
186
- return {
187
- isValid: false,
188
- error: `JWT validation failed: ${error.message}`
189
- };
190
- }
191
- if (error instanceof import_jsonwebtoken2.default.TokenExpiredError) {
192
- return {
193
- isValid: false,
194
- error: `License expired: ${error.message}`,
195
- isExpired: true
196
- };
197
- }
198
- return {
199
- isValid: false,
200
- error: `License validation failed: ${error instanceof Error ? error.message : "Unknown error"}`
201
- };
202
- }
203
- },
204
- /**
205
- * Validate that all required fields are present in license payload
206
- * @param payload - License payload to validate
207
- * @returns Validation result
208
- */
209
- validateRequiredFields(payload) {
210
- const requiredFields = ["plan", "sub", "projectId", "iat", "exp", "issuer", "features"];
211
- for (const field of requiredFields) {
212
- if (!(field in payload)) {
213
- return {
214
- isValid: false,
215
- error: `Missing required field: ${field}`
216
- };
217
- }
218
- }
219
- if (typeof payload.plan !== "string" || !payload.plan.trim()) {
220
- return {
221
- isValid: false,
222
- error: "Invalid plan: must be a non-empty string"
223
- };
224
- }
225
- if (typeof payload.sub !== "string" || !payload.sub.trim()) {
226
- return {
227
- isValid: false,
228
- error: "Invalid sub: must be a non-empty string"
229
- };
230
- }
231
- if (typeof payload.projectId !== "string" || !payload.projectId.trim()) {
232
- return {
233
- isValid: false,
234
- error: "Invalid projectId: must be a non-empty string"
235
- };
236
- }
237
- if (typeof payload.issuer !== "string" || !payload.issuer.trim()) {
238
- return {
239
- isValid: false,
240
- error: "Invalid issuer: must be a non-empty string"
241
- };
242
- }
243
- if (!Array.isArray(payload.features)) {
244
- return {
245
- isValid: false,
246
- error: "Invalid features: must be an array"
247
- };
248
- }
249
- if (typeof payload.iat !== "number" || payload.iat <= 0) {
250
- return {
251
- isValid: false,
252
- error: "Invalid iat: must be a positive number"
253
- };
254
- }
255
- if (typeof payload.exp !== "number" || payload.exp <= 0) {
256
- return {
257
- isValid: false,
258
- error: "Invalid exp: must be a positive number"
259
- };
260
- }
261
- if (payload.iat >= payload.exp) {
262
- return {
263
- isValid: false,
264
- error: "Invalid timestamps: iat must be before exp"
265
- };
266
- }
267
- return { isValid: true };
268
- },
269
- /**
270
- * Check if license has expired
271
- * @param payload - License payload
272
- * @param currentTime - Current time to check against (defaults to now)
273
- * @returns Validation result
274
- */
275
- checkExpiration(payload, currentTime = /* @__PURE__ */ new Date()) {
276
- const now = Math.floor(currentTime.getTime() / 1e3);
277
- const expiresAt = payload.exp;
278
- if (now > expiresAt) {
279
- return {
280
- isValid: false,
281
- error: `License expired on ${new Date(expiresAt * 1e3).toISOString()}`,
282
- isExpired: true
283
- };
284
- }
285
- return { isValid: true, isExpired: false };
286
- },
287
- /**
288
- * Check if license has a specific feature
289
- * @param payload - License payload
290
- * @param feature - Feature to check
291
- * @returns Whether the feature is available
292
- */
293
- hasFeature(payload, feature) {
294
- return payload.features.includes("*") || payload.features.includes(feature);
295
- },
296
- /**
297
- * Get license expiration status
298
- * @param payload - License payload
299
- * @param currentTime - Current time to check against (defaults to now)
300
- * @returns Expiration information
301
- */
302
- getExpirationInfo(payload, currentTime = /* @__PURE__ */ new Date()) {
303
- const now = Math.floor(currentTime.getTime() / 1e3);
304
- const expiresAt = payload.exp;
305
- const isExpired = now > expiresAt;
306
- const daysUntilExpiration = Math.ceil((expiresAt - now) / (24 * 60 * 60));
307
- return {
308
- isExpired,
309
- expiresAt: new Date(expiresAt * 1e3),
310
- daysUntilExpiration: isExpired ? 0 : daysUntilExpiration
311
- };
312
- },
313
- /**
314
- * Decode JWT license without verification (for debugging)
315
- * @param license - JWT license string
316
- * @returns Decoded payload or null if invalid
317
- */
318
- decodeLicense(license) {
319
- try {
320
- const decoded = import_jsonwebtoken2.default.decode(license);
321
- return decoded;
322
- } catch {
323
- return null;
324
- }
325
- }
326
- };
327
- // Annotate the CommonJS export names for ESM import in node:
328
- 0 && (module.exports = {
329
- JWTLicenseSigner,
330
- JWTLicenseValidator
331
- });
package/dist/server.d.cts DELETED
@@ -1,4 +0,0 @@
1
- export { GenerateLicenseOptions, JWTLicenseSigner, JWTLicenseSignerOptions } from './jwt-license-signer.cjs';
2
- export { JWTLicenseValidator } from './jwt-license-validator.cjs';
3
- import 'jsonwebtoken';
4
- import '@usertour/types';
package/dist/server.d.ts DELETED
@@ -1,4 +0,0 @@
1
- export { GenerateLicenseOptions, JWTLicenseSigner, JWTLicenseSignerOptions } from './jwt-license-signer.js';
2
- export { JWTLicenseValidator } from './jwt-license-validator.js';
3
- import 'jsonwebtoken';
4
- import '@usertour/types';
package/dist/server.js DELETED
@@ -1,11 +0,0 @@
1
- import {
2
- JWTLicenseSigner
3
- } from "./chunk-HWWIAVVP.js";
4
- import {
5
- JWTLicenseValidator
6
- } from "./chunk-Y5PCSFVZ.js";
7
- import "./chunk-XEO3YXBM.js";
8
- export {
9
- JWTLicenseSigner,
10
- JWTLicenseValidator
11
- };