@naturalcycles/nodejs-lib 15.116.0 → 15.116.1

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.
@@ -156,6 +156,13 @@ export declare class JWTService2<T extends AnyObject = AnyObject> {
156
156
  sign<TT extends T = T>(payload: TT, opt: JWTSignOptions<TT>): Promise<JWTString>;
157
157
  verify<TT extends T = T>(token: JWTString, opt?: JWTVerifyOptions<TT>): Promise<TT>;
158
158
  decode<TT extends T = T>(token: JWTString, opt?: JWTDecodeOptions<TT>): JWTDecoded<TT>;
159
+ /**
160
+ * Schema-validation errors on Verify/Decode are extended with cfg.errorData:
161
+ * a token with a non-conforming payload is as unauthorized as an invalid one.
162
+ * (On Sign the payload comes from own code, not from user input, so a validation
163
+ * error there indicates a programming error and is thrown as-is.)
164
+ */
165
+ private validate;
159
166
  /**
160
167
  * jose errors are normalized into JWTError (extended with cfg.errorData).
161
168
  * Non-jose errors (e.g TypeError from passing a key that doesn't fit the algorithm)
@@ -1,6 +1,6 @@
1
1
  import { createPrivateKey, createPublicKey } from 'node:crypto';
2
2
  import { _assert } from '@naturalcycles/js-lib/error/assert.js';
3
- import { AppError } from '@naturalcycles/js-lib/error/error.util.js';
3
+ import { _errorDataAppend, AppError } from '@naturalcycles/js-lib/error/error.util.js';
4
4
  import { decodeJwt, decodeProtectedHeader, errors, jwtVerify, SignJWT } from 'jose';
5
5
  /**
6
6
  * Wraps the `jose` library, exposing an implementation-agnostic API:
@@ -79,8 +79,7 @@ export class JWTService2 {
79
79
  catch (err) {
80
80
  throw this.normalizeError(err);
81
81
  }
82
- ;
83
- (schema || this.cfg.schema)?.validate(data);
82
+ this.validate(data, schema);
84
83
  return data;
85
84
  }
86
85
  decode(token, opt = {}) {
@@ -96,14 +95,31 @@ export class JWTService2 {
96
95
  code: 'JWT_INVALID',
97
96
  }, { cause: err });
98
97
  }
99
- ;
100
- (opt.schema || this.cfg.schema)?.validate(payload);
98
+ this.validate(payload, opt.schema);
101
99
  return {
102
100
  header,
103
101
  payload,
104
102
  signature: token.split('.')[2],
105
103
  };
106
104
  }
105
+ /**
106
+ * Schema-validation errors on Verify/Decode are extended with cfg.errorData:
107
+ * a token with a non-conforming payload is as unauthorized as an invalid one.
108
+ * (On Sign the payload comes from own code, not from user input, so a validation
109
+ * error there indicates a programming error and is thrown as-is.)
110
+ */
111
+ validate(payload, schema) {
112
+ try {
113
+ ;
114
+ (schema || this.cfg.schema)?.validate(payload);
115
+ }
116
+ catch (err) {
117
+ if (this.cfg.errorData) {
118
+ _errorDataAppend(err, this.cfg.errorData);
119
+ }
120
+ throw err;
121
+ }
122
+ }
107
123
  /**
108
124
  * jose errors are normalized into JWTError (extended with cfg.errorData).
109
125
  * Non-jose errors (e.g TypeError from passing a key that doesn't fit the algorithm)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
3
  "type": "module",
4
- "version": "15.116.0",
4
+ "version": "15.116.1",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@standard-schema/spec": "^1",
@@ -2,7 +2,7 @@ import { createPrivateKey, createPublicKey } from 'node:crypto'
2
2
  import type { KeyObject } from 'node:crypto'
3
3
  import type { ErrorData } from '@naturalcycles/js-lib/error'
4
4
  import { _assert } from '@naturalcycles/js-lib/error/assert.js'
5
- import { AppError } from '@naturalcycles/js-lib/error/error.util.js'
5
+ import { _errorDataAppend, AppError } from '@naturalcycles/js-lib/error/error.util.js'
6
6
  import type {
7
7
  AnyObject,
8
8
  JWTString,
@@ -243,7 +243,7 @@ export class JWTService2<T extends AnyObject = AnyObject> {
243
243
  throw this.normalizeError(err)
244
244
  }
245
245
 
246
- ;(schema || this.cfg.schema)?.validate(data)
246
+ this.validate(data, schema)
247
247
 
248
248
  return data
249
249
  }
@@ -266,7 +266,7 @@ export class JWTService2<T extends AnyObject = AnyObject> {
266
266
  )
267
267
  }
268
268
 
269
- ;(opt.schema || this.cfg.schema)?.validate(payload)
269
+ this.validate(payload, opt.schema)
270
270
 
271
271
  return {
272
272
  header,
@@ -275,6 +275,23 @@ export class JWTService2<T extends AnyObject = AnyObject> {
275
275
  }
276
276
  }
277
277
 
278
+ /**
279
+ * Schema-validation errors on Verify/Decode are extended with cfg.errorData:
280
+ * a token with a non-conforming payload is as unauthorized as an invalid one.
281
+ * (On Sign the payload comes from own code, not from user input, so a validation
282
+ * error there indicates a programming error and is thrown as-is.)
283
+ */
284
+ private validate<TT extends T>(payload: TT, schema?: JSchema<TT, any> | AjvSchema<TT>): void {
285
+ try {
286
+ ;(schema || this.cfg.schema)?.validate(payload)
287
+ } catch (err) {
288
+ if (this.cfg.errorData) {
289
+ _errorDataAppend(err, this.cfg.errorData)
290
+ }
291
+ throw err
292
+ }
293
+ }
294
+
278
295
  /**
279
296
  * jose errors are normalized into JWTError (extended with cfg.errorData).
280
297
  * Non-jose errors (e.g TypeError from passing a key that doesn't fit the algorithm)