@azteam/express 1.2.521 → 1.2.523
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.
|
@@ -4,6 +4,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports["default"] = _default;
|
|
7
|
+
var _crypto = _interopRequireDefault(require("crypto"));
|
|
7
8
|
var _jsonwebtoken = _interopRequireDefault(require("jsonwebtoken"));
|
|
8
9
|
var _error = require("@azteam/error");
|
|
9
10
|
var _httpClient = _interopRequireDefault(require("@azteam/http-client"));
|
|
@@ -27,6 +28,17 @@ function systemLogin() {
|
|
|
27
28
|
}
|
|
28
29
|
return user;
|
|
29
30
|
}
|
|
31
|
+
function isValidAppSecret(received, expected) {
|
|
32
|
+
if (!expected || typeof received !== 'string' || received.length !== expected.length) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
return _crypto["default"].timingSafeEqual(Buffer.from(received), Buffer.from(expected));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Chỉ XÁC THỰC (authentication): gắn req.user từ x-app-secret / Bearer JWT / apikey.
|
|
40
|
+
* KHÔNG kiểm tra role hay level -> route admin phải dùng adminRoleMiddleware.
|
|
41
|
+
*/
|
|
30
42
|
function _default() {
|
|
31
43
|
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
32
44
|
var opts = _objectSpread({
|
|
@@ -35,17 +47,17 @@ function _default() {
|
|
|
35
47
|
}, options);
|
|
36
48
|
return /*#__PURE__*/function () {
|
|
37
49
|
var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(req, res, next) {
|
|
38
|
-
var headers, secretKey, systemURL, token,
|
|
50
|
+
var headers, secretKey, systemURL, token, splitToken, apiKey, authAPIURL, client, response;
|
|
39
51
|
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
40
52
|
while (1) switch (_context.prev = _context.next) {
|
|
41
53
|
case 0:
|
|
42
|
-
headers = req.headers, secretKey = opts.secretKey, systemURL = opts.systemURL;
|
|
43
|
-
if (!(headers['x-app-secret']
|
|
54
|
+
headers = req.headers, secretKey = opts.secretKey, systemURL = opts.systemURL; // secretKey chưa cấu hình thì undefined === undefined -> trước đây mọi request đều qua
|
|
55
|
+
if (!isValidAppSecret(headers['x-app-secret'], secretKey)) {
|
|
44
56
|
_context.next = 5;
|
|
45
57
|
break;
|
|
46
58
|
}
|
|
47
59
|
req.user = systemLogin(headers['x-app-user']);
|
|
48
|
-
_context.next =
|
|
60
|
+
_context.next = 35;
|
|
49
61
|
break;
|
|
50
62
|
case 5:
|
|
51
63
|
token = null;
|
|
@@ -53,7 +65,7 @@ function _default() {
|
|
|
53
65
|
token = headers.authorization;
|
|
54
66
|
}
|
|
55
67
|
if (!token) {
|
|
56
|
-
_context.next =
|
|
68
|
+
_context.next = 35;
|
|
57
69
|
break;
|
|
58
70
|
}
|
|
59
71
|
if (!token.startsWith('Bearer ')) {
|
|
@@ -77,43 +89,50 @@ function _default() {
|
|
|
77
89
|
throw new _error.ErrorException(_error.TOKEN_FAILED, _context.t0);
|
|
78
90
|
case 19:
|
|
79
91
|
if (!token.startsWith('apikey ')) {
|
|
80
|
-
_context.next =
|
|
92
|
+
_context.next = 35;
|
|
81
93
|
break;
|
|
82
94
|
}
|
|
83
95
|
token = token.replace('apikey ', '');
|
|
84
|
-
|
|
96
|
+
|
|
97
|
+
// Định dạng cũ "<domain>:<api_key>": không dùng req.hostname (lấy từ Host / X-Forwarded-Host do client gửi)
|
|
98
|
+
// để tránh attacker trỏ việc xác thực về server của họ -> luôn xác thực qua systemURL
|
|
85
99
|
splitToken = token.split(':');
|
|
86
|
-
|
|
87
|
-
|
|
100
|
+
apiKey = splitToken.length === 2 ? splitToken[1] : token;
|
|
101
|
+
if (!(!systemURL || !apiKey)) {
|
|
102
|
+
_context.next = 25;
|
|
103
|
+
break;
|
|
88
104
|
}
|
|
105
|
+
throw new _error.ErrorException(_error.TOKEN_FAILED);
|
|
106
|
+
case 25:
|
|
107
|
+
authAPIURL = "".concat(systemURL, "/auth/login_api/").concat(encodeURIComponent(apiKey));
|
|
89
108
|
client = new _httpClient["default"]({
|
|
90
109
|
timeout: 20000,
|
|
91
110
|
headers: {
|
|
92
111
|
'User-Agent': req.get('User-Agent')
|
|
93
112
|
}
|
|
94
113
|
});
|
|
95
|
-
_context.next =
|
|
114
|
+
_context.next = 29;
|
|
96
115
|
return client.get(authAPIURL);
|
|
97
|
-
case
|
|
116
|
+
case 29:
|
|
98
117
|
response = _context.sent;
|
|
99
118
|
if (!response.success) {
|
|
100
|
-
_context.next =
|
|
119
|
+
_context.next = 34;
|
|
101
120
|
break;
|
|
102
121
|
}
|
|
103
122
|
req.user = response.data;
|
|
104
|
-
_context.next =
|
|
123
|
+
_context.next = 35;
|
|
105
124
|
break;
|
|
106
|
-
case
|
|
125
|
+
case 34:
|
|
107
126
|
throw new _error.ErrorException(_error.TOKEN_FAILED);
|
|
108
|
-
case
|
|
127
|
+
case 35:
|
|
109
128
|
if (req.user) {
|
|
110
|
-
_context.next =
|
|
129
|
+
_context.next = 37;
|
|
111
130
|
break;
|
|
112
131
|
}
|
|
113
132
|
throw new _error.ErrorException(_error.UNAUTHORIZED);
|
|
114
|
-
case
|
|
133
|
+
case 37:
|
|
115
134
|
return _context.abrupt("return", next());
|
|
116
|
-
case
|
|
135
|
+
case 38:
|
|
117
136
|
case "end":
|
|
118
137
|
return _context.stop();
|
|
119
138
|
}
|
|
@@ -5,9 +5,10 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports["default"] = _default;
|
|
7
7
|
var _constant = require("@azteam/constant");
|
|
8
|
-
var
|
|
8
|
+
var _adminRoleMiddleware = _interopRequireDefault(require("./adminRoleMiddleware"));
|
|
9
9
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
|
|
10
|
+
// roleMiddleware chỉ nhận (options) và không kiểm tra level -> trước đây mọi user đăng nhập đều qua
|
|
10
11
|
function _default() {
|
|
11
12
|
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
12
|
-
return (0,
|
|
13
|
+
return (0, _adminRoleMiddleware["default"])(null, options, _constant.USER_LEVEL.SYSTEM);
|
|
13
14
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@azteam/express",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.523",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "toda <sp.azsolution.net@gmail.com>",
|
|
6
6
|
"main": "./lib/index.js",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"@azteam/error": "1.0.41",
|
|
15
15
|
"@azteam/http-client": "1.0.210",
|
|
16
16
|
"@azteam/util": "1.0.65",
|
|
17
|
-
"@azteam/validator": "1.0.
|
|
17
|
+
"@azteam/validator": "1.0.31",
|
|
18
18
|
"@grpc/grpc-js": "1.6.7",
|
|
19
19
|
"@grpc/proto-loader": "0.6.12",
|
|
20
20
|
"@socket.io/redis-adapter": "7.2.0",
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import crypto from 'crypto';
|
|
1
2
|
import jwt from 'jsonwebtoken';
|
|
2
3
|
|
|
3
4
|
import {ErrorException, TOKEN_EXPIRED, TOKEN_FAILED, UNAUTHORIZED} from '@azteam/error';
|
|
@@ -13,6 +14,17 @@ function systemLogin(userData = null) {
|
|
|
13
14
|
return user;
|
|
14
15
|
}
|
|
15
16
|
|
|
17
|
+
function isValidAppSecret(received, expected) {
|
|
18
|
+
if (!expected || typeof received !== 'string' || received.length !== expected.length) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
return crypto.timingSafeEqual(Buffer.from(received), Buffer.from(expected));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Chỉ XÁC THỰC (authentication): gắn req.user từ x-app-secret / Bearer JWT / apikey.
|
|
26
|
+
* KHÔNG kiểm tra role hay level -> route admin phải dùng adminRoleMiddleware.
|
|
27
|
+
*/
|
|
16
28
|
export default function (options = {}) {
|
|
17
29
|
const opts = {
|
|
18
30
|
secretKey: process.env.SECRET_KEY,
|
|
@@ -24,7 +36,8 @@ export default function (options = {}) {
|
|
|
24
36
|
const {headers} = req,
|
|
25
37
|
{secretKey, systemURL} = opts;
|
|
26
38
|
|
|
27
|
-
|
|
39
|
+
// secretKey chưa cấu hình thì undefined === undefined -> trước đây mọi request đều qua
|
|
40
|
+
if (isValidAppSecret(headers['x-app-secret'], secretKey)) {
|
|
28
41
|
req.user = systemLogin(headers['x-app-user']);
|
|
29
42
|
} else {
|
|
30
43
|
let token = null;
|
|
@@ -49,12 +62,15 @@ export default function (options = {}) {
|
|
|
49
62
|
|
|
50
63
|
if (token.startsWith('apikey ')) {
|
|
51
64
|
token = token.replace('apikey ', '');
|
|
52
|
-
let authAPIURL = `${systemURL}/auth/login_api/${token}`;
|
|
53
65
|
|
|
66
|
+
// Định dạng cũ "<domain>:<api_key>": không dùng req.hostname (lấy từ Host / X-Forwarded-Host do client gửi)
|
|
67
|
+
// để tránh attacker trỏ việc xác thực về server của họ -> luôn xác thực qua systemURL
|
|
54
68
|
const splitToken = token.split(':');
|
|
55
|
-
|
|
56
|
-
|
|
69
|
+
const apiKey = splitToken.length === 2 ? splitToken[1] : token;
|
|
70
|
+
if (!systemURL || !apiKey) {
|
|
71
|
+
throw new ErrorException(TOKEN_FAILED);
|
|
57
72
|
}
|
|
73
|
+
const authAPIURL = `${systemURL}/auth/login_api/${encodeURIComponent(apiKey)}`;
|
|
58
74
|
|
|
59
75
|
const client = new HttpClient({
|
|
60
76
|
timeout: 20000,
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import {USER_LEVEL} from '@azteam/constant';
|
|
2
2
|
|
|
3
|
-
import
|
|
3
|
+
import adminRoleMiddleware from './adminRoleMiddleware';
|
|
4
4
|
|
|
5
|
+
// roleMiddleware chỉ nhận (options) và không kiểm tra level -> trước đây mọi user đăng nhập đều qua
|
|
5
6
|
export default function (options = {}) {
|
|
6
|
-
return
|
|
7
|
+
return adminRoleMiddleware(null, options, USER_LEVEL.SYSTEM);
|
|
7
8
|
}
|