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 5911f152..a2570d8d 100644 --- a/srcs/requirements/nestjs/api_back/src/chat/chat.controller.ts +++ b/srcs/requirements/nestjs/api_back/src/chat/chat.controller.ts @@ -123,19 +123,30 @@ export class ChatController { throw new HttpException({ error: true, code: 'FORBIDDEN_CHARACTERS', message: `Your room name can not contains these characters : ${forbidden_chars}` }, HttpStatus.UNPROCESSABLE_ENTITY); } + // check for password protection if (typeof room.protection === 'undefined') room.protection = false; else if (room.protection === true) { if (!room.password || room.password.length === 0) { - printCaller(`throw error: error: true, code: 'PASSWORD_TOO_SHORT', message: 'your password is too short'`); - throw new HttpException({ error: true, code: 'PASSWORD_TOO_SHORT', message: `your password is too short` }, HttpStatus.UNPROCESSABLE_ENTITY); + printCaller(`throw error: error: true, code: 'PASSWORD_INVALID', message: 'your password is invalid'`); + throw new HttpException({ error: true, code: 'PASSWORD_INVALID', message: `your password is invalid` }, HttpStatus.UNPROCESSABLE_ENTITY); } } + room.users = [req.user.username]; await this.chatService.addUserToNewRoom(req.user.username, room); + 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 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 "); @@ -196,6 +207,7 @@ export class ChatController { let fields = ["protection", "allowed_users"]; const room_db = await this.chatService.getRoomByName(room.name, fields); + if (room_db.protection === true) { if (!room_db.allowed_users.includes(req.user.username)) @@ -216,8 +228,8 @@ export class ChatController { @UseGuards(AuthenticateGuard) @UseGuards(TwoFactorGuard) - @Post('password') - async setPassword(@Body() room: roomDto, @Req() req, @Res() res): Promise + @Post('passwordauth') + async passwordAuthentication(@Body() room: roomDto, @Req() req, @Res() res): Promise { printCaller("- in "); @@ -239,6 +251,69 @@ export class ChatController { printCaller("- out "); } + @UseGuards(AuthenticateGuard) + @UseGuards(TwoFactorGuard) + @Post('changepassword') + async changePassword(@Body('room') room: roomDto, @Body('old_password') old_password: string, @Req() req, @Res() res): Promise + { + 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 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 }); + printCaller("- out "); + } + @UseGuards(AuthenticateGuard) @UseGuards(TwoFactorGuard) @Post('invite') @@ -302,6 +377,13 @@ export class ChatController { const room_name = await this.chatService.getCurrentRoomName(req.user.username); const room = await this.chatService.getRoomByName(room_name); const users = room.users; + + let index = users.indexOf(req.user.username); + if (index > -1) + { + users.splice(index, 1); + } + res.status(HttpStatus.OK).json({ users: users }); printCaller("- out "); } 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 29897b88..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 ************************************************* */ @@ -221,19 +275,6 @@ export class ChatService { throw new HttpException({ error: true, code: 'ROOM_CONFLICT', message: `This room name already exist` }, HttpStatus.CONFLICT); } - let hash; - if (room.protection) - { - 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 saltOrRounds = 10; - const password = room.password; - hash = await bcrypt.hash(password, saltOrRounds); - } - // create chatroom let newChatroom = new Chatroom(); newChatroom.name = room.name; @@ -241,12 +282,7 @@ export class ChatService { newChatroom.owner = username; newChatroom.users = room.users; newChatroom.allowed_users = []; - newChatroom.protection = room.protection; - if (room.protection) - { - newChatroom.hash = hash; - newChatroom.allowed_users.push(username); - } + newChatroom.protection = false; newChatroom.messages = [ { name: "SERVER", message: `creation of room ${room.name}` }, diff --git a/srcs/requirements/svelte/api_front/public/global.css b/srcs/requirements/svelte/api_front/public/global.css index 3383d87c..b07ffb9c 100644 --- a/srcs/requirements/svelte/api_front/public/global.css +++ b/srcs/requirements/svelte/api_front/public/global.css @@ -1,4 +1,5 @@ + html, body { position: relative; width: 100%; @@ -12,11 +13,23 @@ body { box-sizing: border-box; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif; /* tmp? */ - background: bisque; + background-color: #333; display: flex; flex-direction: column; } +@font-face { + font-family: "Bit5x3"; + src: + url("./fonts/Bit5x3.woff2") format("woff2"), + local("Bit5x3"), + url("./fonts/Bit5x3.woff") format("woff"); + font-weight: normal; + font-style: normal; + font-display: swap; +} + + a { color: rgb(0,100,200); text-decoration: none; @@ -30,6 +43,13 @@ a:visited { color: rgb(0,80,160); } +.background-pages { + background-color: #333; + font-family: "Bit5x3"; + font-size: 2vw; + color: white; +} + label { display: block; } diff --git a/srcs/requirements/svelte/api_front/src/pages/NotFound.svelte b/srcs/requirements/svelte/api_front/src/pages/NotFound.svelte index f34342e6..da6072d3 100644 --- a/srcs/requirements/svelte/api_front/src/pages/NotFound.svelte +++ b/srcs/requirements/svelte/api_front/src/pages/NotFound.svelte @@ -1,10 +1,26 @@ -

We are sorry!

-

This isn't a url that we use.

-

Go home you're drunk.

- -

Take me home →

-
\ No newline at end of file + +
+
+

We are sorry!

+

This isn't a url that we use.

+

Go home you're drunk.

+
+
+ + \ No newline at end of file diff --git a/srcs/requirements/svelte/api_front/src/pages/SplashPage.svelte b/srcs/requirements/svelte/api_front/src/pages/SplashPage.svelte index 996728b8..f3dccddf 100644 --- a/srcs/requirements/svelte/api_front/src/pages/SplashPage.svelte +++ b/srcs/requirements/svelte/api_front/src/pages/SplashPage.svelte @@ -78,8 +78,8 @@ .button-in { background-color: #8c0000; - border-color: black; - border-width: 4px; + border-color: #071013; + border-width: 2px; color: white; font-family: "Bit5x3"; font-size: x-large; @@ -88,8 +88,8 @@ .button-out { background-color: #008c8c; - border-color: black; - border-width: 4px; + border-color: #071013; + border-width: 2px; color: white; font-family: "Bit5x3"; font-size: x-large; diff --git a/srcs/requirements/svelte/api_front/src/pages/game/Ranking.svelte b/srcs/requirements/svelte/api_front/src/pages/game/Ranking.svelte index 5585b051..af39892f 100644 --- a/srcs/requirements/svelte/api_front/src/pages/game/Ranking.svelte +++ b/srcs/requirements/svelte/api_front/src/pages/game/Ranking.svelte @@ -28,7 +28,7 @@

- +
@@ -59,6 +59,7 @@
+
diff --git a/srcs/requirements/svelte/api_front/src/pages/profile/ProfileFriends.svelte b/srcs/requirements/svelte/api_front/src/pages/profile/ProfileFriends.svelte index 71c780c6..8dc48d37 100644 --- a/srcs/requirements/svelte/api_front/src/pages/profile/ProfileFriends.svelte +++ b/srcs/requirements/svelte/api_front/src/pages/profile/ProfileFriends.svelte @@ -24,22 +24,10 @@ onMount( async() => { - // DO I ACTUALLY NEED TO ON MOUNT ALL THIS STUFF? - // ALSO I COULD JUST USE THE FUNCITONS I MADE... - - - // yea no idea what - // i mean do i fetch user? i will for now user = await fetch(`http://${process.env.WEBSITE_HOST}:${process.env.WEBSITE_PORT}/api/v2/user`) .then( (x) => x.json() ); fetchAll(); - // ok this shit works! - // const interval = setInterval(() => { - // fetchAll(); - // }, 1000); - - // return () => clearInterval(interval); }); const fetchAll = async() => { @@ -234,6 +222,7 @@ +
@@ -353,7 +342,7 @@
- + diff --git a/srcs/requirements/svelte/api_front/src/pieces/chat/Chat.svelte b/srcs/requirements/svelte/api_front/src/pieces/chat/Chat.svelte index 06b2ef15..ad691fc8 100644 --- a/srcs/requirements/svelte/api_front/src/pieces/chat/Chat.svelte +++ b/srcs/requirements/svelte/api_front/src/pieces/chat/Chat.svelte @@ -1,7 +1,7 @@ - diff --git a/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_home.svelte b/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_home.svelte index 096d2733..d2d2d6d4 100644 --- a/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_home.svelte +++ b/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_home.svelte @@ -16,7 +16,7 @@ if (room.protection && !room.allowed) { await current_room.set(room); - layout.set("protected"); + layout.set("password"); } else { @@ -55,7 +55,7 @@

rooms are loading...

{:then rooms} {#each rooms as room} - {/each} diff --git a/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_invite.svelte b/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_invite.svelte index 36aa78a5..10b86d4b 100644 --- a/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_invite.svelte +++ b/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_invite.svelte @@ -52,7 +52,7 @@

users are loading...

{:then users} {#each users as user} - {/each} diff --git a/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_new.svelte b/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_new.svelte index 77c5af26..8a845b5d 100644 --- a/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_new.svelte +++ b/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_new.svelte @@ -17,7 +17,7 @@ if (updated_room.protection) { current_room.set(updated_room); - layout.set("protected"); + layout.set("password"); } else await change_room(updated_room); @@ -56,7 +56,7 @@

rooms are loading...

{:then rooms} {#each rooms as room} - {/each} diff --git a/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_protected.svelte b/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_password.svelte similarity index 50% rename from srcs/requirements/svelte/api_front/src/pieces/chat/Layout_protected.svelte rename to srcs/requirements/svelte/api_front/src/pieces/chat/Layout_password.svelte index 201582d2..d2d87f92 100644 --- a/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_protected.svelte +++ b/srcs/requirements/svelte/api_front/src/pieces/chat/Layout_password.svelte @@ -1,14 +1,22 @@ - -
-

temp, for testing :

- - - - - - - - - - - -