<commit_before>// Load modules

var Boom = require('boom');
var Hoek = require('hoek');
var JWT  = require('jsonwebtoken');

// Declare internals >> see: http://hapijs.com/styleguide

var internals = {};

exports.register = function (server, options, next) {
  server.auth.scheme('jwt', internals.implementation);
  next();
};

// hapi requires attributes for a plugin. See: http://hapijs.com/tutorials/plugins
exports.register.attributes = {
    pkg: require('../package.json')
};

internals.implementation = function (server, options) {

  // pre-auth checks
  Hoek.assert(options, 'Missing JWT auth options');
  Hoek.assert(options.key, 'options does not contain secret key');
  Hoek.assert(typeof options.validateFunc === 'function', 'options.validateFunc MUST be a Valid Function');

  var scheme = {
    authenticate: function (request, reply) {

      var auth = request.headers.authorization;
      // console.log("AUTH: "+auth);
      if (!auth) {
        return reply(Boom.unauthorized(null, 'Token not present'));
      }
      else {
        // strip off the pointless "Bearer " label and any whitespace
        var token = auth.replace(/Bearer/gi,'').replace(/ /g,'');

        // rudimentary check for JWT validity
        if(token.split('.').length !== 3) {
          return reply(Boom.unauthorized('Invalid Token (JWT) format', 'Token'));
        } // see: https://github.com/docdis/learn-json-web-tokens for JWT format
        else {

          JWT.verify(token, options.key, function(err, decoded) {
            if (err) {
              return reply(Boom.unauthorized('Invalid Token', 'Token'));
            }
            else {
              // see: http://hapijs.com/tutorials/auth for validateFunc signature
              options.validateFunc(decoded, function (err, valid) {

                if (err || !valid) {
                  return reply(Boom.unauthorized('Invalid token', 'Token'), null, { credentials: "Please Login." });
                }
                // Authenticated
                return reply.continue({ credentials: "See Authorization Header!" });
                // the object containing a credentials property is required by Hapi...
              });
            }
          });
        }
      }
    }
  };

  return scheme;
};<commit_msg>unable to simulate errors. need to do a bit more reading...<commit_after>// Load modules

var Boom = require('boom');
var Hoek = require('hoek');
var JWT  = require('jsonwebtoken');

// Declare internals >> see: http://hapijs.com/styleguide

var internals = {};

exports.register = function (server, options, next) {
  server.auth.scheme('jwt', internals.implementation);
  next();
};

// hapi requires attributes for a plugin. See: http://hapijs.com/tutorials/plugins
exports.register.attributes = {
    pkg: require('../package.json')
};

internals.implementation = function (server, options) {

  // pre-auth checks
  Hoek.assert(options, 'Missing JWT auth options');
  Hoek.assert(options.key, 'options does not contain secret key');
  Hoek.assert(typeof options.validateFunc === 'function', 'options.validateFunc MUST be a Valid Function');

  var scheme = {
    authenticate: function (request, reply) {

      var auth = request.headers.authorization;
      // console.log("AUTH: "+auth);
      if (!auth) {
        return reply(Boom.unauthorized(null, 'Token not present'));
      }
      else {
        // strip off the pointless "Bearer " label and any whitespace
        var token = auth.replace(/Bearer/gi,'').replace(/ /g,'');

        // rudimentary check for JWT validity
        if(token.split('.').length !== 3) {
          return reply(Boom.unauthorized('Invalid Token (JWT) format', 'Token'));
        } // see: https://github.com/docdis/learn-json-web-tokens for JWT format
        else {

          JWT.verify(token, options.key, function(err, decoded) {
            if (err) {
              return reply(Boom.unauthorized('Invalid Token', 'Token'));
            }
            else {
              // see: http://hapijs.com/tutorials/auth for validateFunc signature
              options.validateFunc(decoded, function (err, valid) {
                // not much point validating the token at this point...
                // if (err || !valid) {
                //   return reply(Boom.unauthorized('Invalid token', 'Token'), null, { credentials: "Please Login." });
                // }
                // Authenticated
                return reply.continue({ credentials: "See Authorization Header!" });
                // the object containing a credentials property is required by Hapi...
              });
            }
          });
        }
      }
    }
  };

  return scheme;
};
{'fraction_non_alphanumeric': 0.1396588486140725, 'fraction_numerical': 0.00042643923240938164, 'mean_word_length': 2.2988748241912798, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '6107592', 'n_tokens_mistral': 1341, 'n_tokens_neox': 1287, 'n_words': 349}