<commit_before>'use strict';

var UserModel = require('../../models/user-model');
var jwt = require('jwt-simple');

/**
 * This middleware determines if an incoming user's token is valid
 */

module.exports = function(app) {

	/**
	 * Define a jwtAuth object with--the important part--an auth method
	 *
	 * (The auth method is all that's passed into the REST router)
	 */

	var jwtAuth = {

		auth: function(req, res, next) {
			var token = req.headers.jwt || req.body.jwt; // The latter: for testing
			var decoded;

			/**
			 * Decode the token
			 */

			try {
				decoded = jwt.decode(token, app.get('jwtTokenSecret')); //  Note that changing the token secret makes this throw an error!
			} catch(err) {
				return res.status(401).json({ 'msg': 'access denied' });
			}

			/**
			 * Check for expired token
			 */

			if (decoded.exp <= Date.now()) {
			  res.status(400).json({ 'msg': 'Access token has expired' });
			}

			/**
			 * Look for the decoded user in the db
			 */

			UserModel.findOne({ '_id': decoded.iss }, function(err, user) {
				if (err) {
					console.log('error in finding user');
					return res.status(500).json(err);
				}
				if (!user) {
					return res.status(401).json({ 'msg': 'access denied' });
				}

				req.user = user;
				next(); // Invoke the subsequent middleware
			});
		}
	};

	return jwtAuth;
};<commit_msg>Add decode method to decode a provided JWT<commit_after>'use strict';

var UserModel = require('../../models/user-model');
var jwt = require('jwt-simple');

/**
 * This middleware determines if an incoming user's token is valid
 */

module.exports = function(app) {

	/**
	 * Define a jwtAuth object with--the important part--an auth method
	 *
	 * (The auth method is all that's passed into the REST router)
	 */

	var jwtAuth = {

		auth: function(req, res, next) {
			var token = req.headers.jwt || req.body.jwt; // The latter: for testing
			var decoded;

			/**
			 * Decode the token
			 */

			try {
				decoded = jwt.decode(token, app.get('jwtTokenSecret')); //  Note that changing the token secret makes this throw an error!
			} catch(err) {
				return res.status(401).json({ 'msg': 'access denied' });
			}

			/**
			 * Check for expired token
			 */

			if (decoded.exp <= Date.now()) {
			  res.status(400).json({ 'msg': 'Access token has expired' });
			}

			/**
			 * Look for the decoded user in the db
			 */

			UserModel.findOne({ '_id': decoded.iss }, function(err, user) {
				if (err) {
					console.log('error in finding user');
					return res.status(500).json(err);
				}
				if (!user) {
					return res.status(401).json({ 'msg': 'access denied' });
				}

				req.user = user;
				next(); // Invoke the subsequent middleware
			});
		},

		decode: function(token) {
			var decoded;

			try {
				decoded = jwt.decode(token, app.get('jwtTokenSecret'));
			} catch(err) {
				console.log('Could not decode jwt');
			}

			return decoded;
		}
	};

	return jwtAuth;
};
{'fraction_non_alphanumeric': 0.15678970538435488, 'fraction_numerical': 0.008127328140873687, 'mean_word_length': 2.6334563345633457, 'pattern_counts': {'":': 0, '<': 5, '<?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': '6773807', 'n_tokens_mistral': 1178, 'n_tokens_neox': 926, 'n_words': 282}