@payez/next-mvp 4.1.21 → 4.4.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.
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 +162 -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
@@ -1,116 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.resolveAuthSecret = resolveAuthSecret;
4
- require("server-only");
5
- const secret_validation_1 = require("./secret-validation");
6
- const crypto_1 = require("crypto");
7
- let cachedSecret = null;
8
- let lastFetchedAt = 0;
9
- /**
10
- * Resolve the Better Auth signing secret (server-only).
11
- *
12
- * Priority:
13
- * 1) Use process.env.BETTER_AUTH_SECRET (preferred) or NEXTAUTH_SECRET (legacy)
14
- * if present — allows overrides/production via env.
15
- * 2) Fetch from IDP broker endpoint — IDP handles all Key Vault/signing.
16
- * 3) Cache result in-memory and set process.env.BETTER_AUTH_SECRET for
17
- * subsequent calls.
18
- *
19
- * NOTE on naming: this secret is the cryptographic key Better Auth uses to
20
- * sign session JWTs. The IDP backend still names the broker endpoint and
21
- * response field with the legacy "next-auth" / "nextAuthSecret" names; we
22
- * read both new and legacy on the wire during the migration window.
23
- */
24
- async function resolveAuthSecret() {
25
- // Check if already in environment (prefer new name, fall back to legacy)
26
- const envSecret = (process.env.BETTER_AUTH_SECRET && process.env.BETTER_AUTH_SECRET.trim() !== ''
27
- ? process.env.BETTER_AUTH_SECRET
28
- : undefined) ||
29
- (process.env.NEXTAUTH_SECRET && process.env.NEXTAUTH_SECRET.trim() !== ''
30
- ? process.env.NEXTAUTH_SECRET
31
- : undefined);
32
- if (envSecret) {
33
- // Silent - already configured
34
- return envSecret;
35
- }
36
- // Check if cached and fresh (within 5 minutes)
37
- if (cachedSecret && Date.now() - lastFetchedAt < 5 * 60 * 1000) {
38
- return cachedSecret;
39
- }
40
- // Broker mode: fetch from IDP (IDP handles all Key Vault/signing)
41
- const base = process.env.IDP_URL;
42
- if (!base)
43
- throw new Error('IDP_URL environment variable is required');
44
- const clientIdStr = process.env.CLIENT_ID;
45
- if (!clientIdStr || clientIdStr.trim() === '')
46
- throw new Error('CLIENT_ID is required (e.g., "ideal_resume_website")');
47
- // Step 1: Request IDP to sign a client assertion (IDP has the keys, not us)
48
- const signingUrl = new URL(`${base.replace(/\/$/, '')}/api/ExternalAuth/sign-client-assertion`);
49
- const signingPayload = {
50
- issuer: clientIdStr,
51
- subject: clientIdStr,
52
- audience: 'urn:payez:externalauth:nextauthsecret',
53
- expires_in: 60,
54
- };
55
- const signingResp = await fetch(signingUrl.toString(), {
56
- method: 'POST',
57
- headers: {
58
- 'Accept': 'application/json',
59
- 'Content-Type': 'application/json',
60
- 'X-Client-Id': clientIdStr,
61
- 'X-Correlation-Id': (0, crypto_1.randomUUID)().replace(/-/g, ''),
62
- },
63
- body: JSON.stringify(signingPayload),
64
- cache: 'no-store'
65
- });
66
- if (!signingResp.ok) {
67
- const txt = await signingResp.text().catch(() => 'Unknown error');
68
- throw new Error(`Failed to sign client assertion: ${signingResp.status} ${signingResp.statusText} - ${txt}`);
69
- }
70
- const signingBody = await signingResp.json().catch(() => ({}));
71
- const client_assertion = (signingBody?.data?.client_assertion ??
72
- signingBody?.data?.clientAssertion ??
73
- signingBody?.client_assertion ??
74
- signingBody?.clientAssertion ??
75
- signingBody?.data?.ClientAssertion ??
76
- signingBody?.ClientAssertion);
77
- if (!client_assertion || typeof client_assertion !== 'string') {
78
- throw new Error('IDP did not return a valid signed client assertion');
79
- }
80
- // Step 2: Use the signed assertion to fetch the auth secret
81
- // (Endpoint is still served at /next-auth/secret on the IDP — legacy path.)
82
- const proxyUrl = new URL(`${base.replace(/\/$/, '')}/api/ExternalAuth/next-auth/secret`);
83
- const proxyResp = await fetch(proxyUrl.toString(), {
84
- method: 'POST',
85
- headers: {
86
- 'Accept': 'application/json',
87
- 'Content-Type': 'application/json',
88
- 'X-Client-Id': clientIdStr,
89
- 'X-Correlation-Id': (0, crypto_1.randomUUID)().replace(/-/g, ''),
90
- },
91
- body: JSON.stringify({ client_assertion }),
92
- cache: 'no-store'
93
- });
94
- if (!proxyResp.ok) {
95
- const txt = await proxyResp.text().catch(() => 'Unknown error');
96
- throw new Error(`Proxy error: ${proxyResp.status} ${proxyResp.statusText} - ${txt}`);
97
- }
98
- const proxyBody = await proxyResp.json().catch(() => ({}));
99
- const secret = (proxyBody?.data?.secret ?? proxyBody?.secret);
100
- // Configuration is available but we don't log it verbosely
101
- if (!secret || typeof secret !== 'string') {
102
- throw new Error('Proxy did not return a valid auth secret');
103
- }
104
- const validation = (0, secret_validation_1.validateAuthSecret)(secret);
105
- if (!validation.valid) {
106
- throw new Error(`Fetched auth secret failed validation: ${validation.reason}`);
107
- }
108
- cachedSecret = secret;
109
- lastFetchedAt = Date.now();
110
- process.env.BETTER_AUTH_SECRET = secret;
111
- // Also set legacy name during transition so any consumer still reading
112
- // process.env.NEXTAUTH_SECRET keeps working until they upgrade.
113
- process.env.NEXTAUTH_SECRET = secret;
114
- console.log('[AUTH-SECRET] Resolved from IDP (length:', secret.length + ')');
115
- return secret;
116
- }
@@ -1,137 +0,0 @@
1
- import 'server-only';
2
- import { validateAuthSecret } from './secret-validation';
3
- import { randomUUID } from 'crypto';
4
-
5
- let cachedSecret: string | null = null;
6
- let lastFetchedAt = 0;
7
-
8
- /**
9
- * Resolve the Better Auth signing secret (server-only).
10
- *
11
- * Priority:
12
- * 1) Use process.env.BETTER_AUTH_SECRET (preferred) or NEXTAUTH_SECRET (legacy)
13
- * if present — allows overrides/production via env.
14
- * 2) Fetch from IDP broker endpoint — IDP handles all Key Vault/signing.
15
- * 3) Cache result in-memory and set process.env.BETTER_AUTH_SECRET for
16
- * subsequent calls.
17
- *
18
- * NOTE on naming: this secret is the cryptographic key Better Auth uses to
19
- * sign session JWTs. The IDP backend still names the broker endpoint and
20
- * response field with the legacy "next-auth" / "nextAuthSecret" names; we
21
- * read both new and legacy on the wire during the migration window.
22
- */
23
- export async function resolveAuthSecret(): Promise<string> {
24
- // Check if already in environment (prefer new name, fall back to legacy)
25
- const envSecret =
26
- (process.env.BETTER_AUTH_SECRET && process.env.BETTER_AUTH_SECRET.trim() !== ''
27
- ? process.env.BETTER_AUTH_SECRET
28
- : undefined) ||
29
- (process.env.NEXTAUTH_SECRET && process.env.NEXTAUTH_SECRET.trim() !== ''
30
- ? process.env.NEXTAUTH_SECRET
31
- : undefined);
32
- if (envSecret) {
33
- // Silent - already configured
34
- return envSecret;
35
- }
36
-
37
- // Check if cached and fresh (within 5 minutes)
38
- if (cachedSecret && Date.now() - lastFetchedAt < 5 * 60 * 1000) {
39
- return cachedSecret;
40
- }
41
-
42
- // Broker mode: fetch from IDP (IDP handles all Key Vault/signing)
43
- const base = process.env.IDP_URL;
44
- if (!base) throw new Error('IDP_URL environment variable is required');
45
-
46
- const clientIdStr = process.env.CLIENT_ID;
47
- if (!clientIdStr || clientIdStr.trim() === '') throw new Error('CLIENT_ID is required (e.g., "ideal_resume_website")');
48
-
49
- // Step 1: Request IDP to sign a client assertion (IDP has the keys, not us)
50
-
51
- const signingUrl = new URL(`${base.replace(/\/$/, '')}/api/ExternalAuth/sign-client-assertion`);
52
-
53
- const signingPayload = {
54
- issuer: clientIdStr,
55
- subject: clientIdStr,
56
- audience: 'urn:payez:externalauth:nextauthsecret',
57
- expires_in: 60,
58
- };
59
-
60
- const signingResp = await fetch(signingUrl.toString(), {
61
- method: 'POST',
62
- headers: {
63
- 'Accept': 'application/json',
64
- 'Content-Type': 'application/json',
65
- 'X-Client-Id': clientIdStr,
66
- 'X-Correlation-Id': randomUUID().replace(/-/g, ''),
67
- },
68
- body: JSON.stringify(signingPayload),
69
- cache: 'no-store'
70
- } as RequestInit);
71
-
72
- if (!signingResp.ok) {
73
- const txt = await signingResp.text().catch(() => 'Unknown error');
74
- throw new Error(`Failed to sign client assertion: ${signingResp.status} ${signingResp.statusText} - ${txt}`);
75
- }
76
-
77
- const signingBody: any = await signingResp.json().catch(() => ({}));
78
- const client_assertion = (
79
- signingBody?.data?.client_assertion ??
80
- signingBody?.data?.clientAssertion ??
81
- signingBody?.client_assertion ??
82
- signingBody?.clientAssertion ??
83
- signingBody?.data?.ClientAssertion ??
84
- signingBody?.ClientAssertion
85
- ) as string | undefined;
86
-
87
- if (!client_assertion || typeof client_assertion !== 'string') {
88
- throw new Error('IDP did not return a valid signed client assertion');
89
- }
90
-
91
- // Step 2: Use the signed assertion to fetch the auth secret
92
- // (Endpoint is still served at /next-auth/secret on the IDP — legacy path.)
93
-
94
- const proxyUrl = new URL(`${base.replace(/\/$/, '')}/api/ExternalAuth/next-auth/secret`);
95
-
96
- const proxyResp = await fetch(proxyUrl.toString(), {
97
- method: 'POST',
98
- headers: {
99
- 'Accept': 'application/json',
100
- 'Content-Type': 'application/json',
101
- 'X-Client-Id': clientIdStr,
102
- 'X-Correlation-Id': randomUUID().replace(/-/g, ''),
103
- },
104
- body: JSON.stringify({ client_assertion }),
105
- cache: 'no-store'
106
- } as RequestInit);
107
-
108
- if (!proxyResp.ok) {
109
- const txt = await proxyResp.text().catch(() => 'Unknown error');
110
- throw new Error(`Proxy error: ${proxyResp.status} ${proxyResp.statusText} - ${txt}`);
111
- }
112
-
113
- const proxyBody: any = await proxyResp.json().catch(() => ({}));
114
-
115
- const secret = (proxyBody?.data?.secret ?? proxyBody?.secret) as string | undefined;
116
- // Configuration is available but we don't log it verbosely
117
-
118
- if (!secret || typeof secret !== 'string') {
119
- throw new Error('Proxy did not return a valid auth secret');
120
- }
121
-
122
- const validation = validateAuthSecret(secret);
123
- if (!validation.valid) {
124
- throw new Error(`Fetched auth secret failed validation: ${validation.reason}`);
125
- }
126
-
127
- cachedSecret = secret;
128
- lastFetchedAt = Date.now();
129
- process.env.BETTER_AUTH_SECRET = secret;
130
- // Also set legacy name during transition so any consumer still reading
131
- // process.env.NEXTAUTH_SECRET keeps working until they upgrade.
132
- process.env.NEXTAUTH_SECRET = secret;
133
-
134
- console.log('[AUTH-SECRET] Resolved from IDP (length:', secret.length + ')');
135
-
136
- return secret;
137
- }