cca-auth-module 0.2.1 → 0.2.21

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
@@ -1,395 +1,385 @@
1
- # Auth Module Documentation
2
-
3
- This module provides endpoints and methods for user authentication, including login, logout, registration, and token refresh operations. It also includes a helper function to verify tokens.
4
-
5
- > **Note:** This module expects proper request payloads and uses DTOs (`LoginDTO` and `RegisterDTO`) to enforce data structure. Custom error handling is assumed to be in place.
6
-
7
- ---
8
-
9
- ## Endpoints
10
-
11
- # Auth API Documentation
12
-
13
- ---
14
-
15
- ## 1. Login
16
-
17
- **POST** `/auth/login`
18
-
19
- ### Request Body
20
-
21
- ```json
22
- {
23
- "email": "user@example.com",
24
- "password": "yourPassword"
25
- }
26
- ```
27
-
28
- ### Success Response
29
-
30
- **Status:** `200 OK`
31
-
32
- ```json
33
- {
34
- "success": true,
35
- "message": "Login successful",
36
- "data": {
37
- "accessToken": "string",
38
- "userId": "string",
39
- "expiresAt": "2025-08-06T12:00:00.000Z",
40
- "auth": {
41
- "hasAccessToken": true,
42
- "enable": false,
43
- "status": "BASIC_AUTH",
44
- "verified": false
45
- }
46
- },
47
- "meta": {
48
- "timestamp": "2025-08-06T11:59:59.999Z"
49
- }
50
- }
51
- ```
52
-
53
- ---
54
-
55
- ## 2. Admin Login
56
-
57
- **POST** `/auth/admin-login`
58
-
59
- ### Request Body
60
-
61
- ```json
62
- {
63
- "email": "admin@example.com",
64
- "password": "adminPassword",
65
- "adminPassword": "superSecretAdminPassword"
66
- }
67
- ```
68
-
69
- ### Success Response
70
-
71
- **Status:** `201 Created`
72
-
73
- ```json
74
- {
75
- "success": true,
76
- "message": "Admin login successful",
77
- "data": {
78
- "message": "Admin authenticated",
79
- "auth": {
80
- "hasAccessToken": true,
81
- "enable": false,
82
- "status": "BASIC_AUTH",
83
- "verified": false
84
- }
85
- }
86
- }
87
- ```
88
-
89
- ---
90
-
91
- ## 3. Logout
92
-
93
- **POST** `/auth/logout`
94
-
95
- ### Request Body
96
-
97
- ```json
98
- {
99
- "id": "userId"
100
- }
101
- ```
102
-
103
- ### Success Response
104
-
105
- **Status:** `200 OK`
106
-
107
- ```json
108
- {
109
- "success": true,
110
- "message": "Logout successful",
111
- "data": {
112
- "auth": {
113
- "hasAccessToken": false,
114
- "enable": false,
115
- "status": "LOGGED_OUT",
116
- "verified": false
117
- }
118
- }
119
- }
120
- ```
121
-
122
- ---
123
-
124
- ## 4. Register
125
-
126
- **POST** `/auth/register`
127
-
128
- ### Request Body
129
-
130
- ```json
131
- {
132
- "email": "newuser@example.com",
133
- "name": "New User",
134
- "password": "userPassword",
135
- "role": "user",
136
- "adminPassword": "optionalAdminPassword"
137
- }
138
- ```
139
-
140
- ### Success Response
141
-
142
- **Status:** `200 OK`
143
-
144
- ```json
145
- {
146
- "success": true,
147
- "message": "Registration successful",
148
- "data": {
149
- "auth": {
150
- "hasAccessToken": false,
151
- "enable": false,
152
- "status": "REGISTERED",
153
- "verified": false
154
- }
155
- },
156
- "meta": {
157
- "status": true
158
- }
159
- }
160
- ```
161
-
162
- ---
163
-
164
- ## 5. Refresh Token
165
-
166
- **POST** `/auth/refresh-token`
167
-
168
- ### Request Body
169
-
170
- ```json
171
- {
172
- "refreshToken": "yourRefreshToken"
173
- }
174
- ```
175
-
176
- ### Success Response
177
-
178
- **Status:** `200 OK`
179
-
180
- ```json
181
- {
182
- "success": true,
183
- "message": "Token refreshed successfully",
184
- "data": {
185
- "accessToken": "newAccessToken",
186
- "refreshToken": "newRefreshToken",
187
- "auth": {
188
- "hasAccessToken": true,
189
- "enable": false,
190
- "status": "BASIC_AUTH",
191
- "verified": false
192
- }
193
- }
194
- }
195
- ```
196
-
197
- ---
198
-
199
- ## 6. 2FA Setup
200
-
201
- **POST** `/auth/2fa/setup`
202
-
203
- ### Headers
204
-
205
- ```
206
- Authorization: Bearer <accessToken>
207
- ```
208
-
209
- ### Request Body
210
-
211
- None
212
-
213
- ### Success Response
214
-
215
- **Status:** `200 OK`
216
-
217
- ```json
218
- {
219
- "success": true,
220
- "message": "2FA setup successful",
221
- "data": {
222
- "qrCode": "data:image/png;base64,...",
223
- "auth": {
224
- "hasAccessToken": true,
225
- "enable": false,
226
- "status": "NEEDS_SETUP"
227
- }
228
- },
229
- "meta": {
230
- "nextStep": "Scan the QR code and enter your first code to verify",
231
- "redirectTo": "/2fa-enable"
232
- }
233
- }
234
- ```
235
-
236
- ---
237
-
238
- ## 7. Enable 2FA
239
-
240
- **POST** `/auth/2fa/enable`
241
-
242
- ### Headers
243
-
244
- ```
245
- Authorization: Bearer <accessToken>
246
- ```
247
-
248
- ### Request Body
249
-
250
- ```json
251
- {
252
- "userId": "userId",
253
- "token": "2faToken"
254
- }
255
- ```
256
-
257
- ### Success Response
258
-
259
- **Status:** `200 OK`
260
-
261
- ```json
262
- {
263
- "success": true,
264
- "message": "2FA enabled successfully",
265
- "data": {
266
- "isEnabled": true,
267
- "enabledAt": "2025-08-06T12:00:00.000Z",
268
- "auth": {
269
- "hasAccessToken": true,
270
- "enable": true,
271
- "status": "PENDING_VERIFICATION"
272
- }
273
- },
274
- "meta": {
275
- "nextStep": "Proceed to verify with a valid 2FA token",
276
- "redirectTo": "/verify-2fa"
277
- }
278
- }
279
- ```
280
-
281
- ---
282
-
283
- ## 8. Verify 2FA
284
-
285
- **POST** `/auth/2fa/verify`
286
-
287
- ### Request Body
288
-
289
- ```json
290
- {
291
- "userId": "userId",
292
- "token": "2faVerificationToken"
293
- }
294
- ```
295
-
296
- ### Success Response
297
-
298
- **Status:** `200 OK`
299
-
300
- ```json
301
- {
302
- "success": true,
303
- "message": "2FA verification successful",
304
- "data": {
305
- "token": "accessToken",
306
- "refreshToken": "refreshToken",
307
- "user": {
308
- "id": "userId",
309
- "email": "user@example.com",
310
- "name": "User Name",
311
- "role": "user"
312
- },
313
- "auth": {
314
- "hasAccessToken": true,
315
- "enable": true,
316
- "status": "FULL_AUTH",
317
- "verified": true
318
- }
319
- },
320
- "meta": {
321
- "recommendation": "You're fully authenticated",
322
- "redirectTo": "/"
323
- }
324
- }
325
- ```
326
-
327
- ---
328
-
329
- ## 9. Disable 2FA
330
-
331
- **POST** `/auth/2fa/disable`
332
-
333
- ### Headers
334
-
335
- ```
336
- Authorization: Bearer <accessToken>
337
- ```
338
-
339
- ### Request Body
340
-
341
- ```json
342
- {
343
- "token": "current2faToken"
344
- }
345
- ```
346
-
347
- ### Success Response
348
-
349
- **Status:** `200 OK`
350
-
351
- ```json
352
- {
353
- "success": true,
354
- "message": "2FA disabled successfully",
355
- "data": {
356
- "disabledAt": "2025-08-06T12:00:00.000Z",
357
- "auth": {
358
- "hasAccessToken": true,
359
- "enable": false,
360
- "status": "BASIC_AUTH",
361
- "verified": false
362
- }
363
- },
364
- "meta": {
365
- "securityNote": "Account now relies only on password. Re-enable 2FA for better security.",
366
- "redirectTo": "/login"
367
- }
368
- }
369
- ```
370
-
371
- ---
372
-
373
- ## Notes
374
-
375
- - All endpoints returning tokens require secure HTTPS connections.
376
- - 2FA setup, enable, verify, and disable require authentication with a valid access token (sent in the Authorization header).
377
- - Request bodies use DTOs as defined in the controller (LoginDTO, RegisterDTO, ITwoFactorEnable, ITwoFactorVerify, etc.).
378
- - Error responses will follow the format:
379
-
380
- ```json
381
- {
382
- "success": false,
383
- "error": "Error message"
384
- }
385
- ```
386
-
387
- ---
388
-
389
- ## License
390
-
391
- This module is released under the [MIT License](LICENSE).
392
-
393
- ---
394
-
395
- This documentation provides an overview of the available endpoints and helper methods for authentication. For further details on business logic and DTO structures, please refer to the inline comments and source code within the module.
1
+ # CCA Auth Module
2
+
3
+ Authentication + 2FA module for CCA applications. Provides a controller, use cases, and container wiring for login, registration, refresh, and 2FA flows.
4
+
5
+ ## Features
6
+
7
+ - Login and admin login
8
+ - Registration with role support
9
+ - Refresh token rotation
10
+ - Two-factor setup, enable, verify, disable
11
+ - Middleware to require completed 2FA
12
+
13
+ ## Installation
14
+
15
+ ```bash
16
+ pnpm add cca-auth-module
17
+ ```
18
+
19
+ ## Quick Start
20
+
21
+ ```ts
22
+ import { authConfig, createAuthContainer } from "cca-auth-module";
23
+
24
+ authConfig(async () => ({
25
+ accessTokenSecret: process.env.ACCESS_TOKEN_SECRET!,
26
+ refreshTokenSecret: process.env.REFRESH_TOKEN_SECRET!,
27
+ accessTokenExpiry: "15m",
28
+ refreshTokenExpiry: "30d",
29
+ adminSecretPassword: process.env.ADMIN_SECRET_PASSWORD!,
30
+ app_name: "MyApp",
31
+ secretLength: "20",
32
+ tokenWindow: "1",
33
+ isDevelopment: false,
34
+ }));
35
+
36
+ const { authController, requireComplete2FA } = await createAuthContainer(database);
37
+ ```
38
+
39
+ ## Configuration
40
+
41
+ The module reads configuration via `authConfig` and `ConfigSource`.
42
+
43
+ ```ts
44
+ export interface IConfig {
45
+ accessTokenSecret: string;
46
+ refreshTokenSecret: string;
47
+ accessTokenExpiry: string;
48
+ refreshTokenExpiry: string;
49
+ adminSecretPassword: string;
50
+ app_name: string;
51
+ secretLength: string;
52
+ tokenWindow: string;
53
+ isDevelopment: boolean;
54
+ }
55
+ ```
56
+
57
+ ## API Endpoints
58
+
59
+ ### 1) Login
60
+
61
+ **POST** `/auth/login`
62
+
63
+ Request:
64
+ ```json
65
+ {
66
+ "email": "user@example.com",
67
+ "password": "Password1!"
68
+ }
69
+ ```
70
+
71
+ Response:
72
+ ```json
73
+ {
74
+ "success": true,
75
+ "message": "Login successful",
76
+ "data": {
77
+ "accessToken": "string",
78
+ "userId": "string",
79
+ "expiresAt": 1738166400,
80
+ "auth": {
81
+ "hasAccessToken": true,
82
+ "enabled": false,
83
+ "status": "pending_verification",
84
+ "verified": false
85
+ }
86
+ },
87
+ "meta": {
88
+ "timestamp": "2026-01-29T10:00:00.000Z"
89
+ }
90
+ }
91
+ ```
92
+
93
+ ### 2) Admin Login
94
+
95
+ **POST** `/auth/admin-login`
96
+
97
+ Request:
98
+ ```json
99
+ {
100
+ "email": "admin@example.com",
101
+ "password": "Password1!",
102
+ "adminPassword": "AdminSecret"
103
+ }
104
+ ```
105
+
106
+ Response:
107
+ ```json
108
+ {
109
+ "success": true,
110
+ "message": "Admin login successful",
111
+ "data": {
112
+ "accessToken": "string",
113
+ "userId": "string",
114
+ "expiresAt": 1738166400,
115
+ "auth": {
116
+ "hasAccessToken": true,
117
+ "enabled": true,
118
+ "status": "pending_verification",
119
+ "verified": false
120
+ }
121
+ }
122
+ }
123
+ ```
124
+
125
+ ### 3) Logout
126
+
127
+ **POST** `/auth/logout`
128
+
129
+ Headers:
130
+ ```
131
+ Authorization: Bearer <accessToken>
132
+ ```
133
+
134
+ Request:
135
+ ```json
136
+ {}
137
+ ```
138
+
139
+ Response:
140
+ ```json
141
+ {
142
+ "success": true,
143
+ "message": "Logged out successfully",
144
+ "data": {
145
+ "auth": {
146
+ "hasAccessToken": false,
147
+ "enabled": false,
148
+ "status": "logged_out",
149
+ "verified": false
150
+ }
151
+ }
152
+ }
153
+ ```
154
+
155
+ ### 4) Register
156
+
157
+ **POST** `/auth/register`
158
+
159
+ Request:
160
+ ```json
161
+ {
162
+ "email": "newuser@example.com",
163
+ "name": "New User",
164
+ "password": "Password1!",
165
+ "role": "USER",
166
+ "adminPassword": "optional-if-role-admin"
167
+ }
168
+ ```
169
+
170
+ Response:
171
+ ```json
172
+ {
173
+ "success": true,
174
+ "message": "User registered successfully",
175
+ "data": {
176
+ "auth": {
177
+ "hasAccessToken": false,
178
+ "enabled": false,
179
+ "status": "registered",
180
+ "verified": false
181
+ }
182
+ },
183
+ "meta": {
184
+ "status": true
185
+ }
186
+ }
187
+ ```
188
+
189
+ ### 5) Refresh Token
190
+
191
+ **POST** `/auth/refresh-token`
192
+
193
+ Request:
194
+ ```json
195
+ {
196
+ "refreshToken": "yourRefreshToken"
197
+ }
198
+ ```
199
+
200
+ Response:
201
+ ```json
202
+ {
203
+ "success": true,
204
+ "message": "Token refreshed successfully",
205
+ "data": {
206
+ "accessToken": "newAccessToken",
207
+ "refreshToken": "newRefreshToken",
208
+ "auth": {
209
+ "hasAccessToken": true,
210
+ "enabled": false,
211
+ "status": "basic_auth",
212
+ "verified": false
213
+ }
214
+ }
215
+ }
216
+ ```
217
+
218
+ ### 6) 2FA Setup
219
+
220
+ **POST** `/auth/2fa/setup`
221
+
222
+ Headers:
223
+ ```
224
+ Authorization: Bearer <accessToken>
225
+ ```
226
+
227
+ Response:
228
+ ```json
229
+ {
230
+ "success": true,
231
+ "message": "Two-factor authentication setup initiated",
232
+ "data": {
233
+ "qrCode": "data:image/png;base64,...",
234
+ "auth": {
235
+ "hasAccessToken": true,
236
+ "enabled": false,
237
+ "status": "needs_setup"
238
+ }
239
+ },
240
+ "meta": {
241
+ "nextStep": "Scan the QR code and enter your first code to verify",
242
+ "redirectTo": "/2fa-enable"
243
+ }
244
+ }
245
+ ```
246
+
247
+ ### 7) Enable 2FA
248
+
249
+ **POST** `/auth/2fa/enable`
250
+
251
+ Headers:
252
+ ```
253
+ Authorization: Bearer <accessToken>
254
+ ```
255
+
256
+ Request:
257
+ ```json
258
+ {
259
+ "token": "2faToken"
260
+ }
261
+ ```
262
+
263
+ Response:
264
+ ```json
265
+ {
266
+ "success": true,
267
+ "message": "Two-factor authentication enabled",
268
+ "data": {
269
+ "enabledAt": "2026-01-29T10:00:00.000Z",
270
+ "auth": {
271
+ "hasAccessToken": true,
272
+ "enabled": true,
273
+ "status": "pending_verification"
274
+ }
275
+ },
276
+ "meta": {
277
+ "nextStep": "Proceed to verify with a valid 2FA token",
278
+ "redirectTo": "/verify-2fa"
279
+ }
280
+ }
281
+ ```
282
+
283
+ ### 8) Verify 2FA
284
+
285
+ **POST** `/auth/2fa/verify`
286
+
287
+ Headers:
288
+ ```
289
+ Authorization: Bearer <accessToken>
290
+ ```
291
+
292
+ Request:
293
+ ```json
294
+ {
295
+ "token": "2faVerificationToken"
296
+ }
297
+ ```
298
+
299
+ Response:
300
+ ```json
301
+ {
302
+ "success": true,
303
+ "message": "Two-factor authentication verified successfully",
304
+ "data": {
305
+ "token": "accessToken",
306
+ "refreshToken": "refreshToken",
307
+ "user": {
308
+ "id": "userId",
309
+ "email": "user@example.com",
310
+ "name": "User Name",
311
+ "role": "USER"
312
+ },
313
+ "auth": {
314
+ "hasAccessToken": true,
315
+ "enabled": true,
316
+ "status": "full_auth",
317
+ "verified": true
318
+ }
319
+ },
320
+ "meta": {
321
+ "recommendation": "You're fully authenticated",
322
+ "redirectTo": "/"
323
+ }
324
+ }
325
+ ```
326
+
327
+ ### 9) Disable 2FA
328
+
329
+ **POST** `/auth/2fa/disable`
330
+
331
+ Headers:
332
+ ```
333
+ Authorization: Bearer <accessToken>
334
+ ```
335
+
336
+ Request:
337
+ ```json
338
+ {
339
+ "token": "current2faToken"
340
+ }
341
+ ```
342
+
343
+ Response:
344
+ ```json
345
+ {
346
+ "success": true,
347
+ "message": "Two-factor authentication disabled",
348
+ "data": {
349
+ "disabledAt": "2026-01-29T10:00:00.000Z",
350
+ "auth": {
351
+ "hasAccessToken": true,
352
+ "enabled": false,
353
+ "status": "basic_auth",
354
+ "verified": false
355
+ }
356
+ },
357
+ "meta": {
358
+ "securityNote": "Account now relies only on password. Re-enable 2FA for better security.",
359
+ "redirectTo": "/login"
360
+ }
361
+ }
362
+ ```
363
+
364
+ ## Middleware
365
+
366
+ Use `RequireComplete2FA` to block routes until the user completes 2FA. It expects a JWT with `twoFactorAuthenticated: true`.
367
+
368
+ ```ts
369
+ app.get("/secure", requireComplete2FA.execute, handler);
370
+ ```
371
+
372
+ ## Status Codes and Messages
373
+
374
+ - Status codes are defined in `src/presentation/constants/constants.ts`
375
+ - Error handling uses module errors from `src/utils/Errors.ts`
376
+
377
+ ## Notes
378
+
379
+ - All token endpoints must be served over HTTPS in production.
380
+ - Refresh tokens are rotated and validated against stored values.
381
+ - Admin registration requires a valid `adminSecretPassword`.
382
+
383
+ ## License
384
+
385
+ MIT