From 208e19baa4dc7ae20e4700b8270e22b43e4345cc Mon Sep 17 00:00:00 2001
From: Marius Friess <34072851+mariusfriess@users.noreply.github.com>
Date: Wed, 2 Aug 2023 20:21:00 +0200
Subject: [PATCH] Add password check when entering channel (#39)

---
 src/channel/channel.gateway.ts | 17 ++++++++++++-----
 src/channel/channel.service.ts |  5 +++++
 2 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/src/channel/channel.gateway.ts b/src/channel/channel.gateway.ts
index 6d12be3..0844336 100644
--- a/src/channel/channel.gateway.ts
+++ b/src/channel/channel.gateway.ts
@@ -51,17 +51,24 @@ export class ChannelGateway implements OnGatewayConnection {
     payload: {
       name: string;
       channelId: string;
+      password?: string;
     },
   ) {
     if (!this.channels.exists(payload.channelId)) {
       return { error: 'Der Raum konnte nicht gefunden werden' };
     }
 
-    const channel = await this.channels.joinAsStudent(
-      client,
-      payload.channelId,
-      payload.name,
-    );
+    let channel;
+    try {
+      channel = await this.channels.joinAsStudent(
+        client,
+        payload.channelId,
+        payload.name,
+        payload.password,
+      );
+    } catch (e) {
+      return { error: e.message };
+    }
 
     return this.channelState(channel);
   }
diff --git a/src/channel/channel.service.ts b/src/channel/channel.service.ts
index d4386e1..7c01a63 100644
--- a/src/channel/channel.service.ts
+++ b/src/channel/channel.service.ts
@@ -56,6 +56,7 @@ export class ChannelService {
     client: Socket,
     channelId: string,
     name: string,
+    password?: string,
   ): Promise<Channel> {
     const channel = this.channels[channelId];
 
@@ -63,6 +64,10 @@ export class ChannelService {
       throw new WsException('Channel not found');
     }
 
+    if (channel.room.password && channel.room.password !== password) {
+      throw new WsException('Wrong password');
+    }
+
     await channel.joinAsStudent(client, name);
 
     channel.clearCloseTimeout();
-- 
GitLab