ecwt 0.2.0-beta.2 → 0.2.0-beta.3

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/ecwt.cjs CHANGED
@@ -410,6 +410,36 @@ var EcwtFactory = class {
410
410
  }
411
411
  return ecwt;
412
412
  }
413
+ /**
414
+ * Parses token without throwing errors.
415
+ * @async
416
+ * @param {string} token String representation of token.
417
+ * @returns {Promise<{ success: boolean, ecwt: Ecwt | null }>} Returns whether token was parsed and verified successfully and Ecwt if parsed.
418
+ */
419
+ async safeVerify(token) {
420
+ let ecwt;
421
+ try {
422
+ ecwt = await this.verify(token);
423
+ return {
424
+ success: true,
425
+ ecwt
426
+ };
427
+ } catch (error) {
428
+ if (error instanceof EcwtParseError) {
429
+ return {
430
+ success: false,
431
+ ecwt: null
432
+ };
433
+ }
434
+ if (error instanceof EcwtInvalidError) {
435
+ return {
436
+ success: false,
437
+ ecwt
438
+ };
439
+ }
440
+ throw error;
441
+ }
442
+ }
413
443
  /**
414
444
  * Revokes token.
415
445
  * @async
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ecwt",
3
- "version": "0.2.0-beta.2",
3
+ "version": "0.2.0-beta.3",
4
4
  "description": "Encrypted CBOR-encoded Web Token",
5
5
  "main": "src/main.js",
6
6
  "type": "module",
package/src/factory.js CHANGED
@@ -13,6 +13,7 @@ import { Ecwt } from './token.js';
13
13
  import { base62 } from './utils/base62.js';
14
14
  import {
15
15
  InvalidPackageInstanceError,
16
+ EcwtInvalidError,
16
17
  EcwtExpiredError,
17
18
  EcwtRevokedError,
18
19
  EcwtParseError } from './utils/errors.js';
@@ -299,6 +300,41 @@ export class EcwtFactory {
299
300
  return ecwt;
300
301
  }
301
302
 
303
+ /**
304
+ * Parses token without throwing errors.
305
+ * @async
306
+ * @param {string} token String representation of token.
307
+ * @returns {Promise<{ success: boolean, ecwt: Ecwt | null }>} Returns whether token was parsed and verified successfully and Ecwt if parsed.
308
+ */
309
+ async safeVerify(token) {
310
+ let ecwt;
311
+ try {
312
+ ecwt = await this.verify(token);
313
+
314
+ return {
315
+ success: true,
316
+ ecwt,
317
+ };
318
+ }
319
+ catch (error) {
320
+ if (error instanceof EcwtParseError) {
321
+ return {
322
+ success: false,
323
+ ecwt: null,
324
+ };
325
+ }
326
+
327
+ if (error instanceof EcwtInvalidError) {
328
+ return {
329
+ success: false,
330
+ ecwt,
331
+ };
332
+ }
333
+
334
+ throw error;
335
+ }
336
+ }
337
+
302
338
  /**
303
339
  * Revokes token.
304
340
  * @async
@@ -5,12 +5,18 @@ export class InvalidPackageInstanceError extends TypeError {
5
5
  }
6
6
  }
7
7
 
8
+ /**
9
+ * Error thrown when string token cannot be parsed to Ecwt.
10
+ */
8
11
  export class EcwtParseError extends Error {
9
12
  constructor() {
10
13
  super('Cannot parse data to Ecwt token.');
11
14
  }
12
15
  }
13
16
 
17
+ /**
18
+ * Error thrown when parsed Ecwt is invalid.
19
+ */
14
20
  export class EcwtInvalidError extends Error {
15
21
  constructor(ecwt) {
16
22
  super('Ecwt token is invalid.');
@@ -19,6 +25,9 @@ export class EcwtInvalidError extends Error {
19
25
  }
20
26
  }
21
27
 
28
+ /**
29
+ * Error thrown when parsed Ecwt is expired.
30
+ */
22
31
  export class EcwtExpiredError extends EcwtInvalidError {
23
32
  constructor(ecwt) {
24
33
  super();
@@ -28,6 +37,9 @@ export class EcwtExpiredError extends EcwtInvalidError {
28
37
  }
29
38
  }
30
39
 
40
+ /**
41
+ * Error thrown when parsed Ecwt is revoked.
42
+ */
31
43
  export class EcwtRevokedError extends EcwtInvalidError {
32
44
  constructor(ecwt) {
33
45
  super();