From 48ad801027226f9b4a85c17613bcee7e999ac5c3 Mon Sep 17 00:00:00 2001 From: BurnyLlama Date: Sat, 15 Jan 2022 19:27:18 +0100 Subject: [PATCH] Refactor --- .gitignore | 6 +- index.js | 104 ++++------------------------------ libs/database.js | 9 +++ libs/execawait.js | 12 ++++ package.json | 3 +- routes/auth.js | 68 ++++++++++++++++++++++ routes/routes.js | 9 +++ {public => static}/index.html | 0 8 files changed, 116 insertions(+), 95 deletions(-) create mode 100644 libs/database.js create mode 100644 libs/execawait.js create mode 100644 routes/auth.js create mode 100644 routes/routes.js rename {public => static}/index.html (100%) diff --git a/.gitignore b/.gitignore index 2920621..0a09a30 100644 --- a/.gitignore +++ b/.gitignore @@ -131,4 +131,8 @@ dist captcha.png -package-lock.json \ No newline at end of file +package-lock.json +pnpm-lock.yaml + + +auth.db \ No newline at end of file diff --git a/index.js b/index.js index b8c658f..411aa58 100644 --- a/index.js +++ b/index.js @@ -1,102 +1,22 @@ -import Database from 'better-sqlite3' -import express from 'express' -import crypto from 'crypto' import dotenv from 'dotenv' +import express from 'express' -import { exec } from 'child_process' +import ROUTES from './routes/routes.js' +import { dbInit } from './libs/database.js' dotenv.config() -const app = express() -const port = process.env.PORT -let valid = {} -let glauth = undefined - -console.log(process.env) +const APP = express() +const PORT = process.env.PORT ?? 12345 dbInit() -app.use(express.urlencoded({ - extended: true -})) +APP.use(express.urlencoded({ extended: true })) +APP.use('/static', express.static('static')) -app.use(express.static('public')) +APP.use('/', ROUTES) -app.post('/register', (req, res) => { - // Was input sent? - if(!req.body.username) - return(res.send("No username entered!")) - - if(!req.body.password) - return(res.send("No password entered!")) - - if(!req.body.password) - return(res.send("No captcha entered!")) - - // does the username match the requirements - if(!(/^(?=[a-zA-Z0-9]{2,20}$).*$/.test(req.body.username))) - return(res.send("Username does not match the requirements")) - - // is captcha valid - if(!valid[req.body.captcha]) - return(res.send("Invalid captcha!")) - - const captchaAge = Math.abs((valid[req.body.captcha].getTime() - new Date().getTime())/1000) - - if(captchaAge > 600) - return(res.send("Invalid captcha!")) - - // expire the captcha - delete valid[req.body.captcha] - - // Does user already exist? - if(glauth.prepare(`SELECT * FROM users WHERE name = ?`).get(req.body.username)) - return(res.send("User already exists")) - - // Create the user! - glauth.prepare(` - INSERT INTO users( - name, primarygroup, passsha256 - ) VALUES(?, 0, ?) - `).run(req.body.username, crypto.createHash('sha256').update(req.body.password).digest('hex')) - - console.log(`>>> User: ${req.body.username} was succesfully created!`) - res.send("Account registered!") - - res.end() -}) - -app.get('/captcha', async (req, res) => { - const captcha = crypto.randomBytes(3).toString('hex') - await execawait(`./captcha.sh ${captcha} > captcha.png`) - - // Make it valid for 10 minutes - valid[captcha] = new Date() - - // Send the captcha image - res.contentType('image/png'); - res.sendFile('captcha.png', { - root: './' - }); -}) - -app.listen(port); -console.log('Server started at http://localhost:' + port); - - -function execawait(cmd) { - return new Promise((resolve, reject) => { - exec(cmd, (error, stdout, stderr) => { - if (error) { - console.warn(error); - } - resolve(stdout ? stdout : stderr); - }); - });7 -} - -function dbInit() { - glauth = new Database(process.env.GLAUTH_DB, {}) - - console.log(`Loaded in GLAuth - users.db`) -} \ No newline at end of file +APP.listen( + PORT, + () => console.log('Server started at http://localhost:' + PORT) +) diff --git a/libs/database.js b/libs/database.js new file mode 100644 index 0000000..8df2312 --- /dev/null +++ b/libs/database.js @@ -0,0 +1,9 @@ +import SQLDatabase from 'better-sqlite3' + +export let glauth = undefined + +export function dbInit() { + glauth = new SQLDatabase(process.env.GLAUTH_DB, {}) + + console.log(`Loaded in GLAuth - users.db`) +} \ No newline at end of file diff --git a/libs/execawait.js b/libs/execawait.js new file mode 100644 index 0000000..4dc0927 --- /dev/null +++ b/libs/execawait.js @@ -0,0 +1,12 @@ +import { exec } from 'child_process' + +export default function execawait(cmd) { + return new Promise((resolve, reject) => { + exec(cmd, (error, stdout, stderr) => { + if (error) { + console.warn(error); + } + resolve(stdout ? stdout : stderr); + }); + }); +} diff --git a/package.json b/package.json index fdb07a0..1e1cb4a 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,6 @@ "dependencies": { "better-sqlite3": "^7.4.6", "dotenv": "^12.0.3", - "express": "^4.17.2", - "expresss": "^0.0.0" + "express": "^4.17.2" } } diff --git a/routes/auth.js b/routes/auth.js new file mode 100644 index 0000000..04813b4 --- /dev/null +++ b/routes/auth.js @@ -0,0 +1,68 @@ +import crypto from 'crypto' +import { Router } from 'express' +import execawait from '../libs/execawait.js' + +const AUTH = Router() +let valid = {} + + +AUTH.post('/register', (req, res) => { + // Was input sent? + if(!req.body.username) + return(res.send("No username entered!")) + + if(!req.body.password) + return(res.send("No password entered!")) + + if(!req.body.password) + return(res.send("No captcha entered!")) + + // does the username match the requirements + if(!(/^(?=[a-zA-Z0-9]{2,20}$).*$/.test(req.body.username))) + return(res.send("Username does not match the requirements")) + + // is captcha valid + if(!valid[req.body.captcha]) + return(res.send("Invalid captcha!")) + + const captchaAge = Math.abs((valid[req.body.captcha].getTime() - new Date().getTime())/1000) + + if(captchaAge > 600) + return(res.send("Invalid captcha!")) + + // expire the captcha + delete valid[req.body.captcha] + + // Does user already exist? + if(glauth.prepare(`SELECT * FROM users WHERE name = ?`).get(req.body.username)) + return(res.send("User already exists")) + + // Create the user! + glauth.prepare(` + INSERT INTO users( + name, primarygroup, passsha256 + ) VALUES(?, 0, ?) + `).run(req.body.username, crypto.createHash('sha256').update(req.body.password).digest('hex')) + + console.log(`>>> User: ${req.body.username} was succesfully created!`) + res.send("Account registered!") + + res.end() +}) + +AUTH.get('/captcha', async (req, res) => { + const captcha = crypto.randomBytes(3).toString('hex') + await execawait(`./captcha.sh ${captcha} > captcha.png`) + + // Make it valid for 10 minutes + valid[captcha] = new Date() + + // Send the captcha image + res.contentType('image/png'); + res.sendFile('captcha.png', { + root: './' + }); +}) + + +export default AUTH \ No newline at end of file diff --git a/routes/routes.js b/routes/routes.js new file mode 100644 index 0000000..7f7da21 --- /dev/null +++ b/routes/routes.js @@ -0,0 +1,9 @@ +import { Router } from 'express' +import AUTH from './auth.js' + +const ROUTES = Router() + +ROUTES.get('/', (_, res) => res.send("Welcome!")) +ROUTES.use('/auth', AUTH) + +export default ROUTES \ No newline at end of file diff --git a/public/index.html b/static/index.html similarity index 100% rename from public/index.html rename to static/index.html