<commit_before>var fs = require('fs');
var jwt = require('jsonwebtoken');
var store = require('../store');

module.exports = function(publicKeyPath, options) {
  var publicKey = fs.readFileSync(publicKeyPath, 'utf8');
  return function authenticate(req, res, next) {
    if (!req.session) {
      throw new Error('Session is not available. Confirm the server setting.');
    } else if (req.session.identity) {
      next();
    } else {
      var token = req.query.auth_token;
      if (token) {
        console.log('token=', token);
        store.get('agentid', function(err, agent_id) {
          options.audience = agent_id
          jwt.verify(token, publicKey, options, function(err, identity) {
            if (err) {
              return res.status(401).render('../views/error', {message:err.message});
            }
            console.log('Verified identity=', identity);
            req.session.identity = identity;
            store.get('user_id', function(err, user_id) {
              if(user_id != identity.sub) {
                res.status(401).render('../views/error', {message:'Unauthorized: userId does not match.'});
              }
              res.redirect(req.path);
            });
          });
        });
      } else {
        res.status(401).send('Unauthorized');
      }
    }
  };
};
<commit_msg>Fixed bug: Error: Can't set headers after they are sent.<commit_after>var fs = require('fs');
var jwt = require('jsonwebtoken');
var store = require('../store');

module.exports = function(publicKeyPath, options) {
  var publicKey = fs.readFileSync(publicKeyPath, 'utf8');
  return function authenticate(req, res, next) {
    if (!req.session) {
      throw new Error('Session is not available. Confirm the server setting.');
    } else if (req.session.identity) {
      next();
    } else {
      var token = req.query.auth_token;
      if (token) {
        console.log('token=', token);
        store.get('agentid', function(err, agent_id) {
          options.audience = agent_id
          jwt.verify(token, publicKey, options, function(err, identity) {
            if (err) {
              return res.status(401).render('../views/error', {message:err.message});
            }
            console.log('Verified identity=', identity);
            req.session.identity = identity;
            store.get('user_id', function(err, user_id) {
              if(user_id != identity.sub) {
                res.status(401).render('../views/error', {message:'Unauthorized: userId does not match.'});
              }else{
                res.redirect(req.path);
              }
            });
          });
        });
      } else {
        res.status(401).send('Unauthorized');
      }
    }
  };
};

{'fraction_non_alphanumeric': 0.1514705882352941, 'fraction_numerical': 0.007352941176470588, 'mean_word_length': 2.1639534883720932, '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': '1911259', 'n_tokens_mistral': 796, 'n_tokens_neox': 781, 'n_words': 168}