102 lines
2.5 KiB
JavaScript
102 lines
2.5 KiB
JavaScript
|
import Database from 'better-sqlite3'
|
||
|
import express from 'express'
|
||
|
import crypto from 'crypto'
|
||
|
import dotenv from 'dotenv'
|
||
|
|
||
|
import { exec } from 'child_process'
|
||
|
|
||
|
dotenv.config()
|
||
|
|
||
|
const app = express()
|
||
|
const port = process.env.PORT
|
||
|
let valid = {}
|
||
|
let glauth = undefined
|
||
|
|
||
|
console.log(process.env)
|
||
|
|
||
|
dbInit()
|
||
|
|
||
|
app.use(express.urlencoded({
|
||
|
extended: true
|
||
|
}))
|
||
|
|
||
|
app.use(express.static('public'))
|
||
|
|
||
|
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`)
|
||
|
}
|