diff --git a/src/controllers/UserWebSocketController.ts b/src/controllers/UserWebSocketController.ts index 6d08035..6e636d8 100644 --- a/src/controllers/UserWebSocketController.ts +++ b/src/controllers/UserWebSocketController.ts @@ -17,6 +17,7 @@ interface PendingRequests { // Tokens are valid only 10 seconds after they are generated const TOKENS_DURATION = 10 +const TOKEN_LENGTH = 20 export class UserWebSocketController { @@ -43,7 +44,7 @@ export class UserWebSocketController { this.CleanList(); // Generate a temporary token - const token = randomStr(20); + const token = randomStr(TOKEN_LENGTH); // Add the token to the list this.pending_list.push({ @@ -65,6 +66,30 @@ export class UserWebSocketController { */ public static async UserWS(req: Request, ws: ws) { this.CleanList(); + + // First, check for token + if(!req.query.hasOwnProperty("token") + || String(req.query.token).length != TOKEN_LENGTH) { + ws.send("Missing token!"); + ws.close(); + return; + } + + // Search appropriate connection + const token = req.query.token; + const entryIndex = this.pending_list.findIndex((el) => el.token == token); + + if(entryIndex == -1) { + ws.send("Invalid token!"); + ws.close(); + return; + } + + // Remove the entry from the array + const entry = this.pending_list[entryIndex]; + this.pending_list.splice(entryIndex, 1); + + console.log(entry) } }