@igxjs/node-components 1.0.10 → 1.0.12
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/README.md +100 -6
- package/components/assets/template.html +111 -0
- package/components/http-handlers.js +46 -33
- package/components/jwt.js +8 -6
- package/components/logger.js +131 -0
- package/components/redis.js +18 -10
- package/components/session.js +580 -110
- package/index.d.ts +127 -18
- package/index.js +2 -1
- package/package.json +3 -3
package/index.d.ts
CHANGED
|
@@ -5,6 +5,78 @@ import { EncryptJWT, JWTDecryptResult, JWTPayload } from 'jose';
|
|
|
5
5
|
import { RedisClientType } from '@redis/client';
|
|
6
6
|
import { Application, RequestHandler, Request, Response, NextFunction, Router } from 'express';
|
|
7
7
|
|
|
8
|
+
export { JWTPayload } from 'jose';
|
|
9
|
+
|
|
10
|
+
// Logger class for configurable logging
|
|
11
|
+
export class Logger {
|
|
12
|
+
/**
|
|
13
|
+
* Get or create a Logger instance (singleton pattern)
|
|
14
|
+
* @param componentName Component name for log prefix
|
|
15
|
+
* @param enableLogging Enable/disable logging (defaults to NODE_ENV !== 'production')
|
|
16
|
+
* @returns Logger instance
|
|
17
|
+
*/
|
|
18
|
+
static getInstance(componentName: string, enableLogging?: boolean): Logger;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Clear all logger instances (useful for testing)
|
|
22
|
+
*/
|
|
23
|
+
static clearInstances(): void;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Disable colors globally for all logger instances
|
|
27
|
+
*/
|
|
28
|
+
static disableColors(): void;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Enable colors globally for all logger instances
|
|
32
|
+
*/
|
|
33
|
+
static enableColors(): void;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Create a new Logger instance (backward compatibility)
|
|
37
|
+
* Note: Use Logger.getInstance() for singleton pattern
|
|
38
|
+
* @param componentName Component name for log prefix
|
|
39
|
+
* @param enableLogging Enable/disable logging (defaults to NODE_ENV !== 'production')
|
|
40
|
+
*/
|
|
41
|
+
constructor(componentName: string, enableLogging?: boolean);
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Log debug message
|
|
45
|
+
* @param args Arguments to log
|
|
46
|
+
*/
|
|
47
|
+
debug(...args: any[]): void;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Log info message
|
|
51
|
+
* @param args Arguments to log
|
|
52
|
+
*/
|
|
53
|
+
info(...args: any[]): void;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Log warning message
|
|
57
|
+
* @param args Arguments to log
|
|
58
|
+
*/
|
|
59
|
+
warn(...args: any[]): void;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Log error message
|
|
63
|
+
* @param args Arguments to log
|
|
64
|
+
*/
|
|
65
|
+
error(...args: any[]): void;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Log general message
|
|
69
|
+
* @param args Arguments to log
|
|
70
|
+
*/
|
|
71
|
+
log(...args: any[]): void;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Session Mode constants
|
|
75
|
+
export const SessionMode: {
|
|
76
|
+
SESSION: string;
|
|
77
|
+
TOKEN: string;
|
|
78
|
+
};
|
|
79
|
+
|
|
8
80
|
// Session Configuration - uses strict UPPERCASE naming convention for all property names
|
|
9
81
|
export interface SessionConfig {
|
|
10
82
|
/** Identity Provider */
|
|
@@ -14,13 +86,27 @@ export interface SessionConfig {
|
|
|
14
86
|
SSO_SUCCESS_URL?: string;
|
|
15
87
|
SSO_FAILURE_URL?: string;
|
|
16
88
|
|
|
89
|
+
/** Authentication mode: 'session' or 'token' (default: 'session') */
|
|
90
|
+
SESSION_MODE?: string;
|
|
91
|
+
|
|
17
92
|
SESSION_AGE?: number;
|
|
18
93
|
SESSION_COOKIE_PATH?: string;
|
|
19
94
|
SESSION_SECRET?: string;
|
|
20
95
|
SESSION_PREFIX?: string;
|
|
96
|
+
SESSION_KEY?: string;
|
|
97
|
+
SESSION_EXPIRY_KEY?: string;
|
|
98
|
+
TOKEN_STORAGE_TEMPLATE_PATH?: string;
|
|
21
99
|
|
|
22
100
|
REDIS_URL?: string;
|
|
23
101
|
REDIS_CERT_PATH?: string;
|
|
102
|
+
|
|
103
|
+
JWT_ALGORITHM?: string;
|
|
104
|
+
JWT_ENCRYPTION?: string;
|
|
105
|
+
JWT_CLOCK_TOLERANCE?: number;
|
|
106
|
+
JWT_SECRET_HASH_ALGORITHM?: string;
|
|
107
|
+
JWT_ISSUER?: string;
|
|
108
|
+
JWT_AUDIENCE?: string;
|
|
109
|
+
JWT_SUBJECT?: string;
|
|
24
110
|
}
|
|
25
111
|
|
|
26
112
|
export interface SessionUserAttributes {
|
|
@@ -98,15 +184,35 @@ export class SessionManager {
|
|
|
98
184
|
): Promise<void>;
|
|
99
185
|
|
|
100
186
|
/**
|
|
101
|
-
* Resource protection middleware
|
|
187
|
+
* Resource protection middleware based on configured SESSION_MODE
|
|
188
|
+
* Uses verifySession() for SESSION mode and verifyToken() for TOKEN mode
|
|
102
189
|
* @param isDebugging Debugging flag (default: false)
|
|
103
190
|
* @param redirectUrl Redirect URL (default: '')
|
|
104
191
|
* @returns Returns express Request Handler
|
|
105
192
|
*/
|
|
106
193
|
authenticate(isDebugging?: boolean, redirectUrl?: string): RequestHandler;
|
|
107
194
|
|
|
195
|
+
/**
|
|
196
|
+
* Resource protection by token (explicit token verification)
|
|
197
|
+
* Requires Authorization: Bearer {token} header
|
|
198
|
+
* @param isDebugging Debugging flag (default: false)
|
|
199
|
+
* @param redirectUrl Redirect URL (default: '')
|
|
200
|
+
* @returns Returns express Request Handler
|
|
201
|
+
*/
|
|
202
|
+
verifyToken(isDebugging?: boolean, redirectUrl?: string): RequestHandler;
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Resource protection by session (explicit session verification)
|
|
206
|
+
* @param isDebugging Debugging flag (default: false)
|
|
207
|
+
* @param redirectUrl Redirect URL (default: '')
|
|
208
|
+
* @returns Returns express Request Handler
|
|
209
|
+
*/
|
|
210
|
+
verifySession(isDebugging?: boolean, redirectUrl?: string): RequestHandler;
|
|
211
|
+
|
|
108
212
|
/**
|
|
109
213
|
* SSO callback for successful login
|
|
214
|
+
* SESSION mode: Saves session and redirects
|
|
215
|
+
* TOKEN mode: Generates JWT token, returns HTML page with localStorage script
|
|
110
216
|
* @param initUser Initialize user object function
|
|
111
217
|
* @returns Returns express Request Handler
|
|
112
218
|
*/
|
|
@@ -119,17 +225,22 @@ export class SessionManager {
|
|
|
119
225
|
identityProviders(): RequestHandler;
|
|
120
226
|
|
|
121
227
|
/**
|
|
122
|
-
*
|
|
228
|
+
* Refresh user authentication based on configured SESSION_MODE
|
|
229
|
+
* SESSION mode: Refreshes session data
|
|
230
|
+
* TOKEN mode: Generates new token, invalidates old token
|
|
231
|
+
* @param initUser Initialize user object function
|
|
123
232
|
* @returns Returns express Request Handler
|
|
124
233
|
*/
|
|
125
|
-
|
|
234
|
+
refresh(initUser: (user: SessionUser) => SessionUser): RequestHandler;
|
|
126
235
|
|
|
127
236
|
/**
|
|
128
|
-
*
|
|
129
|
-
*
|
|
237
|
+
* Application logout based on configured SESSION_MODE (NOT SSO)
|
|
238
|
+
* SESSION mode: Destroys session and clears cookie
|
|
239
|
+
* TOKEN mode: Invalidates current token or all tokens (with ?all=true query param)
|
|
240
|
+
* Query params: redirect=true (redirect to success/failure URL), all=true (logout all tokens - TOKEN mode only)
|
|
130
241
|
* @returns Returns express Request Handler
|
|
131
242
|
*/
|
|
132
|
-
|
|
243
|
+
logout(): RequestHandler;
|
|
133
244
|
}
|
|
134
245
|
|
|
135
246
|
// Custom Error class
|
|
@@ -196,32 +307,29 @@ export class RedisManager {
|
|
|
196
307
|
* Disconnect from Redis
|
|
197
308
|
* @returns Returns nothing
|
|
198
309
|
*/
|
|
199
|
-
|
|
310
|
+
disconnect(): Promise<void>;
|
|
200
311
|
}
|
|
201
312
|
|
|
202
313
|
// JWT Manager Configuration - uses strict UPPERCASE naming convention with JWT_ prefix for all property names
|
|
203
314
|
export interface JwtManagerOptions {
|
|
204
315
|
/** JWE algorithm (default: 'dir') */
|
|
205
316
|
JWT_ALGORITHM?: string;
|
|
206
|
-
|
|
317
|
+
|
|
207
318
|
/** JWE encryption method (default: 'A256GCM') */
|
|
208
319
|
JWT_ENCRYPTION?: string;
|
|
209
|
-
|
|
210
|
-
/** Token expiration time (default: '10m') */
|
|
211
|
-
JWT_EXPIRATION_TIME?: string;
|
|
212
|
-
|
|
320
|
+
|
|
213
321
|
/** Clock tolerance in seconds for token validation (default: 30) */
|
|
214
322
|
JWT_CLOCK_TOLERANCE?: number;
|
|
215
|
-
|
|
323
|
+
|
|
216
324
|
/** Hash algorithm for secret derivation (default: 'SHA-256') */
|
|
217
325
|
JWT_SECRET_HASH_ALGORITHM?: string;
|
|
218
|
-
|
|
326
|
+
|
|
219
327
|
/** Optional JWT issuer claim */
|
|
220
328
|
JWT_ISSUER?: string;
|
|
221
|
-
|
|
329
|
+
|
|
222
330
|
/** Optional JWT audience claim */
|
|
223
331
|
JWT_AUDIENCE?: string;
|
|
224
|
-
|
|
332
|
+
|
|
225
333
|
/** Optional JWT subject claim */
|
|
226
334
|
JWT_SUBJECT?: string;
|
|
227
335
|
}
|
|
@@ -237,7 +345,7 @@ export interface JwtEncryptOptions {
|
|
|
237
345
|
encryption?: string;
|
|
238
346
|
|
|
239
347
|
/** Override default expiration time */
|
|
240
|
-
expirationTime?:
|
|
348
|
+
expirationTime?: number;
|
|
241
349
|
|
|
242
350
|
/** Override default hash algorithm */
|
|
243
351
|
secretHashAlgorithm?: string;
|
|
@@ -273,11 +381,12 @@ export interface JwtDecryptOptions {
|
|
|
273
381
|
}
|
|
274
382
|
|
|
275
383
|
export type JwtDecryptResult = JWTDecryptResult<EncryptJWT>;
|
|
384
|
+
|
|
276
385
|
// JwtManager class for JWT encryption and decryption
|
|
277
386
|
export class JwtManager {
|
|
278
387
|
algorithm: string;
|
|
279
388
|
encryption: string;
|
|
280
|
-
expirationTime:
|
|
389
|
+
expirationTime: number;
|
|
281
390
|
clockTolerance: number;
|
|
282
391
|
secretHashAlgorithm: string;
|
|
283
392
|
issuer?: string;
|
package/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
export { SessionConfig, SessionManager } from './components/session.js';
|
|
1
|
+
export { SessionConfig, SessionManager, SessionMode } from './components/session.js';
|
|
2
2
|
export { httpCodes, httpMessages, httpErrorHandler, httpNotFoundHandler, CustomError, httpHelper, httpError } from './components/http-handlers.js';
|
|
3
3
|
export { RedisManager } from './components/redis.js';
|
|
4
4
|
export { FlexRouter } from './components/router.js';
|
|
5
5
|
export { JwtManager } from './components/jwt.js';
|
|
6
|
+
export { Logger } from './components/logger.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@igxjs/node-components",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.12",
|
|
4
4
|
"description": "Node components for igxjs",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -26,14 +26,14 @@
|
|
|
26
26
|
"axios": "^1.13.6",
|
|
27
27
|
"connect-redis": "^9.0.0",
|
|
28
28
|
"express-session": "^1.19.0",
|
|
29
|
-
"jose": "^6.2.
|
|
29
|
+
"jose": "^6.2.1",
|
|
30
30
|
"memorystore": "^1.6.7"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"chai": "^6.2.2",
|
|
34
34
|
"express": "^5.2.1",
|
|
35
35
|
"mocha": "^12.0.0-beta-10",
|
|
36
|
-
"sinon": "^21.0.
|
|
36
|
+
"sinon": "^21.0.3",
|
|
37
37
|
"supertest": "^7.0.0"
|
|
38
38
|
},
|
|
39
39
|
"files": [
|