servcraft 0.1.0 → 0.1.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/.claude/settings.local.json +29 -0
- package/.github/CODEOWNERS +18 -0
- package/.github/PULL_REQUEST_TEMPLATE.md +46 -0
- package/.github/dependabot.yml +59 -0
- package/.github/workflows/ci.yml +188 -0
- package/.github/workflows/release.yml +195 -0
- package/AUDIT.md +602 -0
- package/README.md +1070 -1
- package/dist/cli/index.cjs +2026 -2168
- package/dist/cli/index.cjs.map +1 -1
- package/dist/cli/index.js +2026 -2168
- package/dist/cli/index.js.map +1 -1
- package/dist/index.cjs +595 -616
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +114 -52
- package/dist/index.d.ts +114 -52
- package/dist/index.js +595 -616
- package/dist/index.js.map +1 -1
- package/docs/CLI-001_MULTI_DB_PLAN.md +546 -0
- package/docs/DATABASE_MULTI_ORM.md +399 -0
- package/docs/PHASE1_BREAKDOWN.md +346 -0
- package/docs/PROGRESS.md +550 -0
- package/docs/modules/ANALYTICS.md +226 -0
- package/docs/modules/API-VERSIONING.md +252 -0
- package/docs/modules/AUDIT.md +192 -0
- package/docs/modules/AUTH.md +431 -0
- package/docs/modules/CACHE.md +346 -0
- package/docs/modules/EMAIL.md +254 -0
- package/docs/modules/FEATURE-FLAG.md +291 -0
- package/docs/modules/I18N.md +294 -0
- package/docs/modules/MEDIA-PROCESSING.md +281 -0
- package/docs/modules/MFA.md +266 -0
- package/docs/modules/NOTIFICATION.md +311 -0
- package/docs/modules/OAUTH.md +237 -0
- package/docs/modules/PAYMENT.md +804 -0
- package/docs/modules/QUEUE.md +540 -0
- package/docs/modules/RATE-LIMIT.md +339 -0
- package/docs/modules/SEARCH.md +288 -0
- package/docs/modules/SECURITY.md +327 -0
- package/docs/modules/SESSION.md +382 -0
- package/docs/modules/SWAGGER.md +305 -0
- package/docs/modules/UPLOAD.md +296 -0
- package/docs/modules/USER.md +505 -0
- package/docs/modules/VALIDATION.md +294 -0
- package/docs/modules/WEBHOOK.md +270 -0
- package/docs/modules/WEBSOCKET.md +691 -0
- package/package.json +53 -38
- package/prisma/schema.prisma +395 -1
- package/src/cli/commands/add-module.ts +520 -87
- package/src/cli/commands/db.ts +3 -4
- package/src/cli/commands/docs.ts +256 -6
- package/src/cli/commands/generate.ts +12 -19
- package/src/cli/commands/init.ts +384 -214
- package/src/cli/index.ts +0 -4
- package/src/cli/templates/repository.ts +6 -1
- package/src/cli/templates/routes.ts +6 -21
- package/src/cli/utils/docs-generator.ts +6 -7
- package/src/cli/utils/env-manager.ts +717 -0
- package/src/cli/utils/field-parser.ts +16 -7
- package/src/cli/utils/interactive-prompt.ts +223 -0
- package/src/cli/utils/template-manager.ts +346 -0
- package/src/config/database.config.ts +183 -0
- package/src/config/env.ts +0 -10
- package/src/config/index.ts +0 -14
- package/src/core/server.ts +1 -1
- package/src/database/adapters/mongoose.adapter.ts +132 -0
- package/src/database/adapters/prisma.adapter.ts +118 -0
- package/src/database/connection.ts +190 -0
- package/src/database/interfaces/database.interface.ts +85 -0
- package/src/database/interfaces/index.ts +7 -0
- package/src/database/interfaces/repository.interface.ts +129 -0
- package/src/database/models/mongoose/index.ts +7 -0
- package/src/database/models/mongoose/payment.schema.ts +347 -0
- package/src/database/models/mongoose/user.schema.ts +154 -0
- package/src/database/prisma.ts +1 -4
- package/src/database/redis.ts +101 -0
- package/src/database/repositories/mongoose/index.ts +7 -0
- package/src/database/repositories/mongoose/payment.repository.ts +380 -0
- package/src/database/repositories/mongoose/user.repository.ts +255 -0
- package/src/database/seed.ts +6 -1
- package/src/index.ts +9 -20
- package/src/middleware/security.ts +2 -6
- package/src/modules/analytics/analytics.routes.ts +80 -0
- package/src/modules/analytics/analytics.service.ts +364 -0
- package/src/modules/analytics/index.ts +18 -0
- package/src/modules/analytics/types.ts +180 -0
- package/src/modules/api-versioning/index.ts +15 -0
- package/src/modules/api-versioning/types.ts +86 -0
- package/src/modules/api-versioning/versioning.middleware.ts +120 -0
- package/src/modules/api-versioning/versioning.routes.ts +54 -0
- package/src/modules/api-versioning/versioning.service.ts +189 -0
- package/src/modules/audit/audit.repository.ts +206 -0
- package/src/modules/audit/audit.service.ts +27 -59
- package/src/modules/auth/auth.controller.ts +2 -2
- package/src/modules/auth/auth.middleware.ts +3 -9
- package/src/modules/auth/auth.routes.ts +10 -107
- package/src/modules/auth/auth.service.ts +126 -23
- package/src/modules/auth/index.ts +3 -4
- package/src/modules/cache/cache.service.ts +367 -0
- package/src/modules/cache/index.ts +10 -0
- package/src/modules/cache/types.ts +44 -0
- package/src/modules/email/email.service.ts +3 -10
- package/src/modules/email/templates.ts +2 -8
- package/src/modules/feature-flag/feature-flag.repository.ts +303 -0
- package/src/modules/feature-flag/feature-flag.routes.ts +247 -0
- package/src/modules/feature-flag/feature-flag.service.ts +566 -0
- package/src/modules/feature-flag/index.ts +20 -0
- package/src/modules/feature-flag/types.ts +192 -0
- package/src/modules/i18n/i18n.middleware.ts +186 -0
- package/src/modules/i18n/i18n.routes.ts +191 -0
- package/src/modules/i18n/i18n.service.ts +456 -0
- package/src/modules/i18n/index.ts +18 -0
- package/src/modules/i18n/types.ts +118 -0
- package/src/modules/media-processing/index.ts +17 -0
- package/src/modules/media-processing/media-processing.routes.ts +111 -0
- package/src/modules/media-processing/media-processing.service.ts +245 -0
- package/src/modules/media-processing/types.ts +156 -0
- package/src/modules/mfa/index.ts +20 -0
- package/src/modules/mfa/mfa.repository.ts +206 -0
- package/src/modules/mfa/mfa.routes.ts +595 -0
- package/src/modules/mfa/mfa.service.ts +572 -0
- package/src/modules/mfa/totp.ts +150 -0
- package/src/modules/mfa/types.ts +57 -0
- package/src/modules/notification/index.ts +20 -0
- package/src/modules/notification/notification.repository.ts +356 -0
- package/src/modules/notification/notification.service.ts +483 -0
- package/src/modules/notification/types.ts +119 -0
- package/src/modules/oauth/index.ts +20 -0
- package/src/modules/oauth/oauth.repository.ts +219 -0
- package/src/modules/oauth/oauth.routes.ts +446 -0
- package/src/modules/oauth/oauth.service.ts +293 -0
- package/src/modules/oauth/providers/apple.provider.ts +250 -0
- package/src/modules/oauth/providers/facebook.provider.ts +181 -0
- package/src/modules/oauth/providers/github.provider.ts +248 -0
- package/src/modules/oauth/providers/google.provider.ts +189 -0
- package/src/modules/oauth/providers/twitter.provider.ts +214 -0
- package/src/modules/oauth/types.ts +94 -0
- package/src/modules/payment/index.ts +19 -0
- package/src/modules/payment/payment.repository.ts +733 -0
- package/src/modules/payment/payment.routes.ts +390 -0
- package/src/modules/payment/payment.service.ts +354 -0
- package/src/modules/payment/providers/mobile-money.provider.ts +274 -0
- package/src/modules/payment/providers/paypal.provider.ts +190 -0
- package/src/modules/payment/providers/stripe.provider.ts +215 -0
- package/src/modules/payment/types.ts +140 -0
- package/src/modules/queue/cron.ts +438 -0
- package/src/modules/queue/index.ts +87 -0
- package/src/modules/queue/queue.routes.ts +600 -0
- package/src/modules/queue/queue.service.ts +842 -0
- package/src/modules/queue/types.ts +222 -0
- package/src/modules/queue/workers.ts +366 -0
- package/src/modules/rate-limit/index.ts +59 -0
- package/src/modules/rate-limit/rate-limit.middleware.ts +134 -0
- package/src/modules/rate-limit/rate-limit.routes.ts +269 -0
- package/src/modules/rate-limit/rate-limit.service.ts +348 -0
- package/src/modules/rate-limit/stores/memory.store.ts +165 -0
- package/src/modules/rate-limit/stores/redis.store.ts +322 -0
- package/src/modules/rate-limit/types.ts +153 -0
- package/src/modules/search/adapters/elasticsearch.adapter.ts +326 -0
- package/src/modules/search/adapters/meilisearch.adapter.ts +261 -0
- package/src/modules/search/adapters/memory.adapter.ts +278 -0
- package/src/modules/search/index.ts +21 -0
- package/src/modules/search/search.service.ts +234 -0
- package/src/modules/search/types.ts +214 -0
- package/src/modules/security/index.ts +40 -0
- package/src/modules/security/sanitize.ts +223 -0
- package/src/modules/security/security-audit.service.ts +388 -0
- package/src/modules/security/security.middleware.ts +398 -0
- package/src/modules/session/index.ts +3 -0
- package/src/modules/session/session.repository.ts +159 -0
- package/src/modules/session/session.service.ts +340 -0
- package/src/modules/session/types.ts +38 -0
- package/src/modules/swagger/index.ts +7 -1
- package/src/modules/swagger/schema-builder.ts +16 -4
- package/src/modules/swagger/swagger.service.ts +9 -10
- package/src/modules/swagger/types.ts +0 -2
- package/src/modules/upload/index.ts +14 -0
- package/src/modules/upload/types.ts +83 -0
- package/src/modules/upload/upload.repository.ts +199 -0
- package/src/modules/upload/upload.routes.ts +311 -0
- package/src/modules/upload/upload.service.ts +448 -0
- package/src/modules/user/index.ts +3 -3
- package/src/modules/user/user.controller.ts +15 -9
- package/src/modules/user/user.repository.ts +237 -113
- package/src/modules/user/user.routes.ts +39 -164
- package/src/modules/user/user.service.ts +4 -3
- package/src/modules/validation/validator.ts +12 -17
- package/src/modules/webhook/index.ts +91 -0
- package/src/modules/webhook/retry.ts +196 -0
- package/src/modules/webhook/signature.ts +135 -0
- package/src/modules/webhook/types.ts +181 -0
- package/src/modules/webhook/webhook.repository.ts +358 -0
- package/src/modules/webhook/webhook.routes.ts +442 -0
- package/src/modules/webhook/webhook.service.ts +457 -0
- package/src/modules/websocket/features.ts +504 -0
- package/src/modules/websocket/index.ts +106 -0
- package/src/modules/websocket/middlewares.ts +298 -0
- package/src/modules/websocket/types.ts +181 -0
- package/src/modules/websocket/websocket.service.ts +692 -0
- package/src/utils/errors.ts +7 -0
- package/src/utils/pagination.ts +4 -1
- package/tests/helpers/db-check.ts +79 -0
- package/tests/integration/auth-redis.test.ts +94 -0
- package/tests/integration/cache-redis.test.ts +387 -0
- package/tests/integration/mongoose-repositories.test.ts +410 -0
- package/tests/integration/payment-prisma.test.ts +637 -0
- package/tests/integration/queue-bullmq.test.ts +417 -0
- package/tests/integration/user-prisma.test.ts +441 -0
- package/tests/integration/websocket-socketio.test.ts +552 -0
- package/tests/setup.ts +11 -9
- package/vitest.config.ts +3 -8
- package/npm-cache/_cacache/content-v2/sha512/1c/d0/03440d500a0487621aad1d6402978340698976602046db8e24fa03c01ee6c022c69b0582f969042d9442ee876ac35c038e960dd427d1e622fa24b8eb7dba +0 -0
- package/npm-cache/_cacache/content-v2/sha512/42/55/28b493ca491833e5aab0e9c3108d29ab3f36c248ca88f45d4630674fce9130959e56ae308797ac2b6328fa7f09a610b9550ed09cb971d039876d293fc69d +0 -0
- package/npm-cache/_cacache/content-v2/sha512/e0/12/f360dc9315ee5f17844a0c8c233ee6bf7c30837c4a02ea0d56c61c7f7ab21c0e958e50ed2c57c59f983c762b93056778c9009b2398ffc26def0183999b13 +0 -0
- package/npm-cache/_cacache/content-v2/sha512/ed/b0/fae1161902898f4c913c67d7f6cdf6be0665aec3b389b9c4f4f0a101ca1da59badf1b59c4e0030f5223023b8d63cfe501c46a32c20c895d4fb3f11ca2232 +0 -0
- package/npm-cache/_cacache/index-v5/58/94/c2cba79e0f16b4c10e95a87e32255741149e8222cc314a476aab67c39cc0 +0 -5
|
@@ -0,0 +1,595 @@
|
|
|
1
|
+
import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';
|
|
2
|
+
import type { AuthService } from '../auth/auth.service.js';
|
|
3
|
+
import { createAuthMiddleware } from '../auth/auth.middleware.js';
|
|
4
|
+
import { commonResponses } from '../swagger/index.js';
|
|
5
|
+
import { getMFAService } from './mfa.service.js';
|
|
6
|
+
import type { MFAMethod } from './types.js';
|
|
7
|
+
|
|
8
|
+
const mfaTag = 'MFA';
|
|
9
|
+
|
|
10
|
+
export function registerMFARoutes(app: FastifyInstance, authService: AuthService): void {
|
|
11
|
+
const authenticate = createAuthMiddleware(authService);
|
|
12
|
+
const mfaService = getMFAService();
|
|
13
|
+
|
|
14
|
+
// Get MFA status
|
|
15
|
+
app.get(
|
|
16
|
+
'/auth/mfa/status',
|
|
17
|
+
{
|
|
18
|
+
preHandler: [authenticate],
|
|
19
|
+
schema: {
|
|
20
|
+
tags: [mfaTag],
|
|
21
|
+
summary: 'Get MFA status for current user',
|
|
22
|
+
security: [{ bearerAuth: [] }],
|
|
23
|
+
response: {
|
|
24
|
+
200: {
|
|
25
|
+
type: 'object',
|
|
26
|
+
properties: {
|
|
27
|
+
success: { type: 'boolean' },
|
|
28
|
+
data: {
|
|
29
|
+
type: 'object',
|
|
30
|
+
properties: {
|
|
31
|
+
enabled: { type: 'boolean' },
|
|
32
|
+
methods: {
|
|
33
|
+
type: 'array',
|
|
34
|
+
items: { type: 'string', enum: ['totp', 'sms', 'email', 'backup_codes'] },
|
|
35
|
+
},
|
|
36
|
+
totpEnabled: { type: 'boolean' },
|
|
37
|
+
smsEnabled: { type: 'boolean' },
|
|
38
|
+
emailEnabled: { type: 'boolean' },
|
|
39
|
+
backupCodesRemaining: { type: 'number' },
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
401: commonResponses.unauthorized,
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
async (request: FastifyRequest, reply: FastifyReply) => {
|
|
49
|
+
const user = (request as FastifyRequest & { user: { id: string } }).user;
|
|
50
|
+
const userMFA = await mfaService.getUserMFA(user.id);
|
|
51
|
+
|
|
52
|
+
return reply.send({
|
|
53
|
+
success: true,
|
|
54
|
+
data: {
|
|
55
|
+
enabled: userMFA?.enabled || false,
|
|
56
|
+
methods: userMFA?.methods || [],
|
|
57
|
+
totpEnabled: userMFA?.totpVerified || false,
|
|
58
|
+
smsEnabled: userMFA?.phoneVerified || false,
|
|
59
|
+
emailEnabled: userMFA?.emailVerified || false,
|
|
60
|
+
backupCodesRemaining: mfaService.getRemainingBackupCodes(user.id),
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
// Setup TOTP (Google Authenticator, etc.)
|
|
67
|
+
app.post(
|
|
68
|
+
'/auth/mfa/totp/setup',
|
|
69
|
+
{
|
|
70
|
+
preHandler: [authenticate],
|
|
71
|
+
schema: {
|
|
72
|
+
tags: [mfaTag],
|
|
73
|
+
summary: 'Setup TOTP (Google Authenticator)',
|
|
74
|
+
description: 'Generates a secret and QR code for TOTP setup',
|
|
75
|
+
security: [{ bearerAuth: [] }],
|
|
76
|
+
response: {
|
|
77
|
+
200: {
|
|
78
|
+
type: 'object',
|
|
79
|
+
properties: {
|
|
80
|
+
success: { type: 'boolean' },
|
|
81
|
+
data: {
|
|
82
|
+
type: 'object',
|
|
83
|
+
properties: {
|
|
84
|
+
qrCode: { type: 'string', description: 'URL to QR code image' },
|
|
85
|
+
manualEntry: { type: 'string', description: 'Manual entry key' },
|
|
86
|
+
secret: { type: 'string', description: 'Base32 secret (keep secure)' },
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
401: commonResponses.unauthorized,
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
async (request: FastifyRequest, reply: FastifyReply) => {
|
|
96
|
+
const user = (request as FastifyRequest & { user: { id: string; email: string } }).user;
|
|
97
|
+
const setup = await mfaService.setupTOTP(user.id, user.email);
|
|
98
|
+
|
|
99
|
+
return reply.send({
|
|
100
|
+
success: true,
|
|
101
|
+
data: {
|
|
102
|
+
qrCode: setup.qrCode,
|
|
103
|
+
manualEntry: setup.manualEntry,
|
|
104
|
+
secret: setup.secret,
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
// Verify TOTP setup
|
|
111
|
+
app.post(
|
|
112
|
+
'/auth/mfa/totp/verify',
|
|
113
|
+
{
|
|
114
|
+
preHandler: [authenticate],
|
|
115
|
+
schema: {
|
|
116
|
+
tags: [mfaTag],
|
|
117
|
+
summary: 'Verify TOTP setup',
|
|
118
|
+
description: 'Verify the first TOTP code to complete setup',
|
|
119
|
+
security: [{ bearerAuth: [] }],
|
|
120
|
+
body: {
|
|
121
|
+
type: 'object',
|
|
122
|
+
required: ['code'],
|
|
123
|
+
properties: {
|
|
124
|
+
code: { type: 'string', minLength: 6, maxLength: 6, description: '6-digit TOTP code' },
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
response: {
|
|
128
|
+
200: {
|
|
129
|
+
type: 'object',
|
|
130
|
+
properties: {
|
|
131
|
+
success: { type: 'boolean' },
|
|
132
|
+
data: {
|
|
133
|
+
type: 'object',
|
|
134
|
+
properties: {
|
|
135
|
+
verified: { type: 'boolean' },
|
|
136
|
+
backupCodes: {
|
|
137
|
+
type: 'array',
|
|
138
|
+
items: { type: 'string' },
|
|
139
|
+
description: 'One-time backup codes (save these!)',
|
|
140
|
+
},
|
|
141
|
+
},
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
400: commonResponses.error,
|
|
146
|
+
401: commonResponses.unauthorized,
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
},
|
|
150
|
+
async (request: FastifyRequest, reply: FastifyReply) => {
|
|
151
|
+
const user = (request as FastifyRequest & { user: { id: string } }).user;
|
|
152
|
+
const { code } = request.body as { code: string };
|
|
153
|
+
const verified = await mfaService.verifyTOTPSetup(user.id, code);
|
|
154
|
+
|
|
155
|
+
if (!verified) {
|
|
156
|
+
return reply.status(400).send({
|
|
157
|
+
success: false,
|
|
158
|
+
message: 'Invalid TOTP code',
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Generate backup codes after successful TOTP setup
|
|
163
|
+
const backupCodes = await mfaService.generateBackupCodes(user.id);
|
|
164
|
+
|
|
165
|
+
return reply.send({
|
|
166
|
+
success: true,
|
|
167
|
+
data: {
|
|
168
|
+
verified: true,
|
|
169
|
+
backupCodes: backupCodes.codes,
|
|
170
|
+
},
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
);
|
|
174
|
+
|
|
175
|
+
// Disable TOTP
|
|
176
|
+
app.delete(
|
|
177
|
+
'/auth/mfa/totp',
|
|
178
|
+
{
|
|
179
|
+
preHandler: [authenticate],
|
|
180
|
+
schema: {
|
|
181
|
+
tags: [mfaTag],
|
|
182
|
+
summary: 'Disable TOTP',
|
|
183
|
+
security: [{ bearerAuth: [] }],
|
|
184
|
+
body: {
|
|
185
|
+
type: 'object',
|
|
186
|
+
required: ['code'],
|
|
187
|
+
properties: {
|
|
188
|
+
code: { type: 'string', minLength: 6, maxLength: 6 },
|
|
189
|
+
},
|
|
190
|
+
},
|
|
191
|
+
response: {
|
|
192
|
+
200: {
|
|
193
|
+
type: 'object',
|
|
194
|
+
properties: {
|
|
195
|
+
success: { type: 'boolean' },
|
|
196
|
+
message: { type: 'string' },
|
|
197
|
+
},
|
|
198
|
+
},
|
|
199
|
+
400: commonResponses.error,
|
|
200
|
+
401: commonResponses.unauthorized,
|
|
201
|
+
},
|
|
202
|
+
},
|
|
203
|
+
},
|
|
204
|
+
async (request: FastifyRequest, reply: FastifyReply) => {
|
|
205
|
+
const user = (request as FastifyRequest & { user: { id: string } }).user;
|
|
206
|
+
const { code } = request.body as { code: string };
|
|
207
|
+
await mfaService.disableTOTP(user.id, code);
|
|
208
|
+
|
|
209
|
+
return reply.send({
|
|
210
|
+
success: true,
|
|
211
|
+
message: 'TOTP disabled successfully',
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
);
|
|
215
|
+
|
|
216
|
+
// Setup SMS MFA
|
|
217
|
+
app.post(
|
|
218
|
+
'/auth/mfa/sms/setup',
|
|
219
|
+
{
|
|
220
|
+
preHandler: [authenticate],
|
|
221
|
+
schema: {
|
|
222
|
+
tags: [mfaTag],
|
|
223
|
+
summary: 'Setup SMS MFA',
|
|
224
|
+
security: [{ bearerAuth: [] }],
|
|
225
|
+
body: {
|
|
226
|
+
type: 'object',
|
|
227
|
+
required: ['phoneNumber'],
|
|
228
|
+
properties: {
|
|
229
|
+
phoneNumber: { type: 'string', pattern: '^\\+[1-9]\\d{1,14}$' },
|
|
230
|
+
},
|
|
231
|
+
},
|
|
232
|
+
response: {
|
|
233
|
+
200: {
|
|
234
|
+
type: 'object',
|
|
235
|
+
properties: {
|
|
236
|
+
success: { type: 'boolean' },
|
|
237
|
+
message: { type: 'string' },
|
|
238
|
+
},
|
|
239
|
+
},
|
|
240
|
+
400: commonResponses.error,
|
|
241
|
+
401: commonResponses.unauthorized,
|
|
242
|
+
},
|
|
243
|
+
},
|
|
244
|
+
},
|
|
245
|
+
async (request: FastifyRequest, reply: FastifyReply) => {
|
|
246
|
+
const user = (request as FastifyRequest & { user: { id: string } }).user;
|
|
247
|
+
const { phoneNumber } = request.body as { phoneNumber: string };
|
|
248
|
+
await mfaService.setupSMS(user.id, phoneNumber);
|
|
249
|
+
|
|
250
|
+
return reply.send({
|
|
251
|
+
success: true,
|
|
252
|
+
message: 'Verification code sent to your phone',
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
);
|
|
256
|
+
|
|
257
|
+
// Verify SMS setup
|
|
258
|
+
app.post(
|
|
259
|
+
'/auth/mfa/sms/verify',
|
|
260
|
+
{
|
|
261
|
+
preHandler: [authenticate],
|
|
262
|
+
schema: {
|
|
263
|
+
tags: [mfaTag],
|
|
264
|
+
summary: 'Verify SMS MFA setup',
|
|
265
|
+
security: [{ bearerAuth: [] }],
|
|
266
|
+
body: {
|
|
267
|
+
type: 'object',
|
|
268
|
+
required: ['code'],
|
|
269
|
+
properties: {
|
|
270
|
+
code: { type: 'string', minLength: 6, maxLength: 6 },
|
|
271
|
+
},
|
|
272
|
+
},
|
|
273
|
+
response: {
|
|
274
|
+
200: {
|
|
275
|
+
type: 'object',
|
|
276
|
+
properties: {
|
|
277
|
+
success: { type: 'boolean' },
|
|
278
|
+
data: { type: 'object', properties: { verified: { type: 'boolean' } } },
|
|
279
|
+
},
|
|
280
|
+
},
|
|
281
|
+
400: commonResponses.error,
|
|
282
|
+
401: commonResponses.unauthorized,
|
|
283
|
+
},
|
|
284
|
+
},
|
|
285
|
+
},
|
|
286
|
+
async (request: FastifyRequest, reply: FastifyReply) => {
|
|
287
|
+
const user = (request as FastifyRequest & { user: { id: string } }).user;
|
|
288
|
+
const { code } = request.body as { code: string };
|
|
289
|
+
const verified = await mfaService.verifySMSSetup(user.id, code);
|
|
290
|
+
|
|
291
|
+
return reply.send({
|
|
292
|
+
success: true,
|
|
293
|
+
data: { verified },
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
);
|
|
297
|
+
|
|
298
|
+
// Setup Email MFA
|
|
299
|
+
app.post(
|
|
300
|
+
'/auth/mfa/email/setup',
|
|
301
|
+
{
|
|
302
|
+
preHandler: [authenticate],
|
|
303
|
+
schema: {
|
|
304
|
+
tags: [mfaTag],
|
|
305
|
+
summary: 'Setup Email MFA',
|
|
306
|
+
security: [{ bearerAuth: [] }],
|
|
307
|
+
body: {
|
|
308
|
+
type: 'object',
|
|
309
|
+
properties: {
|
|
310
|
+
email: { type: 'string', format: 'email' },
|
|
311
|
+
},
|
|
312
|
+
},
|
|
313
|
+
response: {
|
|
314
|
+
200: {
|
|
315
|
+
type: 'object',
|
|
316
|
+
properties: {
|
|
317
|
+
success: { type: 'boolean' },
|
|
318
|
+
message: { type: 'string' },
|
|
319
|
+
},
|
|
320
|
+
},
|
|
321
|
+
400: commonResponses.error,
|
|
322
|
+
401: commonResponses.unauthorized,
|
|
323
|
+
},
|
|
324
|
+
},
|
|
325
|
+
},
|
|
326
|
+
async (request: FastifyRequest, reply: FastifyReply) => {
|
|
327
|
+
const user = (request as FastifyRequest & { user: { id: string; email: string } }).user;
|
|
328
|
+
const body = request.body as { email?: string };
|
|
329
|
+
const email = body.email || user.email;
|
|
330
|
+
await mfaService.setupEmail(user.id, email);
|
|
331
|
+
|
|
332
|
+
return reply.send({
|
|
333
|
+
success: true,
|
|
334
|
+
message: 'Verification code sent to your email',
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
);
|
|
338
|
+
|
|
339
|
+
// Verify Email setup
|
|
340
|
+
app.post(
|
|
341
|
+
'/auth/mfa/email/verify',
|
|
342
|
+
{
|
|
343
|
+
preHandler: [authenticate],
|
|
344
|
+
schema: {
|
|
345
|
+
tags: [mfaTag],
|
|
346
|
+
summary: 'Verify Email MFA setup',
|
|
347
|
+
security: [{ bearerAuth: [] }],
|
|
348
|
+
body: {
|
|
349
|
+
type: 'object',
|
|
350
|
+
required: ['code'],
|
|
351
|
+
properties: {
|
|
352
|
+
code: { type: 'string', minLength: 6, maxLength: 6 },
|
|
353
|
+
},
|
|
354
|
+
},
|
|
355
|
+
response: {
|
|
356
|
+
200: {
|
|
357
|
+
type: 'object',
|
|
358
|
+
properties: {
|
|
359
|
+
success: { type: 'boolean' },
|
|
360
|
+
data: { type: 'object', properties: { verified: { type: 'boolean' } } },
|
|
361
|
+
},
|
|
362
|
+
},
|
|
363
|
+
400: commonResponses.error,
|
|
364
|
+
401: commonResponses.unauthorized,
|
|
365
|
+
},
|
|
366
|
+
},
|
|
367
|
+
},
|
|
368
|
+
async (request: FastifyRequest, reply: FastifyReply) => {
|
|
369
|
+
const user = (request as FastifyRequest & { user: { id: string } }).user;
|
|
370
|
+
const { code } = request.body as { code: string };
|
|
371
|
+
const verified = await mfaService.verifyEmailSetup(user.id, code);
|
|
372
|
+
|
|
373
|
+
return reply.send({
|
|
374
|
+
success: true,
|
|
375
|
+
data: { verified },
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
);
|
|
379
|
+
|
|
380
|
+
// Generate new backup codes
|
|
381
|
+
app.post(
|
|
382
|
+
'/auth/mfa/backup-codes/generate',
|
|
383
|
+
{
|
|
384
|
+
preHandler: [authenticate],
|
|
385
|
+
schema: {
|
|
386
|
+
tags: [mfaTag],
|
|
387
|
+
summary: 'Generate new backup codes',
|
|
388
|
+
description: 'Generates new backup codes (invalidates old ones)',
|
|
389
|
+
security: [{ bearerAuth: [] }],
|
|
390
|
+
body: {
|
|
391
|
+
type: 'object',
|
|
392
|
+
required: ['code'],
|
|
393
|
+
properties: {
|
|
394
|
+
code: { type: 'string', description: 'Current TOTP code or backup code' },
|
|
395
|
+
},
|
|
396
|
+
},
|
|
397
|
+
response: {
|
|
398
|
+
200: {
|
|
399
|
+
type: 'object',
|
|
400
|
+
properties: {
|
|
401
|
+
success: { type: 'boolean' },
|
|
402
|
+
data: {
|
|
403
|
+
type: 'object',
|
|
404
|
+
properties: {
|
|
405
|
+
codes: { type: 'array', items: { type: 'string' } },
|
|
406
|
+
generatedAt: { type: 'string', format: 'date-time' },
|
|
407
|
+
},
|
|
408
|
+
},
|
|
409
|
+
},
|
|
410
|
+
},
|
|
411
|
+
400: commonResponses.error,
|
|
412
|
+
401: commonResponses.unauthorized,
|
|
413
|
+
},
|
|
414
|
+
},
|
|
415
|
+
},
|
|
416
|
+
async (request: FastifyRequest, reply: FastifyReply) => {
|
|
417
|
+
const user = (request as FastifyRequest & { user: { id: string } }).user;
|
|
418
|
+
const { code } = request.body as { code: string };
|
|
419
|
+
|
|
420
|
+
// Verify current MFA first
|
|
421
|
+
const verifyResult = await mfaService.verifyChallenge(user.id, code);
|
|
422
|
+
if (!verifyResult.success) {
|
|
423
|
+
return reply.status(400).send({
|
|
424
|
+
success: false,
|
|
425
|
+
message: 'Invalid verification code',
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
const result = await mfaService.generateBackupCodes(user.id);
|
|
430
|
+
|
|
431
|
+
return reply.send({
|
|
432
|
+
success: true,
|
|
433
|
+
data: result,
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
);
|
|
437
|
+
|
|
438
|
+
// Verify MFA (for login flow)
|
|
439
|
+
app.post(
|
|
440
|
+
'/auth/mfa/verify',
|
|
441
|
+
{
|
|
442
|
+
schema: {
|
|
443
|
+
tags: [mfaTag],
|
|
444
|
+
summary: 'Verify MFA code',
|
|
445
|
+
description: 'Verify MFA code during login or sensitive operations',
|
|
446
|
+
body: {
|
|
447
|
+
type: 'object',
|
|
448
|
+
required: ['userId', 'code'],
|
|
449
|
+
properties: {
|
|
450
|
+
userId: { type: 'string' },
|
|
451
|
+
code: { type: 'string' },
|
|
452
|
+
method: { type: 'string', enum: ['totp', 'sms', 'email', 'backup_codes'] },
|
|
453
|
+
challengeId: { type: 'string' },
|
|
454
|
+
},
|
|
455
|
+
},
|
|
456
|
+
response: {
|
|
457
|
+
200: {
|
|
458
|
+
type: 'object',
|
|
459
|
+
properties: {
|
|
460
|
+
success: { type: 'boolean' },
|
|
461
|
+
data: {
|
|
462
|
+
type: 'object',
|
|
463
|
+
properties: {
|
|
464
|
+
verified: { type: 'boolean' },
|
|
465
|
+
method: { type: 'string' },
|
|
466
|
+
remainingAttempts: { type: 'number' },
|
|
467
|
+
lockedUntil: { type: 'string', format: 'date-time' },
|
|
468
|
+
},
|
|
469
|
+
},
|
|
470
|
+
},
|
|
471
|
+
},
|
|
472
|
+
400: commonResponses.error,
|
|
473
|
+
},
|
|
474
|
+
},
|
|
475
|
+
},
|
|
476
|
+
async (request: FastifyRequest, reply: FastifyReply) => {
|
|
477
|
+
const { userId, code, method, challengeId } = request.body as {
|
|
478
|
+
userId: string;
|
|
479
|
+
code: string;
|
|
480
|
+
method?: MFAMethod;
|
|
481
|
+
challengeId?: string;
|
|
482
|
+
};
|
|
483
|
+
const result = await mfaService.verifyChallenge(userId, code, method, challengeId);
|
|
484
|
+
|
|
485
|
+
return reply.send({
|
|
486
|
+
success: true,
|
|
487
|
+
data: {
|
|
488
|
+
verified: result.success,
|
|
489
|
+
method: result.method,
|
|
490
|
+
remainingAttempts: result.remainingAttempts,
|
|
491
|
+
lockedUntil: result.lockedUntil?.toISOString(),
|
|
492
|
+
},
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
);
|
|
496
|
+
|
|
497
|
+
// Request MFA challenge (for SMS/email)
|
|
498
|
+
app.post(
|
|
499
|
+
'/auth/mfa/challenge',
|
|
500
|
+
{
|
|
501
|
+
schema: {
|
|
502
|
+
tags: [mfaTag],
|
|
503
|
+
summary: 'Request MFA challenge',
|
|
504
|
+
description: 'Request a new SMS or email verification code',
|
|
505
|
+
body: {
|
|
506
|
+
type: 'object',
|
|
507
|
+
required: ['userId', 'method'],
|
|
508
|
+
properties: {
|
|
509
|
+
userId: { type: 'string' },
|
|
510
|
+
method: { type: 'string', enum: ['sms', 'email'] },
|
|
511
|
+
},
|
|
512
|
+
},
|
|
513
|
+
response: {
|
|
514
|
+
200: {
|
|
515
|
+
type: 'object',
|
|
516
|
+
properties: {
|
|
517
|
+
success: { type: 'boolean' },
|
|
518
|
+
data: {
|
|
519
|
+
type: 'object',
|
|
520
|
+
properties: {
|
|
521
|
+
challengeId: { type: 'string' },
|
|
522
|
+
expiresAt: { type: 'string', format: 'date-time' },
|
|
523
|
+
},
|
|
524
|
+
},
|
|
525
|
+
},
|
|
526
|
+
},
|
|
527
|
+
400: commonResponses.error,
|
|
528
|
+
},
|
|
529
|
+
},
|
|
530
|
+
},
|
|
531
|
+
async (request: FastifyRequest, reply: FastifyReply) => {
|
|
532
|
+
const { userId, method } = request.body as { userId: string; method: MFAMethod };
|
|
533
|
+
const challenge = await mfaService.createChallenge(userId, method);
|
|
534
|
+
|
|
535
|
+
return reply.send({
|
|
536
|
+
success: true,
|
|
537
|
+
data: {
|
|
538
|
+
challengeId: challenge.id,
|
|
539
|
+
expiresAt: challenge.expiresAt.toISOString(),
|
|
540
|
+
},
|
|
541
|
+
});
|
|
542
|
+
}
|
|
543
|
+
);
|
|
544
|
+
|
|
545
|
+
// Disable all MFA
|
|
546
|
+
app.delete(
|
|
547
|
+
'/auth/mfa',
|
|
548
|
+
{
|
|
549
|
+
preHandler: [authenticate],
|
|
550
|
+
schema: {
|
|
551
|
+
tags: [mfaTag],
|
|
552
|
+
summary: 'Disable all MFA methods',
|
|
553
|
+
security: [{ bearerAuth: [] }],
|
|
554
|
+
body: {
|
|
555
|
+
type: 'object',
|
|
556
|
+
required: ['password'],
|
|
557
|
+
properties: {
|
|
558
|
+
password: { type: 'string' },
|
|
559
|
+
},
|
|
560
|
+
},
|
|
561
|
+
response: {
|
|
562
|
+
200: {
|
|
563
|
+
type: 'object',
|
|
564
|
+
properties: {
|
|
565
|
+
success: { type: 'boolean' },
|
|
566
|
+
message: { type: 'string' },
|
|
567
|
+
},
|
|
568
|
+
},
|
|
569
|
+
400: commonResponses.error,
|
|
570
|
+
401: commonResponses.unauthorized,
|
|
571
|
+
},
|
|
572
|
+
},
|
|
573
|
+
},
|
|
574
|
+
async (request: FastifyRequest, reply: FastifyReply) => {
|
|
575
|
+
const user = (request as FastifyRequest & { user: { id: string } }).user;
|
|
576
|
+
const { password } = request.body as { password: string };
|
|
577
|
+
|
|
578
|
+
// Verify password before disabling MFA
|
|
579
|
+
const isValid = await authService.verifyPasswordById(user.id, password);
|
|
580
|
+
if (!isValid) {
|
|
581
|
+
return reply.status(400).send({
|
|
582
|
+
success: false,
|
|
583
|
+
message: 'Invalid password',
|
|
584
|
+
});
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
await mfaService.disableAllMFA(user.id);
|
|
588
|
+
|
|
589
|
+
return reply.send({
|
|
590
|
+
success: true,
|
|
591
|
+
message: 'All MFA methods disabled',
|
|
592
|
+
});
|
|
593
|
+
}
|
|
594
|
+
);
|
|
595
|
+
}
|