mbkauthe 1.4.2 → 2.0.1
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 +1 -10
- package/README.md +3 -3
- package/docs/api.md +69 -3
- package/docs/db.md +0 -6
- package/env.md +69 -1
- package/lib/main.js +378 -182
- package/lib/pool.js +2 -2
- package/lib/validateSessionAndRole.js +22 -47
- package/package.json +6 -3
- package/public/bg.avif +0 -0
- package/public/main.js +5 -7
- package/views/2fa.handlebars +6 -6
- package/views/Error/dError.handlebars +3 -3
- package/views/info.handlebars +13 -10
- package/views/loginmbkauthe.handlebars +55 -13
- package/views/sharedStyles.handlebars +1 -1
- package/views/showmessage.handlebars +1 -1
package/.env.example
CHANGED
|
@@ -1,12 +1,3 @@
|
|
|
1
|
-
mbkautheVar=
|
|
2
|
-
"APP_NAME": "MBKAUTH",
|
|
3
|
-
"SESSION_SECRET_KEY": "your-session-secret-key",
|
|
4
|
-
"IS_DEPLOYED": "true",
|
|
5
|
-
"LOGIN_DB": "postgres://username:password@host:port/database",
|
|
6
|
-
"MBKAUTH_TWO_FA_ENABLE": "false",
|
|
7
|
-
"COOKIE_EXPIRE_TIME": 2,
|
|
8
|
-
"DOMAIN": "yourdomain.com",
|
|
9
|
-
"loginRedirectURL": "/admin"
|
|
10
|
-
}'
|
|
1
|
+
mbkautheVar={"APP_NAME":"mbkauthe","Main_SECRET_TOKEN": 123,"SESSION_SECRET_KEY":"123","IS_DEPLOYED":"true","LOGIN_DB":"postgres://","MBKAUTH_TWO_FA_ENABLE":"true","COOKIE_EXPIRE_TIME":2,"DOMAIN":"mbktech.org","loginRedirectURL":"/mbkauthe/test","GITHUB_LOGIN_ENABLED":"true","GITHUB_CLIENT_ID":"","GITHUB_CLIENT_SECRET":""}
|
|
11
2
|
|
|
12
3
|
# See env.md for more details
|
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
[](https://github.com/MIbnEKhalid/mbkauthe/actions/workflows/publish.yml)
|
|
7
7
|
[](https://github.com/MIbnEKhalid/mbkauthe/actions/workflows/codeql.yml)
|
|
8
8
|
|
|
9
|
-
**MBKAuth** is a reusable, production-ready authentication system for Node.js applications built by
|
|
9
|
+
**MBKAuth** is a reusable, production-ready authentication system for Node.js applications built by MBKTech.org. It provides secure session management, two-factor authentication (2FA), role-based access control, and multi-application support out of the box.
|
|
10
10
|
|
|
11
11
|
## ✨ Features
|
|
12
12
|
|
|
@@ -313,8 +313,8 @@ Found a bug or need help? Please [open an issue](https://github.com/MIbnEKhalid/
|
|
|
313
313
|
|
|
314
314
|
- [npm Package](https://www.npmjs.com/package/mbkauthe)
|
|
315
315
|
- [GitHub Repository](https://github.com/MIbnEKhalid/mbkauthe)
|
|
316
|
-
- [
|
|
316
|
+
- [MBKTech.org](https://mbktech.org)
|
|
317
317
|
|
|
318
318
|
---
|
|
319
319
|
|
|
320
|
-
Made with ❤️ by [
|
|
320
|
+
Made with ❤️ by [MBKTech.org](https://mbktech.org)
|
package/docs/api.md
CHANGED
|
@@ -328,10 +328,77 @@ Displays MBKAuthe version information and configuration.
|
|
|
328
328
|
|
|
329
329
|
#### `GET /mbkauthe/main.js`
|
|
330
330
|
|
|
331
|
-
Serves the client-side JavaScript file.
|
|
331
|
+
Serves the client-side JavaScript file containing helper functions for authentication operations.
|
|
332
|
+
|
|
333
|
+
**Purpose:** Provides frontend JavaScript utilities including:
|
|
334
|
+
- `logout()` - Logout function with confirmation dialog and cache clearing
|
|
335
|
+
- `logoutuser()` - Alias for logout function
|
|
336
|
+
- `nuclearCacheClear()` - Comprehensive cache and storage clearing (preserves rememberedUsername)
|
|
337
|
+
- `getCookieValue(cookieName)` - Cookie retrieval helper
|
|
338
|
+
- `loadpage(url)` - Page navigation helper
|
|
339
|
+
- `formatDate(date)` - Date formatting utility
|
|
340
|
+
- `reloadPage()` - Page reload helper
|
|
341
|
+
- `checkSession()` - Session validity checker
|
|
332
342
|
|
|
333
343
|
**Response:** JavaScript file (Content-Type: application/javascript)
|
|
334
344
|
|
|
345
|
+
**Usage:**
|
|
346
|
+
```html
|
|
347
|
+
<script src="/mbkauthe/main.js"></script>
|
|
348
|
+
<button onclick="logout()">Logout</button>
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
**Main Functions:**
|
|
352
|
+
|
|
353
|
+
**`logout()`**
|
|
354
|
+
- Shows confirmation dialog before logout
|
|
355
|
+
- Clears all caches except rememberedUsername
|
|
356
|
+
- Calls `/mbkauthe/api/logout` endpoint
|
|
357
|
+
- Redirects to home page on success
|
|
358
|
+
|
|
359
|
+
**`nuclearCacheClear()`**
|
|
360
|
+
- Clears service workers and cache storage
|
|
361
|
+
- Clears localStorage and sessionStorage (preserves rememberedUsername)
|
|
362
|
+
- Clears IndexedDB
|
|
363
|
+
- Clears cookies
|
|
364
|
+
- Forces page reload
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
---
|
|
368
|
+
|
|
369
|
+
#### `GET /mbkauthe/test`
|
|
370
|
+
|
|
371
|
+
Test endpoint to verify authentication and display user session information.
|
|
372
|
+
|
|
373
|
+
**Authentication:** Session required
|
|
374
|
+
|
|
375
|
+
**Rate Limit:** 8 requests per minute
|
|
376
|
+
|
|
377
|
+
**Response:** HTML page displaying:
|
|
378
|
+
- Current username
|
|
379
|
+
- User role
|
|
380
|
+
- Logout button
|
|
381
|
+
- Quick links to info and login pages
|
|
382
|
+
|
|
383
|
+
**Example Response:**
|
|
384
|
+
```html
|
|
385
|
+
<head>
|
|
386
|
+
<script src="/mbkauthe/main.js"></script>
|
|
387
|
+
</head>
|
|
388
|
+
<p>if you are seeing this page than User is logged in.</p>
|
|
389
|
+
<p>id: '${req.session.user.id}', UserName: '${req.session.user.username}', Role: '${req.session.user.role}', SessionId: '${req.session.user.sessionId}'</p>
|
|
390
|
+
<button onclick="logout()">Logout</button><br>
|
|
391
|
+
<a href="/mbkauthe/info">Info Page</a><br>
|
|
392
|
+
<a href="/mbkauthe/login">Login Page</a><br>
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
**Usage:**
|
|
396
|
+
```
|
|
397
|
+
GET /mbkauthe/test
|
|
398
|
+
```
|
|
399
|
+
|
|
400
|
+
**Note:** This endpoint is primarily for testing and debugging authentication. It should not be used in production environments.
|
|
401
|
+
|
|
335
402
|
---
|
|
336
403
|
|
|
337
404
|
## Middleware Reference
|
|
@@ -347,7 +414,7 @@ import { validateSession } from 'mbkauthe';
|
|
|
347
414
|
app.get('/protected', validateSession, (req, res) => {
|
|
348
415
|
// User is authenticated
|
|
349
416
|
const user = req.session.user;
|
|
350
|
-
// user contains: { id, username, UserName, role, Role, sessionId
|
|
417
|
+
// user contains: { id, username, UserName, role, Role, sessionId }
|
|
351
418
|
res.send(`Welcome ${user.username}!`);
|
|
352
419
|
});
|
|
353
420
|
```
|
|
@@ -369,7 +436,6 @@ req.session.user = {
|
|
|
369
436
|
role: "NormalUser", // User role
|
|
370
437
|
Role: "NormalUser", // User role (alias)
|
|
371
438
|
sessionId: "abc123...", // 64-char hex session ID
|
|
372
|
-
allowedApps: ["app1"] // Array of allowed applications
|
|
373
439
|
}
|
|
374
440
|
```
|
|
375
441
|
|
package/docs/db.md
CHANGED
|
@@ -12,8 +12,6 @@ Add these to your `.env` file:
|
|
|
12
12
|
# GitHub OAuth App Configuration
|
|
13
13
|
GITHUB_CLIENT_ID=your_github_client_id
|
|
14
14
|
GITHUB_CLIENT_SECRET=your_github_client_secret
|
|
15
|
-
GITHUB_LOGIN_CALLBACK_URL=https://yourdomain.com/mbkauthe/api/github/login/callback
|
|
16
|
-
BASE_URL=https://yourdomain.com
|
|
17
15
|
```
|
|
18
16
|
|
|
19
17
|
### 2. GitHub OAuth App Setup
|
|
@@ -109,10 +107,6 @@ The login page now includes:
|
|
|
109
107
|
# Required for GitHub Login
|
|
110
108
|
GITHUB_CLIENT_ID=your_github_client_id
|
|
111
109
|
GITHUB_CLIENT_SECRET=your_github_client_secret
|
|
112
|
-
GITHUB_LOGIN_CALLBACK_URL=https://yourdomain.com/mbkauthe/api/github/login/callback
|
|
113
|
-
|
|
114
|
-
# Optional (used as fallback)
|
|
115
|
-
BASE_URL=https://yourdomain.com
|
|
116
110
|
```
|
|
117
111
|
|
|
118
112
|
The GitHub login feature is now fully integrated into your mbkauthe system and ready to use!
|
package/env.md
CHANGED
|
@@ -54,7 +54,7 @@ DOMAIN=localhost
|
|
|
54
54
|
**Description:** Your application's domain name.
|
|
55
55
|
|
|
56
56
|
**Configuration:**
|
|
57
|
-
- **Production:** Set to your actual domain (e.g., `
|
|
57
|
+
- **Production:** Set to your actual domain (e.g., `mbktech.com`)
|
|
58
58
|
- **Development:** Use `localhost` or set `IS_DEPLOYED=false`
|
|
59
59
|
- **Subdomains:** When `IS_DEPLOYED=true`, sessions are shared across all subdomains
|
|
60
60
|
|
|
@@ -138,6 +138,74 @@ COOKIE_EXPIRE_TIME=30 # 1 month (convenience)
|
|
|
138
138
|
|
|
139
139
|
---
|
|
140
140
|
|
|
141
|
+
## 🐙 GitHub OAuth Authentication
|
|
142
|
+
|
|
143
|
+
### GitHub Login Configuration
|
|
144
|
+
```env
|
|
145
|
+
GITHUB_LOGIN_ENABLED=false
|
|
146
|
+
GITHUB_CLIENT_ID=your-github-client-id
|
|
147
|
+
GITHUB_CLIENT_SECRET=your-github-client-secret
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
#### GITHUB_LOGIN_ENABLED
|
|
151
|
+
**Description:** Enables or disables GitHub OAuth login functionality.
|
|
152
|
+
|
|
153
|
+
**Values:**
|
|
154
|
+
- `true` - Enable GitHub login (users can authenticate via GitHub)
|
|
155
|
+
- `false` - Disable GitHub login (default)
|
|
156
|
+
|
|
157
|
+
**Required:** Yes (if using GitHub authentication)
|
|
158
|
+
|
|
159
|
+
#### GITHUB_CLIENT_ID
|
|
160
|
+
**Description:** OAuth application client ID from GitHub.
|
|
161
|
+
|
|
162
|
+
- **Purpose:** Identifies your application to GitHub's OAuth service
|
|
163
|
+
- **Format:** Alphanumeric string provided by GitHub
|
|
164
|
+
- **Setup:** Obtain from [GitHub Developer Settings](https://github.com/settings/developers)
|
|
165
|
+
- **Required:** Yes (when `GITHUB_LOGIN_ENABLED=true`)
|
|
166
|
+
|
|
167
|
+
**Example:** `GITHUB_CLIENT_ID=Iv1.a1b2c3d4e5f6g7h8`
|
|
168
|
+
|
|
169
|
+
#### GITHUB_CLIENT_SECRET
|
|
170
|
+
**Description:** OAuth application client secret from GitHub.
|
|
171
|
+
|
|
172
|
+
- **Purpose:** Authenticates your application with GitHub's OAuth service
|
|
173
|
+
- **Security:** Keep this secret secure and never commit to version control
|
|
174
|
+
- **Format:** Alphanumeric string provided by GitHub
|
|
175
|
+
- **Setup:** Generated when creating OAuth app in GitHub Developer Settings
|
|
176
|
+
- **Required:** Yes (when `GITHUB_LOGIN_ENABLED=true`)
|
|
177
|
+
|
|
178
|
+
**Example:** `GITHUB_CLIENT_SECRET=a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0`
|
|
179
|
+
|
|
180
|
+
### Setting Up GitHub OAuth
|
|
181
|
+
|
|
182
|
+
1. **Create GitHub OAuth App:**
|
|
183
|
+
- Go to [GitHub Developer Settings](https://github.com/settings/developers)
|
|
184
|
+
- Click "New OAuth App"
|
|
185
|
+
- Fill in application details:
|
|
186
|
+
- **Application name:** Your app name
|
|
187
|
+
- **Homepage URL:** `https://yourdomain.com` (or `http://localhost:3000` for dev)
|
|
188
|
+
- **Authorization callback URL:** `https://yourdomain.com/auth/github/callback`
|
|
189
|
+
- Click "Register application"
|
|
190
|
+
|
|
191
|
+
2. **Copy Credentials:**
|
|
192
|
+
- Copy the **Client ID**
|
|
193
|
+
- Generate and copy the **Client Secret**
|
|
194
|
+
|
|
195
|
+
3. **Configure Environment:**
|
|
196
|
+
```env
|
|
197
|
+
GITHUB_LOGIN_ENABLED=true
|
|
198
|
+
GITHUB_CLIENT_ID=your-copied-client-id
|
|
199
|
+
GITHUB_CLIENT_SECRET=your-copied-client-secret
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
**Security Notes:**
|
|
203
|
+
- Use separate OAuth apps for development and production environments
|
|
204
|
+
- Rotate client secrets periodically
|
|
205
|
+
- Never expose client secrets in client-side code
|
|
206
|
+
|
|
207
|
+
---
|
|
208
|
+
|
|
141
209
|
## 🚀 Quick Setup Examples
|
|
142
210
|
|
|
143
211
|
### Development Environment
|