mbkauthe 2.5.0 → 3.0.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/docs/api.md CHANGED
@@ -11,6 +11,10 @@ This document provides comprehensive API documentation for MBKAuthe authenticati
11
11
  - [Authentication](#authentication)
12
12
  - [Session Management](#session-management)
13
13
  - [API Endpoints](#api-endpoints)
14
+ - [Public Endpoints](#public-endpoints)
15
+ - [Protected Endpoints](#protected-endpoints)
16
+ - [OAuth Endpoints](#oauth-endpoints)
17
+ - [Information Endpoints](#information-endpoints)
14
18
  - [Middleware Reference](#middleware-reference)
15
19
  - [Code Examples](#code-examples)
16
20
 
@@ -326,6 +330,35 @@ Displays MBKAuthe version information and configuration.
326
330
 
327
331
  ---
328
332
 
333
+ #### `GET /mbkauthe/ErrorCode`
334
+
335
+ Displays comprehensive error code documentation with descriptions and user messages.
336
+
337
+ **Response:** HTML page showing:
338
+ - All error codes organized by category
339
+ - Error code ranges (Authentication, 2FA, Session, Authorization, etc.)
340
+ - User-friendly messages and hints for each error
341
+ - Dynamically generated from `lib/utils/errors.js`
342
+
343
+ **Categories:**
344
+ - **600-699**: Authentication Errors
345
+ - **700-799**: Two-Factor Authentication Errors
346
+ - **800-899**: Session Management Errors
347
+ - **900-999**: Authorization Errors
348
+ - **1000-1099**: Input Validation Errors
349
+ - **1100-1199**: Rate Limiting Errors
350
+ - **1200-1299**: Server Errors
351
+ - **1300-1399**: OAuth Errors
352
+
353
+ **Note:** This page is automatically synchronized with the error definitions in the codebase. Adding new errors only requires updating `lib/utils/errors.js` and the category code array.
354
+
355
+ **Usage:**
356
+ ```
357
+ GET /mbkauthe/ErrorCode
358
+ ```
359
+
360
+ ---
361
+
329
362
  #### `GET /mbkauthe/main.js`
330
363
 
331
364
  Serves the client-side JavaScript file containing helper functions for authentication operations.
@@ -364,6 +397,35 @@ Serves the client-side JavaScript file containing helper functions for authentic
364
397
  - Forces page reload
365
398
 
366
399
 
400
+ ---
401
+
402
+ #### `GET /mbkauthe/ErrorCode`
403
+
404
+ Displays comprehensive error code documentation with descriptions and user messages.
405
+
406
+ **Response:** HTML page showing:
407
+ - All error codes organized by category
408
+ - Error code ranges (Authentication, 2FA, Session, Authorization, etc.)
409
+ - User-friendly messages and hints for each error
410
+ - Dynamically generated from `lib/utils/errors.js`
411
+
412
+ **Categories:**
413
+ - **600-699**: Authentication Errors
414
+ - **700-799**: Two-Factor Authentication Errors
415
+ - **800-899**: Session Management Errors
416
+ - **900-999**: Authorization Errors
417
+ - **1000-1099**: Input Validation Errors
418
+ - **1100-1199**: Rate Limiting Errors
419
+ - **1200-1299**: Server Errors
420
+ - **1300-1399**: OAuth Errors
421
+
422
+ **Note:** This page is automatically synchronized with the error definitions in the codebase. Adding new errors only requires updating `lib/utils/errors.js` and the category code array.
423
+
424
+ **Usage:**
425
+ ```
426
+ GET /mbkauthe/ErrorCode
427
+ ```
428
+
367
429
  ---
368
430
 
369
431
  #### `GET /icon.svg`
@@ -448,6 +510,79 @@ GET /mbkauthe/test
448
510
 
449
511
  ---
450
512
 
513
+ ### OAuth Endpoints
514
+
515
+ #### `GET /mbkauthe/api/github/login`
516
+
517
+ Initiates the GitHub OAuth authentication flow.
518
+
519
+ **Rate Limit:** 10 requests per 5 minutes
520
+
521
+ **Query Parameters:**
522
+ - `redirect` (optional) - Relative URL to redirect after successful authentication (must start with `/` to prevent open redirect attacks)
523
+
524
+ **Response:** Redirects to GitHub OAuth authorization page
525
+
526
+ **Prerequisites:**
527
+ - `GITHUB_LOGIN_ENABLED=true` in environment
528
+ - Valid `GITHUB_CLIENT_ID` and `GITHUB_CLIENT_SECRET` configured
529
+ - User's GitHub account must be linked to an MBKAuth account in `user_github` table
530
+
531
+ **Example:**
532
+ ```
533
+ GET /mbkauthe/api/github/login?redirect=/dashboard
534
+ ```
535
+
536
+ **Workflow:**
537
+ 1. User clicks "Login with GitHub"
538
+ 2. Redirects to GitHub for authorization
539
+ 3. GitHub redirects back to callback URL
540
+ 4. System verifies GitHub account is linked
541
+ 5. If 2FA enabled, prompts for 2FA
542
+ 6. Creates session and redirects to specified URL
543
+
544
+ ---
545
+
546
+ #### `GET /mbkauthe/api/github/login/callback`
547
+
548
+ Handles the OAuth callback from GitHub after user authorization.
549
+
550
+ **Rate Limit:** Inherited from OAuth rate limiter (10 requests per 5 minutes)
551
+
552
+ **Query Parameters:**
553
+ - `code` - Authorization code from GitHub (automatically provided)
554
+ - `state` - State parameter for CSRF protection (automatically provided)
555
+
556
+ **Response:**
557
+ - Redirects to 2FA page if 2FA is enabled for the user
558
+ - Redirects to `loginRedirectURL` or stored redirect URL if 2FA is not required
559
+ - Renders error page if authentication fails
560
+
561
+ **Error Handling:**
562
+ - **GitHub Not Linked**: Returns error if GitHub account is not in `user_github` table
563
+ - **Account Inactive**: Returns error if user account is deactivated
564
+ - **Not Authorized**: Returns error if user is not allowed to access the application
565
+ - **GitHub Auth Error**: Returns error for any OAuth-related failures
566
+
567
+ **Success Flow:**
568
+ ```
569
+ GitHub → /api/github/login/callback
570
+ → (If 2FA enabled) → /mbkauthe/2fa
571
+ → (If no 2FA) → loginRedirectURL or stored redirect
572
+ ```
573
+
574
+ **Database Query:**
575
+ ```sql
576
+ SELECT ug.*, u."UserName", u."Role", u."Active", u."AllowedApps", u."id"
577
+ FROM user_github ug
578
+ JOIN "Users" u ON ug.user_name = u."UserName"
579
+ WHERE ug.github_id = $1
580
+ ```
581
+
582
+ **Note:** This endpoint is automatically called by GitHub and should not be accessed directly by users.
583
+
584
+ ---
585
+
451
586
  ## Middleware Reference
452
587
 
453
588
  ### `validateSession`
@@ -931,10 +1066,13 @@ app.use((req, res) => {
931
1066
  | `/mbkauthe/api/login` | 8 requests | 1 minute |
932
1067
  | `/mbkauthe/api/logout` | 10 requests | 1 minute |
933
1068
  | `/mbkauthe/api/verify-2fa` | 5 requests | 1 minute |
1069
+ | `/mbkauthe/api/github/login` | 10 requests | 5 minutes |
1070
+ | `/mbkauthe/api/github/login/callback` | 10 requests | 5 minutes |
934
1071
  | `/mbkauthe/login` | 8 requests | 1 minute |
935
1072
  | `/mbkauthe/info` | 8 requests | 1 minute |
1073
+ | `/mbkauthe/test` | 8 requests | 1 minute |
936
1074
 
937
- Rate limits are applied per IP address.
1075
+ Rate limits are applied per IP address. Logged-in users are exempt from some rate limits (e.g., login page rate limit).
938
1076
 
939
1077
  ---
940
1078
 
package/docs/db.md CHANGED
@@ -294,7 +294,7 @@ To add new users to the `Users` table, use the following SQL queries:
294
294
  **Generating Encrypted Passwords:**
295
295
  If you're using `EncPass=true`, you can generate encrypted passwords using the hashPassword function:
296
296
  ```javascript
297
- import { hashPassword } from './lib/config.js';
297
+ import { hashPassword } from 'mbkauthe';
298
298
  const encryptedPassword = hashPassword('12345678', 'support');
299
299
  console.log(encryptedPassword); // Use this value for PasswordEnc column
300
300
  ```