@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.
@@ -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
- /** User's roles/permissions */
42
- roles: string[];
43
-
44
- // -------------------------------------------------------------------------
45
- // IDP Tokens (from PayEz Identity Provider)
46
- // -------------------------------------------------------------------------
47
-
48
- /** IDP access token (JWT) - used for API calls to PayEz services */
49
- idpAccessToken?: string;
50
-
51
- /** IDP refresh token - used to get new access tokens */
52
- idpRefreshToken?: string;
53
-
54
- /** When the IDP access token expires (Unix timestamp ms) */
55
- idpAccessTokenExpires: number;
56
-
57
- /** When the IDP refresh token expires (Unix timestamp ms) */
58
- idpRefreshTokenExpires?: number;
59
-
60
- /** Decoded IDP access token claims (for quick access without re-decoding) */
61
- decodedAccessToken?: any;
62
-
63
- /**
64
- * Bearer Key ID (kid from JWT header).
65
- * Identifies which IDP signing key was used for this token.
66
- * CRITICAL: This is from the JWT HEADER, not client_id from payload.
67
- */
68
- bearerKeyId?: string;
69
-
70
- // -------------------------------------------------------------------------
71
- // MFA (Multi-Factor Authentication) State
72
- // -------------------------------------------------------------------------
73
-
74
- /** Whether MFA has been verified for this session */
75
- mfaVerified: boolean;
76
-
77
- /** The MFA method used (email, sms, totp) */
78
- mfaMethod?: 'email' | 'sms' | 'totp';
79
-
80
- /** When MFA was completed (Unix timestamp ms) */
81
- mfaCompletedAt?: number;
82
-
83
- /** When MFA verification expires (Unix timestamp ms) */
84
- mfaExpiresAt?: number;
85
-
86
- /** How long MFA is valid in hours */
87
- mfaValidityHours?: number;
88
-
89
- /** Authentication methods from token (amr claim) */
90
- authenticationMethods?: string[];
91
-
92
- /** Authentication level from token (acr claim) */
93
- authenticationLevel?: string;
94
-
95
- // -------------------------------------------------------------------------
96
- // OAuth Provider Tokens (Google, Microsoft, etc.)
97
- // -------------------------------------------------------------------------
98
-
99
- /** Which OAuth provider was used (google, apple, microsoft, etc.) */
100
- oauthProvider?: string;
101
-
102
- /** Access token from OAuth provider */
103
- oauthProviderToken?: string;
104
-
105
- /** Refresh token from OAuth provider */
106
- oauthProviderRefreshToken?: string;
107
-
108
- // -------------------------------------------------------------------------
109
- // Multi-Tenant IDP Assignment
110
- // -------------------------------------------------------------------------
111
-
112
- /** IDP client ID this user belongs to */
113
- idpClientId?: string;
114
-
115
- /** Merchant ID (typically same as client ID) */
116
- merchantId?: string;
117
-
118
- // -------------------------------------------------------------------------
119
- // Legacy Field Support (for backward compatibility)
120
- // -------------------------------------------------------------------------
121
-
122
- /**
123
- * Allow any additional fields for backward compatibility.
124
- * During migration, old sessions may have legacy field names.
125
- */
126
- [key: string]: any;
127
- }
128
-
129
-
130
- // ============================================================================
131
- // SESSION MODEL CLASS
132
- // ============================================================================
133
-
134
- /**
135
- * Session model class for working with session data.
136
- *
137
- * Provides typed access to session fields with normalized names.
138
- */
139
- export class SessionModel {
140
- // Core Identity
141
- userId: string;
142
- email: string;
143
- name?: string;
144
- roles: string[];
145
-
146
- // IDP Tokens
147
- idpAccessToken?: string;
148
- idpRefreshToken?: string;
149
- idpAccessTokenExpires: number;
150
- idpRefreshTokenExpires?: number;
151
- decodedAccessToken?: any;
152
- bearerKeyId?: string;
153
-
154
- // MFA State
155
- mfaVerified: boolean;
156
- mfaMethod?: 'email' | 'sms' | 'totp';
157
- mfaCompletedAt?: number;
158
- mfaExpiresAt?: number;
159
- mfaValidityHours?: number;
160
- authenticationMethods?: string[];
161
- authenticationLevel?: string;
162
-
163
- // OAuth Provider
164
- oauthProvider?: string;
165
- oauthProviderToken?: string;
166
- oauthProviderRefreshToken?: string;
167
-
168
- // Multi-Tenant
169
- idpClientId?: string;
170
- merchantId?: string;
171
-
172
- constructor(data: SessionData) {
173
- // Core Identity
174
- this.userId = data.userId;
175
- this.email = data.email;
176
- this.name = data.name;
177
- this.roles = data.roles || [];
178
-
179
- // IDP Tokens
180
- this.idpAccessToken = data.idpAccessToken;
181
- this.idpRefreshToken = data.idpRefreshToken;
182
- this.idpAccessTokenExpires = data.idpAccessTokenExpires;
183
- this.idpRefreshTokenExpires = data.idpRefreshTokenExpires;
184
- this.decodedAccessToken = data.decodedAccessToken;
185
- this.bearerKeyId = data.bearerKeyId;
186
-
187
- // MFA State
188
- this.mfaVerified = data.mfaVerified ?? false;
189
- this.mfaMethod = data.mfaMethod;
190
- this.mfaCompletedAt = data.mfaCompletedAt;
191
- this.mfaExpiresAt = data.mfaExpiresAt;
192
- this.mfaValidityHours = data.mfaValidityHours;
193
- this.authenticationMethods = data.authenticationMethods;
194
- this.authenticationLevel = data.authenticationLevel;
195
-
196
- // OAuth Provider
197
- this.oauthProvider = data.oauthProvider;
198
- this.oauthProviderToken = data.oauthProviderToken;
199
- this.oauthProviderRefreshToken = data.oauthProviderRefreshToken;
200
-
201
- // Multi-Tenant
202
- this.idpClientId = data.idpClientId;
203
- this.merchantId = data.merchantId;
204
- }
205
-
206
- /**
207
- * Check if the IDP access token has expired.
208
- */
209
- isAccessTokenExpired(): boolean {
210
- return Date.now() >= this.idpAccessTokenExpires;
211
- }
212
-
213
- /**
214
- * Check if the IDP refresh token has expired.
215
- */
216
- isRefreshTokenExpired(): boolean {
217
- if (!this.idpRefreshTokenExpires) return false;
218
- return Date.now() >= this.idpRefreshTokenExpires;
219
- }
220
-
221
- /**
222
- * Check if MFA has expired.
223
- */
224
- isMfaExpired(): boolean {
225
- if (!this.mfaExpiresAt) return false;
226
- return Date.now() > this.mfaExpiresAt;
227
- }
228
-
229
- /**
230
- * Convert to plain object for storage.
231
- */
232
- toJSON(): SessionData {
233
- return {
234
- userId: this.userId,
235
- email: this.email,
236
- name: this.name,
237
- roles: this.roles,
238
- idpAccessToken: this.idpAccessToken,
239
- idpRefreshToken: this.idpRefreshToken,
240
- idpAccessTokenExpires: this.idpAccessTokenExpires,
241
- idpRefreshTokenExpires: this.idpRefreshTokenExpires,
242
- decodedAccessToken: this.decodedAccessToken,
243
- bearerKeyId: this.bearerKeyId,
244
- mfaVerified: this.mfaVerified,
245
- mfaMethod: this.mfaMethod,
246
- mfaCompletedAt: this.mfaCompletedAt,
247
- mfaExpiresAt: this.mfaExpiresAt,
248
- mfaValidityHours: this.mfaValidityHours,
249
- authenticationMethods: this.authenticationMethods,
250
- authenticationLevel: this.authenticationLevel,
251
- oauthProvider: this.oauthProvider,
252
- oauthProviderToken: this.oauthProviderToken,
253
- oauthProviderRefreshToken: this.oauthProviderRefreshToken,
254
- idpClientId: this.idpClientId,
255
- merchantId: this.merchantId,
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
+ }