@payez/next-mvp 4.1.21 → 4.4.0

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 (178) hide show
  1. package/POST-INSTALL.md +408 -0
  2. package/README.md +7 -0
  3. package/dist/api-handlers/account/recovery/initiate.js +35 -2
  4. package/dist/api-handlers/account/recovery/send-code.d.ts +0 -33
  5. package/dist/api-handlers/account/recovery/send-code.js +35 -2
  6. package/dist/api-handlers/account/recovery/verify-code.d.ts +0 -33
  7. package/dist/api-handlers/account/recovery/verify-code.js +35 -2
  8. package/dist/api-handlers/account/reset-password.js +35 -2
  9. package/dist/api-handlers/account/validate-password.js +21 -3
  10. package/dist/api-handlers/admin/analytics.js +2 -1
  11. package/dist/api-handlers/admin/audit.js +2 -1
  12. package/dist/api-handlers/admin/sessions.js +2 -1
  13. package/dist/api-handlers/admin/stats.js +2 -1
  14. package/dist/api-handlers/admin/users.js +2 -1
  15. package/dist/api-handlers/admin/vibe-data.js +2 -1
  16. package/dist/api-handlers/auth/jwks.js +10 -0
  17. package/dist/api-handlers/auth/login.js +5 -2
  18. package/dist/api-handlers/auth/passkey-signin.js +16 -1
  19. package/dist/api-handlers/auth/refresh.d.ts +23 -3
  20. package/dist/api-handlers/auth/refresh.js +107 -16
  21. package/dist/api-handlers/auth/signout.js +3 -2
  22. package/dist/api-handlers/auth/verify-code.js +5 -2
  23. package/dist/api-handlers/session/viability.js +27 -1
  24. package/dist/api-handlers/test/force-expire.d.ts +29 -15
  25. package/dist/api-handlers/test/force-expire.js +109 -14
  26. package/dist/auth/better-auth.d.ts +263 -6
  27. package/dist/auth/better-auth.js +437 -59
  28. package/dist/auth/utils/idp-client.js +1 -1
  29. package/dist/client/AuthContext.js +49 -17
  30. package/dist/client/better-auth-client.d.ts +62 -62
  31. package/dist/components/account/UserAvatarMenu.js +12 -0
  32. package/dist/components/auth/ModeAwareLoginPage.js +10 -8
  33. package/dist/components/auth/ModeAwareSignupPage.js +5 -2
  34. package/dist/components/auth/TraditionalAuthSection.js +5 -1
  35. package/dist/hooks/useAuthSettings.js +14 -2
  36. package/dist/hooks/usePublicAuthSettings.d.ts +2 -2
  37. package/dist/hooks/usePublicAuthSettings.js +28 -14
  38. package/dist/lib/client-key.d.ts +61 -0
  39. package/dist/lib/client-key.js +72 -0
  40. package/dist/lib/demo-mode.js +2 -1
  41. package/dist/lib/ensure-fresh-access-token.js +5 -1
  42. package/dist/lib/idp-client-config.d.ts +25 -2
  43. package/dist/lib/idp-client-config.js +381 -36
  44. package/dist/lib/idp-fetch.d.ts +2 -0
  45. package/dist/lib/idp-fetch.js +31 -1
  46. package/dist/lib/idp-url-guard.d.ts +29 -0
  47. package/dist/lib/idp-url-guard.js +38 -0
  48. package/dist/lib/redis.js +77 -5
  49. package/dist/lib/session-cookie-shape.d.ts +69 -0
  50. package/dist/lib/session-cookie-shape.js +126 -0
  51. package/dist/lib/session-store.d.ts +35 -2
  52. package/dist/lib/session-store.js +135 -4
  53. package/dist/lib/single-flight.d.ts +35 -0
  54. package/dist/lib/single-flight.js +30 -0
  55. package/dist/lib/token-lifecycle.js +12 -0
  56. package/dist/middleware/create-middleware.js +12 -0
  57. package/dist/models/SessionModel.d.ts +41 -0
  58. package/dist/models/SessionModel.js +9 -0
  59. package/dist/pages/admin-login/page.d.ts +1 -1
  60. package/dist/pages/admin-login/page.js +5 -41
  61. package/dist/pages/security/EnhancedSecurityPage.d.ts +35 -1
  62. package/dist/pages/security/EnhancedSecurityPage.js +42 -2
  63. package/dist/pages/security/index.d.ts +1 -0
  64. package/dist/pages/security/index.js +3 -0
  65. package/dist/pages/test-env/EmergencyLogoutPage.d.ts +17 -5
  66. package/dist/pages/test-env/EmergencyLogoutPage.js +17 -5
  67. package/dist/pages/test-env/JwtInspectPage.d.ts +30 -5
  68. package/dist/pages/test-env/JwtInspectPage.js +166 -7
  69. package/dist/pages/test-env/RefreshTokenPage.d.ts +22 -6
  70. package/dist/pages/test-env/RefreshTokenPage.js +22 -6
  71. package/dist/pages/test-env/TestEnvPage.d.ts +16 -4
  72. package/dist/pages/test-env/TestEnvPage.js +16 -4
  73. package/dist/pages/test-env/guarded.d.ts +4 -0
  74. package/dist/pages/test-env/guarded.js +86 -0
  75. package/dist/pages/test-env/index.d.ts +50 -7
  76. package/dist/pages/test-env/index.js +58 -12
  77. package/dist/routes/auth/logout.js +3 -2
  78. package/dist/routes/auth/refresh.js +27 -1
  79. package/dist/routes/auth/session.d.ts +12 -3
  80. package/dist/routes/auth/session.js +92 -9
  81. package/dist/routes/auth/settings.d.ts +34 -4
  82. package/dist/routes/auth/settings.js +87 -33
  83. package/dist/routes/auth/viability.d.ts +1 -1
  84. package/dist/routes/auth/viability.js +122 -11
  85. package/dist/server/auth-guard.js +22 -15
  86. package/dist/server/auth.d.ts +177 -20
  87. package/dist/server/auth.js +34 -75
  88. package/dist/server/decode-session.js +84 -10
  89. package/dist/types/auth.d.ts +18 -0
  90. package/package.json +969 -958
  91. package/scripts/check-internal-url-usage.sh +73 -73
  92. package/scripts/check-publish-health.mjs +120 -0
  93. package/scripts/dev-broker.ps1 +71 -6
  94. package/scripts/print-client-config.mjs +162 -0
  95. package/scripts/server-only-stub.cjs +4 -0
  96. package/src/api-handlers/account/recovery/initiate.ts +35 -2
  97. package/src/api-handlers/account/recovery/send-code.ts +36 -2
  98. package/src/api-handlers/account/recovery/verify-code.ts +36 -2
  99. package/src/api-handlers/account/reset-password.ts +35 -2
  100. package/src/api-handlers/account/validate-password.ts +22 -3
  101. package/src/api-handlers/admin/analytics.ts +2 -1
  102. package/src/api-handlers/admin/audit.ts +2 -1
  103. package/src/api-handlers/admin/sessions.ts +2 -1
  104. package/src/api-handlers/admin/stats.ts +2 -1
  105. package/src/api-handlers/admin/users.ts +2 -1
  106. package/src/api-handlers/admin/vibe-data.ts +2 -1
  107. package/src/api-handlers/auth/jwks.ts +10 -0
  108. package/src/api-handlers/auth/login.ts +5 -2
  109. package/src/api-handlers/auth/passkey-signin.ts +112 -98
  110. package/src/api-handlers/auth/refresh.ts +112 -15
  111. package/src/api-handlers/auth/signout.ts +4 -3
  112. package/src/api-handlers/auth/verify-code.ts +5 -2
  113. package/src/api-handlers/mfa/backup-code-verify.ts +175 -175
  114. package/src/api-handlers/mfa/backup-codes.ts +41 -41
  115. package/src/api-handlers/mfa/methods.ts +46 -46
  116. package/src/api-handlers/mfa/totp-confirm.ts +42 -42
  117. package/src/api-handlers/mfa/totp-enroll.ts +42 -42
  118. package/src/api-handlers/passkeys/list.ts +31 -31
  119. package/src/api-handlers/passkeys/manage.ts +54 -54
  120. package/src/api-handlers/passkeys/register-options.ts +42 -42
  121. package/src/api-handlers/passkeys/register-verify.ts +41 -41
  122. package/src/api-handlers/session/viability.ts +29 -1
  123. package/src/api-handlers/test/force-expire.ts +118 -13
  124. package/src/auth/better-auth.ts +486 -51
  125. package/src/auth/utils/idp-client.ts +1 -1
  126. package/src/client/AuthContext.tsx +55 -19
  127. package/src/components/account/UserAvatarMenu.tsx +11 -0
  128. package/src/components/auth/ModeAwareLoginPage.tsx +40 -49
  129. package/src/components/auth/ModeAwareSignupPage.tsx +5 -2
  130. package/src/components/auth/TraditionalAuthSection.tsx +5 -1
  131. package/src/hooks/useAuthSettings.ts +14 -2
  132. package/src/hooks/usePublicAuthSettings.ts +38 -16
  133. package/src/lib/client-key.ts +88 -0
  134. package/src/lib/demo-mode.ts +2 -1
  135. package/src/lib/ensure-fresh-access-token.ts +5 -1
  136. package/src/lib/idp-client-config.ts +399 -36
  137. package/src/lib/idp-fetch.ts +36 -1
  138. package/src/lib/idp-url-guard.ts +37 -0
  139. package/src/lib/redis.ts +84 -6
  140. package/src/lib/session-cookie-shape.ts +184 -0
  141. package/src/lib/session-store.ts +152 -6
  142. package/src/lib/single-flight.ts +64 -0
  143. package/src/lib/token-lifecycle.ts +12 -0
  144. package/src/middleware/create-middleware.ts +11 -0
  145. package/src/models/SessionModel.ts +49 -0
  146. package/src/pages/admin-login/page.tsx +17 -127
  147. package/src/pages/security/EnhancedSecurityPage.tsx +95 -9
  148. package/src/pages/security/index.ts +3 -0
  149. package/src/pages/test-env/EmergencyLogoutPage.tsx +17 -5
  150. package/src/pages/test-env/JwtInspectPage.tsx +235 -20
  151. package/src/pages/test-env/RefreshTokenPage.tsx +22 -6
  152. package/src/pages/test-env/TestEnvPage.tsx +16 -4
  153. package/src/pages/test-env/guarded.tsx +81 -0
  154. package/src/pages/test-env/index.ts +59 -7
  155. package/src/routes/auth/logout.ts +4 -3
  156. package/src/routes/auth/passkey-signin.ts +13 -13
  157. package/src/routes/auth/refresh.ts +30 -1
  158. package/src/routes/auth/session.ts +98 -9
  159. package/src/routes/auth/settings.ts +144 -42
  160. package/src/routes/auth/viability.ts +129 -11
  161. package/src/routes/mfa/backup-code-verify.ts +1 -1
  162. package/src/routes/mfa/backup-codes.ts +10 -10
  163. package/src/routes/mfa/methods.ts +22 -22
  164. package/src/routes/mfa/totp-confirm.ts +10 -10
  165. package/src/routes/mfa/totp-enroll.ts +10 -10
  166. package/src/routes/mfa/totp-verify.ts +14 -14
  167. package/src/routes/passkeys/list.ts +10 -10
  168. package/src/routes/passkeys/manage.ts +23 -23
  169. package/src/routes/passkeys/register-options.ts +10 -10
  170. package/src/routes/passkeys/register-verify.ts +10 -10
  171. package/src/server/auth-guard.ts +24 -17
  172. package/src/server/auth.ts +37 -73
  173. package/src/server/decode-session.ts +95 -11
  174. package/src/templates/instrumentation.ts +21 -2
  175. package/src/types/auth.ts +20 -0
  176. package/dist/lib/auth-secret.d.ts +0 -17
  177. package/dist/lib/auth-secret.js +0 -116
  178. package/src/lib/auth-secret.ts +0 -137
@@ -3,30 +3,135 @@ import { getSession as getBetterAuthSession } from '../../server/auth';
3
3
  import { getSession, updateSession } from '../../lib/session-store';
4
4
 
5
5
  /**
6
- * Force-expire access token for testing refresh flow.
6
+ * ⚠️ DESTRUCTIVE TEST-ONLY HANDLER — DO NOT MOUNT IN PRODUCTION. ⚠️
7
7
  *
8
- * Sets the access token expiry to 2 minutes in the past,
9
- * which will trigger a refresh on the next API call.
8
+ * Force-expire access token for testing refresh flow. Sets the access token
9
+ * expiry to 2 minutes in the past, which will trigger a refresh on the next
10
+ * API call.
11
+ *
12
+ * THIS MUTATES SESSION STATE. It is a test affordance, not a product feature.
13
+ *
14
+ * ── WHY THIS WARNING EXISTS (QAPert 34983) ────────────────────────────────
15
+ * The usage block below is not decoration: THIS DOCSTRING IS THE SPREAD
16
+ * MECHANISM. It ships compiled into dist and into the .d.ts, so it reaches
17
+ * every consumer's editor as hover text, and it is a one-line copy-paste that
18
+ * mounts a destructive endpoint. Every one of the three surfaces this route
19
+ * ended up on followed exactly these instructions.
20
+ *
21
+ * The handler itself was fixed (244877: default-deny behind ENABLE_TEST_ROUTES,
22
+ * plus the caller-supplied-token IDOR removed). But a fixed handler shipped
23
+ * with unqualified mounting instructions still spreads: the guidance was never
24
+ * corrected alongside the code, and a package that documents a dangerous route
25
+ * as a plain convenience is instructing the weaker configuration.
26
+ *
27
+ * ── IF YOU MOUNT IT, MOUNT IT LIKE THIS ───────────────────────────────────
28
+ * Required: set ENABLE_TEST_ROUTES=true IN THAT ENVIRONMENT ONLY. Absent, empty
29
+ * or any other value default-DENIES (404). Never set it in production.
10
30
  *
11
- * Usage in consuming app:
12
31
  * ```typescript
13
- * // app/api/test/force-expire/route.ts
32
+ * // app/api/test/force-expire/route.ts — TEST/DEV ENVIRONMENTS ONLY
33
+ * // Requires ENABLE_TEST_ROUTES=true in that environment; 404s otherwise.
14
34
  * export { POST } from '@payez/next-mvp/api-handlers/test/force-expire';
15
35
  * ```
36
+ *
37
+ * If you do not need to exercise the refresh flow, DO NOT MOUNT THIS AT ALL.
38
+ * The safest configuration is the absence of the route.
16
39
  */
17
40
  export const POST = async (req: NextRequest) => {
41
+ // 244877 (package-level): DEFAULT DENY behind ENABLE_TEST_ROUTES.
42
+ //
43
+ // This handler MUTATES session state — it rewrites idpAccessTokenExpires two
44
+ // minutes into the past — and was gated on nothing but "any signed-in
45
+ // session": no role check, no env kill-switch. Because consuming apps mount
46
+ // it with a one-line re-export, EVERY consumer inherited that, including
47
+ // deployments on production hosts.
48
+ //
49
+ // The card was raised against one app's local copy. Sweeping the pattern
50
+ // rather than the instance found the same route in three places — a local
51
+ // copy in the graft, a local copy in idealvibe.online, and idealresume.online
52
+ // re-exporting THIS handler — so the fix belongs here, at the source all
53
+ // three ultimately derive from, not only in whichever copy was noticed first.
54
+ //
55
+ // The gate is the FIRST statement, before any session decode, so a disabled
56
+ // build does not reveal that the route is live. Absent, empty, or any value
57
+ // other than 'true' disables it. Distinguishable body on purpose: a bare 404
58
+ // reads as "route missing / build broken" and sends someone debugging
59
+ // deployment instead of reading a flag.
60
+ //
61
+ // A consumer that genuinely needs this in a test environment sets
62
+ // ENABLE_TEST_ROUTES=true there, which is the same posture the token
63
+ // inspector already uses for a strictly LESS dangerous, read-only route.
64
+ if (process.env.ENABLE_TEST_ROUTES !== 'true') {
65
+ // DISCLOSURE IS CONDITIONAL ON ENVIRONMENT (QAPert-NightHawk 34539, after
66
+ // QAPert caught that my first version shipped the very leak we had just
67
+ // criticised elsewhere).
68
+ //
69
+ // My original body named the flag: "Set ENABLE_TEST_ROUTES=true...". That
70
+ // is the same class of disclosure as TOKEN_INSPECTOR_DISABLED — it confirms
71
+ // the route exists, names the capability, and hands a prober the exact
72
+ // switch that turns it on. Having argued against that pattern one directory
73
+ // over, shipping it here would have been incoherent.
74
+ //
75
+ // Deleting the message outright is also wrong: it is what stops a developer
76
+ // losing an hour deciding whether their build is broken. The tension
77
+ // resolves by AUDIENCE, not by picking a side:
78
+ // production -> 404, EMPTY body. Nothing about the capability, the
79
+ // flag, or even that a handler is here.
80
+ // non-production -> 404 + the helpful message naming the flag.
81
+ //
82
+ // The operator affordance survives in production too, via the log line
83
+ // below: logs are visible to operators and not to probers, which is exactly
84
+ // the split the response body cannot make.
85
+ console.warn(
86
+ '[FORCE-EXPIRE] blocked: ENABLE_TEST_ROUTES is not "true" (test routes default-deny).',
87
+ );
88
+
89
+ if (process.env.NODE_ENV === 'production') {
90
+ return new NextResponse(null, { status: 404 });
91
+ }
92
+
93
+ return NextResponse.json(
94
+ {
95
+ error: 'TEST_ROUTES_DISABLED',
96
+ message:
97
+ 'Test routes are disabled in this build. Set ENABLE_TEST_ROUTES=true to enable them (development only).',
98
+ },
99
+ { status: 404 },
100
+ );
101
+ }
102
+
18
103
  try {
19
104
  const betterAuthSession = await getBetterAuthSession(req);
20
105
 
21
- let sessionToken = betterAuthSession?.session?.token as string | undefined;
106
+ // ACCESS CONTROL (QAPert 34553). This handler previously fell back to a
107
+ // CALLER-SUPPLIED `X-Session-Token` header when the request carried no
108
+ // session of its own, and then force-expired whatever token that header
109
+ // named — with NO check that the token belonged to the caller. The 401
110
+ // fired only when a caller supplied NEITHER a session NOR the header. So
111
+ // an UNAUTHENTICATED caller who presented any valid session token could
112
+ // expire ANOTHER USER'S session: an unauthenticated cross-session write.
113
+ //
114
+ // The fallback is removed rather than ownership-checked, because an
115
+ // ownership check would make it exactly equivalent to reading the caller's
116
+ // own session — the header could then never name anything else, so it
117
+ // would be a parameter that cannot legally vary. The session to expire is
118
+ // now ALWAYS the caller's own, which is also what the graft's local copy
119
+ // has always done.
120
+ //
121
+ // This is defence in depth, not the only control: the ENABLE_TEST_ROUTES
122
+ // gate above already 404s this handler by default. But the gate is
123
+ // switched ON in test environments, and "unauthenticated users can expire
124
+ // each other's sessions" is not acceptable there either — a control that
125
+ // only holds while a feature is switched off is not an access control.
126
+ //
127
+ // Corrects NextPert's own scope claim on card 244877 ("a caller could only
128
+ // ever expire their OWN session"): that was read off the graft's LOCAL
129
+ // copy, which derives the token from the caller's JWT. It was never true
130
+ // of THIS handler, which is the one consumers actually mount.
131
+ const sessionToken = betterAuthSession?.session?.token as string | undefined;
22
132
  if (!sessionToken) {
23
- const headerSessionToken = req.headers.get('x-session-token') || req.headers.get('X-Session-Token');
24
- if (headerSessionToken) {
25
- sessionToken = headerSessionToken;
26
- } else {
27
- console.warn('[TEST_EXPIRE] No session token or X-Session-Token header');
28
- return NextResponse.json({ success: false, error: 'No session token' }, { status: 401 });
29
- }
133
+ console.warn('[TEST_EXPIRE] No session on the request');
134
+ return NextResponse.json({ success: false, error: 'No session token' }, { status: 401 });
30
135
  }
31
136
 
32
137
  const session = await getSession(sessionToken);