From a898bd9e4cb197bee59b265d62291ef2ed88e525 Mon Sep 17 00:00:00 2001 From: Chrono Date: Sat, 5 Aug 2023 01:25:09 -0400 Subject: [PATCH] testing --- import-sorter.json | 14 ---------- src/db/db.ts | 6 ++-- src/routes/api.ts | 16 ++++++++--- src/routes/auth.ts | 68 +++++++++++++++++++++++----------------------- src/server.ts | 54 +++++++++++++++++++++++++++++++----- src/socket.ts | 2 +- 6 files changed, 97 insertions(+), 63 deletions(-) delete mode 100644 import-sorter.json diff --git a/import-sorter.json b/import-sorter.json deleted file mode 100644 index e2d5bc3..0000000 --- a/import-sorter.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "importStringConfiguration": { - "trailingComma": "none", - "tabSize": 4, - "maximumNumberOfImportExpressionsPerLine": { - "count": 50 - } - }, - "sortConfiguration": { - "customOrderingRules": { - "defaultNumberOfEmptyLinesAfterGroup": 2 - } - } -} diff --git a/src/db/db.ts b/src/db/db.ts index ced5629..dd5a030 100644 --- a/src/db/db.ts +++ b/src/db/db.ts @@ -7,10 +7,10 @@ // external imports // import bcrypt from 'bcrypt'; import * as bcrypt from "https://deno.land/x/bcrypt@v0.4.1/mod.ts"; +import { v4 as uuidv4 } from 'uuid'; // internal imports import { Channel, PermissionsObject, User } from "../structures/structures.ts"; -import generateSnowflake from "../util/snowflake.ts"; import { getUserById, users } from "./users.ts"; // Data structure to store channels @@ -58,7 +58,7 @@ async function addUser( bcrypt .hash(password, salt) .then((hash: string) => { - const id = generateSnowflake(); + const id = uuidv4(); // Add user to database users.set( @@ -138,7 +138,7 @@ function createChannel( const channel = new Channel( name, description, - generateSnowflake(), + uuidv4(), user.Member ); diff --git a/src/routes/api.ts b/src/routes/api.ts index 3320911..2c64834 100644 --- a/src/routes/api.ts +++ b/src/routes/api.ts @@ -33,14 +33,22 @@ const communicator = new EventEmitter(); router.use(express.json()); router.use(auth); +// definition of the type of the request object +declare global { + namespace Express { + interface Request { + authenticated: boolean; + query: { username: string; password: string }; + } + } +} + + // API status endpoint router.get( "/", ( - request: Request & { - authenticated: boolean; - query: { username: string; password: string }; - }, + request: Request, res: Response ) => { const status = { diff --git a/src/routes/auth.ts b/src/routes/auth.ts index 7707edb..64f86e5 100644 --- a/src/routes/auth.ts +++ b/src/routes/auth.ts @@ -18,7 +18,6 @@ import { getUserByToken, } from "../db/db.ts"; import { getUserByEmail } from "../db/users.ts"; -import { type User } from "../structures/structures.ts"; dotenv.config(); @@ -50,44 +49,45 @@ router.get( ); // Login endpoint -router.post("/login/email", (request: Request, res: Response) => { +router.post("/login/email", async (request: Request, res: Response) => { const { username, password } = request.body; - getUserByEmail(username) - .then(async (user: User) => { - // Check if password is correct - if ((await checkPassword(password, user.hash)) == true) { - // Generate token - const token = jwt.sign( - { - username: user.username, - id: user.id, - permissions: user.permissions, - }, - process.env.JWT_SECRET, - { - expiresIn: "12h", - } - ); - - // Assign token to user - user.token = token; - - // Send token - res.status(200).json({ - token, - username: user.username, - id: user.id, - }); - } else { - res.status(401).json({ error: "Incorrect password" }); + const user = await getUserByEmail(username); + + if (!user) { + res.status(404).json({ error: "User not found" }); + } + + // Check if password is correct + if ((await checkPassword(password, user.hash)) == true) { + // Generate token + const token = jwt.sign( + { + username: user.username, + id: user.id, + permissions: user.permissions, + }, + process.env.JWT_SECRET, + { + expiresIn: "12h", } - }) - .catch((error) => { - console.log(error); + ); + + // Assign token to user + user.token = token; - return res.status(401).json({ error: "User does not exist" }); + // Send token + res.status(200).json({ + token: token, + username: user.username, + id: user.id, }); + + // finish request + return; + } else { + res.status(401).json({ error: "Incorrect password" }); + } }); // Register endpoint diff --git a/src/server.ts b/src/server.ts index 49c30b1..1079bfb 100755 --- a/src/server.ts +++ b/src/server.ts @@ -30,12 +30,11 @@ import { getUserById, users } from "./db/users.ts"; import { api, com } from "./routes/api.ts"; import { authRouter } from "./routes/auth.ts"; import socketHandler from "./socket.ts"; -import { type Channel, type User } from "./structures/structures.ts"; // check that we're running Node.js 18 or higher if (Number.parseInt(process.versions.node.split(".")[0]) < 18) { const err = new Error( - "Hammer requires Node.js 18 or higher to run. Please update your Node.js installation.", + "Hammer requires Node.js 18 or higher to run. Please update your Node.js installation." ); console.log(err); @@ -65,8 +64,10 @@ app.use(express.json()); // Listen for connections wss.on( "connection", - ( ws: WebSocket & { json: (data: unknown) => void } & EventEmitter, - request: http.IncomingMessage & { url: string } ) => { + ( + ws: WebSocket & { json: (data: unknown) => void } & EventEmitter, + request: http.IncomingMessage & { url: string } + ) => { const url = new URL(request.url, `http://${request.headers.host}`); const token = url.searchParams.get("token"); @@ -254,9 +255,6 @@ app.use("/app/login", express.static(path.join(__dirname, "./app/login.html"))); // set x-powered-by header setting app.disable("x-powered-by"); -// set x-powered-by header to Express -app.set("x-powered-by", "Express"); - // Start the server const listener = app.listen(port + 1, () => { // Print the banner @@ -275,12 +273,16 @@ const listener = app.listen(port + 1, () => { console.log("Server API is listening on http://localhost:" + ePort); }); +/* addUser("admin@disilla.org", "admin", "password", { ADMINISTRATOR: true, MANAGE_CHANNELS: true, MANAGE_MESSAGES: true, }) .then((user: User) => { + + console.log("Admin user created."); + createChannel( { name: "general", description: "The general channel" }, user.id @@ -307,3 +309,41 @@ addUser("admin@disilla.org", "admin", "password", { .catch((error) => { console.log(error); }); + */ + +(async () => { + const user = await addUser("admin@disilla.org", "admin", "password", { + ADMINISTRATOR: true, + MANAGE_CHANNELS: true, + MANAGE_MESSAGES: true, + }); + + const name = (await getUserById(user.id)).username; + if(name == "admin") { + console.log("Admin user created."); + } + + const channel = await createChannel( + { name: "general", description: "The general channel" }, + user.id + ); + + await addUserToChannel(channel.id, user.id); + com.emit("channelJoin", { user: user.id, channel: channel.id }); + + const nuser = await addUser("me@disilla.org", "chrono", "password", { + ADMINISTRATOR: false, + MANAGE_CHANNELS: false, + MANAGE_MESSAGES: false, + }); + + const name1 = (await getUserById(nuser.id)).username; + if(name1 == "chrono") { + console.log("Chrono user created."); + } + + await addUserToChannel(channel.id, nuser.id); + com.emit("channelJoin", { user: nuser.id, channel: channel.id }); + + console.log("Async/await test complete."); +})(); diff --git a/src/socket.ts b/src/socket.ts index 5ed3fb7..01d5997 100644 --- a/src/socket.ts +++ b/src/socket.ts @@ -1,5 +1,5 @@ /* - * Hammer - A simple WebSocket-based chat server & client written in JavaScript. + * Hammer - A simple WebSocket-based chat server & client written in TypeScript. * * Copyright (C) 2023 Hammer Authors */ -- 2.45.2