mbkauthe 1.1.2 → 1.1.4
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/.env.example +2 -1
- package/README.md +2 -1
- package/env.md +3 -1
- package/index.js +1 -0
- package/lib/authapi.js +57 -0
- package/lib/info.js +14 -8
- package/lib/validateSessionAndRole.js +6 -0
- package/package.json +1 -1
package/.env.example
CHANGED
|
@@ -8,7 +8,8 @@ mbkautheVar='{
|
|
|
8
8
|
"LOGIN_DB": "postgres://username:password@host:port/database",
|
|
9
9
|
"MBKAUTH_TWO_FA_ENABLE": "false",
|
|
10
10
|
"COOKIE_EXPIRE_TIME": 2,
|
|
11
|
-
"DOMAIN": "yourdomain.com"
|
|
11
|
+
"DOMAIN": "yourdomain.com",
|
|
12
|
+
"layout": false
|
|
12
13
|
}'
|
|
13
14
|
|
|
14
15
|
# See env.md for more details
|
package/README.md
CHANGED
package/env.md
CHANGED
package/index.js
CHANGED
|
@@ -47,4 +47,5 @@ if (process.env.test === "true") {
|
|
|
47
47
|
|
|
48
48
|
export { validateSession, checkRolePermission, validateSessionAndRole, getUserData, authenticate } from "./lib/validateSessionAndRole.js";
|
|
49
49
|
export { dblogin } from "./lib/pool.js";
|
|
50
|
+
export { authapi } from "./lib/authapi.js";
|
|
50
51
|
export default router;
|
package/lib/authapi.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { pool } from "./pool.js";
|
|
2
|
+
|
|
3
|
+
export const authapi = () => {
|
|
4
|
+
return (req, res, next) => {
|
|
5
|
+
const token = req.headers["authorization"];
|
|
6
|
+
|
|
7
|
+
// Query to check if the token exists in UserAuthApiKey table
|
|
8
|
+
const tokenQuery = 'SELECT * FROM "UserAuthApiKey" WHERE "key" = $1';
|
|
9
|
+
pool.query(tokenQuery, [token], (err, result) => {
|
|
10
|
+
if (err) {
|
|
11
|
+
console.error("Database query error:", err);
|
|
12
|
+
return res
|
|
13
|
+
.status(500)
|
|
14
|
+
.json({ success: false, message: "Internal Server Error" });
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (result.rows.length === 0) {
|
|
18
|
+
console.log("Invalid token");
|
|
19
|
+
return res
|
|
20
|
+
.status(401)
|
|
21
|
+
.json({ success: false, message: "The AuthApiToken Is InValid" });
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const username = result.rows[0].username;
|
|
25
|
+
|
|
26
|
+
// Query to check if the user exists and is active in Users table
|
|
27
|
+
const userQuery =
|
|
28
|
+
'SELECT * FROM "Users" WHERE "UserName" = $1 AND "Active" = true';
|
|
29
|
+
pool.query(userQuery, [username], (err, userResult) => {
|
|
30
|
+
if (username === "demo") {
|
|
31
|
+
console.log("Demo user is not allowed to access this endpoint");
|
|
32
|
+
return res.status(401).json({
|
|
33
|
+
success: false,
|
|
34
|
+
message: "Demo user is not allowed to access endpoints",
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
if (err) {
|
|
38
|
+
console.error("Database query error:", err);
|
|
39
|
+
return res
|
|
40
|
+
.status(500)
|
|
41
|
+
.json({ success: false, message: "Internal Server Error" });
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (userResult.rows.length === 0) {
|
|
45
|
+
console.log("User does not exist or is not active");
|
|
46
|
+
return res.status(401).json({
|
|
47
|
+
success: false,
|
|
48
|
+
message: "User does not exist or is not active",
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
console.log("Token and user are valid");
|
|
53
|
+
next();
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
};
|
package/lib/info.js
CHANGED
|
@@ -507,9 +507,15 @@ router.get(["/mbkauthe/"], async (_, res) => {
|
|
|
507
507
|
}
|
|
508
508
|
|
|
509
509
|
// Read and process file
|
|
510
|
-
|
|
510
|
+
let readmePath;
|
|
511
|
+
if (process.env.test === "true") {
|
|
512
|
+
readmePath = path.join(process.cwd(), "README.md");
|
|
513
|
+
}
|
|
514
|
+
else {
|
|
515
|
+
readmePath = path.join(process.cwd(), "./node_modules/mbkauthe/README.md");
|
|
516
|
+
}
|
|
511
517
|
const data = await fs.promises.readFile(readmePath, "utf8");
|
|
512
|
-
|
|
518
|
+
|
|
513
519
|
// Convert markdown to HTML
|
|
514
520
|
let html = marked(data, {
|
|
515
521
|
breaks: true,
|
|
@@ -519,9 +525,9 @@ router.get(["/mbkauthe/"], async (_, res) => {
|
|
|
519
525
|
|
|
520
526
|
// Process HTML with cheerio
|
|
521
527
|
const $ = cheerio.load(html);
|
|
522
|
-
|
|
528
|
+
|
|
523
529
|
// Add IDs to headers for anchor links
|
|
524
|
-
$('h1, h2, h3, h4, h5, h6').each(function() {
|
|
530
|
+
$('h1, h2, h3, h4, h5, h6').each(function () {
|
|
525
531
|
const id = $(this).text()
|
|
526
532
|
.toLowerCase()
|
|
527
533
|
.replace(/\s+/g, '-')
|
|
@@ -529,9 +535,9 @@ router.get(["/mbkauthe/"], async (_, res) => {
|
|
|
529
535
|
$(this).attr('id', id);
|
|
530
536
|
$(this).addClass('header-anchor');
|
|
531
537
|
});
|
|
532
|
-
|
|
538
|
+
|
|
533
539
|
// Fix table of contents links and add icons
|
|
534
|
-
$('a[href^="#"]').each(function() {
|
|
540
|
+
$('a[href^="#"]').each(function () {
|
|
535
541
|
const href = $(this).attr('href');
|
|
536
542
|
const id = href.substring(1)
|
|
537
543
|
.toLowerCase()
|
|
@@ -542,7 +548,7 @@ router.get(["/mbkauthe/"], async (_, res) => {
|
|
|
542
548
|
});
|
|
543
549
|
|
|
544
550
|
// Add copy buttons to code blocks
|
|
545
|
-
$('pre').each(function() {
|
|
551
|
+
$('pre').each(function () {
|
|
546
552
|
const $pre = $(this);
|
|
547
553
|
const $button = $(`<button class="copy-button" aria-label="Copy code">📋</button>`);
|
|
548
554
|
$pre.prepend($button);
|
|
@@ -550,7 +556,7 @@ router.get(["/mbkauthe/"], async (_, res) => {
|
|
|
550
556
|
|
|
551
557
|
// Create the full HTML response
|
|
552
558
|
const htmlContent = generateFullHtml($.html());
|
|
553
|
-
|
|
559
|
+
|
|
554
560
|
// Update cache
|
|
555
561
|
cachedHtml = htmlContent;
|
|
556
562
|
cacheTimestamp = now;
|
|
@@ -36,6 +36,7 @@ async function validateSession(req, res, next) {
|
|
|
36
36
|
console.log("User not authenticated");
|
|
37
37
|
console.log(req.session.user);
|
|
38
38
|
return res.render("templates/Error/NotLoggedIn.handlebars", {
|
|
39
|
+
layout: mbkautheVar.layout === true ? true : false,
|
|
39
40
|
currentUrl: req.originalUrl,
|
|
40
41
|
});
|
|
41
42
|
}
|
|
@@ -54,6 +55,7 @@ async function validateSession(req, res, next) {
|
|
|
54
55
|
res.clearCookie("sessionId", cookieOptions);
|
|
55
56
|
res.clearCookie("username", cookieOptions);
|
|
56
57
|
return res.render("templates/Error/SessionExpire.handlebars", {
|
|
58
|
+
layout: mbkautheVar.layout === true ? true : false,
|
|
57
59
|
currentUrl: req.originalUrl,
|
|
58
60
|
});
|
|
59
61
|
}
|
|
@@ -66,6 +68,7 @@ async function validateSession(req, res, next) {
|
|
|
66
68
|
res.clearCookie("sessionId", cookieOptions);
|
|
67
69
|
res.clearCookie("username", cookieOptions);
|
|
68
70
|
return res.render("templates/Error/AccountInactive.handlebars", {
|
|
71
|
+
layout: mbkautheVar.layout === true ? true : false,
|
|
69
72
|
currentUrl: req.originalUrl,
|
|
70
73
|
});
|
|
71
74
|
}
|
|
@@ -80,6 +83,7 @@ async function validateSession(req, res, next) {
|
|
|
80
83
|
res.clearCookie("sessionId", cookieOptions);
|
|
81
84
|
res.clearCookie("username", cookieOptions);
|
|
82
85
|
return res.render("templates/Error/Error.handlebars", {
|
|
86
|
+
layout: mbkautheVar.layout === true ? true : false,
|
|
83
87
|
error: `You Are Not Authorized To Use The Application \"${mbkautheVar.APP_NAME}\"`,
|
|
84
88
|
});
|
|
85
89
|
}
|
|
@@ -99,6 +103,7 @@ const checkRolePermission = (requiredRole) => {
|
|
|
99
103
|
console.log("User not authenticated");
|
|
100
104
|
console.log(req.session);
|
|
101
105
|
return res.render("templates/Error/NotLoggedIn.handlebars", {
|
|
106
|
+
layout: mbkautheVar.layout === true ? true : false,
|
|
102
107
|
currentUrl: req.originalUrl,
|
|
103
108
|
});
|
|
104
109
|
}
|
|
@@ -119,6 +124,7 @@ const checkRolePermission = (requiredRole) => {
|
|
|
119
124
|
const userRole = result.rows[0].Role;
|
|
120
125
|
if (userRole !== requiredRole) {
|
|
121
126
|
return res.render("templates/Error/AccessDenied.handlebars", {
|
|
127
|
+
layout: mbkautheVar.layout === true ? true : false,
|
|
122
128
|
currentRole: userRole,
|
|
123
129
|
requiredRole: requiredRole,
|
|
124
130
|
});
|