mbkauthe 3.1.0 โ 3.2.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/README.md +75 -15
- package/docs/api.md +115 -10
- package/docs/db.md +72 -17
- package/docs/env.md +91 -8
- package/index.d.ts +25 -2
- package/lib/config/index.js +48 -1
- package/lib/main.js +12 -0
- package/lib/middleware/index.js +4 -2
- package/lib/routes/auth.js +25 -3
- package/lib/routes/misc.js +24 -3
- package/lib/routes/oauth.js +379 -264
- package/lib/utils/response.js +2 -2
- package/package.json +18 -5
- package/test.spec.js +20 -2
- package/views/2fa.handlebars +2 -2
- package/views/loginmbkauthe.handlebars +35 -5
- package/views/sharedStyles.handlebars +5 -0
- package/views/showmessage.handlebars +27 -1
- package/.env.example +0 -3
- package/.github/PACKAGE.md +0 -17
- package/.github/workflows/codeql.yml +0 -98
- package/.github/workflows/publish.yml +0 -87
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# MBKAuthe v3.
|
|
1
|
+
# MBKAuthe v3.2 - Authentication System for Node.js
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/mbkauthe)
|
|
4
4
|
[](LICENSE)
|
|
@@ -16,19 +16,20 @@
|
|
|
16
16
|
<img height="48px" src="https://handlebarsjs.com/handlebars-icon.svg" alt="Handlebars" />
|
|
17
17
|
</p>
|
|
18
18
|
|
|
19
|
-
**MBKAuth v3.
|
|
19
|
+
**MBKAuth v3.2** is a production-ready authentication system for Node.js applications. Built with Express and PostgreSQL, it provides secure authentication, 2FA, role-based access, and OAuth integration (GitHub & Google) out of the box.
|
|
20
20
|
|
|
21
21
|
## โจ Key Features
|
|
22
22
|
|
|
23
23
|
- ๐ Secure password authentication with PBKDF2 hashing
|
|
24
24
|
- ๐ PostgreSQL session management with cross-subdomain support
|
|
25
25
|
- ๐ฑ Optional TOTP-based 2FA with trusted device memory
|
|
26
|
-
- ๐ GitHub
|
|
26
|
+
- ๐ OAuth integration (GitHub & Google)
|
|
27
27
|
- ๐ฅ Role-based access control (SuperAdmin, NormalUser, Guest)
|
|
28
28
|
- ๐ฏ Multi-application user management
|
|
29
|
-
- ๐ก๏ธ CSRF protection & rate limiting
|
|
29
|
+
- ๐ก๏ธ CSRF protection & advanced rate limiting
|
|
30
30
|
- ๐ Easy Express.js integration
|
|
31
31
|
- ๐จ Customizable Handlebars templates
|
|
32
|
+
- ๐ Enhanced security with session fixation prevention
|
|
32
33
|
|
|
33
34
|
## ๐ฆ Installation
|
|
34
35
|
|
|
@@ -48,10 +49,18 @@ IS_DEPLOYED=false
|
|
|
48
49
|
DOMAIN=localhost
|
|
49
50
|
LOGIN_DB=postgresql://user:pass@localhost:5432/db
|
|
50
51
|
|
|
51
|
-
# Optional
|
|
52
|
+
# Optional Features
|
|
52
53
|
MBKAUTH_TWO_FA_ENABLE=false
|
|
53
54
|
COOKIE_EXPIRE_TIME=2
|
|
55
|
+
|
|
56
|
+
# OAuth Configuration (Optional)
|
|
54
57
|
GITHUB_LOGIN_ENABLED=false
|
|
58
|
+
GITHUB_CLIENT_ID=
|
|
59
|
+
GITHUB_CLIENT_SECRET=
|
|
60
|
+
|
|
61
|
+
GOOGLE_LOGIN_ENABLED=false
|
|
62
|
+
GOOGLE_CLIENT_ID=
|
|
63
|
+
GOOGLE_CLIENT_SECRET=
|
|
55
64
|
```
|
|
56
65
|
|
|
57
66
|
**2. Set Up Database**
|
|
@@ -110,24 +119,44 @@ app.get('/admin', validateSession, checkRolePermission(['SuperAdmin']), (req, re
|
|
|
110
119
|
app.listen(3000);
|
|
111
120
|
```
|
|
112
121
|
|
|
113
|
-
## ๐งช Testing
|
|
114
122
|
|
|
115
|
-
|
|
123
|
+
## ๐งช Testing & Git Hooks
|
|
124
|
+
|
|
125
|
+
MBKAuthe includes comprehensive test coverage for all authentication features. **A pre-commit hook is provided to ensure code quality:**
|
|
126
|
+
|
|
127
|
+
### Pre-commit Hook (Automatic Test Runner)
|
|
128
|
+
|
|
129
|
+
- Located at `scripts/pre-commit` and `scripts/pre-commit` (Node.js, cross-platform)
|
|
130
|
+
- Starts the dev server, runs all tests, and blocks commits if any test fails
|
|
131
|
+
- The dev server is automatically stopped after tests complete
|
|
132
|
+
- Ensures you never commit code that breaks tests
|
|
133
|
+
|
|
134
|
+
### Git Hook Setup
|
|
135
|
+
|
|
136
|
+
Hooks are auto-configured every time you run `npm run dev`, `npm test`, or `npm run test:watch` (see `scripts/setup-hooks.js`).
|
|
137
|
+
|
|
138
|
+
If you ever need to manually set up hooks:
|
|
116
139
|
|
|
117
140
|
```bash
|
|
118
|
-
|
|
141
|
+
node scripts/setup-hooks.js
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### Running Tests
|
|
145
|
+
|
|
146
|
+
```bash
|
|
147
|
+
# Run all tests (auto-configures hooks)
|
|
119
148
|
npm test
|
|
120
149
|
|
|
121
|
-
# Run tests in watch mode (auto-
|
|
150
|
+
# Run tests in watch mode (auto-configures hooks)
|
|
122
151
|
npm run test:watch
|
|
123
152
|
|
|
124
|
-
# Run with development flags
|
|
153
|
+
# Run with development flags (auto-configures hooks)
|
|
125
154
|
npm run dev
|
|
126
155
|
```
|
|
127
156
|
|
|
128
157
|
**Test Coverage:**
|
|
129
158
|
- โ
Authentication flows (login, 2FA, logout)
|
|
130
|
-
- โ
OAuth integration (GitHub)
|
|
159
|
+
- โ
OAuth integration (GitHub)
|
|
131
160
|
- โ
Session management and security
|
|
132
161
|
- โ
Role-based access control
|
|
133
162
|
- โ
API endpoints and error handling
|
|
@@ -183,6 +212,8 @@ app.post('/api/data', authenticate(process.env.API_TOKEN), handler);
|
|
|
183
212
|
**OAuth Routes:**
|
|
184
213
|
- `GET /mbkauthe/api/github/login` - GitHub OAuth initiation (10/5min rate limit)
|
|
185
214
|
- `GET /mbkauthe/api/github/login/callback` - GitHub OAuth callback
|
|
215
|
+
- `GET /mbkauthe/api/google/login` - Google OAuth initiation (10/5min rate limit)
|
|
216
|
+
- `GET /mbkauthe/api/google/login/callback` - Google OAuth callback
|
|
186
217
|
|
|
187
218
|
**Information & Utility Routes:**
|
|
188
219
|
- `GET /mbkauthe/info`, `/mbkauthe/i` - Version & config info (8/min rate limit)
|
|
@@ -200,11 +231,12 @@ app.post('/api/data', authenticate(process.env.API_TOKEN), handler);
|
|
|
200
231
|
|
|
201
232
|
## ๐ Security Features
|
|
202
233
|
|
|
203
|
-
- **Rate Limiting**: Login (8/min), Logout (10/min), 2FA (5/min), OAuth (10/5min)
|
|
204
|
-
- **CSRF Protection**: All
|
|
234
|
+
- **Rate Limiting**: Login (8/min), Logout (10/min), 2FA (5/min), OAuth (10/5min), Admin (3/5min)
|
|
235
|
+
- **CSRF Protection**: All state-changing routes protected with token validation
|
|
205
236
|
- **Secure Cookies**: httpOnly, sameSite, secure in production
|
|
206
237
|
- **Password Hashing**: PBKDF2 with 100k iterations
|
|
207
|
-
- **Session Security**: PostgreSQL-backed, automatic cleanup
|
|
238
|
+
- **Session Security**: PostgreSQL-backed, automatic cleanup, session fixation prevention
|
|
239
|
+
- **OAuth Security**: State validation, token expiry handling, secure callback validation
|
|
208
240
|
|
|
209
241
|
## ๐ฑ Two-Factor Authentication
|
|
210
242
|
|
|
@@ -220,7 +252,9 @@ CREATE TABLE "TwoFA" (
|
|
|
220
252
|
|
|
221
253
|
Users can mark devices as trusted to skip 2FA for configurable duration.
|
|
222
254
|
|
|
223
|
-
## ๐
|
|
255
|
+
## ๐ OAuth Integration
|
|
256
|
+
|
|
257
|
+
### GitHub OAuth
|
|
224
258
|
|
|
225
259
|
**Setup:**
|
|
226
260
|
|
|
@@ -243,6 +277,32 @@ CREATE TABLE user_github (
|
|
|
243
277
|
);
|
|
244
278
|
```
|
|
245
279
|
|
|
280
|
+
### Google OAuth
|
|
281
|
+
|
|
282
|
+
**Setup:**
|
|
283
|
+
|
|
284
|
+
1. Create Google OAuth 2.0 Client in [Google Cloud Console](https://console.cloud.google.com/)
|
|
285
|
+
2. Add authorized redirect URI: `https://yourdomain.com/mbkauthe/api/google/login/callback`
|
|
286
|
+
3. Configure environment:
|
|
287
|
+
```env
|
|
288
|
+
GOOGLE_LOGIN_ENABLED=true
|
|
289
|
+
GOOGLE_CLIENT_ID=your_client_id
|
|
290
|
+
GOOGLE_CLIENT_SECRET=your_client_secret
|
|
291
|
+
```
|
|
292
|
+
4. Create table:
|
|
293
|
+
```sql
|
|
294
|
+
CREATE TABLE user_google (
|
|
295
|
+
id SERIAL PRIMARY KEY,
|
|
296
|
+
user_name VARCHAR(50) REFERENCES "Users"("UserName"),
|
|
297
|
+
google_id VARCHAR(255) UNIQUE,
|
|
298
|
+
google_email VARCHAR(255),
|
|
299
|
+
access_token VARCHAR(255),
|
|
300
|
+
created_at TIMESTAMP DEFAULT NOW()
|
|
301
|
+
);
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
**Note:** Users must link their OAuth accounts before they can use OAuth login.
|
|
305
|
+
|
|
246
306
|
## ๐จ Customization
|
|
247
307
|
|
|
248
308
|
**Redirect URL:**
|
package/docs/api.md
CHANGED
|
@@ -108,6 +108,7 @@ Renders the main login page.
|
|
|
108
108
|
|
|
109
109
|
**Template Variables:**
|
|
110
110
|
- `githubLoginEnabled` - Whether GitHub OAuth is enabled
|
|
111
|
+
- `googleLoginEnabled` - Whether Google OAuth is enabled
|
|
111
112
|
- `customURL` - Redirect URL after login
|
|
112
113
|
- `userLoggedIn` - Whether user is already authenticated
|
|
113
114
|
- `username` - Current username if logged in
|
|
@@ -328,7 +329,7 @@ Terminates all active sessions across all users (admin only).
|
|
|
328
329
|
|
|
329
330
|
**Authentication:** API token required (via `authenticate` middleware)
|
|
330
331
|
|
|
331
|
-
**Rate Limit:**
|
|
332
|
+
**Rate Limit:** 3 requests per 5 minutes
|
|
332
333
|
|
|
333
334
|
**Headers:**
|
|
334
335
|
```
|
|
@@ -354,11 +355,12 @@ Authorization: your-main-secret-token
|
|
|
354
355
|
| 500 | Internal Server Error |
|
|
355
356
|
|
|
356
357
|
**Implementation Details:**
|
|
357
|
-
- Clears all user session IDs in the database (`Users` table)
|
|
358
|
-
- Deletes all session records from the `session` table
|
|
358
|
+
- Clears all user session IDs in the database (`Users` table) where sessions exist
|
|
359
|
+
- Deletes all active session records from the `session` table
|
|
359
360
|
- Destroys the current request session
|
|
360
361
|
- Clears session cookies
|
|
361
362
|
- Runs database operations in parallel for better performance
|
|
363
|
+
- Uses optimized queries with WHERE clauses to avoid unnecessary updates
|
|
362
364
|
|
|
363
365
|
**Example Request:**
|
|
364
366
|
```javascript
|
|
@@ -557,12 +559,16 @@ GET /mbkauthe/test
|
|
|
557
559
|
|
|
558
560
|
### OAuth Endpoints
|
|
559
561
|
|
|
560
|
-
####
|
|
562
|
+
#### GitHub OAuth
|
|
563
|
+
|
|
564
|
+
##### `GET /mbkauthe/api/github/login`
|
|
561
565
|
|
|
562
566
|
Initiates the GitHub OAuth authentication flow.
|
|
563
567
|
|
|
564
568
|
**Rate Limit:** 10 requests per 5 minutes
|
|
565
569
|
|
|
570
|
+
**CSRF Protection:** Required (state parameter used for validation)
|
|
571
|
+
|
|
566
572
|
**Query Parameters:**
|
|
567
573
|
- `redirect` (optional) - Relative URL to redirect after successful authentication (must start with `/` to prevent open redirect attacks)
|
|
568
574
|
|
|
@@ -580,15 +586,16 @@ GET /mbkauthe/api/github/login?redirect=/dashboard
|
|
|
580
586
|
|
|
581
587
|
**Workflow:**
|
|
582
588
|
1. User clicks "Login with GitHub"
|
|
583
|
-
2.
|
|
584
|
-
3.
|
|
585
|
-
4.
|
|
586
|
-
5.
|
|
587
|
-
6.
|
|
589
|
+
2. CSRF token generated and stored in session
|
|
590
|
+
3. Redirects to GitHub for authorization
|
|
591
|
+
4. GitHub redirects back to callback URL
|
|
592
|
+
5. System verifies GitHub account is linked
|
|
593
|
+
6. If 2FA enabled, prompts for 2FA
|
|
594
|
+
7. Creates session and redirects to specified URL
|
|
588
595
|
|
|
589
596
|
---
|
|
590
597
|
|
|
591
|
-
|
|
598
|
+
##### `GET /mbkauthe/api/github/login/callback`
|
|
592
599
|
|
|
593
600
|
Handles the OAuth callback from GitHub after user authorization.
|
|
594
601
|
|
|
@@ -628,6 +635,104 @@ WHERE ug.github_id = $1
|
|
|
628
635
|
|
|
629
636
|
---
|
|
630
637
|
|
|
638
|
+
#### Google OAuth
|
|
639
|
+
|
|
640
|
+
##### `GET /mbkauthe/api/google/login`
|
|
641
|
+
|
|
642
|
+
Initiates the Google OAuth 2.0 authentication flow.
|
|
643
|
+
|
|
644
|
+
**Rate Limit:** 10 requests per 5 minutes
|
|
645
|
+
|
|
646
|
+
**CSRF Protection:** Required (state parameter used for validation)
|
|
647
|
+
|
|
648
|
+
**Query Parameters:**
|
|
649
|
+
- `redirect` (optional) - Relative URL to redirect after successful authentication (must start with `/` to prevent open redirect attacks)
|
|
650
|
+
|
|
651
|
+
**Response:** Redirects to Google OAuth authorization page
|
|
652
|
+
|
|
653
|
+
**Prerequisites:**
|
|
654
|
+
- `GOOGLE_LOGIN_ENABLED=true` in environment
|
|
655
|
+
- Valid `GOOGLE_CLIENT_ID` and `GOOGLE_CLIENT_SECRET` configured
|
|
656
|
+
- User's Google account must be linked to an MBKAuth account in `user_google` table
|
|
657
|
+
|
|
658
|
+
**Example:**
|
|
659
|
+
```
|
|
660
|
+
GET /mbkauthe/api/google/login?redirect=/dashboard
|
|
661
|
+
```
|
|
662
|
+
|
|
663
|
+
**Workflow:**
|
|
664
|
+
1. User clicks "Login with Google"
|
|
665
|
+
2. CSRF token generated and stored in session
|
|
666
|
+
3. Redirects to Google for authorization
|
|
667
|
+
4. Google redirects back to callback URL
|
|
668
|
+
5. System verifies Google account is linked
|
|
669
|
+
6. If 2FA enabled, prompts for 2FA
|
|
670
|
+
7. Creates session and redirects to specified URL
|
|
671
|
+
|
|
672
|
+
**Error Responses:**
|
|
673
|
+
- **Configuration Error**: Returns 500 if Google OAuth credentials are not properly configured
|
|
674
|
+
- **Disabled**: Returns 403 if `GOOGLE_LOGIN_ENABLED` is false
|
|
675
|
+
|
|
676
|
+
---
|
|
677
|
+
|
|
678
|
+
##### `GET /mbkauthe/api/google/login/callback`
|
|
679
|
+
|
|
680
|
+
Handles the OAuth callback from Google after user authorization.
|
|
681
|
+
|
|
682
|
+
**Rate Limit:** Inherited from OAuth rate limiter (10 requests per 5 minutes)
|
|
683
|
+
|
|
684
|
+
**Query Parameters:**
|
|
685
|
+
- `code` - Authorization code from Google (automatically provided)
|
|
686
|
+
- `state` - State parameter for CSRF protection (automatically provided)
|
|
687
|
+
|
|
688
|
+
**Response:**
|
|
689
|
+
- Redirects to 2FA page if 2FA is enabled for the user
|
|
690
|
+
- Redirects to `loginRedirectURL` or stored redirect URL if 2FA is not required
|
|
691
|
+
- Renders error page if authentication fails
|
|
692
|
+
|
|
693
|
+
**Error Handling:**
|
|
694
|
+
- **Google Not Linked**: Returns error if Google account is not in `user_google` table
|
|
695
|
+
- **Account Inactive**: Returns error if user account is deactivated
|
|
696
|
+
- **Not Authorized**: Returns error if user is not allowed to access the application
|
|
697
|
+
- **Google Auth Error**: Returns error for any OAuth-related failures
|
|
698
|
+
- **Token Error**: Handles expired or invalid OAuth tokens with user-friendly message
|
|
699
|
+
- **CSRF Validation Failed**: Returns 403 if state parameter doesn't match
|
|
700
|
+
|
|
701
|
+
**Success Flow:**
|
|
702
|
+
```
|
|
703
|
+
Google โ /api/google/login/callback
|
|
704
|
+
โ (CSRF Validation)
|
|
705
|
+
โ (If 2FA enabled) โ /mbkauthe/2fa
|
|
706
|
+
โ (If no 2FA) โ loginRedirectURL or stored redirect
|
|
707
|
+
```
|
|
708
|
+
|
|
709
|
+
**Database Query:**
|
|
710
|
+
```sql
|
|
711
|
+
SELECT ug.*, u."UserName", u."Role", u."Active", u."AllowedApps", u."id"
|
|
712
|
+
FROM user_google ug
|
|
713
|
+
JOIN "Users" u ON ug.user_name = u."UserName"
|
|
714
|
+
WHERE ug.google_id = $1
|
|
715
|
+
```
|
|
716
|
+
|
|
717
|
+
**Note:** This endpoint is automatically called by Google and should not be accessed directly by users.
|
|
718
|
+
|
|
719
|
+
---
|
|
720
|
+
|
|
721
|
+
#### OAuth Security Features
|
|
722
|
+
|
|
723
|
+
Both GitHub and Google OAuth implementations include:
|
|
724
|
+
|
|
725
|
+
- **CSRF Protection**: State parameter validation to prevent cross-site request forgery
|
|
726
|
+
- **Session Security**: OAuth state tokens stored in session and validated on callback
|
|
727
|
+
- **Rate Limiting**: 10 requests per 5 minutes to prevent abuse
|
|
728
|
+
- **Token Validation**: Proper handling of expired or invalid OAuth tokens
|
|
729
|
+
- **Redirect Validation**: Only allows relative URLs to prevent open redirect attacks
|
|
730
|
+
- **Account Linking**: Users must pre-link OAuth accounts before login
|
|
731
|
+
- **2FA Integration**: Respects 2FA settings and trusted device configuration
|
|
732
|
+
- **Comprehensive Error Handling**: User-friendly error messages for all failure scenarios
|
|
733
|
+
|
|
734
|
+
---
|
|
735
|
+
|
|
631
736
|
## Middleware Reference
|
|
632
737
|
|
|
633
738
|
### `validateSession`
|
package/docs/db.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
#
|
|
1
|
+
# OAuth Login Setup Guide
|
|
2
2
|
|
|
3
3
|
## Overview
|
|
4
|
-
This
|
|
4
|
+
This OAuth login feature allows users to authenticate using their GitHub or Google account if it's already linked to their account in the system. Users must first connect their OAuth account through the regular account linking process, then they can use it to log in directly.
|
|
5
5
|
|
|
6
6
|
## Setup Instructions
|
|
7
7
|
|
|
@@ -12,6 +12,10 @@ 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
|
+
|
|
16
|
+
# Google OAuth App Configuration
|
|
17
|
+
GOOGLE_CLIENT_ID=your_google_client_id
|
|
18
|
+
GOOGLE_CLIENT_SECRET=your_google_client_secret
|
|
15
19
|
```
|
|
16
20
|
|
|
17
21
|
### 2. GitHub OAuth App Setup
|
|
@@ -20,16 +24,26 @@ GITHUB_CLIENT_SECRET=your_github_client_secret
|
|
|
20
24
|
3. Set the Authorization callback URL to: `https://yourdomain.com/mbkauthe/api/github/login/callback`
|
|
21
25
|
4. Copy the Client ID and Client Secret to your `.env` file
|
|
22
26
|
|
|
23
|
-
### 3.
|
|
24
|
-
|
|
27
|
+
### 3. Google OAuth App Setup
|
|
28
|
+
1. Go to Google Cloud Console (https://console.cloud.google.com/)
|
|
29
|
+
2. Create a new project or select an existing one
|
|
30
|
+
3. Enable the Google+ API
|
|
31
|
+
4. Go to Credentials > Create Credentials > OAuth 2.0 Client ID
|
|
32
|
+
5. Set the application type to "Web application"
|
|
33
|
+
6. Add authorized redirect URI: `https://yourdomain.com/mbkauthe/api/google/login/callback`
|
|
34
|
+
7. Copy the Client ID and Client Secret to your `.env` file
|
|
35
|
+
|
|
36
|
+
### 4. Database Schema
|
|
37
|
+
Ensure your OAuth tables exist with these columns:
|
|
25
38
|
|
|
26
39
|
```sql
|
|
40
|
+
-- GitHub users table
|
|
27
41
|
CREATE TABLE user_github (
|
|
28
42
|
id SERIAL PRIMARY KEY,
|
|
29
43
|
user_name VARCHAR(50) REFERENCES "Users"("UserName"),
|
|
30
44
|
github_id VARCHAR(255) UNIQUE,
|
|
31
45
|
github_username VARCHAR(255),
|
|
32
|
-
access_token
|
|
46
|
+
access_token TEXT,
|
|
33
47
|
created_at TimeStamp WITH TIME ZONE DEFAULT NOW(),
|
|
34
48
|
updated_at TimeStamp WITH TIME ZONE DEFAULT NOW()
|
|
35
49
|
);
|
|
@@ -37,57 +51,98 @@ CREATE TABLE user_github (
|
|
|
37
51
|
-- Add indexes for performance optimization
|
|
38
52
|
CREATE INDEX IF NOT EXISTS idx_user_github_github_id ON user_github (github_id);
|
|
39
53
|
CREATE INDEX IF NOT EXISTS idx_user_github_user_name ON user_github (user_name);
|
|
54
|
+
|
|
55
|
+
-- Google users table
|
|
56
|
+
CREATE TABLE user_google (
|
|
57
|
+
id SERIAL PRIMARY KEY,
|
|
58
|
+
user_name VARCHAR(50) REFERENCES "Users"("UserName"),
|
|
59
|
+
google_id VARCHAR(255) UNIQUE,
|
|
60
|
+
google_email VARCHAR(255),
|
|
61
|
+
access_token TEXT,
|
|
62
|
+
created_at TimeStamp WITH TIME ZONE DEFAULT NOW(),
|
|
63
|
+
updated_at TimeStamp WITH TIME ZONE DEFAULT NOW()
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
-- Add indexes for performance optimization
|
|
67
|
+
CREATE INDEX IF NOT EXISTS idx_user_google_google_id ON user_google (google_id);
|
|
68
|
+
CREATE INDEX IF NOT EXISTS idx_user_google_user_name ON user_google (user_name);
|
|
40
69
|
```
|
|
41
70
|
|
|
42
71
|
## How It Works
|
|
43
72
|
|
|
44
|
-
### Login Flow
|
|
45
|
-
1. User clicks "Login with GitHub" on the login page
|
|
46
|
-
2. User is redirected to
|
|
47
|
-
3.
|
|
48
|
-
4. System checks if the
|
|
73
|
+
### Login Flow (GitHub/Google)
|
|
74
|
+
1. User clicks "Login with GitHub" or "Login with Google" on the login page
|
|
75
|
+
2. User is redirected to the OAuth provider for authentication
|
|
76
|
+
3. Provider redirects back to `/mbkauthe/api/{provider}/login/callback`
|
|
77
|
+
4. System checks if the OAuth ID exists in the respective `user_{provider}` table
|
|
49
78
|
5. If found and user is active/authorized:
|
|
50
79
|
- If 2FA is enabled, redirect to 2FA page
|
|
51
80
|
- If no 2FA, complete login and redirect to home
|
|
52
81
|
6. If not found, redirect to login page with error
|
|
53
82
|
|
|
54
83
|
### Account Linking
|
|
55
|
-
Users must first link their
|
|
84
|
+
Users must first link their OAuth account through your existing connection system (likely in user settings) before they can use OAuth login.
|
|
56
85
|
|
|
57
86
|
## API Routes Added
|
|
58
87
|
|
|
59
|
-
###
|
|
88
|
+
### GitHub Routes
|
|
89
|
+
|
|
90
|
+
#### `/mbkauthe/api/github/login`
|
|
60
91
|
- **Method**: GET
|
|
61
92
|
- **Description**: Initiates GitHub OAuth flow
|
|
62
93
|
- **Redirects to**: GitHub authorization page
|
|
63
94
|
|
|
64
|
-
|
|
95
|
+
#### `/mbkauthe/api/github/login/callback`
|
|
65
96
|
- **Method**: GET
|
|
66
97
|
- **Description**: Handles GitHub OAuth callback
|
|
67
98
|
- **Parameters**: `code` (from GitHub), `state` (optional)
|
|
68
99
|
- **Success**: Redirects to home page or configured redirect URL
|
|
69
100
|
- **Error**: Redirects to login page with error parameter
|
|
70
101
|
|
|
102
|
+
### Google Routes
|
|
103
|
+
|
|
104
|
+
#### `/mbkauthe/api/google/login`
|
|
105
|
+
- **Method**: GET
|
|
106
|
+
- **Description**: Initiates Google OAuth flow
|
|
107
|
+
- **Redirects to**: Google authorization page
|
|
108
|
+
|
|
109
|
+
#### `/mbkauthe/api/google/login/callback`
|
|
110
|
+
- **Method**: GET
|
|
111
|
+
- **Description**: Handles Google OAuth callback
|
|
112
|
+
- **Parameters**: `code` (from Google), `state` (optional)
|
|
113
|
+
- **Success**: Redirects to home page or configured redirect URL
|
|
114
|
+
- **Error**: Redirects to login page with error parameter
|
|
115
|
+
|
|
71
116
|
## Error Handling
|
|
72
117
|
|
|
73
118
|
The system handles various error cases:
|
|
74
|
-
- `github_auth_failed`:
|
|
75
|
-
- `user_not_found`:
|
|
119
|
+
- `github_auth_failed` / `google_auth_failed`: OAuth authentication failed
|
|
120
|
+
- `user_not_found`: OAuth account not linked to any user
|
|
121
|
+
- `account_inactive`: User account is deactivated
|
|
122
|
+
- `not_authorized`: User not authorized for this app
|
|
76
123
|
- `session_error`: Session save failed
|
|
77
124
|
- `internal_error`: General server error
|
|
78
125
|
|
|
79
126
|
## Testing
|
|
80
127
|
|
|
128
|
+
### GitHub Login
|
|
81
129
|
1. Create a test user in your `Users` table
|
|
82
130
|
2. Link a GitHub account to that user using your existing connection system
|
|
83
131
|
3. Try logging in with GitHub using the new login button
|
|
84
132
|
4. Check console logs for debugging information
|
|
85
133
|
|
|
134
|
+
### Google Login
|
|
135
|
+
1. Create a test user in your `Users` table
|
|
136
|
+
2. Link a Google account to that user using your existing connection system
|
|
137
|
+
3. Try logging in with Google using the new login button
|
|
138
|
+
4. Check console logs for debugging information
|
|
139
|
+
|
|
86
140
|
## Login Page Updates
|
|
87
141
|
|
|
88
142
|
The login page now includes:
|
|
89
143
|
- A "Continue with GitHub" button
|
|
90
|
-
- A
|
|
144
|
+
- A "Continue with Google" button
|
|
145
|
+
- A divider ("or") between regular and OAuth login
|
|
91
146
|
- Proper styling that matches your existing design
|
|
92
147
|
|
|
93
148
|
## Security Notes
|
|
@@ -96,7 +151,7 @@ The login page now includes:
|
|
|
96
151
|
- App authorization is checked (same as regular login)
|
|
97
152
|
- 2FA is respected if enabled
|
|
98
153
|
- Session management is handled the same way as regular login
|
|
99
|
-
-
|
|
154
|
+
- OAuth access tokens are stored securely
|
|
100
155
|
|
|
101
156
|
## Troubleshooting
|
|
102
157
|
|
package/docs/env.md
CHANGED
|
@@ -230,16 +230,18 @@ DEVICE_TRUST_DURATION_DAYS=30 # 30 days (convenience)
|
|
|
230
230
|
|
|
231
231
|
---
|
|
232
232
|
|
|
233
|
-
##
|
|
233
|
+
## ๐ OAuth Authentication
|
|
234
234
|
|
|
235
|
-
### GitHub
|
|
235
|
+
### GitHub OAuth Authentication
|
|
236
|
+
|
|
237
|
+
#### GitHub Login Configuration
|
|
236
238
|
```env
|
|
237
239
|
GITHUB_LOGIN_ENABLED=false
|
|
238
240
|
GITHUB_CLIENT_ID=your-github-client-id
|
|
239
241
|
GITHUB_CLIENT_SECRET=your-github-client-secret
|
|
240
242
|
```
|
|
241
243
|
|
|
242
|
-
|
|
244
|
+
##### GITHUB_LOGIN_ENABLED
|
|
243
245
|
**Description:** Enables or disables GitHub OAuth login functionality.
|
|
244
246
|
|
|
245
247
|
**Values:**
|
|
@@ -248,7 +250,7 @@ GITHUB_CLIENT_SECRET=your-github-client-secret
|
|
|
248
250
|
|
|
249
251
|
**Required:** Yes (if using GitHub authentication)
|
|
250
252
|
|
|
251
|
-
|
|
253
|
+
##### GITHUB_CLIENT_ID
|
|
252
254
|
**Description:** OAuth application client ID from GitHub.
|
|
253
255
|
|
|
254
256
|
- **Purpose:** Identifies your application to GitHub's OAuth service
|
|
@@ -258,7 +260,7 @@ GITHUB_CLIENT_SECRET=your-github-client-secret
|
|
|
258
260
|
|
|
259
261
|
**Example:** `GITHUB_CLIENT_ID=Iv1.a1b2c3d4e5f6g7h8`
|
|
260
262
|
|
|
261
|
-
|
|
263
|
+
##### GITHUB_CLIENT_SECRET
|
|
262
264
|
**Description:** OAuth application client secret from GitHub.
|
|
263
265
|
|
|
264
266
|
- **Purpose:** Authenticates your application with GitHub's OAuth service
|
|
@@ -269,7 +271,7 @@ GITHUB_CLIENT_SECRET=your-github-client-secret
|
|
|
269
271
|
|
|
270
272
|
**Example:** `GITHUB_CLIENT_SECRET=a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0`
|
|
271
273
|
|
|
272
|
-
|
|
274
|
+
#### Setting Up GitHub OAuth
|
|
273
275
|
|
|
274
276
|
1. **Create GitHub OAuth App:**
|
|
275
277
|
- Go to [GitHub Developer Settings](https://github.com/settings/developers)
|
|
@@ -277,7 +279,7 @@ GITHUB_CLIENT_SECRET=your-github-client-secret
|
|
|
277
279
|
- Fill in application details:
|
|
278
280
|
- **Application name:** Your app name
|
|
279
281
|
- **Homepage URL:** `https://yourdomain.com` (or `http://localhost:3000` for dev)
|
|
280
|
-
- **Authorization callback URL:** `https://yourdomain.com/
|
|
282
|
+
- **Authorization callback URL:** `https://yourdomain.com/mbkauthe/api/github/login/callback`
|
|
281
283
|
- Click "Register application"
|
|
282
284
|
|
|
283
285
|
2. **Copy Credentials:**
|
|
@@ -291,10 +293,91 @@ GITHUB_CLIENT_SECRET=your-github-client-secret
|
|
|
291
293
|
GITHUB_CLIENT_SECRET=your-copied-client-secret
|
|
292
294
|
```
|
|
293
295
|
|
|
294
|
-
|
|
296
|
+
---
|
|
297
|
+
|
|
298
|
+
### Google OAuth Authentication
|
|
299
|
+
|
|
300
|
+
#### Google Login Configuration
|
|
301
|
+
```env
|
|
302
|
+
GOOGLE_LOGIN_ENABLED=false
|
|
303
|
+
GOOGLE_CLIENT_ID=your-google-client-id
|
|
304
|
+
GOOGLE_CLIENT_SECRET=your-google-client-secret
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
##### GOOGLE_LOGIN_ENABLED
|
|
308
|
+
**Description:** Enables or disables Google OAuth login functionality.
|
|
309
|
+
|
|
310
|
+
**Values:**
|
|
311
|
+
- `true` - Enable Google login (users can authenticate via Google)
|
|
312
|
+
- `false` - Disable Google login (default)
|
|
313
|
+
|
|
314
|
+
**Required:** Yes (if using Google authentication)
|
|
315
|
+
|
|
316
|
+
##### GOOGLE_CLIENT_ID
|
|
317
|
+
**Description:** OAuth 2.0 client ID from Google Cloud Console.
|
|
318
|
+
|
|
319
|
+
- **Purpose:** Identifies your application to Google's OAuth service
|
|
320
|
+
- **Format:** String ending in `.apps.googleusercontent.com`
|
|
321
|
+
- **Setup:** Obtain from [Google Cloud Console](https://console.cloud.google.com/)
|
|
322
|
+
- **Required:** Yes (when `GOOGLE_LOGIN_ENABLED=true`)
|
|
323
|
+
|
|
324
|
+
**Example:** `GOOGLE_CLIENT_ID=123456789-abc123def456.apps.googleusercontent.com`
|
|
325
|
+
|
|
326
|
+
##### GOOGLE_CLIENT_SECRET
|
|
327
|
+
**Description:** OAuth 2.0 client secret from Google Cloud Console.
|
|
328
|
+
|
|
329
|
+
- **Purpose:** Authenticates your application with Google's OAuth service
|
|
330
|
+
- **Security:** Keep this secret secure and never commit to version control
|
|
331
|
+
- **Format:** Alphanumeric string provided by Google
|
|
332
|
+
- **Setup:** Generated when creating OAuth credentials in Google Cloud Console
|
|
333
|
+
- **Required:** Yes (when `GOOGLE_LOGIN_ENABLED=true`)
|
|
334
|
+
|
|
335
|
+
**Example:** `GOOGLE_CLIENT_SECRET=GOCSPX-abc123def456ghi789jkl012mno`
|
|
336
|
+
|
|
337
|
+
#### Setting Up Google OAuth
|
|
338
|
+
|
|
339
|
+
1. **Create Google Cloud Project:**
|
|
340
|
+
- Go to [Google Cloud Console](https://console.cloud.google.com/)
|
|
341
|
+
- Create a new project or select an existing one
|
|
342
|
+
- Enable the Google+ API (or People API)
|
|
343
|
+
|
|
344
|
+
2. **Configure OAuth Consent Screen:**
|
|
345
|
+
- Navigate to "OAuth consent screen" in the sidebar
|
|
346
|
+
- Choose "External" user type (or "Internal" for Google Workspace)
|
|
347
|
+
- Fill in required app information
|
|
348
|
+
- Add your domain to authorized domains
|
|
349
|
+
- Save and continue
|
|
350
|
+
|
|
351
|
+
3. **Create OAuth Credentials:**
|
|
352
|
+
- Navigate to "Credentials" in the sidebar
|
|
353
|
+
- Click "Create Credentials" > "OAuth 2.0 Client ID"
|
|
354
|
+
- Choose "Web application" as application type
|
|
355
|
+
- Add authorized JavaScript origins:
|
|
356
|
+
- `https://yourdomain.com`
|
|
357
|
+
- `http://localhost:3000` (for development)
|
|
358
|
+
- Add authorized redirect URIs:
|
|
359
|
+
- `https://yourdomain.com/mbkauthe/api/google/login/callback`
|
|
360
|
+
- `http://localhost:3000/mbkauthe/api/google/login/callback` (for development)
|
|
361
|
+
- Click "Create"
|
|
362
|
+
|
|
363
|
+
4. **Copy Credentials:**
|
|
364
|
+
- Copy the **Client ID**
|
|
365
|
+
- Copy the **Client Secret**
|
|
366
|
+
|
|
367
|
+
5. **Configure Environment:**
|
|
368
|
+
```env
|
|
369
|
+
GOOGLE_LOGIN_ENABLED=true
|
|
370
|
+
GOOGLE_CLIENT_ID=your-copied-client-id
|
|
371
|
+
GOOGLE_CLIENT_SECRET=your-copied-client-secret
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
### OAuth Security Notes
|
|
375
|
+
|
|
295
376
|
- Use separate OAuth apps for development and production environments
|
|
296
377
|
- Rotate client secrets periodically
|
|
297
378
|
- Never expose client secrets in client-side code
|
|
379
|
+
- Ensure callback URLs match exactly with OAuth provider configuration
|
|
380
|
+
- Users must link their OAuth accounts before they can use OAuth login
|
|
298
381
|
|
|
299
382
|
---
|
|
300
383
|
|