wip protected, already solved error http handling for createroom

This commit is contained in:
simplonco
2023-01-14 13:07:18 +01:00
parent 8e4ce8e9a8
commit 356e1caac3
7 changed files with 163 additions and 65 deletions

View File

@@ -111,8 +111,8 @@ export class ChatController {
if (test_regex.test(room.name) === false)
{
let forbidden_chars = room.name.replace(new RegExp(regex_base, "g"), "");
console.log(`throw error: Your room name can not contains these characters : ${forbidden_chars}`);
throw new HttpException( `Your room name can not contains these characters : ${forbidden_chars}`, HttpStatus.UNPROCESSABLE_ENTITY);
console.log(`throw error: display: true, code: 'FORBIDDEN_CHARACTERS', message: 'Your room name can not contains these characters : ${forbidden_chars}'`);
throw new HttpException({ display: true, code: 'FORBIDDEN_CHARACTERS', message: `Your room name can not contains these characters : ${forbidden_chars}` }, HttpStatus.OK);
}
if (typeof room.protection === 'undefined')
@@ -121,8 +121,8 @@ export class ChatController {
{
if (!room.password || room.password.length === 0)
{
console.log(`throw error: code: 'PASSWORD_BAD_FORMAT', message: 'your password is too short'`);
throw new HttpException({ code: 'PASSWORD_BAD_FORMAT', message: `your password is too short` }, HttpStatus.UNPROCESSABLE_ENTITY);
console.log(`throw error: display: true, code: 'PASSWORD_TOO_SHORT', message: 'your password is too short'`);
throw new HttpException({ display: true, code: 'PASSWORD_TOO_SHORT', message: `your password is too short` }, HttpStatus.OK);
}
}
room.users = [req.user.username];
@@ -156,18 +156,18 @@ export class ChatController {
const room_db = await this.chatService.getRoomByName(room.name, fields);
if (room_db.type === 'direct')
{
console.log("throw error: cannot join a direct messages room");
throw new HttpException( `cannot join a direct messages room`, HttpStatus.CONFLICT);
console.log("throw error: display: true, code: 'JOIN_DIRECT_FORBIDDEN', message: 'cannot join a direct messages room'");
throw new HttpException({ display: true, code: 'JOIN_DIRECT_FORBIDDEN', message: `cannot join a direct messages room` }, HttpStatus.OK);
}
if (room_db.type === 'private')
{
console.log("throw error: cannot join a private room");
throw new HttpException( `cannot join a private room`, HttpStatus.CONFLICT);
console.log("throw error: display: true, code: 'JOIN_PRIVATE_FORBIDDEN', message: 'cannot join a private room'");
throw new HttpException({ display: true, code: 'JOIN_PRIVATE_FORBIDDEN', message: `cannot join a private room` }, HttpStatus.OK);
}
if (room_db.users.includes(req.user.username))
{
console.log("throw error: your have already joined this room");
throw new HttpException( `your have already joined this room`, HttpStatus.CONFLICT);
console.log("throw error: display: true, code: 'ALREADY_JOIN', message: 'your have already joined this room'");
throw new HttpException({ display: true, code: 'ALREADY_JOIN', message: `your have already joined this room` }, HttpStatus.OK);
}
room = await this.chatService.addUserToRoom(req.user.username, room.name);
}
@@ -188,14 +188,42 @@ export class ChatController {
{
console.log("- in changeRoom controller");
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))
{
console.log("throw error: display: true, code: 'NEED_AUTHENTICATE', message: 'You didn't provide the password for this room'");
throw new HttpException({ display: true, code: 'NEED_AUTHENTICATE', message: `You didn't provide the password for this room` }, HttpStatus.OK);
}
}
await this.chatService.setCurrentRoom(req.user.username, room.name);
let socket: socketDto = this.chatGateway.sockets.get(req.user.username);
await this.chatService.socketChangeRoom(socket, room.name);
const ret_room = this.format_room(room);
res.status(HttpStatus.OK).json({ room: ret_room });
console.log("- out changeRoom controller");
}
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Post('password')
async setPassword(@Body() room: roomDto, @Req() req, @Res() res): Promise<void>
{
console.log("- in setPassword controller");
let fields = ["protection", "allowed_users"];
const room_db = await this.chatService.getRoomByName(room.name, fields);
if (room_db.protection === true)
{
if (!room.password)
{
console.log("throw error: code: 'PASSWORD_MISSING', message: 'this room is protected, you need to provide a password'");
throw new HttpException({ code: 'PASSWORD_MISSING', message: `this room is protected, you need to provide a password` }, HttpStatus.BAD_REQUEST);
console.log("throw error: display: true, code: 'PASSWORD_MISSING', message: 'this room is protected, you need to provide a password'");
throw new HttpException({ display: true, code: 'PASSWORD_MISSING', message: `this room is protected, you need to provide a password` }, HttpStatus.OK);
}
if (!room_db.allowed_users.includes(req.user.username))
await this.chatService.setPasswordValidation(req.user.username, room);
@@ -208,7 +236,7 @@ export class ChatController {
const ret_room = this.format_room(room);
res.status(HttpStatus.OK).json({ room: ret_room });
console.log("- out changeRoom controller");
console.log("- out setPassword controller");
}
@UseGuards(AuthenticateGuard)

View File

@@ -194,8 +194,8 @@ export class ChatService {
const is_match = await bcrypt.compare(room.password, room_db.hash);
if (!is_match)
{
console.log(`throw error: code: 'BAD_PASSWORD', message: 'bad password'`);
throw new HttpException({ code: 'BAD_PASSWORD', message: `bad password` }, HttpStatus.UNAUTHORIZED);
console.log(`throw error: display: true, code: 'BAD_PASSWORD', message: 'bad password'`);
throw new HttpException({ display: true, code: 'BAD_PASSWORD', message: `bad password` }, HttpStatus.OK);
}
room_db.allowed_users.push(username);
@@ -215,8 +215,8 @@ export class ChatService {
const find_room = await this.getRoomByName(room.name);
if (find_room)
{
console.log("throw error: This room name already exist");
throw new HttpException( `This room name already exist`, HttpStatus.CONFLICT);
console.log("throw error: display: true, code: 'ROOM_CONFLICT', message: 'This room name already exist'");
throw new HttpException({ display: true, code: 'ROOM_CONFLICT', message: `This room name already exist` }, HttpStatus.OK);
}
let hash;
@@ -225,8 +225,8 @@ export class ChatService {
console.log("in room protection hash");
if (room.type === 'direct')
{
console.log("throw error: code: 'DIRECT_PASSWORD_FORBIDDEN', message: 'you cannot set a password in a direct message room'");
throw new HttpException({ code: 'DIRECT_PASSWORD_FORBIDDEN', message: `you cannot set a password in a direct message room`}, HttpStatus.CONFLICT);
console.log("throw error: display: true, code: 'DIRECT_PASSWORD_FORBIDDEN', message: 'you cannot set a password in a direct message room'");
throw new HttpException({ display: true, code: 'DIRECT_PASSWORD_FORBIDDEN', message: `you cannot set a password in a direct message room`}, HttpStatus.OK);
}
const saltOrRounds = 10;
const password = room.password;
@@ -295,13 +295,13 @@ export class ChatService {
const room = await this.getRoomByName(room_name);
if (!room.users.includes(username))
{
console.log("throw error: your are not in this room");
throw new HttpException(`your are not in this room`, HttpStatus.CONFLICT);
console.log("throw error: display: true, code: 'USER_NOT_FOUND', message: 'your are not in this room'");
throw new HttpException({ display: true, code: 'USER_NOT_FOUND', message: `your are not in this room` }, HttpStatus.OK);
}
if (room.type === "direct")
{
console.log("throw error: code: 'LEAVING_DIRECT_FORBIDDEN', message: 'you cannot leave a direct messages conversation'");
throw new HttpException({ code: `LEAVING_DIRECT_FORBIDDEN`, message: `you cannot leave a direct messages conversation`, status: HttpStatus.CONFLICT }, HttpStatus.CONFLICT);
console.log("throw error: display: true, code: 'LEAVE_DIRECY_FORBIDDEN', message: 'you cannot leave a direct messages conversation'");
throw new HttpException({ display: true, code: 'LEAVE_DIRECY_FORBIDDEN', message: `you cannot leave a direct messages conversation` }, HttpStatus.OK);
}
// delete user from room