This repository has been archived on 2024-11-11. You can view files and clone it, but cannot push or open issues or pull requests.
socketio-jwt/lib/index.js

114 lines
2.8 KiB
JavaScript
Raw Normal View History

2013-06-05 13:38:33 +02:00
var xtend = require('xtend');
var jwt = require('jsonwebtoken');
var UnauthorizedError = require('./UnauthorizedError');
2014-01-14 12:30:39 +01:00
function noQsMethod(options) {
2014-01-13 22:41:10 +01:00
return function (socket) {
2014-01-14 12:30:39 +01:00
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');
}
}
2014-01-13 22:41:10 +01:00
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');
}
2014-01-14 21:44:03 +01:00
socket.decoded_token = decoded;
2014-01-13 22:41:10 +01:00
socket.emit('authenticated');
if (server.$emit) {
server.$emit('authenticated', socket);
} else {
server.server.sockets.emit('authenticated', socket);
}
2014-01-13 22:41:10 +01:00
});
});
};
}
function authorize(options, onConnection) {
var defaults = {
success: function(data, accept){
2014-06-03 13:12:07 +02:00
if (data.request) {
accept();
} else {
accept(null, true);
}
},
fail: function(error, data, accept){
2014-06-03 13:12:07 +02:00
if (data.request) {
accept(error);
2014-06-03 13:12:07 +02:00
} else {
accept(null, false);
}
}
};
2012-09-05 20:14:36 +02:00
2013-11-15 10:47:51 +01:00
var auth = xtend(defaults, options);
2013-06-30 21:06:21 +02:00
2014-01-14 12:30:39 +01:00
if (!options.handshake) {
return noQsMethod(options);
2014-01-13 22:41:10 +01:00
}
2012-09-05 20:14:36 +02:00
return function(data, accept){
var token, error;
var req = data.request || data;
var authorization_header = (req.headers || {}).authorization;
2014-06-03 13:12:07 +02:00
if (authorization_header) {
var parts = authorization_header.split(' ');
if (parts.length == 2) {
var scheme = parts[0],
credentials = parts[1];
2012-09-05 20:14:36 +02:00
if (/^Bearer$/i.test(scheme)) {
token = credentials;
}
} else {
error = new UnauthorizedError('credentials_bad_format', {
message: 'Format is Authorization: Bearer [token]'
});
return auth.fail(error, data, accept);
}
}
2014-06-03 13:12:07 +02:00
//get the token from query string
if (req._query && req._query.token) {
token = req._query.token;
}
2012-09-05 20:14:36 +02:00
if (!token) {
error = new UnauthorizedError('credentials_required', {
message: 'No Authorization header was found'
2012-09-05 20:14:36 +02:00
});
return auth.fail(error, data, accept);
}
jwt.verify(token, options.secret, options, function(err, decoded) {
if (err) {
error = new UnauthorizedError('invalid_token', err);
return auth.fail(error, data, accept);
}
2014-01-14 21:44:03 +01:00
data.decoded_token = decoded;
2012-09-05 20:14:36 +02:00
auth.success(data, accept);
2012-09-05 20:14:36 +02:00
});
};
}
exports.authorize = authorize;