Skip to content

Commit

Permalink
implement login-required functionality (fixes #219)
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffrey-wu committed Sep 28, 2024
1 parent c7a5949 commit 6bfb3a4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion client/multiplayer/room.html
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ <h1 id="funny-toast-text" class="me-auto text-danger"></h1>
</div>
<div class="form-check form-switch">
<input class="form-check-input" id="toggle-login-required" type="checkbox" role="switch" disabled>
<label class="form-check-label" for="toggle-login-required">Require players to be logged in</label>
<label class="form-check-label" for="toggle-login-required">Require new players to be logged in</label>
</div>
<div class="mb-2"></div>
<label for="reading-speed">Reading speed: <span id="reading-speed-display">50</span><br></label>
Expand Down
19 changes: 19 additions & 0 deletions server/multiplayer/handle-wss-connection.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { checkToken } from '../authentication.js';
import hasValidCharacters from '../moderation/has-valid-characters.js';
import isAppropriateString from '../moderation/is-appropriate-string.js';
import { createAndReturnRoom } from './TossupRoom.js';
Expand Down Expand Up @@ -46,6 +47,24 @@ export default function handleWssConnection (ws, req) {
return false;
}

if (room.settings.loginRequired === true) {
const cookieString = (req?.headers?.cookie ?? 'session=;').split(';').find(token => token.trim().startsWith('session='));
const cookieBuffer = Buffer.from(cookieString.split('=')[1], 'base64');
let valid = true;
try {
const cookies = JSON.parse(cookieBuffer.toString('utf-8'));
valid = checkToken(cookies.username, cookies.token, true);
} catch (e) { valid = false; }

if (!valid) {
ws.send(JSON.stringify({
type: 'error',
message: 'You must be logged in with a verified email to join this room.'
}));
return false;
}
}

if (!isAppropriateString(username)) {
username = getRandomName();
ws.send(JSON.stringify({
Expand Down

0 comments on commit 6bfb3a4

Please sign in to comment.