@azteam/express 1.2.348 → 1.2.350
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/lib/controller/AdminController.js +13 -12
- package/lib/middleware/adminRoleMiddleware.js +2 -1
- package/lib/middleware/roleMiddleware.js +4 -3
- package/lib/middleware/systemRoleMiddleware.js +2 -1
- package/package.json +1 -1
- package/src/controller/AdminController.js +25 -12
- package/src/middleware/adminRoleMiddleware.js +2 -2
- package/src/middleware/roleMiddleware.js +4 -4
- package/src/middleware/systemRoleMiddleware.js +2 -2
- package/lib/middleware/authGlobalMiddleware.js +0 -80
- package/lib/middleware/authMiddleware.js +0 -97
- package/src/middleware/authGlobalMiddleware.js +0 -46
- package/src/middleware/authMiddleware.js +0 -53
|
@@ -302,6 +302,7 @@ var AdminController = /*#__PURE__*/function (_Controller) {
|
|
|
302
302
|
IMPORT: null,
|
|
303
303
|
EXPORT: null
|
|
304
304
|
}, options.roles);
|
|
305
|
+
_this.secretKey = options.secretKey || process.env.SECRET_KEY;
|
|
305
306
|
_this.paginateOptions = options.paginateOptions || {};
|
|
306
307
|
_this.guardResponse = options.guardResponse || [];
|
|
307
308
|
_this.allowResponse = options.allowResponse ? [].concat(_toConsumableArray(options.allowResponse), ALLOW_FIELDS) : ALLOW_FIELDS;
|
|
@@ -317,7 +318,7 @@ var AdminController = /*#__PURE__*/function (_Controller) {
|
|
|
317
318
|
return {
|
|
318
319
|
disabled: !this.roles.READ,
|
|
319
320
|
path: '/',
|
|
320
|
-
method: [(0, _middleware.adminRoleMiddleware)([this.roles.READ]), (0, _middleware.paginateMiddleware)(this.paginateOptions), this.methodGetPaginate]
|
|
321
|
+
method: [(0, _middleware.adminRoleMiddleware)([this.roles.READ], this.secretKey), (0, _middleware.paginateMiddleware)(this.paginateOptions), this.methodGetPaginate]
|
|
321
322
|
};
|
|
322
323
|
}
|
|
323
324
|
}, {
|
|
@@ -326,7 +327,7 @@ var AdminController = /*#__PURE__*/function (_Controller) {
|
|
|
326
327
|
return {
|
|
327
328
|
disabled: !this.roles.READ,
|
|
328
329
|
path: '/',
|
|
329
|
-
method: [(0, _middleware.adminRoleMiddleware)([this.roles.READ]), (0, _middleware.paginateMiddleware)(this.paginateOptions), this.methodGetPaginateTrash]
|
|
330
|
+
method: [(0, _middleware.adminRoleMiddleware)([this.roles.READ], this.secretKey), (0, _middleware.paginateMiddleware)(this.paginateOptions), this.methodGetPaginateTrash]
|
|
330
331
|
};
|
|
331
332
|
}
|
|
332
333
|
}, {
|
|
@@ -335,7 +336,7 @@ var AdminController = /*#__PURE__*/function (_Controller) {
|
|
|
335
336
|
return {
|
|
336
337
|
disabled: !this.roles.READ,
|
|
337
338
|
path: '/:id',
|
|
338
|
-
method: [(0, _middleware.adminRoleMiddleware)([this.roles.READ]), (0, _middleware.validateMiddleware)(_constant.REQUEST_TYPE.PARAMS, _validator.rulesId), this.methodGetOne]
|
|
339
|
+
method: [(0, _middleware.adminRoleMiddleware)([this.roles.READ], this.secretKey), (0, _middleware.validateMiddleware)(_constant.REQUEST_TYPE.PARAMS, _validator.rulesId), this.methodGetOne]
|
|
339
340
|
};
|
|
340
341
|
}
|
|
341
342
|
}, {
|
|
@@ -344,7 +345,7 @@ var AdminController = /*#__PURE__*/function (_Controller) {
|
|
|
344
345
|
return {
|
|
345
346
|
disabled: !this.roles.READ,
|
|
346
347
|
path: '/:id',
|
|
347
|
-
method: [(0, _middleware.adminRoleMiddleware)([this.roles.READ]), (0, _middleware.validateMiddleware)(_constant.REQUEST_TYPE.PARAMS, _validator.rulesId), this.methodGetOneTrash]
|
|
348
|
+
method: [(0, _middleware.adminRoleMiddleware)([this.roles.READ], this.secretKey), (0, _middleware.validateMiddleware)(_constant.REQUEST_TYPE.PARAMS, _validator.rulesId), this.methodGetOneTrash]
|
|
348
349
|
};
|
|
349
350
|
}
|
|
350
351
|
}, {
|
|
@@ -396,7 +397,7 @@ var AdminController = /*#__PURE__*/function (_Controller) {
|
|
|
396
397
|
return {
|
|
397
398
|
disabled: !this.roles.CREATE,
|
|
398
399
|
path: '/',
|
|
399
|
-
method: [(0, _middleware.adminRoleMiddleware)([this.roles.CREATE]), /*#__PURE__*/function () {
|
|
400
|
+
method: [(0, _middleware.adminRoleMiddleware)([this.roles.CREATE], this.secretKey), /*#__PURE__*/function () {
|
|
400
401
|
var _ref10 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee12(req, res) {
|
|
401
402
|
var data, item;
|
|
402
403
|
return _regeneratorRuntime().wrap(function _callee12$(_context12) {
|
|
@@ -478,7 +479,7 @@ var AdminController = /*#__PURE__*/function (_Controller) {
|
|
|
478
479
|
return {
|
|
479
480
|
disabled: !this.roles.UPDATE,
|
|
480
481
|
path: '/:id',
|
|
481
|
-
method: [(0, _middleware.adminRoleMiddleware)([this.roles.UPDATE]), (0, _middleware.validateMiddleware)(_constant.REQUEST_TYPE.PARAMS, _validator.rulesId), /*#__PURE__*/function () {
|
|
482
|
+
method: [(0, _middleware.adminRoleMiddleware)([this.roles.UPDATE], this.secretKey), (0, _middleware.validateMiddleware)(_constant.REQUEST_TYPE.PARAMS, _validator.rulesId), /*#__PURE__*/function () {
|
|
482
483
|
var _ref11 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee15(req, res) {
|
|
483
484
|
var item, data;
|
|
484
485
|
return _regeneratorRuntime().wrap(function _callee15$(_context15) {
|
|
@@ -525,7 +526,7 @@ var AdminController = /*#__PURE__*/function (_Controller) {
|
|
|
525
526
|
return {
|
|
526
527
|
disabled: !this.roles.UPDATE,
|
|
527
528
|
path: '/available/:id',
|
|
528
|
-
method: [(0, _middleware.adminRoleMiddleware)([this.roles.UPDATE]), (0, _middleware.validateMiddleware)(_constant.REQUEST_TYPE.PARAMS, _validator.rulesId), /*#__PURE__*/function () {
|
|
529
|
+
method: [(0, _middleware.adminRoleMiddleware)([this.roles.UPDATE], this.secretKey), (0, _middleware.validateMiddleware)(_constant.REQUEST_TYPE.PARAMS, _validator.rulesId), /*#__PURE__*/function () {
|
|
529
530
|
var _ref12 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee16(req, res) {
|
|
530
531
|
var item;
|
|
531
532
|
return _regeneratorRuntime().wrap(function _callee16$(_context16) {
|
|
@@ -570,7 +571,7 @@ var AdminController = /*#__PURE__*/function (_Controller) {
|
|
|
570
571
|
return {
|
|
571
572
|
disabled: !this.roles.UPDATE,
|
|
572
573
|
path: '/unavailable/:id',
|
|
573
|
-
method: [(0, _middleware.adminRoleMiddleware)([this.roles.UPDATE]), (0, _middleware.validateMiddleware)(_constant.REQUEST_TYPE.PARAMS, _validator.rulesId), /*#__PURE__*/function () {
|
|
574
|
+
method: [(0, _middleware.adminRoleMiddleware)([this.roles.UPDATE], this.secretKey), (0, _middleware.validateMiddleware)(_constant.REQUEST_TYPE.PARAMS, _validator.rulesId), /*#__PURE__*/function () {
|
|
574
575
|
var _ref13 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee17(req, res) {
|
|
575
576
|
var item;
|
|
576
577
|
return _regeneratorRuntime().wrap(function _callee17$(_context17) {
|
|
@@ -615,7 +616,7 @@ var AdminController = /*#__PURE__*/function (_Controller) {
|
|
|
615
616
|
return {
|
|
616
617
|
disabled: !this.roles.UPDATE,
|
|
617
618
|
path: '/waiting/:id',
|
|
618
|
-
method: [(0, _middleware.adminRoleMiddleware)([this.roles.UPDATE]), (0, _middleware.validateMiddleware)(_constant.REQUEST_TYPE.PARAMS, _validator.rulesId), /*#__PURE__*/function () {
|
|
619
|
+
method: [(0, _middleware.adminRoleMiddleware)([this.roles.UPDATE], this.secretKey), (0, _middleware.validateMiddleware)(_constant.REQUEST_TYPE.PARAMS, _validator.rulesId), /*#__PURE__*/function () {
|
|
619
620
|
var _ref14 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee18(req, res) {
|
|
620
621
|
var item;
|
|
621
622
|
return _regeneratorRuntime().wrap(function _callee18$(_context18) {
|
|
@@ -680,7 +681,7 @@ var AdminController = /*#__PURE__*/function (_Controller) {
|
|
|
680
681
|
return {
|
|
681
682
|
disabled: !this.roles.DELETE,
|
|
682
683
|
path: '/:id',
|
|
683
|
-
method: [(0, _middleware.adminRoleMiddleware)([this.roles.DELETE]), (0, _middleware.validateMiddleware)(_constant.REQUEST_TYPE.PARAMS, _validator.rulesId), this.methodDelete]
|
|
684
|
+
method: [(0, _middleware.adminRoleMiddleware)([this.roles.DELETE], this.secretKey), (0, _middleware.validateMiddleware)(_constant.REQUEST_TYPE.PARAMS, _validator.rulesId), this.methodDelete]
|
|
684
685
|
};
|
|
685
686
|
}
|
|
686
687
|
}, {
|
|
@@ -689,7 +690,7 @@ var AdminController = /*#__PURE__*/function (_Controller) {
|
|
|
689
690
|
return {
|
|
690
691
|
disabled: !this.roles.RESTORE,
|
|
691
692
|
path: '/:id',
|
|
692
|
-
method: [(0, _middleware.adminRoleMiddleware)([this.roles.RESTORE]), (0, _middleware.validateMiddleware)(_constant.REQUEST_TYPE.PARAMS, _validator.rulesId), this.methodPostRestoreTrash]
|
|
693
|
+
method: [(0, _middleware.adminRoleMiddleware)([this.roles.RESTORE], this.secretKey), (0, _middleware.validateMiddleware)(_constant.REQUEST_TYPE.PARAMS, _validator.rulesId), this.methodPostRestoreTrash]
|
|
693
694
|
};
|
|
694
695
|
}
|
|
695
696
|
}, {
|
|
@@ -719,7 +720,7 @@ var AdminController = /*#__PURE__*/function (_Controller) {
|
|
|
719
720
|
return {
|
|
720
721
|
disabled: !this.roles.DESTROY,
|
|
721
722
|
path: '/:id',
|
|
722
|
-
method: [(0, _middleware.adminRoleMiddleware)([this.roles.DESTROY]), (0, _middleware.validateMiddleware)(_constant.REQUEST_TYPE.PARAMS, _validator.rulesId), this.methodDeleteDestroyTrash]
|
|
723
|
+
method: [(0, _middleware.adminRoleMiddleware)([this.roles.DESTROY], this.secretKey), (0, _middleware.validateMiddleware)(_constant.REQUEST_TYPE.PARAMS, _validator.rulesId), this.methodDeleteDestroyTrash]
|
|
723
724
|
};
|
|
724
725
|
}
|
|
725
726
|
}]);
|
|
@@ -9,5 +9,6 @@ var _roleMiddleware = _interopRequireDefault(require("./roleMiddleware"));
|
|
|
9
9
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
10
10
|
function _default() {
|
|
11
11
|
var roles = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
|
|
12
|
-
|
|
12
|
+
var secretKey = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : process.env.SECRET_KEY;
|
|
13
|
+
return (0, _roleMiddleware["default"])(roles, _constant.USER_LEVEL.ADMIN, secretKey);
|
|
13
14
|
}
|
|
@@ -25,6 +25,7 @@ function systemLogin() {
|
|
|
25
25
|
function _default() {
|
|
26
26
|
var roles = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
|
|
27
27
|
var minLevel = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _constant.USER_LEVEL.USER;
|
|
28
|
+
var secretKey = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : process.env.SECRET_KEY;
|
|
28
29
|
return /*#__PURE__*/function () {
|
|
29
30
|
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req, res, next) {
|
|
30
31
|
var headers, token;
|
|
@@ -33,7 +34,7 @@ function _default() {
|
|
|
33
34
|
switch (_context.prev = _context.next) {
|
|
34
35
|
case 0:
|
|
35
36
|
headers = req.headers;
|
|
36
|
-
if (!(headers['x-app-secret'] ===
|
|
37
|
+
if (!(headers['x-app-secret'] === secretKey)) {
|
|
37
38
|
_context.next = 5;
|
|
38
39
|
break;
|
|
39
40
|
}
|
|
@@ -55,7 +56,7 @@ function _default() {
|
|
|
55
56
|
}
|
|
56
57
|
token = token.replace('Bearer ', '');
|
|
57
58
|
_context.prev = 10;
|
|
58
|
-
req.user = _jsonwebtoken["default"].verify(token,
|
|
59
|
+
req.user = _jsonwebtoken["default"].verify(token, secretKey);
|
|
59
60
|
_context.next = 19;
|
|
60
61
|
break;
|
|
61
62
|
case 14:
|
|
@@ -76,7 +77,7 @@ function _default() {
|
|
|
76
77
|
throw new _error.ErrorException(_error.UNAUTHORIZED);
|
|
77
78
|
case 21:
|
|
78
79
|
req.user.level = req.user.level || _constant.USER_LEVEL.USER;
|
|
79
|
-
if (!(!roles || req.user.level === 100 || req.user.level
|
|
80
|
+
if (!(!roles || req.user.level === 100 || req.user.level >= minLevel && req.user.roles.some(function (r) {
|
|
80
81
|
return roles.includes(r);
|
|
81
82
|
}))) {
|
|
82
83
|
_context.next = 24;
|
|
@@ -8,5 +8,6 @@ var _constant = require("@azteam/constant");
|
|
|
8
8
|
var _roleMiddleware = _interopRequireDefault(require("./roleMiddleware"));
|
|
9
9
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
10
10
|
function _default() {
|
|
11
|
-
|
|
11
|
+
var secretKey = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : process.env.SECRET_KEY;
|
|
12
|
+
return (0, _roleMiddleware["default"])(null, _constant.USER_LEVEL.SYSTEM, secretKey);
|
|
12
13
|
}
|
package/package.json
CHANGED
|
@@ -39,6 +39,7 @@ class AdminController extends Controller {
|
|
|
39
39
|
...options.roles,
|
|
40
40
|
};
|
|
41
41
|
|
|
42
|
+
this.secretKey = options.secretKey || process.env.SECRET_KEY;
|
|
42
43
|
this.paginateOptions = options.paginateOptions || {};
|
|
43
44
|
this.guardResponse = options.guardResponse || [];
|
|
44
45
|
this.allowResponse = options.allowResponse ? [...options.allowResponse, ...ALLOW_FIELDS] : ALLOW_FIELDS;
|
|
@@ -65,7 +66,7 @@ class AdminController extends Controller {
|
|
|
65
66
|
return {
|
|
66
67
|
disabled: !this.roles.READ,
|
|
67
68
|
path: '/',
|
|
68
|
-
method: [adminRoleMiddleware([this.roles.READ]), paginateMiddleware(this.paginateOptions), this.methodGetPaginate],
|
|
69
|
+
method: [adminRoleMiddleware([this.roles.READ], this.secretKey), paginateMiddleware(this.paginateOptions), this.methodGetPaginate],
|
|
69
70
|
};
|
|
70
71
|
}
|
|
71
72
|
|
|
@@ -78,7 +79,7 @@ class AdminController extends Controller {
|
|
|
78
79
|
return {
|
|
79
80
|
disabled: !this.roles.READ,
|
|
80
81
|
path: '/',
|
|
81
|
-
method: [adminRoleMiddleware([this.roles.READ]), paginateMiddleware(this.paginateOptions), this.methodGetPaginateTrash],
|
|
82
|
+
method: [adminRoleMiddleware([this.roles.READ], this.secretKey), paginateMiddleware(this.paginateOptions), this.methodGetPaginateTrash],
|
|
82
83
|
};
|
|
83
84
|
}
|
|
84
85
|
|
|
@@ -92,7 +93,7 @@ class AdminController extends Controller {
|
|
|
92
93
|
return {
|
|
93
94
|
disabled: !this.roles.READ,
|
|
94
95
|
path: '/:id',
|
|
95
|
-
method: [adminRoleMiddleware([this.roles.READ]), validateMiddleware(REQUEST_TYPE.PARAMS, rulesId), this.methodGetOne],
|
|
96
|
+
method: [adminRoleMiddleware([this.roles.READ], this.secretKey), validateMiddleware(REQUEST_TYPE.PARAMS, rulesId), this.methodGetOne],
|
|
96
97
|
};
|
|
97
98
|
}
|
|
98
99
|
|
|
@@ -106,7 +107,11 @@ class AdminController extends Controller {
|
|
|
106
107
|
return {
|
|
107
108
|
disabled: !this.roles.READ,
|
|
108
109
|
path: '/:id',
|
|
109
|
-
method: [
|
|
110
|
+
method: [
|
|
111
|
+
adminRoleMiddleware([this.roles.READ], this.secretKey),
|
|
112
|
+
validateMiddleware(REQUEST_TYPE.PARAMS, rulesId),
|
|
113
|
+
this.methodGetOneTrash,
|
|
114
|
+
],
|
|
110
115
|
};
|
|
111
116
|
}
|
|
112
117
|
|
|
@@ -127,7 +132,7 @@ class AdminController extends Controller {
|
|
|
127
132
|
disabled: !this.roles.CREATE,
|
|
128
133
|
path: '/',
|
|
129
134
|
method: [
|
|
130
|
-
adminRoleMiddleware([this.roles.CREATE]),
|
|
135
|
+
adminRoleMiddleware([this.roles.CREATE], this.secretKey),
|
|
131
136
|
async (req, res) => {
|
|
132
137
|
const data = await this.beforeCreate(req.body);
|
|
133
138
|
|
|
@@ -158,7 +163,7 @@ class AdminController extends Controller {
|
|
|
158
163
|
disabled: !this.roles.UPDATE,
|
|
159
164
|
path: '/:id',
|
|
160
165
|
method: [
|
|
161
|
-
adminRoleMiddleware([this.roles.UPDATE]),
|
|
166
|
+
adminRoleMiddleware([this.roles.UPDATE], this.secretKey),
|
|
162
167
|
validateMiddleware(REQUEST_TYPE.PARAMS, rulesId),
|
|
163
168
|
async (req, res) => {
|
|
164
169
|
let item = await this.repository.findOneById(req.params.id);
|
|
@@ -181,7 +186,7 @@ class AdminController extends Controller {
|
|
|
181
186
|
disabled: !this.roles.UPDATE,
|
|
182
187
|
path: '/available/:id',
|
|
183
188
|
method: [
|
|
184
|
-
adminRoleMiddleware([this.roles.UPDATE]),
|
|
189
|
+
adminRoleMiddleware([this.roles.UPDATE], this.secretKey),
|
|
185
190
|
validateMiddleware(REQUEST_TYPE.PARAMS, rulesId),
|
|
186
191
|
async (req, res) => {
|
|
187
192
|
let item = await this.repository.findOneById(req.params.id);
|
|
@@ -202,7 +207,7 @@ class AdminController extends Controller {
|
|
|
202
207
|
disabled: !this.roles.UPDATE,
|
|
203
208
|
path: '/unavailable/:id',
|
|
204
209
|
method: [
|
|
205
|
-
adminRoleMiddleware([this.roles.UPDATE]),
|
|
210
|
+
adminRoleMiddleware([this.roles.UPDATE], this.secretKey),
|
|
206
211
|
validateMiddleware(REQUEST_TYPE.PARAMS, rulesId),
|
|
207
212
|
async (req, res) => {
|
|
208
213
|
let item = await this.repository.findOneById(req.params.id);
|
|
@@ -223,7 +228,7 @@ class AdminController extends Controller {
|
|
|
223
228
|
disabled: !this.roles.UPDATE,
|
|
224
229
|
path: '/waiting/:id',
|
|
225
230
|
method: [
|
|
226
|
-
adminRoleMiddleware([this.roles.UPDATE]),
|
|
231
|
+
adminRoleMiddleware([this.roles.UPDATE], this.secretKey),
|
|
227
232
|
validateMiddleware(REQUEST_TYPE.PARAMS, rulesId),
|
|
228
233
|
async (req, res) => {
|
|
229
234
|
let item = await this.repository.findOneById(req.params.id);
|
|
@@ -258,7 +263,7 @@ class AdminController extends Controller {
|
|
|
258
263
|
return {
|
|
259
264
|
disabled: !this.roles.DELETE,
|
|
260
265
|
path: '/:id',
|
|
261
|
-
method: [adminRoleMiddleware([this.roles.DELETE]), validateMiddleware(REQUEST_TYPE.PARAMS, rulesId), this.methodDelete],
|
|
266
|
+
method: [adminRoleMiddleware([this.roles.DELETE], this.secretKey), validateMiddleware(REQUEST_TYPE.PARAMS, rulesId), this.methodDelete],
|
|
262
267
|
};
|
|
263
268
|
}
|
|
264
269
|
|
|
@@ -275,7 +280,11 @@ class AdminController extends Controller {
|
|
|
275
280
|
return {
|
|
276
281
|
disabled: !this.roles.RESTORE,
|
|
277
282
|
path: '/:id',
|
|
278
|
-
method: [
|
|
283
|
+
method: [
|
|
284
|
+
adminRoleMiddleware([this.roles.RESTORE], this.secretKey),
|
|
285
|
+
validateMiddleware(REQUEST_TYPE.PARAMS, rulesId),
|
|
286
|
+
this.methodPostRestoreTrash,
|
|
287
|
+
],
|
|
279
288
|
};
|
|
280
289
|
}
|
|
281
290
|
|
|
@@ -307,7 +316,11 @@ class AdminController extends Controller {
|
|
|
307
316
|
return {
|
|
308
317
|
disabled: !this.roles.DESTROY,
|
|
309
318
|
path: '/:id',
|
|
310
|
-
method: [
|
|
319
|
+
method: [
|
|
320
|
+
adminRoleMiddleware([this.roles.DESTROY], this.secretKey),
|
|
321
|
+
validateMiddleware(REQUEST_TYPE.PARAMS, rulesId),
|
|
322
|
+
this.methodDeleteDestroyTrash,
|
|
323
|
+
],
|
|
311
324
|
};
|
|
312
325
|
}
|
|
313
326
|
}
|
|
@@ -2,6 +2,6 @@ import {USER_LEVEL} from '@azteam/constant';
|
|
|
2
2
|
|
|
3
3
|
import roleMiddleware from './roleMiddleware';
|
|
4
4
|
|
|
5
|
-
export default function (roles = null) {
|
|
6
|
-
return roleMiddleware(roles, USER_LEVEL.ADMIN);
|
|
5
|
+
export default function (roles = null, secretKey = process.env.SECRET_KEY) {
|
|
6
|
+
return roleMiddleware(roles, USER_LEVEL.ADMIN, secretKey);
|
|
7
7
|
}
|
|
@@ -12,11 +12,11 @@ function systemLogin(userData = null) {
|
|
|
12
12
|
return user;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
export default function (roles = null, minLevel = USER_LEVEL.USER) {
|
|
15
|
+
export default function (roles = null, minLevel = USER_LEVEL.USER, secretKey = process.env.SECRET_KEY) {
|
|
16
16
|
return async function (req, res, next) {
|
|
17
17
|
const {headers} = req;
|
|
18
18
|
|
|
19
|
-
if (headers['x-app-secret'] ===
|
|
19
|
+
if (headers['x-app-secret'] === secretKey) {
|
|
20
20
|
req.user = systemLogin(headers['x-app-user']);
|
|
21
21
|
} else {
|
|
22
22
|
let token = null;
|
|
@@ -30,7 +30,7 @@ export default function (roles = null, minLevel = USER_LEVEL.USER) {
|
|
|
30
30
|
token = token.replace('Bearer ', '');
|
|
31
31
|
|
|
32
32
|
try {
|
|
33
|
-
req.user = jwt.verify(token,
|
|
33
|
+
req.user = jwt.verify(token, secretKey);
|
|
34
34
|
} catch (err) {
|
|
35
35
|
if (err.name === 'TokenExpiredError') {
|
|
36
36
|
throw new ErrorException(TOKEN_EXPIRED, err);
|
|
@@ -47,7 +47,7 @@ export default function (roles = null, minLevel = USER_LEVEL.USER) {
|
|
|
47
47
|
|
|
48
48
|
req.user.level = req.user.level || USER_LEVEL.USER;
|
|
49
49
|
|
|
50
|
-
if (!roles || req.user.level === 100 || (req.user.level
|
|
50
|
+
if (!roles || req.user.level === 100 || (req.user.level >= minLevel && req.user.roles.some((r) => roles.includes(r)))) {
|
|
51
51
|
return next();
|
|
52
52
|
}
|
|
53
53
|
throw new ErrorException(PERMISSION);
|
|
@@ -2,6 +2,6 @@ import {USER_LEVEL} from '@azteam/constant';
|
|
|
2
2
|
|
|
3
3
|
import roleMiddleware from './roleMiddleware';
|
|
4
4
|
|
|
5
|
-
export default function () {
|
|
6
|
-
return roleMiddleware(null, USER_LEVEL.SYSTEM);
|
|
5
|
+
export default function (secretKey = process.env.SECRET_KEY) {
|
|
6
|
+
return roleMiddleware(null, USER_LEVEL.SYSTEM, secretKey);
|
|
7
7
|
}
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
4
|
-
Object.defineProperty(exports, "__esModule", {
|
|
5
|
-
value: true
|
|
6
|
-
});
|
|
7
|
-
exports["default"] = _default;
|
|
8
|
-
var _jsonwebtoken = _interopRequireDefault(require("jsonwebtoken"));
|
|
9
|
-
var _error = require("@azteam/error");
|
|
10
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
11
|
-
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return exports; }; var exports = {}, Op = Object.prototype, hasOwn = Op.hasOwnProperty, $Symbol = "function" == typeof Symbol ? Symbol : {}, iteratorSymbol = $Symbol.iterator || "@@iterator", asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator", toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag"; function define(obj, key, value) { return Object.defineProperty(obj, key, { value: value, enumerable: !0, configurable: !0, writable: !0 }), obj[key]; } try { define({}, ""); } catch (err) { define = function define(obj, key, value) { return obj[key] = value; }; } function wrap(innerFn, outerFn, self, tryLocsList) { var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator, generator = Object.create(protoGenerator.prototype), context = new Context(tryLocsList || []); return generator._invoke = function (innerFn, self, context) { var state = "suspendedStart"; return function (method, arg) { if ("executing" === state) throw new Error("Generator is already running"); if ("completed" === state) { if ("throw" === method) throw arg; return doneResult(); } for (context.method = method, context.arg = arg;;) { var delegate = context.delegate; if (delegate) { var delegateResult = maybeInvokeDelegate(delegate, context); if (delegateResult) { if (delegateResult === ContinueSentinel) continue; return delegateResult; } } if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) { if ("suspendedStart" === state) throw state = "completed", context.arg; context.dispatchException(context.arg); } else "return" === context.method && context.abrupt("return", context.arg); state = "executing"; var record = tryCatch(innerFn, self, context); if ("normal" === record.type) { if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue; return { value: record.arg, done: context.done }; } "throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg); } }; }(innerFn, self, context), generator; } function tryCatch(fn, obj, arg) { try { return { type: "normal", arg: fn.call(obj, arg) }; } catch (err) { return { type: "throw", arg: err }; } } exports.wrap = wrap; var ContinueSentinel = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var IteratorPrototype = {}; define(IteratorPrototype, iteratorSymbol, function () { return this; }); var getProto = Object.getPrototypeOf, NativeIteratorPrototype = getProto && getProto(getProto(values([]))); NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype); var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype); function defineIteratorMethods(prototype) { ["next", "throw", "return"].forEach(function (method) { define(prototype, method, function (arg) { return this._invoke(method, arg); }); }); } function AsyncIterator(generator, PromiseImpl) { function invoke(method, arg, resolve, reject) { var record = tryCatch(generator[method], generator, arg); if ("throw" !== record.type) { var result = record.arg, value = result.value; return value && "object" == _typeof(value) && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) { invoke("next", value, resolve, reject); }, function (err) { invoke("throw", err, resolve, reject); }) : PromiseImpl.resolve(value).then(function (unwrapped) { result.value = unwrapped, resolve(result); }, function (error) { return invoke("throw", error, resolve, reject); }); } reject(record.arg); } var previousPromise; this._invoke = function (method, arg) { function callInvokeWithMethodAndArg() { return new PromiseImpl(function (resolve, reject) { invoke(method, arg, resolve, reject); }); } return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); }; } function maybeInvokeDelegate(delegate, context) { var method = delegate.iterator[context.method]; if (undefined === method) { if (context.delegate = null, "throw" === context.method) { if (delegate.iterator["return"] && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method)) return ContinueSentinel; context.method = "throw", context.arg = new TypeError("The iterator does not provide a 'throw' method"); } return ContinueSentinel; } var record = tryCatch(method, delegate.iterator, context.arg); if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel; var info = record.arg; return info ? info.done ? (context[delegate.resultName] = info.value, context.next = delegate.nextLoc, "return" !== context.method && (context.method = "next", context.arg = undefined), context.delegate = null, ContinueSentinel) : info : (context.method = "throw", context.arg = new TypeError("iterator result is not an object"), context.delegate = null, ContinueSentinel); } function pushTryEntry(locs) { var entry = { tryLoc: locs[0] }; 1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry); } function resetTryEntry(entry) { var record = entry.completion || {}; record.type = "normal", delete record.arg, entry.completion = record; } function Context(tryLocsList) { this.tryEntries = [{ tryLoc: "root" }], tryLocsList.forEach(pushTryEntry, this), this.reset(!0); } function values(iterable) { if (iterable) { var iteratorMethod = iterable[iteratorSymbol]; if (iteratorMethod) return iteratorMethod.call(iterable); if ("function" == typeof iterable.next) return iterable; if (!isNaN(iterable.length)) { var i = -1, next = function next() { for (; ++i < iterable.length;) { if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next; } return next.value = undefined, next.done = !0, next; }; return next.next = next; } } return { next: doneResult }; } function doneResult() { return { value: undefined, done: !0 }; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, define(Gp, "constructor", GeneratorFunctionPrototype), define(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) { var ctor = "function" == typeof genFun && genFun.constructor; return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name)); }, exports.mark = function (genFun) { return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun; }, exports.awrap = function (arg) { return { __await: arg }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () { return this; }), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) { void 0 === PromiseImpl && (PromiseImpl = Promise); var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl); return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) { return result.done ? result.value : iter.next(); }); }, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () { return this; }), define(Gp, "toString", function () { return "[object Generator]"; }), exports.keys = function (object) { var keys = []; for (var key in object) { keys.push(key); } return keys.reverse(), function next() { for (; keys.length;) { var key = keys.pop(); if (key in object) return next.value = key, next.done = !1, next; } return next.done = !0, next; }; }, exports.values = values, Context.prototype = { constructor: Context, reset: function reset(skipTempReset) { if (this.prev = 0, this.next = 0, this.sent = this._sent = undefined, this.done = !1, this.delegate = null, this.method = "next", this.arg = undefined, this.tryEntries.forEach(resetTryEntry), !skipTempReset) for (var name in this) { "t" === name.charAt(0) && hasOwn.call(this, name) && !isNaN(+name.slice(1)) && (this[name] = undefined); } }, stop: function stop() { this.done = !0; var rootRecord = this.tryEntries[0].completion; if ("throw" === rootRecord.type) throw rootRecord.arg; return this.rval; }, dispatchException: function dispatchException(exception) { if (this.done) throw exception; var context = this; function handle(loc, caught) { return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught; } for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i], record = entry.completion; if ("root" === entry.tryLoc) return handle("end"); if (entry.tryLoc <= this.prev) { var hasCatch = hasOwn.call(entry, "catchLoc"), hasFinally = hasOwn.call(entry, "finallyLoc"); if (hasCatch && hasFinally) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } else if (hasCatch) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); } else { if (!hasFinally) throw new Error("try statement without catch or finally"); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } } } }, abrupt: function abrupt(type, arg) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) { var finallyEntry = entry; break; } } finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null); var record = finallyEntry ? finallyEntry.completion : {}; return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record); }, complete: function complete(record, afterLoc) { if ("throw" === record.type) throw record.arg; return "break" === record.type || "continue" === record.type ? this.next = record.arg : "return" === record.type ? (this.rval = this.arg = record.arg, this.method = "return", this.next = "end") : "normal" === record.type && afterLoc && (this.next = afterLoc), ContinueSentinel; }, finish: function finish(finallyLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel; } }, "catch": function _catch(tryLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc === tryLoc) { var record = entry.completion; if ("throw" === record.type) { var thrown = record.arg; resetTryEntry(entry); } return thrown; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(iterable, resultName, nextLoc) { return this.delegate = { iterator: values(iterable), resultName: resultName, nextLoc: nextLoc }, "next" === this.method && (this.arg = undefined), ContinueSentinel; } }, exports; }
|
|
12
|
-
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
13
|
-
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
14
|
-
function systemLogin() {
|
|
15
|
-
var userData = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
|
|
16
|
-
var user = {};
|
|
17
|
-
if (userData) {
|
|
18
|
-
try {
|
|
19
|
-
user = JSON.parse(userData);
|
|
20
|
-
} catch (err) {}
|
|
21
|
-
}
|
|
22
|
-
return user;
|
|
23
|
-
}
|
|
24
|
-
function _default() {
|
|
25
|
-
return /*#__PURE__*/function () {
|
|
26
|
-
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req, res, next) {
|
|
27
|
-
var headers, token;
|
|
28
|
-
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
29
|
-
while (1) {
|
|
30
|
-
switch (_context.prev = _context.next) {
|
|
31
|
-
case 0:
|
|
32
|
-
headers = req.headers;
|
|
33
|
-
if (!(headers['x-app-secret'] === process.env.SECRET_KEY)) {
|
|
34
|
-
_context.next = 5;
|
|
35
|
-
break;
|
|
36
|
-
}
|
|
37
|
-
req.user = systemLogin(headers['x-app-user']);
|
|
38
|
-
_context.next = 20;
|
|
39
|
-
break;
|
|
40
|
-
case 5:
|
|
41
|
-
token = null;
|
|
42
|
-
if (headers.authorization) {
|
|
43
|
-
token = headers.authorization;
|
|
44
|
-
}
|
|
45
|
-
if (!token) {
|
|
46
|
-
_context.next = 20;
|
|
47
|
-
break;
|
|
48
|
-
}
|
|
49
|
-
if (!token.startsWith('Bearer ')) {
|
|
50
|
-
_context.next = 20;
|
|
51
|
-
break;
|
|
52
|
-
}
|
|
53
|
-
token = token.replace('Bearer ', '');
|
|
54
|
-
_context.prev = 10;
|
|
55
|
-
req.user = _jsonwebtoken["default"].verify(token, process.env.SECRET_KEY);
|
|
56
|
-
return _context.abrupt("return", next());
|
|
57
|
-
case 15:
|
|
58
|
-
_context.prev = 15;
|
|
59
|
-
_context.t0 = _context["catch"](10);
|
|
60
|
-
if (!(_context.t0.name === 'TokenExpiredError')) {
|
|
61
|
-
_context.next = 19;
|
|
62
|
-
break;
|
|
63
|
-
}
|
|
64
|
-
throw new _error.ErrorException(_error.TOKEN_EXPIRED, _context.t0);
|
|
65
|
-
case 19:
|
|
66
|
-
throw new _error.ErrorException(_error.TOKEN_FAILED, _context.t0);
|
|
67
|
-
case 20:
|
|
68
|
-
return _context.abrupt("return", next());
|
|
69
|
-
case 21:
|
|
70
|
-
case "end":
|
|
71
|
-
return _context.stop();
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}, _callee, null, [[10, 15]]);
|
|
75
|
-
}));
|
|
76
|
-
return function (_x, _x2, _x3) {
|
|
77
|
-
return _ref.apply(this, arguments);
|
|
78
|
-
};
|
|
79
|
-
}();
|
|
80
|
-
}
|
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
4
|
-
Object.defineProperty(exports, "__esModule", {
|
|
5
|
-
value: true
|
|
6
|
-
});
|
|
7
|
-
exports["default"] = _default;
|
|
8
|
-
var _jsonwebtoken = _interopRequireDefault(require("jsonwebtoken"));
|
|
9
|
-
var _error = require("@azteam/error");
|
|
10
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
11
|
-
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return exports; }; var exports = {}, Op = Object.prototype, hasOwn = Op.hasOwnProperty, $Symbol = "function" == typeof Symbol ? Symbol : {}, iteratorSymbol = $Symbol.iterator || "@@iterator", asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator", toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag"; function define(obj, key, value) { return Object.defineProperty(obj, key, { value: value, enumerable: !0, configurable: !0, writable: !0 }), obj[key]; } try { define({}, ""); } catch (err) { define = function define(obj, key, value) { return obj[key] = value; }; } function wrap(innerFn, outerFn, self, tryLocsList) { var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator, generator = Object.create(protoGenerator.prototype), context = new Context(tryLocsList || []); return generator._invoke = function (innerFn, self, context) { var state = "suspendedStart"; return function (method, arg) { if ("executing" === state) throw new Error("Generator is already running"); if ("completed" === state) { if ("throw" === method) throw arg; return doneResult(); } for (context.method = method, context.arg = arg;;) { var delegate = context.delegate; if (delegate) { var delegateResult = maybeInvokeDelegate(delegate, context); if (delegateResult) { if (delegateResult === ContinueSentinel) continue; return delegateResult; } } if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) { if ("suspendedStart" === state) throw state = "completed", context.arg; context.dispatchException(context.arg); } else "return" === context.method && context.abrupt("return", context.arg); state = "executing"; var record = tryCatch(innerFn, self, context); if ("normal" === record.type) { if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue; return { value: record.arg, done: context.done }; } "throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg); } }; }(innerFn, self, context), generator; } function tryCatch(fn, obj, arg) { try { return { type: "normal", arg: fn.call(obj, arg) }; } catch (err) { return { type: "throw", arg: err }; } } exports.wrap = wrap; var ContinueSentinel = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var IteratorPrototype = {}; define(IteratorPrototype, iteratorSymbol, function () { return this; }); var getProto = Object.getPrototypeOf, NativeIteratorPrototype = getProto && getProto(getProto(values([]))); NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype); var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype); function defineIteratorMethods(prototype) { ["next", "throw", "return"].forEach(function (method) { define(prototype, method, function (arg) { return this._invoke(method, arg); }); }); } function AsyncIterator(generator, PromiseImpl) { function invoke(method, arg, resolve, reject) { var record = tryCatch(generator[method], generator, arg); if ("throw" !== record.type) { var result = record.arg, value = result.value; return value && "object" == _typeof(value) && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) { invoke("next", value, resolve, reject); }, function (err) { invoke("throw", err, resolve, reject); }) : PromiseImpl.resolve(value).then(function (unwrapped) { result.value = unwrapped, resolve(result); }, function (error) { return invoke("throw", error, resolve, reject); }); } reject(record.arg); } var previousPromise; this._invoke = function (method, arg) { function callInvokeWithMethodAndArg() { return new PromiseImpl(function (resolve, reject) { invoke(method, arg, resolve, reject); }); } return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); }; } function maybeInvokeDelegate(delegate, context) { var method = delegate.iterator[context.method]; if (undefined === method) { if (context.delegate = null, "throw" === context.method) { if (delegate.iterator["return"] && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method)) return ContinueSentinel; context.method = "throw", context.arg = new TypeError("The iterator does not provide a 'throw' method"); } return ContinueSentinel; } var record = tryCatch(method, delegate.iterator, context.arg); if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel; var info = record.arg; return info ? info.done ? (context[delegate.resultName] = info.value, context.next = delegate.nextLoc, "return" !== context.method && (context.method = "next", context.arg = undefined), context.delegate = null, ContinueSentinel) : info : (context.method = "throw", context.arg = new TypeError("iterator result is not an object"), context.delegate = null, ContinueSentinel); } function pushTryEntry(locs) { var entry = { tryLoc: locs[0] }; 1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry); } function resetTryEntry(entry) { var record = entry.completion || {}; record.type = "normal", delete record.arg, entry.completion = record; } function Context(tryLocsList) { this.tryEntries = [{ tryLoc: "root" }], tryLocsList.forEach(pushTryEntry, this), this.reset(!0); } function values(iterable) { if (iterable) { var iteratorMethod = iterable[iteratorSymbol]; if (iteratorMethod) return iteratorMethod.call(iterable); if ("function" == typeof iterable.next) return iterable; if (!isNaN(iterable.length)) { var i = -1, next = function next() { for (; ++i < iterable.length;) { if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next; } return next.value = undefined, next.done = !0, next; }; return next.next = next; } } return { next: doneResult }; } function doneResult() { return { value: undefined, done: !0 }; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, define(Gp, "constructor", GeneratorFunctionPrototype), define(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) { var ctor = "function" == typeof genFun && genFun.constructor; return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name)); }, exports.mark = function (genFun) { return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun; }, exports.awrap = function (arg) { return { __await: arg }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () { return this; }), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) { void 0 === PromiseImpl && (PromiseImpl = Promise); var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl); return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) { return result.done ? result.value : iter.next(); }); }, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () { return this; }), define(Gp, "toString", function () { return "[object Generator]"; }), exports.keys = function (object) { var keys = []; for (var key in object) { keys.push(key); } return keys.reverse(), function next() { for (; keys.length;) { var key = keys.pop(); if (key in object) return next.value = key, next.done = !1, next; } return next.done = !0, next; }; }, exports.values = values, Context.prototype = { constructor: Context, reset: function reset(skipTempReset) { if (this.prev = 0, this.next = 0, this.sent = this._sent = undefined, this.done = !1, this.delegate = null, this.method = "next", this.arg = undefined, this.tryEntries.forEach(resetTryEntry), !skipTempReset) for (var name in this) { "t" === name.charAt(0) && hasOwn.call(this, name) && !isNaN(+name.slice(1)) && (this[name] = undefined); } }, stop: function stop() { this.done = !0; var rootRecord = this.tryEntries[0].completion; if ("throw" === rootRecord.type) throw rootRecord.arg; return this.rval; }, dispatchException: function dispatchException(exception) { if (this.done) throw exception; var context = this; function handle(loc, caught) { return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught; } for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i], record = entry.completion; if ("root" === entry.tryLoc) return handle("end"); if (entry.tryLoc <= this.prev) { var hasCatch = hasOwn.call(entry, "catchLoc"), hasFinally = hasOwn.call(entry, "finallyLoc"); if (hasCatch && hasFinally) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } else if (hasCatch) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); } else { if (!hasFinally) throw new Error("try statement without catch or finally"); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } } } }, abrupt: function abrupt(type, arg) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) { var finallyEntry = entry; break; } } finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null); var record = finallyEntry ? finallyEntry.completion : {}; return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record); }, complete: function complete(record, afterLoc) { if ("throw" === record.type) throw record.arg; return "break" === record.type || "continue" === record.type ? this.next = record.arg : "return" === record.type ? (this.rval = this.arg = record.arg, this.method = "return", this.next = "end") : "normal" === record.type && afterLoc && (this.next = afterLoc), ContinueSentinel; }, finish: function finish(finallyLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel; } }, "catch": function _catch(tryLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc === tryLoc) { var record = entry.completion; if ("throw" === record.type) { var thrown = record.arg; resetTryEntry(entry); } return thrown; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(iterable, resultName, nextLoc) { return this.delegate = { iterator: values(iterable), resultName: resultName, nextLoc: nextLoc }, "next" === this.method && (this.arg = undefined), ContinueSentinel; } }, exports; }
|
|
12
|
-
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
13
|
-
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
14
|
-
function systemLogin() {
|
|
15
|
-
var userData = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
|
|
16
|
-
var user = {};
|
|
17
|
-
if (userData) {
|
|
18
|
-
try {
|
|
19
|
-
user = JSON.parse(userData);
|
|
20
|
-
} catch (err) {}
|
|
21
|
-
}
|
|
22
|
-
return user;
|
|
23
|
-
}
|
|
24
|
-
function _default(cbLoginAPI) {
|
|
25
|
-
return /*#__PURE__*/function () {
|
|
26
|
-
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req, res, next) {
|
|
27
|
-
var headers, token, data;
|
|
28
|
-
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
29
|
-
while (1) {
|
|
30
|
-
switch (_context.prev = _context.next) {
|
|
31
|
-
case 0:
|
|
32
|
-
headers = req.headers;
|
|
33
|
-
if (!(headers['x-app-secret'] === process.env.SECRET_KEY)) {
|
|
34
|
-
_context.next = 5;
|
|
35
|
-
break;
|
|
36
|
-
}
|
|
37
|
-
req.user = systemLogin(headers['x-app-user']);
|
|
38
|
-
_context.next = 30;
|
|
39
|
-
break;
|
|
40
|
-
case 5:
|
|
41
|
-
token = null;
|
|
42
|
-
if (headers.authorization) {
|
|
43
|
-
token = headers.authorization;
|
|
44
|
-
}
|
|
45
|
-
if (!token) {
|
|
46
|
-
_context.next = 30;
|
|
47
|
-
break;
|
|
48
|
-
}
|
|
49
|
-
if (!token.startsWith('Bearer ')) {
|
|
50
|
-
_context.next = 22;
|
|
51
|
-
break;
|
|
52
|
-
}
|
|
53
|
-
token = token.replace('Bearer ', '');
|
|
54
|
-
_context.prev = 10;
|
|
55
|
-
req.user = _jsonwebtoken["default"].verify(token, process.env.SECRET_KEY);
|
|
56
|
-
return _context.abrupt("return", next());
|
|
57
|
-
case 15:
|
|
58
|
-
_context.prev = 15;
|
|
59
|
-
_context.t0 = _context["catch"](10);
|
|
60
|
-
if (!(_context.t0.name === 'TokenExpiredError')) {
|
|
61
|
-
_context.next = 19;
|
|
62
|
-
break;
|
|
63
|
-
}
|
|
64
|
-
throw new _error.ErrorException(_error.TOKEN_EXPIRED, _context.t0);
|
|
65
|
-
case 19:
|
|
66
|
-
throw new _error.ErrorException(_error.TOKEN_FAILED, _context.t0);
|
|
67
|
-
case 20:
|
|
68
|
-
_context.next = 30;
|
|
69
|
-
break;
|
|
70
|
-
case 22:
|
|
71
|
-
_context.next = 24;
|
|
72
|
-
return cbLoginAPI(token);
|
|
73
|
-
case 24:
|
|
74
|
-
data = _context.sent;
|
|
75
|
-
if (!data) {
|
|
76
|
-
_context.next = 29;
|
|
77
|
-
break;
|
|
78
|
-
}
|
|
79
|
-
req.user = data;
|
|
80
|
-
_context.next = 30;
|
|
81
|
-
break;
|
|
82
|
-
case 29:
|
|
83
|
-
throw new _error.ErrorException(_error.TOKEN_FAILED);
|
|
84
|
-
case 30:
|
|
85
|
-
return _context.abrupt("return", next());
|
|
86
|
-
case 31:
|
|
87
|
-
case "end":
|
|
88
|
-
return _context.stop();
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}, _callee, null, [[10, 15]]);
|
|
92
|
-
}));
|
|
93
|
-
return function (_x, _x2, _x3) {
|
|
94
|
-
return _ref.apply(this, arguments);
|
|
95
|
-
};
|
|
96
|
-
}();
|
|
97
|
-
}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import jwt from 'jsonwebtoken';
|
|
2
|
-
import {ErrorException, TOKEN_EXPIRED, TOKEN_FAILED} from '@azteam/error';
|
|
3
|
-
|
|
4
|
-
function systemLogin(userData = null) {
|
|
5
|
-
let user = {};
|
|
6
|
-
if (userData) {
|
|
7
|
-
try {
|
|
8
|
-
user = JSON.parse(userData);
|
|
9
|
-
} catch (err) {}
|
|
10
|
-
}
|
|
11
|
-
return user;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export default function () {
|
|
15
|
-
return async function (req, res, next) {
|
|
16
|
-
const {headers} = req;
|
|
17
|
-
|
|
18
|
-
if (headers['x-app-secret'] === process.env.SECRET_KEY) {
|
|
19
|
-
req.user = systemLogin(headers['x-app-user']);
|
|
20
|
-
} else {
|
|
21
|
-
let token = null;
|
|
22
|
-
|
|
23
|
-
if (headers.authorization) {
|
|
24
|
-
token = headers.authorization;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
if (token) {
|
|
28
|
-
if (token.startsWith('Bearer ')) {
|
|
29
|
-
token = token.replace('Bearer ', '');
|
|
30
|
-
|
|
31
|
-
try {
|
|
32
|
-
req.user = jwt.verify(token, process.env.SECRET_KEY);
|
|
33
|
-
return next();
|
|
34
|
-
} catch (err) {
|
|
35
|
-
if (err.name === 'TokenExpiredError') {
|
|
36
|
-
throw new ErrorException(TOKEN_EXPIRED, err);
|
|
37
|
-
}
|
|
38
|
-
throw new ErrorException(TOKEN_FAILED, err);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
return next();
|
|
45
|
-
};
|
|
46
|
-
}
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import jwt from 'jsonwebtoken';
|
|
2
|
-
import {ErrorException, TOKEN_EXPIRED, TOKEN_FAILED} from '@azteam/error';
|
|
3
|
-
|
|
4
|
-
function systemLogin(userData = null) {
|
|
5
|
-
let user = {};
|
|
6
|
-
if (userData) {
|
|
7
|
-
try {
|
|
8
|
-
user = JSON.parse(userData);
|
|
9
|
-
} catch (err) {}
|
|
10
|
-
}
|
|
11
|
-
return user;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export default function (cbLoginAPI) {
|
|
15
|
-
return async function (req, res, next) {
|
|
16
|
-
const {headers} = req;
|
|
17
|
-
|
|
18
|
-
if (headers['x-app-secret'] === process.env.SECRET_KEY) {
|
|
19
|
-
req.user = systemLogin(headers['x-app-user']);
|
|
20
|
-
} else {
|
|
21
|
-
let token = null;
|
|
22
|
-
|
|
23
|
-
if (headers.authorization) {
|
|
24
|
-
token = headers.authorization;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
if (token) {
|
|
28
|
-
if (token.startsWith('Bearer ')) {
|
|
29
|
-
token = token.replace('Bearer ', '');
|
|
30
|
-
|
|
31
|
-
try {
|
|
32
|
-
req.user = jwt.verify(token, process.env.SECRET_KEY);
|
|
33
|
-
return next();
|
|
34
|
-
} catch (err) {
|
|
35
|
-
if (err.name === 'TokenExpiredError') {
|
|
36
|
-
throw new ErrorException(TOKEN_EXPIRED, err);
|
|
37
|
-
}
|
|
38
|
-
throw new ErrorException(TOKEN_FAILED, err);
|
|
39
|
-
}
|
|
40
|
-
} else {
|
|
41
|
-
const data = await cbLoginAPI(token);
|
|
42
|
-
if (data) {
|
|
43
|
-
req.user = data;
|
|
44
|
-
} else {
|
|
45
|
-
throw new ErrorException(TOKEN_FAILED);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
return next();
|
|
52
|
-
};
|
|
53
|
-
}
|