2018-04-13 16:20:57 +00:00
|
|
|
const config = require('./../config')
|
2018-01-23 20:06:30 +00:00
|
|
|
const db = require('knex')(config.database)
|
2018-10-13 11:06:58 +00:00
|
|
|
const perms = require('./permissionController')
|
2018-01-23 20:06:30 +00:00
|
|
|
const randomstring = require('randomstring')
|
2018-04-13 16:20:57 +00:00
|
|
|
const utils = require('./utilsController')
|
2017-01-17 19:54:25 +00:00
|
|
|
|
2019-06-18 21:04:14 +00:00
|
|
|
const TOKEN_LENGTH = 64
|
|
|
|
const UNIQUE_TOKEN_MAX_TRIES = 3
|
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
const tokenController = {}
|
2017-01-17 19:54:25 +00:00
|
|
|
|
2019-06-18 21:04:14 +00:00
|
|
|
tokenController.generateUniqueToken = () => {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
const query = async i => {
|
|
|
|
const token = randomstring.generate(TOKEN_LENGTH)
|
|
|
|
const user = await db.table('users').where('token', token).first().catch(() => undefined)
|
|
|
|
if (user === undefined) return resolve(token)
|
|
|
|
if (++i < UNIQUE_TOKEN_MAX_TRIES) return query(i)
|
|
|
|
resolve(null)
|
|
|
|
}
|
|
|
|
query(0)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-10-04 00:13:38 +00:00
|
|
|
tokenController.verify = async (req, res, next) => {
|
2018-01-23 20:06:30 +00:00
|
|
|
const token = req.body.token
|
2018-12-18 17:01:28 +00:00
|
|
|
if (token === undefined)
|
2018-10-09 19:52:41 +00:00
|
|
|
return res.status(401).json({
|
|
|
|
success: false,
|
|
|
|
description: 'No token provided.'
|
|
|
|
})
|
2017-01-17 19:54:25 +00:00
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
const user = await db.table('users').where('token', token).first()
|
2018-12-18 17:01:28 +00:00
|
|
|
if (!user)
|
2018-10-09 19:52:41 +00:00
|
|
|
return res.status(401).json({
|
|
|
|
success: false,
|
|
|
|
description: 'Invalid token.'
|
|
|
|
})
|
|
|
|
|
|
|
|
return res.json({
|
|
|
|
success: true,
|
|
|
|
username: user.username,
|
2018-10-13 11:06:58 +00:00
|
|
|
permissions: perms.mapPermissions(user)
|
2018-10-09 19:52:41 +00:00
|
|
|
})
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
2017-01-17 19:54:25 +00:00
|
|
|
|
2017-10-04 00:13:38 +00:00
|
|
|
tokenController.list = async (req, res, next) => {
|
2018-01-23 20:06:30 +00:00
|
|
|
const user = await utils.authorize(req, res)
|
2018-12-18 17:01:28 +00:00
|
|
|
if (!user) return
|
2018-10-09 19:52:41 +00:00
|
|
|
return res.json({
|
|
|
|
success: true,
|
|
|
|
token: user.token
|
|
|
|
})
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
2017-01-17 19:54:25 +00:00
|
|
|
|
2017-10-04 00:13:38 +00:00
|
|
|
tokenController.change = async (req, res, next) => {
|
2018-01-23 20:06:30 +00:00
|
|
|
const user = await utils.authorize(req, res)
|
2018-12-18 17:01:28 +00:00
|
|
|
if (!user) return
|
2017-01-18 07:51:42 +00:00
|
|
|
|
2019-06-18 21:04:14 +00:00
|
|
|
const newtoken = await tokenController.generateUniqueToken()
|
|
|
|
if (!newtoken)
|
|
|
|
return res.json({ success: false, description: 'Error generating unique token (╯°□°)╯︵ ┻━┻.' })
|
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
await db.table('users').where('token', user.token).update({
|
|
|
|
token: newtoken,
|
|
|
|
timestamp: Math.floor(Date.now() / 1000)
|
|
|
|
})
|
2019-06-18 21:04:14 +00:00
|
|
|
return res.json({
|
2018-10-09 19:52:41 +00:00
|
|
|
success: true,
|
|
|
|
token: newtoken
|
|
|
|
})
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
2017-01-18 07:51:42 +00:00
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
module.exports = tokenController
|