vona-module-a-jwt 5.0.29 → 5.0.30

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/index.js CHANGED
@@ -1,14 +1,13 @@
1
1
  import { BeanInfo, BeanBase, deepExtend, cast, BeanScopeBase } from 'vona';
2
+ import { __decorate, __metadata } from 'tslib';
2
3
  import ms from 'ms';
3
4
  import { Service, Bean, Scope } from 'vona-module-a-bean';
5
+ import { catchError } from '@cabloy/utils';
4
6
  import jwt from 'jsonwebtoken';
5
7
  import { Api } from 'vona-module-a-openapiutils';
6
8
  import { Dto } from 'vona-module-a-web';
7
9
 
8
- var _dec$4, _dec2$4, _class$4;
9
- let ServiceJwtClient = (_dec$4 = Service(), _dec2$4 = BeanInfo({
10
- module: "a-jwt"
11
- }), _dec$4(_class$4 = _dec2$4(_class$4 = class ServiceJwtClient extends BeanBase {
10
+ let ServiceJwtClient = class ServiceJwtClient extends BeanBase {
12
11
  constructor(...args) {
13
12
  super(...args);
14
13
  this._jwtInstance = void 0;
@@ -43,6 +42,14 @@ let ServiceJwtClient = (_dec$4 = Service(), _dec2$4 = BeanInfo({
43
42
  return this.scope.config.field.payload.data;
44
43
  }
45
44
  async sign(payloadData, options) {
45
+ const [res, error] = await catchError(() => {
46
+ return this._signInner(payloadData, options);
47
+ });
48
+ this.$loggerChild('jwt').debug(() => `jwt.sign: client:${this._clientName}, token:${res}${error ? `, error: ${error.message}` : ''}`);
49
+ if (error) throw error;
50
+ return res;
51
+ }
52
+ async _signInner(payloadData, options) {
46
53
  return new Promise((resolve, reject) => {
47
54
  const payload = {
48
55
  [this.fieldClient]: this._clientName,
@@ -68,10 +75,20 @@ let ServiceJwtClient = (_dec$4 = Service(), _dec2$4 = BeanInfo({
68
75
  }
69
76
  async verify(token, options) {
70
77
  if (!token && this._clientName === 'access') token = this.scope.service.jwtExtract.fromAllWays();
78
+ const [res, error] = await catchError(() => {
79
+ return this._verifyInner(token, options);
80
+ });
81
+ this.$loggerChild('jwt').debug(() => `jwt.verify: client:${this._clientName}, token:${token}${error ? `, error: ${error.message}` : ''}`);
82
+ if (error) throw error;
83
+ return res;
84
+ }
85
+ async _verifyInner(token, options) {
71
86
  if (!token) return undefined;
72
87
  return new Promise((resolve, reject) => {
73
88
  this._jwtInstance.verify(token, this._clientOptions.secret, this._clientOptions.verifyOptions, (err, decoded) => {
74
- if (err) return reject(err);
89
+ if (err) {
90
+ return reject(err);
91
+ }
75
92
  const payload = cast(decoded);
76
93
  // check field client
77
94
  if (payload[this.fieldClient] !== this._clientName) return this.app.throw(401);
@@ -88,12 +105,12 @@ let ServiceJwtClient = (_dec$4 = Service(), _dec2$4 = BeanInfo({
88
105
  if (Array.isArray(pathTarget) && !pathTarget.includes(path)) return false;
89
106
  return pathTarget === path;
90
107
  }
91
- }) || _class$4) || _class$4);
92
-
93
- var _dec$3, _dec2$3, _class$3;
94
- let BeanJwt = (_dec$3 = Bean(), _dec2$3 = BeanInfo({
108
+ };
109
+ ServiceJwtClient = __decorate([Service(), BeanInfo({
95
110
  module: "a-jwt"
96
- }), _dec$3(_class$3 = _dec2$3(_class$3 = class BeanJwt extends BeanBase {
111
+ })], ServiceJwtClient);
112
+
113
+ let BeanJwt = class BeanJwt extends BeanBase {
97
114
  get(clientName) {
98
115
  return this.app.bean._getBeanSelector(ServiceJwtClient, clientName);
99
116
  }
@@ -129,7 +146,10 @@ let BeanJwt = (_dec$3 = Bean(), _dec2$3 = BeanInfo({
129
146
  extractAuthTokenFromAllWays() {
130
147
  return this.scope.service.jwtExtract.fromAllWays();
131
148
  }
132
- }) || _class$3) || _class$3);
149
+ };
150
+ BeanJwt = __decorate([Bean(), BeanInfo({
151
+ module: "a-jwt"
152
+ })], BeanJwt);
133
153
 
134
154
  const re = /(\S+)\s+(\S+)/;
135
155
  function parseAuthHeader(headerValue) {
@@ -141,10 +161,7 @@ function parseAuthHeader(headerValue) {
141
161
  };
142
162
  }
143
163
 
144
- var _dec$2, _dec2$2, _class$2;
145
- let ServiceJwtExtract = (_dec$2 = Service(), _dec2$2 = BeanInfo({
146
- module: "a-jwt"
147
- }), _dec$2(_class$2 = _dec2$2(_class$2 = class ServiceJwtExtract extends BeanBase {
164
+ let ServiceJwtExtract = class ServiceJwtExtract extends BeanBase {
148
165
  fromHeader() {
149
166
  if (!this.scope.config.field.extract.header) return;
150
167
  return this.ctx.request.headers[this.scope.config.field.extract.header];
@@ -168,50 +185,24 @@ let ServiceJwtExtract = (_dec$2 = Service(), _dec2$2 = BeanInfo({
168
185
  if (!token) token = this.fromCookie();
169
186
  return token;
170
187
  }
171
- }) || _class$2) || _class$2);
172
-
173
- function _applyDecoratedDescriptor(i, e, r, n, l) {
174
- var a = {};
175
- return Object.keys(n).forEach(function (i) {
176
- a[i] = n[i];
177
- }), a.enumerable = !!a.enumerable, a.configurable = !!a.configurable, ("value" in a || a.initializer) && (a.writable = true), a = r.slice().reverse().reduce(function (r, n) {
178
- return n(i, e, r) || r;
179
- }, a), void 0 === a.initializer ? (Object.defineProperty(i, e, a), null) : a;
180
- }
181
- function _initializerDefineProperty(e, i, r, l) {
182
- r && Object.defineProperty(e, i, {
183
- enumerable: r.enumerable,
184
- configurable: r.configurable,
185
- writable: r.writable,
186
- value: r.initializer ? r.initializer.call(l) : void 0
187
- });
188
- }
189
-
190
- var _dec$1, _dec2$1, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _class$1, _class2, _descriptor, _descriptor2, _descriptor3;
191
- let DtoJwtToken = (_dec$1 = Dto(), _dec2$1 = BeanInfo({
188
+ };
189
+ ServiceJwtExtract = __decorate([Service(), BeanInfo({
192
190
  module: "a-jwt"
193
- }), _dec3 = Api.field(), _dec4 = Reflect.metadata("design:type", String), _dec5 = Api.field(), _dec6 = Reflect.metadata("design:type", String), _dec7 = Api.field(), _dec8 = Reflect.metadata("design:type", Number), _dec$1(_class$1 = _dec2$1(_class$1 = (_class2 = class DtoJwtToken {
191
+ })], ServiceJwtExtract);
192
+
193
+ let DtoJwtToken = class DtoJwtToken {
194
194
  constructor() {
195
- _initializerDefineProperty(this, "accessToken", _descriptor, this);
196
- _initializerDefineProperty(this, "refreshToken", _descriptor2, this);
197
- _initializerDefineProperty(this, "expiresIn", _descriptor3, this);
195
+ this.accessToken = void 0;
196
+ this.refreshToken = void 0;
197
+ this.expiresIn = void 0;
198
198
  }
199
- }, _descriptor = _applyDecoratedDescriptor(_class2.prototype, "accessToken", [_dec3, _dec4], {
200
- configurable: true,
201
- enumerable: true,
202
- writable: true,
203
- initializer: null
204
- }), _descriptor2 = _applyDecoratedDescriptor(_class2.prototype, "refreshToken", [_dec5, _dec6], {
205
- configurable: true,
206
- enumerable: true,
207
- writable: true,
208
- initializer: null
209
- }), _descriptor3 = _applyDecoratedDescriptor(_class2.prototype, "expiresIn", [_dec7, _dec8], {
210
- configurable: true,
211
- enumerable: true,
212
- writable: true,
213
- initializer: null
214
- }), _class2)) || _class$1) || _class$1);
199
+ };
200
+ __decorate([Api.field(), __metadata("design:type", String)], DtoJwtToken.prototype, "accessToken", void 0);
201
+ __decorate([Api.field(), __metadata("design:type", String)], DtoJwtToken.prototype, "refreshToken", void 0);
202
+ __decorate([Api.field(), __metadata("design:type", Number)], DtoJwtToken.prototype, "expiresIn", void 0);
203
+ DtoJwtToken = __decorate([Dto(), BeanInfo({
204
+ module: "a-jwt"
205
+ })], DtoJwtToken);
215
206
 
216
207
  function config(_app, env) {
217
208
  return {
@@ -273,11 +264,10 @@ function config(_app, env) {
273
264
  };
274
265
  }
275
266
 
276
- var _dec, _dec2, _class;
277
- let ScopeModuleAJwt = (_dec = Scope(), _dec2 = BeanInfo({
267
+ let ScopeModuleAJwt = class ScopeModuleAJwt extends BeanScopeBase {};
268
+ ScopeModuleAJwt = __decorate([Scope(), BeanInfo({
278
269
  module: "a-jwt"
279
- }), _dec(_class = _dec2(_class = class ScopeModuleAJwt extends BeanScopeBase {}) || _class) || _class);
280
-
270
+ })], ScopeModuleAJwt);
281
271
  /** scope: end */
282
272
 
283
273
  const ErrorMessageJwtExpired = 'jwt expired';
@@ -12,6 +12,8 @@ export declare class ServiceJwtClient extends BeanBase {
12
12
  private get fieldPath();
13
13
  private get fieldData();
14
14
  sign(payloadData: IPayloadData, options?: IJwtSignOptions): Promise<string>;
15
+ private _signInner;
15
16
  verify(token?: string, options?: IJwtVerifyOptions): Promise<IPayloadData | undefined>;
17
+ private _verifyInner;
16
18
  _checkVerifyPath(pathTarget: string | string[] | undefined, pathReal: string | undefined): boolean;
17
19
  }
@@ -1,5 +1,6 @@
1
1
  import type { SignOptions, VerifyOptions } from 'jsonwebtoken';
2
2
  import type { StringValue } from 'ms';
3
+ import 'vona';
3
4
  export declare const ErrorMessageJwtExpired = "jwt expired";
4
5
  export interface IJwtToken {
5
6
  accessToken: string;
@@ -53,3 +54,8 @@ export interface IPayloadData {
53
54
  }
54
55
  export interface IJwtPayload {
55
56
  }
57
+ declare module 'vona' {
58
+ interface ILoggerChildRecord {
59
+ jwt: never;
60
+ }
61
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vona-module-a-jwt",
3
3
  "type": "module",
4
- "version": "5.0.29",
4
+ "version": "5.0.30",
5
5
  "title": "a-jwt",
6
6
  "vonaModule": {
7
7
  "dependencies": {}