<commit_before>/**
 * Simple API Key authentication middleware
 */
module.exports = apikey;

/**
 * Auth Middleware
 */
function apikey (fn, realm) {
  realm = realm || 'api key';

  function apikey (req, res, next) {
    var key = findApi(req) || auth(req);

    if (! key)
      return unauthorized(res, realm);

    if (!! key.name)
      key = key.name;

    fn(key, function (err, doc) {
      if (!! err)
        return next(err);

      if (!! doc)
        return next();

      return unauthorized(res, realm);
    })
  }
}

/**
 * Respond with 401 "Unauthorized".
 *
 * @param {ServerResponse} res
 * @param {String} realm
 * @api private
 */

function unauthorized(res, realm) {
  res.statusCode = 401;
  res.setHeader('WWW-Authenticate', 'Basic realm="' + realm + '"');
  res.end('Unauthorized');
};

/**
 * Looks for apikey inside readers and query
 * @api private
 */
function findApi (req) {
  return
     req.headers['x-apikey']
  || req.headers['apikey']
  || req.headers['x-api']
  || req.headers['api']
  || req.param('apikey') //FIXME: this only works using express.js
  || req.param('api')
}

/**
 * Taken from [basic-auth](https://www.npmjs.org/package/basic-auth)
 *
 * modified to accept empty passwords
 */

/**
 * Parse the Authorization header field of `req`.
 *
 * @param {Request} req
 * @return {Object} with .name and .pass
 * @api public
 */
function auth (req) {
  req = req.req || req;

  var auth = req.headers.authorization;
  if (!auth) return;

  // malformed
  var parts = auth.split(' ');
  if ('basic' != parts[0].toLowerCase()) return;
  if (!parts[1]) return;
  auth = parts[1];

  // credentials
  auth = new Buffer(auth, 'base64').toString();
  auth = auth.match(/^([^:]+):(.*)$/); // password can be empty
  if (!auth) return;

  return { name: auth[1], pass: auth[2] };
};
<commit_msg>return the function<commit_after>/**
 * Simple API Key authentication middleware
 */
module.exports = apikey;

/**
 * Auth Middleware
 */
function apikey (fn, realm) {
  realm = realm || 'api key';

  return function (req, res, next) {
    var key = findApi(req) || auth(req);

    if (! key)
      return unauthorized(res, realm);

    if (!! key.name)
      key = key.name;

    fn(key, function (err, result) {
      if (!! err)
        return next(err);

      if (!! result)
        return next();

      return unauthorized(res, realm);
    });
  }
}

/**
 * Respond with 401 "Unauthorized".
 *
 * @param {ServerResponse} res
 * @param {String} realm
 * @api private
 */

function unauthorized(res, realm) {
  res.statusCode = 401;
  res.setHeader('WWW-Authenticate', 'Basic realm="' + realm + '"');
  res.end('Unauthorized');
};

/**
 * Looks for apikey inside readers and query
 * @api private
 */
function findApi (req) {
  return
     req.headers['x-apikey']
  || req.headers['apikey']
  || req.headers['x-api']
  || req.headers['api']
  || req.param('apikey') //FIXME: this only works using express.js
  || req.param('api')
}

/**
 * Taken from [basic-auth](https://www.npmjs.org/package/basic-auth)
 *
 * modified to accept empty passwords
 */

/**
 * Parse the Authorization header field of `req`.
 *
 * @param {Request} req
 * @return {Object} with .name and .pass
 * @api public
 */
function auth (req) {
  req = req.req || req;

  var auth = req.headers.authorization;
  if (!auth) return;

  // malformed
  var parts = auth.split(' ');
  if ('basic' != parts[0].toLowerCase()) return;
  if (!parts[1]) return;
  auth = parts[1];

  // credentials
  auth = new Buffer(auth, 'base64').toString();
  auth = auth.match(/^([^:]+):(.*)$/); // password can be empty
  if (!auth) return;

  return { name: auth[1], pass: auth[2] };
};

{'fraction_non_alphanumeric': 0.18258503401360543, 'fraction_numerical': 0.007074829931972789, 'mean_word_length': 2.991313789359392, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '2901670', 'n_tokens_mistral': 1268, 'n_tokens_neox': 1219, 'n_words': 338}