@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
|
@@ -1,258 +1,264 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Session Model - Redis Session Data Structure
|
|
3
|
-
*
|
|
4
|
-
* This is the single source of truth for session data stored in Redis.
|
|
5
|
-
* The session contains all authentication state - the JWT cookie only
|
|
6
|
-
* stores the session ID (redisSessionId).
|
|
7
|
-
*
|
|
8
|
-
* FIELD NAMING CONVENTIONS:
|
|
9
|
-
* - idp* prefix: Tokens from PayEz IDP (identity provider)
|
|
10
|
-
* - oauth* prefix: Tokens from external OAuth providers (Google, etc.)
|
|
11
|
-
* - mfa* prefix: Multi-factor authentication related fields
|
|
12
|
-
*
|
|
13
|
-
* @version 2.0.0 - Normalized field names
|
|
14
|
-
* @since auth-refactor-2026-01
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
// ============================================================================
|
|
18
|
-
// NORMALIZED SESSION DATA (v2)
|
|
19
|
-
// ============================================================================
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Session data stored in Redis.
|
|
23
|
-
*
|
|
24
|
-
* This interface uses normalized field names for clarity.
|
|
25
|
-
* All tokens and user data live here - the browser only gets the session ID.
|
|
26
|
-
*/
|
|
27
|
-
export interface SessionData {
|
|
28
|
-
// -------------------------------------------------------------------------
|
|
29
|
-
// Core Identity
|
|
30
|
-
// -------------------------------------------------------------------------
|
|
31
|
-
|
|
32
|
-
/** User ID from IDP (sub claim) */
|
|
33
|
-
userId: string;
|
|
34
|
-
|
|
35
|
-
/** User's email address */
|
|
36
|
-
email: string;
|
|
37
|
-
|
|
38
|
-
/** Display name (from OAuth profile or IDP) */
|
|
39
|
-
name?: string;
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
/** IDP
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
/** When the IDP
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
/** When MFA
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
/** Authentication
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
this.
|
|
181
|
-
this.
|
|
182
|
-
this.
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
this.
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
this.
|
|
189
|
-
this.
|
|
190
|
-
this.
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
this.
|
|
194
|
-
this.
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
this.
|
|
198
|
-
this.
|
|
199
|
-
this.
|
|
200
|
-
|
|
201
|
-
//
|
|
202
|
-
this.
|
|
203
|
-
this.
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Session Model - Redis Session Data Structure
|
|
3
|
+
*
|
|
4
|
+
* This is the single source of truth for session data stored in Redis.
|
|
5
|
+
* The session contains all authentication state - the JWT cookie only
|
|
6
|
+
* stores the session ID (redisSessionId).
|
|
7
|
+
*
|
|
8
|
+
* FIELD NAMING CONVENTIONS:
|
|
9
|
+
* - idp* prefix: Tokens from PayEz IDP (identity provider)
|
|
10
|
+
* - oauth* prefix: Tokens from external OAuth providers (Google, etc.)
|
|
11
|
+
* - mfa* prefix: Multi-factor authentication related fields
|
|
12
|
+
*
|
|
13
|
+
* @version 2.0.0 - Normalized field names
|
|
14
|
+
* @since auth-refactor-2026-01
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
// ============================================================================
|
|
18
|
+
// NORMALIZED SESSION DATA (v2)
|
|
19
|
+
// ============================================================================
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Session data stored in Redis.
|
|
23
|
+
*
|
|
24
|
+
* This interface uses normalized field names for clarity.
|
|
25
|
+
* All tokens and user data live here - the browser only gets the session ID.
|
|
26
|
+
*/
|
|
27
|
+
export interface SessionData {
|
|
28
|
+
// -------------------------------------------------------------------------
|
|
29
|
+
// Core Identity
|
|
30
|
+
// -------------------------------------------------------------------------
|
|
31
|
+
|
|
32
|
+
/** User ID from IDP (sub claim) */
|
|
33
|
+
userId: string;
|
|
34
|
+
|
|
35
|
+
/** User's email address */
|
|
36
|
+
email: string;
|
|
37
|
+
|
|
38
|
+
/** Display name (from OAuth profile or IDP) */
|
|
39
|
+
name?: string;
|
|
40
|
+
|
|
41
|
+
/** Avatar image URL (from OAuth profile) */
|
|
42
|
+
image?: string;
|
|
43
|
+
|
|
44
|
+
/** User's roles/permissions */
|
|
45
|
+
roles: string[];
|
|
46
|
+
|
|
47
|
+
// -------------------------------------------------------------------------
|
|
48
|
+
// IDP Tokens (from PayEz Identity Provider)
|
|
49
|
+
// -------------------------------------------------------------------------
|
|
50
|
+
|
|
51
|
+
/** IDP access token (JWT) - used for API calls to PayEz services */
|
|
52
|
+
idpAccessToken?: string;
|
|
53
|
+
|
|
54
|
+
/** IDP refresh token - used to get new access tokens */
|
|
55
|
+
idpRefreshToken?: string;
|
|
56
|
+
|
|
57
|
+
/** When the IDP access token expires (Unix timestamp ms) */
|
|
58
|
+
idpAccessTokenExpires: number;
|
|
59
|
+
|
|
60
|
+
/** When the IDP refresh token expires (Unix timestamp ms) */
|
|
61
|
+
idpRefreshTokenExpires?: number;
|
|
62
|
+
|
|
63
|
+
/** Decoded IDP access token claims (for quick access without re-decoding) */
|
|
64
|
+
decodedAccessToken?: any;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Bearer Key ID (kid from JWT header).
|
|
68
|
+
* Identifies which IDP signing key was used for this token.
|
|
69
|
+
* CRITICAL: This is from the JWT HEADER, not client_id from payload.
|
|
70
|
+
*/
|
|
71
|
+
bearerKeyId?: string;
|
|
72
|
+
|
|
73
|
+
// -------------------------------------------------------------------------
|
|
74
|
+
// MFA (Multi-Factor Authentication) State
|
|
75
|
+
// -------------------------------------------------------------------------
|
|
76
|
+
|
|
77
|
+
/** Whether MFA has been verified for this session */
|
|
78
|
+
mfaVerified: boolean;
|
|
79
|
+
|
|
80
|
+
/** The MFA method used (email, sms, totp) */
|
|
81
|
+
mfaMethod?: 'email' | 'sms' | 'totp';
|
|
82
|
+
|
|
83
|
+
/** When MFA was completed (Unix timestamp ms) */
|
|
84
|
+
mfaCompletedAt?: number;
|
|
85
|
+
|
|
86
|
+
/** When MFA verification expires (Unix timestamp ms) */
|
|
87
|
+
mfaExpiresAt?: number;
|
|
88
|
+
|
|
89
|
+
/** How long MFA is valid in hours */
|
|
90
|
+
mfaValidityHours?: number;
|
|
91
|
+
|
|
92
|
+
/** Authentication methods from token (amr claim) */
|
|
93
|
+
authenticationMethods?: string[];
|
|
94
|
+
|
|
95
|
+
/** Authentication level from token (acr claim) */
|
|
96
|
+
authenticationLevel?: string;
|
|
97
|
+
|
|
98
|
+
// -------------------------------------------------------------------------
|
|
99
|
+
// OAuth Provider Tokens (Google, Microsoft, etc.)
|
|
100
|
+
// -------------------------------------------------------------------------
|
|
101
|
+
|
|
102
|
+
/** Which OAuth provider was used (google, apple, microsoft, etc.) */
|
|
103
|
+
oauthProvider?: string;
|
|
104
|
+
|
|
105
|
+
/** Access token from OAuth provider */
|
|
106
|
+
oauthProviderToken?: string;
|
|
107
|
+
|
|
108
|
+
/** Refresh token from OAuth provider */
|
|
109
|
+
oauthProviderRefreshToken?: string;
|
|
110
|
+
|
|
111
|
+
// -------------------------------------------------------------------------
|
|
112
|
+
// Multi-Tenant IDP Assignment
|
|
113
|
+
// -------------------------------------------------------------------------
|
|
114
|
+
|
|
115
|
+
/** IDP client ID this user belongs to */
|
|
116
|
+
idpClientId?: string;
|
|
117
|
+
|
|
118
|
+
/** Merchant ID (typically same as client ID) */
|
|
119
|
+
merchantId?: string;
|
|
120
|
+
|
|
121
|
+
// -------------------------------------------------------------------------
|
|
122
|
+
// Legacy Field Support (for backward compatibility)
|
|
123
|
+
// -------------------------------------------------------------------------
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Allow any additional fields for backward compatibility.
|
|
127
|
+
* During migration, old sessions may have legacy field names.
|
|
128
|
+
*/
|
|
129
|
+
[key: string]: any;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
// ============================================================================
|
|
134
|
+
// SESSION MODEL CLASS
|
|
135
|
+
// ============================================================================
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Session model class for working with session data.
|
|
139
|
+
*
|
|
140
|
+
* Provides typed access to session fields with normalized names.
|
|
141
|
+
*/
|
|
142
|
+
export class SessionModel {
|
|
143
|
+
// Core Identity
|
|
144
|
+
userId: string;
|
|
145
|
+
email: string;
|
|
146
|
+
name?: string;
|
|
147
|
+
image?: string;
|
|
148
|
+
roles: string[];
|
|
149
|
+
|
|
150
|
+
// IDP Tokens
|
|
151
|
+
idpAccessToken?: string;
|
|
152
|
+
idpRefreshToken?: string;
|
|
153
|
+
idpAccessTokenExpires: number;
|
|
154
|
+
idpRefreshTokenExpires?: number;
|
|
155
|
+
decodedAccessToken?: any;
|
|
156
|
+
bearerKeyId?: string;
|
|
157
|
+
|
|
158
|
+
// MFA State
|
|
159
|
+
mfaVerified: boolean;
|
|
160
|
+
mfaMethod?: 'email' | 'sms' | 'totp';
|
|
161
|
+
mfaCompletedAt?: number;
|
|
162
|
+
mfaExpiresAt?: number;
|
|
163
|
+
mfaValidityHours?: number;
|
|
164
|
+
authenticationMethods?: string[];
|
|
165
|
+
authenticationLevel?: string;
|
|
166
|
+
|
|
167
|
+
// OAuth Provider
|
|
168
|
+
oauthProvider?: string;
|
|
169
|
+
oauthProviderToken?: string;
|
|
170
|
+
oauthProviderRefreshToken?: string;
|
|
171
|
+
|
|
172
|
+
// Multi-Tenant
|
|
173
|
+
idpClientId?: string;
|
|
174
|
+
merchantId?: string;
|
|
175
|
+
|
|
176
|
+
constructor(data: SessionData) {
|
|
177
|
+
// Core Identity
|
|
178
|
+
this.userId = data.userId;
|
|
179
|
+
this.email = data.email;
|
|
180
|
+
this.name = data.name;
|
|
181
|
+
this.image = data.image;
|
|
182
|
+
this.roles = data.roles || [];
|
|
183
|
+
|
|
184
|
+
// IDP Tokens
|
|
185
|
+
this.idpAccessToken = data.idpAccessToken;
|
|
186
|
+
this.idpRefreshToken = data.idpRefreshToken;
|
|
187
|
+
this.idpAccessTokenExpires = data.idpAccessTokenExpires;
|
|
188
|
+
this.idpRefreshTokenExpires = data.idpRefreshTokenExpires;
|
|
189
|
+
this.decodedAccessToken = data.decodedAccessToken;
|
|
190
|
+
this.bearerKeyId = data.bearerKeyId;
|
|
191
|
+
|
|
192
|
+
// MFA State
|
|
193
|
+
this.mfaVerified = data.mfaVerified ?? false;
|
|
194
|
+
this.mfaMethod = data.mfaMethod;
|
|
195
|
+
this.mfaCompletedAt = data.mfaCompletedAt;
|
|
196
|
+
this.mfaExpiresAt = data.mfaExpiresAt;
|
|
197
|
+
this.mfaValidityHours = data.mfaValidityHours;
|
|
198
|
+
this.authenticationMethods = data.authenticationMethods;
|
|
199
|
+
this.authenticationLevel = data.authenticationLevel;
|
|
200
|
+
|
|
201
|
+
// OAuth Provider
|
|
202
|
+
this.oauthProvider = data.oauthProvider;
|
|
203
|
+
this.oauthProviderToken = data.oauthProviderToken;
|
|
204
|
+
this.oauthProviderRefreshToken = data.oauthProviderRefreshToken;
|
|
205
|
+
|
|
206
|
+
// Multi-Tenant
|
|
207
|
+
this.idpClientId = data.idpClientId;
|
|
208
|
+
this.merchantId = data.merchantId;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Check if the IDP access token has expired.
|
|
213
|
+
*/
|
|
214
|
+
isAccessTokenExpired(): boolean {
|
|
215
|
+
return Date.now() >= this.idpAccessTokenExpires;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Check if the IDP refresh token has expired.
|
|
220
|
+
*/
|
|
221
|
+
isRefreshTokenExpired(): boolean {
|
|
222
|
+
if (!this.idpRefreshTokenExpires) return false;
|
|
223
|
+
return Date.now() >= this.idpRefreshTokenExpires;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Check if MFA has expired.
|
|
228
|
+
*/
|
|
229
|
+
isMfaExpired(): boolean {
|
|
230
|
+
if (!this.mfaExpiresAt) return false;
|
|
231
|
+
return Date.now() > this.mfaExpiresAt;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Convert to plain object for storage.
|
|
236
|
+
*/
|
|
237
|
+
toJSON(): SessionData {
|
|
238
|
+
return {
|
|
239
|
+
userId: this.userId,
|
|
240
|
+
email: this.email,
|
|
241
|
+
name: this.name,
|
|
242
|
+
image: this.image,
|
|
243
|
+
roles: this.roles,
|
|
244
|
+
idpAccessToken: this.idpAccessToken,
|
|
245
|
+
idpRefreshToken: this.idpRefreshToken,
|
|
246
|
+
idpAccessTokenExpires: this.idpAccessTokenExpires,
|
|
247
|
+
idpRefreshTokenExpires: this.idpRefreshTokenExpires,
|
|
248
|
+
decodedAccessToken: this.decodedAccessToken,
|
|
249
|
+
bearerKeyId: this.bearerKeyId,
|
|
250
|
+
mfaVerified: this.mfaVerified,
|
|
251
|
+
mfaMethod: this.mfaMethod,
|
|
252
|
+
mfaCompletedAt: this.mfaCompletedAt,
|
|
253
|
+
mfaExpiresAt: this.mfaExpiresAt,
|
|
254
|
+
mfaValidityHours: this.mfaValidityHours,
|
|
255
|
+
authenticationMethods: this.authenticationMethods,
|
|
256
|
+
authenticationLevel: this.authenticationLevel,
|
|
257
|
+
oauthProvider: this.oauthProvider,
|
|
258
|
+
oauthProviderToken: this.oauthProviderToken,
|
|
259
|
+
oauthProviderRefreshToken: this.oauthProviderRefreshToken,
|
|
260
|
+
idpClientId: this.idpClientId,
|
|
261
|
+
merchantId: this.merchantId,
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
}
|