@payez/next-mvp 4.1.0 → 4.1.2

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.
@@ -8,10 +8,12 @@
8
8
  */
9
9
 
10
10
  import { createAuthClient } from 'better-auth/react';
11
+ import { magicLinkClient } from 'better-auth/client/plugins';
11
12
  import { useMemo } from 'react';
12
13
 
13
14
  export const authClient = createAuthClient({
14
15
  // baseURL derived from BETTER_AUTH_URL or window.location.origin
16
+ plugins: [magicLinkClient()],
15
17
  });
16
18
 
17
19
  // Convenience exports
@@ -0,0 +1,320 @@
1
+ /**
2
+ * ensureFreshAccessToken — server-side preflight refresh.
3
+ *
4
+ * Returns a non-expired IDP access token for a given session. Refreshes
5
+ * proactively when the stored token is within the safety window of expiry,
6
+ * using the same Redis lock and IDP wire shape as `createRefreshHandler`.
7
+ *
8
+ * Designed for proxy-route auth helpers that today read the stored token
9
+ * blindly and let the backend reject it with a 401. Calling this instead of
10
+ * `getSession(...).idpAccessToken` means a good token client never sends
11
+ * credentials it already knows are invalid.
12
+ *
13
+ * Single-use refresh-token semantics are preserved via Redis-backed
14
+ * single-flight locking (see `acquireRefreshLock`).
15
+ */
16
+ import {
17
+ getSession,
18
+ updateSession,
19
+ acquireRefreshLock,
20
+ releaseRefreshLock,
21
+ checkRefreshLock,
22
+ } from './session-store';
23
+ import { computeTokenExpiries } from './token-expiry';
24
+ import { extractKidFromToken } from '../auth/utils/token-utils';
25
+
26
+ export interface EnsureFreshConfig {
27
+ idpBaseUrl: string;
28
+ clientId: string;
29
+ refreshEndpoint?: string;
30
+ }
31
+
32
+ export interface EnsureFreshOptions {
33
+ /** Refresh if the access token is within this many ms of expiry. Default 60_000. */
34
+ safetyWindowMs?: number;
35
+ /** Max wait while another caller holds the refresh lock. Default 5000. */
36
+ lockWaitMs?: number;
37
+ /** Optional caller request id for lock attribution. */
38
+ requestId?: string;
39
+ }
40
+
41
+ export type EnsureFreshResult =
42
+ | {
43
+ ok: true;
44
+ accessToken: string;
45
+ accessTokenExpires: number;
46
+ /** True if we refreshed (or a concurrent refresh completed); false if the stored token was already fresh. */
47
+ refreshed: boolean;
48
+ }
49
+ | {
50
+ ok: false;
51
+ code: string;
52
+ message: string;
53
+ status: number;
54
+ terminal?: boolean;
55
+ discardToken?: boolean;
56
+ retryable?: boolean;
57
+ resolution?: string;
58
+ };
59
+
60
+ const DEFAULT_SAFETY_WINDOW_MS = 60_000;
61
+ const DEFAULT_LOCK_WAIT_MS = 5000;
62
+
63
+ function decodeJwtExp(token: string): number {
64
+ const parts = token.split('.');
65
+ if (parts.length !== 3) return -1;
66
+ try {
67
+ const payload = JSON.parse(Buffer.from(parts[1], 'base64url').toString());
68
+ return (payload.exp || 0) * 1000;
69
+ } catch {
70
+ return -1;
71
+ }
72
+ }
73
+
74
+ export async function ensureFreshAccessToken(
75
+ sessionToken: string,
76
+ config: EnsureFreshConfig,
77
+ options: EnsureFreshOptions = {}
78
+ ): Promise<EnsureFreshResult> {
79
+ const { idpBaseUrl, clientId, refreshEndpoint = '/api/ExternalAuth/refresh' } = config;
80
+ const safetyWindowMs = options.safetyWindowMs ?? DEFAULT_SAFETY_WINDOW_MS;
81
+ const lockWaitMs = options.lockWaitMs ?? DEFAULT_LOCK_WAIT_MS;
82
+ const requestId = options.requestId ?? `ensure_fresh_${Date.now()}`;
83
+
84
+ const session = await getSession(sessionToken);
85
+ if (!session) {
86
+ return { ok: false, code: 'NO_SESSION', message: 'No session for token', status: 401, terminal: true };
87
+ }
88
+
89
+ const storedToken = session.idpAccessToken;
90
+ if (!storedToken) {
91
+ return { ok: false, code: 'NO_TOKEN', message: 'No IDP access token in session', status: 401, terminal: true };
92
+ }
93
+
94
+ const now = Date.now();
95
+ const redisExpires = session.idpAccessTokenExpires ?? 0;
96
+ const jwtExpires = decodeJwtExp(storedToken);
97
+ // Use the smaller of the two to be conservative — Redis can be stale, JWT exp is authoritative.
98
+ const effectiveExpires = jwtExpires > 0 ? Math.min(redisExpires || jwtExpires, jwtExpires) : redisExpires;
99
+ const msUntilExpiry = effectiveExpires - now;
100
+
101
+ if (msUntilExpiry > safetyWindowMs) {
102
+ return { ok: true, accessToken: storedToken, accessTokenExpires: effectiveExpires, refreshed: false };
103
+ }
104
+
105
+ if (!session.idpRefreshToken) {
106
+ return {
107
+ ok: false,
108
+ code: 'NO_REFRESH_TOKEN',
109
+ message: 'Access token expired and no refresh token available',
110
+ status: 401,
111
+ terminal: true,
112
+ resolution: 'User must re-authenticate',
113
+ };
114
+ }
115
+
116
+ const lock = await acquireRefreshLock(sessionToken, requestId, lockWaitMs);
117
+ let weHoldLock = false;
118
+ let releaseVersion: number | undefined;
119
+
120
+ if (!lock.acquired) {
121
+ const existing = await checkRefreshLock(sessionToken);
122
+ if (!existing || existing.acquiredBy !== requestId) {
123
+ const startWait = Date.now();
124
+ while (Date.now() - startWait < lockWaitMs) {
125
+ await new Promise(r => setTimeout(r, 200));
126
+ const stillLocked = await checkRefreshLock(sessionToken);
127
+ if (!stillLocked) {
128
+ const after = await getSession(sessionToken);
129
+ if (after?.idpAccessToken && after.idpAccessTokenExpires) {
130
+ const remaining = after.idpAccessTokenExpires - Date.now();
131
+ if (remaining > safetyWindowMs) {
132
+ return {
133
+ ok: true,
134
+ accessToken: after.idpAccessToken,
135
+ accessTokenExpires: after.idpAccessTokenExpires,
136
+ refreshed: true,
137
+ };
138
+ }
139
+ }
140
+ break;
141
+ }
142
+ }
143
+ return {
144
+ ok: false,
145
+ code: 'CONFLICT',
146
+ message: 'Refresh already in progress',
147
+ status: 409,
148
+ retryable: true,
149
+ };
150
+ }
151
+ } else {
152
+ weHoldLock = true;
153
+ releaseVersion = lock.lockInfo?.lockVersion;
154
+ }
155
+
156
+ try {
157
+ // Re-check after lock — another caller may have already refreshed.
158
+ const latest = await getSession(sessionToken);
159
+ if (latest?.idpAccessToken && latest.idpAccessTokenExpires) {
160
+ const latestRemaining = latest.idpAccessTokenExpires - Date.now();
161
+ const latestJwtExp = decodeJwtExp(latest.idpAccessToken);
162
+ const stillStale = latestRemaining <= safetyWindowMs || (latestJwtExp > 0 && latestJwtExp <= Date.now());
163
+ if (!stillStale) {
164
+ return {
165
+ ok: true,
166
+ accessToken: latest.idpAccessToken,
167
+ accessTokenExpires: latest.idpAccessTokenExpires,
168
+ refreshed: true,
169
+ };
170
+ }
171
+ }
172
+
173
+ // Build refresh request body — wire shape must match what the IDP expects.
174
+ let authMethods: string[] = [];
175
+ if (Array.isArray(session.authenticationMethods)) {
176
+ authMethods = session.authenticationMethods;
177
+ } else if (typeof session.authenticationMethods === 'string') {
178
+ try { authMethods = JSON.parse(session.authenticationMethods); } catch { /* fall through */ }
179
+ }
180
+ const isOAuthSession = !!session.oauthProvider;
181
+ if (authMethods.length === 0 && isOAuthSession) {
182
+ authMethods = ['pwd', 'mfa'];
183
+ }
184
+ const twoFactorMethod =
185
+ authMethods.find(m => ['sms', 'totp', 'email'].includes(m)) ||
186
+ session.mfaMethod ||
187
+ (isOAuthSession ? 'oauth' : null);
188
+
189
+ let acrValue = String(session.authenticationLevel ?? '1');
190
+ if (isOAuthSession && session.mfaVerified && acrValue === '1') {
191
+ acrValue = '2';
192
+ }
193
+
194
+ const body: Record<string, unknown> = {
195
+ refresh_token: session.idpRefreshToken,
196
+ amr: authMethods,
197
+ acr: acrValue,
198
+ };
199
+ if (session.mfaVerified) body.two_factor_verified = true;
200
+ if (twoFactorMethod) body.two_factor_method = twoFactorMethod;
201
+ if (session.mfaCompletedAt) body.two_factor_completed_at = new Date(session.mfaCompletedAt).toISOString();
202
+
203
+ let idpResponse: Response;
204
+ try {
205
+ idpResponse = await fetch(`${idpBaseUrl}${refreshEndpoint}`, {
206
+ method: 'POST',
207
+ headers: { 'Content-Type': 'application/json', 'X-Client-Id': clientId },
208
+ body: JSON.stringify(body),
209
+ });
210
+ } catch (err) {
211
+ return {
212
+ ok: false,
213
+ code: 'UPSTREAM_SERVICE_UNAVAILABLE',
214
+ message: err instanceof Error ? err.message : 'IDP unreachable',
215
+ status: 503,
216
+ retryable: true,
217
+ };
218
+ }
219
+
220
+ let responseData: any;
221
+ try {
222
+ const text = await idpResponse.text();
223
+ if (!text.trim()) {
224
+ return { ok: false, code: 'UPSTREAM_SERVICE_ERROR', message: 'Empty response from IDP', status: 502, retryable: true };
225
+ }
226
+ responseData = JSON.parse(text);
227
+ } catch (err) {
228
+ return { ok: false, code: 'UPSTREAM_SERVICE_ERROR', message: 'Invalid JSON from IDP', status: 502, retryable: true };
229
+ }
230
+
231
+ if (!idpResponse.ok) {
232
+ const idpError = responseData?.error || {};
233
+ const code = idpError.code || 'UNKNOWN_ERROR';
234
+ const discardToken = idpResponse.status === 401 || idpError.discard_token === true;
235
+ const retryable = idpResponse.status !== 401 && idpError.retryable === true;
236
+ if (discardToken) {
237
+ await updateSession(sessionToken, {
238
+ idpRefreshToken: '',
239
+ idpRefreshTokenExpires: undefined,
240
+ refreshTokenClearedAt: Date.now(),
241
+ refreshTokenClearedReason: `IDP_DISCARD_TOKEN:${code}`,
242
+ });
243
+ }
244
+ return {
245
+ ok: false,
246
+ code,
247
+ message: idpError.message || 'Token refresh failed',
248
+ status: idpResponse.status,
249
+ discardToken,
250
+ retryable,
251
+ resolution: idpError.resolution,
252
+ terminal: discardToken,
253
+ };
254
+ }
255
+
256
+ // Validate canonical envelope.
257
+ if (
258
+ !responseData ||
259
+ typeof responseData !== 'object' ||
260
+ responseData.success !== true ||
261
+ !responseData.data
262
+ ) {
263
+ return { ok: false, code: 'UPSTREAM_SERVICE_ERROR', message: 'Non-compliant IDP envelope', status: 502, retryable: true };
264
+ }
265
+
266
+ const newAccess = responseData.data.access_token;
267
+ const newRefresh = responseData.data.refresh_token;
268
+ if (!newAccess) {
269
+ return { ok: false, code: 'INTERNAL_SERVER_ERROR', message: 'Missing access token in IDP response', status: 500 };
270
+ }
271
+
272
+ let accessTokenExpires: number;
273
+ let refreshTokenExpires: number | undefined;
274
+ let decoded: any;
275
+ try {
276
+ const r = computeTokenExpiries({ accessToken: newAccess, refreshToken: newRefresh, preferJwt: true });
277
+ decoded = r.decodedAccessToken;
278
+ accessTokenExpires = r.accessTokenExpires;
279
+ refreshTokenExpires = r.refreshTokenExpires;
280
+ } catch {
281
+ return { ok: false, code: 'INTERNAL_SERVER_ERROR', message: 'Failed to decode new tokens', status: 500 };
282
+ }
283
+
284
+ let amrClaims: string[] = [];
285
+ if (decoded?.amr) {
286
+ try {
287
+ amrClaims = typeof decoded.amr === 'string' ? JSON.parse(decoded.amr) : decoded.amr;
288
+ } catch {
289
+ amrClaims = session.authenticationMethods || [];
290
+ }
291
+ } else {
292
+ amrClaims = session.authenticationMethods || [];
293
+ }
294
+ const acrLevel = String(decoded?.acr || session.authenticationLevel || '1');
295
+ const hasNewRefresh = typeof newRefresh === 'string' && newRefresh.length > 0;
296
+ const newKid = extractKidFromToken(newAccess);
297
+
298
+ await updateSession(sessionToken, {
299
+ ...session,
300
+ idpAccessToken: newAccess,
301
+ idpAccessTokenExpires: accessTokenExpires,
302
+ idpRefreshToken: hasNewRefresh ? newRefresh : session.idpRefreshToken,
303
+ idpRefreshTokenExpires: hasNewRefresh ? refreshTokenExpires : session.idpRefreshTokenExpires,
304
+ decodedAccessToken: decoded,
305
+ bearerKeyId: newKid || session.bearerKeyId,
306
+ authenticationMethods: amrClaims,
307
+ authenticationLevel: acrLevel,
308
+ mfaVerified: amrClaims.includes('mfa') || session.mfaVerified,
309
+ mfaCompletedAt: decoded?.mfa_time ? parseInt(decoded.mfa_time) * 1000 : session.mfaCompletedAt,
310
+ mfaExpiresAt: decoded?.mfa_expires ? parseInt(decoded.mfa_expires) * 1000 : session.mfaExpiresAt,
311
+ mfaValidityHours: decoded?.mfa_validity_hours ? parseInt(decoded.mfa_validity_hours) : session.mfaValidityHours,
312
+ });
313
+
314
+ return { ok: true, accessToken: newAccess, accessTokenExpires, refreshed: true };
315
+ } finally {
316
+ if (weHoldLock) {
317
+ await releaseRefreshLock(sessionToken, requestId, releaseVersion);
318
+ }
319
+ }
320
+ }