filesafe/controllers/tokenController.js

59 lines
1.3 KiB
JavaScript
Raw Normal View History

2018-10-09 19:52:41 +00:00
const auth = require('./authController')
const config = require('./../config')
const db = require('knex')(config.database)
const randomstring = require('randomstring')
const utils = require('./utilsController')
2017-01-17 19:54:25 +00:00
const tokenController = {}
2017-01-17 19:54:25 +00:00
2017-10-04 00:13:38 +00:00
tokenController.verify = async (req, res, next) => {
const token = req.body.token
2018-10-09 19:52:41 +00:00
if (token === undefined) {
return res.status(401).json({
success: false,
description: 'No token provided.'
})
}
2017-01-17 19:54:25 +00:00
const user = await db.table('users').where('token', token).first()
2018-10-09 19:52:41 +00:00
if (!user) {
return res.status(401).json({
success: false,
description: 'Invalid token.'
})
}
return res.json({
success: true,
username: user.username,
permissions: auth.mapPermissions(user)
})
}
2017-01-17 19:54:25 +00:00
2017-10-04 00:13:38 +00:00
tokenController.list = async (req, res, next) => {
const user = await utils.authorize(req, res)
if (!user) { return }
2018-10-09 19:52:41 +00:00
return res.json({
success: true,
token: user.token
})
}
2017-01-17 19:54:25 +00:00
2017-10-04 00:13:38 +00:00
tokenController.change = async (req, res, next) => {
const user = await utils.authorize(req, res)
if (!user) { return }
const newtoken = randomstring.generate(64)
await db.table('users').where('token', user.token).update({
token: newtoken,
timestamp: Math.floor(Date.now() / 1000)
})
2017-10-04 00:13:38 +00:00
2018-10-09 19:52:41 +00:00
res.json({
success: true,
token: newtoken
})
}
module.exports = tokenController