New token shananigans

This commit is contained in:
Pitu 2017-01-29 22:06:52 -03:00
parent 116c04907e
commit 16164115aa

View File

@ -12,12 +12,13 @@ tokenController.verify = function(req, res, next){
if(user.length === 0) return res.json({ success: false, description: 'Token mismatch' }) if(user.length === 0) return res.json({ success: false, description: 'Token mismatch' })
return res.json({ success: true }) return res.json({ success: true })
}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) }) }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
} }
tokenController.list = function(req, res, next){ tokenController.list = function(req, res, next){
if(req.headers.auth === undefined) return res.json({ success: false, description: 'No token provided' }) let token = req.headers.token
let token = req.headers.auth if(token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
db.table('users').where('token', token).then((user) => { db.table('users').where('token', token).then((user) => {
if(user.length === 0) return res.json({ success: false, description: 'Token mismatch' }) if(user.length === 0) return res.json({ success: false, description: 'Token mismatch' })
@ -27,26 +28,19 @@ tokenController.list = function(req, res, next){
} }
tokenController.change = function(req, res, next){ tokenController.change = function(req, res, next){
if(req.headers.auth !== config.adminToken)
return res.status(401).json({ success: false, description: 'not-authorized'})
let type = req.body.type let token = req.headers.token
let token = req.body.token if(token === undefined) return res.status(401).json({ success: false, description: 'No token provided' })
if(type === undefined) return res.json({ success: false, description: 'No type provided.' }) let newtoken = randomstring.generate(64)
if(token === undefined) return res.json({ success: false, description: 'No token provided.' })
if(type !== 'client' && type !== 'admin') return res.json({ success: false, description: 'Wrong type provided.' })
db.table('tokens').where('name', type).update({ value: token, timestamp: Math.floor(Date.now() / 1000) }) db.table('users').where('token', token).update({
.then(() => { token: newtoken,
timestamp: Math.floor(Date.now() / 1000)
if(type === 'client') }).then((user) => {
config.clientToken = token res.json({ success: true, token: newtoken })
else if(type === 'admin')
config.adminToken = token
res.json({ success: true })
}).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) }) }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
} }
module.exports = tokenController module.exports = tokenController