<commit_before>var User = require('../models').User,
    restify = require('express-restify-mongoose');

/*
 * Middleware for all API requests that looks for a token
 * parameter and validates it against the list of authorized
 * users, returning an error if it's invalid.
 */
module.exports = function(req, res, next) {
  // TODO: Skip validation based on header if there's already a
  // user set meaning (meaning this is probably a Web request
  // that's already authed)

  var key = req.header('Authorization');

  if(typeof key === 'undefined') {
    return res.status(400).send('An API key is required.');
  }

  User.findByKey(key, function(err, user) {
    if(err) return next(err);

    if(user) {
      req.user = user;
      next();
    }
    else {
      res.status(401).send('Invalid API key.');
    }
  });
};<commit_msg>Use Web authentication for API, too<commit_after>var User = require('../models').User,
    restify = require('express-restify-mongoose');

/*
 * Middleware for all API requests that looks for a token
 * in the header and validates it against the list of authorized
 * users, returning an error if it's invalid.
 */
module.exports = function(req, res, next) {
  // Skip header authorization for users with an active passwordless
  // session
  if(typeof req.user !== 'undefined') {
    return next();
  }

  var key = req.header('Authorization');

  if(typeof key === 'undefined') {
    return res.status(400).send('An API key is required.');
  }

  User.findByKey(key, function(err, user) {
    if(err) return next(err);

    if(user) {
      req.user = user.id;
      next();
    }
    else {
      res.status(401).send('Invalid API key.');
    }
  });
};
{'fraction_non_alphanumeric': 0.13947990543735225, 'fraction_numerical': 0.0070921985815602835, 'mean_word_length': 3.0893719806763285, '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': '3039156', 'n_tokens_mistral': 523, 'n_tokens_neox': 508, 'n_words': 167}