set required defaults to true
This commit is contained in:
parent
9325691f29
commit
9b234c44a8
39
lib/index.js
39
lib/index.js
@ -3,6 +3,9 @@ var jwt = require('jsonwebtoken');
|
|||||||
var UnauthorizedError = require('./UnauthorizedError');
|
var UnauthorizedError = require('./UnauthorizedError');
|
||||||
|
|
||||||
function noQsMethod(options) {
|
function noQsMethod(options) {
|
||||||
|
var defaults = { required: true };
|
||||||
|
options = xtend(defaults, options);
|
||||||
|
|
||||||
return function (socket) {
|
return function (socket) {
|
||||||
var server = this;
|
var server = this;
|
||||||
|
|
||||||
@ -13,27 +16,27 @@ function noQsMethod(options) {
|
|||||||
Namespace.events.push('authenticated');
|
Namespace.events.push('authenticated');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(options.required){
|
if(options.required){
|
||||||
var auth_timeout = setTimeout(function () {
|
var auth_timeout = setTimeout(function () {
|
||||||
socket.disconnect('unauthorized');
|
socket.disconnect('unauthorized');
|
||||||
}, options.timeout || 5000);
|
}, options.timeout || 5000);
|
||||||
}
|
}
|
||||||
|
|
||||||
socket.on('authenticate', function (data) {
|
socket.on('authenticate', function (data) {
|
||||||
if(options.required){
|
if(options.required){
|
||||||
clearTimeout(auth_timeout);
|
clearTimeout(auth_timeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
jwt.verify(data.token, options.secret, options, function(err, decoded) {
|
jwt.verify(data.token, options.secret, options, function(err, decoded) {
|
||||||
var onError = function(){
|
var onError = function(){
|
||||||
return socket.disconnect('unauthorized');
|
return socket.disconnect('unauthorized');
|
||||||
};
|
};
|
||||||
|
|
||||||
if (err) {
|
if (err) {
|
||||||
onError();
|
onError();
|
||||||
}
|
}
|
||||||
|
|
||||||
var onSuccess = function(){
|
var onSuccess = function(){
|
||||||
socket.decoded_token = decoded;
|
socket.decoded_token = decoded;
|
||||||
socket.emit('authenticated');
|
socket.emit('authenticated');
|
||||||
@ -43,7 +46,7 @@ function noQsMethod(options) {
|
|||||||
server.server.sockets.emit('authenticated', socket);
|
server.server.sockets.emit('authenticated', socket);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if(options.additional_auth){
|
if(options.additional_auth){
|
||||||
options.additional_auth(decoded, onSuccess, onError);
|
options.additional_auth(decoded, onSuccess, onError);
|
||||||
}else{
|
}else{
|
||||||
@ -56,6 +59,10 @@ function noQsMethod(options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function authorize(options, onConnection) {
|
function authorize(options, onConnection) {
|
||||||
|
if (!options.handshake) {
|
||||||
|
return noQsMethod(options);
|
||||||
|
}
|
||||||
|
|
||||||
var defaults = {
|
var defaults = {
|
||||||
success: function(data, accept){
|
success: function(data, accept){
|
||||||
if (data.request) {
|
if (data.request) {
|
||||||
@ -75,10 +82,6 @@ function authorize(options, onConnection) {
|
|||||||
|
|
||||||
var auth = xtend(defaults, options);
|
var auth = xtend(defaults, options);
|
||||||
|
|
||||||
if (!options.handshake) {
|
|
||||||
return noQsMethod(options);
|
|
||||||
}
|
|
||||||
|
|
||||||
return function(data, accept){
|
return function(data, accept){
|
||||||
var token, error;
|
var token, error;
|
||||||
var req = data.request || data;
|
var req = data.request || data;
|
||||||
|
Reference in New Issue
Block a user