-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathserver.js
More file actions
384 lines (310 loc) · 10.6 KB
/
server.js
File metadata and controls
384 lines (310 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
84
85
86
87
88
89
90
91
92
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import { createServer } from 'node:http';
import { createHmac, timingSafeEqual } from 'node:crypto';
import { URL } from 'node:url';
import { database, dbPath } from './db.js';
import "./env.js";
const port = Number(process.env.PORT || 4000);
const LOGIN_RATE_LIMIT_MAX_ATTEMPTS = Number(process.env.LOGIN_RATE_LIMIT_MAX_ATTEMPTS || 5);
const LOGIN_RATE_LIMIT_WINDOW_MS = Number(process.env.LOGIN_RATE_LIMIT_WINDOW_MS || 15 * 60 * 1000);
const LOGIN_RATE_LIMIT_BLOCK_MS = Number(process.env.LOGIN_RATE_LIMIT_BLOCK_MS || 15 * 60 * 1000);
const AUTH_TOKEN_SECRET = process.env.AUTH_TOKEN_SECRET || 'brocode-dev-secret-change-me';
const AUTH_TOKEN_TTL_SECONDS = Number(process.env.AUTH_TOKEN_TTL_SECONDS || 60 * 60 * 12);
const CORS_ALLOW_ORIGIN = process.env.CORS_ALLOW_ORIGIN || '*';
const loginAttempts = new Map();
const getLoginKey = (req, username) => {
const forwardedFor = req.headers['x-forwarded-for'];
const firstForwardedIp = Array.isArray(forwardedFor)
? forwardedFor[0]
: typeof forwardedFor === 'string'
? forwardedFor.split(',')[0]
: '';
const remoteIp = firstForwardedIp?.trim() || req.socket?.remoteAddress || 'unknown-ip';
return `${remoteIp}:${username}`;
};
const getRateLimitState = (key) => {
const now = Date.now();
const existing = loginAttempts.get(key);
if (!existing) {
const state = { count: 0, windowStart: now, blockedUntil: 0 };
loginAttempts.set(key, state);
return state;
}
if (existing.blockedUntil > 0 && existing.blockedUntil <= now) {
existing.count = 0;
existing.windowStart = now;
existing.blockedUntil = 0;
}
if (now - existing.windowStart > LOGIN_RATE_LIMIT_WINDOW_MS) {
existing.count = 0;
existing.windowStart = now;
}
return existing;
};
const clearRateLimitState = (key) => {
loginAttempts.delete(key);
};
const parseBearerToken = (authHeader) => {
if (typeof authHeader !== 'string') {
return null;
}
const [scheme, token] = authHeader.trim().split(/\s+/, 2);
if (!scheme || !token || scheme.toLowerCase() !== 'bearer') {
return null;
}
return token;
};
const toBase64Url = (value) => Buffer.from(value).toString('base64url');
const signToken = (payload) =>
createHmac('sha256', AUTH_TOKEN_SECRET).update(payload).digest('base64url');
const generateAuthToken = (user) => {
const payload = {
sub: user.id,
role: user.role,
exp: Math.floor(Date.now() / 1000) + AUTH_TOKEN_TTL_SECONDS,
};
const payloadPart = toBase64Url(JSON.stringify(payload));
const signature = signToken(payloadPart);
return `${payloadPart}.${signature}`;
};
const verifyAuthToken = (token) => {
const [payloadPart, signaturePart] = token.split('.');
if (!payloadPart || !signaturePart) {
return null;
}
const expectedSignature = signToken(payloadPart);
const providedSignatureBuffer = Buffer.from(signaturePart, 'base64url');
const expectedSignatureBuffer = Buffer.from(expectedSignature, 'base64url');
if (providedSignatureBuffer.length !== expectedSignatureBuffer.length) {
return null;
}
if (!timingSafeEqual(providedSignatureBuffer, expectedSignatureBuffer)) {
return null;
}
try {
const payload = JSON.parse(Buffer.from(payloadPart, 'base64url').toString('utf-8'));
if (!payload.sub || !payload.exp || payload.exp < Math.floor(Date.now() / 1000)) {
return null;
}
return payload;
} catch {
return null;
}
};
const getUserFromAuthHeader = (authHeader) => {
const token = parseBearerToken(authHeader);
if (!token) {
return null;
}
const verifiedPayload = verifyAuthToken(token);
if (!verifiedPayload) {
return null;
}
return database.getUserById(verifiedPayload.sub);
};
const recordFailedLoginAttempt = (key) => {
const now = Date.now();
const state = getRateLimitState(key);
state.count += 1;
if (state.count >= LOGIN_RATE_LIMIT_MAX_ATTEMPTS) {
state.blockedUntil = now + LOGIN_RATE_LIMIT_BLOCK_MS;
}
};
const sendJson = (res, statusCode, body) => {
res.writeHead(statusCode, {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': CORS_ALLOW_ORIGIN,
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
'Access-Control-Allow-Methods': 'GET,POST,DELETE,OPTIONS',
});
res.end(JSON.stringify(body));
};
const readBody = (req) =>
new Promise((resolve, reject) => {
let data = '';
req.on('data', (chunk) => {
data += chunk;
});
req.on('end', () => {
if (!data) {
resolve({});
return;
}
try {
resolve(JSON.parse(data));
} catch {
reject(new Error('Invalid JSON payload'));
}
});
req.on('error', reject);
});
const server = createServer(async (req, res) => {
const method = req.method || 'GET';
const parsedUrl = new URL(req.url || '/', `http://localhost:${port}`);
const path = parsedUrl.pathname;
if (method === 'OPTIONS') {
sendJson(res, 204, {});
return;
}
if (method === 'GET' && path === '/api/health') {
sendJson(res, 200, { status: 'ok', service: 'brocode-backend', timestamp: new Date().toISOString() });
return;
}
if (method === 'POST' && path === '/api/auth/login') {
try {
const { username, password } = await readBody(req);
if (!username || !password) {
sendJson(res, 400, { error: 'username and password are required' });
return;
}
const loginKey = getLoginKey(req, username);
const rateLimitState = getRateLimitState(loginKey);
const now = Date.now();
if (rateLimitState.blockedUntil > now) {
const retryAfterSeconds = Math.ceil((rateLimitState.blockedUntil - now) / 1000);
sendJson(res, 429, {
error: 'Too many failed login attempts. Please try again later.',
retryAfterSeconds,
});
return;
}
const user = database.getUserByCredentials(username, password);
if (!user) {
recordFailedLoginAttempt(loginKey);
sendJson(res, 401, { error: 'invalid credentials' });
return;
}
clearRateLimitState(loginKey);
sendJson(res, 200, { token: generateAuthToken(user), user });
return;
} catch (error) {
sendJson(res, 400, { error: error.message });
return;
}
}
if (method === 'GET' && path === '/api/catalog') {
sendJson(res, 200, database.getCatalog());
return;
}
if (method === 'GET' && path.startsWith('/api/catalog/')) {
const category = path.replace('/api/catalog/', '');
const catalog = database.getCatalog();
const items = catalog[category];
if (!items) {
sendJson(res, 404, { error: `Unknown category: ${category}` });
return;
}
sendJson(res, 200, items);
return;
}
if (method === 'GET' && path === '/api/spots') {
sendJson(res, 200, database.getSpots());
return;
}
if (method === 'GET' && path === '/api/orders') {
const authedUser = getUserFromAuthHeader(req.headers.authorization);
if (!authedUser) {
sendJson(res, 401, { error: 'Unauthorized' });
return;
}
const spotId = parsedUrl.searchParams.get('spotId');
const userId = parsedUrl.searchParams.get('userId');
if (authedUser.role !== 'admin' && userId && userId !== authedUser.id) {
sendJson(res, 403, { error: 'Forbidden' });
return;
}
const effectiveUserId = authedUser.role === 'admin' ? userId : authedUser.id;
const orders = database.getOrders({ spotId, userId: effectiveUserId });
sendJson(res, 200, orders);
return;
}
if (method === 'GET' && path.startsWith('/api/orders/')) {
const orderId = path.replace('/api/orders/', '');
const authedUser = getUserFromAuthHeader(req.headers.authorization);
if (!authedUser) {
sendJson(res, 401, { error: 'Unauthorized' });
return;
}
const order = database.getOrderById(orderId);
if (!order) {
sendJson(res, 404, { error: `Order not found: ${orderId}` });
return;
}
const isAdmin = authedUser.role === 'admin';
if (!isAdmin && order.userId !== authedUser.id) {
sendJson(res, 403, { error: 'Forbidden' });
return;
}
sendJson(res, 200, order);
return;
}
if (method === 'POST' && path === '/api/orders') {
try {
const authedUser = getUserFromAuthHeader(req.headers.authorization);
if (!authedUser) {
sendJson(res, 401, { error: 'Unauthorized' });
return;
}
const { spotId, userId, items } = await readBody(req);
if (!spotId || !userId || !Array.isArray(items) || items.length === 0) {
sendJson(res, 400, { error: 'spotId, userId and at least one order item are required' });
return;
}
if (authedUser.role !== 'admin' && userId !== authedUser.id) {
sendJson(res, 403, { error: 'Forbidden' });
return;
}
if (!database.userExists(userId)) {
sendJson(res, 404, { error: `Unknown userId: ${userId}` });
return;
}
if (!database.spotExists(spotId)) {
sendJson(res, 404, { error: `Unknown spotId: ${spotId}` });
return;
}
const newOrder = database.createOrder({ spotId, userId, items });
sendJson(res, 201, newOrder);
return;
} catch (error) {
const isValidationError =
error.message.includes('Unknown productId') ||
error.message.includes('Each order item must include productId');
sendJson(res, isValidationError ? 400 : 500, { error: error.message });
return;
}
}
if (method === 'GET' && path.startsWith('/api/bills/')) {
const authedUser = getUserFromAuthHeader(req.headers.authorization);
if (!authedUser || authedUser.role !== 'admin') {
sendJson(res, 403, { error: 'Forbidden' });
return;
}
const spotId = path.replace('/api/bills/', '');
const bill = database.getBillBySpotId(spotId);
sendJson(res, 200, bill);
return;
}
if (method === 'DELETE' && path.startsWith('/api/users/')) {
const authedUser = getUserFromAuthHeader(req.headers.authorization);
if (!authedUser || authedUser.role !== 'admin') {
sendJson(res, 403, { error: 'Forbidden' });
return;
}
const userId = path.replace('/api/users/', '');
if (!userId) {
sendJson(res, 400, { error: 'userId is required' });
return;
}
if (!database.userExists(userId)) {
sendJson(res, 404, { error: `Unknown userId: ${userId}` });
return;
}
database.deleteUserCompletely(userId);
sendJson(res, 200, { success: true, deletedUserId: userId });
return;
}
sendJson(res, 404, { error: 'Route not found' });
});
server.listen(port, () => {
console.log(`Backend API running on http://localhost:${port}`);
console.log(`Using local database at: ${dbPath}`);
});