mbkauthe 1.0.25 → 1.1.0

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/README.md CHANGED
@@ -32,12 +32,18 @@
32
32
 
33
33
  ## Features
34
34
 
35
- - **Session Management**: Secure session handling using `express-session` and `connect-pg-simple`.
36
- - **Role-Based Access Control**: Validate user roles and permissions with ease.
37
- - **Two-Factor Authentication (2FA)**: Optional 2FA support for enhanced security.
38
- - **reCAPTCHA Integration**: Protect login endpoints with Google reCAPTCHA.
39
- - **Cookie Management**: Configurable cookie expiration and domain settings.
40
- - **PostgreSQL Integration**: Uses a connection pool for efficient database interactions.
35
+ - **Session Management:** Simplifies session handling with secure session restoration and expiration mechanisms.
36
+ - **User Authentication:** Provides robust authentication, including support for username/password and Two-Factor Authentication (2FA).
37
+ - **Role-Based Access Control (RBAC):** Enables fine-grained access control by validating user roles and permissions.
38
+ - **Integration with PostgreSQL:** Seamlessly integrates with PostgreSQL for user and session data storage.
39
+ - **reCAPTCHA Verification:** Adds an extra layer of security with reCAPTCHA support to prevent automated attacks.
40
+ - **Middleware Functions:** Includes reusable middleware for session validation, role checking, and user authentication.
41
+ - **API Endpoints:** Offers a set of RESTful APIs for login, logout, session termination, and package information retrieval.
42
+ - **Environment Configuration:** Supports flexible configuration through .env files for deployment-specific settings.
43
+ - **Demo Account:** Provides a demo account for hands-on exploration of the authentication system.
44
+ - **Database Schema:** Predefined database structure for user, session, and 2FA data management.
45
+ - **Extensibility:** Designed to be easily integrated into existing Node.js applications.
46
+ - **Secure Cookies:** Ensures secure session handling with cookie expiration and domain-specific settings
41
47
 
42
48
  ## Installation
43
49
 
@@ -87,7 +93,7 @@ mbkautheVar='{
87
93
  "IS_DEPLOYED": "true",
88
94
  "LOGIN_DB": "postgres://username:password@host:port/database",
89
95
  "MBKAUTH_TWO_FA_ENABLE": "false",
90
- "COOKIE_EXPIRE_TIME": "1",
96
+ "COOKIE_EXPIRE_TIME": 2,
91
97
  "DOMAIN": "yourdomain.com"
92
98
  }'
93
99
  ```
package/docs/db.md CHANGED
@@ -34,7 +34,6 @@
34
34
  "Active" BOOLEAN NOT NULL DEFAULT true,
35
35
  "HaveMailAccount" BOOLEAN NOT NULL DEFAULT false,
36
36
  "SessionId" TEXT,
37
- "GuestRole" JSONB DEFAULT '{"allowPages": [""], "NotallowPages": [""]}'::jsonb
38
37
  "AllowedApps" JSONB DEFAULT '["mbkauthe"]'::jsonb
39
38
  );
40
39
  ```
package/lib/info.js ADDED
@@ -0,0 +1,185 @@
1
+ import express from "express";
2
+ import fetch from 'node-fetch';
3
+
4
+ import { createRequire } from "module";
5
+ const require = createRequire(import.meta.url);
6
+ const packageJson = require("../package.json");
7
+ import fs from "fs";
8
+ import path from "path";
9
+
10
+ import dotenv from "dotenv";
11
+ dotenv.config();
12
+ const mbkautheVar = JSON.parse(process.env.mbkautheVar);
13
+
14
+ const router = express.Router();
15
+
16
+ // Return package.json data of mbkauthe
17
+ router.get("/mbkauthe/package", async (_, res) => {
18
+ try {
19
+ const response = await fetch("https://mbkauthe.mbktechstudio.com/mbkauthe/package");
20
+ const latestPackageData = await response.json();
21
+ res.status(200).send(`
22
+ <html>
23
+ <head>
24
+ <title>Package Information</title>
25
+ </head>
26
+ <body>
27
+ <h1>Package Information</h1>
28
+ <p><strong>Current Version:</strong> ${JSON.stringify(packageJson, null, 2)}</p>
29
+ <p><strong>Latest Version:</strong> ${JSON.stringify(latestPackageData, null, 2)}</p>
30
+ </body>
31
+ </html>
32
+ `);
33
+ } catch (err) {
34
+ res.status(200).send(`
35
+ <html>
36
+ <head>
37
+ <title>Package Information</title>
38
+ </head>
39
+ <body>
40
+ <h1>Package Information</h1>
41
+ <p><strong>Current Version:</strong> ${JSON.stringify(packageJson, null, 2)}</p>
42
+ <p><strong>Latest Version:</strong> Failed to fetch latest package data, Erro:${err.message}</p>
43
+ </body>
44
+ </html>
45
+ `);
46
+ }
47
+ });
48
+
49
+ // Return version number of mbkauthe
50
+ router.get(["/mbkauthe/version", "/mbkauthe/v"], async (_, res) => {
51
+ try {
52
+ const response = await fetch("https://raw.githubusercontent.com/MIbnEKhalid/mbkauthe/refs/heads/main/package.json");
53
+ const latestPackageData = await response.json();
54
+ res.status(200).send(`
55
+ <html>
56
+ <head>
57
+ <title>Version Information</title>
58
+ </head>
59
+ <body>
60
+ <h1>Package Information</h1>
61
+ <p><strong>Current Version:</strong> ${JSON.stringify(packageJson.version, null, 2)}</p>
62
+ <p><strong>Latest Version:</strong> ${JSON.stringify(latestPackageData.version, null, 2)}</p>
63
+ </body>
64
+ </html>
65
+ `);
66
+ } catch (err) {
67
+ res.status(200).send(`
68
+ <html>
69
+ <head>
70
+ <title>Package Information</title>
71
+ </head>
72
+ <body>
73
+ <h1>Package Information</h1>
74
+ <p><strong>Current Version:</strong> ${JSON.stringify(packageJson.version, null, 2)}</p>
75
+ <p><strong>Latest Version:</strong> Failed to fetch latest package data, Erro:${err.message}</p>
76
+ </body>
77
+ </html>
78
+ `);
79
+ }
80
+ });
81
+
82
+ // Return package-lock.json data of mbkauthe from project the package is installed in
83
+ router.get("/mbkauthe/package-lock", (_, res) => {
84
+ console.log("Request for package-lock.json received");
85
+ const packageLockPath = path.resolve(process.cwd(), "package-lock.json");
86
+ fs.readFile(packageLockPath, "utf8", (err, data) => {
87
+ if (err) {
88
+ console.error("Error reading package-lock.json:", err);
89
+ return res.status(500).json({ success: false, message: "Failed to read package-lock.json" });
90
+ }
91
+ try {
92
+ const packageLock = JSON.parse(data);
93
+ const mbkautheData = {
94
+ name: 'mbkauthe',
95
+ version: packageLock.packages['node_modules/mbkauthe'].version,
96
+ resolved: packageLock.packages['node_modules/mbkauthe'].resolved,
97
+ integrity: packageLock.packages['node_modules/mbkauthe'].integrity,
98
+ license: packageLock.packages['node_modules/mbkauthe'].license,
99
+ dependencies: packageLock.packages['node_modules/mbkauthe'].dependencies
100
+ };
101
+ const rootDependency = packageLock.packages[''].dependencies.mbkauthe;
102
+ console.log('mbkauthe package data:', mbkautheData);
103
+ console.log('Root dependency version:', rootDependency);
104
+ res.status(200).json({ mbkautheData, rootDependency });
105
+ } catch (parseError) {
106
+ console.error("Error parsing package-lock.json:", parseError);
107
+ res.status(500).json({ success: false, message: "Failed to parse package-lock.json" });
108
+ }
109
+ });
110
+ });
111
+
112
+ // Return version number of mbkauthe
113
+ router.get(["/mbkauthe", "/mbkauthe/info", "/mbkauthe/i"], async (_, res) => {
114
+ try {
115
+ res.status(200).send(`
116
+ <html>
117
+ <head>
118
+ <title>Version and Configuration Information</title>
119
+ <style>
120
+ body {
121
+ font-family: Arial, sans-serif;
122
+ line-height: 1.6;
123
+ margin: 20px;
124
+ }
125
+ h1 {
126
+ color: #333;
127
+ }
128
+ p {
129
+ margin: 5px 0;
130
+ }
131
+ a {
132
+ display: block;
133
+ margin: 10px 0;
134
+ color: #007BFF;
135
+ text-decoration: none;
136
+ }
137
+ a:hover {
138
+ text-decoration: underline;
139
+ }
140
+ .info-section {
141
+ margin-bottom: 20px;
142
+ }
143
+ </style>
144
+ </head>
145
+ <body>
146
+ <h1>Version and Configuration Information</h1>
147
+ <div class="info-section">
148
+ <h2>Current Version</h2>
149
+ <p><strong>Version:</strong> ${JSON.stringify(packageJson.version, null, 2)}</p>
150
+ </div>
151
+ <div class="info-section">
152
+ <h2>Configuration Information</h2>
153
+ <p><strong>APP_NAME:</strong> ${mbkautheVar.APP_NAME}</p>
154
+ <p><strong>RECAPTCHA_Enabled:</strong> ${mbkautheVar.RECAPTCHA_Enabled}</p>
155
+ <p><strong>MBKAUTH_TWO_FA_ENABLE:</strong> ${mbkautheVar.MBKAUTH_TWO_FA_ENABLE}</p>
156
+ <p><strong>COOKIE_EXPIRE_TIME:</strong> ${mbkautheVar.COOKIE_EXPIRE_TIME} Days</p>
157
+ <p><strong>IS_DEPLOYED:</strong> ${mbkautheVar.IS_DEPLOYED}</p>
158
+ <p><strong>DOMAIN:</strong> ${mbkautheVar.DOMAIN}</p>
159
+ </div>
160
+ <div class="info-section">
161
+ <h2>Useful Links</h2>
162
+ <a href="/mbkauthe/package">View mbkauthe package.json</a>
163
+ <a href="/mbkauthe/package-lock">View mbkauthe version info from installed project package-lock.json</a>
164
+ <a href="/mbkauthe/version">View Current and Latest Package Version</a>
165
+ </div>
166
+ </body>
167
+ </html>
168
+ `);
169
+ } catch (err) {
170
+ console.error("Error fetching version information:", err);
171
+ res.status(500).send(`
172
+ <html>
173
+ <head>
174
+ <title>Error</title>
175
+ </head>
176
+ <body>
177
+ <h1>Error</h1>
178
+ <p>Failed to fetch version information. Please try again later.</p>
179
+ </body>
180
+ </html>
181
+ `);
182
+ }
183
+ });
184
+
185
+ export default router;
package/lib/main.js CHANGED
@@ -1,4 +1,4 @@
1
- import express, { json } from "express";
1
+ import express from "express";
2
2
  import crypto from "crypto";
3
3
  import session from "express-session";
4
4
  import pgSession from "connect-pg-simple";
@@ -9,12 +9,7 @@ import fetch from 'node-fetch';
9
9
  import cookieParser from "cookie-parser";
10
10
  import bcrypt from 'bcrypt';
11
11
  import rateLimit from 'express-rate-limit';
12
-
13
- import { createRequire } from "module";
14
- const require = createRequire(import.meta.url);
15
- const packageJson = require("../package.json");
16
- import fs from "fs";
17
- import path from "path";
12
+ import mbkautheinfo from "./info.js";
18
13
 
19
14
  import dotenv from "dotenv";
20
15
  dotenv.config();
@@ -33,7 +28,7 @@ router.use((req, res, next) => {
33
28
  }
34
29
  next();
35
30
  });
36
-
31
+ router.use(mbkautheinfo);
37
32
  router.use(express.json());
38
33
  router.use(express.urlencoded({ extended: true }));
39
34
  router.use(cookieParser());
@@ -48,36 +43,6 @@ const LoginLimit = rateLimit({
48
43
  }
49
44
  });
50
45
 
51
- router.use((req, res, next) => {
52
- // Don't allow embedding in iframes
53
- res.setHeader('X-Frame-Options', 'DENY');
54
-
55
- // Prevent MIME type sniffing
56
- res.setHeader('X-Content-Type-Options', 'nosniff');
57
-
58
- // Enable XSS protection
59
- res.setHeader('X-XSS-Protection', '1; mode=block');
60
-
61
- // Referrer policy
62
- res.setHeader('Referrer-Policy', 'strict-origin-when-cross-origin');
63
-
64
- // Content Security Policy
65
- const csp = [
66
- "default-src 'self'",
67
- "script-src 'self' 'unsafe-inline' https://www.google.com https://www.gstatic.com",
68
- "style-src 'self' 'unsafe-inline'",
69
- "img-src 'self' data:",
70
- "connect-src 'self' https://www.google.com",
71
- "frame-src https://www.google.com",
72
- "form-action 'self'"
73
- ].join('; ');
74
-
75
- res.setHeader('Content-Security-Policy', csp);
76
-
77
- next();
78
- });
79
-
80
-
81
46
  // Configure session with proper domain settings for cross-subdomain sharing
82
47
  const sessionConfig = {
83
48
  store: new PgSession({
@@ -353,100 +318,4 @@ router.post("/mbkauthe/api/logout", async (req, res) => {
353
318
  }
354
319
  });
355
320
 
356
- // Return package.json data of mbkauthe
357
- router.get("/mbkauthe/package", async (_, res) => {
358
- try {
359
- const response = await fetch("https://mbkauthe.mbktechstudio.com/mbkauthe/package");
360
- const latestPackageData = await response.json();
361
- res.status(200).send(`
362
- <html>
363
- <head>
364
- <title>Package Information</title>
365
- </head>
366
- <body>
367
- <h1>Package Information</h1>
368
- <p><strong>Current Version:</strong> ${JSON.stringify(packageJson, null, 2)}</p>
369
- <p><strong>Latest Version:</strong> ${JSON.stringify(latestPackageData, null, 2)}</p>
370
- </body>
371
- </html>
372
- `);
373
- } catch (err) {
374
- res.status(200).send(`
375
- <html>
376
- <head>
377
- <title>Package Information</title>
378
- </head>
379
- <body>
380
- <h1>Package Information</h1>
381
- <p><strong>Current Version:</strong> ${JSON.stringify(packageJson, null, 2)}</p>
382
- <p><strong>Latest Version:</strong> Failed to fetch latest package data, Erro:${err.message}</p>
383
- </body>
384
- </html>
385
- `);
386
- }
387
- });
388
-
389
- // Return version number of mbkauthe
390
- router.get(["/mbkauthe/version", "/mbkauthe/v"], async(_, res) => {
391
- try {
392
- const response = await fetch("https://mbkauthe.mbktechstudio.com/mbkauthe/version");
393
- const latestPackageData = await response.json();
394
- res.status(200).send(`
395
- <html>
396
- <head>
397
- <title>Version Information</title>
398
- </head>
399
- <body>
400
- <h1>Package Information</h1>
401
- <p><strong>Current Version:</strong> ${JSON.stringify(packageJson.version, null, 2)}</p>
402
- <p><strong>Latest Version:</strong> ${JSON.stringify(latestPackageData, null, 2)}</p>
403
- </body>
404
- </html>
405
- `);
406
- } catch (err) {
407
- res.status(200).send(`
408
- <html>
409
- <head>
410
- <title>Package Information</title>
411
- </head>
412
- <body>
413
- <h1>Package Information</h1>
414
- <p><strong>Current Version:</strong> ${JSON.stringify(packageJson.version, null, 2)}</p>
415
- <p><strong>Latest Version:</strong> Failed to fetch latest package data, Erro:${err.message}</p>
416
- </body>
417
- </html>
418
- `);
419
- }
420
- });
421
-
422
- // Return package-lock.json data of mbkauthe from project the package is installed in
423
- router.get("/mbkauthe/package-lock", (_, res) => {
424
- console.log("Request for package-lock.json received");
425
- const packageLockPath = path.resolve(process.cwd(), "package-lock.json");
426
- fs.readFile(packageLockPath, "utf8", (err, data) => {
427
- if (err) {
428
- console.error("Error reading package-lock.json:", err);
429
- return res.status(500).json({ success: false, message: "Failed to read package-lock.json" });
430
- }
431
- try {
432
- const packageLock = JSON.parse(data);
433
- const mbkautheData = {
434
- name: 'mbkauthe',
435
- version: packageLock.packages['node_modules/mbkauthe'].version,
436
- resolved: packageLock.packages['node_modules/mbkauthe'].resolved,
437
- integrity: packageLock.packages['node_modules/mbkauthe'].integrity,
438
- license: packageLock.packages['node_modules/mbkauthe'].license,
439
- dependencies: packageLock.packages['node_modules/mbkauthe'].dependencies
440
- };
441
- const rootDependency = packageLock.packages[''].dependencies.mbkauthe;
442
- console.log('mbkauthe package data:', mbkautheData);
443
- console.log('Root dependency version:', rootDependency);
444
- res.status(200).json({ mbkautheData, rootDependency });
445
- } catch (parseError) {
446
- console.error("Error parsing package-lock.json:", parseError);
447
- res.status(500).json({ success: false, message: "Failed to parse package-lock.json" });
448
- }
449
- });
450
- });
451
-
452
321
  export default router;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mbkauthe",
3
- "version": "1.0.25",
3
+ "version": "1.1.0",
4
4
  "description": "MBKTechStudio's reusable authentication system for Node.js applications.",
5
5
  "main": "index.js",
6
6
  "type": "module",