mdkcontroller 1.0.14 → 1.0.16
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 +2 -2
- package/dk_modules/dkvar.js +2 -38
- package/dk_modules/users.js +19 -20
- package/index.js +7 -0
- package/package.json +2 -1
package/demoApp.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import {Root} from "./index.js";
|
|
2
2
|
import {Router} from "express";
|
|
3
3
|
// import svInstance from "mdkcontroller";
|
|
4
4
|
|
|
@@ -7,7 +7,7 @@ const cfg = {
|
|
|
7
7
|
res.redirect("/pages/DemoHomeV1/home");
|
|
8
8
|
}
|
|
9
9
|
}
|
|
10
|
-
const _sv = await
|
|
10
|
+
const _sv = await Root("khanhnbd", cfg);
|
|
11
11
|
const app = _sv.server;
|
|
12
12
|
const db = _sv.database;
|
|
13
13
|
const authCrypt = _sv.authen;
|
package/dk_modules/dkvar.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {Buffer} from 'node:buffer';
|
|
2
2
|
|
|
3
|
-
const algorithm = "aes-192-cbc";
|
|
4
3
|
export const privateKey = 'eyJzdWIiOiI5Nzc4IiwibmFtZSI6IkRraGFuaE1NTSIsImlhdCI6ODkxMDB9';
|
|
5
4
|
export const allowRegister = true;
|
|
5
|
+
|
|
6
6
|
export function generateRandomToken(length = 100) {
|
|
7
7
|
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
8
8
|
let token = '';
|
|
@@ -11,40 +11,4 @@ export function generateRandomToken(length = 100) {
|
|
|
11
11
|
token += characters.charAt(randomIndex);
|
|
12
12
|
}
|
|
13
13
|
return token;
|
|
14
|
-
}
|
|
15
|
-
export function decodeMini(text, pass) {
|
|
16
|
-
const byteArray = Buffer.from(text, 'base64');
|
|
17
|
-
const byteArray2 = Buffer.from(pass.toString(), 'utf8');
|
|
18
|
-
|
|
19
|
-
const originalArray1 = [];
|
|
20
|
-
// const originalArray2 = [];
|
|
21
|
-
for (let i = 0; i < byteArray.length; i++) {
|
|
22
|
-
if (i % 2 === 0 && originalArray1.length < (byteArray.length - byteArray2.length)) {
|
|
23
|
-
originalArray1.push(byteArray[i]);
|
|
24
|
-
}
|
|
25
|
-
// else {
|
|
26
|
-
// originalArray2.push(byteArray[i]);
|
|
27
|
-
// }
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const string1 = Buffer.from(originalArray1).toString('utf8');
|
|
31
|
-
// const string2 = Buffer.from(originalArray2).toString('utf8');
|
|
32
|
-
|
|
33
|
-
return string1;
|
|
34
|
-
}
|
|
35
|
-
export function encodeMini(value, pass) {
|
|
36
|
-
const byteArray1 = Buffer.from(value.toString(), 'utf8');
|
|
37
|
-
const byteArray2 = Buffer.from(pass.toString(), 'utf8');
|
|
38
|
-
|
|
39
|
-
const interleavedArray = [];
|
|
40
|
-
const maxLength = Math.max(byteArray1.length, byteArray2.length);
|
|
41
|
-
for (let i = 0; i < maxLength; i++) {
|
|
42
|
-
if (i < byteArray1.length) {
|
|
43
|
-
interleavedArray.push(byteArray1[i]);
|
|
44
|
-
}
|
|
45
|
-
if (i < byteArray2.length) {
|
|
46
|
-
interleavedArray.push(byteArray2[i]);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
return Buffer.from(interleavedArray).toString('base64');
|
|
50
14
|
}
|
package/dk_modules/users.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import authInstant from "./authorization.js";
|
|
2
2
|
import autoSequenceInstant from "./autoSequence.js";
|
|
3
|
-
import {
|
|
3
|
+
import {allowRegister, generateRandomToken, privateKey} from "./dkvar.js";
|
|
4
|
+
|
|
4
5
|
export default (router, db) => {
|
|
5
6
|
|
|
6
7
|
const auth = authInstant(db);
|
|
@@ -10,13 +11,13 @@ export default (router, db) => {
|
|
|
10
11
|
|
|
11
12
|
|
|
12
13
|
router.get("/users/gets", auth.validate, (req, res) => {
|
|
13
|
-
res.json({
|
|
14
|
+
res.json({message: "Get all users"});
|
|
14
15
|
});
|
|
15
16
|
|
|
16
17
|
router.get("/users/get", auth.validate, (req, res) => {
|
|
17
18
|
const userId = req.params.id;
|
|
18
|
-
const userO = tbUser.find({
|
|
19
|
-
res.json({
|
|
19
|
+
const userO = tbUser.find({id: parseInt(userId)});
|
|
20
|
+
res.json({message: `Get user with ID ${userId}`, user: userO});
|
|
20
21
|
});
|
|
21
22
|
router.get('/users/logout', async (req, res) => {
|
|
22
23
|
const accessToken = req.cookies?.access_token;
|
|
@@ -25,7 +26,7 @@ export default (router, db) => {
|
|
|
25
26
|
tbLoginToken.slice(indexToken, 1);
|
|
26
27
|
await db.write();
|
|
27
28
|
}
|
|
28
|
-
res.clearCookie('access_token', {
|
|
29
|
+
res.clearCookie('access_token', {httpOnly: true});
|
|
29
30
|
res.clearCookie('sessionUExt', {});
|
|
30
31
|
res.redirect('/login');
|
|
31
32
|
});
|
|
@@ -35,7 +36,8 @@ export default (router, db) => {
|
|
|
35
36
|
if (bodyParser.userName && bodyParser.password) {
|
|
36
37
|
let userLoging = tbUser.find(f => f.userName == bodyParser.userName.toLowerCase() && f.password == bodyParser.password);
|
|
37
38
|
if (userLoging) {
|
|
38
|
-
const
|
|
39
|
+
const userIdCode = await auth.encrypt(userLoging.userName);
|
|
40
|
+
const accessToken = userIdCode + generateRandomToken(100);
|
|
39
41
|
|
|
40
42
|
const expiredValue = new Date();
|
|
41
43
|
expiredValue.setMonth(expiredValue.getMonth() + 1);
|
|
@@ -47,14 +49,14 @@ export default (router, db) => {
|
|
|
47
49
|
expired: expiredValue
|
|
48
50
|
});
|
|
49
51
|
await db.write();
|
|
50
|
-
res.cookie('access_token', accessToken, {
|
|
51
|
-
res.cookie('sessionUExt',
|
|
52
|
-
res.json({
|
|
52
|
+
res.cookie('access_token', accessToken, {httpOnly: true, expires: expiredValue});
|
|
53
|
+
res.cookie('sessionUExt', userIdCode, {expires: expiredValue});
|
|
54
|
+
res.json({message: `Đăng nhập thành công.`, success: true});
|
|
53
55
|
} else {
|
|
54
|
-
res.json({
|
|
56
|
+
res.json({message: `Tài khoản hoặc mật khẩu không chính xác.`, success: false});
|
|
55
57
|
}
|
|
56
58
|
} else {
|
|
57
|
-
res.json({
|
|
59
|
+
res.json({message: `Không đủ thông tin để đăng nhập.`, success: false});
|
|
58
60
|
}
|
|
59
61
|
});
|
|
60
62
|
router.get("/users/tryGetAccess", (req, res) => {
|
|
@@ -63,13 +65,13 @@ export default (router, db) => {
|
|
|
63
65
|
const accessToken = req.cookies?.access_token;
|
|
64
66
|
const expiredValue = new Date();
|
|
65
67
|
expiredValue.setMonth(expiredValue.getMonth() + 1);
|
|
66
|
-
res.cookie('access_token', accessToken, {
|
|
68
|
+
res.cookie('access_token', accessToken, {httpOnly: true, expires: expiredValue});
|
|
67
69
|
}
|
|
68
|
-
res.json({
|
|
70
|
+
res.json({success: true, isLogin: isLogin});
|
|
69
71
|
});
|
|
70
72
|
router.post("/users/create", async (req, res) => {
|
|
71
73
|
if (!allowRegister) {
|
|
72
|
-
res.json({
|
|
74
|
+
res.json({message: `Chức năng đăng ký tài khoản mới không được cung cấp.`, success: false});
|
|
73
75
|
return;
|
|
74
76
|
}
|
|
75
77
|
const bodyParser = req.body;
|
|
@@ -78,7 +80,7 @@ export default (router, db) => {
|
|
|
78
80
|
if (!newUser) {
|
|
79
81
|
let validateUserName = tbUser.find(f => f.userName == bodyParser.userName.toLowerCase());
|
|
80
82
|
if (validateUserName) {
|
|
81
|
-
res.json({
|
|
83
|
+
res.json({message: `Tên tài khoản đã có người đăng ký.`, success: false});
|
|
82
84
|
return;
|
|
83
85
|
}
|
|
84
86
|
tbUser
|
|
@@ -105,19 +107,16 @@ export default (router, db) => {
|
|
|
105
107
|
});
|
|
106
108
|
}
|
|
107
109
|
} else {
|
|
108
|
-
res.json({
|
|
110
|
+
res.json({message: `Không đủ thông tin để tạo User mới.`, success: false});
|
|
109
111
|
}
|
|
110
112
|
});
|
|
111
113
|
|
|
112
114
|
console.log(
|
|
113
115
|
"Module dk_modules/users.js: (db)=>{ router: router, table: tbUser }"
|
|
114
116
|
);
|
|
115
|
-
return {
|
|
117
|
+
return {router: router, table: tbUser};
|
|
116
118
|
};
|
|
117
119
|
|
|
118
|
-
function generateToken(subText) {
|
|
119
|
-
return encodeMini(subText + generateRandomToken(100), privateKey);
|
|
120
|
-
}
|
|
121
120
|
// // Thêm một người dùng mới
|
|
122
121
|
// users
|
|
123
122
|
// .push({ id: 3, name: 'Bob Smith' })
|
package/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import main from "./main.js"
|
|
2
|
+
import authen from "./dk_modules/authorization.js"
|
|
3
|
+
import ause from "./dk_modules/autoSequence.js"
|
|
4
|
+
|
|
5
|
+
export const Root = async (a, b) => await main(a, b);
|
|
6
|
+
export const Authen = (db) => authen(db);
|
|
7
|
+
export const Sequense = (db) => ause(db);
|
package/package.json
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"socket.io": "^4.7.2"
|
|
11
11
|
},
|
|
12
12
|
"name": "mdkcontroller",
|
|
13
|
-
"version": "1.0.
|
|
13
|
+
"version": "1.0.16",
|
|
14
14
|
"keywords": [],
|
|
15
15
|
"author": "KHANHNBD <khanh272421@gmail.com>",
|
|
16
16
|
"license": "ISC",
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
"files": [
|
|
24
24
|
"demoApp.js",
|
|
25
25
|
"main.js",
|
|
26
|
+
"index.js",
|
|
26
27
|
"Cores/",
|
|
27
28
|
"dk_modules/",
|
|
28
29
|
"Pages/"
|