mbkauthe 4.6.1 → 4.7.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
@@ -13,6 +13,9 @@
13
13
 
14
14
  **MBKAuthe** is a production-ready authentication system for Node.js with Express and PostgreSQL. Features include secure login, 2FA, role-based access, OAuth (GitHub & Google), multi-session support, and multi-app user management.
15
15
 
16
+ ## Todo
17
+ - Currently, for every request to a protected page, a database query is made to retrieve authentication information (allowed apps, username, session ID, role, etc.). We should implement a caching mechanism to reduce this overhead, but also find a way to allow administrators to log users out and update permissions in near real-time.
18
+
16
19
  ## ✨ Key Features
17
20
 
18
21
  - Secure password authentication (PBKDF2)
@@ -27,6 +30,7 @@
27
30
  - Session fixation prevention
28
31
  - Dynamic profile picture routing with session caching
29
32
  - Modern responsive UI with desktop two-column layout
33
+ - Dev-only DB Query Monitor with callsite, timing, and request context
30
34
 
31
35
  ## 📦 Installation
32
36
 
@@ -43,7 +47,7 @@ Copy-Item .env.example .env
43
47
  ```
44
48
  See [docs/env.md](docs/env.md).
45
49
 
46
- **2. Set Up Database**
50
+ **2. Set Up Database**
47
51
  Run [docs/db.sql](docs/db.sql) to create tables and a default SuperAdmin (`support` / `12345678`). Change the password immediately. See [docs/db.md](docs/db.md).
48
52
 
49
53
  **3. Integrate with Express**
@@ -78,6 +82,14 @@ npm run dev
78
82
  - **Combined:** `validateSessionAndRole(['SuperAdmin', 'NormalUser'])`
79
83
  - **API Token Auth:** `authenticate(process.env.API_TOKEN)`
80
84
 
85
+ ## 🧰 Diagnostics (dev only)
86
+
87
+ These are only mounted when `process.env.env === "dev"`:
88
+
89
+ - **DB Query Monitor (HTML):** `/mbkauthe/db`
90
+ - **DB Query Monitor (JSON):** `/mbkauthe/db.json`
91
+ - **SuperAdmin check:** `/mbkauthe/validate-superadmin`
92
+
81
93
  ## 🔐 Security
82
94
 
83
95
  - Rate limiting, CSRF protection, secure cookies
@@ -99,6 +111,18 @@ Enable via `MBKAUTH_TWO_FA_ENABLE=true`. Trusted devices can skip 2FA for a set
99
111
  - **Custom Views:** `views/loginmbkauthe.handlebars`, `2fa.handlebars`, `Error/dError.handlebars`
100
112
  - **Database Access:** `import { dblogin } from 'mbkauthe'; const result = await dblogin.query('SELECT * FROM "Users"');`
101
113
 
114
+ ## 📄 API Reference
115
+
116
+ - Full endpoint list and details: [docs/api.md](docs/api.md)
117
+
118
+ ## 🧰 Diagnostics (dev only)
119
+
120
+ - **DB Query Monitor (HTML):** `/mbkauthe/db`
121
+ - **DB Query Monitor (JSON):** `/mbkauthe/db.json`
122
+ - **SuperAdmin check:** `/mbkauthe/debug/validate-superadmin`
123
+
124
+ These routes are only mounted when `process.env.env === "dev"`. They expose query timing, status/error, pool stats, request context, and callsite data for troubleshooting.
125
+
102
126
  ## 🚢 Deployment
103
127
 
104
128
  Checklist for production:
package/docs/api.md CHANGED
@@ -15,6 +15,8 @@ This document provides comprehensive API documentation for MBKAuthe authenticati
15
15
  - [Protected Endpoints](#protected-endpoints)
16
16
  - [OAuth Endpoints](#oauth-endpoints)
17
17
  - [Information Endpoints](#information-endpoints)
18
+ - [Diagnostics (Dev Only)](#diagnostics-dev-only)
19
+ - [Additional Endpoints](#additional-endpoints)
18
20
  - [Middleware Reference](#middleware-reference)
19
21
  - [Code Examples](#code-examples)
20
22
 
@@ -195,6 +197,150 @@ GET /mbkauthe/login?redirect=/dashboard
195
197
 
196
198
  ---
197
199
 
200
+ #### `GET /mbkauthe/2fa`
201
+
202
+ Renders the 2FA challenge page after a login that requires TOTP.
203
+
204
+ **Rate Limit:** 5 requests per minute
205
+
206
+ ---
207
+
208
+ #### `GET /mbkauthe/accounts`
209
+
210
+ Renders the account-switch page for remembered sessions on the device.
211
+
212
+ **Rate Limit:** 8 requests per minute
213
+
214
+ ---
215
+
216
+ #### `GET /mbkauthe/test`
217
+
218
+ Renders a test page for the current session context.
219
+
220
+ **Rate Limit:** 8 requests per minute
221
+
222
+ ---
223
+
224
+ #### `POST /mbkauthe/test`
225
+
226
+ Lightweight check to verify an authenticated session.
227
+
228
+ **Response:** `{ "success": true, "message": "You are logged in" }`
229
+
230
+ ---
231
+
232
+ ## Diagnostics (Dev Only)
233
+
234
+ These endpoints are only mounted when `process.env.env === "dev"`.
235
+
236
+ #### `GET /mbkauthe/db`
237
+
238
+ Renders the DB Query Monitor page. The UI fetches data from `/mbkauthe/db.json`.
239
+
240
+ **Query Parameters:**
241
+ - `limit` (optional) - number of most recent queries to show (default: 50)
242
+ - `resetDone` (optional) - UI notification flag used after reset
243
+
244
+ ---
245
+
246
+ #### `GET /mbkauthe/db.json`
247
+
248
+ Returns recent DB query diagnostics.
249
+
250
+ **Query Parameters:**
251
+ - `limit` (optional) - number of most recent queries to return (default: 50)
252
+ - `reset` (optional) - set to `1` to clear the query log and counter
253
+
254
+ **Response Body:**
255
+ ```json
256
+ {
257
+ "queryCount": 120,
258
+ "queryLimit": 50,
259
+ "resetDone": false,
260
+ "queryLog": [
261
+ {
262
+ "time": "2026-03-19T12:00:00.000Z",
263
+ "name": "login-get-user",
264
+ "query": "SELECT ...",
265
+ "values": ["user"],
266
+ "durationMs": 3.42,
267
+ "success": true,
268
+ "error": null,
269
+ "request": {
270
+ "method": "GET",
271
+ "url": "/mbkauthe/login",
272
+ "ip": "::1",
273
+ "userId": 1,
274
+ "username": "support"
275
+ },
276
+ "pool": {
277
+ "total": 2,
278
+ "idle": 1,
279
+ "waiting": 0
280
+ },
281
+ "callsite": {
282
+ "function": "validateSession",
283
+ "file": "lib/middleware/auth.js",
284
+ "line": 197,
285
+ "column": 30
286
+ }
287
+ }
288
+ ]
289
+ }
290
+ ```
291
+
292
+ ---
293
+
294
+ #### `GET /mbkauthe/validate-superadmin`
295
+
296
+ Validates that the current session has `SuperAdmin` role and returns a JSON summary.
297
+
298
+ ---
299
+
300
+ ## Additional Endpoints
301
+
302
+ The endpoints below are active in the router but are not fully expanded above. Use this list as a reference.
303
+
304
+ **Auth & Session:**
305
+
306
+ - `POST /mbkauthe/api/verify-2fa` - Verifies TOTP and completes login.
307
+ - `POST /mbkauthe/api/logout` - Logs out the current session.
308
+ - `GET /mbkauthe/api/account-sessions` - Lists remembered accounts for the current device.
309
+ - `POST /mbkauthe/api/switch-session` - Switches active session to another remembered account.
310
+ - `POST /mbkauthe/api/logout-all` - Logs out all remembered accounts on the device.
311
+
312
+ **Session Validation:**
313
+
314
+ - `GET /mbkauthe/api/checkSession` - Checks session validity (cookie-based).
315
+ - `POST /mbkauthe/api/checkSession` - Checks session validity by sessionId (body).
316
+ - `POST /mbkauthe/api/verifySession` - Returns session details by sessionId (body).
317
+
318
+ **OAuth:**
319
+
320
+ - `GET /mbkauthe/api/github/login` - Starts GitHub OAuth login flow.
321
+ - `GET /mbkauthe/api/github/login/callback` - GitHub OAuth callback.
322
+ - `GET /mbkauthe/api/google/login` - Starts Google OAuth login flow.
323
+ - `GET /mbkauthe/api/google/login/callback` - Google OAuth callback.
324
+
325
+ **Info & UI:**
326
+
327
+ - `GET /mbkauthe/info` and `GET /mbkauthe/i` - Info page.
328
+ - `GET /mbkauthe/info.json` and `GET /mbkauthe/i.json` - Info page JSON.
329
+ - `GET /mbkauthe/ErrorCode` - Error codes page.
330
+ - `GET /mbkauthe/user/profilepic` - User profile picture proxy.
331
+
332
+ **Admin:**
333
+
334
+ - `POST /mbkauthe/api/terminateAllSessions` - Terminates all sessions (requires `Main_SECRET_TOKEN`).
335
+
336
+ **Static Assets:**
337
+
338
+ - `GET /mbkauthe/main.js`
339
+ - `GET /mbkauthe/main.css`
340
+ - `GET /mbkauthe/bg.webp`
341
+
342
+ ---
343
+
198
344
  #### `POST /mbkauthe/api/login`
199
345
 
200
346
  Authenticates a user and creates a session.