@@ -145,29 +145,29 @@ MongoDB.MongoClient.connect('mongodb://localhost:27017', {
145
145
146
146
// (2) Insert Validation
147
147
try {
148
- await db . command ( { collMod : "users" , validator : validators . users } )
149
- await db . command ( { collMod : "transactions" , validator : validators . transactions } )
150
- await db . command ( { collMod : "challs" , validator : validators . challs } )
148
+ await db . command ( { collMod : "users" , validator : validators . users } )
149
+ await db . command ( { collMod : "transactions" , validator : validators . transactions } )
150
+ await db . command ( { collMod : "challs" , validator : validators . challs } )
151
151
console . log ( "Validation inserted" )
152
152
}
153
- catch ( e ) { console . error ( e ) }
153
+ catch ( e ) { console . error ( e ) }
154
154
155
155
// (3) Create Indexes
156
156
if ( ( await collections . users . indexes ( ) ) . length === 1 ) {
157
157
// Users indexes
158
- collections . users . createIndex ( { "username" : 1 } , { unique : true , name : "username" } )
159
- collections . users . createIndex ( { "email" : 1 } , { unique : true , name : "email" } )
158
+ collections . users . createIndex ( { "username" : 1 } , { unique : true , name : "username" } )
159
+ collections . users . createIndex ( { "email" : 1 } , { unique : true , name : "email" } )
160
160
console . log ( "Users indexes created" )
161
161
}
162
162
if ( ( await collections . challs . indexes ( ) ) . length === 1 ) {
163
163
// Challs indexes
164
- collections . challs . createIndex ( { "category" : 1 , "visibility" : 1 } , { name : "catvis" } )
165
- collections . challs . createIndex ( { "name" : 1 } , { unique : true , name : "name" } )
164
+ collections . challs . createIndex ( { "category" : 1 , "visibility" : 1 } , { name : "catvis" } )
165
+ collections . challs . createIndex ( { "name" : 1 } , { unique : true , name : "name" } )
166
166
console . log ( "Challs indexes created" )
167
167
}
168
168
if ( ( await collections . transactions . indexes ( ) ) . length === 1 ) {
169
169
// Transcations indexes
170
- collections . transactions . createIndex ( { "author" : 1 , "challenge" : 1 , "type" : 1 } , { name : "userchall" } )
170
+ collections . transactions . createIndex ( { "author" : 1 , "challenge" : 1 , "type" : 1 } , { name : "userchall" } )
171
171
console . log ( "Transcations indexes created" )
172
172
}
173
173
@@ -368,6 +368,8 @@ MongoDB.MongoClient.connect('mongodb://localhost:27017', {
368
368
res . send ( { success : true } ) ;
369
369
}
370
370
else {
371
+ const user = await collections . users . findOne ( { username : userToDelete } , { projection : { password : 1 , _id : 0 } } ) ;
372
+ if ( ! ( await argon2 . verify ( user . password , req . body . password ) ) ) return res . send ( { success : false , error : "wrong-pass" } )
371
373
if ( ( await collections . users . deleteOne ( { username : userToDelete . toLowerCase ( ) } ) ) . deletedCount == 0 ) {
372
374
res . status ( 400 ) ;
373
375
res . send ( {
@@ -478,6 +480,31 @@ MongoDB.MongoClient.connect('mongodb://localhost:27017', {
478
480
errors ( err , res ) ;
479
481
}
480
482
} ) ;
483
+ app . post ( '/v1/account/adminChangePassword' , async ( req , res ) => {
484
+ try {
485
+ if ( req . headers . authorization == undefined ) throw new Error ( 'MissingToken' ) ;
486
+ const username = signer . unsign ( req . headers . authorization ) ;
487
+ if ( await checkPermissions ( username ) < 2 ) throw new Error ( 'Permissions' ) ;
488
+ if ( req . body . password == '' ) throw new Error ( 'EmptyPassword' ) ;
489
+ await collections . users . updateOne (
490
+ { username : req . body . username } ,
491
+ { '$set' : { password : await argon2 . hash ( req . body . password ) } }
492
+ ) ;
493
+ res . send ( { success : true } ) ;
494
+ }
495
+ catch ( err ) {
496
+ switch ( err . message ) {
497
+ case 'EmptyPassword' :
498
+ res . status ( 400 ) ;
499
+ res . send ( {
500
+ success : false ,
501
+ error : 'empty-password'
502
+ } ) ;
503
+ return ;
504
+ }
505
+ errors ( err , res ) ;
506
+ }
507
+ } ) ;
481
508
app . get ( '/v1/announcements/list/:version' , async ( req , res ) => {
482
509
try {
483
510
if ( req . headers . authorization == undefined ) throw new Error ( 'MissingToken' ) ;
@@ -1413,7 +1440,7 @@ MongoDB.MongoClient.connect('mongodb://localhost:27017', {
1413
1440
//websocket methods
1414
1441
wss . on ( 'connection' , ( socket ) => {
1415
1442
socket . isAlive = true
1416
- socket . on ( 'pong' , ( ) => { socket . isAlive = true } ) ; // check for any clients that dced without informing the server
1443
+ socket . on ( 'pong' , ( ) => { socket . isAlive = true } ) ; // check for any clients that dced without informing the server
1417
1444
1418
1445
socket . on ( "message" , async ( msg ) => {
1419
1446
const data = JSON . parse ( msg )
@@ -1430,31 +1457,31 @@ MongoDB.MongoClient.connect('mongodb://localhost:27017', {
1430
1457
return socket . terminate ( )
1431
1458
}
1432
1459
socket . isAuthed = true
1433
-
1460
+
1434
1461
if ( payload . lastChallengeID < cache . latestSolveSubmissionID ) {
1435
- const challengesToBeSent = await collections . transactions . find ( null , { projection : { _id : 0 , author : 1 , timestamp : 1 , points : 1 } } ) . sort ( { $natural :- 1 } ) . limit ( cache . latestSolveSubmissionID - payload . lastChallengeID ) . toArray ( ) ;
1436
- socket . send ( JSON . stringify ( { type : "init" , data : challengesToBeSent , lastChallengeID : cache . latestSolveSubmissionID } ) )
1462
+ const challengesToBeSent = await collections . transactions . find ( null , { projection : { _id : 0 , author : 1 , timestamp : 1 , points : 1 } } ) . sort ( { $natural : - 1 } ) . limit ( cache . latestSolveSubmissionID - payload . lastChallengeID ) . toArray ( ) ;
1463
+ socket . send ( JSON . stringify ( { type : "init" , data : challengesToBeSent , lastChallengeID : cache . latestSolveSubmissionID } ) )
1437
1464
}
1438
- else socket . send ( JSON . stringify ( { type : "init" , data : "up-to-date" } ) )
1465
+ else socket . send ( JSON . stringify ( { type : "init" , data : "up-to-date" } ) )
1439
1466
}
1440
1467
} )
1441
1468
} )
1442
1469
1443
1470
// check for any clients that dced without informing the server
1444
1471
const interval = setInterval ( function ping ( ) {
1445
1472
wss . clients . forEach ( function each ( ws ) {
1446
- if ( ws . isAlive === false ) return ws . terminate ( ) ;
1447
-
1448
- ws . isAlive = false ;
1449
- ws . ping ( ) ;
1473
+ if ( ws . isAlive === false ) return ws . terminate ( ) ;
1474
+
1475
+ ws . isAlive = false ;
1476
+ ws . ping ( ) ;
1450
1477
} ) ;
1451
- } , 30000 ) ;
1478
+ } , 30000 ) ;
1452
1479
1453
1480
wss . on ( 'close' , function close ( ) {
1454
1481
clearInterval ( interval ) ;
1455
- } ) ;
1482
+ } ) ;
1483
+
1456
1484
1457
-
1458
1485
1459
1486
1460
1487
} ) . catch ( err => {
0 commit comments