converted to express, with sessions, no more login proc

This commit is contained in:
STEINNI
2025-09-15 19:13:05 +00:00
parent b4cd02add5
commit e7dcb7290e
5 changed files with 126 additions and 100 deletions
+6 -42
View File
@@ -25,33 +25,15 @@ export class WssConnexion {
this.socket.on('message', this.receive.bind(this));
if(this.debug) console.log(`Spawned new WSS connection to client: ${this.req.socket.remoteAddress}:${this.req.socket.remotePort}`)
console.log('Session infos:',this.socket.session.authenticated, this.socket.session.userInfos)
}
doLogin(){
this.cnxState = 'LOGIN' // then CONNECTED
this.challenge = crypto.randomUUID()
this.challengeTimeout = setTimeout(() => {
if(this.debug) console.warn(`Timeout waiting for login response for UUID ${this.uuid}, closing connection !`);
this.close()
}, this.config.server.challengeExpiration*1000)
this.send(JSON.stringify({
'action': 'LOGIN',
'challenge': this.challenge
}));
if(this.debug) console.log(`Sent LOGIN for UUID ${this.uuid} ==> challenge=${this.challenge}`)
return(new Promise((resolve, reject) => {
this.resolveLogin = resolve
}))
}
welcome(){
clearTimeout(this.challengeTimeout)
this.challengeTimeout = null
this.cnxState = 'CONNECTED'
if(this.debug) console.log(`Welcome to UUID ${this.uuid}`)
this.resolveLogin()
}
async checkLogin(userInfo, otp){
@@ -98,31 +80,13 @@ export class WssConnexion {
return;
}
if(this.cnxState == 'LOGIN'){
if((action=='LOGIN') && pdata.userInfo && pdata.otp) {
if(this.debug) console.log(`received login response : user=${pdata.userInfo} otp=${pdata.otp}`)
if(await this.checkLogin(pdata.userInfo, pdata.otp)) {
this.userId = pdata.userInfo
this.welcome()
} else {
if(this.debug) console.warn(`Bad OTP response to login request for uuid ${this.uuid}`);
this.send(JSON.stringify({
'action': 'LOGIN',
'logged': false
}));
this.close()
}
} else {
if(this.debug) console.warn(`Invalid response to login request for uuid ${this.uuid}`,pdata);
}
if(typeof this['action_'+action] == "function") {
if((this.debug) && (action != 'PONG')) console.warn(`${action} for uuid ${this.uuid}`);
this['action_'+action](action, payload, reqid);
} else {
if(typeof this['action_'+action] == "function") {
if((this.debug) && (action != 'PONG')) console.warn(`${action} for uuid ${this.uuid}`);
this['action_'+action](action, payload, reqid);
} else {
if(this.debug) console.warn(`Unknown action ${action} for UUID ${this.uuid}`);
}
if(this.debug) console.warn(`Unknown action ${action} for UUID ${this.uuid}`);
}
}
send(data) { // Send to Websocket