tokenController.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. const config = require('../config.js')
  2. const db = require('knex')(config.database)
  3. let tokenController = {}
  4. tokenController.verify = function(req, res, next){
  5. let type = req.body.type
  6. let token = req.body.token
  7. if(type === undefined) return res.json({ success: false, description: 'No type provided.' })
  8. if(token === undefined) return res.json({ success: false, description: 'No token provided.' })
  9. if(type !== 'client' && type !== 'admin') return res.json({ success: false, description: 'Wrong type provided.' })
  10. if(type === 'client'){
  11. if(token !== config.clientToken) return res.json({ success: false, description: 'Token mismatch.' })
  12. return res.json({ success: true })
  13. }
  14. if(type === 'admin'){
  15. if(token !== config.adminToken) return res.json({ success: false, description: 'Token mismatch.' })
  16. return res.json({ success: true })
  17. }
  18. return res.json({ success: false, description: '(╯°□°)╯︵ ┻━┻' })
  19. }
  20. tokenController.list = function(req, res, next){
  21. if(req.headers.auth !== config.adminToken)
  22. return res.status(401).json({ success: false, description: 'not-authorized'})
  23. return res.json({
  24. clientToken: config.clientToken,
  25. adminToken: config.adminToken
  26. })
  27. }
  28. tokenController.change = function(req, res, next){
  29. if(req.headers.auth !== config.adminToken)
  30. return res.status(401).json({ success: false, description: 'not-authorized'})
  31. let type = req.body.type
  32. let token = req.body.token
  33. if(type === undefined) return res.json({ success: false, description: 'No type provided.' })
  34. if(token === undefined) return res.json({ success: false, description: 'No token provided.' })
  35. if(type !== 'client' && type !== 'admin') return res.json({ success: false, description: 'Wrong type provided.' })
  36. db.table('tokens').where('name', type).update({ value: token, timestamp: Math.floor(Date.now() / 1000) })
  37. .then(() => {
  38. if(type === 'client')
  39. config.clientToken = token
  40. else if(type === 'admin')
  41. config.adminToken = token
  42. res.json({ success: true })
  43. }).catch(function(error) { console.log(error); res.json({success: false, description: 'error'}) })
  44. }
  45. module.exports = tokenController