From 4342da67498a61fe87e6dfb7846dcef4f476524b Mon Sep 17 00:00:00 2001 From: simplonco Date: Sun, 15 Jan 2023 02:11:45 +0100 Subject: [PATCH] modify password ok --- README.md | 2 +- .../api_back/src/chat/chat.controller.ts | 55 ++++++++- .../nestjs/api_back/src/chat/chat.service.ts | 105 +++++++++--------- .../src/pieces/chat/Chat_layouts.svelte | 3 + .../src/pieces/chat/Layout_password.svelte | 22 +++- .../src/pieces/chat/Layout_room_set.svelte | 2 +- .../src/pieces/chat/Request_rooms.ts | 46 +++++++- .../api_front/src/pieces/chat/Types_chat.ts | 2 +- 8 files changed, 171 insertions(+), 66 deletions(-) diff --git a/README.md b/README.md index df043e84..300c2d9d 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,7 @@ - [/] leave room - [/] leave direct impossible - [/] protect room with password -- [ ] add and change password in room +- [/] add, change, and remove password in room - [ ] make admin - [ ] ban - [ ] mute diff --git a/srcs/requirements/nestjs/api_back/src/chat/chat.controller.ts b/srcs/requirements/nestjs/api_back/src/chat/chat.controller.ts index 4f789b25..a2570d8d 100644 --- a/srcs/requirements/nestjs/api_back/src/chat/chat.controller.ts +++ b/srcs/requirements/nestjs/api_back/src/chat/chat.controller.ts @@ -140,8 +140,11 @@ export class ChatController { if (room.protection) { + let message = `${req.user.username} changed the password`; + room.allowed_users = [req.user.username]; + await this.chatService.setPassword(req.user.username, message, room); let socket: socketDto = this.chatGateway.sockets.get(req.user.username); - await this.chatService.addPassword(req.user.username, room, socket); + await socket.to(socket.room).emit('message', "SERVER", message); } const ret_room = this.format_room(room); @@ -255,8 +258,56 @@ export class ChatController { { printCaller("- in "); + let message = `${req.user.username} changed the password`; + room.allowed_users = [req.user.username]; + room.protection = true; + await this.chatService.setPassword(req.user.username, message, room, old_password); + + // inform other connected users let socket: socketDto = this.chatGateway.sockets.get(req.user.username); - await this.chatService.addPassword(req.user.username, room, socket, old_password); + await socket.to(socket.room).emit('message', "SERVER", message); + + const ret_room = this.format_room(room); + res.status(HttpStatus.OK).json({ room: ret_room }); + printCaller("- out "); + } + + @UseGuards(AuthenticateGuard) + @UseGuards(TwoFactorGuard) + @Post('addpassword') + async addPassword(@Body() room: roomDto, @Req() req, @Res() res): Promise + { + printCaller("- in "); + + let message = `${req.user.username} added a password`; + room.allowed_users = [req.user.username]; + room.protection = true; + await this.chatService.setPassword(req.user.username, message, room); + + // inform other connected users + let socket: socketDto = this.chatGateway.sockets.get(req.user.username); + await socket.to(socket.room).emit('message', "SERVER", message); + + const ret_room = this.format_room(room); + res.status(HttpStatus.OK).json({ room: ret_room }); + printCaller("- out "); + } + + @UseGuards(AuthenticateGuard) + @UseGuards(TwoFactorGuard) + @Delete('removepassword') + async removePassword(@Body() room: roomDto, @Req() req, @Res() res): Promise + { + printCaller("- in "); + + let message = `${req.user.username} removed a new password`; + room.allowed_users = []; + room.protection = false; + await this.chatService.setPassword(req.user.username, message, room); + + // inform other connected users + let socket: socketDto = this.chatGateway.sockets.get(req.user.username); + await socket.to(socket.room).emit('message', "SERVER", message); const ret_room = this.format_room(room); res.status(HttpStatus.OK).json({ room: ret_room }); diff --git a/srcs/requirements/nestjs/api_back/src/chat/chat.service.ts b/srcs/requirements/nestjs/api_back/src/chat/chat.service.ts index d6001b6d..e094920f 100644 --- a/srcs/requirements/nestjs/api_back/src/chat/chat.service.ts +++ b/srcs/requirements/nestjs/api_back/src/chat/chat.service.ts @@ -206,6 +206,60 @@ export class ChatService { await this.chatroomRepository.save(room_db); } + async setPassword(username: string, message: string, room: roomDto, old_password?: string): Promise + { + printCaller("-- in "); + + if (room.type === 'direct') + { + console.log("throw error: error: true, code: 'DIRECT_PASSWORD_FORBIDDEN', message: 'you cannot set a password in a direct message room'"); + throw new HttpException({ error: true, code: 'DIRECT_PASSWORD_FORBIDDEN', message: `you cannot set a password in a direct message room` }, HttpStatus.FORBIDDEN); + } + + const current_room = await this.getRoomByName(room.name); + + if (!room.password) + { + console.log("throw error: error: true, code: 'NO_PASSWORD', message: 'this room has no password protection'"); + throw new HttpException({ error: true, code: 'NO_PASSWORD', message: `this room has no password protection` }, HttpStatus.FORBIDDEN); + } + if (current_room.protection) + { + if (room.protection && !old_password) + { + console.log("throw error: error: true, code: 'MISSING_OLD_PASSWORD', message: 'you need to provide the old password to set a new one'"); + throw new HttpException({ error: true, code: 'MISSING_OLD_PASSWORD', message: `you need to provide the old password to set a new one` }, HttpStatus.FORBIDDEN); + } + if (old_password) + { + const is_match = await bcrypt.compare(old_password, current_room.hash); + if (!is_match) + { + printCaller(`throw error: error: true, code: 'BAD_PASSWORD', message: 'you provided a bad password'`); + throw new HttpException({ error: true, code: 'BAD_PASSWORD', message: `you provided a bad password` }, HttpStatus.BAD_REQUEST); + } + } + } + + const saltOrRounds = 10; + const password = room.password; + let hash: string; + if (room.protection) + hash = await bcrypt.hash(password, saltOrRounds); + + // add password to chatroom + current_room.allowed_users = room.allowed_users; + current_room.protection = room.protection; + if (room.protection) + current_room.hash = hash; + else + delete current_room.hash; + current_room.messages.push({ name: "SERVER", message: message }); + await this.chatroomRepository.save(current_room); + + printCaller("-- out "); + } + /* ADDERS ************************************************* */ @@ -252,57 +306,6 @@ export class ChatService { return room; } - async addPassword(username: string, room: roomDto, socket:socketDto, old_password?: string): Promise - { - printCaller("-- in "); - - if (room.type === 'direct') - { - console.log("throw error: error: true, code: 'DIRECT_PASSWORD_FORBIDDEN', message: 'you cannot set a password in a direct message room'"); - throw new HttpException({ error: true, code: 'DIRECT_PASSWORD_FORBIDDEN', message: `you cannot set a password in a direct message room` }, HttpStatus.FORBIDDEN); - } - - const current_room = await this.getRoomByName(room.name); - - let message = `${username} set a new password`; - - if (current_room.protection) - { - if (!old_password) - { - console.log("throw error: error: true, code: 'MISSING_OLD_PASSWORD', message: 'you need to provide the old password to set a new one'"); - throw new HttpException({ error: true, code: 'MISSING_OLD_PASSWORD', message: `you need to provide the old password to set a new one` }, HttpStatus.FORBIDDEN); - } - const is_match = await bcrypt.compare(old_password, current_room.hash); - if (!is_match) - { - printCaller(`throw error: error: true, code: 'BAD_PASSWORD', message: 'you provided a bad password'`); - throw new HttpException({ error: true, code: 'BAD_PASSWORD', message: `you provided a bad password` }, HttpStatus.BAD_REQUEST); - } - } - - const saltOrRounds = 10; - const password = room.password; - let hash = await bcrypt.hash(password, saltOrRounds); - - // add password to chatroom - if (!current_room.allowed_users.includes(username)) - current_room.allowed_users.push(username); - current_room.protection = true; - current_room.hash = hash; - current_room.messages.push({ name: "SERVER", message: message }); - await this.chatroomRepository.save(current_room); - - console.log("current_room:", current_room); - const all_rooms = await this.getAllRooms(); - console.log("all_rooms:", all_rooms); - - // inform other connected users - await socket.to(socket.room).emit('message', "SERVER", message); - - printCaller("-- out "); - } - async addMessageToRoom(room_name: string, username: string, message: string): Promise { printCaller("-- in "); diff --git a/srcs/requirements/svelte/api_front/src/pieces/chat/Chat_layouts.svelte b/srcs/requirements/svelte/api_front/src/pieces/chat/Chat_layouts.svelte index 68f4fe16..55ae3216 100644 --- a/srcs/requirements/svelte/api_front/src/pieces/chat/Chat_layouts.svelte +++ b/srcs/requirements/svelte/api_front/src/pieces/chat/Chat_layouts.svelte @@ -67,6 +67,9 @@ {:else if $layout === "password"} + + + {:else if $layout === "add_password"} {:else if $layout === "change_password"} diff --git a/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_password.svelte b/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_password.svelte index 95524f5e..d2d87f92 100644 --- a/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_password.svelte +++ b/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_password.svelte @@ -1,13 +1,13 @@