vona-module-a-jwt 5.0.14 → 5.0.16

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.
@@ -1,3 +1,4 @@
1
+ import type { TypeSymbolKeyFieldsMore } from 'vona-module-a-orm';
1
2
  import type { TypeEntityOptionsFields } from 'vona-module-a-openapi';
2
3
  /** bean: begin */
3
4
  export * from '../bean/bean.jwt.ts';
@@ -18,31 +19,11 @@ declare module 'vona' {
18
19
  }
19
20
  }
20
21
  /** bean: end */
21
- /** dto: begin */
22
- export * from '../dto/jwtToken.ts';
23
- import type { IDtoOptionsJwtToken } from '../dto/jwtToken.ts';
24
- import 'vona';
25
- declare module 'vona-module-a-web' {
26
- interface IDtoRecord {
27
- 'a-jwt:jwtToken': Omit<IDtoOptionsJwtToken, '_fieldsMore_'>;
28
- }
29
- }
30
- declare module 'vona-module-a-jwt' {
31
- }
32
- /** dto: end */
33
- /** dto: begin */
34
- import type { DtoJwtToken } from '../dto/jwtToken.ts';
35
- declare module 'vona-module-a-jwt' {
36
- interface IDtoOptionsJwtToken {
37
- fields?: TypeEntityOptionsFields<DtoJwtToken, IDtoOptionsJwtToken['_fieldsMore_']>;
38
- }
39
- }
40
- /** dto: end */
41
22
  /** service: begin */
42
23
  export * from '../service/jwtClient.ts';
43
24
  export * from '../service/jwtExtract.ts';
44
25
  import 'vona';
45
- declare module 'vona-module-a-web' {
26
+ declare module 'vona-module-a-bean' {
46
27
  interface IServiceRecord {
47
28
  'a-jwt:jwtClient': never;
48
29
  'a-jwt:jwtExtract': never;
@@ -51,8 +32,16 @@ declare module 'vona-module-a-web' {
51
32
  declare module 'vona-module-a-jwt' {
52
33
  interface ServiceJwtClient {
53
34
  }
35
+ interface ServiceJwtClient {
36
+ get $beanFullName(): 'a-jwt.service.jwtClient';
37
+ get $onionName(): 'a-jwt:jwtClient';
38
+ }
54
39
  interface ServiceJwtExtract {
55
40
  }
41
+ interface ServiceJwtExtract {
42
+ get $beanFullName(): 'a-jwt.service.jwtExtract';
43
+ get $onionName(): 'a-jwt:jwtExtract';
44
+ }
56
45
  }
57
46
  /** service: end */
58
47
  /** service: begin */
@@ -72,6 +61,26 @@ declare module 'vona' {
72
61
  }
73
62
  }
74
63
  /** service: end */
64
+ /** dto: begin */
65
+ export * from '../dto/jwtToken.ts';
66
+ import type { IDtoOptionsJwtToken } from '../dto/jwtToken.ts';
67
+ import 'vona';
68
+ declare module 'vona-module-a-web' {
69
+ interface IDtoRecord {
70
+ 'a-jwt:jwtToken': IDtoOptionsJwtToken;
71
+ }
72
+ }
73
+ declare module 'vona-module-a-jwt' {
74
+ }
75
+ /** dto: end */
76
+ /** dto: begin */
77
+ import type { DtoJwtToken } from '../dto/jwtToken.ts';
78
+ declare module 'vona-module-a-jwt' {
79
+ interface IDtoOptionsJwtToken {
80
+ fields?: TypeEntityOptionsFields<DtoJwtToken, IDtoOptionsJwtToken[TypeSymbolKeyFieldsMore]>;
81
+ }
82
+ }
83
+ /** dto: end */
75
84
  /** config: begin */
76
85
  export * from '../config/config.ts';
77
86
  import type { config } from '../config/config.ts';
@@ -4,8 +4,8 @@ import { ServiceJwtClient } from '../service/jwtClient.ts';
4
4
  export declare class BeanJwt extends BeanBase {
5
5
  get(clientName?: keyof IJwtClientRecord): ServiceJwtClient;
6
6
  create(payloadData: IPayloadDataBase, options?: IJwtSignOptions): Promise<IJwtToken>;
7
- createTemp(payloadData: IPayloadDataBase, options?: IJwtSignOptions): Promise<string>;
8
- createOauth(payloadData: IPayloadDataBase, options?: IJwtSignOptions): Promise<string>;
7
+ createTempAuthToken(payloadData: IPayloadDataBase, options?: IJwtSignOptions): Promise<string>;
8
+ createOauthAuthToken(payloadData: IPayloadDataBase, options?: IJwtSignOptions): Promise<string>;
9
9
  createOauthState(payloadData: IPayloadDataBase, options?: IJwtSignOptions): Promise<string>;
10
10
  createOauthCode(payloadData: IPayloadDataBase, options?: IJwtSignOptions): Promise<string>;
11
11
  }
package/dist/index.js CHANGED
@@ -1,9 +1,9 @@
1
1
  import { BeanInfo, BeanBase, deepExtend, cast, BeanScopeBase } from 'vona';
2
2
  import ms from 'ms';
3
- import { Bean, Scope } from 'vona-module-a-bean';
3
+ import { Service, Bean, Scope } from 'vona-module-a-bean';
4
4
  import jwt from 'jsonwebtoken';
5
- import { Service, Dto } from 'vona-module-a-web';
6
5
  import { Api } from 'vona-module-a-openapi';
6
+ import { Dto } from 'vona-module-a-web';
7
7
 
8
8
  var _dec$4, _dec2$4, _class$4;
9
9
  let ServiceJwtClient = (_dec$4 = Service(), _dec2$4 = BeanInfo({
@@ -66,7 +66,7 @@ let ServiceJwtClient = (_dec$4 = Service(), _dec2$4 = BeanInfo({
66
66
  });
67
67
  });
68
68
  }
69
- async verify(token) {
69
+ async verify(token, options) {
70
70
  if (!token && this._clientName === 'access') token = this.scope.service.jwtExtract.fromAllWays();
71
71
  if (!token) return undefined;
72
72
  return new Promise((resolve, reject) => {
@@ -76,12 +76,18 @@ let ServiceJwtClient = (_dec$4 = Service(), _dec2$4 = BeanInfo({
76
76
  // check field client
77
77
  if (payload[this.fieldClient] !== this._clientName) return this.app.throw(401);
78
78
  // check field path
79
- if (payload[this.fieldPath] && payload[this.fieldPath] !== this.ctx.route.routePathRaw) return this.app.throw(401);
79
+ if (!this._checkVerifyPath(payload[this.fieldPath], options?.path)) return this.app.throw(401);
80
80
  // passed
81
81
  resolve(payload[this.fieldData]);
82
82
  });
83
83
  });
84
84
  }
85
+ _checkVerifyPath(pathTarget, pathReal) {
86
+ if (!pathTarget) return true;
87
+ const path = pathReal ?? String(this.ctx.route.routePathRaw);
88
+ if (Array.isArray(pathTarget) && !pathTarget.includes(path)) return false;
89
+ return pathTarget === path;
90
+ }
85
91
  }) || _class$4) || _class$4);
86
92
 
87
93
  var _dec$3, _dec2$3, _class$3;
@@ -106,12 +112,12 @@ let BeanJwt = (_dec$3 = Bean(), _dec2$3 = BeanInfo({
106
112
  expiresIn
107
113
  };
108
114
  }
109
- async createTemp(payloadData, options) {
115
+ async createTempAuthToken(payloadData, options) {
110
116
  return await this.get('access').sign(payloadData, Object.assign({}, options, {
111
117
  temp: true
112
118
  }));
113
119
  }
114
- async createOauth(payloadData, options) {
120
+ async createOauthAuthToken(payloadData, options) {
115
121
  return await this.get('oauth').sign(payloadData, options);
116
122
  }
117
123
  async createOauthState(payloadData, options) {
@@ -122,6 +128,45 @@ let BeanJwt = (_dec$3 = Bean(), _dec2$3 = BeanInfo({
122
128
  }
123
129
  }) || _class$3) || _class$3);
124
130
 
131
+ const re = /(\S+)\s+(\S+)/;
132
+ function parseAuthHeader(headerValue) {
133
+ if (typeof headerValue !== 'string') return;
134
+ const matches = headerValue.match(re);
135
+ return matches && {
136
+ scheme: matches[1],
137
+ value: matches[2]
138
+ };
139
+ }
140
+
141
+ var _dec$2, _dec2$2, _class$2;
142
+ let ServiceJwtExtract = (_dec$2 = Service(), _dec2$2 = BeanInfo({
143
+ module: "a-jwt"
144
+ }), _dec$2(_class$2 = _dec2$2(_class$2 = class ServiceJwtExtract extends BeanBase {
145
+ fromHeader() {
146
+ if (!this.scope.config.field.extract.header) return;
147
+ return this.ctx.request.headers[this.scope.config.field.extract.header];
148
+ }
149
+ fromQuery() {
150
+ return this.ctx.request.query[this.scope.config.field.extract.query];
151
+ }
152
+ fromAuthHeaderWithScheme() {
153
+ const headerValue = this.ctx.request.headers[this.scope.config.field.extract.headerAuth];
154
+ const auth = parseAuthHeader(headerValue);
155
+ if (!auth || auth.scheme.toLocaleLowerCase() !== this.scope.config.field.extract.headerAuthScheme.toLocaleLowerCase()) return;
156
+ return auth.value;
157
+ }
158
+ fromCookie() {
159
+ return this.ctx.cookies.get(this.scope.config.field.extract.cookie);
160
+ }
161
+ fromAllWays() {
162
+ let token = this.fromQuery();
163
+ if (!token) token = this.fromAuthHeaderWithScheme();
164
+ if (!token) token = this.fromHeader();
165
+ if (!token) token = this.fromCookie();
166
+ return token;
167
+ }
168
+ }) || _class$2) || _class$2);
169
+
125
170
  function _applyDecoratedDescriptor(i, e, r, n, l) {
126
171
  var a = {};
127
172
  return Object.keys(n).forEach(function (i) {
@@ -139,10 +184,10 @@ function _initializerDefineProperty(e, i, r, l) {
139
184
  });
140
185
  }
141
186
 
142
- var _dec$2, _dec2$2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _class$2, _class2, _descriptor, _descriptor2, _descriptor3;
143
- let DtoJwtToken = (_dec$2 = Dto(), _dec2$2 = BeanInfo({
187
+ var _dec$1, _dec2$1, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _class$1, _class2, _descriptor, _descriptor2, _descriptor3;
188
+ let DtoJwtToken = (_dec$1 = Dto(), _dec2$1 = BeanInfo({
144
189
  module: "a-jwt"
145
- }), _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$2(_class$2 = _dec2$2(_class$2 = (_class2 = class DtoJwtToken {
190
+ }), _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 {
146
191
  constructor() {
147
192
  _initializerDefineProperty(this, "accessToken", _descriptor, this);
148
193
  _initializerDefineProperty(this, "refreshToken", _descriptor2, this);
@@ -163,46 +208,7 @@ let DtoJwtToken = (_dec$2 = Dto(), _dec2$2 = BeanInfo({
163
208
  enumerable: true,
164
209
  writable: true,
165
210
  initializer: null
166
- }), _class2)) || _class$2) || _class$2);
167
-
168
- const re = /(\S+)\s+(\S+)/;
169
- function parseAuthHeader(headerValue) {
170
- if (typeof headerValue !== 'string') return;
171
- const matches = headerValue.match(re);
172
- return matches && {
173
- scheme: matches[1],
174
- value: matches[2]
175
- };
176
- }
177
-
178
- var _dec$1, _dec2$1, _class$1;
179
- let ServiceJwtExtract = (_dec$1 = Service(), _dec2$1 = BeanInfo({
180
- module: "a-jwt"
181
- }), _dec$1(_class$1 = _dec2$1(_class$1 = class ServiceJwtExtract extends BeanBase {
182
- fromHeader() {
183
- if (!this.scope.config.field.extract.header) return;
184
- return this.ctx.request.headers[this.scope.config.field.extract.header];
185
- }
186
- fromQuery() {
187
- return this.ctx.request.query[this.scope.config.field.extract.query];
188
- }
189
- fromAuthHeaderWithScheme() {
190
- const headerValue = this.ctx.request.headers[this.scope.config.field.extract.headerAuth];
191
- const auth = parseAuthHeader(headerValue);
192
- if (!auth || auth.scheme.toLocaleLowerCase() !== this.scope.config.field.extract.headerAuthScheme.toLocaleLowerCase()) return;
193
- return auth.value;
194
- }
195
- fromCookie() {
196
- return this.ctx.cookies.get(this.scope.config.field.extract.cookie);
197
- }
198
- fromAllWays() {
199
- let token = this.fromQuery();
200
- if (!token) token = this.fromAuthHeaderWithScheme();
201
- if (!token) token = this.fromHeader();
202
- if (!token) token = this.fromCookie();
203
- return token;
204
- }
205
- }) || _class$1) || _class$1);
211
+ }), _class2)) || _class$1) || _class$1);
206
212
 
207
213
  function config(app) {
208
214
  return {
@@ -1,4 +1,4 @@
1
- import type { IJwtClientRecord, IJwtSignOptions, IPayloadDataBase } from '../types/jwt.ts';
1
+ import type { IJwtClientRecord, IJwtSignOptions, IJwtVerifyOptions, IPayloadDataBase } from '../types/jwt.ts';
2
2
  import jwt from 'jsonwebtoken';
3
3
  import { BeanBase } from 'vona';
4
4
  export declare class ServiceJwtClient extends BeanBase {
@@ -12,5 +12,6 @@ export declare class ServiceJwtClient extends BeanBase {
12
12
  private get fieldPath();
13
13
  private get fieldData();
14
14
  sign(payloadData: IPayloadDataBase, options?: IJwtSignOptions): Promise<string>;
15
- verify(token?: string): Promise<IPayloadDataBase | undefined>;
15
+ verify(token?: string, options?: IJwtVerifyOptions): Promise<IPayloadDataBase | undefined>;
16
+ _checkVerifyPath(pathTarget: string | string[] | undefined, pathReal: string | undefined): boolean;
16
17
  }
@@ -17,6 +17,9 @@ export interface IJwtSignOptions {
17
17
  dev?: boolean;
18
18
  temp?: boolean;
19
19
  }
20
+ export interface IJwtVerifyOptions {
21
+ path?: string;
22
+ }
20
23
  export interface IJwtClientOptions {
21
24
  secret?: string;
22
25
  signOptions: SignOptions;
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.14",
4
+ "version": "5.0.16",
5
5
  "title": "a-jwt",
6
6
  "vonaModule": {
7
7
  "dependencies": {}