@zero-server/sdk 0.9.1 → 0.9.3

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.
Files changed (128) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +460 -443
  3. package/index.js +414 -412
  4. package/lib/app.js +1172 -1172
  5. package/lib/auth/authorize.js +399 -399
  6. package/lib/auth/enrollment.js +367 -367
  7. package/lib/auth/index.js +57 -57
  8. package/lib/auth/jwt.js +731 -731
  9. package/lib/auth/oauth.js +362 -362
  10. package/lib/auth/session.js +588 -588
  11. package/lib/auth/trustedDevice.js +409 -409
  12. package/lib/auth/twoFactor.js +1150 -1150
  13. package/lib/auth/webauthn.js +946 -946
  14. package/lib/body/index.js +14 -14
  15. package/lib/body/json.js +109 -109
  16. package/lib/body/multipart.js +440 -440
  17. package/lib/body/raw.js +71 -71
  18. package/lib/body/rawBuffer.js +160 -160
  19. package/lib/body/sendError.js +25 -25
  20. package/lib/body/text.js +75 -75
  21. package/lib/body/typeMatch.js +41 -41
  22. package/lib/body/urlencoded.js +235 -235
  23. package/lib/cli.js +845 -845
  24. package/lib/cluster.js +666 -666
  25. package/lib/debug.js +372 -372
  26. package/lib/env/index.js +465 -465
  27. package/lib/errors.js +683 -683
  28. package/lib/fetch/index.js +256 -256
  29. package/lib/grpc/balancer.js +378 -378
  30. package/lib/grpc/call.js +708 -708
  31. package/lib/grpc/client.js +764 -764
  32. package/lib/grpc/codec.js +1221 -1221
  33. package/lib/grpc/credentials.js +398 -398
  34. package/lib/grpc/frame.js +262 -262
  35. package/lib/grpc/health.js +287 -287
  36. package/lib/grpc/index.js +121 -121
  37. package/lib/grpc/metadata.js +461 -461
  38. package/lib/grpc/proto.js +821 -821
  39. package/lib/grpc/reflection.js +590 -590
  40. package/lib/grpc/server.js +445 -445
  41. package/lib/grpc/status.js +118 -118
  42. package/lib/grpc/watch.js +173 -173
  43. package/lib/http/index.js +10 -10
  44. package/lib/http/request.js +727 -727
  45. package/lib/http/response.js +799 -799
  46. package/lib/lifecycle.js +557 -557
  47. package/lib/middleware/compress.js +230 -230
  48. package/lib/middleware/cookieParser.js +237 -237
  49. package/lib/middleware/cors.js +93 -93
  50. package/lib/middleware/csrf.js +137 -137
  51. package/lib/middleware/errorHandler.js +101 -101
  52. package/lib/middleware/helmet.js +175 -175
  53. package/lib/middleware/index.js +19 -17
  54. package/lib/middleware/logger.js +74 -74
  55. package/lib/middleware/rateLimit.js +88 -88
  56. package/lib/middleware/requestId.js +53 -53
  57. package/lib/middleware/static.js +326 -326
  58. package/lib/middleware/timeout.js +71 -71
  59. package/lib/middleware/validator.js +255 -255
  60. package/lib/observe/health.js +326 -326
  61. package/lib/observe/index.js +50 -50
  62. package/lib/observe/logger.js +359 -359
  63. package/lib/observe/metrics.js +805 -805
  64. package/lib/observe/tracing.js +592 -592
  65. package/lib/orm/adapters/json.js +290 -290
  66. package/lib/orm/adapters/memory.js +764 -764
  67. package/lib/orm/adapters/mongo.js +764 -764
  68. package/lib/orm/adapters/mysql.js +933 -933
  69. package/lib/orm/adapters/postgres.js +1144 -1144
  70. package/lib/orm/adapters/redis.js +1534 -1534
  71. package/lib/orm/adapters/sql-base.js +212 -212
  72. package/lib/orm/adapters/sqlite.js +858 -858
  73. package/lib/orm/audit.js +649 -649
  74. package/lib/orm/cache.js +394 -394
  75. package/lib/orm/geo.js +387 -387
  76. package/lib/orm/index.js +784 -784
  77. package/lib/orm/migrate.js +432 -432
  78. package/lib/orm/model.js +1706 -1706
  79. package/lib/orm/plugin.js +375 -375
  80. package/lib/orm/procedures.js +836 -836
  81. package/lib/orm/profiler.js +233 -233
  82. package/lib/orm/query.js +1772 -1772
  83. package/lib/orm/replicas.js +241 -241
  84. package/lib/orm/schema.js +307 -307
  85. package/lib/orm/search.js +380 -380
  86. package/lib/orm/seed/data/commerce.js +136 -136
  87. package/lib/orm/seed/data/internet.js +111 -111
  88. package/lib/orm/seed/data/locations.js +204 -204
  89. package/lib/orm/seed/data/names.js +338 -338
  90. package/lib/orm/seed/data/person.js +128 -128
  91. package/lib/orm/seed/data/phone.js +211 -211
  92. package/lib/orm/seed/data/words.js +134 -134
  93. package/lib/orm/seed/factory.js +178 -178
  94. package/lib/orm/seed/fake.js +1186 -1186
  95. package/lib/orm/seed/index.js +18 -18
  96. package/lib/orm/seed/rng.js +70 -70
  97. package/lib/orm/seed/seeder.js +124 -124
  98. package/lib/orm/seed/unique.js +68 -68
  99. package/lib/orm/snapshot.js +366 -366
  100. package/lib/orm/tenancy.js +605 -605
  101. package/lib/orm/views.js +350 -350
  102. package/lib/router/index.js +436 -436
  103. package/lib/sse/index.js +8 -8
  104. package/lib/sse/stream.js +349 -349
  105. package/lib/ws/connection.js +451 -451
  106. package/lib/ws/handshake.js +125 -125
  107. package/lib/ws/index.js +14 -14
  108. package/lib/ws/room.js +223 -223
  109. package/package.json +73 -73
  110. package/types/app.d.ts +223 -223
  111. package/types/auth.d.ts +520 -520
  112. package/types/body.d.ts +14 -0
  113. package/types/cli.d.ts +2 -0
  114. package/types/cluster.d.ts +75 -75
  115. package/types/env.d.ts +80 -80
  116. package/types/errors.d.ts +316 -316
  117. package/types/fetch.d.ts +43 -43
  118. package/types/grpc.d.ts +432 -432
  119. package/types/index.d.ts +384 -384
  120. package/types/lifecycle.d.ts +60 -60
  121. package/types/middleware.d.ts +320 -320
  122. package/types/observe.d.ts +304 -304
  123. package/types/orm.d.ts +1887 -1887
  124. package/types/request.d.ts +109 -109
  125. package/types/response.d.ts +157 -157
  126. package/types/router.d.ts +78 -78
  127. package/types/sse.d.ts +78 -78
  128. package/types/websocket.d.ts +126 -126
@@ -1,367 +1,367 @@
1
- /**
2
- * @module auth/enrollment
3
- * @description 2FA Enrollment Flow Helper.
4
- * Provides a session-scoped, multi-step enrollment workflow
5
- * for TOTP-based two-factor authentication.
6
- *
7
- * Steps:
8
- * 1. `start()` — Generate secret + backup codes, store in session
9
- * 2. `verify()` — Confirm user can produce a valid TOTP code
10
- * 3. `complete()` — Persist the verified secret to the database
11
- * 4. `disable()` — Remove 2FA from the account
12
- *
13
- * @example | Full enrollment flow
14
- * const { enrollment } = require('@zero-server/sdk');
15
- * const flow = enrollment({
16
- * saveSecret: async (req, secret, backupHashes) => {
17
- * await db.users.update(req.user.id, { totpSecret: secret, backupHashes });
18
- * },
19
- * removeSecret: async (req) => {
20
- * await db.users.update(req.user.id, { totpSecret: null, backupHashes: [] });
21
- * },
22
- * });
23
- *
24
- * app.post('/2fa/start', json(), flow.start());
25
- * app.post('/2fa/verify', json(), flow.verify());
26
- * app.post('/2fa/complete', json(), flow.complete());
27
- * app.post('/2fa/disable', json(), flow.disable());
28
- */
29
-
30
- const log = require('../debug')('zero:enrollment');
31
-
32
- // Lazy-load twoFactor to avoid circular deps at module level
33
- let _twoFactor = null;
34
- function _getTwoFactor()
35
- {
36
- if (!_twoFactor) _twoFactor = require('./twoFactor');
37
- return _twoFactor;
38
- }
39
-
40
- // -- Constants ---------------------------------------------------
41
-
42
- const DEFAULT_SESSION_KEY = '_2faEnrollment';
43
- const DEFAULT_ENROLLMENT_TTL = 10 * 60 * 1000; // 10 minutes
44
-
45
- // -- Enrollment Factory ------------------------------------------
46
-
47
- /**
48
- * Create a 2FA enrollment flow bound to your persistence callbacks.
49
- *
50
- * @param {object} opts - Options.
51
- * @param {Function} opts.saveSecret - `(req, base32Secret, backupHashes) => Promise<void>`.
52
- * Persist the verified TOTP secret and backup hashes.
53
- * @param {Function} opts.removeSecret - `(req) => Promise<void>`.
54
- * Remove TOTP secret on disable.
55
- * @param {string} [opts.issuer='App'] - Issuer name for the otpauth URI.
56
- * @param {Function} [opts.getAccount] - `(req) => string`. User label for QR code.
57
- * Defaults to `req.user.email || req.user.id`.
58
- * @param {string} [opts.sessionKey='_2faEnrollment'] - Session key for pending enrollment.
59
- * @param {number} [opts.ttl=600000] - Enrollment session TTL in ms (default 10 min).
60
- * @param {number} [opts.backupCount=10] - Number of backup codes to generate.
61
- * @param {number} [opts.window=1] - TOTP verification window.
62
- * @param {number} [opts.period=30] - TOTP period in seconds.
63
- * @param {string} [opts.algorithm='sha1'] - HMAC algorithm.
64
- * @param {number} [opts.digits=6] - Code length.
65
- * @param {Function} [opts.isEnabled] - `(req) => boolean|Promise<boolean>`.
66
- * Check if 2FA is already enabled (for guarding start/disable).
67
- * @returns {{ start: Function, verify: Function, complete: Function, disable: Function }}
68
- */
69
- function enrollment(opts = {})
70
- {
71
- if (typeof opts.saveSecret !== 'function')
72
- throw new Error('enrollment() requires a saveSecret(req, secret, backupHashes) function');
73
- if (typeof opts.removeSecret !== 'function')
74
- throw new Error('enrollment() requires a removeSecret(req) function');
75
-
76
- const issuer = opts.issuer || 'App';
77
- const getAccount = opts.getAccount || _defaultGetAccount;
78
- const sessionKey = opts.sessionKey || DEFAULT_SESSION_KEY;
79
- const ttl = opts.ttl || DEFAULT_ENROLLMENT_TTL;
80
- const backupCount = opts.backupCount || 10;
81
- const window = opts.window != null ? opts.window : 1;
82
- const period = opts.period || 30;
83
- const algorithm = opts.algorithm || 'sha1';
84
- const digits = opts.digits || 6;
85
- const isEnabled = opts.isEnabled || null;
86
-
87
- function _sendJson(res, status, body)
88
- {
89
- const raw = res.raw || res;
90
- if (raw.headersSent) return;
91
- raw.statusCode = status;
92
- raw.setHeader('Content-Type', 'application/json');
93
- raw.end(JSON.stringify(body));
94
- }
95
-
96
- function _requireSession(req, res)
97
- {
98
- if (!req.session || typeof req.session.set !== 'function')
99
- {
100
- _sendJson(res, 500, { error: 'Session middleware required for 2FA enrollment' });
101
- return false;
102
- }
103
- return true;
104
- }
105
-
106
- // ---- start() ----
107
-
108
- /**
109
- * Start the enrollment process.
110
- * Generates a TOTP secret and backup codes, stores them in the session,
111
- * and returns the otpauth URI (for QR code) plus backup codes to the client.
112
- *
113
- * @returns {Function} Middleware `(req, res) => void`.
114
- */
115
- function start()
116
- {
117
- return async function _enrollmentStart(req, res)
118
- {
119
- if (!_requireSession(req, res)) return;
120
-
121
- // Guard: if 2FA is already enabled
122
- if (typeof isEnabled === 'function')
123
- {
124
- try
125
- {
126
- const enabled = await isEnabled(req);
127
- if (enabled)
128
- {
129
- _sendJson(res, 409, { error: '2FA is already enabled. Disable it first.' });
130
- return;
131
- }
132
- }
133
- catch (err)
134
- {
135
- log.error('isEnabled check error: %s', err.message);
136
- _sendJson(res, 500, { error: 'Internal server error' });
137
- return;
138
- }
139
- }
140
-
141
- const tf = _getTwoFactor();
142
-
143
- const secret = tf.generateSecret();
144
- let account;
145
- try
146
- {
147
- account = await getAccount(req);
148
- }
149
- catch (err)
150
- {
151
- log.error('getAccount error: %s', err.message);
152
- _sendJson(res, 500, { error: 'Internal server error' });
153
- return;
154
- }
155
-
156
- const uri = tf.otpauthURI({ secret: secret.base32, issuer, account });
157
- const { codes, hashes } = tf.generateBackupCodes(backupCount);
158
-
159
- // Store pending enrollment in session
160
- req.session.set(sessionKey, {
161
- secret: secret.base32,
162
- backupHashes: hashes,
163
- createdAt: Date.now(),
164
- });
165
-
166
- log.info('enrollment started for %s', account);
167
-
168
- _sendJson(res, 200, {
169
- secret: secret.base32,
170
- uri,
171
- backupCodes: codes,
172
- expiresIn: Math.floor(ttl / 1000),
173
- });
174
- };
175
- }
176
-
177
- // ---- verify() ----
178
-
179
- /**
180
- * Verify that the user can produce a valid TOTP code with the pending secret.
181
- * This confirms their authenticator app is configured correctly.
182
- *
183
- * @param {object} [verifyOpts] - Options.
184
- * @param {string} [verifyOpts.codeField='code'] - Body field for the TOTP code.
185
- * @returns {Function} Middleware `(req, res) => void`.
186
- */
187
- function verify(verifyOpts = {})
188
- {
189
- const codeField = verifyOpts.codeField || 'code';
190
-
191
- return async function _enrollmentVerify(req, res)
192
- {
193
- if (!_requireSession(req, res)) return;
194
-
195
- const pending = req.session.get(sessionKey);
196
- if (!pending || !pending.secret)
197
- {
198
- _sendJson(res, 400, { error: 'No pending enrollment. Call start() first.' });
199
- return;
200
- }
201
-
202
- // Check TTL
203
- if (Date.now() - pending.createdAt > ttl)
204
- {
205
- req.session.set(sessionKey, null);
206
- _sendJson(res, 410, { error: 'Enrollment expired. Please start again.' });
207
- return;
208
- }
209
-
210
- const code = req.body?.[codeField];
211
- if (!code || typeof code !== 'string')
212
- {
213
- _sendJson(res, 400, { error: `Missing ${codeField} field` });
214
- return;
215
- }
216
-
217
- const tf = _getTwoFactor();
218
- const result = tf.verifyTOTP(code, pending.secret, { window, period, algorithm, digits });
219
-
220
- if (!result.valid)
221
- {
222
- _sendJson(res, 401, { error: 'Invalid code. Check your authenticator app and try again.' });
223
- return;
224
- }
225
-
226
- // Mark as verified in session
227
- pending.verified = true;
228
- req.session.set(sessionKey, pending);
229
-
230
- log.info('enrollment code verified (delta=%d)', result.delta);
231
-
232
- _sendJson(res, 200, { verified: true });
233
- };
234
- }
235
-
236
- // ---- complete() ----
237
-
238
- /**
239
- * Complete the enrollment by persisting the verified secret.
240
- * Only succeeds if `verify()` was called first.
241
- *
242
- * @returns {Function} Middleware `(req, res) => void`.
243
- */
244
- function complete()
245
- {
246
- return async function _enrollmentComplete(req, res)
247
- {
248
- if (!_requireSession(req, res)) return;
249
-
250
- const pending = req.session.get(sessionKey);
251
- if (!pending || !pending.secret)
252
- {
253
- _sendJson(res, 400, { error: 'No pending enrollment.' });
254
- return;
255
- }
256
-
257
- if (!pending.verified)
258
- {
259
- _sendJson(res, 400, { error: 'Enrollment not yet verified. Call verify() first.' });
260
- return;
261
- }
262
-
263
- // Check TTL
264
- if (Date.now() - pending.createdAt > ttl)
265
- {
266
- req.session.set(sessionKey, null);
267
- _sendJson(res, 410, { error: 'Enrollment expired. Please start again.' });
268
- return;
269
- }
270
-
271
- try
272
- {
273
- await opts.saveSecret(req, pending.secret, pending.backupHashes);
274
- }
275
- catch (err)
276
- {
277
- log.error('saveSecret error: %s', err.message);
278
- _sendJson(res, 500, { error: 'Failed to save 2FA configuration' });
279
- return;
280
- }
281
-
282
- // Clear pending enrollment
283
- req.session.set(sessionKey, null);
284
-
285
- // Mark 2FA as verified in session so require2FA() passes
286
- req.session.set('twoFactorVerified', true);
287
-
288
- log.info('enrollment completed successfully');
289
-
290
- _sendJson(res, 200, { enabled: true });
291
- };
292
- }
293
-
294
- // ---- disable() ----
295
-
296
- /**
297
- * Disable 2FA for the user.
298
- * Optionally requires current TOTP code or password confirmation.
299
- *
300
- * @param {object} [disableOpts] - Options.
301
- * @param {Function} [disableOpts.confirm] - `(req) => boolean|Promise<boolean>`.
302
- * If provided, must return `true` to allow disable (e.g. validate password).
303
- * @returns {Function} Middleware `(req, res) => void`.
304
- */
305
- function disable(disableOpts = {})
306
- {
307
- return async function _enrollmentDisable(req, res)
308
- {
309
- if (!_requireSession(req, res)) return;
310
-
311
- // Confirmation check
312
- if (typeof disableOpts.confirm === 'function')
313
- {
314
- try
315
- {
316
- const ok = await disableOpts.confirm(req);
317
- if (!ok)
318
- {
319
- _sendJson(res, 403, { error: 'Confirmation failed' });
320
- return;
321
- }
322
- }
323
- catch (err)
324
- {
325
- log.error('disable confirm error: %s', err.message);
326
- _sendJson(res, 500, { error: 'Internal server error' });
327
- return;
328
- }
329
- }
330
-
331
- try
332
- {
333
- await opts.removeSecret(req);
334
- }
335
- catch (err)
336
- {
337
- log.error('removeSecret error: %s', err.message);
338
- _sendJson(res, 500, { error: 'Failed to remove 2FA configuration' });
339
- return;
340
- }
341
-
342
- // Clear enrollment and 2FA session state
343
- req.session.set(sessionKey, null);
344
- req.session.set('twoFactorVerified', false);
345
-
346
- log.info('2FA disabled');
347
-
348
- _sendJson(res, 200, { disabled: true });
349
- };
350
- }
351
-
352
- return { start, verify, complete, disable };
353
- }
354
-
355
- // -- Helpers -------------------------------------------------
356
-
357
- function _defaultGetAccount(req)
358
- {
359
- if (!req.user) throw new Error('No user on request — authentication middleware required');
360
- return req.user.email || req.user.id || req.user.sub;
361
- }
362
-
363
- // -- Exports -------------------------------------------------
364
-
365
- module.exports = {
366
- enrollment,
367
- };
1
+ /**
2
+ * @module auth/enrollment
3
+ * @description 2FA Enrollment Flow Helper.
4
+ * Provides a session-scoped, multi-step enrollment workflow
5
+ * for TOTP-based two-factor authentication.
6
+ *
7
+ * Steps:
8
+ * 1. `start()` — Generate secret + backup codes, store in session
9
+ * 2. `verify()` — Confirm user can produce a valid TOTP code
10
+ * 3. `complete()` — Persist the verified secret to the database
11
+ * 4. `disable()` — Remove 2FA from the account
12
+ *
13
+ * @example | Full enrollment flow
14
+ * const { enrollment } = require('@zero-server/sdk');
15
+ * const flow = enrollment({
16
+ * saveSecret: async (req, secret, backupHashes) => {
17
+ * await db.users.update(req.user.id, { totpSecret: secret, backupHashes });
18
+ * },
19
+ * removeSecret: async (req) => {
20
+ * await db.users.update(req.user.id, { totpSecret: null, backupHashes: [] });
21
+ * },
22
+ * });
23
+ *
24
+ * app.post('/2fa/start', json(), flow.start());
25
+ * app.post('/2fa/verify', json(), flow.verify());
26
+ * app.post('/2fa/complete', json(), flow.complete());
27
+ * app.post('/2fa/disable', json(), flow.disable());
28
+ */
29
+
30
+ const log = require('../debug')('zero:enrollment');
31
+
32
+ // Lazy-load twoFactor to avoid circular deps at module level
33
+ let _twoFactor = null;
34
+ function _getTwoFactor()
35
+ {
36
+ if (!_twoFactor) _twoFactor = require('./twoFactor');
37
+ return _twoFactor;
38
+ }
39
+
40
+ // -- Constants ---------------------------------------------------
41
+
42
+ const DEFAULT_SESSION_KEY = '_2faEnrollment';
43
+ const DEFAULT_ENROLLMENT_TTL = 10 * 60 * 1000; // 10 minutes
44
+
45
+ // -- Enrollment Factory ------------------------------------------
46
+
47
+ /**
48
+ * Create a 2FA enrollment flow bound to your persistence callbacks.
49
+ *
50
+ * @param {object} opts - Options.
51
+ * @param {Function} opts.saveSecret - `(req, base32Secret, backupHashes) => Promise<void>`.
52
+ * Persist the verified TOTP secret and backup hashes.
53
+ * @param {Function} opts.removeSecret - `(req) => Promise<void>`.
54
+ * Remove TOTP secret on disable.
55
+ * @param {string} [opts.issuer='App'] - Issuer name for the otpauth URI.
56
+ * @param {Function} [opts.getAccount] - `(req) => string`. User label for QR code.
57
+ * Defaults to `req.user.email || req.user.id`.
58
+ * @param {string} [opts.sessionKey='_2faEnrollment'] - Session key for pending enrollment.
59
+ * @param {number} [opts.ttl=600000] - Enrollment session TTL in ms (default 10 min).
60
+ * @param {number} [opts.backupCount=10] - Number of backup codes to generate.
61
+ * @param {number} [opts.window=1] - TOTP verification window.
62
+ * @param {number} [opts.period=30] - TOTP period in seconds.
63
+ * @param {string} [opts.algorithm='sha1'] - HMAC algorithm.
64
+ * @param {number} [opts.digits=6] - Code length.
65
+ * @param {Function} [opts.isEnabled] - `(req) => boolean|Promise<boolean>`.
66
+ * Check if 2FA is already enabled (for guarding start/disable).
67
+ * @returns {{ start: Function, verify: Function, complete: Function, disable: Function }}
68
+ */
69
+ function enrollment(opts = {})
70
+ {
71
+ if (typeof opts.saveSecret !== 'function')
72
+ throw new Error('enrollment() requires a saveSecret(req, secret, backupHashes) function');
73
+ if (typeof opts.removeSecret !== 'function')
74
+ throw new Error('enrollment() requires a removeSecret(req) function');
75
+
76
+ const issuer = opts.issuer || 'App';
77
+ const getAccount = opts.getAccount || _defaultGetAccount;
78
+ const sessionKey = opts.sessionKey || DEFAULT_SESSION_KEY;
79
+ const ttl = opts.ttl || DEFAULT_ENROLLMENT_TTL;
80
+ const backupCount = opts.backupCount || 10;
81
+ const window = opts.window != null ? opts.window : 1;
82
+ const period = opts.period || 30;
83
+ const algorithm = opts.algorithm || 'sha1';
84
+ const digits = opts.digits || 6;
85
+ const isEnabled = opts.isEnabled || null;
86
+
87
+ function _sendJson(res, status, body)
88
+ {
89
+ const raw = res.raw || res;
90
+ if (raw.headersSent) return;
91
+ raw.statusCode = status;
92
+ raw.setHeader('Content-Type', 'application/json');
93
+ raw.end(JSON.stringify(body));
94
+ }
95
+
96
+ function _requireSession(req, res)
97
+ {
98
+ if (!req.session || typeof req.session.set !== 'function')
99
+ {
100
+ _sendJson(res, 500, { error: 'Session middleware required for 2FA enrollment' });
101
+ return false;
102
+ }
103
+ return true;
104
+ }
105
+
106
+ // ---- start() ----
107
+
108
+ /**
109
+ * Start the enrollment process.
110
+ * Generates a TOTP secret and backup codes, stores them in the session,
111
+ * and returns the otpauth URI (for QR code) plus backup codes to the client.
112
+ *
113
+ * @returns {Function} Middleware `(req, res) => void`.
114
+ */
115
+ function start()
116
+ {
117
+ return async function _enrollmentStart(req, res)
118
+ {
119
+ if (!_requireSession(req, res)) return;
120
+
121
+ // Guard: if 2FA is already enabled
122
+ if (typeof isEnabled === 'function')
123
+ {
124
+ try
125
+ {
126
+ const enabled = await isEnabled(req);
127
+ if (enabled)
128
+ {
129
+ _sendJson(res, 409, { error: '2FA is already enabled. Disable it first.' });
130
+ return;
131
+ }
132
+ }
133
+ catch (err)
134
+ {
135
+ log.error('isEnabled check error: %s', err.message);
136
+ _sendJson(res, 500, { error: 'Internal server error' });
137
+ return;
138
+ }
139
+ }
140
+
141
+ const tf = _getTwoFactor();
142
+
143
+ const secret = tf.generateSecret();
144
+ let account;
145
+ try
146
+ {
147
+ account = await getAccount(req);
148
+ }
149
+ catch (err)
150
+ {
151
+ log.error('getAccount error: %s', err.message);
152
+ _sendJson(res, 500, { error: 'Internal server error' });
153
+ return;
154
+ }
155
+
156
+ const uri = tf.otpauthURI({ secret: secret.base32, issuer, account });
157
+ const { codes, hashes } = tf.generateBackupCodes(backupCount);
158
+
159
+ // Store pending enrollment in session
160
+ req.session.set(sessionKey, {
161
+ secret: secret.base32,
162
+ backupHashes: hashes,
163
+ createdAt: Date.now(),
164
+ });
165
+
166
+ log.info('enrollment started for %s', account);
167
+
168
+ _sendJson(res, 200, {
169
+ secret: secret.base32,
170
+ uri,
171
+ backupCodes: codes,
172
+ expiresIn: Math.floor(ttl / 1000),
173
+ });
174
+ };
175
+ }
176
+
177
+ // ---- verify() ----
178
+
179
+ /**
180
+ * Verify that the user can produce a valid TOTP code with the pending secret.
181
+ * This confirms their authenticator app is configured correctly.
182
+ *
183
+ * @param {object} [verifyOpts] - Options.
184
+ * @param {string} [verifyOpts.codeField='code'] - Body field for the TOTP code.
185
+ * @returns {Function} Middleware `(req, res) => void`.
186
+ */
187
+ function verify(verifyOpts = {})
188
+ {
189
+ const codeField = verifyOpts.codeField || 'code';
190
+
191
+ return async function _enrollmentVerify(req, res)
192
+ {
193
+ if (!_requireSession(req, res)) return;
194
+
195
+ const pending = req.session.get(sessionKey);
196
+ if (!pending || !pending.secret)
197
+ {
198
+ _sendJson(res, 400, { error: 'No pending enrollment. Call start() first.' });
199
+ return;
200
+ }
201
+
202
+ // Check TTL
203
+ if (Date.now() - pending.createdAt > ttl)
204
+ {
205
+ req.session.set(sessionKey, null);
206
+ _sendJson(res, 410, { error: 'Enrollment expired. Please start again.' });
207
+ return;
208
+ }
209
+
210
+ const code = req.body?.[codeField];
211
+ if (!code || typeof code !== 'string')
212
+ {
213
+ _sendJson(res, 400, { error: `Missing ${codeField} field` });
214
+ return;
215
+ }
216
+
217
+ const tf = _getTwoFactor();
218
+ const result = tf.verifyTOTP(code, pending.secret, { window, period, algorithm, digits });
219
+
220
+ if (!result.valid)
221
+ {
222
+ _sendJson(res, 401, { error: 'Invalid code. Check your authenticator app and try again.' });
223
+ return;
224
+ }
225
+
226
+ // Mark as verified in session
227
+ pending.verified = true;
228
+ req.session.set(sessionKey, pending);
229
+
230
+ log.info('enrollment code verified (delta=%d)', result.delta);
231
+
232
+ _sendJson(res, 200, { verified: true });
233
+ };
234
+ }
235
+
236
+ // ---- complete() ----
237
+
238
+ /**
239
+ * Complete the enrollment by persisting the verified secret.
240
+ * Only succeeds if `verify()` was called first.
241
+ *
242
+ * @returns {Function} Middleware `(req, res) => void`.
243
+ */
244
+ function complete()
245
+ {
246
+ return async function _enrollmentComplete(req, res)
247
+ {
248
+ if (!_requireSession(req, res)) return;
249
+
250
+ const pending = req.session.get(sessionKey);
251
+ if (!pending || !pending.secret)
252
+ {
253
+ _sendJson(res, 400, { error: 'No pending enrollment.' });
254
+ return;
255
+ }
256
+
257
+ if (!pending.verified)
258
+ {
259
+ _sendJson(res, 400, { error: 'Enrollment not yet verified. Call verify() first.' });
260
+ return;
261
+ }
262
+
263
+ // Check TTL
264
+ if (Date.now() - pending.createdAt > ttl)
265
+ {
266
+ req.session.set(sessionKey, null);
267
+ _sendJson(res, 410, { error: 'Enrollment expired. Please start again.' });
268
+ return;
269
+ }
270
+
271
+ try
272
+ {
273
+ await opts.saveSecret(req, pending.secret, pending.backupHashes);
274
+ }
275
+ catch (err)
276
+ {
277
+ log.error('saveSecret error: %s', err.message);
278
+ _sendJson(res, 500, { error: 'Failed to save 2FA configuration' });
279
+ return;
280
+ }
281
+
282
+ // Clear pending enrollment
283
+ req.session.set(sessionKey, null);
284
+
285
+ // Mark 2FA as verified in session so require2FA() passes
286
+ req.session.set('twoFactorVerified', true);
287
+
288
+ log.info('enrollment completed successfully');
289
+
290
+ _sendJson(res, 200, { enabled: true });
291
+ };
292
+ }
293
+
294
+ // ---- disable() ----
295
+
296
+ /**
297
+ * Disable 2FA for the user.
298
+ * Optionally requires current TOTP code or password confirmation.
299
+ *
300
+ * @param {object} [disableOpts] - Options.
301
+ * @param {Function} [disableOpts.confirm] - `(req) => boolean|Promise<boolean>`.
302
+ * If provided, must return `true` to allow disable (e.g. validate password).
303
+ * @returns {Function} Middleware `(req, res) => void`.
304
+ */
305
+ function disable(disableOpts = {})
306
+ {
307
+ return async function _enrollmentDisable(req, res)
308
+ {
309
+ if (!_requireSession(req, res)) return;
310
+
311
+ // Confirmation check
312
+ if (typeof disableOpts.confirm === 'function')
313
+ {
314
+ try
315
+ {
316
+ const ok = await disableOpts.confirm(req);
317
+ if (!ok)
318
+ {
319
+ _sendJson(res, 403, { error: 'Confirmation failed' });
320
+ return;
321
+ }
322
+ }
323
+ catch (err)
324
+ {
325
+ log.error('disable confirm error: %s', err.message);
326
+ _sendJson(res, 500, { error: 'Internal server error' });
327
+ return;
328
+ }
329
+ }
330
+
331
+ try
332
+ {
333
+ await opts.removeSecret(req);
334
+ }
335
+ catch (err)
336
+ {
337
+ log.error('removeSecret error: %s', err.message);
338
+ _sendJson(res, 500, { error: 'Failed to remove 2FA configuration' });
339
+ return;
340
+ }
341
+
342
+ // Clear enrollment and 2FA session state
343
+ req.session.set(sessionKey, null);
344
+ req.session.set('twoFactorVerified', false);
345
+
346
+ log.info('2FA disabled');
347
+
348
+ _sendJson(res, 200, { disabled: true });
349
+ };
350
+ }
351
+
352
+ return { start, verify, complete, disable };
353
+ }
354
+
355
+ // -- Helpers -------------------------------------------------
356
+
357
+ function _defaultGetAccount(req)
358
+ {
359
+ if (!req.user) throw new Error('No user on request — authentication middleware required');
360
+ return req.user.email || req.user.id || req.user.sub;
361
+ }
362
+
363
+ // -- Exports -------------------------------------------------
364
+
365
+ module.exports = {
366
+ enrollment,
367
+ };