@payez/next-mvp 4.1.1 → 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.
- package/dist/auth/better-auth.d.ts +3 -0
- package/dist/auth/better-auth.js +22 -1
- package/dist/lib/ensure-fresh-access-token.d.ts +30 -0
- package/dist/lib/ensure-fresh-access-token.js +269 -0
- package/dist/lib/session-store.js +24 -21
- package/dist/lib/token-lifecycle.js +2 -0
- package/dist/models/SessionModel.d.ts +3 -0
- package/dist/models/SessionModel.js +3 -0
- package/dist/routes/auth/session.js +1 -1
- package/dist/server/auth.d.ts +59 -0
- package/dist/server/auth.js +156 -16
- package/dist/server/decode-session.js +2 -0
- package/package.json +6 -1
- package/src/auth/better-auth.ts +434 -408
- package/src/lib/ensure-fresh-access-token.ts +320 -0
- package/src/lib/session-store.ts +692 -689
- package/src/lib/token-lifecycle.ts +470 -468
- package/src/models/SessionModel.ts +264 -258
- package/src/routes/auth/session.ts +166 -166
- package/src/server/auth.ts +272 -78
- package/src/server/decode-session.ts +202 -200
package/dist/auth/better-auth.js
CHANGED
|
@@ -69,10 +69,28 @@ function buildBetterAuthProviders(config) {
|
|
|
69
69
|
if (!oauth.enabled)
|
|
70
70
|
continue;
|
|
71
71
|
const name = oauth.provider.toLowerCase();
|
|
72
|
+
const additionalParams = oauth.additionalParams ?? {};
|
|
73
|
+
const rawPrompt = additionalParams.prompt;
|
|
74
|
+
const rawAccessType = additionalParams.accessType ?? additionalParams.access_type;
|
|
75
|
+
const rawHostedDomain = additionalParams.hd;
|
|
76
|
+
// Ensure profile scope is present for Google so avatar image is returned
|
|
77
|
+
const scopes = oauth.scopes?.split(' ') || [];
|
|
78
|
+
if (name === 'google' && !scopes.includes('profile')) {
|
|
79
|
+
scopes.push('profile');
|
|
80
|
+
}
|
|
72
81
|
providers[name] = {
|
|
73
82
|
clientId: oauth.clientId,
|
|
74
83
|
clientSecret: oauth.clientSecret,
|
|
75
|
-
scope:
|
|
84
|
+
scope: scopes.length > 0 ? scopes : undefined,
|
|
85
|
+
// Google is overly eager to reuse the last account unless we
|
|
86
|
+
// explicitly ask for account selection on each social login.
|
|
87
|
+
prompt: typeof rawPrompt === 'string'
|
|
88
|
+
? rawPrompt
|
|
89
|
+
: name === 'google'
|
|
90
|
+
? 'select_account'
|
|
91
|
+
: undefined,
|
|
92
|
+
accessType: rawAccessType === 'online' ? 'online' : rawAccessType === 'offline' ? 'offline' : undefined,
|
|
93
|
+
hd: typeof rawHostedDomain === 'string' ? rawHostedDomain : undefined,
|
|
76
94
|
};
|
|
77
95
|
}
|
|
78
96
|
return providers;
|
|
@@ -301,8 +319,11 @@ async function exchangeOAuthForIdpTokens(sessionToken, provider = 'google') {
|
|
|
301
319
|
userId: String(result.user?.user_id || result.user?.id || result.user_id || baUserId),
|
|
302
320
|
email: result.user?.email || result.email || email,
|
|
303
321
|
name: result.user?.full_name || result.user?.name || result.name || name,
|
|
322
|
+
image: image,
|
|
304
323
|
roles: result.user?.roles || result.roles || [],
|
|
305
324
|
mfaVerified: !requiresTwoFactor,
|
|
325
|
+
idpClientId: result.client_id ? String(result.client_id) : undefined,
|
|
326
|
+
merchantId: result.merchant_id ? String(result.merchant_id) : undefined,
|
|
306
327
|
};
|
|
307
328
|
// Store in BA Redis session (for decodeSession)
|
|
308
329
|
baData.idpTokens = idpTokenData;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export interface EnsureFreshConfig {
|
|
2
|
+
idpBaseUrl: string;
|
|
3
|
+
clientId: string;
|
|
4
|
+
refreshEndpoint?: string;
|
|
5
|
+
}
|
|
6
|
+
export interface EnsureFreshOptions {
|
|
7
|
+
/** Refresh if the access token is within this many ms of expiry. Default 60_000. */
|
|
8
|
+
safetyWindowMs?: number;
|
|
9
|
+
/** Max wait while another caller holds the refresh lock. Default 5000. */
|
|
10
|
+
lockWaitMs?: number;
|
|
11
|
+
/** Optional caller request id for lock attribution. */
|
|
12
|
+
requestId?: string;
|
|
13
|
+
}
|
|
14
|
+
export type EnsureFreshResult = {
|
|
15
|
+
ok: true;
|
|
16
|
+
accessToken: string;
|
|
17
|
+
accessTokenExpires: number;
|
|
18
|
+
/** True if we refreshed (or a concurrent refresh completed); false if the stored token was already fresh. */
|
|
19
|
+
refreshed: boolean;
|
|
20
|
+
} | {
|
|
21
|
+
ok: false;
|
|
22
|
+
code: string;
|
|
23
|
+
message: string;
|
|
24
|
+
status: number;
|
|
25
|
+
terminal?: boolean;
|
|
26
|
+
discardToken?: boolean;
|
|
27
|
+
retryable?: boolean;
|
|
28
|
+
resolution?: string;
|
|
29
|
+
};
|
|
30
|
+
export declare function ensureFreshAccessToken(sessionToken: string, config: EnsureFreshConfig, options?: EnsureFreshOptions): Promise<EnsureFreshResult>;
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ensureFreshAccessToken = ensureFreshAccessToken;
|
|
4
|
+
/**
|
|
5
|
+
* ensureFreshAccessToken — server-side preflight refresh.
|
|
6
|
+
*
|
|
7
|
+
* Returns a non-expired IDP access token for a given session. Refreshes
|
|
8
|
+
* proactively when the stored token is within the safety window of expiry,
|
|
9
|
+
* using the same Redis lock and IDP wire shape as `createRefreshHandler`.
|
|
10
|
+
*
|
|
11
|
+
* Designed for proxy-route auth helpers that today read the stored token
|
|
12
|
+
* blindly and let the backend reject it with a 401. Calling this instead of
|
|
13
|
+
* `getSession(...).idpAccessToken` means a good token client never sends
|
|
14
|
+
* credentials it already knows are invalid.
|
|
15
|
+
*
|
|
16
|
+
* Single-use refresh-token semantics are preserved via Redis-backed
|
|
17
|
+
* single-flight locking (see `acquireRefreshLock`).
|
|
18
|
+
*/
|
|
19
|
+
const session_store_1 = require("./session-store");
|
|
20
|
+
const token_expiry_1 = require("./token-expiry");
|
|
21
|
+
const token_utils_1 = require("../auth/utils/token-utils");
|
|
22
|
+
const DEFAULT_SAFETY_WINDOW_MS = 60_000;
|
|
23
|
+
const DEFAULT_LOCK_WAIT_MS = 5000;
|
|
24
|
+
function decodeJwtExp(token) {
|
|
25
|
+
const parts = token.split('.');
|
|
26
|
+
if (parts.length !== 3)
|
|
27
|
+
return -1;
|
|
28
|
+
try {
|
|
29
|
+
const payload = JSON.parse(Buffer.from(parts[1], 'base64url').toString());
|
|
30
|
+
return (payload.exp || 0) * 1000;
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return -1;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
async function ensureFreshAccessToken(sessionToken, config, options = {}) {
|
|
37
|
+
const { idpBaseUrl, clientId, refreshEndpoint = '/api/ExternalAuth/refresh' } = config;
|
|
38
|
+
const safetyWindowMs = options.safetyWindowMs ?? DEFAULT_SAFETY_WINDOW_MS;
|
|
39
|
+
const lockWaitMs = options.lockWaitMs ?? DEFAULT_LOCK_WAIT_MS;
|
|
40
|
+
const requestId = options.requestId ?? `ensure_fresh_${Date.now()}`;
|
|
41
|
+
const session = await (0, session_store_1.getSession)(sessionToken);
|
|
42
|
+
if (!session) {
|
|
43
|
+
return { ok: false, code: 'NO_SESSION', message: 'No session for token', status: 401, terminal: true };
|
|
44
|
+
}
|
|
45
|
+
const storedToken = session.idpAccessToken;
|
|
46
|
+
if (!storedToken) {
|
|
47
|
+
return { ok: false, code: 'NO_TOKEN', message: 'No IDP access token in session', status: 401, terminal: true };
|
|
48
|
+
}
|
|
49
|
+
const now = Date.now();
|
|
50
|
+
const redisExpires = session.idpAccessTokenExpires ?? 0;
|
|
51
|
+
const jwtExpires = decodeJwtExp(storedToken);
|
|
52
|
+
// Use the smaller of the two to be conservative — Redis can be stale, JWT exp is authoritative.
|
|
53
|
+
const effectiveExpires = jwtExpires > 0 ? Math.min(redisExpires || jwtExpires, jwtExpires) : redisExpires;
|
|
54
|
+
const msUntilExpiry = effectiveExpires - now;
|
|
55
|
+
if (msUntilExpiry > safetyWindowMs) {
|
|
56
|
+
return { ok: true, accessToken: storedToken, accessTokenExpires: effectiveExpires, refreshed: false };
|
|
57
|
+
}
|
|
58
|
+
if (!session.idpRefreshToken) {
|
|
59
|
+
return {
|
|
60
|
+
ok: false,
|
|
61
|
+
code: 'NO_REFRESH_TOKEN',
|
|
62
|
+
message: 'Access token expired and no refresh token available',
|
|
63
|
+
status: 401,
|
|
64
|
+
terminal: true,
|
|
65
|
+
resolution: 'User must re-authenticate',
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
const lock = await (0, session_store_1.acquireRefreshLock)(sessionToken, requestId, lockWaitMs);
|
|
69
|
+
let weHoldLock = false;
|
|
70
|
+
let releaseVersion;
|
|
71
|
+
if (!lock.acquired) {
|
|
72
|
+
const existing = await (0, session_store_1.checkRefreshLock)(sessionToken);
|
|
73
|
+
if (!existing || existing.acquiredBy !== requestId) {
|
|
74
|
+
const startWait = Date.now();
|
|
75
|
+
while (Date.now() - startWait < lockWaitMs) {
|
|
76
|
+
await new Promise(r => setTimeout(r, 200));
|
|
77
|
+
const stillLocked = await (0, session_store_1.checkRefreshLock)(sessionToken);
|
|
78
|
+
if (!stillLocked) {
|
|
79
|
+
const after = await (0, session_store_1.getSession)(sessionToken);
|
|
80
|
+
if (after?.idpAccessToken && after.idpAccessTokenExpires) {
|
|
81
|
+
const remaining = after.idpAccessTokenExpires - Date.now();
|
|
82
|
+
if (remaining > safetyWindowMs) {
|
|
83
|
+
return {
|
|
84
|
+
ok: true,
|
|
85
|
+
accessToken: after.idpAccessToken,
|
|
86
|
+
accessTokenExpires: after.idpAccessTokenExpires,
|
|
87
|
+
refreshed: true,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return {
|
|
95
|
+
ok: false,
|
|
96
|
+
code: 'CONFLICT',
|
|
97
|
+
message: 'Refresh already in progress',
|
|
98
|
+
status: 409,
|
|
99
|
+
retryable: true,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
weHoldLock = true;
|
|
105
|
+
releaseVersion = lock.lockInfo?.lockVersion;
|
|
106
|
+
}
|
|
107
|
+
try {
|
|
108
|
+
// Re-check after lock — another caller may have already refreshed.
|
|
109
|
+
const latest = await (0, session_store_1.getSession)(sessionToken);
|
|
110
|
+
if (latest?.idpAccessToken && latest.idpAccessTokenExpires) {
|
|
111
|
+
const latestRemaining = latest.idpAccessTokenExpires - Date.now();
|
|
112
|
+
const latestJwtExp = decodeJwtExp(latest.idpAccessToken);
|
|
113
|
+
const stillStale = latestRemaining <= safetyWindowMs || (latestJwtExp > 0 && latestJwtExp <= Date.now());
|
|
114
|
+
if (!stillStale) {
|
|
115
|
+
return {
|
|
116
|
+
ok: true,
|
|
117
|
+
accessToken: latest.idpAccessToken,
|
|
118
|
+
accessTokenExpires: latest.idpAccessTokenExpires,
|
|
119
|
+
refreshed: true,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
// Build refresh request body — wire shape must match what the IDP expects.
|
|
124
|
+
let authMethods = [];
|
|
125
|
+
if (Array.isArray(session.authenticationMethods)) {
|
|
126
|
+
authMethods = session.authenticationMethods;
|
|
127
|
+
}
|
|
128
|
+
else if (typeof session.authenticationMethods === 'string') {
|
|
129
|
+
try {
|
|
130
|
+
authMethods = JSON.parse(session.authenticationMethods);
|
|
131
|
+
}
|
|
132
|
+
catch { /* fall through */ }
|
|
133
|
+
}
|
|
134
|
+
const isOAuthSession = !!session.oauthProvider;
|
|
135
|
+
if (authMethods.length === 0 && isOAuthSession) {
|
|
136
|
+
authMethods = ['pwd', 'mfa'];
|
|
137
|
+
}
|
|
138
|
+
const twoFactorMethod = authMethods.find(m => ['sms', 'totp', 'email'].includes(m)) ||
|
|
139
|
+
session.mfaMethod ||
|
|
140
|
+
(isOAuthSession ? 'oauth' : null);
|
|
141
|
+
let acrValue = String(session.authenticationLevel ?? '1');
|
|
142
|
+
if (isOAuthSession && session.mfaVerified && acrValue === '1') {
|
|
143
|
+
acrValue = '2';
|
|
144
|
+
}
|
|
145
|
+
const body = {
|
|
146
|
+
refresh_token: session.idpRefreshToken,
|
|
147
|
+
amr: authMethods,
|
|
148
|
+
acr: acrValue,
|
|
149
|
+
};
|
|
150
|
+
if (session.mfaVerified)
|
|
151
|
+
body.two_factor_verified = true;
|
|
152
|
+
if (twoFactorMethod)
|
|
153
|
+
body.two_factor_method = twoFactorMethod;
|
|
154
|
+
if (session.mfaCompletedAt)
|
|
155
|
+
body.two_factor_completed_at = new Date(session.mfaCompletedAt).toISOString();
|
|
156
|
+
let idpResponse;
|
|
157
|
+
try {
|
|
158
|
+
idpResponse = await fetch(`${idpBaseUrl}${refreshEndpoint}`, {
|
|
159
|
+
method: 'POST',
|
|
160
|
+
headers: { 'Content-Type': 'application/json', 'X-Client-Id': clientId },
|
|
161
|
+
body: JSON.stringify(body),
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
catch (err) {
|
|
165
|
+
return {
|
|
166
|
+
ok: false,
|
|
167
|
+
code: 'UPSTREAM_SERVICE_UNAVAILABLE',
|
|
168
|
+
message: err instanceof Error ? err.message : 'IDP unreachable',
|
|
169
|
+
status: 503,
|
|
170
|
+
retryable: true,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
let responseData;
|
|
174
|
+
try {
|
|
175
|
+
const text = await idpResponse.text();
|
|
176
|
+
if (!text.trim()) {
|
|
177
|
+
return { ok: false, code: 'UPSTREAM_SERVICE_ERROR', message: 'Empty response from IDP', status: 502, retryable: true };
|
|
178
|
+
}
|
|
179
|
+
responseData = JSON.parse(text);
|
|
180
|
+
}
|
|
181
|
+
catch (err) {
|
|
182
|
+
return { ok: false, code: 'UPSTREAM_SERVICE_ERROR', message: 'Invalid JSON from IDP', status: 502, retryable: true };
|
|
183
|
+
}
|
|
184
|
+
if (!idpResponse.ok) {
|
|
185
|
+
const idpError = responseData?.error || {};
|
|
186
|
+
const code = idpError.code || 'UNKNOWN_ERROR';
|
|
187
|
+
const discardToken = idpResponse.status === 401 || idpError.discard_token === true;
|
|
188
|
+
const retryable = idpResponse.status !== 401 && idpError.retryable === true;
|
|
189
|
+
if (discardToken) {
|
|
190
|
+
await (0, session_store_1.updateSession)(sessionToken, {
|
|
191
|
+
idpRefreshToken: '',
|
|
192
|
+
idpRefreshTokenExpires: undefined,
|
|
193
|
+
refreshTokenClearedAt: Date.now(),
|
|
194
|
+
refreshTokenClearedReason: `IDP_DISCARD_TOKEN:${code}`,
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
return {
|
|
198
|
+
ok: false,
|
|
199
|
+
code,
|
|
200
|
+
message: idpError.message || 'Token refresh failed',
|
|
201
|
+
status: idpResponse.status,
|
|
202
|
+
discardToken,
|
|
203
|
+
retryable,
|
|
204
|
+
resolution: idpError.resolution,
|
|
205
|
+
terminal: discardToken,
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
// Validate canonical envelope.
|
|
209
|
+
if (!responseData ||
|
|
210
|
+
typeof responseData !== 'object' ||
|
|
211
|
+
responseData.success !== true ||
|
|
212
|
+
!responseData.data) {
|
|
213
|
+
return { ok: false, code: 'UPSTREAM_SERVICE_ERROR', message: 'Non-compliant IDP envelope', status: 502, retryable: true };
|
|
214
|
+
}
|
|
215
|
+
const newAccess = responseData.data.access_token;
|
|
216
|
+
const newRefresh = responseData.data.refresh_token;
|
|
217
|
+
if (!newAccess) {
|
|
218
|
+
return { ok: false, code: 'INTERNAL_SERVER_ERROR', message: 'Missing access token in IDP response', status: 500 };
|
|
219
|
+
}
|
|
220
|
+
let accessTokenExpires;
|
|
221
|
+
let refreshTokenExpires;
|
|
222
|
+
let decoded;
|
|
223
|
+
try {
|
|
224
|
+
const r = (0, token_expiry_1.computeTokenExpiries)({ accessToken: newAccess, refreshToken: newRefresh, preferJwt: true });
|
|
225
|
+
decoded = r.decodedAccessToken;
|
|
226
|
+
accessTokenExpires = r.accessTokenExpires;
|
|
227
|
+
refreshTokenExpires = r.refreshTokenExpires;
|
|
228
|
+
}
|
|
229
|
+
catch {
|
|
230
|
+
return { ok: false, code: 'INTERNAL_SERVER_ERROR', message: 'Failed to decode new tokens', status: 500 };
|
|
231
|
+
}
|
|
232
|
+
let amrClaims = [];
|
|
233
|
+
if (decoded?.amr) {
|
|
234
|
+
try {
|
|
235
|
+
amrClaims = typeof decoded.amr === 'string' ? JSON.parse(decoded.amr) : decoded.amr;
|
|
236
|
+
}
|
|
237
|
+
catch {
|
|
238
|
+
amrClaims = session.authenticationMethods || [];
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
amrClaims = session.authenticationMethods || [];
|
|
243
|
+
}
|
|
244
|
+
const acrLevel = String(decoded?.acr || session.authenticationLevel || '1');
|
|
245
|
+
const hasNewRefresh = typeof newRefresh === 'string' && newRefresh.length > 0;
|
|
246
|
+
const newKid = (0, token_utils_1.extractKidFromToken)(newAccess);
|
|
247
|
+
await (0, session_store_1.updateSession)(sessionToken, {
|
|
248
|
+
...session,
|
|
249
|
+
idpAccessToken: newAccess,
|
|
250
|
+
idpAccessTokenExpires: accessTokenExpires,
|
|
251
|
+
idpRefreshToken: hasNewRefresh ? newRefresh : session.idpRefreshToken,
|
|
252
|
+
idpRefreshTokenExpires: hasNewRefresh ? refreshTokenExpires : session.idpRefreshTokenExpires,
|
|
253
|
+
decodedAccessToken: decoded,
|
|
254
|
+
bearerKeyId: newKid || session.bearerKeyId,
|
|
255
|
+
authenticationMethods: amrClaims,
|
|
256
|
+
authenticationLevel: acrLevel,
|
|
257
|
+
mfaVerified: amrClaims.includes('mfa') || session.mfaVerified,
|
|
258
|
+
mfaCompletedAt: decoded?.mfa_time ? parseInt(decoded.mfa_time) * 1000 : session.mfaCompletedAt,
|
|
259
|
+
mfaExpiresAt: decoded?.mfa_expires ? parseInt(decoded.mfa_expires) * 1000 : session.mfaExpiresAt,
|
|
260
|
+
mfaValidityHours: decoded?.mfa_validity_hours ? parseInt(decoded.mfa_validity_hours) : session.mfaValidityHours,
|
|
261
|
+
});
|
|
262
|
+
return { ok: true, accessToken: newAccess, accessTokenExpires, refreshed: true };
|
|
263
|
+
}
|
|
264
|
+
finally {
|
|
265
|
+
if (weHoldLock) {
|
|
266
|
+
await (0, session_store_1.releaseRefreshLock)(sessionToken, requestId, releaseVersion);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
@@ -126,11 +126,14 @@ async function getBetterAuthSession(sessionToken, appSlug) {
|
|
|
126
126
|
userId: data.user.id || data.user.email,
|
|
127
127
|
email: data.user.email,
|
|
128
128
|
name: data.user.name,
|
|
129
|
+
image: data.user.image,
|
|
129
130
|
idpAccessToken: data.idpTokens?.idpAccessToken,
|
|
130
131
|
idpRefreshToken: data.idpTokens?.idpRefreshToken,
|
|
131
132
|
idpAccessTokenExpires: data.idpTokens?.idpAccessTokenExpires,
|
|
132
133
|
mfaVerified: data.idpTokens?.mfaVerified ?? false,
|
|
133
134
|
roles: data.idpTokens?.roles || [],
|
|
135
|
+
idpClientId: data.idpTokens?.idpClientId ?? data.idpTokens?.clientId ?? data.idpClientId,
|
|
136
|
+
merchantId: data.idpTokens?.merchantId ?? data.merchantId,
|
|
134
137
|
};
|
|
135
138
|
}
|
|
136
139
|
return data;
|
|
@@ -482,27 +485,27 @@ async function releaseRefreshLock(sessionToken, requestId, lockVersion) {
|
|
|
482
485
|
const lockKey = getRefreshLockKey(sessionToken);
|
|
483
486
|
try {
|
|
484
487
|
// Lua script for atomic lock validation and release
|
|
485
|
-
const luaScript = `
|
|
486
|
-
local lockKey = KEYS[1]
|
|
487
|
-
local expectedRequestId = ARGV[1]
|
|
488
|
-
local expectedVersion = ARGV[2]
|
|
489
|
-
|
|
490
|
-
local lockData = redis.call('GET', lockKey)
|
|
491
|
-
if not lockData then
|
|
492
|
-
return 0 -- Lock doesn't exist
|
|
493
|
-
end
|
|
494
|
-
|
|
495
|
-
local lockInfo = cjson.decode(lockData)
|
|
496
|
-
if lockInfo.acquiredBy == expectedRequestId then
|
|
497
|
-
if not expectedVersion or expectedVersion == '' or tostring(lockInfo.lockVersion) == expectedVersion then
|
|
498
|
-
redis.call('DEL', lockKey)
|
|
499
|
-
return 1 -- Successfully released
|
|
500
|
-
else
|
|
501
|
-
return -2 -- Version mismatch
|
|
502
|
-
end
|
|
503
|
-
else
|
|
504
|
-
return -1 -- Wrong owner
|
|
505
|
-
end
|
|
488
|
+
const luaScript = `
|
|
489
|
+
local lockKey = KEYS[1]
|
|
490
|
+
local expectedRequestId = ARGV[1]
|
|
491
|
+
local expectedVersion = ARGV[2]
|
|
492
|
+
|
|
493
|
+
local lockData = redis.call('GET', lockKey)
|
|
494
|
+
if not lockData then
|
|
495
|
+
return 0 -- Lock doesn't exist
|
|
496
|
+
end
|
|
497
|
+
|
|
498
|
+
local lockInfo = cjson.decode(lockData)
|
|
499
|
+
if lockInfo.acquiredBy == expectedRequestId then
|
|
500
|
+
if not expectedVersion or expectedVersion == '' or tostring(lockInfo.lockVersion) == expectedVersion then
|
|
501
|
+
redis.call('DEL', lockKey)
|
|
502
|
+
return 1 -- Successfully released
|
|
503
|
+
else
|
|
504
|
+
return -2 -- Version mismatch
|
|
505
|
+
end
|
|
506
|
+
else
|
|
507
|
+
return -1 -- Wrong owner
|
|
508
|
+
end
|
|
506
509
|
`;
|
|
507
510
|
const result = await redis_1.default.eval(luaScript, 1, lockKey, requestId, lockVersion ? lockVersion.toString() : '');
|
|
508
511
|
if (result === 1) {
|
|
@@ -243,6 +243,8 @@ async function ensureFreshToken(request) {
|
|
|
243
243
|
|| (baSession.session?.expiresAt ? new Date(baSession.session.expiresAt).getTime() : Date.now() + 24 * 60 * 60 * 1000),
|
|
244
244
|
mfaVerified: true,
|
|
245
245
|
oauthProvider: 'google',
|
|
246
|
+
idpClientId: idpTokens?.idpClientId ?? idpTokens?.clientId ?? baSession.idpClientId,
|
|
247
|
+
merchantId: idpTokens?.merchantId ?? baSession.merchantId,
|
|
246
248
|
};
|
|
247
249
|
}
|
|
248
250
|
}
|
|
@@ -26,6 +26,8 @@ export interface SessionData {
|
|
|
26
26
|
email: string;
|
|
27
27
|
/** Display name (from OAuth profile or IDP) */
|
|
28
28
|
name?: string;
|
|
29
|
+
/** Avatar image URL (from OAuth profile) */
|
|
30
|
+
image?: string;
|
|
29
31
|
/** User's roles/permissions */
|
|
30
32
|
roles: string[];
|
|
31
33
|
/** IDP access token (JWT) - used for API calls to PayEz services */
|
|
@@ -83,6 +85,7 @@ export declare class SessionModel {
|
|
|
83
85
|
userId: string;
|
|
84
86
|
email: string;
|
|
85
87
|
name?: string;
|
|
88
|
+
image?: string;
|
|
86
89
|
roles: string[];
|
|
87
90
|
idpAccessToken?: string;
|
|
88
91
|
idpRefreshToken?: string;
|
|
@@ -29,6 +29,7 @@ class SessionModel {
|
|
|
29
29
|
userId;
|
|
30
30
|
email;
|
|
31
31
|
name;
|
|
32
|
+
image;
|
|
32
33
|
roles;
|
|
33
34
|
// IDP Tokens
|
|
34
35
|
idpAccessToken;
|
|
@@ -57,6 +58,7 @@ class SessionModel {
|
|
|
57
58
|
this.userId = data.userId;
|
|
58
59
|
this.email = data.email;
|
|
59
60
|
this.name = data.name;
|
|
61
|
+
this.image = data.image;
|
|
60
62
|
this.roles = data.roles || [];
|
|
61
63
|
// IDP Tokens
|
|
62
64
|
this.idpAccessToken = data.idpAccessToken;
|
|
@@ -111,6 +113,7 @@ class SessionModel {
|
|
|
111
113
|
userId: this.userId,
|
|
112
114
|
email: this.email,
|
|
113
115
|
name: this.name,
|
|
116
|
+
image: this.image,
|
|
114
117
|
roles: this.roles,
|
|
115
118
|
idpAccessToken: this.idpAccessToken,
|
|
116
119
|
idpRefreshToken: this.idpRefreshToken,
|
|
@@ -59,7 +59,7 @@ async function GET(req) {
|
|
|
59
59
|
id: session?.userId || authSession.user?.id,
|
|
60
60
|
email: session?.email || authSession.user?.email,
|
|
61
61
|
name: session?.name || authSession.user?.name,
|
|
62
|
-
image: authSession.user?.image || null,
|
|
62
|
+
image: authSession.user?.image || session?.image || null,
|
|
63
63
|
// Redis session data
|
|
64
64
|
roles: session?.roles || [],
|
|
65
65
|
twoFactorSessionVerified: session?.mfaVerified || false,
|
package/dist/server/auth.d.ts
CHANGED
|
@@ -5,6 +5,16 @@
|
|
|
5
5
|
* getAuthInstance(); use getSession(req) for the request-scoped session.
|
|
6
6
|
*/
|
|
7
7
|
import 'server-only';
|
|
8
|
+
import { type SessionData } from '../lib/session-store';
|
|
9
|
+
export type IdpTokenResult = {
|
|
10
|
+
success: true;
|
|
11
|
+
accessToken: string;
|
|
12
|
+
sessionData: SessionData;
|
|
13
|
+
} | {
|
|
14
|
+
success: false;
|
|
15
|
+
error: 'NO_SESSION' | 'NO_TOKEN';
|
|
16
|
+
terminal: true;
|
|
17
|
+
};
|
|
8
18
|
/**
|
|
9
19
|
* Get the initialized Better Auth instance (singleton).
|
|
10
20
|
*/
|
|
@@ -149,6 +159,55 @@ export declare function getAuthInstance(): Promise<import("better-auth/types").A
|
|
|
149
159
|
* Returns the session object or null if not authenticated.
|
|
150
160
|
*/
|
|
151
161
|
export declare function getSession(request?: Request): Promise<any>;
|
|
162
|
+
/**
|
|
163
|
+
* Get normalized session data for the current request.
|
|
164
|
+
*
|
|
165
|
+
* This prefers the app's Redis session because it carries the canonical
|
|
166
|
+
* IDP token, roles, and tenant-specific user identity used by app routes.
|
|
167
|
+
*/
|
|
168
|
+
export declare function getSessionData(request?: Request): Promise<SessionData | null>;
|
|
169
|
+
/**
|
|
170
|
+
* Get the current request's IDP access token without triggering a refresh.
|
|
171
|
+
*
|
|
172
|
+
* Use this for routes that only need the currently-issued bearer token and
|
|
173
|
+
* should fail closed instead of performing token lifecycle work. For backend
|
|
174
|
+
* proxy routes that forward the token to a downstream API, prefer
|
|
175
|
+
* `getFreshIdpToken` — it preflights expiry and refreshes single-flight, so
|
|
176
|
+
* the proxy never sends a credential it already knows is invalid.
|
|
177
|
+
*/
|
|
178
|
+
export declare function getIdpToken(request?: Request): Promise<IdpTokenResult>;
|
|
179
|
+
export type FreshIdpTokenResult = {
|
|
180
|
+
success: true;
|
|
181
|
+
accessToken: string;
|
|
182
|
+
sessionData: SessionData;
|
|
183
|
+
refreshed: boolean;
|
|
184
|
+
} | {
|
|
185
|
+
success: false;
|
|
186
|
+
error: string;
|
|
187
|
+
status: number;
|
|
188
|
+
terminal?: boolean;
|
|
189
|
+
discardToken?: boolean;
|
|
190
|
+
retryable?: boolean;
|
|
191
|
+
resolution?: string;
|
|
192
|
+
};
|
|
193
|
+
export interface FreshIdpTokenConfig {
|
|
194
|
+
idpBaseUrl: string;
|
|
195
|
+
clientId: string;
|
|
196
|
+
refreshEndpoint?: string;
|
|
197
|
+
/** Refresh if the access token is within this many ms of expiry. Default 60_000. */
|
|
198
|
+
safetyWindowMs?: number;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Get the current request's IDP access token, preflight-refreshing if it is
|
|
202
|
+
* expired or within the safety window. Single-flight via Redis lock, so
|
|
203
|
+
* concurrent calls on the same session share one IDP round-trip and one
|
|
204
|
+
* single-use refresh-token consumption.
|
|
205
|
+
*
|
|
206
|
+
* Use this in proxy routes. The returned `accessToken` is safe to forward to
|
|
207
|
+
* a downstream API without expecting a 401. If `success` is false, surface a
|
|
208
|
+
* 401/redirect — there is no recoverable token for this session.
|
|
209
|
+
*/
|
|
210
|
+
export declare function getFreshIdpToken(request: Request | undefined, config: FreshIdpTokenConfig): Promise<FreshIdpTokenResult>;
|
|
152
211
|
/**
|
|
153
212
|
* Get the current session, throwing if not authenticated.
|
|
154
213
|
* Use in API handlers that require auth.
|