1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-22 05:19:22 +00:00

Extract user token

This commit is contained in:
Pierre HUBERT 2020-03-29 18:40:33 +02:00
parent eaa0e704bb
commit fa5335f156

View File

@ -17,6 +17,7 @@ interface PendingRequests {
// Tokens are valid only 10 seconds after they are generated // Tokens are valid only 10 seconds after they are generated
const TOKENS_DURATION = 10 const TOKENS_DURATION = 10
const TOKEN_LENGTH = 20
export class UserWebSocketController { export class UserWebSocketController {
@ -43,7 +44,7 @@ export class UserWebSocketController {
this.CleanList(); this.CleanList();
// Generate a temporary token // Generate a temporary token
const token = randomStr(20); const token = randomStr(TOKEN_LENGTH);
// Add the token to the list // Add the token to the list
this.pending_list.push({ this.pending_list.push({
@ -65,6 +66,30 @@ export class UserWebSocketController {
*/ */
public static async UserWS(req: Request, ws: ws) { public static async UserWS(req: Request, ws: ws) {
this.CleanList(); 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)
} }
} }