@azteam/express 1.2.212 → 1.2.215

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@azteam/express",
3
- "version": "1.2.212",
3
+ "version": "1.2.215",
4
4
  "main": "src/index.js",
5
5
  "engines": {
6
6
  "node": ">= 12.0.0",
@@ -11,7 +11,7 @@
11
11
  "dependencies": {
12
12
  "@azteam/crypto": "1.0.24",
13
13
  "@azteam/error": "1.0.25",
14
- "@azteam/http-client": "1.0.94",
14
+ "@azteam/http-client": "1.0.95",
15
15
  "@grpc/grpc-js": "1.6.7",
16
16
  "@grpc/proto-loader": "0.6.12",
17
17
  "body-parser": "1.19.0",
package/src/Controller.js CHANGED
@@ -6,12 +6,12 @@ class Controller {
6
6
 
7
7
  publicRouter() {
8
8
  let child = this;
9
- let result = [];
9
+ const result = [];
10
10
 
11
11
  while (Object.getPrototypeOf(child.__proto__)) {
12
12
  const data = Object.getOwnPropertyNames(Object.getPrototypeOf(child));
13
13
 
14
- data.map((methodName) => {
14
+ data.map(function (methodName) {
15
15
  const matches = methodName.match(/^(get|post|put|patch|delete)/);
16
16
  if (matches) {
17
17
  let path = '/';
@@ -27,6 +27,7 @@ class Controller {
27
27
  path,
28
28
  });
29
29
  }
30
+ return true;
30
31
  });
31
32
  child = child.__proto__;
32
33
  }
package/src/Server.js CHANGED
@@ -10,7 +10,9 @@ import morgan from 'morgan';
10
10
  import cors from 'cors';
11
11
  import _ from 'lodash';
12
12
  import 'express-async-errors';
13
- import {errorCatch, CORS, ErrorException, NOT_FOUND, UNKNOWN} from '@azteam/error';
13
+ import {CORS, errorCatch, ErrorException, UNKNOWN} from '@azteam/error';
14
+
15
+ import {authMiddleware} from './middleware/authMiddleware';
14
16
 
15
17
  const RES_TYPE = {
16
18
  ARRAY: 'ARRAY',
@@ -104,11 +106,15 @@ class Server {
104
106
  if (apiDir) {
105
107
  const controllerDirs = fs.readdirSync(apiDir);
106
108
 
107
- for (const dirName of controllerDirs) {
109
+ for (let i = 0; i < controllerDirs.length; i += 1) {
110
+ const dirName = controllerDirs[i];
108
111
  if (fs.statSync(`${apiDir}/${dirName}`).isDirectory()) {
109
112
  const versionDirs = fs.readdirSync(`${apiDir}/${dirName}`);
110
113
 
111
- for (const versionName of versionDirs) {
114
+ for (let j = 0; j < versionDirs.length; j += 1) {
115
+ const versionName = versionDirs[j];
116
+
117
+ // eslint-disable-next-line global-require,import/no-dynamic-require
112
118
  const controller = require(`${apiDir}/${dirName}/${versionName}/controller`).default;
113
119
  this.addController(dirName, versionName, controller);
114
120
  }
@@ -250,7 +256,7 @@ class Server {
250
256
  });
251
257
  };
252
258
 
253
- app.use(async function (req, res, next) {
259
+ app.use(function (req, res, next) {
254
260
  delete res.cache;
255
261
 
256
262
  req.trackDevice = {
@@ -267,6 +273,8 @@ class Server {
267
273
  app.use(middleware);
268
274
  });
269
275
 
276
+ app.use(authMiddleware);
277
+
270
278
  const msg = [];
271
279
  _.map(this.controllers, (data) => {
272
280
  const {controller} = data;
@@ -303,10 +311,6 @@ class Server {
303
311
  return res.success('welcome');
304
312
  });
305
313
 
306
- app.use((req, res) => {
307
- throw new ErrorException(NOT_FOUND);
308
- });
309
-
310
314
  app.use((err, req, res, next) => {
311
315
  const error = errorCatch(err);
312
316
 
@@ -1,4 +1,5 @@
1
1
  import jwt from 'jsonwebtoken';
2
+ import {ErrorException, TOKEN_EXPIRED, TOKEN_FAILED, UNKNOWN} from '@azteam/error';
2
3
 
3
4
  function systemLogin(userData = null) {
4
5
  let user = {};
@@ -10,51 +11,35 @@ function systemLogin(userData = null) {
10
11
  return user;
11
12
  }
12
13
 
13
- export default function (cbRefreshToken, cbLoginAPI) {
14
- return async function (req, res, next) {
15
- const {headers, signedCookies} = req;
14
+ export function authMiddleware(req, res, next) {
15
+ const {headers} = req;
16
16
 
17
- if (headers['x-app-secret'] === process.env.SECRET_KEY) {
18
- req.user = systemLogin(headers['x-app-user']);
19
- } else {
20
- let token = signedCookies.access_token;
17
+ if (headers['x-app-secret'] === process.env.SECRET_KEY) {
18
+ req.user = systemLogin(headers['x-app-user']);
19
+ } else {
20
+ let token = null;
21
21
 
22
- if (headers.authorization && signedCookies.api_key !== headers.authorization) {
23
- token = headers.authorization;
24
- }
22
+ if (headers.authorization) {
23
+ token = headers.authorization;
24
+ }
25
25
 
26
- if (token) {
27
- token = token.replace('Bearer ', '');
28
- return jwt.verify(token, process.env.SECRET_KEY, async (error, jwtData) => {
29
- if (error) {
30
- try {
31
- let data = null;
32
- if (error.name === 'TokenExpiredError') {
33
- if (signedCookies.refresh_token) {
34
- data = await cbRefreshToken(signedCookies.refresh_token);
35
- } else if (signedCookies.api_key) {
36
- data = await cbLoginAPI(signedCookies.api_key);
37
- }
38
- } else if (error.name === 'JsonWebTokenError') {
39
- data = await cbLoginAPI(token);
40
- }
41
- if (data) {
42
- jwtData = jwt.decode(data.access_token);
43
- res.addCookie({
44
- access_token: data.access_token,
45
- });
46
- res.set('Auth-Token', data.access_token);
47
- }
48
- } catch (e) {}
49
- }
50
- if (jwtData) {
51
- req.user = jwtData;
52
- }
26
+ if (token) {
27
+ if (token.startsWith('JWT ')) {
28
+ token = token.replace('JWT ', '');
29
+
30
+ try {
31
+ req.user = jwt.verify(token, process.env.SECRET_KEY);
53
32
  return next();
54
- });
33
+ } catch (err) {
34
+ if (err.name === 'TokenExpiredError') {
35
+ throw new ErrorException(TOKEN_EXPIRED, err);
36
+ }
37
+ throw new ErrorException(UNKNOWN, err);
38
+ }
55
39
  }
40
+ throw new ErrorException(TOKEN_FAILED, ['Token type failed']);
56
41
  }
42
+ }
57
43
 
58
- return next();
59
- };
44
+ return next();
60
45
  }
@@ -26,12 +26,11 @@ export default function (key, options = {}) {
26
26
  const cacheData = await redis.get(cacheKey);
27
27
  if (cacheData) {
28
28
  return res.json(cacheData);
29
- } else {
30
- res.cache = {
31
- key: cacheKey,
32
- ttl: options.ttl,
33
- };
34
29
  }
30
+ res.cache = {
31
+ key: cacheKey,
32
+ ttl: options.ttl,
33
+ };
35
34
  }
36
35
  return next();
37
36
  };
@@ -1,6 +1,5 @@
1
1
  export {default as signMiddleware} from './signMiddleware';
2
2
  export {default as etagMiddleware} from './etagMiddleware';
3
- export {default as authMiddleware} from './authMiddleware';
4
3
  export {default as roleMiddleware} from './roleMiddleware';
5
4
  export {default as adminRoleMiddleware} from './adminRoleMiddleware';
6
5
  export {default as systemRoleMiddleware} from './systemRoleMiddleware';