<commit_before>var xtend = require('xtend');
var jwt = require('jsonwebtoken');
var UnauthorizedError = require('./UnauthorizedError');

function noQsMethod(options) {
  return function (socket) {
    var server = this;

    if (!server.$emit) {
      //then is socket.io 1.0
      var Namespace = Object.getPrototypeOf(server.server.sockets).constructor;
      if (!~Namespace.events.indexOf('authenticated')) {
        Namespace.events.push('authenticated');
      }
    }

    var auth_timeout = setTimeout(function () {
      socket.disconnect('unauthorized');
    }, options.timeout || 5000);

    socket.on('authenticate', function (data) {
      clearTimeout(auth_timeout);
      jwt.verify(data.token, options.secret, options, function(err, decoded) {
        if (err) {
          return socket.disconnect('unauthorized');
        }

        socket.decoded_token = decoded;
        socket.emit('authenticated');
        if (server.$emit) {
          server.$emit('authenticated', socket);
        } else {
          server.server.sockets.emit('authenticated', socket);
        }
      });
    });

  };
}

function authorize(options, onConnection) {
  var defaults = {
    success: function(data, accept){
      if (data.request) {
        accept();
      } else {
        accept(null, true);
      }
    },
    fail: function(error, data, accept){
      if (data.request) {
        accept(error);
      } else {
        accept(null, false);
      }
    }<commit_msg>Added optional authentication and the ability to call another function to further validate the token<commit_after>var xtend = require('xtend');
var jwt = require('jsonwebtoken');
var UnauthorizedError = require('./UnauthorizedError');

function noQsMethod(options) {
  return function (socket) {
    var server = this;

    if (!server.$emit) {
      //then is socket.io 1.0
      var Namespace = Object.getPrototypeOf(server.server.sockets).constructor;
      if (!~Namespace.events.indexOf('authenticated')) {
        Namespace.events.push('authenticated');
      }
    }
	
	if(options.required){
		var auth_timeout = setTimeout(function () {
			socket.disconnect('unauthorized');
		}, options.timeout || 5000);
	}
	
    socket.on('authenticate', function (data) {
	  if(options.required){
		clearTimeout(auth_timeout);
	  }
	
      jwt.verify(data.token, options.secret, options, function(err, decoded) {
        var onError = function(){
          return socket.disconnect('unauthorized');
        };
        
        if (err) {
          onError();
        }
        
        var onSuccess = function(){
            socket.decoded_token = decoded;
            socket.emit('authenticated');
            if (server.$emit) {
              server.$emit('authenticated', socket);
            } else {
              server.server.sockets.emit('authenticated', socket);
            }
        };
        
        if(options.additional_auth){
          options.additional_auth(decoded, onSuccess, onError);
        }else{
          onSuccess();
        }
      });
    });

  };
}

function authorize(options, onConnection) {
  var defaults = {
    success: function(data, accept){
      if (data.request) {
        accept();
      } else {
        accept(null, true);
      }
    },
    fail: function(error, data, accept){
      if (data.request) {
        accept(error);
      } else {
        accept(null, false);
      }
    }
{'fraction_non_alphanumeric': 0.1355085243974133, 'fraction_numerical': 0.003527336860670194, 'mean_word_length': 2.3232421875, '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': '6664393', 'n_tokens_mistral': 1002, 'n_tokens_neox': 952, 'n_words': 193}