From fa032036d7010cdc6a08d1db8163ac4cd606c414 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5var=20Aamb=C3=B8=20Fosstveit?= Date: Wed, 1 Apr 2020 21:58:41 +0200 Subject: [PATCH] Give client correct authentication status when joining a room. Fixes #166 --- app/src/RoomClient.js | 11 +++++++++-- server/lib/Peer.js | 19 +++++++++++++++++++ server/lib/Room.js | 5 +++-- server/server.js | 9 ++++++--- 4 files changed, 37 insertions(+), 7 deletions(-) diff --git a/app/src/RoomClient.js b/app/src/RoomClient.js index 14194d6..1f42c0d 100644 --- a/app/src/RoomClient.js +++ b/app/src/RoomClient.js @@ -2524,7 +2524,7 @@ export default class RoomClient canShareFiles : this._torrentSupport })); - const { roles, peers } = await this.sendRequest( + const { authenticated, roles, peers } = await this.sendRequest( 'join', { displayName : displayName, @@ -2532,7 +2532,14 @@ export default class RoomClient rtpCapabilities : this._mediasoupDevice.rtpCapabilities }); - logger.debug('_joinRoom() joined [peers:"%o", roles:"%o"]', peers, roles); + logger.debug( + '_joinRoom() joined [authenticated:"%s", peers:"%o", roles:"%o"]', + authenticated, + peers, + roles + ); + + store.dispatch(meActions.loggedIn(authenticated)); const myRoles = store.getState().me.roles; diff --git a/server/lib/Peer.js b/server/lib/Peer.js index 1878a61..f1dc1a3 100644 --- a/server/lib/Peer.js +++ b/server/lib/Peer.js @@ -25,6 +25,8 @@ class Peer extends EventEmitter this._inLobby = false; + this._authenticated = false; + this._roles = [ userRoles.ALL ]; this._displayName = false; @@ -146,6 +148,23 @@ class Peer extends EventEmitter this._inLobby = inLobby; } + get authenticated() + { + return this._authenticated; + } + + set authenticated(authenticated) + { + if (authenticated !== this._authenticated) + { + const oldAuthenticated = this._authenticated; + + this._authenticated = authenticated; + + this.emit('authenticationChanged', { oldAuthenticated }); + } + } + get roles() { return this._roles; diff --git a/server/lib/Room.js b/server/lib/Room.js index ed16f1a..f2a4e1a 100644 --- a/server/lib/Room.js +++ b/server/lib/Room.js @@ -554,8 +554,9 @@ class Room extends EventEmitter .map((joinedPeer) => (joinedPeer.peerInfo)); cb(null, { - roles : peer.roles, - peers : peerInfos + roles : peer.roles, + peers : peerInfos, + authenticated : peer.authenticated }); // Mark the new Peer as joined. diff --git a/server/server.js b/server/server.js index 3243280..542f3f0 100755 --- a/server/server.js +++ b/server/server.js @@ -354,10 +354,10 @@ async function setupAuth() if (!peer) // User has no socket session yet, make temporary peer = new Peer({ id: peerId, roomId }); - if (peer && peer.roomId !== roomId) // The peer is mischievous + if (peer.roomId !== roomId) // The peer is mischievous throw new Error('peer authenticated with wrong room'); - if (peer && typeof config.userMapping === 'function') + if (typeof config.userMapping === 'function') { await config.userMapping({ peer, @@ -366,6 +366,8 @@ async function setupAuth() }); } + peer.authenticated = true; + res.send(loginHelper({ displayName : peer.displayName, picture : peer.picture @@ -530,10 +532,11 @@ async function runWebSocketServer() _userinfo } = socket.handshake.session.passport.user; - peer.authId= id; + peer.authId = id; peer.displayName = displayName; peer.picture = picture; peer.email = email; + peer.authenticated = true; if (typeof config.userMapping === 'function') {