<commit_before>var express = require('express')
var router = express.Router()
var bodyParser = require('body-parser')
var DataModel = require('../dataModel')
var when = require('promised-io/promise').when
var errors = require('dactic/errors')
var UserModel = DataModel.get('user')
var generateToken = require('../generateToken')
var validateToken = require('../validateToken')
var bcrypt = require('bcrypt')

/* Basic Password auth */
router.post('/', [
  bodyParser.urlencoded({extended: true}),
  function (req, res, next) {
    if (!req.body.username || !req.body.password) {
      return next(new errors.Unauthorized('Missing Username or Password'))
    }

    when(UserModel.validatePassword(req.body.username, req.body.password), function (ruser) {
      var user = ruser.getData()
      if (user) {
        var token = generateToken(user, 'user')
        res.status(200)
        res.send(token)
        res.end()
      } else {
        next(errors.Unauthorized('Invalid username, email, or password'))
      }
    }, function (err) {
      next(errors.Unauthorized('Invalid username, email, or password'))
    })
  }
])

/* Superuser Login */
router.post('/sulogin', [
  bodyParser.urlencoded({extended: true}),
  function (req, res, next) {
    console.log('Authenticate User Token: ', req.body)

    if (!req.body.username || !req.body.password || !req.body.targetUser) {
      return next(new errors.Unauthorized('Missing Username, Password, or Target User'))
    }

    console.log('Get User: ', req.body.username)
    when(UserModel.get(req.body.username), function (ruser) {
      var user = ruser.getData()
      console.log('Got Login User: ', user)
      if (!user || !user.roles || (user.roles.length < 1) || (user.roles.indexOf('admin') < 0)) {
        return next(errors.Unauthorized())
      }

      bcrypt.compare(req.body.password, user.password, function (err, response) {
        console.log('Authenticated')
        if (err) { console.log('Invalid Password'); next(errors.Unauthorized(err)) }
        when(UserModel.get(req.body.targetUser), function (tres) {
          if (!tres) {
            return next(errors.NotAcceptable('Invalid Target User'))
          }
          var tuser = tres.getData()

          console.log('Target User: ', tuser)
          var token = generateToken(tuser, 'user')
          res.status(200)
          res.send(token)
          res.end()
        }, function () {
          next(errors.NotAcceptable('Invalid Target User'))
        })
      })
    })
  }
])

/* Refresh a valid token */
router.get('/refresh',
  function (req, res, next) {
    if (!req.user) {<commit_msg>fix issue<commit_after>var express = require('express')
var router = express.Router()
var bodyParser = require('body-parser')
var DataModel = require('../dataModel')
var when = require('promised-io/promise').when
var errors = require('dactic/errors')
var UserModel = DataModel.get('user')
var generateToken = require('../generateToken')
var validateToken = require('../validateToken')
var bcrypt = require('bcrypt')

/* Basic Password auth */
router.post('/', [
  bodyParser.urlencoded({extended: true}),
  function (req, res, next) {
    if (!req.body.username || !req.body.password) {
      return next(new errors.Unauthorized('Missing Username or Password'))
    }

    when(UserModel.validatePassword(req.body.username, req.body.password), function (ruser) {
      var user = ruser.getData()
      if (user) {
        var token = generateToken(user, 'user')
        res.status(200)
        res.send(token)
        res.end()
      } else {
        next(errors.Unauthorized('Invalid username, email, or password'))
      }
    }, function (err) {
      next(errors.Unauthorized('Invalid username, email, or password'))
    })
  }
])

/* Superuser Login */
router.post('/sulogin', [
  bodyParser.urlencoded({extended: true}),
  function (req, res, next) {

    if (!req.body.username || !req.body.password || !req.body.targetUser) {
      return next(new errors.Unauthorized('Missing Username, Password, or Target User'))
    }

    console.log('Get User: ', req.body.username)
    when(UserModel.get(req.body.username), function (ruser) {
      var user = ruser.getData()
      console.log('Got Login User: ', user)
      if (!user || !user.roles || (user.roles.length < 1) || (user.roles.indexOf('admin') < 0)) {
        return next(errors.Unauthorized())
      }

      bcrypt.compare(req.body.password, user.password, function (err, response) {
        console.log('Authenticated')
        if (err || !response) { 
		console.log('Invalid Password'); 
		return next(errors.Unauthorized("Invalid Password")) 
	}

        when(UserModel.get(req.body.targetUser), function (tres) {
          if (!tres) {
            return next(errors.NotAcceptable('Invalid Target User'))
          }
          var tuser = tres.getData()

          console.log('Target User: ', tuser)
          var token = generateToken(tuser, 'user')
          res.status(200)
          res.send(token)
          res.end()
        }, function () {
          next(errors.NotAcceptable('Invalid Target User'))
        })
      })
    })
  }
])

/* Refresh a valid token */
router.get('/refresh',
  function (req, res, next) {
    if (!req.user) {
{'fraction_non_alphanumeric': 0.1572926596758818, 'fraction_numerical': 0.003050524308865586, 'mean_word_length': 2.920777279521674, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2845677', 'n_tokens_mistral': 1597, 'n_tokens_neox': 1563, 'n_words': 346}