mdkcontroller 1.0.4 → 1.0.6
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/demoApp.js +27 -3
- package/dk_modules/authorization.js +17 -3
- package/package.json +1 -1
package/demoApp.js
CHANGED
|
@@ -1,8 +1,32 @@
|
|
|
1
1
|
import svInstance from "./main.js";
|
|
2
|
+
import {Router} from "express";
|
|
3
|
+
// import svInstance from "mdkcontroller";
|
|
2
4
|
|
|
3
5
|
const _sv = await svInstance("khanhnbd");
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
+
const app = _sv.server;
|
|
7
|
+
const db = _sv.database;
|
|
8
|
+
const authCrypt = _sv.authen;
|
|
9
|
+
|
|
10
|
+
const tbMovie = db.data.yourTableName ?? [];
|
|
11
|
+
|
|
12
|
+
const newRouterApi = Router();
|
|
13
|
+
newRouterApi.get("/demoFunction/gets", authCrypt.validate, (req, res) => {
|
|
14
|
+
res.json({message: "Get all users"});
|
|
15
|
+
});
|
|
16
|
+
newRouterApi.get('/demoPhase2/CallNonAuth', async (req, res) => {
|
|
17
|
+
const accessToken = req.cookies?.access_token;
|
|
18
|
+
if (accessToken) {
|
|
19
|
+
const indexToken = tbMovie.findIndex(f => true);
|
|
20
|
+
tbMovie.slice(indexToken, 1);
|
|
21
|
+
await db.write();
|
|
22
|
+
}
|
|
23
|
+
res.redirect('/login');
|
|
24
|
+
});
|
|
25
|
+
app.use("/api", newRouterApi);
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
const enc = await authCrypt.encrypt("khanhnbd");
|
|
29
|
+
const dec = await authCrypt.decrypt(enc);
|
|
6
30
|
console.log("khanhnbd => " + enc);
|
|
7
|
-
console.log("
|
|
31
|
+
console.log(enc + " will => " + dec);
|
|
8
32
|
_sv.startListen(8905);
|
|
@@ -1,9 +1,21 @@
|
|
|
1
1
|
import child_process from "child_process";
|
|
2
2
|
|
|
3
|
+
function isCalledFromNodeModules() {
|
|
4
|
+
const error = new Error();
|
|
5
|
+
const stackLines = error.stack.split('\n');
|
|
6
|
+
// Bỏ qua dòng đầu tiên vì đó là thông tin của Error
|
|
7
|
+
for (let i = 1; i < stackLines.length; i++) {
|
|
8
|
+
if (stackLines[i].includes('node_modules')) {
|
|
9
|
+
return true;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
|
|
3
15
|
export default function (db) {
|
|
4
16
|
const tbLoginToken = db.data.loginTokens;
|
|
5
17
|
const tbUser = db.data.users;
|
|
6
|
-
|
|
18
|
+
const isModule = isCalledFromNodeModules();
|
|
7
19
|
const validateBase = function name(req, res) {
|
|
8
20
|
const accessToken = req.cookies?.access_token;
|
|
9
21
|
if (accessToken) {
|
|
@@ -46,9 +58,10 @@ export default function (db) {
|
|
|
46
58
|
return validateBase(req, res);
|
|
47
59
|
},
|
|
48
60
|
encrypt: async function (data = "", pw = "mdkpaswoEcrHidDefat") {
|
|
61
|
+
const cdtoCmd = isModule ? "node_modules\\mdkcontroller\\" : "";
|
|
49
62
|
const cmd = data + " " + pw;
|
|
50
63
|
return new Promise((resolve, reject) => {
|
|
51
|
-
child_process.exec(
|
|
64
|
+
child_process.exec(`${cdtoCmd}dk_modules\\EncryptC\\DKEncryptCMD e ${cmd}`, (error, stdout, stderr) => {
|
|
52
65
|
if (error) {
|
|
53
66
|
reject(error);
|
|
54
67
|
return;
|
|
@@ -58,9 +71,10 @@ export default function (db) {
|
|
|
58
71
|
});
|
|
59
72
|
},
|
|
60
73
|
decrypt: function (data = "", pw = "mdkpaswoEcrHidDefat") {
|
|
74
|
+
const cdtoCmd = isModule ? "node_modules\\mdkcontroller\\" : "";
|
|
61
75
|
const cmd = data + " " + pw;
|
|
62
76
|
return new Promise((resolve, reject) => {
|
|
63
|
-
child_process.exec(
|
|
77
|
+
child_process.exec(`${cdtoCmd}dk_modules\\EncryptC\\DKEncryptCMD d ${cmd}`, (error, stdout, stderr) => {
|
|
64
78
|
if (error) {
|
|
65
79
|
reject(error);
|
|
66
80
|
return;
|