@naturalcycles/nodejs-lib 15.118.0 → 15.119.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.
@@ -175,6 +175,15 @@ export declare class JWTService2<T extends AnyObject = AnyObject> {
175
175
  constructor(cfg: JWTService2Cfg<T>);
176
176
  sign<TT extends T = T>(payload: TT, opt: JWTSignOptions<TT>): Promise<JWTString>;
177
177
  verify<TT extends T = T>(token: JWTString, opt?: JWTVerifyOptions<TT>): Promise<TT>;
178
+ /**
179
+ * Tries to Verify the token, returning an [error, payload] tuple instead of throwing:
180
+ * - [null, payload] - the token verified, payload can be trusted
181
+ * - [error, null] - verification failed
182
+ *
183
+ * Same contract as tryToVerifyOrDecode, but without the unverified-decode fallback:
184
+ * the payload is only returned when the token verified.
185
+ */
186
+ tryToVerify<TT extends T = T>(token: JWTString, opt?: JWTVerifyOptions<TT>): Promise<[err: null, payload: TT] | [err: Error, payload: null]>;
178
187
  /**
179
188
  * Tries to Verify the token, falling back to unverified Decode on failure,
180
189
  * so the caller can "peek" into the token's content even when it doesn't verify.
@@ -88,6 +88,22 @@ export class JWTService2 {
88
88
  this.validate(data, schema);
89
89
  return data;
90
90
  }
91
+ /**
92
+ * Tries to Verify the token, returning an [error, payload] tuple instead of throwing:
93
+ * - [null, payload] - the token verified, payload can be trusted
94
+ * - [error, null] - verification failed
95
+ *
96
+ * Same contract as tryToVerifyOrDecode, but without the unverified-decode fallback:
97
+ * the payload is only returned when the token verified.
98
+ */
99
+ async tryToVerify(token, opt = {}) {
100
+ try {
101
+ return [null, await this.verify(token, opt)];
102
+ }
103
+ catch (err) {
104
+ return [err, null];
105
+ }
106
+ }
91
107
  /**
92
108
  * Tries to Verify the token, falling back to unverified Decode on failure,
93
109
  * so the caller can "peek" into the token's content even when it doesn't verify.
@@ -104,13 +120,9 @@ export class JWTService2 {
104
120
  * The decode fallback is raw: opt.schema/cfg.schema are only applied on the Verify path.
105
121
  */
106
122
  async tryToVerifyOrDecode(token, opt = {}) {
107
- let verifyError;
108
- try {
109
- return [null, await this.verify(token, opt)];
110
- }
111
- catch (err) {
112
- verifyError = err;
113
- }
123
+ const [verifyError, payload] = await this.tryToVerify(token, opt);
124
+ if (!verifyError)
125
+ return [null, payload];
114
126
  // Expired/not-yet-valid errors already carry the (signature-verified) payload
115
127
  if (verifyError instanceof JWTExpiredError || verifyError instanceof JWTNotYetValidError) {
116
128
  return [verifyError, verifyError.payload];
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
3
  "type": "module",
4
- "version": "15.118.0",
4
+ "version": "15.119.0",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@standard-schema/spec": "^1",
@@ -274,6 +274,25 @@ export class JWTService2<T extends AnyObject = AnyObject> {
274
274
  return data
275
275
  }
276
276
 
277
+ /**
278
+ * Tries to Verify the token, returning an [error, payload] tuple instead of throwing:
279
+ * - [null, payload] - the token verified, payload can be trusted
280
+ * - [error, null] - verification failed
281
+ *
282
+ * Same contract as tryToVerifyOrDecode, but without the unverified-decode fallback:
283
+ * the payload is only returned when the token verified.
284
+ */
285
+ async tryToVerify<TT extends T = T>(
286
+ token: JWTString,
287
+ opt: JWTVerifyOptions<TT> = {},
288
+ ): Promise<[err: null, payload: TT] | [err: Error, payload: null]> {
289
+ try {
290
+ return [null, await this.verify<TT>(token, opt)]
291
+ } catch (err) {
292
+ return [err as Error, null]
293
+ }
294
+ }
295
+
277
296
  /**
278
297
  * Tries to Verify the token, falling back to unverified Decode on failure,
279
298
  * so the caller can "peek" into the token's content even when it doesn't verify.
@@ -293,13 +312,8 @@ export class JWTService2<T extends AnyObject = AnyObject> {
293
312
  token: JWTString,
294
313
  opt: JWTVerifyOptions<TT> = {},
295
314
  ): Promise<[err: null, payload: TT] | [err: Error, payload: TT | null]> {
296
- let verifyError: Error
297
-
298
- try {
299
- return [null, await this.verify<TT>(token, opt)]
300
- } catch (err) {
301
- verifyError = err as Error
302
- }
315
+ const [verifyError, payload] = await this.tryToVerify<TT>(token, opt)
316
+ if (!verifyError) return [null, payload]
303
317
 
304
318
  // Expired/not-yet-valid errors already carry the (signature-verified) payload
305
319
  if (verifyError instanceof JWTExpiredError || verifyError instanceof JWTNotYetValidError) {