@payez/next-mvp 4.1.14 → 4.1.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/dist/api-handlers/account/profile.d.ts +38 -0
- package/dist/api-handlers/account/profile.js +73 -45
- package/dist/api-handlers/account/recovery/send-code.d.ts +33 -0
- package/dist/api-handlers/account/recovery/send-code.js +52 -18
- package/dist/api-handlers/account/recovery/verify-code.d.ts +33 -0
- package/dist/api-handlers/account/recovery/verify-code.js +52 -18
- package/dist/api-handlers/account/verify-email.js +15 -1
- package/dist/api-handlers/account/verify-sms.js +15 -1
- package/dist/api-handlers/auth/passkey-signin.d.ts +29 -0
- package/dist/api-handlers/auth/passkey-signin.js +97 -0
- package/dist/api-handlers/auth/refresh.js +38 -15
- package/dist/api-handlers/mfa/backup-code-verify.d.ts +73 -0
- package/dist/api-handlers/mfa/backup-code-verify.js +153 -0
- package/dist/api-handlers/mfa/backup-codes.d.ts +7 -0
- package/dist/api-handlers/mfa/backup-codes.js +40 -0
- package/dist/api-handlers/mfa/methods.d.ts +3 -0
- package/dist/api-handlers/mfa/methods.js +41 -0
- package/dist/api-handlers/mfa/totp-confirm.d.ts +8 -0
- package/dist/api-handlers/mfa/totp-confirm.js +41 -0
- package/dist/api-handlers/mfa/totp-enroll.d.ts +8 -0
- package/dist/api-handlers/mfa/totp-enroll.js +41 -0
- package/dist/api-handlers/mfa/totp-verify.d.ts +47 -0
- package/dist/api-handlers/mfa/totp-verify.js +134 -0
- package/dist/api-handlers/passkeys/list.d.ts +7 -0
- package/dist/api-handlers/passkeys/list.js +29 -0
- package/dist/api-handlers/passkeys/manage.d.ts +3 -0
- package/dist/api-handlers/passkeys/manage.js +54 -0
- package/dist/api-handlers/passkeys/register-options.d.ts +8 -0
- package/dist/api-handlers/passkeys/register-options.js +41 -0
- package/dist/api-handlers/passkeys/register-verify.d.ts +7 -0
- package/dist/api-handlers/passkeys/register-verify.js +40 -0
- package/dist/api-handlers/session/viability.js +19 -0
- package/dist/auth/better-auth.d.ts +68 -2
- package/dist/auth/better-auth.js +137 -18
- package/dist/auth/utils/idp-client.d.ts +13 -22
- package/dist/auth/utils/idp-client.js +12 -82
- package/dist/client/better-auth-client.d.ts +62 -62
- package/dist/lib/ensure-fresh-access-token.js +49 -8
- package/dist/lib/session-store.d.ts +1 -0
- package/dist/lib/session-store.js +19 -8
- package/dist/lib/token-lifecycle.d.ts +2 -0
- package/dist/lib/token-lifecycle.js +61 -7
- package/dist/models/SessionModel.d.ts +17 -0
- package/dist/pages/login/page.d.ts +10 -1
- package/dist/pages/login/page.js +69 -80
- package/dist/pages/verify-code/page.d.ts +10 -1
- package/dist/pages/verify-code/page.js +72 -4
- package/dist/routes/auth/passkey-signin.d.ts +13 -0
- package/dist/routes/auth/passkey-signin.js +18 -0
- package/dist/routes/auth/viability.d.ts +2 -0
- package/dist/routes/auth/viability.js +88 -0
- package/dist/routes/mfa/backup-code-verify.d.ts +1 -0
- package/dist/routes/mfa/backup-code-verify.js +5 -0
- package/dist/routes/mfa/backup-codes.d.ts +10 -0
- package/dist/routes/mfa/backup-codes.js +14 -0
- package/dist/routes/mfa/methods.d.ts +21 -0
- package/dist/routes/mfa/methods.js +10 -0
- package/dist/routes/mfa/totp-confirm.d.ts +10 -0
- package/dist/routes/mfa/totp-confirm.js +14 -0
- package/dist/routes/mfa/totp-enroll.d.ts +10 -0
- package/dist/routes/mfa/totp-enroll.js +14 -0
- package/dist/routes/mfa/totp-verify.d.ts +14 -0
- package/dist/routes/mfa/totp-verify.js +18 -0
- package/dist/routes/passkeys/list.d.ts +10 -0
- package/dist/routes/passkeys/list.js +14 -0
- package/dist/routes/passkeys/manage.d.ts +18 -0
- package/dist/routes/passkeys/manage.js +13 -0
- package/dist/routes/passkeys/register-options.d.ts +10 -0
- package/dist/routes/passkeys/register-options.js +14 -0
- package/dist/routes/passkeys/register-verify.d.ts +10 -0
- package/dist/routes/passkeys/register-verify.js +14 -0
- package/dist/server/auth.d.ts +29 -20
- package/dist/server/auth.js +8 -0
- package/dist/server/decode-session.js +37 -3
- package/package.json +958 -894
- package/scripts/check-internal-url-usage.sh +73 -73
- package/src/api-handlers/account/profile.ts +76 -57
- package/src/api-handlers/account/recovery/send-code.ts +65 -17
- package/src/api-handlers/account/recovery/verify-code.ts +65 -17
- package/src/api-handlers/account/verify-email.ts +15 -1
- package/src/api-handlers/account/verify-sms.ts +15 -1
- package/src/api-handlers/auth/passkey-signin.ts +98 -0
- package/src/api-handlers/auth/refresh.ts +38 -15
- package/src/api-handlers/mfa/backup-code-verify.ts +175 -0
- package/src/api-handlers/mfa/backup-codes.ts +41 -0
- package/src/api-handlers/mfa/methods.ts +46 -0
- package/src/api-handlers/mfa/totp-confirm.ts +42 -0
- package/src/api-handlers/mfa/totp-enroll.ts +42 -0
- package/src/api-handlers/mfa/totp-verify.ts +156 -0
- package/src/api-handlers/passkeys/list.ts +31 -0
- package/src/api-handlers/passkeys/manage.ts +54 -0
- package/src/api-handlers/passkeys/register-options.ts +42 -0
- package/src/api-handlers/passkeys/register-verify.ts +41 -0
- package/src/api-handlers/session/viability.ts +19 -0
- package/src/auth/better-auth.ts +158 -18
- package/src/auth/utils/idp-client.ts +13 -95
- package/src/lib/ensure-fresh-access-token.ts +49 -8
- package/src/lib/session-store.ts +11 -1
- package/src/lib/token-lifecycle.ts +70 -7
- package/src/models/SessionModel.ts +18 -0
- package/src/pages/login/page.tsx +123 -228
- package/src/pages/verify-code/page.tsx +71 -5
- package/src/routes/auth/passkey-signin.ts +13 -0
- package/src/routes/auth/viability.ts +89 -0
- package/src/routes/mfa/backup-code-verify.ts +1 -0
- package/src/routes/mfa/backup-codes.ts +10 -0
- package/src/routes/mfa/methods.ts +22 -0
- package/src/routes/mfa/totp-confirm.ts +10 -0
- package/src/routes/mfa/totp-enroll.ts +10 -0
- package/src/routes/mfa/totp-verify.ts +14 -0
- package/src/routes/passkeys/list.ts +10 -0
- package/src/routes/passkeys/manage.ts +23 -0
- package/src/routes/passkeys/register-options.ts +10 -0
- package/src/routes/passkeys/register-verify.ts +10 -0
- package/src/server/auth.ts +17 -1
- package/src/server/decode-session.ts +36 -3
|
@@ -371,7 +371,7 @@ function createRefreshHandler(config) {
|
|
|
371
371
|
const errorMessage = idpError.message || 'Token refresh failed';
|
|
372
372
|
// CRITICAL: 401 means token is definitively invalid - always discard
|
|
373
373
|
// This prevents redirect loops where viability check keeps returning canRefresh:true
|
|
374
|
-
|
|
374
|
+
let discardToken = refreshResponse.status === 401 || idpError.discard_token === true;
|
|
375
375
|
const retryable = refreshResponse.status !== 401 && idpError.retryable === true;
|
|
376
376
|
const resolution = idpError.resolution || null;
|
|
377
377
|
// ============================================================================
|
|
@@ -408,20 +408,43 @@ function createRefreshHandler(config) {
|
|
|
408
408
|
console.error('[AUTH_REFRESH] Full IDP error response:', JSON.stringify(responseData, null, 2).substring(0, 1000));
|
|
409
409
|
// If IDP signals to discard token, clear it from Redis
|
|
410
410
|
if (discardToken) {
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
411
|
+
// Race-safe discard (CAS): only clear if the refresh token we sent is STILL
|
|
412
|
+
// the current one. A concurrent refresh may have already rotated it — our 401
|
|
413
|
+
// is then stale, and wiping here would silently log out a healthy session
|
|
414
|
+
// (the rotation-race wipe, QA 6873 site 2). Skip the wipe + drop terminal if changed.
|
|
415
|
+
const postRefresh = await (0, session_store_1.getSession)(sessionToken);
|
|
416
|
+
const tokenUnchanged = !!originalRefreshToken && postRefresh?.idpRefreshToken === originalRefreshToken;
|
|
417
|
+
if (tokenUnchanged) {
|
|
418
|
+
console.error('[AUTH_REFRESH] ⚠️ REFRESH_TOKEN_REMOVAL: IDP signaled discard_token', {
|
|
419
|
+
reason: 'IDP_DISCARD_TOKEN',
|
|
420
|
+
errorCode,
|
|
421
|
+
userId,
|
|
422
|
+
sessionToken: sessionToken.substring(0, 8) + '...',
|
|
423
|
+
hadRefreshToken: !!currentSession.idpRefreshToken,
|
|
424
|
+
stack: new Error().stack
|
|
425
|
+
});
|
|
426
|
+
await (0, session_store_1.updateSession)(sessionToken, {
|
|
427
|
+
idpRefreshToken: '',
|
|
428
|
+
idpRefreshTokenExpires: undefined,
|
|
429
|
+
refreshTokenClearedAt: Date.now(),
|
|
430
|
+
refreshTokenClearedReason: `IDP_DISCARD_TOKEN:${errorCode}`
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
else {
|
|
434
|
+
// Concurrent refresh already rotated the token — our 401 is stale. Return the
|
|
435
|
+
// freshly-rotated token directly so the loser succeeds without a retry
|
|
436
|
+
// (race-fix follow-up #1).
|
|
437
|
+
discardToken = false;
|
|
438
|
+
console.warn('[AUTH_REFRESH] Discard suppressed: refresh token already rotated by a concurrent refresh — returning the fresh token (race-safe).');
|
|
439
|
+
if (postRefresh?.idpAccessToken && postRefresh.idpAccessTokenExpires) {
|
|
440
|
+
return server_1.NextResponse.json({
|
|
441
|
+
refreshed: true,
|
|
442
|
+
reason: 'completed_by_concurrent_request',
|
|
443
|
+
accessTokenExpires: postRefresh.idpAccessTokenExpires,
|
|
444
|
+
hasRefreshToken: !!postRefresh.idpRefreshToken,
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
}
|
|
425
448
|
}
|
|
426
449
|
return server_1.NextResponse.json({
|
|
427
450
|
error: errorMessage,
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Verify Backup Code 2FA Challenge Handler
|
|
3
|
+
*
|
|
4
|
+
* Verifies a single-use recovery code AT LOGIN and completes the 2FA flow,
|
|
5
|
+
* transitioning the session to MFA-verified.
|
|
6
|
+
*
|
|
7
|
+
* WHY THIS EXISTS (2026-08-02)
|
|
8
|
+
* The package shipped backup-code GENERATION (backup-codes.ts, POST
|
|
9
|
+
* /api/Account/mfa/backup-codes) and no way to SPEND one. Codes could be
|
|
10
|
+
* generated, displayed once, written down — and then never used to satisfy a
|
|
11
|
+
* login challenge. Exactly the gap TOTP had before totp-verify: the enrolment
|
|
12
|
+
* half without the challenge half.
|
|
13
|
+
*
|
|
14
|
+
* THIS IS THE FACTOR OF LAST RESORT. A user reaches for a backup code when they
|
|
15
|
+
* have lost the authenticator, so it is the one path that must work when
|
|
16
|
+
* everything else has already failed. A recovery mechanism that cannot be
|
|
17
|
+
* redeemed is worse than none: it invites people to rely on it.
|
|
18
|
+
*
|
|
19
|
+
* FIELD NAME: the IDP expects { "code": ... }
|
|
20
|
+
* (MfaBackupCodeVerifyRequest, [JsonPropertyName("code")]). The dash in
|
|
21
|
+
* 'xxxx-xxxx' is optional per the IDP contract, so the code is forwarded as
|
|
22
|
+
* typed and normalisation is left to the IDP rather than guessed here.
|
|
23
|
+
*
|
|
24
|
+
* SINGLE USE, CONSUMED ATOMICALLY. The IDP hash-compares against unused codes
|
|
25
|
+
* and stamps used_at + used_by_ip in the same operation (MfaController 5g).
|
|
26
|
+
* NEVER retry this call on a network error or an ambiguous response — a retry
|
|
27
|
+
* can burn a second code, and the user has ten.
|
|
28
|
+
*
|
|
29
|
+
* DO NOT MAKE GENERATION TRANSITION THE SESSION, the same trap totp-confirm
|
|
30
|
+
* carries: generating recovery codes is not proof of possessing one.
|
|
31
|
+
*
|
|
32
|
+
* THE STORED 2FA METHOD IS 'totp', NOT 'backup_code'. READ THIS BEFORE
|
|
33
|
+
* "CORRECTING" IT — it looks wrong and is not.
|
|
34
|
+
*
|
|
35
|
+
* 1. IT MATCHES THE IDP. MfaController 5g sets
|
|
36
|
+
* `TwoFactorMethod = TwoFactorMethodType.Totp` for this exact path,
|
|
37
|
+
* commented "Backup code = recovery proof of the TOTP factor", and the
|
|
38
|
+
* reissued token's amr carries mfa+totp. A backup code is not a separate
|
|
39
|
+
* factor; it is recovery proof of the authenticator factor.
|
|
40
|
+
*
|
|
41
|
+
* 2. INVENTING A NEW VALUE SILENTLY BREAKS TOKEN REFRESH. refresh.ts:279
|
|
42
|
+
* resolves the method with ['sms','totp','email'].includes(m). Anything
|
|
43
|
+
* outside that set is dropped, so `two_factor_method` never reaches the
|
|
44
|
+
* IDP on refresh, and a session established with a recovery code refreshes
|
|
45
|
+
* differently from one established with the authenticator — for the same
|
|
46
|
+
* underlying factor. It fails quietly, days later, as "sessions
|
|
47
|
+
* occasionally drop", with nothing pointing back to this string.
|
|
48
|
+
*
|
|
49
|
+
* The type union (email|sms|totp) is not the reason for the choice, it is a
|
|
50
|
+
* coincidence that caught it. Widening the union would have compiled and
|
|
51
|
+
* shipped the refresh bug.
|
|
52
|
+
*
|
|
53
|
+
* @package @payez/next-mvp
|
|
54
|
+
*/
|
|
55
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
56
|
+
export declare function POST(req: NextRequest): Promise<NextResponse<{
|
|
57
|
+
success: boolean;
|
|
58
|
+
error: string;
|
|
59
|
+
code: string;
|
|
60
|
+
}> | NextResponse<{
|
|
61
|
+
success: boolean;
|
|
62
|
+
error: any;
|
|
63
|
+
code: any;
|
|
64
|
+
meta: {
|
|
65
|
+
attemptedRefresh: boolean;
|
|
66
|
+
};
|
|
67
|
+
}> | NextResponse<{
|
|
68
|
+
success: boolean;
|
|
69
|
+
verificationSuccessful: boolean;
|
|
70
|
+
twoFactorSessionVerified: boolean;
|
|
71
|
+
codeConsumed: boolean;
|
|
72
|
+
message: any;
|
|
73
|
+
}>>;
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Verify Backup Code 2FA Challenge Handler
|
|
4
|
+
*
|
|
5
|
+
* Verifies a single-use recovery code AT LOGIN and completes the 2FA flow,
|
|
6
|
+
* transitioning the session to MFA-verified.
|
|
7
|
+
*
|
|
8
|
+
* WHY THIS EXISTS (2026-08-02)
|
|
9
|
+
* The package shipped backup-code GENERATION (backup-codes.ts, POST
|
|
10
|
+
* /api/Account/mfa/backup-codes) and no way to SPEND one. Codes could be
|
|
11
|
+
* generated, displayed once, written down — and then never used to satisfy a
|
|
12
|
+
* login challenge. Exactly the gap TOTP had before totp-verify: the enrolment
|
|
13
|
+
* half without the challenge half.
|
|
14
|
+
*
|
|
15
|
+
* THIS IS THE FACTOR OF LAST RESORT. A user reaches for a backup code when they
|
|
16
|
+
* have lost the authenticator, so it is the one path that must work when
|
|
17
|
+
* everything else has already failed. A recovery mechanism that cannot be
|
|
18
|
+
* redeemed is worse than none: it invites people to rely on it.
|
|
19
|
+
*
|
|
20
|
+
* FIELD NAME: the IDP expects { "code": ... }
|
|
21
|
+
* (MfaBackupCodeVerifyRequest, [JsonPropertyName("code")]). The dash in
|
|
22
|
+
* 'xxxx-xxxx' is optional per the IDP contract, so the code is forwarded as
|
|
23
|
+
* typed and normalisation is left to the IDP rather than guessed here.
|
|
24
|
+
*
|
|
25
|
+
* SINGLE USE, CONSUMED ATOMICALLY. The IDP hash-compares against unused codes
|
|
26
|
+
* and stamps used_at + used_by_ip in the same operation (MfaController 5g).
|
|
27
|
+
* NEVER retry this call on a network error or an ambiguous response — a retry
|
|
28
|
+
* can burn a second code, and the user has ten.
|
|
29
|
+
*
|
|
30
|
+
* DO NOT MAKE GENERATION TRANSITION THE SESSION, the same trap totp-confirm
|
|
31
|
+
* carries: generating recovery codes is not proof of possessing one.
|
|
32
|
+
*
|
|
33
|
+
* THE STORED 2FA METHOD IS 'totp', NOT 'backup_code'. READ THIS BEFORE
|
|
34
|
+
* "CORRECTING" IT — it looks wrong and is not.
|
|
35
|
+
*
|
|
36
|
+
* 1. IT MATCHES THE IDP. MfaController 5g sets
|
|
37
|
+
* `TwoFactorMethod = TwoFactorMethodType.Totp` for this exact path,
|
|
38
|
+
* commented "Backup code = recovery proof of the TOTP factor", and the
|
|
39
|
+
* reissued token's amr carries mfa+totp. A backup code is not a separate
|
|
40
|
+
* factor; it is recovery proof of the authenticator factor.
|
|
41
|
+
*
|
|
42
|
+
* 2. INVENTING A NEW VALUE SILENTLY BREAKS TOKEN REFRESH. refresh.ts:279
|
|
43
|
+
* resolves the method with ['sms','totp','email'].includes(m). Anything
|
|
44
|
+
* outside that set is dropped, so `two_factor_method` never reaches the
|
|
45
|
+
* IDP on refresh, and a session established with a recovery code refreshes
|
|
46
|
+
* differently from one established with the authenticator — for the same
|
|
47
|
+
* underlying factor. It fails quietly, days later, as "sessions
|
|
48
|
+
* occasionally drop", with nothing pointing back to this string.
|
|
49
|
+
*
|
|
50
|
+
* The type union (email|sms|totp) is not the reason for the choice, it is a
|
|
51
|
+
* coincidence that caught it. Widening the union would have compiled and
|
|
52
|
+
* shipped the refresh bug.
|
|
53
|
+
*
|
|
54
|
+
* @package @payez/next-mvp
|
|
55
|
+
*/
|
|
56
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
57
|
+
exports.POST = POST;
|
|
58
|
+
const server_1 = require("next/server");
|
|
59
|
+
const idp_fetch_1 = require("../../lib/idp-fetch");
|
|
60
|
+
const env_1 = require("../../config/env");
|
|
61
|
+
const test_aware_get_token_1 = require("../../lib/test-aware-get-token");
|
|
62
|
+
const session_store_1 = require("../../lib/session-store");
|
|
63
|
+
const jwt_decode_1 = require("../../lib/jwt-decode");
|
|
64
|
+
async function POST(req) {
|
|
65
|
+
try {
|
|
66
|
+
const body = await req.json();
|
|
67
|
+
const code = body.code || body.backupCode || body.verificationCode || body.verification_code;
|
|
68
|
+
if (!code) {
|
|
69
|
+
return server_1.NextResponse.json({
|
|
70
|
+
success: false,
|
|
71
|
+
error: 'Backup code is required',
|
|
72
|
+
code: 'INVALID_REQUEST',
|
|
73
|
+
}, { status: 400 });
|
|
74
|
+
}
|
|
75
|
+
const result = await (0, idp_fetch_1.idpFetchJSON)(req, `${env_1.ENV_CONFIG.IDP_URL}/api/ExternalAuth/twofa/backup-code/verify`, {
|
|
76
|
+
method: 'POST',
|
|
77
|
+
headers: { 'Content-Type': 'application/json' },
|
|
78
|
+
body: JSON.stringify({ code }),
|
|
79
|
+
});
|
|
80
|
+
if (!result.ok) {
|
|
81
|
+
return server_1.NextResponse.json({
|
|
82
|
+
success: false,
|
|
83
|
+
error: result.json?.message || 'Verification failed',
|
|
84
|
+
code: result.json?.code || 'IDP_ERROR',
|
|
85
|
+
meta: { attemptedRefresh: result.attemptedRefresh },
|
|
86
|
+
}, { status: result.status });
|
|
87
|
+
}
|
|
88
|
+
const unwrappedData = result.json?.data || result.json;
|
|
89
|
+
// Only a challenge that returns fresh tokens may transition the session.
|
|
90
|
+
let sessionTransitioned = false;
|
|
91
|
+
if (unwrappedData.access_token && unwrappedData.refresh_token) {
|
|
92
|
+
const token = await (0, test_aware_get_token_1.getTokenTestAware)(req);
|
|
93
|
+
const sessionToken = (token?.sessionToken || token?.redisSessionId);
|
|
94
|
+
if (sessionToken) {
|
|
95
|
+
let accessTokenExpires = Date.now() + 15 * 60 * 1000;
|
|
96
|
+
try {
|
|
97
|
+
const decoded = (0, jwt_decode_1.jwtDecode)(unwrappedData.access_token);
|
|
98
|
+
if (decoded?.exp)
|
|
99
|
+
accessTokenExpires = decoded.exp * 1000;
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
console.warn('[VERIFY_BACKUP_CODE] Could not decode access token, using default expiration');
|
|
103
|
+
}
|
|
104
|
+
let refreshTokenExpires = Date.now() + 3 * 24 * 60 * 60 * 1000;
|
|
105
|
+
try {
|
|
106
|
+
const decoded = (0, jwt_decode_1.jwtDecode)(unwrappedData.refresh_token);
|
|
107
|
+
if (decoded?.exp)
|
|
108
|
+
refreshTokenExpires = decoded.exp * 1000;
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
// Refresh token may carry no exp claim; default stands.
|
|
112
|
+
}
|
|
113
|
+
await (0, session_store_1.transitionTo2FASession)(sessionToken, {
|
|
114
|
+
accessToken: unwrappedData.access_token,
|
|
115
|
+
refreshToken: unwrappedData.refresh_token,
|
|
116
|
+
accessTokenExpires,
|
|
117
|
+
refreshTokenExpires,
|
|
118
|
+
},
|
|
119
|
+
// 'totp' is deliberate and load-bearing. See "THE STORED 2FA METHOD"
|
|
120
|
+
// in the file header before changing it.
|
|
121
|
+
'totp');
|
|
122
|
+
sessionTransitioned = true;
|
|
123
|
+
console.log('[VERIFY_BACKUP_CODE] Session transitioned to 2FA-verified');
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
console.warn('[VERIFY_BACKUP_CODE] IDP accepted the code but no session token was resolvable — session NOT transitioned. THE CODE IS ALREADY SPENT.');
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
console.warn('[VERIFY_BACKUP_CODE] IDP returned 2xx without tokens — session NOT transitioned. THE CODE IS ALREADY SPENT.');
|
|
131
|
+
}
|
|
132
|
+
// twoFactorSessionVerified reports WHAT ACTUALLY HAPPENED, never a copy of
|
|
133
|
+
// `success`. It matters more here than anywhere else: the code is consumed
|
|
134
|
+
// the moment the IDP accepts it, so a client that trusted a hardcoded true
|
|
135
|
+
// would show the user as verified, leave the session unverified, and have
|
|
136
|
+
// burned one of their ten recovery codes to do it.
|
|
137
|
+
return server_1.NextResponse.json({
|
|
138
|
+
success: true,
|
|
139
|
+
verificationSuccessful: true,
|
|
140
|
+
twoFactorSessionVerified: sessionTransitioned,
|
|
141
|
+
codeConsumed: true,
|
|
142
|
+
message: unwrappedData.message || 'Backup code verified successfully',
|
|
143
|
+
}, { status: 200 });
|
|
144
|
+
}
|
|
145
|
+
catch (error) {
|
|
146
|
+
console.error('[VERIFY_BACKUP_CODE] Error:', error);
|
|
147
|
+
return server_1.NextResponse.json({
|
|
148
|
+
success: false,
|
|
149
|
+
error: 'Failed to verify backup code',
|
|
150
|
+
code: 'INTERNAL_ERROR',
|
|
151
|
+
}, { status: 500 });
|
|
152
|
+
}
|
|
153
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
2
|
+
/**
|
|
3
|
+
* Generate MFA backup codes (POST /api/Account/mfa/backup-codes, user JWT).
|
|
4
|
+
* Regeneration REPLACES the prior set — the response carries the new codes
|
|
5
|
+
* once; they are shown once and never retrievable again.
|
|
6
|
+
*/
|
|
7
|
+
export declare function POST(req: NextRequest): Promise<NextResponse<any>>;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.POST = POST;
|
|
4
|
+
const server_1 = require("next/server");
|
|
5
|
+
const idp_fetch_1 = require("../../lib/idp-fetch");
|
|
6
|
+
const env_1 = require("../../config/env");
|
|
7
|
+
/**
|
|
8
|
+
* Generate MFA backup codes (POST /api/Account/mfa/backup-codes, user JWT).
|
|
9
|
+
* Regeneration REPLACES the prior set — the response carries the new codes
|
|
10
|
+
* once; they are shown once and never retrievable again.
|
|
11
|
+
*/
|
|
12
|
+
async function POST(req) {
|
|
13
|
+
let body = '{}';
|
|
14
|
+
try {
|
|
15
|
+
const raw = await req.text();
|
|
16
|
+
if (raw && raw.trim().length > 0)
|
|
17
|
+
body = raw;
|
|
18
|
+
}
|
|
19
|
+
catch { }
|
|
20
|
+
const result = await (0, idp_fetch_1.idpFetchJSON)(req, `${env_1.ENV_CONFIG.IDP_URL}/api/Account/mfa/backup-codes`, {
|
|
21
|
+
method: 'POST',
|
|
22
|
+
headers: { 'Content-Type': 'application/json' },
|
|
23
|
+
body,
|
|
24
|
+
});
|
|
25
|
+
if (!result.ok) {
|
|
26
|
+
return server_1.NextResponse.json({
|
|
27
|
+
success: false,
|
|
28
|
+
message: 'Upstream error',
|
|
29
|
+
error: { code: 'UPSTREAM_SERVICE_ERROR', status: result.status, details: result.json },
|
|
30
|
+
meta: { attemptedRefresh: result.attemptedRefresh },
|
|
31
|
+
}, { status: result.status });
|
|
32
|
+
}
|
|
33
|
+
const bodyJson = result.json;
|
|
34
|
+
if (bodyJson && typeof bodyJson === 'object' && 'success' in bodyJson && 'data' in bodyJson) {
|
|
35
|
+
if (bodyJson.success === true)
|
|
36
|
+
return server_1.NextResponse.json(bodyJson.data, { status: 200 });
|
|
37
|
+
return server_1.NextResponse.json(bodyJson, { status: 200 });
|
|
38
|
+
}
|
|
39
|
+
return server_1.NextResponse.json(bodyJson ?? {}, { status: 200 });
|
|
40
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GET = GET;
|
|
4
|
+
exports.DELETE = DELETE;
|
|
5
|
+
const server_1 = require("next/server");
|
|
6
|
+
const idp_fetch_1 = require("../../lib/idp-fetch");
|
|
7
|
+
const env_1 = require("../../config/env");
|
|
8
|
+
/**
|
|
9
|
+
* MFA methods (GET list / DELETE one, /api/Account/mfa/methods[/{id}],
|
|
10
|
+
* user JWT). Drives the method picker: an EMPTY list must route the user to
|
|
11
|
+
* ENROLMENT, never to a verify screen for a factor they do not hold.
|
|
12
|
+
*/
|
|
13
|
+
function upstreamError(result) {
|
|
14
|
+
return server_1.NextResponse.json({
|
|
15
|
+
success: false,
|
|
16
|
+
message: 'Upstream error',
|
|
17
|
+
error: { code: 'UPSTREAM_SERVICE_ERROR', status: result.status, details: result.json },
|
|
18
|
+
meta: { attemptedRefresh: result.attemptedRefresh },
|
|
19
|
+
}, { status: result.status });
|
|
20
|
+
}
|
|
21
|
+
function passthrough(json) {
|
|
22
|
+
const bodyJson = json;
|
|
23
|
+
if (bodyJson && typeof bodyJson === 'object' && 'success' in bodyJson && 'data' in bodyJson) {
|
|
24
|
+
if (bodyJson.success === true)
|
|
25
|
+
return server_1.NextResponse.json(bodyJson.data ?? { success: true }, { status: 200 });
|
|
26
|
+
return server_1.NextResponse.json(bodyJson, { status: 200 });
|
|
27
|
+
}
|
|
28
|
+
return server_1.NextResponse.json(bodyJson ?? { success: true }, { status: 200 });
|
|
29
|
+
}
|
|
30
|
+
async function GET(req) {
|
|
31
|
+
const result = await (0, idp_fetch_1.idpFetchJSON)(req, `${env_1.ENV_CONFIG.IDP_URL}/api/Account/mfa/methods`, { method: 'GET' });
|
|
32
|
+
if (!result.ok)
|
|
33
|
+
return upstreamError(result);
|
|
34
|
+
return passthrough(result.json);
|
|
35
|
+
}
|
|
36
|
+
async function DELETE(req, id) {
|
|
37
|
+
const result = await (0, idp_fetch_1.idpFetchJSON)(req, `${env_1.ENV_CONFIG.IDP_URL}/api/Account/mfa/methods/${encodeURIComponent(id)}`, { method: 'DELETE' });
|
|
38
|
+
if (!result.ok)
|
|
39
|
+
return upstreamError(result);
|
|
40
|
+
return passthrough(result.json);
|
|
41
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
2
|
+
/**
|
|
3
|
+
* Confirm TOTP enrolment with the first authenticator code
|
|
4
|
+
* (POST /api/Account/mfa/totp/confirm, user JWT). Body forwarded verbatim —
|
|
5
|
+
* it carries the enrolment handle plus the code. Only after this succeeds is
|
|
6
|
+
* the method ACTIVE.
|
|
7
|
+
*/
|
|
8
|
+
export declare function POST(req: NextRequest): Promise<NextResponse<any>>;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.POST = POST;
|
|
4
|
+
const server_1 = require("next/server");
|
|
5
|
+
const idp_fetch_1 = require("../../lib/idp-fetch");
|
|
6
|
+
const env_1 = require("../../config/env");
|
|
7
|
+
/**
|
|
8
|
+
* Confirm TOTP enrolment with the first authenticator code
|
|
9
|
+
* (POST /api/Account/mfa/totp/confirm, user JWT). Body forwarded verbatim —
|
|
10
|
+
* it carries the enrolment handle plus the code. Only after this succeeds is
|
|
11
|
+
* the method ACTIVE.
|
|
12
|
+
*/
|
|
13
|
+
async function POST(req) {
|
|
14
|
+
let body = '{}';
|
|
15
|
+
try {
|
|
16
|
+
const raw = await req.text();
|
|
17
|
+
if (raw && raw.trim().length > 0)
|
|
18
|
+
body = raw;
|
|
19
|
+
}
|
|
20
|
+
catch { }
|
|
21
|
+
const result = await (0, idp_fetch_1.idpFetchJSON)(req, `${env_1.ENV_CONFIG.IDP_URL}/api/Account/mfa/totp/confirm`, {
|
|
22
|
+
method: 'POST',
|
|
23
|
+
headers: { 'Content-Type': 'application/json' },
|
|
24
|
+
body,
|
|
25
|
+
});
|
|
26
|
+
if (!result.ok) {
|
|
27
|
+
return server_1.NextResponse.json({
|
|
28
|
+
success: false,
|
|
29
|
+
message: 'Upstream error',
|
|
30
|
+
error: { code: 'UPSTREAM_SERVICE_ERROR', status: result.status, details: result.json },
|
|
31
|
+
meta: { attemptedRefresh: result.attemptedRefresh },
|
|
32
|
+
}, { status: result.status });
|
|
33
|
+
}
|
|
34
|
+
const bodyJson = result.json;
|
|
35
|
+
if (bodyJson && typeof bodyJson === 'object' && 'success' in bodyJson && 'data' in bodyJson) {
|
|
36
|
+
if (bodyJson.success === true)
|
|
37
|
+
return server_1.NextResponse.json(bodyJson.data, { status: 200 });
|
|
38
|
+
return server_1.NextResponse.json(bodyJson, { status: 200 });
|
|
39
|
+
}
|
|
40
|
+
return server_1.NextResponse.json(bodyJson ?? {}, { status: 200 });
|
|
41
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
2
|
+
/**
|
|
3
|
+
* Begin TOTP enrolment (POST /api/Account/mfa/totp/enroll, user JWT).
|
|
4
|
+
* Returns the shared secret / otpauth URI for the QR. An enrolment left
|
|
5
|
+
* UNCONFIRMED is pending — it can only be RESTARTED, and must never render
|
|
6
|
+
* as "Active" (MFA honesty fixes, 2026-08-01).
|
|
7
|
+
*/
|
|
8
|
+
export declare function POST(req: NextRequest): Promise<NextResponse<any>>;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.POST = POST;
|
|
4
|
+
const server_1 = require("next/server");
|
|
5
|
+
const idp_fetch_1 = require("../../lib/idp-fetch");
|
|
6
|
+
const env_1 = require("../../config/env");
|
|
7
|
+
/**
|
|
8
|
+
* Begin TOTP enrolment (POST /api/Account/mfa/totp/enroll, user JWT).
|
|
9
|
+
* Returns the shared secret / otpauth URI for the QR. An enrolment left
|
|
10
|
+
* UNCONFIRMED is pending — it can only be RESTARTED, and must never render
|
|
11
|
+
* as "Active" (MFA honesty fixes, 2026-08-01).
|
|
12
|
+
*/
|
|
13
|
+
async function POST(req) {
|
|
14
|
+
let body = '{}';
|
|
15
|
+
try {
|
|
16
|
+
const raw = await req.text();
|
|
17
|
+
if (raw && raw.trim().length > 0)
|
|
18
|
+
body = raw;
|
|
19
|
+
}
|
|
20
|
+
catch { }
|
|
21
|
+
const result = await (0, idp_fetch_1.idpFetchJSON)(req, `${env_1.ENV_CONFIG.IDP_URL}/api/Account/mfa/totp/enroll`, {
|
|
22
|
+
method: 'POST',
|
|
23
|
+
headers: { 'Content-Type': 'application/json' },
|
|
24
|
+
body,
|
|
25
|
+
});
|
|
26
|
+
if (!result.ok) {
|
|
27
|
+
return server_1.NextResponse.json({
|
|
28
|
+
success: false,
|
|
29
|
+
message: 'Upstream error',
|
|
30
|
+
error: { code: 'UPSTREAM_SERVICE_ERROR', status: result.status, details: result.json },
|
|
31
|
+
meta: { attemptedRefresh: result.attemptedRefresh },
|
|
32
|
+
}, { status: result.status });
|
|
33
|
+
}
|
|
34
|
+
const bodyJson = result.json;
|
|
35
|
+
if (bodyJson && typeof bodyJson === 'object' && 'success' in bodyJson && 'data' in bodyJson) {
|
|
36
|
+
if (bodyJson.success === true)
|
|
37
|
+
return server_1.NextResponse.json(bodyJson.data, { status: 200 });
|
|
38
|
+
return server_1.NextResponse.json(bodyJson, { status: 200 });
|
|
39
|
+
}
|
|
40
|
+
return server_1.NextResponse.json(bodyJson ?? {}, { status: 200 });
|
|
41
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Verify TOTP 2FA Challenge Handler
|
|
3
|
+
*
|
|
4
|
+
* Verifies an authenticator-app code AT LOGIN and completes the 2FA flow,
|
|
5
|
+
* transitioning the session to MFA-verified.
|
|
6
|
+
*
|
|
7
|
+
* WHY THIS EXISTS (2026-08-02)
|
|
8
|
+
* The package shipped TOTP ENROLMENT (totp-enroll, totp-confirm, against
|
|
9
|
+
* /api/Account/mfa/totp/*) but no TOTP CHALLENGE. SMS and email both had one
|
|
10
|
+
* (verify-sms, verify-email, against /api/ExternalAuth/twofa/*), so TOTP could
|
|
11
|
+
* be enrolled and then never used to satisfy a login challenge — mfaVerified
|
|
12
|
+
* stayed false forever for authenticator-app users, even though the IDP
|
|
13
|
+
* advertises totp in allowed2FAMethods and exposes the endpoint.
|
|
14
|
+
*
|
|
15
|
+
* DO NOT "FIX" THIS BY MAKING totp-confirm TRANSITION THE SESSION. Three people
|
|
16
|
+
* proposed exactly that on the grounds that its SMS/email "siblings" do it. They
|
|
17
|
+
* are not siblings: totp-confirm is ENROLMENT. Flipping mfaVerified there would
|
|
18
|
+
* mean enrolling a factor counts as passing a challenge, letting a
|
|
19
|
+
* password-only session self-promote to MFA-verified by adding an
|
|
20
|
+
* authenticator. Enrolment must never set mfaVerified.
|
|
21
|
+
*
|
|
22
|
+
* FIELD NAME: the IDP expects { "code": ... } here
|
|
23
|
+
* (MfaTotpVerifyRequest, [JsonPropertyName("code")]), NOT the
|
|
24
|
+
* { "verification_code": ... } that the SMS endpoint takes. Copying verify-sms
|
|
25
|
+
* and changing only the URL produces a handler that reaches the IDP and is
|
|
26
|
+
* rejected, which reads as "wrong code" rather than "wrong shape".
|
|
27
|
+
*
|
|
28
|
+
* @package @payez/next-mvp
|
|
29
|
+
*/
|
|
30
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
31
|
+
export declare function POST(req: NextRequest): Promise<NextResponse<{
|
|
32
|
+
success: boolean;
|
|
33
|
+
error: string;
|
|
34
|
+
code: string;
|
|
35
|
+
}> | NextResponse<{
|
|
36
|
+
success: boolean;
|
|
37
|
+
error: any;
|
|
38
|
+
code: any;
|
|
39
|
+
meta: {
|
|
40
|
+
attemptedRefresh: boolean;
|
|
41
|
+
};
|
|
42
|
+
}> | NextResponse<{
|
|
43
|
+
success: boolean;
|
|
44
|
+
verificationSuccessful: boolean;
|
|
45
|
+
twoFactorSessionVerified: boolean;
|
|
46
|
+
message: any;
|
|
47
|
+
}>>;
|