@igxjs/node-components 1.0.8 → 1.0.10
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/components/http-handlers.js +6 -0
- package/components/jwt.js +89 -45
- package/components/session.js +36 -18
- package/index.d.ts +64 -40
- package/package.json +10 -3
- package/.github/workflows/node.js.yml +0 -31
- package/.github/workflows/npm-publish.yml +0 -33
- package/docs/README.md +0 -54
- package/docs/flex-router.md +0 -167
- package/docs/http-handlers.md +0 -302
- package/docs/jwt-manager.md +0 -124
- package/docs/redis-manager.md +0 -210
- package/docs/session-manager.md +0 -160
- package/tests/http-handlers.test.js +0 -21
- package/tests/jwt.test.js +0 -345
- package/tests/redis.test.js +0 -50
- package/tests/router.test.js +0 -50
- package/tests/session.test.js +0 -116
package/docs/flex-router.md
DELETED
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
# FlexRouter
|
|
2
|
-
|
|
3
|
-
A flexible routing utility for Express.js that allows mounting routers with custom context paths and middleware handlers.
|
|
4
|
-
|
|
5
|
-
## Overview
|
|
6
|
-
|
|
7
|
-
FlexRouter provides a convenient way to organize and mount Express routers with predefined context paths and optional middleware. This is particularly useful for:
|
|
8
|
-
- API versioning (e.g., `/api/v1`, `/api/v2`)
|
|
9
|
-
- Separating public and protected routes
|
|
10
|
-
- Applying middleware to specific route groups
|
|
11
|
-
- Managing complex routing hierarchies
|
|
12
|
-
|
|
13
|
-
## Usage Example
|
|
14
|
-
|
|
15
|
-
```javascript
|
|
16
|
-
import { Router } from 'express';
|
|
17
|
-
import { FlexRouter } from '@igxjs/node-components';
|
|
18
|
-
// Assuming you have an authenticate middleware
|
|
19
|
-
// import { authenticate } from './middlewares/auth.js';
|
|
20
|
-
|
|
21
|
-
// Create routers
|
|
22
|
-
const publicRouter = Router();
|
|
23
|
-
const privateRouter = Router();
|
|
24
|
-
|
|
25
|
-
publicRouter.get('/health', (req, res) => {
|
|
26
|
-
res.send('OK');
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
privateRouter.get('/users', (req, res) => {
|
|
30
|
-
res.json({ users: [] });
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
// Define flex routers with context paths and optional middleware
|
|
34
|
-
export const routers = [
|
|
35
|
-
new FlexRouter('/api/v1/public', publicRouter),
|
|
36
|
-
new FlexRouter('/api/v1/protected', privateRouter, [authenticate]), // with middleware
|
|
37
|
-
];
|
|
38
|
-
|
|
39
|
-
// Mount all routers to your Express app
|
|
40
|
-
const app = express();
|
|
41
|
-
const basePath = '';
|
|
42
|
-
|
|
43
|
-
routers.forEach(router => {
|
|
44
|
-
router.mount(app, basePath);
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
// Routes will be available at:
|
|
48
|
-
// - /api/v1/public/health
|
|
49
|
-
// - /api/v1/protected/users (with authenticate middleware)
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
## Advanced Usage
|
|
53
|
-
|
|
54
|
-
### Multiple Middleware
|
|
55
|
-
|
|
56
|
-
You can apply multiple middleware functions to a FlexRouter:
|
|
57
|
-
|
|
58
|
-
```javascript
|
|
59
|
-
import { rateLimiter } from './middlewares/rate-limiter.js';
|
|
60
|
-
import { logger } from './middlewares/logger.js';
|
|
61
|
-
import { authenticate } from './middlewares/auth.js';
|
|
62
|
-
|
|
63
|
-
const apiRouter = Router();
|
|
64
|
-
|
|
65
|
-
apiRouter.get('/data', (req, res) => {
|
|
66
|
-
res.json({ [] });
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
// Apply multiple middleware in order
|
|
70
|
-
const protectedApi = new FlexRouter(
|
|
71
|
-
'/api/v1',
|
|
72
|
-
apiRouter,
|
|
73
|
-
[logger, rateLimiter, authenticate] // Applied in this order
|
|
74
|
-
);
|
|
75
|
-
|
|
76
|
-
protectedApi.mount(app, '');
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
### Using Base Paths
|
|
80
|
-
|
|
81
|
-
You can add a base path when mounting routers, useful for multi-tenant applications:
|
|
82
|
-
|
|
83
|
-
```javascript
|
|
84
|
-
const tenantRouter = Router();
|
|
85
|
-
|
|
86
|
-
tenantRouter.get('/dashboard', (req, res) => {
|
|
87
|
-
res.json({ tenant: req.params.tenantId });
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
const flexRouter = new FlexRouter('/api', tenantRouter);
|
|
91
|
-
|
|
92
|
-
// Mount with tenant-specific base path
|
|
93
|
-
flexRouter.mount(app, '/tenant/:tenantId');
|
|
94
|
-
|
|
95
|
-
// Route will be available at: /tenant/:tenantId/api/dashboard
|
|
96
|
-
```
|
|
97
|
-
|
|
98
|
-
### Organizing Routes by Feature
|
|
99
|
-
|
|
100
|
-
```javascript
|
|
101
|
-
// features/users/routes.js
|
|
102
|
-
const usersRouter = Router();
|
|
103
|
-
usersRouter.get('/', getAllUsers);
|
|
104
|
-
usersRouter.post('/', createUser);
|
|
105
|
-
export const usersFlexRouter = new FlexRouter('/users', usersRouter);
|
|
106
|
-
|
|
107
|
-
// features/products/routes.js
|
|
108
|
-
const productsRouter = Router();
|
|
109
|
-
productsRouter.get('/', getAllProducts);
|
|
110
|
-
export const productsFlexRouter = new FlexRouter('/products', productsRouter);
|
|
111
|
-
|
|
112
|
-
// app.js
|
|
113
|
-
import { usersFlexRouter } from './features/users/routes.js';
|
|
114
|
-
import { productsFlexRouter } from './features/products/routes.js';
|
|
115
|
-
|
|
116
|
-
const apiRouters = [usersFlexRouter, productsFlexRouter];
|
|
117
|
-
|
|
118
|
-
apiRouters.forEach(router => router.mount(app, '/api/v1'));
|
|
119
|
-
|
|
120
|
-
// Routes available:
|
|
121
|
-
// - /api/v1/users
|
|
122
|
-
// - /api/v1/products
|
|
123
|
-
```
|
|
124
|
-
|
|
125
|
-
## API
|
|
126
|
-
|
|
127
|
-
### Constructor
|
|
128
|
-
|
|
129
|
-
**`new FlexRouter(context, router, handlers?)`**
|
|
130
|
-
|
|
131
|
-
Creates a new FlexRouter instance.
|
|
132
|
-
|
|
133
|
-
**Parameters:**
|
|
134
|
-
- `context` (string) - The context path for the router (e.g., `/api/v1`)
|
|
135
|
-
- `router` (Router) - Express Router instance containing your route definitions
|
|
136
|
-
- `handlers` (Array, optional) - Array of middleware handler functions to apply to all routes in this router
|
|
137
|
-
|
|
138
|
-
**Returns:** FlexRouter instance
|
|
139
|
-
|
|
140
|
-
### Methods
|
|
141
|
-
|
|
142
|
-
**`mount(app, basePath)`**
|
|
143
|
-
|
|
144
|
-
Mounts the router to an Express application with the specified base path.
|
|
145
|
-
|
|
146
|
-
**Parameters:**
|
|
147
|
-
- `app` (Express) - Express application instance
|
|
148
|
-
- `basePath` (string) - Base path to prepend to the context path
|
|
149
|
-
|
|
150
|
-
**Example:**
|
|
151
|
-
```javascript
|
|
152
|
-
const flexRouter = new FlexRouter('/api', router);
|
|
153
|
-
flexRouter.mount(app, '/v1'); // Mounts at /v1/api
|
|
154
|
-
```
|
|
155
|
-
|
|
156
|
-
## Best Practices
|
|
157
|
-
|
|
158
|
-
1. **Group Related Routes**: Use FlexRouter to group related routes together
|
|
159
|
-
2. **Apply Middleware Strategically**: Apply middleware at the FlexRouter level for route groups that share the same requirements
|
|
160
|
-
3. **Consistent Naming**: Use consistent naming conventions for context paths (e.g., all lowercase, kebab-case)
|
|
161
|
-
4. **Version Your APIs**: Use FlexRouter to manage different API versions easily
|
|
162
|
-
5. **Keep Routers Focused**: Each router should handle a specific feature or resource type
|
|
163
|
-
|
|
164
|
-
## Related Documentation
|
|
165
|
-
|
|
166
|
-
- [SessionManager](./session-manager.md) - For authentication middleware usage
|
|
167
|
-
- [Back to main documentation](../README.md)
|
package/docs/http-handlers.md
DELETED
|
@@ -1,302 +0,0 @@
|
|
|
1
|
-
# HTTP Handlers
|
|
2
|
-
|
|
3
|
-
Custom error handling utilities with standardized HTTP status codes and error responses for Express.js applications.
|
|
4
|
-
|
|
5
|
-
## Overview
|
|
6
|
-
|
|
7
|
-
The HTTP Handlers module provides:
|
|
8
|
-
- Standardized error handling middleware
|
|
9
|
-
- Custom error classes
|
|
10
|
-
- HTTP status codes and messages constants
|
|
11
|
-
- 404 handler for unmatched routes
|
|
12
|
-
- Utility functions for error formatting
|
|
13
|
-
- Axios error handling helpers
|
|
14
|
-
|
|
15
|
-
## Available Exports
|
|
16
|
-
|
|
17
|
-
```javascript
|
|
18
|
-
import {
|
|
19
|
-
httpCodes, // HTTP status code constants
|
|
20
|
-
httpMessages, // HTTP status message constants
|
|
21
|
-
httpErrorHandler, // Error handling middleware
|
|
22
|
-
httpNotFoundHandler, // 404 handler middleware
|
|
23
|
-
CustomError, // Custom error class
|
|
24
|
-
httpHelper, // Utility functions
|
|
25
|
-
httpError // Error factory function
|
|
26
|
-
} from '@igxjs/node-components';
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
## Quick Start
|
|
30
|
-
|
|
31
|
-
```javascript
|
|
32
|
-
import express from 'express';
|
|
33
|
-
import {
|
|
34
|
-
httpCodes,
|
|
35
|
-
httpErrorHandler,
|
|
36
|
-
httpNotFoundHandler,
|
|
37
|
-
httpError
|
|
38
|
-
} from '@igxjs/node-components';
|
|
39
|
-
|
|
40
|
-
const app = express();
|
|
41
|
-
|
|
42
|
-
// Your routes
|
|
43
|
-
app.get('/api/data', async (req, res, next) => {
|
|
44
|
-
try {
|
|
45
|
-
const data = await fetchData();
|
|
46
|
-
res.json(data);
|
|
47
|
-
} catch (error) {
|
|
48
|
-
next(httpError(httpCodes.SYSTEM_FAILURE, 'Failed to fetch data', error));
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
// Add 404 handler before error handler
|
|
53
|
-
app.use(httpNotFoundHandler);
|
|
54
|
-
|
|
55
|
-
// Add error handler as the last middleware
|
|
56
|
-
app.use(httpErrorHandler);
|
|
57
|
-
|
|
58
|
-
app.listen(3000);
|
|
59
|
-
```
|
|
60
|
-
|
|
61
|
-
## HTTP Status Codes
|
|
62
|
-
|
|
63
|
-
The `httpCodes` object provides constants for common HTTP status codes:
|
|
64
|
-
|
|
65
|
-
```javascript
|
|
66
|
-
import { httpCodes } from '@igxjs/node-components';
|
|
67
|
-
|
|
68
|
-
// Success codes
|
|
69
|
-
httpCodes.OK // 200
|
|
70
|
-
httpCodes.CREATED // 201
|
|
71
|
-
httpCodes.NO_CONTENT // 204
|
|
72
|
-
|
|
73
|
-
// Client error codes
|
|
74
|
-
httpCodes.BAD_REQUEST // 400
|
|
75
|
-
httpCodes.UNAUTHORIZED // 401
|
|
76
|
-
httpCodes.FORBIDDEN // 403
|
|
77
|
-
httpCodes.NOT_FOUND // 404
|
|
78
|
-
httpCodes.NOT_ACCEPTABLE // 406
|
|
79
|
-
httpCodes.CONFLICT // 409
|
|
80
|
-
httpCodes.LOCKED // 423
|
|
81
|
-
|
|
82
|
-
// Server error codes
|
|
83
|
-
httpCodes.SYSTEM_FAILURE // 500
|
|
84
|
-
httpCodes.NOT_IMPLEMENTED // 501
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
## HTTP Status Messages
|
|
88
|
-
|
|
89
|
-
Corresponding status messages are available via `httpMessages`:
|
|
90
|
-
|
|
91
|
-
```javascript
|
|
92
|
-
import { httpMessages } from '@igxjs/node-components';
|
|
93
|
-
|
|
94
|
-
httpMessages.OK // 'OK'
|
|
95
|
-
httpMessages.BAD_REQUEST // 'Bad Request'
|
|
96
|
-
httpMessages.UNAUTHORIZED // 'Unauthorized'
|
|
97
|
-
// ... etc.
|
|
98
|
-
```
|
|
99
|
-
|
|
100
|
-
## CustomError Class
|
|
101
|
-
|
|
102
|
-
Create custom errors with specific HTTP status codes:
|
|
103
|
-
|
|
104
|
-
```javascript
|
|
105
|
-
import { CustomError, httpCodes } from '@igxjs/node-components';
|
|
106
|
-
|
|
107
|
-
// Constructor: new CustomError(code, message, error?, data?)
|
|
108
|
-
throw new CustomError(
|
|
109
|
-
httpCodes.BAD_REQUEST,
|
|
110
|
-
'Email is required',
|
|
111
|
-
null,
|
|
112
|
-
{ field: 'email' }
|
|
113
|
-
);
|
|
114
|
-
```
|
|
115
|
-
|
|
116
|
-
**Properties:**
|
|
117
|
-
- `code` (number) - HTTP status code
|
|
118
|
-
- `message` (string) - Error message
|
|
119
|
-
- `error` (object, optional) - Original error object
|
|
120
|
-
- `data` (object, optional) - Additional error data
|
|
121
|
-
|
|
122
|
-
## httpError Function
|
|
123
|
-
|
|
124
|
-
Convenience function to create CustomError instances:
|
|
125
|
-
|
|
126
|
-
```javascript
|
|
127
|
-
import { httpError, httpCodes } from '@igxjs/node-components';
|
|
128
|
-
|
|
129
|
-
// Same as: new CustomError(code, message, error, data)
|
|
130
|
-
throw httpError(
|
|
131
|
-
httpCodes.UNAUTHORIZED,
|
|
132
|
-
'Invalid credentials',
|
|
133
|
-
originalError,
|
|
134
|
-
{ attempted: username }
|
|
135
|
-
);
|
|
136
|
-
```
|
|
137
|
-
|
|
138
|
-
## Middleware
|
|
139
|
-
|
|
140
|
-
### httpErrorHandler
|
|
141
|
-
|
|
142
|
-
Express error handling middleware that processes CustomError and other errors:
|
|
143
|
-
|
|
144
|
-
```javascript
|
|
145
|
-
import { httpErrorHandler } from '@igxjs/node-components';
|
|
146
|
-
|
|
147
|
-
// Add as the last middleware
|
|
148
|
-
app.use(httpErrorHandler);
|
|
149
|
-
```
|
|
150
|
-
|
|
151
|
-
**Features:**
|
|
152
|
-
- Automatically handles `CustomError` instances
|
|
153
|
-
- Sets appropriate HTTP status codes
|
|
154
|
-
- Adds CORS headers
|
|
155
|
-
- Logs error details to console
|
|
156
|
-
- Returns standardized JSON error responses
|
|
157
|
-
|
|
158
|
-
**Response Format:**
|
|
159
|
-
```json
|
|
160
|
-
{
|
|
161
|
-
"error": {
|
|
162
|
-
"code": 400,
|
|
163
|
-
"message": "Email is required",
|
|
164
|
-
"data": { "field": "email" }
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
```
|
|
168
|
-
|
|
169
|
-
### httpNotFoundHandler
|
|
170
|
-
|
|
171
|
-
Middleware that catches all unmatched routes and returns 404:
|
|
172
|
-
|
|
173
|
-
```javascript
|
|
174
|
-
import { httpNotFoundHandler } from '@igxjs/node-components';
|
|
175
|
-
|
|
176
|
-
// Add before error handler
|
|
177
|
-
app.use(httpNotFoundHandler);
|
|
178
|
-
app.use(httpErrorHandler);
|
|
179
|
-
```
|
|
180
|
-
|
|
181
|
-
## httpHelper Utilities
|
|
182
|
-
|
|
183
|
-
### `handleAxiosError(error, defaultMessage?)`
|
|
184
|
-
|
|
185
|
-
Analyze and convert Axios/HTTP errors to CustomError:
|
|
186
|
-
|
|
187
|
-
```javascript
|
|
188
|
-
import axios from 'axios';
|
|
189
|
-
import { httpHelper } from '@igxjs/node-components';
|
|
190
|
-
|
|
191
|
-
try {
|
|
192
|
-
const response = await axios.get('https://api.example.com/data');
|
|
193
|
-
return response.data;
|
|
194
|
-
} catch (error) {
|
|
195
|
-
// Converts Axios error to CustomError with extracted status and message
|
|
196
|
-
throw httpHelper.handleAxiosError(error, 'API request failed');
|
|
197
|
-
}
|
|
198
|
-
```
|
|
199
|
-
|
|
200
|
-
### `format(str, ...args)`
|
|
201
|
-
|
|
202
|
-
Format strings with placeholders:
|
|
203
|
-
|
|
204
|
-
```javascript
|
|
205
|
-
import { httpHelper } from '@igxjs/node-components';
|
|
206
|
-
|
|
207
|
-
const message = httpHelper.format(
|
|
208
|
-
'User {0} not found in {1}',
|
|
209
|
-
'john@example.com',
|
|
210
|
-
'database'
|
|
211
|
-
);
|
|
212
|
-
// Result: 'User john@example.com not found in database'
|
|
213
|
-
```
|
|
214
|
-
|
|
215
|
-
### `toZodMessage(error)`
|
|
216
|
-
|
|
217
|
-
Generate friendly Zod validation error messages:
|
|
218
|
-
|
|
219
|
-
```javascript
|
|
220
|
-
import { z } from 'zod';
|
|
221
|
-
import { httpHelper, httpError, httpCodes } from '@igxjs/node-components';
|
|
222
|
-
|
|
223
|
-
const userSchema = z.object({
|
|
224
|
-
email: z.string().email(),
|
|
225
|
-
age: z.number().min(18)
|
|
226
|
-
});
|
|
227
|
-
|
|
228
|
-
app.post('/api/users', (req, res, next) => {
|
|
229
|
-
try {
|
|
230
|
-
const validated = userSchema.parse(req.body);
|
|
231
|
-
// Process validated data
|
|
232
|
-
} catch (error) {
|
|
233
|
-
if (error instanceof z.ZodError) {
|
|
234
|
-
const message = httpHelper.toZodMessage(error);
|
|
235
|
-
throw httpError(httpCodes.BAD_REQUEST, message, error);
|
|
236
|
-
}
|
|
237
|
-
next(error);
|
|
238
|
-
}
|
|
239
|
-
});
|
|
240
|
-
```
|
|
241
|
-
|
|
242
|
-
## Complete Example
|
|
243
|
-
|
|
244
|
-
```javascript
|
|
245
|
-
import express from 'express';
|
|
246
|
-
import {
|
|
247
|
-
httpCodes,
|
|
248
|
-
httpMessages,
|
|
249
|
-
httpError,
|
|
250
|
-
httpErrorHandler,
|
|
251
|
-
httpNotFoundHandler,
|
|
252
|
-
CustomError
|
|
253
|
-
} from '@igxjs/node-components';
|
|
254
|
-
|
|
255
|
-
const app = express();
|
|
256
|
-
app.use(express.json());
|
|
257
|
-
|
|
258
|
-
// Routes
|
|
259
|
-
app.get('/api/health', (req, res) => {
|
|
260
|
-
res.json({ status: httpMessages.OK });
|
|
261
|
-
});
|
|
262
|
-
|
|
263
|
-
app.post('/api/login', async (req, res, next) => {
|
|
264
|
-
try {
|
|
265
|
-
const { username, password } = req.body;
|
|
266
|
-
|
|
267
|
-
if (!username || !password) {
|
|
268
|
-
throw httpError(
|
|
269
|
-
httpCodes.BAD_REQUEST,
|
|
270
|
-
'Username and password are required'
|
|
271
|
-
);
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
const user = await authenticate(username, password);
|
|
275
|
-
|
|
276
|
-
if (!user) {
|
|
277
|
-
throw new CustomError(
|
|
278
|
-
httpCodes.UNAUTHORIZED,
|
|
279
|
-
'Invalid credentials'
|
|
280
|
-
);
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
res.json({ user });
|
|
284
|
-
} catch (error) {
|
|
285
|
-
next(error);
|
|
286
|
-
}
|
|
287
|
-
});
|
|
288
|
-
|
|
289
|
-
// 404 handler
|
|
290
|
-
app.use(httpNotFoundHandler);
|
|
291
|
-
|
|
292
|
-
// Error handler (must be last)
|
|
293
|
-
app.use(httpErrorHandler);
|
|
294
|
-
|
|
295
|
-
app.listen(3000);
|
|
296
|
-
```
|
|
297
|
-
|
|
298
|
-
## Related Documentation
|
|
299
|
-
|
|
300
|
-
- [JWT Manager](./jwt-manager.md) - For token-based authentication
|
|
301
|
-
- [SessionManager](./session-manager.md) - For session-based authentication
|
|
302
|
-
- [Back to main documentation](../README.md)
|
package/docs/jwt-manager.md
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
# JWT Manager
|
|
2
|
-
|
|
3
|
-
Provides JWT (JSON Web Token) encryption and decryption utilities using the `jose` library with JWE (JSON Web Encryption) for secure token management.
|
|
4
|
-
|
|
5
|
-
## Overview
|
|
6
|
-
|
|
7
|
-
JwtManager provides secure token-based authentication using:
|
|
8
|
-
- JWE (JSON Web Encryption) for encrypted tokens
|
|
9
|
-
- Configurable expiration times
|
|
10
|
-
- Standard JWT claims (issuer, audience, subject)
|
|
11
|
-
- Clock tolerance for time synchronization issues
|
|
12
|
-
- Easy integration with Express.js applications
|
|
13
|
-
|
|
14
|
-
## Configuration Options
|
|
15
|
-
|
|
16
|
-
```javascript
|
|
17
|
-
// Example configuration object
|
|
18
|
-
const jwtOptions = {
|
|
19
|
-
algorithm: 'dir', // JWE algorithm (default: 'dir')
|
|
20
|
-
encryption: 'A256GCM', // JWE encryption method (default: 'A256GCM')
|
|
21
|
-
expirationTime: '10m', // Token expiration (default: '10m')
|
|
22
|
-
clockTolerance: 30, // Clock tolerance in seconds (default: 30)
|
|
23
|
-
secretHashAlgorithm: 'SHA-256', // Hash algorithm for secret derivation (default: 'SHA-256')
|
|
24
|
-
issuer: 'your-app', // Optional JWT issuer claim
|
|
25
|
-
audience: 'your-users', // Optional JWT audience claim
|
|
26
|
-
subject: 'user-session' // Optional JWT subject claim
|
|
27
|
-
};
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
## Basic Usage
|
|
31
|
-
|
|
32
|
-
```javascript
|
|
33
|
-
import { JwtManager } from '@igxjs/node-components';
|
|
34
|
-
|
|
35
|
-
// Create instance with default configuration
|
|
36
|
-
const jwtManager = new JwtManager({
|
|
37
|
-
expirationTime: '1h',
|
|
38
|
-
issuer: 'my-app',
|
|
39
|
-
audience: 'my-users'
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
// Encrypt user data
|
|
43
|
-
const userData = {
|
|
44
|
-
userId: '12345',
|
|
45
|
-
email: 'user@example.com',
|
|
46
|
-
roles: ['admin', 'user']
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
const secret = 'your-secret-key';
|
|
50
|
-
const token = await jwtManager.encrypt(userData, secret);
|
|
51
|
-
|
|
52
|
-
console.log('Encrypted Token:', token);
|
|
53
|
-
|
|
54
|
-
// Decrypt token
|
|
55
|
-
try {
|
|
56
|
-
const result = await jwtManager.decrypt(token, secret);
|
|
57
|
-
console.log('Decrypted Payload:', result.payload);
|
|
58
|
-
console.log('Protected Header:', result.protectedHeader);
|
|
59
|
-
} catch (error) {
|
|
60
|
-
console.error('Token validation failed:', error);
|
|
61
|
-
}
|
|
62
|
-
```
|
|
63
|
-
|
|
64
|
-
## API Reference
|
|
65
|
-
|
|
66
|
-
### Constructor
|
|
67
|
-
|
|
68
|
-
**`new JwtManager(options?)`**
|
|
69
|
-
|
|
70
|
-
Create a new JwtManager instance. All options are optional with sensible defaults.
|
|
71
|
-
|
|
72
|
-
**Parameters:**
|
|
73
|
-
- `options` (JwtManagerOptions, optional) - Configuration options
|
|
74
|
-
|
|
75
|
-
### Methods
|
|
76
|
-
|
|
77
|
-
#### `encrypt(data, input, options?)`
|
|
78
|
-
|
|
79
|
-
Generate an encrypted JWT token.
|
|
80
|
-
|
|
81
|
-
**Parameters:**
|
|
82
|
-
- `data` (JWTPayload) - User data payload to encrypt
|
|
83
|
-
- `input` (string) - Secret key or password for encryption
|
|
84
|
-
- `options` (JwtEncryptOptions, optional) - Per-call configuration overrides
|
|
85
|
-
|
|
86
|
-
**Returns:** `Promise<string>` - Encrypted JWT token
|
|
87
|
-
|
|
88
|
-
#### `decrypt(token, input, options?)`
|
|
89
|
-
|
|
90
|
-
Decrypt and validate a JWT token.
|
|
91
|
-
|
|
92
|
-
**Parameters:**
|
|
93
|
-
- `token` (string) - JWT token to decrypt
|
|
94
|
-
- `input` (string) - Secret key or password for decryption
|
|
95
|
-
- `options` (JwtDecryptOptions, optional) - Per-call configuration overrides
|
|
96
|
-
|
|
97
|
-
**Returns:** `Promise<JWTDecryptResult>` - Object containing `payload` and `protectedHeader`
|
|
98
|
-
|
|
99
|
-
## Configuration Details
|
|
100
|
-
|
|
101
|
-
### Algorithms
|
|
102
|
-
- `'dir'` (default) - Direct encryption with shared symmetric key
|
|
103
|
-
- `'A128KW'`, `'A192KW'`, `'A256KW'` - AES Key Wrap algorithms
|
|
104
|
-
|
|
105
|
-
### Encryption Methods
|
|
106
|
-
- `'A256GCM'` (default) - AES-GCM with 256-bit key
|
|
107
|
-
- `'A128GCM'`, `'A192GCM'` - AES-GCM with 128/192-bit keys
|
|
108
|
-
|
|
109
|
-
### Expiration Time Format
|
|
110
|
-
- `'10m'` - 10 minutes
|
|
111
|
-
- `'1h'` - 1 hour
|
|
112
|
-
- `'7d'` - 7 days
|
|
113
|
-
- `'30s'` - 30 seconds
|
|
114
|
-
|
|
115
|
-
### JWT Claims
|
|
116
|
-
- `issuer` (iss) - Token issuer identification
|
|
117
|
-
- `audience` (aud) - Intended token recipient
|
|
118
|
-
- `subject` (sub) - Token subject (usually user ID)
|
|
119
|
-
|
|
120
|
-
## Related Documentation
|
|
121
|
-
|
|
122
|
-
- [SessionManager](./session-manager.md) - For SSO-based session management
|
|
123
|
-
- [HTTP Handlers](./http-handlers.md) - For error handling in authentication flows
|
|
124
|
-
- [Back to main documentation](../README.md)
|