vona-module-a-jwt 5.0.28 → 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/bean/bean.jwt.d.ts +1 -0
- package/dist/index.js +53 -60
- package/dist/service/jwtClient.d.ts +2 -0
- package/dist/types/jwt.d.ts +6 -0
- package/package.json +1 -1
package/dist/bean/bean.jwt.d.ts
CHANGED
|
@@ -9,4 +9,5 @@ export declare class BeanJwt extends BeanBase {
|
|
|
9
9
|
createOauthAuthToken(payloadData: IPayloadData, options?: IJwtSignOptions): Promise<string>;
|
|
10
10
|
createOauthState(payloadData: IAuthenticateStrategyState, options?: IJwtSignOptions): Promise<string>;
|
|
11
11
|
createOauthCode(payloadData: IPayloadData, options?: IJwtSignOptions): Promise<string>;
|
|
12
|
+
extractAuthTokenFromAllWays(): string | undefined;
|
|
12
13
|
}
|
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
|
-
|
|
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)
|
|
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
|
-
}
|
|
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
|
-
}),
|
|
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
|
}
|
|
@@ -126,7 +143,13 @@ let BeanJwt = (_dec$3 = Bean(), _dec2$3 = BeanInfo({
|
|
|
126
143
|
async createOauthCode(payloadData, options) {
|
|
127
144
|
return await this.get('code').sign(payloadData, options);
|
|
128
145
|
}
|
|
129
|
-
|
|
146
|
+
extractAuthTokenFromAllWays() {
|
|
147
|
+
return this.scope.service.jwtExtract.fromAllWays();
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
BeanJwt = __decorate([Bean(), BeanInfo({
|
|
151
|
+
module: "a-jwt"
|
|
152
|
+
})], BeanJwt);
|
|
130
153
|
|
|
131
154
|
const re = /(\S+)\s+(\S+)/;
|
|
132
155
|
function parseAuthHeader(headerValue) {
|
|
@@ -138,10 +161,7 @@ function parseAuthHeader(headerValue) {
|
|
|
138
161
|
};
|
|
139
162
|
}
|
|
140
163
|
|
|
141
|
-
|
|
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 {
|
|
164
|
+
let ServiceJwtExtract = class ServiceJwtExtract extends BeanBase {
|
|
145
165
|
fromHeader() {
|
|
146
166
|
if (!this.scope.config.field.extract.header) return;
|
|
147
167
|
return this.ctx.request.headers[this.scope.config.field.extract.header];
|
|
@@ -165,50 +185,24 @@ let ServiceJwtExtract = (_dec$2 = Service(), _dec2$2 = BeanInfo({
|
|
|
165
185
|
if (!token) token = this.fromCookie();
|
|
166
186
|
return token;
|
|
167
187
|
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
function _applyDecoratedDescriptor(i, e, r, n, l) {
|
|
171
|
-
var a = {};
|
|
172
|
-
return Object.keys(n).forEach(function (i) {
|
|
173
|
-
a[i] = n[i];
|
|
174
|
-
}), a.enumerable = !!a.enumerable, a.configurable = !!a.configurable, ("value" in a || a.initializer) && (a.writable = true), a = r.slice().reverse().reduce(function (r, n) {
|
|
175
|
-
return n(i, e, r) || r;
|
|
176
|
-
}, a), void 0 === a.initializer ? (Object.defineProperty(i, e, a), null) : a;
|
|
177
|
-
}
|
|
178
|
-
function _initializerDefineProperty(e, i, r, l) {
|
|
179
|
-
r && Object.defineProperty(e, i, {
|
|
180
|
-
enumerable: r.enumerable,
|
|
181
|
-
configurable: r.configurable,
|
|
182
|
-
writable: r.writable,
|
|
183
|
-
value: r.initializer ? r.initializer.call(l) : void 0
|
|
184
|
-
});
|
|
185
|
-
}
|
|
186
|
-
|
|
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({
|
|
188
|
+
};
|
|
189
|
+
ServiceJwtExtract = __decorate([Service(), BeanInfo({
|
|
189
190
|
module: "a-jwt"
|
|
190
|
-
}),
|
|
191
|
+
})], ServiceJwtExtract);
|
|
192
|
+
|
|
193
|
+
let DtoJwtToken = class DtoJwtToken {
|
|
191
194
|
constructor() {
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
+
this.accessToken = void 0;
|
|
196
|
+
this.refreshToken = void 0;
|
|
197
|
+
this.expiresIn = void 0;
|
|
195
198
|
}
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
enumerable: true,
|
|
204
|
-
writable: true,
|
|
205
|
-
initializer: null
|
|
206
|
-
}), _descriptor3 = _applyDecoratedDescriptor(_class2.prototype, "expiresIn", [_dec7, _dec8], {
|
|
207
|
-
configurable: true,
|
|
208
|
-
enumerable: true,
|
|
209
|
-
writable: true,
|
|
210
|
-
initializer: null
|
|
211
|
-
}), _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);
|
|
212
206
|
|
|
213
207
|
function config(_app, env) {
|
|
214
208
|
return {
|
|
@@ -270,11 +264,10 @@ function config(_app, env) {
|
|
|
270
264
|
};
|
|
271
265
|
}
|
|
272
266
|
|
|
273
|
-
|
|
274
|
-
|
|
267
|
+
let ScopeModuleAJwt = class ScopeModuleAJwt extends BeanScopeBase {};
|
|
268
|
+
ScopeModuleAJwt = __decorate([Scope(), BeanInfo({
|
|
275
269
|
module: "a-jwt"
|
|
276
|
-
}),
|
|
277
|
-
|
|
270
|
+
})], ScopeModuleAJwt);
|
|
278
271
|
/** scope: end */
|
|
279
272
|
|
|
280
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
|
}
|
package/dist/types/jwt.d.ts
CHANGED
|
@@ -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
|
+
}
|