From 9b234c44a82ca355b2cf81de5705bab6670ac68c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=CC=81=20F=2E=20Romaniello?= Date: Fri, 29 May 2015 08:52:14 -0300 Subject: [PATCH] set required defaults to true --- lib/index.js | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/lib/index.js b/lib/index.js index 1daa24b..08e6b8a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -3,6 +3,9 @@ var jwt = require('jsonwebtoken'); var UnauthorizedError = require('./UnauthorizedError'); function noQsMethod(options) { + var defaults = { required: true }; + options = xtend(defaults, options); + return function (socket) { var server = this; @@ -13,27 +16,27 @@ function noQsMethod(options) { Namespace.events.push('authenticated'); } } - - if(options.required){ - var auth_timeout = setTimeout(function () { - socket.disconnect('unauthorized'); - }, options.timeout || 5000); - } - + + 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); - } - + 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'); @@ -43,7 +46,7 @@ function noQsMethod(options) { server.server.sockets.emit('authenticated', socket); } }; - + if(options.additional_auth){ options.additional_auth(decoded, onSuccess, onError); }else{ @@ -56,6 +59,10 @@ function noQsMethod(options) { } function authorize(options, onConnection) { + if (!options.handshake) { + return noQsMethod(options); + } + var defaults = { success: function(data, accept){ if (data.request) { @@ -75,10 +82,6 @@ function authorize(options, onConnection) { var auth = xtend(defaults, options); - if (!options.handshake) { - return noQsMethod(options); - } - return function(data, accept){ var token, error; var req = data.request || data;