@ranimontagna/agent-toolkit 0.1.4 → 0.1.5
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 +282 -277
- package/docs/assets/install-plan.svg +29 -0
- package/docs/assets/install-skill-packages.svg +31 -0
- package/docs/assets/install-status.svg +32 -0
- package/package.json +10 -9
- package/setup-agent-toolkit.sh +1 -1
- package/skills/backend/fastify-best-practices/LICENSE +21 -0
- package/skills/backend/fastify-best-practices/NOTICE.md +11 -0
- package/skills/backend/fastify-best-practices/SKILL.md +75 -0
- package/skills/backend/fastify-best-practices/rules/authentication.md +521 -0
- package/skills/backend/fastify-best-practices/rules/configuration.md +217 -0
- package/skills/backend/fastify-best-practices/rules/content-type.md +387 -0
- package/skills/backend/fastify-best-practices/rules/cors-security.md +445 -0
- package/skills/backend/fastify-best-practices/rules/database.md +320 -0
- package/skills/backend/fastify-best-practices/rules/decorators.md +416 -0
- package/skills/backend/fastify-best-practices/rules/deployment.md +423 -0
- package/skills/backend/fastify-best-practices/rules/error-handling.md +412 -0
- package/skills/backend/fastify-best-practices/rules/hooks.md +464 -0
- package/skills/backend/fastify-best-practices/rules/http-proxy.md +247 -0
- package/skills/backend/fastify-best-practices/rules/logging.md +402 -0
- package/skills/backend/fastify-best-practices/rules/performance.md +425 -0
- package/skills/backend/fastify-best-practices/rules/plugins.md +320 -0
- package/skills/backend/fastify-best-practices/rules/routes.md +467 -0
- package/skills/backend/fastify-best-practices/rules/schemas.md +585 -0
- package/skills/backend/fastify-best-practices/rules/serialization.md +475 -0
- package/skills/backend/fastify-best-practices/rules/testing.md +536 -0
- package/skills/backend/fastify-best-practices/rules/typescript.md +458 -0
- package/skills/backend/fastify-best-practices/rules/websockets.md +421 -0
- package/skills/backend/fastify-best-practices/tile.json +11 -0
- package/skills/core/agent-toolkit-maintainer/SKILL.md +16 -14
|
@@ -0,0 +1,521 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: authentication
|
|
3
|
+
description: Authentication and authorization patterns in Fastify
|
|
4
|
+
metadata:
|
|
5
|
+
tags: auth, jwt, session, oauth, security, authorization
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
# Authentication and Authorization
|
|
9
|
+
|
|
10
|
+
## JWT Authentication with @fastify/jwt
|
|
11
|
+
|
|
12
|
+
Use `@fastify/jwt` for JSON Web Token authentication:
|
|
13
|
+
|
|
14
|
+
```typescript
|
|
15
|
+
import Fastify from 'fastify';
|
|
16
|
+
import fastifyJwt from '@fastify/jwt';
|
|
17
|
+
|
|
18
|
+
const app = Fastify();
|
|
19
|
+
|
|
20
|
+
app.register(fastifyJwt, {
|
|
21
|
+
secret: process.env.JWT_SECRET,
|
|
22
|
+
sign: {
|
|
23
|
+
expiresIn: '1h',
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
// Decorate request with authentication method
|
|
28
|
+
app.decorate('authenticate', async function (request, reply) {
|
|
29
|
+
try {
|
|
30
|
+
await request.jwtVerify();
|
|
31
|
+
} catch (err) {
|
|
32
|
+
reply.code(401).send({ error: 'Unauthorized' });
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// Login route
|
|
37
|
+
app.post('/login', {
|
|
38
|
+
schema: {
|
|
39
|
+
body: {
|
|
40
|
+
type: 'object',
|
|
41
|
+
properties: {
|
|
42
|
+
email: { type: 'string', format: 'email' },
|
|
43
|
+
password: { type: 'string' },
|
|
44
|
+
},
|
|
45
|
+
required: ['email', 'password'],
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
}, async (request, reply) => {
|
|
49
|
+
const { email, password } = request.body;
|
|
50
|
+
const user = await validateCredentials(email, password);
|
|
51
|
+
|
|
52
|
+
if (!user) {
|
|
53
|
+
return reply.code(401).send({ error: 'Invalid credentials' });
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const token = app.jwt.sign({
|
|
57
|
+
id: user.id,
|
|
58
|
+
email: user.email,
|
|
59
|
+
role: user.role,
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
return { token };
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// Protected route
|
|
66
|
+
app.get('/profile', {
|
|
67
|
+
onRequest: [app.authenticate],
|
|
68
|
+
}, async (request) => {
|
|
69
|
+
return { user: request.user };
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Refresh Tokens
|
|
74
|
+
|
|
75
|
+
Implement refresh token rotation:
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
import fastifyJwt from '@fastify/jwt';
|
|
79
|
+
import { randomBytes } from 'node:crypto';
|
|
80
|
+
|
|
81
|
+
app.register(fastifyJwt, {
|
|
82
|
+
secret: process.env.JWT_SECRET,
|
|
83
|
+
sign: {
|
|
84
|
+
expiresIn: '15m', // Short-lived access tokens
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
// Store refresh tokens (use Redis in production)
|
|
89
|
+
const refreshTokens = new Map<string, { userId: string; expires: number }>();
|
|
90
|
+
|
|
91
|
+
app.post('/auth/login', async (request, reply) => {
|
|
92
|
+
const { email, password } = request.body;
|
|
93
|
+
const user = await validateCredentials(email, password);
|
|
94
|
+
|
|
95
|
+
if (!user) {
|
|
96
|
+
return reply.code(401).send({ error: 'Invalid credentials' });
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const accessToken = app.jwt.sign({ id: user.id, role: user.role });
|
|
100
|
+
const refreshToken = randomBytes(32).toString('hex');
|
|
101
|
+
|
|
102
|
+
refreshTokens.set(refreshToken, {
|
|
103
|
+
userId: user.id,
|
|
104
|
+
expires: Date.now() + 7 * 24 * 60 * 60 * 1000, // 7 days
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
return { accessToken, refreshToken };
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
app.post('/auth/refresh', async (request, reply) => {
|
|
111
|
+
const { refreshToken } = request.body;
|
|
112
|
+
const stored = refreshTokens.get(refreshToken);
|
|
113
|
+
|
|
114
|
+
if (!stored || stored.expires < Date.now()) {
|
|
115
|
+
refreshTokens.delete(refreshToken);
|
|
116
|
+
return reply.code(401).send({ error: 'Invalid refresh token' });
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Delete old token (rotation)
|
|
120
|
+
refreshTokens.delete(refreshToken);
|
|
121
|
+
|
|
122
|
+
const user = await db.users.findById(stored.userId);
|
|
123
|
+
const accessToken = app.jwt.sign({ id: user.id, role: user.role });
|
|
124
|
+
const newRefreshToken = randomBytes(32).toString('hex');
|
|
125
|
+
|
|
126
|
+
refreshTokens.set(newRefreshToken, {
|
|
127
|
+
userId: user.id,
|
|
128
|
+
expires: Date.now() + 7 * 24 * 60 * 60 * 1000,
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
return { accessToken, refreshToken: newRefreshToken };
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
app.post('/auth/logout', async (request, reply) => {
|
|
135
|
+
const { refreshToken } = request.body;
|
|
136
|
+
refreshTokens.delete(refreshToken);
|
|
137
|
+
return { success: true };
|
|
138
|
+
});
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## Role-Based Access Control
|
|
142
|
+
|
|
143
|
+
Implement RBAC with decorators:
|
|
144
|
+
|
|
145
|
+
```typescript
|
|
146
|
+
type Role = 'admin' | 'user' | 'moderator';
|
|
147
|
+
|
|
148
|
+
// Create authorization decorator
|
|
149
|
+
app.decorate('authorize', function (...allowedRoles: Role[]) {
|
|
150
|
+
return async (request, reply) => {
|
|
151
|
+
await request.jwtVerify();
|
|
152
|
+
|
|
153
|
+
const userRole = request.user.role as Role;
|
|
154
|
+
if (!allowedRoles.includes(userRole)) {
|
|
155
|
+
return reply.code(403).send({
|
|
156
|
+
error: 'Forbidden',
|
|
157
|
+
message: `Role '${userRole}' is not authorized for this resource`,
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
// Admin only route
|
|
164
|
+
app.get('/admin/users', {
|
|
165
|
+
onRequest: [app.authorize('admin')],
|
|
166
|
+
}, async (request) => {
|
|
167
|
+
return db.users.findAll();
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
// Admin or moderator
|
|
171
|
+
app.delete('/posts/:id', {
|
|
172
|
+
onRequest: [app.authorize('admin', 'moderator')],
|
|
173
|
+
}, async (request) => {
|
|
174
|
+
await db.posts.delete(request.params.id);
|
|
175
|
+
return { deleted: true };
|
|
176
|
+
});
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
## Permission-Based Authorization
|
|
180
|
+
|
|
181
|
+
Fine-grained permission checks:
|
|
182
|
+
|
|
183
|
+
```typescript
|
|
184
|
+
interface Permission {
|
|
185
|
+
resource: string;
|
|
186
|
+
action: 'create' | 'read' | 'update' | 'delete';
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const rolePermissions: Record<string, Permission[]> = {
|
|
190
|
+
admin: [
|
|
191
|
+
{ resource: '*', action: 'create' },
|
|
192
|
+
{ resource: '*', action: 'read' },
|
|
193
|
+
{ resource: '*', action: 'update' },
|
|
194
|
+
{ resource: '*', action: 'delete' },
|
|
195
|
+
],
|
|
196
|
+
user: [
|
|
197
|
+
{ resource: 'posts', action: 'create' },
|
|
198
|
+
{ resource: 'posts', action: 'read' },
|
|
199
|
+
{ resource: 'comments', action: 'create' },
|
|
200
|
+
{ resource: 'comments', action: 'read' },
|
|
201
|
+
],
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
function hasPermission(role: string, resource: string, action: string): boolean {
|
|
205
|
+
const permissions = rolePermissions[role] || [];
|
|
206
|
+
return permissions.some(
|
|
207
|
+
(p) =>
|
|
208
|
+
(p.resource === '*' || p.resource === resource) &&
|
|
209
|
+
p.action === action
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
app.decorate('checkPermission', function (resource: string, action: string) {
|
|
214
|
+
return async (request, reply) => {
|
|
215
|
+
await request.jwtVerify();
|
|
216
|
+
|
|
217
|
+
if (!hasPermission(request.user.role, resource, action)) {
|
|
218
|
+
return reply.code(403).send({
|
|
219
|
+
error: 'Forbidden',
|
|
220
|
+
message: `Not allowed to ${action} ${resource}`,
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
// Usage
|
|
227
|
+
app.post('/posts', {
|
|
228
|
+
onRequest: [app.checkPermission('posts', 'create')],
|
|
229
|
+
}, createPostHandler);
|
|
230
|
+
|
|
231
|
+
app.delete('/posts/:id', {
|
|
232
|
+
onRequest: [app.checkPermission('posts', 'delete')],
|
|
233
|
+
}, deletePostHandler);
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
## API Key / Bearer Token Authentication
|
|
237
|
+
|
|
238
|
+
Use `@fastify/bearer-auth` for API key and bearer token authentication:
|
|
239
|
+
|
|
240
|
+
```typescript
|
|
241
|
+
import bearerAuth from '@fastify/bearer-auth';
|
|
242
|
+
|
|
243
|
+
const validKeys = new Set([process.env.API_KEY]);
|
|
244
|
+
|
|
245
|
+
app.register(bearerAuth, {
|
|
246
|
+
keys: validKeys,
|
|
247
|
+
errorResponse: (err) => ({
|
|
248
|
+
error: 'Unauthorized',
|
|
249
|
+
message: 'Invalid API key',
|
|
250
|
+
}),
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
// All routes are now protected
|
|
254
|
+
app.get('/api/data', async (request) => {
|
|
255
|
+
return { data: [] };
|
|
256
|
+
});
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
For database-backed API keys with custom validation:
|
|
260
|
+
|
|
261
|
+
```typescript
|
|
262
|
+
import bearerAuth from '@fastify/bearer-auth';
|
|
263
|
+
|
|
264
|
+
app.register(bearerAuth, {
|
|
265
|
+
auth: async (key, request) => {
|
|
266
|
+
const apiKey = await db.apiKeys.findByKey(key);
|
|
267
|
+
|
|
268
|
+
if (!apiKey || !apiKey.active) {
|
|
269
|
+
return false;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// Track usage (fire and forget)
|
|
273
|
+
db.apiKeys.recordUsage(apiKey.id, {
|
|
274
|
+
ip: request.ip,
|
|
275
|
+
timestamp: new Date(),
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
request.apiKey = apiKey;
|
|
279
|
+
return true;
|
|
280
|
+
},
|
|
281
|
+
errorResponse: (err) => ({
|
|
282
|
+
error: 'Unauthorized',
|
|
283
|
+
message: 'Invalid API key',
|
|
284
|
+
}),
|
|
285
|
+
});
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
## OAuth 2.0 Integration
|
|
289
|
+
|
|
290
|
+
Integrate with OAuth providers using @fastify/oauth2:
|
|
291
|
+
|
|
292
|
+
```typescript
|
|
293
|
+
import fastifyOauth2 from '@fastify/oauth2';
|
|
294
|
+
|
|
295
|
+
app.register(fastifyOauth2, {
|
|
296
|
+
name: 'googleOAuth2',
|
|
297
|
+
scope: ['profile', 'email'],
|
|
298
|
+
credentials: {
|
|
299
|
+
client: {
|
|
300
|
+
id: process.env.GOOGLE_CLIENT_ID,
|
|
301
|
+
secret: process.env.GOOGLE_CLIENT_SECRET,
|
|
302
|
+
},
|
|
303
|
+
},
|
|
304
|
+
startRedirectPath: '/auth/google',
|
|
305
|
+
callbackUri: 'http://localhost:3000/auth/google/callback',
|
|
306
|
+
discovery: {
|
|
307
|
+
issuer: 'https://accounts.google.com',
|
|
308
|
+
},
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
app.get('/auth/google/callback', async (request, reply) => {
|
|
312
|
+
const { token } = await app.googleOAuth2.getAccessTokenFromAuthorizationCodeFlow(request);
|
|
313
|
+
|
|
314
|
+
// Fetch user info from Google
|
|
315
|
+
const userInfo = await fetch('https://www.googleapis.com/oauth2/v2/userinfo', {
|
|
316
|
+
headers: { Authorization: `Bearer ${token.access_token}` },
|
|
317
|
+
}).then((r) => r.json());
|
|
318
|
+
|
|
319
|
+
// Find or create user
|
|
320
|
+
let user = await db.users.findByEmail(userInfo.email);
|
|
321
|
+
if (!user) {
|
|
322
|
+
user = await db.users.create({
|
|
323
|
+
email: userInfo.email,
|
|
324
|
+
name: userInfo.name,
|
|
325
|
+
provider: 'google',
|
|
326
|
+
providerId: userInfo.id,
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// Generate JWT
|
|
331
|
+
const jwt = app.jwt.sign({ id: user.id, role: user.role });
|
|
332
|
+
|
|
333
|
+
// Redirect to frontend with token
|
|
334
|
+
return reply.redirect(`/auth/success?token=${jwt}`);
|
|
335
|
+
});
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
## Session-Based Authentication
|
|
339
|
+
|
|
340
|
+
Use @fastify/session for session management:
|
|
341
|
+
|
|
342
|
+
```typescript
|
|
343
|
+
import fastifyCookie from '@fastify/cookie';
|
|
344
|
+
import fastifySession from '@fastify/session';
|
|
345
|
+
import RedisStore from 'connect-redis';
|
|
346
|
+
import { createClient } from 'redis';
|
|
347
|
+
|
|
348
|
+
const redisClient = createClient({ url: process.env.REDIS_URL });
|
|
349
|
+
await redisClient.connect();
|
|
350
|
+
|
|
351
|
+
app.register(fastifyCookie);
|
|
352
|
+
app.register(fastifySession, {
|
|
353
|
+
secret: process.env.SESSION_SECRET,
|
|
354
|
+
store: new RedisStore({ client: redisClient }),
|
|
355
|
+
cookie: {
|
|
356
|
+
secure: process.env.NODE_ENV === 'production',
|
|
357
|
+
httpOnly: true,
|
|
358
|
+
maxAge: 24 * 60 * 60 * 1000, // 1 day
|
|
359
|
+
},
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
app.post('/login', async (request, reply) => {
|
|
363
|
+
const { email, password } = request.body;
|
|
364
|
+
const user = await validateCredentials(email, password);
|
|
365
|
+
|
|
366
|
+
if (!user) {
|
|
367
|
+
return reply.code(401).send({ error: 'Invalid credentials' });
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
request.session.userId = user.id;
|
|
371
|
+
request.session.role = user.role;
|
|
372
|
+
|
|
373
|
+
return { success: true };
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
app.decorate('requireSession', async function (request, reply) {
|
|
377
|
+
if (!request.session.userId) {
|
|
378
|
+
return reply.code(401).send({ error: 'Not authenticated' });
|
|
379
|
+
}
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
app.get('/profile', {
|
|
383
|
+
onRequest: [app.requireSession],
|
|
384
|
+
}, async (request) => {
|
|
385
|
+
const user = await db.users.findById(request.session.userId);
|
|
386
|
+
return { user };
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
app.post('/logout', async (request, reply) => {
|
|
390
|
+
await request.session.destroy();
|
|
391
|
+
return { success: true };
|
|
392
|
+
});
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
## Resource-Based Authorization
|
|
396
|
+
|
|
397
|
+
Check ownership of resources:
|
|
398
|
+
|
|
399
|
+
```typescript
|
|
400
|
+
app.decorate('checkOwnership', function (getResourceOwnerId: (request) => Promise<string>) {
|
|
401
|
+
return async (request, reply) => {
|
|
402
|
+
const ownerId = await getResourceOwnerId(request);
|
|
403
|
+
|
|
404
|
+
if (ownerId !== request.user.id && request.user.role !== 'admin') {
|
|
405
|
+
return reply.code(403).send({
|
|
406
|
+
error: 'Forbidden',
|
|
407
|
+
message: 'You do not own this resource',
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
};
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
// Check post ownership
|
|
414
|
+
app.put('/posts/:id', {
|
|
415
|
+
onRequest: [
|
|
416
|
+
app.authenticate,
|
|
417
|
+
app.checkOwnership(async (request) => {
|
|
418
|
+
const post = await db.posts.findById(request.params.id);
|
|
419
|
+
return post?.authorId;
|
|
420
|
+
}),
|
|
421
|
+
],
|
|
422
|
+
}, updatePostHandler);
|
|
423
|
+
|
|
424
|
+
// Alternative: inline check
|
|
425
|
+
app.put('/posts/:id', {
|
|
426
|
+
onRequest: [app.authenticate],
|
|
427
|
+
}, async (request, reply) => {
|
|
428
|
+
const post = await db.posts.findById(request.params.id);
|
|
429
|
+
|
|
430
|
+
if (!post) {
|
|
431
|
+
return reply.code(404).send({ error: 'Post not found' });
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
if (post.authorId !== request.user.id && request.user.role !== 'admin') {
|
|
435
|
+
return reply.code(403).send({ error: 'Forbidden' });
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
return db.posts.update(post.id, request.body);
|
|
439
|
+
});
|
|
440
|
+
```
|
|
441
|
+
|
|
442
|
+
## Password Hashing
|
|
443
|
+
|
|
444
|
+
Use secure password hashing with argon2:
|
|
445
|
+
|
|
446
|
+
```typescript
|
|
447
|
+
import { hash, verify } from '@node-rs/argon2';
|
|
448
|
+
|
|
449
|
+
async function hashPassword(password: string): Promise<string> {
|
|
450
|
+
return hash(password, {
|
|
451
|
+
memoryCost: 65536,
|
|
452
|
+
timeCost: 3,
|
|
453
|
+
parallelism: 4,
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
async function verifyPassword(hash: string, password: string): Promise<boolean> {
|
|
458
|
+
return verify(hash, password);
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
app.post('/register', async (request, reply) => {
|
|
462
|
+
const { email, password } = request.body;
|
|
463
|
+
|
|
464
|
+
const hashedPassword = await hashPassword(password);
|
|
465
|
+
const user = await db.users.create({
|
|
466
|
+
email,
|
|
467
|
+
password: hashedPassword,
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
reply.code(201);
|
|
471
|
+
return { id: user.id, email: user.email };
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
app.post('/login', async (request, reply) => {
|
|
475
|
+
const { email, password } = request.body;
|
|
476
|
+
const user = await db.users.findByEmail(email);
|
|
477
|
+
|
|
478
|
+
if (!user || !(await verifyPassword(user.password, password))) {
|
|
479
|
+
return reply.code(401).send({ error: 'Invalid credentials' });
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
const token = app.jwt.sign({ id: user.id, role: user.role });
|
|
483
|
+
return { token };
|
|
484
|
+
});
|
|
485
|
+
```
|
|
486
|
+
|
|
487
|
+
## Rate Limiting for Auth Endpoints
|
|
488
|
+
|
|
489
|
+
Protect auth endpoints from brute force. **IMPORTANT: For production security, you MUST configure rate limiting with a Redis backend.** In-memory rate limiting is not safe for distributed deployments and can be bypassed.
|
|
490
|
+
|
|
491
|
+
```typescript
|
|
492
|
+
import fastifyRateLimit from '@fastify/rate-limit';
|
|
493
|
+
import Redis from 'ioredis';
|
|
494
|
+
|
|
495
|
+
const redis = new Redis(process.env.REDIS_URL);
|
|
496
|
+
|
|
497
|
+
// Global rate limit with Redis backend
|
|
498
|
+
app.register(fastifyRateLimit, {
|
|
499
|
+
max: 100,
|
|
500
|
+
timeWindow: '1 minute',
|
|
501
|
+
redis, // REQUIRED for production - ensures rate limiting works across all instances
|
|
502
|
+
});
|
|
503
|
+
|
|
504
|
+
// Stricter limit for auth endpoints
|
|
505
|
+
app.register(async function authRoutes(fastify) {
|
|
506
|
+
await fastify.register(fastifyRateLimit, {
|
|
507
|
+
max: 5,
|
|
508
|
+
timeWindow: '1 minute',
|
|
509
|
+
redis, // REQUIRED for production
|
|
510
|
+
keyGenerator: (request) => {
|
|
511
|
+
// Rate limit by IP + email combination
|
|
512
|
+
const email = request.body?.email || '';
|
|
513
|
+
return `${request.ip}:${email}`;
|
|
514
|
+
},
|
|
515
|
+
});
|
|
516
|
+
|
|
517
|
+
fastify.post('/login', loginHandler);
|
|
518
|
+
fastify.post('/register', registerHandler);
|
|
519
|
+
fastify.post('/forgot-password', forgotPasswordHandler);
|
|
520
|
+
}, { prefix: '/auth' });
|
|
521
|
+
```
|