forked from akordavid373/sealed-auction-platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
2992 lines (2583 loc) · 90.4 KB
/
Copy pathserver.js
File metadata and controls
2992 lines (2583 loc) · 90.4 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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Initialize APM (MUST be the first line)
const apm = require('elastic-apm-node').start({
serviceName: process.env.APM_SERVICE_NAME || 'sealed-auction-platform',
secretToken: process.env.APM_SECRET_TOKEN || '',
serverUrl: process.env.APM_SERVER_URL || 'http://localhost:8200',
environment: process.env.NODE_ENV || 'development'
});
const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const Sentry = require('@sentry/node');
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const bcrypt = require('bcrypt');
const helmet = require('helmet');
const cors = require('cors');
const rateLimit = require('express-rate-limit');
const jwt = require('jsonwebtoken');
const { v4: uuidv4 } = require('uuid');
const { Server, Keypair, TransactionBuilder, Networks, BASE_FEE, Asset } = require('stellar-sdk');
const session = require('express-session');
const passport = require('passport');
const AuctionDatabase = require('./database');
const { ApplicationMetrics, createMetricsMiddleware } = require('./utils/metrics');
const NetworkMonitor = require('./utils/network-monitor');
// Initialize database
const db = new AuctionDatabase();
// Initialize network monitor
const networkMonitor = new NetworkMonitor();
const app = express();
const server = http.createServer(app);
const appMetrics = new ApplicationMetrics();
const io = socketIo(server, {
cors: {
origin: "*",
methods: ["GET", "POST"]
}
});
const sentryDsn = process.env.SENTRY_DSN;
const sentryEnabled = Boolean(sentryDsn);
if (sentryEnabled) {
Sentry.init({
dsn: sentryDsn,
environment: process.env.SENTRY_ENVIRONMENT || process.env.NODE_ENV || 'development',
release: process.env.SENTRY_RELEASE,
tracesSampleRate: Number(process.env.SENTRY_TRACES_SAMPLE_RATE || 0.1)
});
app.use(Sentry.Handlers.requestHandler());
console.log('Sentry error tracking enabled.');
} else {
console.log('Sentry disabled (set SENTRY_DSN to enable error tracking).');
}
function trackError(error, context = {}) {
if (sentryEnabled) {
Sentry.withScope((scope) => {
Object.entries(context).forEach(([key, value]) => {
scope.setExtra(key, value);
});
Sentry.captureException(error);
});
}
if (apm && typeof apm.captureError === 'function') {
apm.captureError(error, { custom: context });
}
}
function logError(message, error, context = {}) {
console.error(message, error);
trackError(error, { message, ...context });
}
// Security middleware
app.use(helmet());
app.use(createMetricsMiddleware(appMetrics));
// Security monitoring endpoint (admin only)
app.get('/api/security/stats', (req, res) => {
try {
// In production, add admin authentication here
const stats = db.getSecurityStats();
res.json(stats);
} catch (error) {
logError('Error getting security stats:', error, { endpoint: '/api/security/stats' });
res.status(500).json({ error: 'Failed to get security stats' });
}
});
app.get('/api/security/logs', (req, res) => {
try {
const limit = parseInt(req.query.limit) || 100;
// In production, add admin authentication here
const logs = db.getQueryLog(limit);
res.json(logs);
} catch (error) {
logError('Error getting query logs:', error, { endpoint: '/api/security/logs' });
res.status(500).json({ error: 'Failed to get query logs' });
}
});
// Application metrics endpoints
app.get('/api/monitoring/metrics', (req, res) => {
try {
res.json(appMetrics.getSnapshot());
} catch (error) {
console.error('Error getting application metrics:', error);
res.status(500).json({ error: 'Failed to get application metrics' });
}
});
app.get('/api/monitoring/metrics/prometheus', (req, res) => {
try {
res.type('text/plain');
res.send(appMetrics.toPrometheus());
} catch (error) {
console.error('Error getting prometheus metrics:', error);
res.status(500).json({ error: 'Failed to get prometheus metrics' });
}
});
// Restrictive CORS configuration
const allowedOrigins = process.env.ALLOWED_ORIGINS
? process.env.ALLOWED_ORIGINS.split(',')
: ['http://localhost:3000', 'http://localhost:3001'];
const corsOptions = {
origin: function (origin, callback) {
// Allow requests with no origin (like mobile apps or curl requests)
if (!origin) return callback(null, true);
if (allowedOrigins.indexOf(origin) !== -1) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
},
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With'],
credentials: true,
maxAge: 86400 // 24 hours
};
app.use(cors(corsOptions));
app.use(express.json());
app.use(express.static('public'));
// Session middleware for OAuth
app.use(session({
secret: process.env.SESSION_SECRET || 'your-session-secret-change-in-production',
resave: false,
saveUninitialized: false,
cookie: { secure: process.env.NODE_ENV === 'production' }
}));
// Initialize Passport
app.use(passport.initialize());
app.use(passport.session());
// Rate limiting configuration
const JWT_SECRET = process.env.JWT_SECRET || 'your-jwt-secret-change-in-production';
const tokenBlacklist = new Set();
// Import validation middleware
const { validateRequest } = require('./utils/validation');
const { validateSchema } = require('./utils/schema-validation');
// Tiered rate limiting configuration
// Strict limits for authentication endpoints
const authLimiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 5, // limit each IP to 5 requests per windowMs
message: {
error: 'Too many authentication attempts, please try again after 15 minutes'
},
standardHeaders: true,
legacyHeaders: false,
});
// Moderate limits for bid operations
const bidLimiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 30, // limit each IP to 30 requests per windowMs
message: {
error: 'Too many bid operations, please slow down'
},
standardHeaders: true,
legacyHeaders: false,
});
// Higher limits for read operations
const readLimiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // limit each IP to 100 requests per windowMs
message: {
error: 'Too many requests, please try again later'
},
standardHeaders: true,
legacyHeaders: false,
});
// Very strict limits for auction creation
const createLimiter = rateLimit({
windowMs: 60 * 60 * 1000, // 1 hour
max: 10, // limit each IP to 10 auction creations per hour
message: {
error: 'Too many auction creations, please try again later'
},
standardHeaders: true,
legacyHeaders: false,
});
// General API limiter as fallback
const generalLimiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // limit each IP to 100 requests per windowMs
message: {
error: 'Too many requests, please try again later'
},
standardHeaders: true,
legacyHeaders: false,
});
// Apply general rate limiting to all routes
app.use(generalLimiter);
// Backup directory
const backupDir = path.join(__dirname, 'backups');
// In-memory storage (in production, use a proper database)
let auctions = new Map();
let bids = new Map();
let users = new Map();
// Auction class
class Auction {
constructor(id, title, description, startingBid, endTime, creator) {
this.id = id;
this.title = title;
this.description = description;
this.startingBid = startingBid;
this.currentHighestBid = startingBid;
this.endTime = endTime;
this.creator = creator;
this.status = 'active';
this.bids = [];
this.winner = null;
this.winningBid = null;
this.createdAt = new Date();
}
addBid(bid) {
this.bids.push(bid);
if (bid.amount > this.currentHighestBid) {
this.currentHighestBid = bid.amount;
}
}
close() {
this.status = 'closed';
if (this.bids.length > 0) {
const winningBid = this.bids.reduce((prev, current) =>
prev.amount > current.amount ? prev : current
);
this.winner = winningBid.bidderId;
this.winningBid = winningBid;
}
}
}
// Bid class
class Bid {
constructor(id, auctionId, bidderId, amount, encryptedBid) {
this.id = id;
this.auctionId = auctionId;
this.bidderId = bidderId;
this.amount = amount;
this.encryptedBid = encryptedBid;
this.timestamp = new Date();
this.revealed = false;
}
}
// User class
class User {
constructor(id, username, hashedPassword, email = null, provider = null, providerId = null) {
this.id = id;
this.username = username;
this.hashedPassword = hashedPassword;
this.email = email;
this.provider = provider;
this.providerId = providerId;
this.createdAt = new Date();
}
}
// Helper functions
function generateAuctionId() {
return uuidv4();
}
// Admin Authentication Middleware
function authenticateAdmin(req, res, next) {
const authHeader = req.headers['authorization'];
const token = authHeader && authHeader.split(' ')[1];
if (!token) {
return res.status(401).json({ error: 'Admin access token required' });
}
jwt.verify(token, JWT_SECRET, (err, user) => {
if (err) {
return res.status(403).json({ error: 'Invalid or expired token' });
}
// Check if user has admin or moderator role
const dbUser = db.getUserById(user.id);
if (!dbUser || (dbUser.role !== 'admin' && dbUser.role !== 'moderator')) {
return res.status(403).json({ error: 'Admin access required' });
}
req.user = user;
req.userRole = dbUser.role;
next();
});
}
// JWT Authentication Middleware
function authenticateToken(req, res, next) {
const authHeader = req.headers['authorization'];
const token = authHeader && authHeader.split(' ')[1];
if (!token) {
return res.status(401).json({ error: 'Access token required' });
}
if (tokenBlacklist.has(token)) {
return res.status(401).json({ error: 'Token has been revoked' });
}
jwt.verify(token, JWT_SECRET, (err, user) => {
if (err) {
return res.status(403).json({ error: 'Invalid or expired token' });
}
req.user = user;
next();
});
}
// Account Lockout Middleware
function checkAccountLockout(req, res, next) {
const user = db.getUserById(req.user.userId);
if (!user) {
return res.status(404).json({ error: 'User not found' });
}
if (db.isAccountLocked(user.username)) {
return res.status(423).json({
error: 'Account is temporarily locked due to too many failed login attempts',
lockedUntil: user.locked_until
});
}
next();
}
// Generate JWT Token
function generateToken(user) {
return jwt.sign(
{ id: user.id, userId: user.id, username: user.username, role: user.role },
JWT_SECRET,
{ expiresIn: '24h' }
);
}
function encryptBid(bidAmount, secretKey) {
const algorithm = 'aes-256-cbc';
const key = crypto.scryptSync(secretKey, 'salt', 32);
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv(algorithm, key, iv);
let encrypted = cipher.update(bidAmount.toString(), 'utf8', 'hex');
encrypted += cipher.final('hex');
return {
encrypted,
iv: iv.toString('hex')
};
}
// --- Content Negotiation Helpers ---
function toXML(obj, root = 'response') {
let xml = `<?xml version="1.0" encoding="UTF-8"?>\n<${root}>\n`;
const processValue = (val, level) => {
let s = '';
const indent = ' '.repeat(level);
if (Array.isArray(val)) {
val.forEach(item => {
s += `${indent}<item>\n${processValue(item, level + 1)}${indent}</item>\n`;
});
} else if (typeof val === 'object' && val !== null) {
for (const [k, v] of Object.entries(val)) {
s += `${indent}<${k}>${processValue(v, level + 1).trim()}</${k}>\n`;
}
} else {
s += `${val}`;
}
return s;
};
xml += processValue(obj, 1);
xml += `</${root}>`;
return xml;
}
function toYAML(obj, indent = 0) {
let yaml = '';
const spaces = ' '.repeat(indent);
for (const [key, value] of Object.entries(obj)) {
if (Array.isArray(value)) {
yaml += `${spaces}${key}:\n`;
value.forEach(item => {
yaml += `${spaces} - `;
if (typeof item === 'object' && item !== null) {
yaml += toYAML(item, indent + 4).trimStart();
} else {
yaml += `${item}\n`;
}
});
} else if (typeof value === 'object' && value !== null) {
yaml += `${spaces}${key}:\n${toYAML(value, indent + 2)}`;
} else {
yaml += `${spaces}${key}: ${value}\n`;
}
}
return yaml;
}
// Content negotiation middleware
app.use((req, res, next) => {
res.sendData = (data, root = 'response') => {
const accept = req.headers.accept || '';
if (accept.includes('application/xml')) {
res.type('application/xml');
return res.send(toXML(data, root));
} else if (accept.includes('text/yaml') || accept.includes('application/yaml')) {
res.type('text/yaml');
return res.send(toYAML(data));
}
res.json(data);
};
next();
});
function decryptBid(encryptedData, secretKey) {
const algorithm = 'aes-256-cbc';
const key = crypto.scryptSync(secretKey, 'salt', 32);
const iv = Buffer.from(encryptedData.iv, 'hex');
const decipher = crypto.createDecipheriv(algorithm, key, iv);
let decrypted = decipher.update(encryptedData.encrypted, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return parseFloat(decrypted);
}
// --- Backup and Restore ---
function backupData() {
try {
if (!fs.existsSync(backupDir)) {
fs.mkdirSync(backupDir, { recursive: true });
}
fs.writeFileSync(path.join(backupDir, 'auctions.json'), JSON.stringify(Array.from(auctions.entries()), null, 2));
fs.writeFileSync(path.join(backupDir, 'bids.json'), JSON.stringify(Array.from(bids.entries()), null, 2));
fs.writeFileSync(path.join(backupDir, 'users.json'), JSON.stringify(Array.from(users.entries()), null, 2));
console.log(`[${new Date().toISOString()}] Data backup successful.`);
} catch (error) {
logError('Data backup failed:', error, { operation: 'backupData' });
}
}
function restoreData() {
try {
const auctionsPath = path.join(backupDir, 'auctions.json');
if (fs.existsSync(auctionsPath)) {
const data = JSON.parse(fs.readFileSync(auctionsPath));
const restoredAuctions = data.map(([id, plainAuction]) => {
const auction = Object.assign(new Auction(), plainAuction);
auction.endTime = new Date(auction.endTime);
auction.createdAt = new Date(auction.createdAt);
auction.bids = auction.bids.map(plainBid => Object.assign(new Bid(), plainBid));
return [id, auction];
});
auctions = new Map(restoredAuctions);
console.log(`Restored ${auctions.size} auctions from backup.`);
}
const bidsPath = path.join(backupDir, 'bids.json');
if (fs.existsSync(bidsPath)) {
const data = JSON.parse(fs.readFileSync(bidsPath));
const restoredBids = data.map(([id, plainBid]) => {
const bid = Object.assign(new Bid(), plainBid);
bid.timestamp = new Date(bid.timestamp);
return [id, bid];
});
bids = new Map(restoredBids);
console.log(`Restored ${bids.size} bids from backup.`);
}
const usersPath = path.join(backupDir, 'users.json');
if (fs.existsSync(usersPath)) {
const data = JSON.parse(fs.readFileSync(usersPath));
const restoredUsers = data.map(([id, plainUser]) => {
const user = Object.assign(new User(), plainUser);
user.createdAt = new Date(user.createdAt);
return [id, user];
});
users = new Map(restoredUsers);
console.log(`Restored ${users.size} users from backup.`);
}
} catch (error) {
logError('Failed to restore data from backup. Starting with a clean state.', error, { operation: 'restoreData' });
auctions = new Map();
bids = new Map();
users = new Map();
}
}
// Restore data on startup
restoreData();
// Routes
app.get('/api/auctions',
readLimiter,
validateSchema('auctionsQuery'),
validateRequest.query({
page: { type: 'number' },
limit: { type: 'number' },
status: { type: 'status' }
}),
(req, res) => {
try {
const { page = 1, limit = 10, status = null } = req.sanitizedQuery || {};
// Additional validation for limit to prevent excessive data loading
const validatedLimit = Math.min(limit, 100);
const result = db.getPaginatedAuctions(page, validatedLimit, status);
const auctionList = result.auctions.map(auction => ({
id: auction.id,
title: auction.title,
description: auction.description,
startingBid: auction.starting_bid,
currentHighestBid: auction.current_highest_bid || auction.starting_bid,
endTime: auction.end_time,
status: auction.status,
bidCount: db.getBidCount(auction.id),
creator: auction.creator_id,
_links: {
self: { href: `/api/auctions/${auction.id}` },
bids: { href: `/api/auctions/${auction.id}/bids` },
close: { href: `/api/auctions/${auction.id}`, method: 'PATCH' }
}
}));
res.sendData({
auctions: auctionList,
pagination: result.pagination,
_links: {
self: { href: `/api/auctions?page=${page}&limit=${validatedLimit}` + (status ? `&status=${status}` : '') }
}
}, 'auctionsResponse');
} catch (error) {
logError('Error fetching auctions:', error, { endpoint: '/api/auctions', method: 'GET' });
res.status(500).sendData({ error: 'Failed to fetch auctions' });
}
});
app.post('/api/auctions',
authenticateToken,
checkAccountLockout,
createLimiter,
validateSchema('createAuction'),
validateRequest.body({
title: { type: 'title', required: true },
description: { type: 'description', required: true },
startingBid: { type: 'bidAmount', required: true, minimumBid: 0.01 },
endTime: { type: 'date', required: true, allowPast: false }
}),
async (req, res) => {
try {
const userId = req.user.userId;
const { title, description, startingBid, endTime } = req.sanitizedBody;
// Check if user exists
const user = db.getUserById(userId);
if (!user) {
return res.status(404).json({ error: 'User not found' });
}
const auctionId = generateAuctionId();
// Create auction in database
db.createAuction({
id: auctionId,
title,
description,
startingBid,
endTime,
creator: userId,
status: 'active'
});
// Also create in-memory for compatibility
const auction = new Auction(auctionId, title, description, startingBid, new Date(endTime), userId);
auctions.set(auctionId, auction);
const responseData = {
...auction,
_links: {
self: { href: `/api/auctions/${auctionId}` },
bids: { href: `/api/auctions/${auctionId}/bids` },
close: { href: `/api/auctions/${auctionId}`, method: 'PATCH' }
}
};
io.emit('auctionCreated', auction);
io.to('dashboard').emit('auction_update', { type: 'auction', data: auction });
res.status(201).sendData(responseData, 'auctionCreated');
} catch (error) {
logError('Auction creation failed:', error, { endpoint: '/api/auctions', method: 'POST' });
res.status(500).sendData({ error: 'Failed to create auction' });
}
});
app.get('/api/auctions/:id',
readLimiter,
validateSchema('auctionIdParam'),
validateRequest.params({
id: { type: 'uuid', required: true }
}),
(req, res) => {
try {
const auctionId = req.sanitizedParams.id;
const auctionDb = db.getAuction(auctionId);
if (!auctionDb) {
return res.status(404).json({ error: 'Auction not found' });
}
res.sendData({
id: auctionDb.id,
title: auctionDb.title,
description: auctionDb.description,
startingBid: auctionDb.starting_bid,
currentHighestBid: auctionDb.current_highest_bid || auctionDb.starting_bid,
endTime: auctionDb.end_time,
status: auctionDb.status,
bidCount: db.getBidCount(auctionId),
creator: auctionDb.creator_id,
_links: {
self: { href: `/api/auctions/${auctionDb.id}` },
bids: { href: `/api/auctions/${auctionDb.id}/bids` },
close: { href: `/api/auctions/${auctionDb.id}`, method: 'PATCH' }
}
}, 'auctionDetails');
} catch (error) {
logError('Error fetching auction:', error, { endpoint: '/api/auctions/:id', method: 'GET' });
res.status(500).sendData({ error: 'Failed to fetch auction' });
}
});
app.post('/api/auctions/:id/bids',
authenticateToken,
checkAccountLockout,
bidLimiter,
validateSchema('placeBid'),
validateRequest.body({
amount: { type: 'bidAmount', required: true, minimumBid: 0.01 },
secretKey: { type: 'secretKey', required: true }
}),
validateRequest.params({
id: { type: 'uuid', required: true }
}),
async (req, res) => {
try {
const auctionId = req.sanitizedParams.id;
const bidderId = req.user.userId;
const { amount, secretKey } = req.sanitizedBody;
const auctionDb = db.getAuction(auctionId);
if (!auctionDb) {
return res.status(404).sendData({ error: 'Auction not found' });
}
if (auctionDb.status !== 'active') {
return res.status(400).sendData({ error: 'Auction is not active' });
}
// Validate bid amount against current highest bid
const minimumBid = Math.max(auctionDb.starting_bid, auctionDb.current_highest_bid || auctionDb.starting_bid);
if (amount <= minimumBid) {
return res.status(400).sendData({ error: `Bid must be higher than ${minimumBid}` });
}
const encryptedBid = encryptBid(amount, secretKey);
const bidId = uuidv4();
const bid = new Bid(bidId, auctionId, bidderId, amount, encryptedBid);
// Save bid to database
db.createBid({
id: bidId,
auctionId,
bidderId,
amount,
encryptedBid
});
// Update auction's current highest bid
db.updateAuction(auctionId, { current_highest_bid: amount });
// Also update in-memory for compatibility
const auction = auctions.get(auctionId);
if (auction) {
auction.addBid(bid);
}
io.emit('bidPlaced', { auctionId, bidCount: auction ? auction.bids.length : 1 });
// Send detailed bid data to dashboard
const bidData = {
id: bidId,
auction_id: auctionId,
amount: amount,
timestamp: new Date().toISOString(),
bidder_id: req.user?.id || 'anonymous'
};
io.to('dashboard').emit('bid_update', { type: 'bid', data: bidData });
res.status(201).sendData({
message: 'Bid placed successfully',
bidId,
_links: {
self: { href: `/api/auctions/${auctionId}/bids` },
auction: { href: `/api/auctions/${auctionId}` }
}
}, 'bidPlaced');
} catch (error) {
logError('Bid placement failed:', error, { endpoint: '/api/auctions/:id/bids', method: 'POST' });
res.status(500).sendData({ error: 'Failed to place bid' });
}
});
// Use PATCH for updating auction state (RESTful)
app.patch('/api/auctions/:id',
authenticateToken,
bidLimiter,
validateSchema('auctionIdParam'),
validateRequest.params({
id: { type: 'uuid', required: true }
}),
(req, res) => {
try {
const auctionId = req.sanitizedParams.id;
const { status } = req.body;
if (status !== 'closed') {
return res.status(400).sendData({ error: 'Only closing auctions is currently supported via this endpoint' });
}
const auctionDb = db.getAuction(auctionId);
if (!auctionDb) {
return res.status(404).sendData({ error: 'Auction not found' });
}
if (auctionDb.status === 'closed') {
return res.status(400).sendData({ error: 'Auction is already closed' });
}
// Permission check: only creator can close
if (auctionDb.creator_id !== req.user.userId) {
return res.status(403).sendData({ error: 'Only the creator can close this auction' });
}
// Get all bids and find winner
const allBids = db.getBidsForAuction(auctionId);
let winnerId = null;
let winningBidId = null;
if (allBids.length > 0) {
const highestBid = allBids[0]; // Already ordered by amount DESC
winnerId = highestBid.bidder_id;
winningBidId = highestBid.id;
}
// Update auction in database
db.closeAuction(auctionId, winnerId, winningBidId);
// Update in-memory
const auction = auctions.get(auctionId);
if (auction) {
auction.close();
}
const responseData = {
...auctionDb,
status: 'closed',
winner: winnerId,
winningBid: winningBidId,
_links: {
self: { href: `/api/auctions/${auctionId}` },
bids: { href: `/api/auctions/${auctionId}/bids` }
}
};
io.emit('auctionClosed', responseData);
io.to('dashboard').emit('auction_update', { type: 'auction', data: responseData });
res.sendData(responseData, 'auctionClosed');
} catch (error) {
logError('Error closing auction:', error, { endpoint: '/api/auctions/:id', method: 'PATCH' });
res.status(500).sendData({ error: 'Failed to close auction' });
}
});
// ODHUNTER: Kept only PATCH endpoint and removed legacy /api/auctions/:id/close and /api/auctions/:id/bid
app.post('/api/users',
authLimiter,
validateSchema('registerUser'),
validateRequest.body({
username: { type: 'username', required: true },
password: { type: 'password', required: true }
}),
async (req, res) => {
try {
const { username, password } = req.sanitizedBody;
// Check if user already exists
const existingUser = db.getUserByUsername(username);
if (existingUser) {
return res.status(400).json({ error: 'Username already exists' });
}
const userId = uuidv4();
// Create user in database
db.createUser(userId, username, password);
res.status(201).sendData({
userId,
username,
message: 'User registered successfully',
_links: {
login: { href: '/api/auth/login', method: 'POST' }
}
}, 'userRegistered');
} catch (error) {
logError('Error registering user:', error, { endpoint: '/api/users', method: 'POST' });
res.status(500).sendData({ error: 'Failed to register user' });
}
});
// ODHUNTER: Removed legacy /api/users/login and using the standard RESTful endpoint instead
app.post('/api/auth/login',
authLimiter,
validateSchema('loginUser'),
validateRequest.body({
username: { type: 'string', required: true },
password: { type: 'string', required: true }
}),
async (req, res) => {
try {
const { username, password } = req.sanitizedBody;
if (db.isAccountLocked(username)) {
const user = db.getUserByUsername(username);
const lockedUntil = user ? new Date(user.locked_until) : null;
return res.status(423).sendData({
error: 'Account is temporarily locked due to too many failed login attempts',
lockedUntil: lockedUntil ? lockedUntil.toISOString() : null
});
}
const user = db.getUserByUsername(username);
if (!user) {
return res.status(401).sendData({ error: 'Invalid credentials' });
}
const isValid = await bcrypt.compare(password, user.hashed_password);
if (!isValid) {
db.incrementFailedLoginAttempts(username);
const updatedUser = db.getUserByUsername(username);
const MAX_FAILED_ATTEMPTS = 5;
if (updatedUser && updatedUser.failed_login_attempts >= MAX_FAILED_ATTEMPTS) {
db.lockAccount(username, 30);
return res.status(423).sendData({
error: 'Account has been locked due to too many failed login attempts',
lockedUntil: new Date(Date.now() + 30 * 60 * 1000).toISOString()
});
}
return res.status(401).sendData({ error: 'Invalid credentials' });
}
db.resetFailedLoginAttempts(username);
const token = generateToken(user);
res.sendData({
id: user.id,
userId: user.id,
username: user.username,
role: user.role,
token: token,
expiresIn: '24h',
_links: {
verify: { href: '/api/auth/verify', method: 'GET' },
auctions: { href: '/api/auctions', method: 'GET' }
}
}, 'loginResponse');
} catch (error) {
logError('Error logging in user:', error, { endpoint: '/api/auth/login', method: 'POST' });
res.status(500).sendData({ error: 'Failed to login' });
}
});
// ODHUNTER: Removed legacy /api/users/logout and using the standard RESTful endpoint instead
app.post('/api/auth/logout', authenticateToken, (req, res) => {
try {
const authHeader = req.headers['authorization'];
const token = authHeader && authHeader.split(' ')[1];
if (token) {
tokenBlacklist.add(token);
}
res.sendData({
message: 'Logged out successfully',
_links: {
login: { href: '/api/auth/login', method: 'POST' }
}
}, 'logoutResponse');
} catch (error) {
res.status(500).sendData({ error: 'Failed to logout' });
}
});
// ODHUNTER: Removed legacy /api/users/verify and using the standard RESTful endpoint instead
app.get('/api/auth/verify', authenticateToken, (req, res) => {
res.sendData({
valid: true,
user: {
userId: req.user.userId,
username: req.user.username
},
_links: {
self: { href: '/api/auth/verify', method: 'GET' },
logout: { href: '/api/auth/logout', method: 'POST' }
}
}, 'verifyResponse');
});
// Account lockout status endpoint
app.get('/api/users/lockout-status',
validateSchema('lockoutStatus'),
validateRequest.query({
username: { type: 'string', required: true }
}),
(req, res) => {
try {
const { username } = req.sanitizedQuery;
const user = db.getUserByUsername(username);
if (!user) {
return res.status(404).json({ error: 'User not found' });
}
const isLocked = db.isAccountLocked(username);
res.json({
username: user.username,
isLocked,
failedLoginAttempts: user.failed_login_attempts || 0,
lastFailedLogin: user.last_failed_login,
lockedUntil: user.locked_until,
_links: {
self: { href: `/api/users/lockout-status?username=${username}` }
}
});
} catch (error) {
logError('Error checking lockout status:', error, { endpoint: '/api/users/lockout-status', method: 'GET' });
res.status(500).json({ error: 'Failed to check lockout status' });
}
});