@modular-rest/server 1.11.11 → 1.11.13
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 +1 -1
- package/src/application.js +13 -10
- package/src/middlewares.js +1 -0
- package/src/services/data_provider/service.js +15 -6
- package/src/services/jwt/router.js +43 -31
- package/types/middlewares.d.ts +1 -0
package/package.json
CHANGED
package/src/application.js
CHANGED
|
@@ -18,7 +18,10 @@ const defaultServiceRoot = __dirname + "/services";
|
|
|
18
18
|
* @typedef {import('./class/cms_trigger.js')} CmsTrigger
|
|
19
19
|
*/
|
|
20
20
|
|
|
21
|
-
const {
|
|
21
|
+
const {
|
|
22
|
+
config,
|
|
23
|
+
setConfig
|
|
24
|
+
} = require("./config");
|
|
22
25
|
|
|
23
26
|
/**
|
|
24
27
|
* Create a modular REST instance with Koa and MongoDB support.
|
|
@@ -147,17 +150,10 @@ async function createRest(options) {
|
|
|
147
150
|
mongoOption: config.mongo,
|
|
148
151
|
});
|
|
149
152
|
|
|
150
|
-
// 3. Setting up default services
|
|
151
|
-
try {
|
|
152
|
-
await require("./helper/presetup_services").setup(options);
|
|
153
|
-
} catch (e) {
|
|
154
|
-
return Promise.reject(e);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
153
|
/**
|
|
158
154
|
* User Services
|
|
159
155
|
*
|
|
160
|
-
* Plug in routes and database
|
|
156
|
+
* 3. Plug in routes and database
|
|
161
157
|
*/
|
|
162
158
|
if (config.modulesPath) {
|
|
163
159
|
// Plug in user routes
|
|
@@ -202,6 +198,13 @@ async function createRest(options) {
|
|
|
202
198
|
});
|
|
203
199
|
}
|
|
204
200
|
|
|
201
|
+
// 4. Setting up default services
|
|
202
|
+
try {
|
|
203
|
+
await require("./helper/presetup_services").setup(options);
|
|
204
|
+
} catch (e) {
|
|
205
|
+
return Promise.reject(e);
|
|
206
|
+
}
|
|
207
|
+
|
|
205
208
|
/**
|
|
206
209
|
* Run the server
|
|
207
210
|
*
|
|
@@ -233,4 +236,4 @@ async function createRest(options) {
|
|
|
233
236
|
});
|
|
234
237
|
}
|
|
235
238
|
|
|
236
|
-
module.exports = createRest;
|
|
239
|
+
module.exports = createRest;
|
package/src/middlewares.js
CHANGED
|
@@ -13,6 +13,7 @@ const userManager = require("./services/user_manager/service");
|
|
|
13
13
|
/**
|
|
14
14
|
* Authentication middleware
|
|
15
15
|
* It checks if incoming request has a valid token in header.authorization
|
|
16
|
+
* Then attaches the user object to ctx.state.user
|
|
16
17
|
*
|
|
17
18
|
* @param {Object} ctx - Koa context
|
|
18
19
|
* @param {Function} next - Koa next function
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
let name = "dataProvider";
|
|
2
2
|
const colog = require("colog");
|
|
3
|
-
let {
|
|
3
|
+
let {
|
|
4
|
+
AccessTypes,
|
|
5
|
+
AccessDefinition
|
|
6
|
+
} = require("../../class/security");
|
|
4
7
|
|
|
5
8
|
const Mongoose = require("mongoose");
|
|
6
9
|
Mongoose.set("useCreateIndex", true);
|
|
@@ -11,7 +14,9 @@ let permissionDefinitions = {};
|
|
|
11
14
|
|
|
12
15
|
let triggers = require("../../class/trigger_operator");
|
|
13
16
|
let TypeCasters = require("./typeCasters");
|
|
14
|
-
const {
|
|
17
|
+
const {
|
|
18
|
+
config
|
|
19
|
+
} = require("../../config");
|
|
15
20
|
|
|
16
21
|
/**
|
|
17
22
|
*
|
|
@@ -30,13 +35,14 @@ function connectToDatabaseByCollectionDefinitionList(
|
|
|
30
35
|
// Create db connection
|
|
31
36
|
//
|
|
32
37
|
const fullDbName = (mongoOption.dbPrefix || "") + dbName;
|
|
33
|
-
const connectionString = mongoOption.mongoBaseAddress
|
|
38
|
+
const connectionString = mongoOption.mongoBaseAddress;
|
|
34
39
|
|
|
35
|
-
colog.info(`- Connecting to database ${
|
|
40
|
+
colog.info(`- Connecting to database: ${fullDbName}`);
|
|
36
41
|
|
|
37
42
|
let connection = Mongoose.createConnection(connectionString, {
|
|
38
43
|
useUnifiedTopology: true,
|
|
39
44
|
useNewUrlParser: true,
|
|
45
|
+
dbName: fullDbName,
|
|
40
46
|
});
|
|
41
47
|
|
|
42
48
|
// Store connection
|
|
@@ -94,7 +100,10 @@ function connectToDatabaseByCollectionDefinitionList(
|
|
|
94
100
|
* @param {string} option.mongoOption.dbPrefix
|
|
95
101
|
* @param {string} option.mongoOption.mongoBaseAddress
|
|
96
102
|
*/
|
|
97
|
-
async function addCollectionDefinitionByList({
|
|
103
|
+
async function addCollectionDefinitionByList({
|
|
104
|
+
list,
|
|
105
|
+
mongoOption
|
|
106
|
+
}) {
|
|
98
107
|
let clusteredByDBName = {};
|
|
99
108
|
|
|
100
109
|
// cluster list by their database name.
|
|
@@ -250,4 +259,4 @@ module.exports = {
|
|
|
250
259
|
performAdditionalOptionsToQueryObject,
|
|
251
260
|
triggers,
|
|
252
261
|
TypeCasters,
|
|
253
|
-
};
|
|
262
|
+
};
|
|
@@ -7,64 +7,76 @@ let verify = new Router();
|
|
|
7
7
|
|
|
8
8
|
let service = require('./service').main;
|
|
9
9
|
|
|
10
|
-
verify.post('/token', async (ctx) =>
|
|
11
|
-
{
|
|
10
|
+
verify.post('/token', async (ctx) => {
|
|
12
11
|
let body = ctx.request.body;
|
|
13
12
|
|
|
14
13
|
// validate result
|
|
15
14
|
let bodyValidate = validateObject(body, 'token');
|
|
16
15
|
|
|
17
16
|
// fields validation
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
17
|
+
if (!bodyValidate.isValid) {
|
|
18
|
+
ctx.status = 412;
|
|
19
|
+
ctx.body = reply('e', {
|
|
20
|
+
'e': bodyValidate.requires
|
|
21
|
+
});
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
24
|
|
|
25
25
|
await service.verify(body.token)
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
26
|
+
.then((payload) => ctx.body = reply('s', {
|
|
27
|
+
'user': payload
|
|
28
|
+
}))
|
|
29
|
+
.catch(err => {
|
|
30
|
+
ctx.status = 412;
|
|
31
|
+
ctx.body = reply('e', {
|
|
32
|
+
'e': err
|
|
33
|
+
});
|
|
34
|
+
});
|
|
31
35
|
});
|
|
32
36
|
|
|
33
|
-
verify.post('/checkAccess', async (ctx) =>
|
|
34
|
-
{
|
|
37
|
+
verify.post('/checkAccess', async (ctx) => {
|
|
35
38
|
let body = ctx.request.body;
|
|
36
39
|
|
|
37
40
|
// validate result
|
|
38
41
|
let bodyValidate = validateObject(body, 'token permissionField');
|
|
39
42
|
|
|
40
43
|
// fields validation
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
44
|
+
if (!bodyValidate.isValid) {
|
|
45
|
+
ctx.status = 412;
|
|
46
|
+
ctx.body = reply('e', {
|
|
47
|
+
'e': bodyValidate.requires
|
|
48
|
+
});
|
|
49
|
+
return;
|
|
46
50
|
}
|
|
47
|
-
|
|
51
|
+
|
|
48
52
|
let payload = await service.verify(body.token)
|
|
49
53
|
.catch(err => {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
54
|
+
console.log(err);
|
|
55
|
+
ctx.throw(412, err.message);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
|
|
55
59
|
let userid = payload.id;
|
|
56
|
-
|
|
60
|
+
|
|
57
61
|
await global.services.userManager.main.getUserById(userid)
|
|
58
|
-
.then((user) =>
|
|
59
|
-
{
|
|
62
|
+
.then((user) => {
|
|
60
63
|
let key = user.hasPermission(body.permissionField);
|
|
61
|
-
ctx.body = reply('s', {
|
|
64
|
+
ctx.body = reply('s', {
|
|
65
|
+
'access': key
|
|
66
|
+
});
|
|
62
67
|
})
|
|
63
68
|
.catch(err => {
|
|
64
69
|
ctx.status = 412;
|
|
65
|
-
ctx.body = reply('e', {
|
|
70
|
+
ctx.body = reply('e', {
|
|
71
|
+
'e': err
|
|
72
|
+
});
|
|
66
73
|
});
|
|
67
74
|
});
|
|
68
75
|
|
|
76
|
+
verify.get('/ready', async (ctx) => {
|
|
77
|
+
// it's health check, so return success
|
|
78
|
+
ctx.body = reply('s', {});
|
|
79
|
+
});
|
|
80
|
+
|
|
69
81
|
module.exports.name = name;
|
|
70
82
|
module.exports.main = verify;
|
package/types/middlewares.d.ts
CHANGED