Completely getting rid of "userid"

Previously, userid was generated by the "/login" route and passed along.
This commit completely removes the uuid "userid" (and disables the LoginController too and any Jwt check).

"userid" is replaced by the "socket id" of the connection.
So a user is now identified using a socket id, which is unique for a given connection.
This commit is contained in:
David Négrier 2020-05-14 23:19:48 +02:00
parent 76b43245c5
commit 4de552437d
13 changed files with 130 additions and 84 deletions

View file

@ -4,7 +4,7 @@ import {BAD_REQUEST, OK} from "http-status-codes";
import {SECRET_KEY, URL_ROOM_STARTED} from "../Enum/EnvironmentVariable"; //TODO fix import by "_Enum/..."
import { uuid } from 'uuidv4';
export class AuthenticateController{
export class AuthenticateController {
App : Application;
constructor(App : Application) {
@ -14,7 +14,8 @@ export class AuthenticateController{
//permit to login on application. Return token to connect on Websocket IO.
login(){
this.App.post("/login", (req: Request, res: Response) => {
// For now, let's completely forget the /login route.
/*this.App.post("/login", (req: Request, res: Response) => {
let param = req.body;
if(!param.email){
return res.status(BAD_REQUEST).send({
@ -29,6 +30,6 @@ export class AuthenticateController{
mapUrlStart: URL_ROOM_STARTED,
userId: userId,
});
});
});*/
}
}
}

View file

@ -14,6 +14,7 @@ import {UserInterface} from "_Model/UserInterface";
enum SockerIoEvent {
CONNECTION = "connection",
DISCONNECT = "disconnect",
ATTRIBUTE_USER_ID = "attribute-user-id", // Sent from server to client just after the connexion is established to give the client its unique id.
JOIN_ROOM = "join-room",
USER_POSITION = "user-position",
WEBRTC_SIGNAL = "webrtc-signal",
@ -33,7 +34,8 @@ export class IoSocketController {
this.Io = socketIO(server);
// Authentication with token. it will be decoded and stored in the socket.
this.Io.use((socket: Socket, next) => {
// Completely commented for now, as we do not use the "/login" route at all.
/*this.Io.use((socket: Socket, next) => {
if (!socket.handshake.query || !socket.handshake.query.token) {
return next(new Error('Authentication error'));
}
@ -47,7 +49,7 @@ export class IoSocketController {
(socket as ExSocketInterface).token = tokenDecoded;
next();
});
});
});*/
this.ioConnection();
this.shareUsersPosition();
@ -74,6 +76,7 @@ export class IoSocketController {
let userId = lastUser.id;
let client: ExSocketInterface|null = this.searchClientById(userId);
if (client === null) {
console.warn('Could not find client ', userId, ' in group')
return;
}
let roomId = client.roomId;
@ -180,7 +183,6 @@ export class IoSocketController {
socket.leave(Client.webRtcRoomId);
//delete all socket information
delete Client.userId;
delete Client.webRtcRoomId;
delete Client.roomId;
delete Client.token;
@ -190,6 +192,9 @@ export class IoSocketController {
console.error(e);
}
});
// Let's send the user id to the user
socket.emit(SockerIoEvent.ATTRIBUTE_USER_ID, socket.id);
});
}
@ -201,11 +206,12 @@ export class IoSocketController {
let clients: Array<any> = Object.values(this.Io.sockets.sockets);
for (let i = 0; i < clients.length; i++) {
let client: ExSocketInterface = clients[i];
if (client.userId !== userId) {
continue
if (client.id !== userId) {
continue;
}
return client;
}
console.log("Could not find user with id ", userId);
return null;
}
@ -216,7 +222,7 @@ export class IoSocketController {
let clients: Array<any> = Object.values(this.Io.sockets.sockets);
for (let i = 0; i < clients.length; i++) {
let client: ExSocketInterface = clients[i];
if (client.userId !== userId) {
if (client.id !== userId) {
continue
}
return client;
@ -230,7 +236,7 @@ export class IoSocketController {
*/
sendDisconnectedEvent(Client: ExSocketInterface) {
Client.broadcast.emit(SockerIoEvent.WEBRTC_DISCONNECT, JSON.stringify({
userId: Client.userId
userId: Client.id
}));
//disconnect webrtc room
@ -248,14 +254,16 @@ export class IoSocketController {
leaveRoom(Client : ExSocketInterface){
//lease previous room and world
if(Client.roomId){
//user leave previous room
Client.leave(Client.roomId);
//user leave previous world
let world : World|undefined = this.Worlds.get(Client.roomId);
if(world){
console.log('Entering world.leave')
world.leave(Client);
this.Worlds.set(Client.roomId, world);
//this.Worlds.set(Client.roomId, world);
}
//user leave previous room
Client.leave(Client.roomId);
delete Client.roomId;
}
}
/**
@ -293,7 +301,7 @@ export class IoSocketController {
});
});
//join world
world.join(messageUserPosition);
world.join(Client, messageUserPosition);
this.Worlds.set(messageUserPosition.roomId, world);
}
@ -322,11 +330,11 @@ export class IoSocketController {
clients.forEach((client: ExSocketInterface, index: number) => {
let clientsId = clients.reduce((tabs: Array<any>, clientId: ExSocketInterface, indexClientId: number) => {
if (!clientId.userId || clientId.userId === client.userId) {
if (!clientId.id || clientId.id === client.id) {
return tabs;
}
tabs.push({
userId: clientId.userId,
userId: clientId.id,
name: clientId.name,
initiator: index <= indexClientId
});
@ -341,7 +349,7 @@ export class IoSocketController {
saveUserInformation(socket: ExSocketInterface, message: MessageUserPosition) {
socket.position = message.position;
socket.roomId = message.roomId;
socket.userId = message.userId;
//socket.userId = message.userId;
socket.name = message.name;
socket.character = message.character;
}
@ -354,9 +362,9 @@ export class IoSocketController {
}
rooms.refreshUserPosition(rooms, this.Io);
// update position in the worl
// update position in the world
let data = {
userId: Client.userId,
userId: Client.id,
roomId: Client.roomId,
position: Client.position,
name: Client.name,
@ -367,7 +375,7 @@ export class IoSocketController {
if (!world) {
return;
}
world.updatePosition(messageUserPosition);
world.updatePosition(Client, messageUserPosition);
this.Worlds.set(messageUserPosition.roomId, world);
}

View file

@ -1,11 +1,12 @@
import {Socket} from "socket.io";
import {PointInterface} from "./PointInterface";
import {Identificable} from "./Identificable";
export interface ExSocketInterface extends Socket {
export interface ExSocketInterface extends Socket, Identificable {
token: any;
roomId: string;
webRtcRoomId: string;
userId: string;
//userId: string;
name: string;
character: string;
position: PointInterface;

View file

@ -22,7 +22,7 @@ let RefreshUserPositionFunction = function(rooms : ExtRooms, Io: socketIO.Server
continue;
}
let data = {
userId: socket.userId,
userId: socket.id,
roomId: socket.roomId,
position: socket.position,
name: socket.name,

View file

@ -0,0 +1,3 @@
export interface Identificable {
id: string;
}

View file

@ -16,6 +16,7 @@ export class Message {
}
toJson() {
return {
userId: this.userId,
roomId: this.roomId,

View file

@ -5,6 +5,7 @@ import {Distance} from "./Distance";
import {UserInterface} from "./UserInterface";
import {ExSocketInterface} from "_Model/Websocket/ExSocketInterface";
import {PositionInterface} from "_Model/PositionInterface";
import {Identificable} from "_Model/Websocket/Identificable";
export type ConnectCallback = (user: string, group: Group) => void;
export type DisconnectCallback = (user: string, group: Group) => void;
@ -14,17 +15,17 @@ export type GroupUpdatedCallback = (group: Group) => void;
export type GroupDeletedCallback = (uuid: string, lastUser: UserInterface) => void;
export class World {
private minDistance: number;
private groupRadius: number;
private readonly minDistance: number;
private readonly groupRadius: number;
// Users, sorted by ID
private users: Map<string, UserInterface>;
private groups: Group[];
private readonly users: Map<string, UserInterface>;
private readonly groups: Group[];
private connectCallback: ConnectCallback;
private disconnectCallback: DisconnectCallback;
private groupUpdatedCallback: GroupUpdatedCallback;
private groupDeletedCallback: GroupDeletedCallback;
private readonly connectCallback: ConnectCallback;
private readonly disconnectCallback: DisconnectCallback;
private readonly groupUpdatedCallback: GroupUpdatedCallback;
private readonly groupDeletedCallback: GroupDeletedCallback;
constructor(connectCallback: ConnectCallback,
disconnectCallback: DisconnectCallback,
@ -47,25 +48,29 @@ export class World {
return this.groups;
}
public join(userPosition: MessageUserPosition): void {
this.users.set(userPosition.userId, {
id: userPosition.userId,
public join(socket : Identificable, userPosition: MessageUserPosition): void {
this.users.set(socket.id, {
id: socket.id,
position: userPosition.position
});
// Let's call update position to trigger the join / leave room
this.updatePosition(userPosition);
this.updatePosition(socket, userPosition);
}
public leave(user : ExSocketInterface){
public leave(user : Identificable){
let userObj = this.users.get(user.id);
if (userObj !== undefined && typeof userObj.group !== 'undefined') {
this.leaveGroup(user);
if (userObj === undefined) {
// FIXME: this seems always wrong. I guess user.id is different from userPosition.userId
console.warn('User ', user.id, 'does not belong to world! It should!');
}
this.users.delete(user.userId);
if (userObj !== undefined && typeof userObj.group !== 'undefined') {
this.leaveGroup(userObj);
}
this.users.delete(user.id);
}
public updatePosition(userPosition: MessageUserPosition): void {
let user = this.users.get(userPosition.userId);
public updatePosition(socket : Identificable, userPosition: MessageUserPosition): void {
let user = this.users.get(socket.id);
if(typeof user === 'undefined') {
return;
}
@ -118,7 +123,6 @@ export class World {
throw new Error("The user is part of no group");
}
group.leave(user);
if (group.isEmpty()) {
this.groupDeletedCallback(group.getId(), user);
group.destroy();