2
1
mirror of https://github.com/Thream/socketio-jwt.git synced 2024-07-21 09:38:31 +02:00

add support for socket.io 1.0

This commit is contained in:
José F. Romaniello 2014-06-03 08:12:07 -03:00
parent 0577d07b47
commit e8380c10b8
2 changed files with 25 additions and 8 deletions

View File

@ -41,11 +41,19 @@ The previous approach uses a second roundtrip to send the jwt, there is a way yo
var io = require("socket.io")(server);
var socketioJwt = require("socketio-jwt");
// set authorization for socket.io
//// With socket.io < 1.0 ////
io.set('authorization', socketioJwt.authorize({
secret: 'your secret or public key',
handshake: true
}));
//////////////////////////////
//// With socket.io >= 1.0 ////
io.use(socketioJwt.authorize({
secret: 'your secret or public key',
handshake: true
}));
///////////////////////////////
io.on('connection', function (socket) {
console.log('hello! ', socket.handshake.decoded_token.name);

View File

@ -29,11 +29,19 @@ function noQsMethod(options) {
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();
} else {
accept(null, false);
}
}
};
var auth = xtend(defaults, options);
@ -45,8 +53,10 @@ function authorize(options, onConnection) {
return function(data, accept){
var token, error;
if (data.headers && data.headers.authorization) {
var parts = data.headers.authorization.split(' ');
var authorization_header = ((data.request || data).headers || {}).authorization;
if (authorization_header) {
var parts = authorization_header.split(' ');
if (parts.length == 2) {
var scheme = parts[0],
credentials = parts[1];
@ -62,9 +72,8 @@ function authorize(options, onConnection) {
}
}
if (data.query.token) {
token = data.query.token;
}
//get the token from query string
token = (data.request || data).query.token;
if (!token) {
error = new UnauthorizedError('credentials_required', {