diff --git a/back/src/chat/chat.gateway.ts b/back/src/chat/chat.gateway.ts index e80bd5c..ce13bfb 100644 --- a/back/src/chat/chat.gateway.ts +++ b/back/src/chat/chat.gateway.ts @@ -64,6 +64,10 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { throw new WsException('You are banned from this channel'); } const user = await this.userService.getFullUser(connect.UserId); + if (connect.socketKey !== user.socketKey) { + this.server.to(socket.id).emit('failedJoin', 'Wrong socket key'); + throw new WsException('Wrong socket key'); + } user.socketKey = ''; if (channel.password && channel.password !== '') { if ( @@ -89,13 +93,11 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { } @SubscribeMessage('leaveChannel') - async onLeaveChannel(socket: Socket): Promise { - console.log('socket %s has left channel', socket.id) - + async onLeaveChannel(socket: Socket): Promise { const connect = await this.connectedUserRepository.findOneBy({ socket: socket.id, }); - if (connect == null) return false; + if (connect == null) throw new WsException('You must be connected to the channel'); const channel = await this.chatService.getFullChannel(connect.channel); if (connect.user === channel.owner.ftId) { this.server.in(channel.id.toString()).emit('deleted'); @@ -106,11 +108,15 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { await this.chatService.save(channel); } await this.connectedUserRepository.delete({ socket: socket.id }); - return true; + console.log('socket %s has left channel', socket.id) } @SubscribeMessage('addMessage') async onAddMessage (socket: Socket, message: CreateMessageDto): Promise { + const connect = await this.connectedUserRepository.findOneBy({ + socket: socket.id, + }); + if (connect == null) throw new WsException('You must be connected to the channel'); let channel: Channel | null = null channel = await this.chatService.getChannel(message.ChannelId).catch(() => { return null }) if (channel == null) { @@ -128,6 +134,11 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { @SubscribeMessage('kickUser') async onKickUser(socket: Socket, kick: kickUserDto): Promise { + let connect = (await this.connectedUserRepository.findOneBy({ + socket: socket.id + })) + if (connect === null) + throw new WsException('You must be connected to the channel') const channel = await this.chatService.getFullChannel(kick.chan); if (channel.owner.ftId === kick.to) { throw new WsException('You cannot kick the owner of a channel'); @@ -138,13 +149,15 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { ) { throw new WsException('You do not have the required privileges') } - const user = (await this.userService.findUser(kick.to)) as User - const connect = (await this.connectedUserRepository.findOneBy({ - user: user.ftId + const target = (await this.userService.findUser(kick.to)) as User + connect = (await this.connectedUserRepository.findOneBy({ + user: target.ftId })) if (connect !== null) { - console.log(`kicking ${user.username} from ${channel.name} with socket ${connect.socket}`) + console.log(`kicking ${target.username} from ${channel.name} with socket ${connect.socket}`) this.server.to(connect.socket).emit('kicked') + } else { + throw new WsException('Target is not connected to the channel') } } } diff --git a/back/src/chat/dto/connection.dto.ts b/back/src/chat/dto/connection.dto.ts index c70f38c..03dd9d9 100644 --- a/back/src/chat/dto/connection.dto.ts +++ b/back/src/chat/dto/connection.dto.ts @@ -1,6 +1,10 @@ -import { IsNumber, IsOptional, IsString } from 'class-validator' +import { IsNotEmpty, IsNumber, IsOptional, IsString } from 'class-validator' export class ConnectionDto { + @IsString() + @IsNotEmpty() + socketKey: string + @IsNumber() UserId: number diff --git a/front/src/components/Chat.svelte b/front/src/components/Chat.svelte index b9249ea..c57942f 100644 --- a/front/src/components/Chat.svelte +++ b/front/src/components/Chat.svelte @@ -52,6 +52,7 @@ if (!channel.password) { const data: ConnectionDto = { UserId: $store.ftId, + socketKey: $store.socketKey, ChannelId: channel.id, pwd: "", }; @@ -65,6 +66,7 @@ } const data: ConnectionDto = { UserId: $store.ftId, + socketKey: $store.socketKey, ChannelId: channel.id, pwd: password, }; diff --git a/front/src/components/dtos/connection.dto.ts b/front/src/components/dtos/connection.dto.ts index 74ecae3..f502408 100644 --- a/front/src/components/dtos/connection.dto.ts +++ b/front/src/components/dtos/connection.dto.ts @@ -1,5 +1,6 @@ export class ConnectionDto { UserId: number + socketKey: string ChannelId: number pwd: string }