<commit_before>var debug   = require('debug')('api:controller:auth');
var jwt     = require('jwt-simple'),
    moment  = require('moment'),
    config  = require('config'),
    models  = require('../models');

function AuthController(UserModel) {
    this.model = UserModel;
}

AuthController.prototype.middlewareAuth = function(request, response, next) {
	var token = request.query.token || request.headers['x-access-token'];
	if(!token) {
		var err = new Error('Forbidden');
  	err.status = 403;
  	return next(err);
	}
	try {
  	var decoded = jwt.decode(token, config.get('jwtTokenSecret'));
  	var isExpired = moment(decoded.exp).isBefore(new Date());
    if(isExpired) {
        var err = new Error('Unauthorized');
        err.status = 401;
        return next(err);
    } else {
        request.user = decoded;
        next();
    }
	} catch(err) {
  	return next(err);
	}
};

AuthController.prototype.token = function(request, response, next) {
  var username = request.body.username;
  var password = request.body.password;

  if(!username || !password) {
    var err = new Error('Bad request');
    err.status = 400;
    return next(err);
  }

  this.model.findOne({ where: {username: username} })
    .then(function(data) {
      	if (data) {
            if (data.validPassword(password, data.password)) {

              var expires = moment().add(1, 'days').valueOf();
              var token = jwt.encode({
                id: data.id,
                username: data.username,
                exp: expires
              }, config.get('jwtTokenSecret'));

              response.json({
                token: token
              });

          	} else {
            	var err = new Error('Unauthorized');
            	err.status = 401;
            	next(err);
          	}
        } else {
          var err = new Error('Login inexistent');
          err.status = 404;
          next(err);
        }
    })
    .catch(next);
};

AuthController.prototype.middlewareLogging = function(request, response, next) {
	debug('LOGGING OK');
  next();
};

module.exports = function(UserModel) {
    return new AuthController(UserModel);
};<commit_msg>progress logging<commit_after>var debug   = require('debug')('api:controller:auth');
var jwt     = require('jwt-simple'),
    moment  = require('moment'),
    config  = require('config'),
    models  = require('../models');

function AuthController(UserModel) {
    this.model = UserModel;
}

AuthController.prototype.middlewareAuth = function(request, response, next) {
	var token = request.query.token || request.headers['x-access-token'];
	if(!token) {
		var err = new Error('Forbidden');
  	err.status = 403;
  	return next(err);
	}
	try {
  	var decoded = jwt.decode(token, config.get('jwtTokenSecret'));
  	var isExpired = moment(decoded.exp).isBefore(new Date());
    if(isExpired) {
        var err = new Error('Unauthorized');
        err.status = 401;
        return next(err);
    } else {
        request.user = decoded;
        next();
    }
	} catch(err) {
  	return next(err);
	}
};

AuthController.prototype.token = function(request, response, next) {
  var username = request.body.username;
  var password = request.body.password;

  if(!username || !password) {
    var err = new Error('Bad request');
    err.status = 400;
    return next(err);
  }

  this.model.findOne({ where: {username: username} })
    .then(function(data) {
      	if (data) {
            if (data.validPassword(password, data.password)) {

              var expires = moment().add(1, 'days').valueOf();
              var token = jwt.encode({
                id: data.id,
                username: data.username,
                exp: expires
              }, config.get('jwtTokenSecret'));

              response.json({
                token: token
              });

          	} else {
            	var err = new Error('Unauthorized');
            	err.status = 401;
            	next(err);
          	}
        } else {
          var err = new Error('Login inexistent');
          err.status = 404;
          next(err);
        }
    })
    .catch(next);
};

AuthController.prototype.middlewareLogging = function(request, response, next) {
  var fullUrl = request.protocol + '://' + request.get('host') + request.originalUrl;
	debug('LOGGING OK');
  debug('USER REQUEST: ' + request.user.username);
  debug('ENDPOINT REQUEST: ' + fullUrl);
  next();
};

module.exports = function(UserModel) {
    return new AuthController(UserModel);
};
{'fraction_non_alphanumeric': 0.14648829431438126, 'fraction_numerical': 0.007134894091415831, 'mean_word_length': 2.3353159851301117, 'pattern_counts': {'":': 0, '<': 3, '<?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': '749818', 'n_tokens_mistral': 1388, 'n_tokens_neox': 1327, 'n_words': 284}