vona-module-a-jwt 5.0.9 → 5.0.11

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.
@@ -0,0 +1,101 @@
1
+ import type { BeanScopeUtil, TypeModuleConfig } from 'vona';
2
+ import type { TypeEntityOptionsFields } from 'vona-module-a-openapi';
3
+ /** bean: end */
4
+ /** bean: begin */
5
+ import type { BeanJwt } from '../bean/bean.jwt.ts';
6
+ import type { config } from '../config/config.ts';
7
+ import type { IDtoOptionsJwtToken } from '../dto/jwtToken.ts';
8
+ /** dto: end */
9
+ /** dto: begin */
10
+ import type { DtoJwtToken } from '../dto/jwtToken.ts';
11
+ /** service: end */
12
+ /** service: begin */
13
+ import type { ServiceJwtClient } from '../service/jwtClient.ts';
14
+ import type { ServiceJwtExtract } from '../service/jwtExtract.ts';
15
+ /** config: end */
16
+ /** scope: begin */
17
+ import { BeanScopeBase } from 'vona';
18
+ import 'vona';
19
+ /** service: end */
20
+ /** service: begin */
21
+ import 'vona';
22
+ import 'vona';
23
+ import 'vona';
24
+ import 'vona';
25
+ import 'vona';
26
+ /** bean: begin */
27
+ export * from '../bean/bean.jwt.ts';
28
+ declare module 'vona' {
29
+ }
30
+ declare module 'vona-module-a-jwt' {
31
+ interface BeanJwt {
32
+ }
33
+ }
34
+ declare module 'vona' {
35
+ interface IBeanRecordGlobal {
36
+ jwt: BeanJwt;
37
+ }
38
+ }
39
+ /** service: end */
40
+ /** config: begin */
41
+ export * from '../config/config.ts';
42
+ declare module 'vona-module-a-web' {
43
+ interface IDtoRecord {
44
+ 'a-jwt:jwtToken': IDtoOptionsJwtToken;
45
+ }
46
+ }
47
+ declare module 'vona-module-a-jwt' {
48
+ }
49
+ declare module 'vona-module-a-jwt' {
50
+ interface IDtoOptionsJwtToken {
51
+ fields?: TypeEntityOptionsFields<DtoJwtToken, IDtoOptionsJwtToken['fieldsMore']>;
52
+ }
53
+ }
54
+ /** bean: end */
55
+ /** dto: begin */
56
+ export * from '../dto/jwtToken.ts';
57
+ /** dto: end */
58
+ /** service: begin */
59
+ export * from '../service/jwtClient.ts';
60
+ declare module 'vona-module-a-web' {
61
+ interface IServiceRecord {
62
+ 'a-jwt:jwtClient': never;
63
+ 'a-jwt:jwtExtract': never;
64
+ }
65
+ }
66
+ declare module 'vona-module-a-jwt' {
67
+ interface ServiceJwtClient {
68
+ }
69
+ interface ServiceJwtExtract {
70
+ }
71
+ }
72
+ export interface IModuleService {
73
+ jwtClient: ServiceJwtClient;
74
+ jwtExtract: ServiceJwtExtract;
75
+ }
76
+ declare module 'vona' {
77
+ interface IBeanRecordGeneral {
78
+ 'a-jwt.service.jwtClient': ServiceJwtClient;
79
+ 'a-jwt.service.jwtExtract': ServiceJwtExtract;
80
+ }
81
+ }
82
+ export * from '../service/jwtExtract.ts';
83
+ export declare class ScopeModuleAJwt extends BeanScopeBase {
84
+ }
85
+ export interface ScopeModuleAJwt {
86
+ util: BeanScopeUtil;
87
+ config: TypeModuleConfig<typeof config>;
88
+ service: IModuleService;
89
+ }
90
+ declare module 'vona' {
91
+ interface IBeanScopeRecord {
92
+ 'a-jwt': ScopeModuleAJwt;
93
+ }
94
+ interface IBeanScopeContainer {
95
+ jwt: ScopeModuleAJwt;
96
+ }
97
+ interface IBeanScopeConfig {
98
+ 'a-jwt': ReturnType<typeof config>;
99
+ }
100
+ }
101
+ /** scope: end */
@@ -0,0 +1,2 @@
1
+ export declare const __ThisModule__ = "a-jwt";
2
+ export { ScopeModuleAJwt as ScopeModule } from './index.ts';
@@ -0,0 +1,11 @@
1
+ import type { IJwtClientRecord, IJwtSignOptions, IJwtToken, IPayloadDataBase } from '../types/jwt.ts';
2
+ import { BeanBase } from 'vona';
3
+ import { ServiceJwtClient } from '../service/jwtClient.ts';
4
+ export declare class BeanJwt extends BeanBase {
5
+ get(clientName?: keyof IJwtClientRecord): ServiceJwtClient;
6
+ create(payloadData: IPayloadDataBase, options?: IJwtSignOptions): Promise<IJwtToken>;
7
+ createTemp(payloadData: IPayloadDataBase, options?: IJwtSignOptions): Promise<string>;
8
+ createOauth(payloadData: IPayloadDataBase, options?: IJwtSignOptions): Promise<string>;
9
+ createOauthState(payloadData: IPayloadDataBase, options?: IJwtSignOptions): Promise<string>;
10
+ createOauthCode(payloadData: IPayloadDataBase, options?: IJwtSignOptions): Promise<string>;
11
+ }
@@ -0,0 +1,3 @@
1
+ import type { VonaApplication } from 'vona';
2
+ import type { ConfigJwt } from '../types/jwt.ts';
3
+ export declare function config(app: VonaApplication): ConfigJwt;
@@ -0,0 +1,9 @@
1
+ import type { IDecoratorDtoOptions } from 'vona-module-a-web';
2
+ import type { IJwtToken } from '../types/jwt.ts';
3
+ export interface IDtoOptionsJwtToken extends IDecoratorDtoOptions {
4
+ }
5
+ export declare class DtoJwtToken implements IJwtToken {
6
+ accessToken: string;
7
+ refreshToken: string;
8
+ expiresIn: number;
9
+ }
@@ -0,0 +1,3 @@
1
+ export * from './.metadata/index.ts';
2
+ export * from './lib/index.ts';
3
+ export * from './types/index.ts';
package/dist/index.js ADDED
@@ -0,0 +1,271 @@
1
+ import { BeanInfo, BeanBase, deepExtend, cast, BeanScopeBase } from 'vona';
2
+ import { Bean, Scope } from 'vona-module-a-bean';
3
+ import ms from 'ms';
4
+ import jwt from 'jsonwebtoken';
5
+ import { Service, Dto } from 'vona-module-a-web';
6
+ import { Api } from 'vona-module-a-openapi';
7
+
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 {
12
+ constructor(...args) {
13
+ super(...args);
14
+ this._jwtInstance = void 0;
15
+ this._clientName = void 0;
16
+ this._clientOptions = void 0;
17
+ }
18
+ get instance() {
19
+ return this._jwtInstance;
20
+ }
21
+ __init__(clientName) {
22
+ this._createClient(clientName);
23
+ }
24
+ _createClient(clientName) {
25
+ clientName = clientName || 'access';
26
+ const configJwt = this.scope.config;
27
+ const configClient = configJwt.clients[clientName];
28
+ if (!configClient) throw new Error(`jwt client not found: ${clientName}`);
29
+ const secret = configJwt.default.secret ?? this.app.config.server.keys[0];
30
+ this._clientOptions = deepExtend({}, configJwt.default, {
31
+ secret
32
+ }, configClient);
33
+ this._clientName = clientName;
34
+ this._jwtInstance = jwt;
35
+ }
36
+ get fieldClient() {
37
+ return this.scope.config.field.payload.client;
38
+ }
39
+ get fieldPath() {
40
+ return this.scope.config.field.payload.path;
41
+ }
42
+ get fieldData() {
43
+ return this.scope.config.field.payload.data;
44
+ }
45
+ async sign(payloadData, options) {
46
+ return new Promise((resolve, reject) => {
47
+ const payload = {
48
+ [this.fieldClient]: this._clientName,
49
+ [this.fieldData]: payloadData
50
+ };
51
+ if (options?.path) payload[this.fieldPath] = options.path;
52
+ let signOptions = this._clientOptions.signOptions;
53
+ if (options?.dev) {
54
+ signOptions = Object.assign({}, signOptions, {
55
+ expiresIn: this.scope.config.clients.refresh.signOptions.expiresIn
56
+ });
57
+ }
58
+ if (options?.temp) {
59
+ signOptions = Object.assign({}, signOptions, {
60
+ expiresIn: this.scope.config.tempToken.signOptions.expiresIn
61
+ });
62
+ }
63
+ this._jwtInstance.sign(payload, this._clientOptions.secret, signOptions, (err, encoded) => {
64
+ if (err) return reject(err);
65
+ resolve(encoded);
66
+ });
67
+ });
68
+ }
69
+ async verify(token) {
70
+ if (!token && this._clientName === 'access') token = this.scope.service.jwtExtract.fromAllWays();
71
+ if (!token) return undefined;
72
+ return new Promise((resolve, reject) => {
73
+ this._jwtInstance.verify(token, this._clientOptions.secret, this._clientOptions.signOptions, (err, decoded) => {
74
+ if (err) return reject(err);
75
+ const payload = cast(decoded);
76
+ // check field client
77
+ if (payload[this.fieldClient] !== this._clientName) return this.app.throw(401);
78
+ // check field path
79
+ if (payload[this.fieldPath] && payload[this.fieldPath] !== this.ctx.route.routePathRaw) return this.app.throw(401);
80
+ // passed
81
+ resolve(payload[this.fieldData]);
82
+ });
83
+ });
84
+ }
85
+ }) || _class$4) || _class$4);
86
+
87
+ var _dec$3, _dec2$3, _class$3;
88
+ let BeanJwt = (_dec$3 = Bean(), _dec2$3 = BeanInfo({
89
+ module: "a-jwt"
90
+ }), _dec$3(_class$3 = _dec2$3(_class$3 = class BeanJwt extends BeanBase {
91
+ get(clientName) {
92
+ return this.app.bean._getBeanSelector(ServiceJwtClient, clientName);
93
+ }
94
+ async create(payloadData, options) {
95
+ // accessToken
96
+ const accessToken = await this.get('access').sign(payloadData, options);
97
+ // refreshToken
98
+ const refreshToken = await this.get('refresh').sign(payloadData, options);
99
+ // expiresIn
100
+ let expiresIn = this.scope.config.clients.access.signOptions.expiresIn;
101
+ if (typeof expiresIn === 'string') expiresIn = ms(expiresIn);
102
+ // ok
103
+ return {
104
+ accessToken,
105
+ refreshToken,
106
+ expiresIn
107
+ };
108
+ }
109
+ async createTemp(payloadData, options) {
110
+ return await this.get('access').sign(payloadData, Object.assign({}, options, {
111
+ temp: true
112
+ }));
113
+ }
114
+ async createOauth(payloadData, options) {
115
+ return await this.get('oauth').sign(payloadData, options);
116
+ }
117
+ async createOauthState(payloadData, options) {
118
+ return await this.get('oauthstate').sign(payloadData, options);
119
+ }
120
+ async createOauthCode(payloadData, options) {
121
+ return await this.get('code').sign(payloadData, options);
122
+ }
123
+ }) || _class$3) || _class$3);
124
+
125
+ function config(app) {
126
+ return {
127
+ field: {
128
+ payload: {
129
+ client: 'client',
130
+ path: 'path',
131
+ data: 'data'
132
+ },
133
+ extract: {
134
+ header: '',
135
+ headerAuth: 'authorization',
136
+ headerAuthScheme: 'bearer',
137
+ query: 'auth_token',
138
+ cookie: 'token'
139
+ }
140
+ },
141
+ tempToken: {
142
+ signOptions: {
143
+ expiresIn: 10 * 60
144
+ }
145
+ },
146
+ default: {
147
+ secret: undefined,
148
+ signOptions: {
149
+ issuer: app.meta.env.APP_NAME
150
+ }
151
+ },
152
+ clients: {
153
+ access: {
154
+ signOptions: {
155
+ expiresIn: 2 * 60 * 60
156
+ }
157
+ },
158
+ refresh: {
159
+ signOptions: {
160
+ expiresIn: 7 * 24 * 60 * 60
161
+ }
162
+ },
163
+ oauth: {
164
+ signOptions: {
165
+ expiresIn: 5 * 60
166
+ }
167
+ },
168
+ oauthstate: {
169
+ signOptions: {
170
+ expiresIn: 5 * 60
171
+ }
172
+ },
173
+ code: {
174
+ signOptions: {
175
+ expiresIn: 3 * 60
176
+ }
177
+ }
178
+ }
179
+ };
180
+ }
181
+
182
+ function _applyDecoratedDescriptor(i, e, r, n, l) {
183
+ var a = {};
184
+ return Object.keys(n).forEach(function (i) {
185
+ a[i] = n[i];
186
+ }), a.enumerable = !!a.enumerable, a.configurable = !!a.configurable, ("value" in a || a.initializer) && (a.writable = true), a = r.slice().reverse().reduce(function (r, n) {
187
+ return n(i, e, r) || r;
188
+ }, a), void 0 === a.initializer ? (Object.defineProperty(i, e, a), null) : a;
189
+ }
190
+ function _initializerDefineProperty(e, i, r, l) {
191
+ r && Object.defineProperty(e, i, {
192
+ enumerable: r.enumerable,
193
+ configurable: r.configurable,
194
+ writable: r.writable,
195
+ value: r.initializer ? r.initializer.call(l) : void 0
196
+ });
197
+ }
198
+
199
+ var _dec$2, _dec2$2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _class$2, _class2, _descriptor, _descriptor2, _descriptor3;
200
+ let DtoJwtToken = (_dec$2 = Dto(), _dec2$2 = BeanInfo({
201
+ module: "a-jwt"
202
+ }), _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 {
203
+ constructor() {
204
+ _initializerDefineProperty(this, "accessToken", _descriptor, this);
205
+ _initializerDefineProperty(this, "refreshToken", _descriptor2, this);
206
+ _initializerDefineProperty(this, "expiresIn", _descriptor3, this);
207
+ }
208
+ }, _descriptor = _applyDecoratedDescriptor(_class2.prototype, "accessToken", [_dec3, _dec4], {
209
+ configurable: true,
210
+ enumerable: true,
211
+ writable: true,
212
+ initializer: null
213
+ }), _descriptor2 = _applyDecoratedDescriptor(_class2.prototype, "refreshToken", [_dec5, _dec6], {
214
+ configurable: true,
215
+ enumerable: true,
216
+ writable: true,
217
+ initializer: null
218
+ }), _descriptor3 = _applyDecoratedDescriptor(_class2.prototype, "expiresIn", [_dec7, _dec8], {
219
+ configurable: true,
220
+ enumerable: true,
221
+ writable: true,
222
+ initializer: null
223
+ }), _class2)) || _class$2) || _class$2);
224
+
225
+ const re = /(\S+)\s+(\S+)/;
226
+ function parseAuthHeader(headerValue) {
227
+ if (typeof headerValue !== 'string') return;
228
+ const matches = headerValue.match(re);
229
+ return matches && {
230
+ scheme: matches[1],
231
+ value: matches[2]
232
+ };
233
+ }
234
+
235
+ var _dec$1, _dec2$1, _class$1;
236
+ let ServiceJwtExtract = (_dec$1 = Service(), _dec2$1 = BeanInfo({
237
+ module: "a-jwt"
238
+ }), _dec$1(_class$1 = _dec2$1(_class$1 = class ServiceJwtExtract extends BeanBase {
239
+ fromHeader() {
240
+ if (!this.scope.config.field.extract.header) return;
241
+ return this.ctx.request.headers[this.scope.config.field.extract.header];
242
+ }
243
+ fromQuery() {
244
+ return this.ctx.request.query[this.scope.config.field.extract.query];
245
+ }
246
+ fromAuthHeaderWithScheme() {
247
+ const headerValue = this.ctx.request.headers[this.scope.config.field.extract.headerAuth];
248
+ const auth = parseAuthHeader(headerValue);
249
+ if (!auth || auth.scheme.toLocaleLowerCase() !== this.scope.config.field.extract.headerAuthScheme.toLocaleLowerCase()) return;
250
+ return auth.value;
251
+ }
252
+ fromCookie() {
253
+ return this.ctx.cookies.get(this.scope.config.field.extract.cookie);
254
+ }
255
+ fromAllWays() {
256
+ let token = this.fromQuery();
257
+ if (!token) token = this.fromAuthHeaderWithScheme();
258
+ if (!token) token = this.fromHeader();
259
+ if (!token) token = this.fromCookie();
260
+ return token;
261
+ }
262
+ }) || _class$1) || _class$1);
263
+
264
+ var _dec, _dec2, _class;
265
+ let ScopeModuleAJwt = (_dec = Scope(), _dec2 = BeanInfo({
266
+ module: "a-jwt"
267
+ }), _dec(_class = _dec2(_class = class ScopeModuleAJwt extends BeanScopeBase {}) || _class) || _class);
268
+
269
+ /** scope: end */
270
+
271
+ export { BeanJwt, DtoJwtToken, ScopeModuleAJwt, ServiceJwtClient, ServiceJwtExtract, config, parseAuthHeader };
@@ -0,0 +1,4 @@
1
+ export declare function parseAuthHeader(headerValue?: any): {
2
+ scheme: string;
3
+ value: string;
4
+ } | null | undefined;
@@ -0,0 +1 @@
1
+ export * from './authHeader.ts';
@@ -0,0 +1,16 @@
1
+ import type { IJwtClientRecord, IJwtSignOptions, IPayloadDataBase } from '../types/jwt.ts';
2
+ import jwt from 'jsonwebtoken';
3
+ import { BeanBase } from 'vona';
4
+ export declare class ServiceJwtClient extends BeanBase {
5
+ private _jwtInstance;
6
+ private _clientName;
7
+ private _clientOptions;
8
+ get instance(): typeof jwt;
9
+ protected __init__(clientName?: keyof IJwtClientRecord): void;
10
+ private _createClient;
11
+ private get fieldClient();
12
+ private get fieldPath();
13
+ private get fieldData();
14
+ sign(payloadData: IPayloadDataBase, options?: IJwtSignOptions): Promise<string>;
15
+ verify(token?: string): Promise<IPayloadDataBase | undefined>;
16
+ }
@@ -0,0 +1,8 @@
1
+ import { BeanBase } from 'vona';
2
+ export declare class ServiceJwtExtract extends BeanBase {
3
+ fromHeader(): string | undefined;
4
+ fromQuery(): string;
5
+ fromAuthHeaderWithScheme(): string | undefined;
6
+ fromCookie(): string | undefined;
7
+ fromAllWays(): string | undefined;
8
+ }
@@ -0,0 +1 @@
1
+ export * from './jwt.ts';
@@ -0,0 +1,49 @@
1
+ import type { SignOptions } from 'jsonwebtoken';
2
+ export interface IJwtToken {
3
+ accessToken: string;
4
+ refreshToken: string;
5
+ expiresIn: number;
6
+ }
7
+ export interface IJwtClientRecord {
8
+ access: never;
9
+ refresh: never;
10
+ oauth: never;
11
+ oauthstate: never;
12
+ code: never;
13
+ }
14
+ export interface IJwtSignOptions {
15
+ path?: string | string[];
16
+ dev?: boolean;
17
+ temp?: boolean;
18
+ }
19
+ export interface IJwtClientOptions {
20
+ secret?: string;
21
+ signOptions: SignOptions;
22
+ }
23
+ export interface ConfigJwt {
24
+ field: {
25
+ payload: {
26
+ client: string;
27
+ path: string;
28
+ data: string;
29
+ };
30
+ extract: {
31
+ header: string;
32
+ headerAuth: string;
33
+ headerAuthScheme: string;
34
+ query: string;
35
+ cookie: string;
36
+ };
37
+ };
38
+ tempToken: {
39
+ signOptions: {
40
+ expiresIn: number;
41
+ };
42
+ };
43
+ default: IJwtClientOptions;
44
+ clients: Record<keyof IJwtClientRecord, IJwtClientOptions>;
45
+ }
46
+ export interface IPayloadDataBase {
47
+ }
48
+ export interface IJwtPayload {
49
+ }
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.9",
4
+ "version": "5.0.11",
5
5
  "title": "a-jwt",
6
6
  "vonaModule": {
7
7
  "dependencies": {}
@@ -30,8 +30,12 @@
30
30
  "jsonwebtoken": "^9.0.2",
31
31
  "ms": "^2.1.3"
32
32
  },
33
+ "devDependencies": {
34
+ "clean-package": "^2.2.0",
35
+ "rimraf": "^6.0.1"
36
+ },
33
37
  "scripts": {
34
- "clean": "rimraf dist tsconfig.tsbuildinfo",
35
- "tsc:publish": "npm run clean && tsc"
38
+ "clean": "rimraf dist tsconfig.build.tsbuildinfo",
39
+ "tsc:publish": "npm run clean && vona :bin:buildModule && tsc -p tsconfig.build.json"
36
40
  }
37
41
  }